]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_parse.c
d7e0f0f6ec2e1229876b9813de2eddfc7413351c
[divverent/darkplaces.git] / cl_parse.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_parse.c  -- parse a message received from the server
21
22 #include "quakedef.h"
23 #include "cdaudio.h"
24 #include "cl_collision.h"
25 #include "csprogs.h"
26
27 char *svc_strings[128] =
28 {
29         "svc_bad",
30         "svc_nop",
31         "svc_disconnect",
32         "svc_updatestat",
33         "svc_version",          // [int] server version
34         "svc_setview",          // [short] entity number
35         "svc_sound",                    // <see code>
36         "svc_time",                     // [float] server time
37         "svc_print",                    // [string] null terminated string
38         "svc_stufftext",                // [string] stuffed into client's console buffer
39                                                 // the string should be \n terminated
40         "svc_setangle",         // [vec3] set the view angle to this absolute value
41
42         "svc_serverinfo",               // [int] version
43                                                 // [string] signon string
44                                                 // [string]..[0]model cache [string]...[0]sounds cache
45                                                 // [string]..[0]item cache
46         "svc_lightstyle",               // [byte] [string]
47         "svc_updatename",               // [byte] [string]
48         "svc_updatefrags",      // [byte] [short]
49         "svc_clientdata",               // <shortbits + data>
50         "svc_stopsound",                // <see code>
51         "svc_updatecolors",     // [byte] [byte]
52         "svc_particle",         // [vec3] <variable>
53         "svc_damage",                   // [byte] impact [byte] blood [vec3] from
54
55         "svc_spawnstatic",
56         "OBSOLETE svc_spawnbinary",
57         "svc_spawnbaseline",
58
59         "svc_temp_entity",              // <variable>
60         "svc_setpause",
61         "svc_signonnum",
62         "svc_centerprint",
63         "svc_killedmonster",
64         "svc_foundsecret",
65         "svc_spawnstaticsound",
66         "svc_intermission",
67         "svc_finale",                   // [string] music [string] text
68         "svc_cdtrack",                  // [byte] track [byte] looptrack
69         "svc_sellscreen",
70         "svc_cutscene",
71         "svc_showlmp",  // [string] iconlabel [string] lmpfile [short] x [short] y
72         "svc_hidelmp",  // [string] iconlabel
73         "svc_skybox", // [string] skyname
74         "", // 38
75         "", // 39
76         "", // 40
77         "", // 41
78         "", // 42
79         "", // 43
80         "", // 44
81         "", // 45
82         "", // 46
83         "", // 47
84         "", // 48
85         "", // 49
86         "svc_cgame", //                         50              // [short] length [bytes] data
87         "svc_updatestatubyte", //                       51              // [byte] stat [byte] value
88         "svc_effect", //                        52              // [vector] org [byte] modelindex [byte] startframe [byte] framecount [byte] framerate
89         "svc_effect2", //                       53              // [vector] org [short] modelindex [short] startframe [byte] framecount [byte] framerate
90         "svc_sound2", //                        54              // short soundindex instead of byte
91         "svc_spawnbaseline2", //        55              // short modelindex instead of byte
92         "svc_spawnstatic2", //          56              // short modelindex instead of byte
93         "svc_entities", //                      57              // [int] deltaframe [int] thisframe [float vector] eye [variable length] entitydata
94         "svc_csqcentities", //          58              // [short] entnum [variable length] entitydata ... [short] 0x0000
95         "svc_spawnstaticsound2", //     59              // [coord3] [short] samp [byte] vol [byte] aten
96 };
97
98 char *qw_svc_strings[128] =
99 {
100         "qw_svc_bad",                                   // 0
101         "qw_svc_nop",                                   // 1
102         "qw_svc_disconnect",                    // 2
103         "qw_svc_updatestat",                    // 3    // [byte] [byte]
104         "",                                                             // 4
105         "qw_svc_setview",                               // 5    // [short] entity number
106         "qw_svc_sound",                                 // 6    // <see code>
107         "",                                                             // 7
108         "qw_svc_print",                                 // 8    // [byte] id [string] null terminated string
109         "qw_svc_stufftext",                             // 9    // [string] stuffed into client's console buffer
110         "qw_svc_setangle",                              // 10   // [angle3] set the view angle to this absolute value
111         "qw_svc_serverdata",                    // 11   // [long] protocol ...
112         "qw_svc_lightstyle",                    // 12   // [byte] [string]
113         "",                                                             // 13
114         "qw_svc_updatefrags",                   // 14   // [byte] [short]
115         "",                                                             // 15
116         "qw_svc_stopsound",                             // 16   // <see code>
117         "",                                                             // 17
118         "",                                                             // 18
119         "qw_svc_damage",                                // 19
120         "qw_svc_spawnstatic",                   // 20
121         "",                                                             // 21
122         "qw_svc_spawnbaseline",                 // 22
123         "qw_svc_temp_entity",                   // 23   // variable
124         "qw_svc_setpause",                              // 24   // [byte] on / off
125         "",                                                             // 25
126         "qw_svc_centerprint",                   // 26   // [string] to put in center of the screen
127         "qw_svc_killedmonster",                 // 27
128         "qw_svc_foundsecret",                   // 28
129         "qw_svc_spawnstaticsound",              // 29   // [coord3] [byte] samp [byte] vol [byte] aten
130         "qw_svc_intermission",                  // 30           // [vec3_t] origin [vec3_t] angle
131         "qw_svc_finale",                                // 31           // [string] text
132         "qw_svc_cdtrack",                               // 32           // [byte] track
133         "qw_svc_sellscreen",                    // 33
134         "qw_svc_smallkick",                             // 34           // set client punchangle to 2
135         "qw_svc_bigkick",                               // 35           // set client punchangle to 4
136         "qw_svc_updateping",                    // 36           // [byte] [short]
137         "qw_svc_updateentertime",               // 37           // [byte] [float]
138         "qw_svc_updatestatlong",                // 38           // [byte] [long]
139         "qw_svc_muzzleflash",                   // 39           // [short] entity
140         "qw_svc_updateuserinfo",                // 40           // [byte] slot [long] uid
141         "qw_svc_download",                              // 41           // [short] size [size bytes]
142         "qw_svc_playerinfo",                    // 42           // variable
143         "qw_svc_nails",                                 // 43           // [byte] num [48 bits] xyzpy 12 12 12 4 8
144         "qw_svc_chokecount",                    // 44           // [byte] packets choked
145         "qw_svc_modellist",                             // 45           // [strings]
146         "qw_svc_soundlist",                             // 46           // [strings]
147         "qw_svc_packetentities",                // 47           // [...]
148         "qw_svc_deltapacketentities",   // 48           // [...]
149         "qw_svc_maxspeed",                              // 49           // maxspeed change, for prediction
150         "qw_svc_entgravity",                    // 50           // gravity change, for prediction
151         "qw_svc_setinfo",                               // 51           // setinfo on a client
152         "qw_svc_serverinfo",                    // 52           // serverinfo
153         "qw_svc_updatepl",                              // 53           // [byte] [byte]
154 };
155
156 //=============================================================================
157
158 cvar_t demo_nehahra = {0, "demo_nehahra", "0", "reads all quake demos as nehahra movie protocol"};
159 cvar_t developer_networkentities = {0, "developer_networkentities", "0", "prints received entities, value is 0-4 (higher for more info)"};
160
161 static qboolean QW_CL_CheckOrDownloadFile(const char *filename);
162 static void QW_CL_RequestNextDownload(void);
163 static void QW_CL_NextUpload(void);
164 void QW_CL_StartUpload(unsigned char *data, int size);
165 //static qboolean QW_CL_IsUploading(void);
166 static void QW_CL_StopUpload(void);
167
168 /*
169 ==================
170 CL_ParseStartSoundPacket
171 ==================
172 */
173 void CL_ParseStartSoundPacket(int largesoundindex)
174 {
175         vec3_t  pos;
176         int     channel, ent;
177         int     sound_num;
178         int     volume;
179         int     field_mask;
180         float   attenuation;
181
182         if (cls.protocol == PROTOCOL_QUAKEWORLD)
183         {
184                 channel = MSG_ReadShort();
185
186                 if (channel & (1<<15))
187                         volume = MSG_ReadByte ();
188                 else
189                         volume = DEFAULT_SOUND_PACKET_VOLUME;
190
191                 if (channel & (1<<14))
192                         attenuation = MSG_ReadByte () / 64.0;
193                 else
194                         attenuation = DEFAULT_SOUND_PACKET_ATTENUATION;
195
196                 ent = (channel>>3)&1023;
197                 channel &= 7;
198
199                 sound_num = MSG_ReadByte ();
200         }
201         else
202         {
203                 field_mask = MSG_ReadByte();
204
205                 if (field_mask & SND_VOLUME)
206                         volume = MSG_ReadByte ();
207                 else
208                         volume = DEFAULT_SOUND_PACKET_VOLUME;
209
210                 if (field_mask & SND_ATTENUATION)
211                         attenuation = MSG_ReadByte () / 64.0;
212                 else
213                         attenuation = DEFAULT_SOUND_PACKET_ATTENUATION;
214
215                 if (field_mask & SND_LARGEENTITY)
216                 {
217                         ent = (unsigned short) MSG_ReadShort ();
218                         channel = MSG_ReadByte ();
219                 }
220                 else
221                 {
222                         channel = (unsigned short) MSG_ReadShort ();
223                         ent = channel >> 3;
224                         channel &= 7;
225                 }
226
227                 if (largesoundindex || field_mask & SND_LARGESOUND)
228                         sound_num = (unsigned short) MSG_ReadShort ();
229                 else
230                         sound_num = MSG_ReadByte ();
231         }
232
233         MSG_ReadVector(pos, cls.protocol);
234
235         if (sound_num >= MAX_SOUNDS)
236         {
237                 Con_Printf("CL_ParseStartSoundPacket: sound_num (%i) >= MAX_SOUNDS (%i)\n", sound_num, MAX_SOUNDS);
238                 return;
239         }
240
241         if (ent >= MAX_EDICTS)
242         {
243                 Con_Printf("CL_ParseStartSoundPacket: ent = %i", ent);
244                 return;
245         }
246
247         S_StartSound (ent, channel, cl.sound_precache[sound_num], pos, volume/255.0f, attenuation);
248 }
249
250 /*
251 ==================
252 CL_KeepaliveMessage
253
254 When the client is taking a long time to load stuff, send keepalive messages
255 so the server doesn't disconnect.
256 ==================
257 */
258
259 static unsigned char olddata[NET_MAXMESSAGE];
260 void CL_KeepaliveMessage (void)
261 {
262         float time;
263         static double nextmsg = -1;
264         int oldreadcount;
265         qboolean oldbadread;
266         sizebuf_t old;
267
268         // no need if server is local and definitely not if this is a demo
269         if (sv.active || !cls.netcon || cls.protocol == PROTOCOL_QUAKEWORLD)
270                 return;
271
272 // read messages from server, should just be nops
273         oldreadcount = msg_readcount;
274         oldbadread = msg_badread;
275         old = net_message;
276         memcpy(olddata, net_message.data, net_message.cursize);
277
278         NetConn_ClientFrame();
279
280         msg_readcount = oldreadcount;
281         msg_badread = oldbadread;
282         net_message = old;
283         memcpy(net_message.data, olddata, net_message.cursize);
284
285         if (cls.netcon && (time = Sys_DoubleTime()) >= nextmsg)
286         {
287                 sizebuf_t       msg;
288                 unsigned char           buf[4];
289                 nextmsg = time + 5;
290                 // write out a nop
291                 // LordHavoc: must use unreliable because reliable could kill the sigon message!
292                 Con_Print("--> client to server keepalive\n");
293                 memset(&msg, 0, sizeof(msg));
294                 msg.data = buf;
295                 msg.maxsize = sizeof(buf);
296                 MSG_WriteChar(&msg, svc_nop);
297                 NetConn_SendUnreliableMessage(cls.netcon, &msg, cls.protocol);
298         }
299 }
300
301 void CL_ParseEntityLump(char *entdata)
302 {
303         const char *data;
304         char key[128], value[MAX_INPUTLINE];
305         FOG_clear(); // LordHavoc: no fog until set
306         // LordHavoc: default to the map's sky (q3 shader parsing sets this)
307         R_SetSkyBox(cl.worldmodel->brush.skybox);
308         data = entdata;
309         if (!data)
310                 return;
311         if (!COM_ParseToken(&data, false))
312                 return; // error
313         if (com_token[0] != '{')
314                 return; // error
315         while (1)
316         {
317                 if (!COM_ParseToken(&data, false))
318                         return; // error
319                 if (com_token[0] == '}')
320                         break; // end of worldspawn
321                 if (com_token[0] == '_')
322                         strlcpy (key, com_token + 1, sizeof (key));
323                 else
324                         strlcpy (key, com_token, sizeof (key));
325                 while (key[strlen(key)-1] == ' ') // remove trailing spaces
326                         key[strlen(key)-1] = 0;
327                 if (!COM_ParseToken(&data, false))
328                         return; // error
329                 strlcpy (value, com_token, sizeof (value));
330                 if (!strcmp("sky", key))
331                         R_SetSkyBox(value);
332                 else if (!strcmp("skyname", key)) // non-standard, introduced by QuakeForge... sigh.
333                         R_SetSkyBox(value);
334                 else if (!strcmp("qlsky", key)) // non-standard, introduced by QuakeLives (EEK)
335                         R_SetSkyBox(value);
336                 else if (!strcmp("fog", key))
337                         sscanf(value, "%f %f %f %f", &fog_density, &fog_red, &fog_green, &fog_blue);
338                 else if (!strcmp("fog_density", key))
339                         fog_density = atof(value);
340                 else if (!strcmp("fog_red", key))
341                         fog_red = atof(value);
342                 else if (!strcmp("fog_green", key))
343                         fog_green = atof(value);
344                 else if (!strcmp("fog_blue", key))
345                         fog_blue = atof(value);
346         }
347 }
348
349 static qboolean QW_CL_CheckOrDownloadFile(const char *filename)
350 {
351         qfile_t *file;
352
353         // see if the file already exists
354         file = FS_Open(filename, "rb", true, false);
355         if (file)
356         {
357                 FS_Close(file);
358                 return true;
359         }
360
361         // download messages in a demo would be bad
362         if (cls.demorecording)
363         {
364                 Con_Printf("Unable to download \"%s\" when recording.\n", filename);
365                 return true;
366         }
367
368         // don't try to download when playing a demo
369         if (!cls.netcon)
370                 return true;
371
372         strlcpy(cls.qw_downloadname, filename, sizeof(cls.qw_downloadname));
373         Con_Printf("Downloading %s\n", filename);
374
375         if (!cls.qw_downloadmemory)
376         {
377                 cls.qw_downloadmemory = NULL;
378                 cls.qw_downloadmemorycursize = 0;
379                 cls.qw_downloadmemorymaxsize = 1024*1024; // start out with a 1MB buffer
380         }
381
382         MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
383         MSG_WriteString(&cls.netcon->message, va("download %s", filename));
384
385         cls.qw_downloadnumber++;
386         cls.qw_downloadpercent = 0;
387
388         return false;
389 }
390
391 static void QW_CL_RequestNextDownload(void)
392 {
393         int i;
394
395         // clear name of file that just finished
396         cls.qw_downloadname[0] = 0;
397
398         switch (cls.qw_downloadtype)
399         {
400         case dl_single:
401                 break;
402         case dl_skin:
403                 // TODO
404                 break;
405         case dl_model:
406                 if (cls.qw_downloadnumber == 0)
407                 {
408                         Con_Printf("Checking models...\n");
409                         cls.qw_downloadnumber = 1;
410                 }
411
412                 cls.qw_downloadtype = dl_model;
413                 for (;cls.qw_downloadnumber < MAX_MODELS && cl.model_name[cls.qw_downloadnumber][0];cls.qw_downloadnumber++)
414                 {
415                         // skip submodels
416                         if (cl.model_name[cls.qw_downloadnumber][0] == '*')
417                                 continue;
418                         if (!strcmp(cl.model_name[cls.qw_downloadnumber], "progs/spike.mdl"))
419                                 cl.qw_modelindex_spike = cls.qw_downloadnumber;
420                         if (!strcmp(cl.model_name[cls.qw_downloadnumber], "progs/player.mdl"))
421                                 cl.qw_modelindex_player = cls.qw_downloadnumber;
422                         if (!strcmp(cl.model_name[cls.qw_downloadnumber], "progs/flag.mdl"))
423                                 cl.qw_modelindex_flag = cls.qw_downloadnumber;
424                         if (!strcmp(cl.model_name[cls.qw_downloadnumber], "progs/s_explod.spr"))
425                                 cl.qw_modelindex_s_explod = cls.qw_downloadnumber;
426                         // check if we need to download the file, and return if so
427                         if (!QW_CL_CheckOrDownloadFile(cl.model_name[cls.qw_downloadnumber]))
428                                 return;
429                 }
430
431                 // touch all of the precached models that are still loaded so we can free
432                 // anything that isn't needed
433                 Mod_ClearUsed();
434                 for (i = 1;i < MAX_MODELS && cl.model_name[i][0];i++)
435                         Mod_FindName(cl.model_name[i]);
436                 // precache any models used by the client (this also marks them used)
437                 cl.model_bolt = Mod_ForName("progs/bolt.mdl", false, false, false);
438                 cl.model_bolt2 = Mod_ForName("progs/bolt2.mdl", false, false, false);
439                 cl.model_bolt3 = Mod_ForName("progs/bolt3.mdl", false, false, false);
440                 cl.model_beam = Mod_ForName("progs/beam.mdl", false, false, false);
441                 Mod_PurgeUnused();
442
443                 // now we try to load everything that is new
444
445                 // world model
446                 cl.model_precache[1] = Mod_ForName(cl.model_name[1], false, false, true);
447                 if (cl.model_precache[1]->Draw == NULL)
448                         Con_Printf("Map %s could not be found or downloaded\n", cl.model_name[1]);
449
450                 // normal models
451                 for (i = 2;i < MAX_MODELS && cl.model_name[i][0];i++)
452                         if ((cl.model_precache[i] = Mod_ForName(cl.model_name[i], false, false, false))->Draw == NULL)
453                                 Con_Printf("Model %s could not be found or downloaded\n", cl.model_name[i]);
454
455                 // done checking sounds and models, send a prespawn command now
456                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
457                 MSG_WriteString(&cls.netcon->message, va("prespawn %i 0 %i", cl.qw_servercount, cl.worldmodel ? cl.worldmodel->brush.qw_md4sum2 : 0));
458
459                 if (cls.qw_downloadmemory)
460                 {
461                         Mem_Free(cls.qw_downloadmemory);
462                         cls.qw_downloadmemory = NULL;
463                 }
464                 break;
465         case dl_sound:
466                 if (cls.qw_downloadnumber == 0)
467                 {
468                         Con_Printf("Checking sounds...\n");
469                         cls.qw_downloadnumber = 1;
470                 }
471
472                 cls.qw_downloadtype = dl_sound;
473                 for (;cl.sound_name[cls.qw_downloadnumber][0];cls.qw_downloadnumber++)
474                 {
475                         // skip subsounds
476                         if (cl.sound_name[cls.qw_downloadnumber][0] == '*')
477                                 continue;
478                         // check if we need to download the file, and return if so
479                         if (!QW_CL_CheckOrDownloadFile(va("sound/%s", cl.sound_name[cls.qw_downloadnumber])))
480                                 return;
481                 }
482
483                 // load new sounds and unload old ones
484                 // FIXME: S_ServerSounds does not know about cl.sfx_ sounds
485                 S_ServerSounds(cl.sound_name, cls.qw_downloadnumber);
486
487                 // precache any sounds used by the client
488                 cl.sfx_wizhit = S_PrecacheSound("sound/wizard/hit.wav", false, true);
489                 cl.sfx_knighthit = S_PrecacheSound("sound/hknight/hit.wav", false, true);
490                 cl.sfx_tink1 = S_PrecacheSound("sound/weapons/tink1.wav", false, true);
491                 cl.sfx_ric1 = S_PrecacheSound("sound/weapons/ric1.wav", false, true);
492                 cl.sfx_ric2 = S_PrecacheSound("sound/weapons/ric2.wav", false, true);
493                 cl.sfx_ric3 = S_PrecacheSound("sound/weapons/ric3.wav", false, true);
494                 cl.sfx_r_exp3 = S_PrecacheSound("sound/weapons/r_exp3.wav", false, true);
495
496                 // sounds
497                 for (i = 1;i < MAX_SOUNDS && cl.sound_name[i][0];i++)
498                 {
499                         // Don't lock the sfx here, S_ServerSounds already did that
500                         cl.sound_precache[i] = S_PrecacheSound(cl.sound_name[i], true, false);
501                 }
502
503                 // done with sound downloads, next we check models
504                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
505                 MSG_WriteString(&cls.netcon->message, va("modellist %i %i", cl.qw_servercount, 0));
506                 break;
507         case dl_none:
508         default:
509                 Con_Printf("Unknown download type.\n");
510         }
511 }
512
513 static void QW_CL_ParseDownload(void)
514 {
515         int size = MSG_ReadShort();
516         int percent = MSG_ReadByte();
517
518         // skip the download fragment if playing a demo
519         if (!cls.netcon)
520         {
521                 if (size > 0)
522                         msg_readcount += size;
523                 return;
524         }
525
526         if (size == -1)
527         {
528                 Con_Printf("File not found.\n");
529                 QW_CL_RequestNextDownload();
530                 return;
531         }
532
533         if (msg_readcount + (unsigned short)size > net_message.cursize)
534                 Host_Error("corrupt download message\n");
535
536         // make sure the buffer is big enough to include this new fragment
537         if (!cls.qw_downloadmemory || cls.qw_downloadmemorymaxsize < cls.qw_downloadmemorycursize + size)
538         {
539                 unsigned char *old;
540                 while (cls.qw_downloadmemorymaxsize < cls.qw_downloadmemorycursize + size)
541                         cls.qw_downloadmemorymaxsize *= 2;
542                 old = cls.qw_downloadmemory;
543                 cls.qw_downloadmemory = Mem_Alloc(cl_mempool, cls.qw_downloadmemorymaxsize);
544                 if (old)
545                 {
546                         memcpy(cls.qw_downloadmemory, old, cls.qw_downloadmemorycursize);
547                         Mem_Free(old);
548                 }
549         }
550
551         // read the fragment out of the packet
552         MSG_ReadBytes(size, cls.qw_downloadmemory + cls.qw_downloadmemorycursize);
553         cls.qw_downloadmemorycursize += size;
554
555         cls.qw_downloadpercent = percent;
556
557         if (percent != 100)
558         {
559                 // request next fragment
560                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
561                 MSG_WriteString(&cls.netcon->message, "nextdl");
562         }
563         else
564         {
565                 // finished file
566                 Con_Printf("Downloaded \"%s\"\n", cls.qw_downloadname);
567
568                 FS_WriteFile(cls.qw_downloadname, cls.qw_downloadmemory, cls.qw_downloadmemorycursize);
569
570                 cls.qw_downloadpercent = 0;
571
572                 // start downloading the next file (or join the game)
573                 QW_CL_RequestNextDownload();
574         }
575 }
576
577 static void QW_CL_ParseModelList(void)
578 {
579         int n;
580         int nummodels = MSG_ReadByte();
581         char *str;
582
583         // parse model precache list
584         for (;;)
585         {
586                 str = MSG_ReadString();
587                 if (!str[0])
588                         break;
589                 nummodels++;
590                 if (nummodels==MAX_MODELS)
591                         Host_Error("Server sent too many model precaches");
592                 if (strlen(str) >= MAX_QPATH)
593                         Host_Error("Server sent a precache name of %i characters (max %i)", strlen(str), MAX_QPATH - 1);
594                 strlcpy(cl.model_name[nummodels], str, sizeof (cl.model_name[nummodels]));
595         }
596
597         n = MSG_ReadByte();
598         if (n)
599         {
600                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
601                 MSG_WriteString(&cls.netcon->message, va("modellist %i %i", cl.qw_servercount, n));
602                 return;
603         }
604
605         cls.qw_downloadnumber = 0;
606         cls.qw_downloadtype = dl_model;
607         QW_CL_RequestNextDownload();
608 }
609
610 static void QW_CL_ParseSoundList(void)
611 {
612         int n;
613         int numsounds = MSG_ReadByte();
614         char *str;
615
616         // parse sound precache list
617         for (;;)
618         {
619                 str = MSG_ReadString();
620                 if (!str[0])
621                         break;
622                 numsounds++;
623                 if (numsounds==MAX_SOUNDS)
624                         Host_Error("Server sent too many sound precaches");
625                 if (strlen(str) >= MAX_QPATH)
626                         Host_Error("Server sent a precache name of %i characters (max %i)", strlen(str), MAX_QPATH - 1);
627                 strlcpy(cl.sound_name[numsounds], str, sizeof (cl.sound_name[numsounds]));
628         }
629
630         n = MSG_ReadByte();
631
632         if (n)
633         {
634                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
635                 MSG_WriteString(&cls.netcon->message, va("soundlist %i %i", cl.qw_servercount, n));
636                 return;
637         }
638
639         cls.qw_downloadnumber = 0;
640         cls.qw_downloadtype = dl_sound;
641         QW_CL_RequestNextDownload();
642 }
643
644 void QW_CL_NextUpload(void)
645 {
646         int r, percent, size;
647
648         if (!cls.qw_uploaddata)
649                 return;
650
651         r = cls.qw_uploadsize - cls.qw_uploadpos;
652         if (r > 768)
653                 r = 768;
654         size = min(1, cls.qw_uploadsize);
655         percent = (cls.qw_uploadpos+r)*100/size;
656
657         MSG_WriteByte(&cls.netcon->message, qw_clc_upload);
658         MSG_WriteShort(&cls.netcon->message, r);
659         MSG_WriteByte(&cls.netcon->message, percent);
660         SZ_Write(&cls.netcon->message, cls.qw_uploaddata + cls.qw_uploadpos, r);
661
662         Con_DPrintf("UPLOAD: %6d: %d written\n", cls.qw_uploadpos, r);
663
664         cls.qw_uploadpos += r;
665
666         if (cls.qw_uploadpos < cls.qw_uploadsize)
667                 return;
668
669         Con_Printf("Upload completed\n");
670
671         QW_CL_StopUpload();
672 }
673
674 void QW_CL_StartUpload(unsigned char *data, int size)
675 {
676         // do nothing in demos or if not connected
677         if (!cls.netcon)
678                 return;
679
680         // abort existing upload if in progress
681         QW_CL_StopUpload();
682
683         Con_DPrintf("Starting upload of %d bytes...\n", size);
684
685         cls.qw_uploaddata = Mem_Alloc(cl_mempool, size);
686         memcpy(cls.qw_uploaddata, data, size);
687         cls.qw_uploadsize = size;
688         cls.qw_uploadpos = 0;
689
690         QW_CL_NextUpload();
691 }
692
693 #if 0
694 qboolean QW_CL_IsUploading(void)
695 {
696         return cls.qw_uploaddata != NULL;
697 }
698 #endif
699
700 void QW_CL_StopUpload(void)
701 {
702         if (cls.qw_uploaddata)
703                 Mem_Free(cls.qw_uploaddata);
704         cls.qw_uploaddata = NULL;
705         cls.qw_uploadsize = 0;
706         cls.qw_uploadpos = 0;
707 }
708
709 static void QW_CL_ProcessUserInfo(int slot)
710 {
711         int topcolor, bottomcolor;
712         char temp[2048];
713         InfoString_GetValue(cl.scores[slot].qw_userinfo, "name", cl.scores[slot].name, sizeof(cl.scores[slot].name));
714         InfoString_GetValue(cl.scores[slot].qw_userinfo, "topcolor", temp, sizeof(temp));topcolor = atoi(temp);
715         InfoString_GetValue(cl.scores[slot].qw_userinfo, "bottomcolor", temp, sizeof(temp));bottomcolor = atoi(temp);
716         cl.scores[slot].colors = topcolor * 16 + bottomcolor;
717         InfoString_GetValue(cl.scores[slot].qw_userinfo, "*spectator", temp, sizeof(temp));
718         cl.scores[slot].qw_spectator = temp[0] != 0;
719         InfoString_GetValue(cl.scores[slot].qw_userinfo, "skin", cl.scores[slot].qw_skin, sizeof(cl.scores[slot].qw_skin));
720         // LordHavoc: abusing Draw_CachePic for caching skins...
721         cl.scores[slot].qw_skin_cachepic = Draw_CachePic(cl.scores[slot].qw_skin, true);
722 }
723
724 static void QW_CL_UpdateUserInfo(void)
725 {
726         int slot;
727         slot = MSG_ReadByte();
728         if (slot >= cl.maxclients)
729         {
730                 Con_Printf("svc_updateuserinfo >= cl.maxclients\n");
731                 MSG_ReadLong();
732                 MSG_ReadString();
733                 return;
734         }
735         cl.scores[slot].qw_userid = MSG_ReadLong();
736         strlcpy(cl.scores[slot].qw_userinfo, MSG_ReadString(), sizeof(cl.scores[slot].qw_userinfo));
737
738         QW_CL_ProcessUserInfo(slot);
739 }
740
741 static void QW_CL_SetInfo(void)
742 {
743         int slot;
744         char key[2048];
745         char value[2048];
746         slot = MSG_ReadByte();
747         strlcpy(key, MSG_ReadString(), sizeof(key));
748         strlcpy(value, MSG_ReadString(), sizeof(value));
749         if (slot >= cl.maxclients)
750         {
751                 Con_Printf("svc_setinfo >= cl.maxclients\n");
752                 return;
753         }
754         InfoString_SetValue(cl.scores[slot].qw_userinfo, sizeof(cl.scores[slot].qw_userinfo), key, value);
755
756         QW_CL_ProcessUserInfo(slot);
757 }
758
759 static void QW_CL_ServerInfo(void)
760 {
761         char key[2048];
762         char value[2048];
763         strlcpy(key, MSG_ReadString(), sizeof(key));
764         strlcpy(value, MSG_ReadString(), sizeof(value));
765         Con_DPrintf("SERVERINFO: %s=%s\n", key, value);
766         InfoString_SetValue(cl.qw_serverinfo, sizeof(cl.qw_serverinfo), key, value);
767 }
768
769 static void QW_CL_ParseNails(void)
770 {
771         int i, j;
772         int numnails = MSG_ReadByte();
773         vec_t *v;
774         unsigned char bits[6];
775         cl.qw_num_nails = 0;
776         for (i = 0;i < numnails;i++)
777         {
778                 v = cl.qw_nails[cl.qw_num_nails++];
779                 for (j = 0;j < 6;j++)
780                         bits[j] = MSG_ReadByte();
781                 v[0] = ( ( bits[0] + ((bits[1]&15)<<8) ) <<1) - 4096;
782                 v[1] = ( ( (bits[1]>>4) + (bits[2]<<4) ) <<1) - 4096;
783                 v[2] = ( ( bits[3] + ((bits[4]&15)<<8) ) <<1) - 4096;
784                 v[3] = 360*(bits[4]>>4)/16;
785                 v[4] = 360*bits[5]/256;
786                 v[5] = 0;
787         }
788 }
789
790 static void QW_CL_UpdateItemsAndWeapon(void)
791 {
792         int j;
793         // check for important changes
794
795         // set flash times
796         if (cl.olditems != cl.stats[STAT_ITEMS])
797                 for (j = 0;j < 32;j++)
798                         if ((cl.stats[STAT_ITEMS] & (1<<j)) && !(cl.olditems & (1<<j)))
799                                 cl.item_gettime[j] = cl.time;
800         cl.olditems = cl.stats[STAT_ITEMS];
801
802         // GAME_NEXUIZ hud needs weapon change time
803         if (cl.activeweapon != cl.stats[STAT_ACTIVEWEAPON])
804                 cl.weapontime = cl.time;
805         cl.activeweapon = cl.stats[STAT_ACTIVEWEAPON];
806 }
807
808 /*
809 =====================
810 CL_SignonReply
811
812 An svc_signonnum has been received, perform a client side setup
813 =====================
814 */
815 static void CL_SignonReply (void)
816 {
817         Con_DPrintf("CL_SignonReply: %i\n", cls.signon);
818
819         switch (cls.signon)
820         {
821         case 1:
822                 if (cls.netcon)
823                 {
824                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
825                         MSG_WriteString (&cls.netcon->message, "prespawn");
826                 }
827                 break;
828
829         case 2:
830                 if (cls.netcon)
831                 {
832                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
833                         MSG_WriteString (&cls.netcon->message, va("name \"%s\"", cl_name.string));
834
835                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
836                         MSG_WriteString (&cls.netcon->message, va("color %i %i", cl_color.integer >> 4, cl_color.integer & 15));
837
838                         if (cl_pmodel.integer)
839                         {
840                                 MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
841                                 MSG_WriteString (&cls.netcon->message, va("pmodel %i", cl_pmodel.integer));
842                         }
843                         if (*cl_playermodel.string)
844                         {
845                                 MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
846                                 MSG_WriteString (&cls.netcon->message, va("playermodel %s", cl_playermodel.string));
847                         }
848                         if (*cl_playerskin.string)
849                         {
850                                 MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
851                                 MSG_WriteString (&cls.netcon->message, va("playerskin %s", cl_playerskin.string));
852                         }
853
854                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
855                         MSG_WriteString (&cls.netcon->message, va("rate %i", cl_rate.integer));
856
857                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
858                         MSG_WriteString (&cls.netcon->message, "spawn");
859                 }
860                 break;
861
862         case 3:
863                 if (cls.netcon)
864                 {
865                         MSG_WriteByte (&cls.netcon->message, clc_stringcmd);
866                         MSG_WriteString (&cls.netcon->message, "begin");
867                 }
868                 break;
869
870         case 4:
871                 Con_ClearNotify();
872                 break;
873         }
874 }
875
876 /*
877 ==================
878 CL_ParseServerInfo
879 ==================
880 */
881 void CL_ParseServerInfo (void)
882 {
883         char *str;
884         int i;
885         protocolversion_t protocol;
886         int nummodels, numsounds;
887         entity_t *ent;
888
889         Con_DPrint("Serverinfo packet received.\n");
890
891         // check memory integrity
892         Mem_CheckSentinelsGlobal();
893
894 //
895 // wipe the client_state_t struct
896 //
897         CL_ClearState ();
898
899 // parse protocol version number
900         i = MSG_ReadLong ();
901         protocol = Protocol_EnumForNumber(i);
902         if (protocol == PROTOCOL_UNKNOWN)
903         {
904                 Host_Error("CL_ParseServerInfo: Server is unrecognized protocol number (%i)", i);
905                 return;
906         }
907         // hack for unmarked Nehahra movie demos which had a custom protocol
908         if (protocol == PROTOCOL_QUAKEDP && cls.demoplayback && demo_nehahra.integer)
909                 protocol = PROTOCOL_NEHAHRAMOVIE;
910         cls.protocol = protocol;
911         Con_DPrintf("Server protocol is %s\n", Protocol_NameForEnum(cls.protocol));
912
913         if (protocol == PROTOCOL_QUAKEWORLD)
914         {
915                 cl.qw_servercount = MSG_ReadLong();
916
917                 str = MSG_ReadString();
918                 Con_Printf("server gamedir is %s\n", str);
919 #if 0
920                 // FIXME: change gamedir if needed!
921                 if (strcasecmp(gamedirfile, str))
922                 {
923                         Host_SaveConfig_f();
924                         cflag = 1;
925                 }
926
927                 Com_Gamedir(str); // change gamedir
928
929                 if (cflag)
930                 {
931                         // exec the new config stuff
932                 }
933 #endif
934
935                 cl.gametype = GAME_DEATHMATCH;
936                 cl.maxclients = 32;
937
938                 // parse player number
939                 i = MSG_ReadByte();
940                 cl.qw_spectator = (i & 128) != 0;
941                 cl.playerentity = cl.viewentity = (i & 127) + 1;
942                 cl.scores = (scoreboard_t *)Mem_Alloc(cl_mempool, cl.maxclients*sizeof(*cl.scores));
943
944                 // get the full level name
945                 str = MSG_ReadString ();
946                 strlcpy (cl.levelname, str, sizeof(cl.levelname));
947
948                 // get the movevars
949                 cl.qw_movevars_gravity            = MSG_ReadFloat();
950                 cl.qw_movevars_stopspeed          = MSG_ReadFloat();
951                 cl.qw_movevars_maxspeed           = MSG_ReadFloat();
952                 cl.qw_movevars_spectatormaxspeed  = MSG_ReadFloat();
953                 cl.qw_movevars_accelerate         = MSG_ReadFloat();
954                 cl.qw_movevars_airaccelerate      = MSG_ReadFloat();
955                 cl.qw_movevars_wateraccelerate    = MSG_ReadFloat();
956                 cl.qw_movevars_friction           = MSG_ReadFloat();
957                 cl.qw_movevars_waterfriction      = MSG_ReadFloat();
958                 cl.qw_movevars_entgravity         = MSG_ReadFloat();
959
960                 // seperate the printfs so the server message can have a color
961                 Con_Printf("\n\n\35\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\37\n\n\2%s\n", str);
962
963                 // check memory integrity
964                 Mem_CheckSentinelsGlobal();
965
966                 S_StopAllSounds();
967                 // if server is active, we already began a loading plaque
968                 if (!sv.active)
969                         SCR_BeginLoadingPlaque();
970
971                 // disable until we get textures for it
972                 R_ResetSkyBox();
973
974                 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));      //[515]: csqc
975                 memset(cl.model_precache, 0, sizeof(cl.model_precache));
976                 memset(cl.sound_precache, 0, sizeof(cl.sound_precache));
977
978                 MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
979                 MSG_WriteString(&cls.netcon->message, va("soundlist %i %i", cl.qw_servercount, 0));
980
981                 cls.state = ca_connected;
982                 cls.signon = 1;
983         }
984         else
985         {
986         // parse maxclients
987                 cl.maxclients = MSG_ReadByte ();
988                 if (cl.maxclients < 1 || cl.maxclients > MAX_SCOREBOARD)
989                 {
990                         Host_Error("Bad maxclients (%u) from server", cl.maxclients);
991                         return;
992                 }
993                 cl.scores = (scoreboard_t *)Mem_Alloc(cl_mempool, cl.maxclients*sizeof(*cl.scores));
994
995         // parse gametype
996                 cl.gametype = MSG_ReadByte ();
997
998         // parse signon message
999                 str = MSG_ReadString ();
1000                 strlcpy (cl.levelname, str, sizeof(cl.levelname));
1001
1002         // seperate the printfs so the server message can have a color
1003                 if (cls.protocol != PROTOCOL_NEHAHRAMOVIE) // no messages when playing the Nehahra movie
1004                         Con_Printf("\n\n\35\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\36\37\n\n\2%s\n", str);
1005
1006                 // check memory integrity
1007                 Mem_CheckSentinelsGlobal();
1008
1009                 S_StopAllSounds();
1010                 // if server is active, we already began a loading plaque
1011                 if (!sv.active)
1012                         SCR_BeginLoadingPlaque();
1013
1014                 // disable until we get textures for it
1015                 R_ResetSkyBox();
1016
1017                 memset(cl.csqc_model_precache, 0, sizeof(cl.csqc_model_precache));      //[515]: csqc
1018                 memset(cl.model_precache, 0, sizeof(cl.model_precache));
1019                 memset(cl.sound_precache, 0, sizeof(cl.sound_precache));
1020
1021                 // parse model precache list
1022                 for (nummodels=1 ; ; nummodels++)
1023                 {
1024                         str = MSG_ReadString();
1025                         if (!str[0])
1026                                 break;
1027                         if (nummodels==MAX_MODELS)
1028                                 Host_Error ("Server sent too many model precaches");
1029                         if (strlen(str) >= MAX_QPATH)
1030                                 Host_Error ("Server sent a precache name of %i characters (max %i)", strlen(str), MAX_QPATH - 1);
1031                         strlcpy (cl.model_name[nummodels], str, sizeof (cl.model_name[nummodels]));
1032                 }
1033                 // parse sound precache list
1034                 for (numsounds=1 ; ; numsounds++)
1035                 {
1036                         str = MSG_ReadString();
1037                         if (!str[0])
1038                                 break;
1039                         if (numsounds==MAX_SOUNDS)
1040                                 Host_Error("Server sent too many sound precaches");
1041                         if (strlen(str) >= MAX_QPATH)
1042                                 Host_Error("Server sent a precache name of %i characters (max %i)", strlen(str), MAX_QPATH - 1);
1043                         strlcpy (cl.sound_name[numsounds], str, sizeof (cl.sound_name[numsounds]));
1044                 }
1045
1046                 // touch all of the precached models that are still loaded so we can free
1047                 // anything that isn't needed
1048                 Mod_ClearUsed();
1049                 for (i = 1;i < nummodels;i++)
1050                         Mod_FindName(cl.model_name[i]);
1051                 // precache any models used by the client (this also marks them used)
1052                 cl.model_bolt = Mod_ForName("progs/bolt.mdl", false, false, false);
1053                 cl.model_bolt2 = Mod_ForName("progs/bolt2.mdl", false, false, false);
1054                 cl.model_bolt3 = Mod_ForName("progs/bolt3.mdl", false, false, false);
1055                 cl.model_beam = Mod_ForName("progs/beam.mdl", false, false, false);
1056                 Mod_PurgeUnused();
1057
1058                 // do the same for sounds
1059                 // FIXME: S_ServerSounds does not know about cl.sfx_ sounds
1060                 S_ServerSounds (cl.sound_name, numsounds);
1061
1062                 // precache any sounds used by the client
1063                 cl.sfx_wizhit = S_PrecacheSound("sound/wizard/hit.wav", false, true);
1064                 cl.sfx_knighthit = S_PrecacheSound("sound/hknight/hit.wav", false, true);
1065                 cl.sfx_tink1 = S_PrecacheSound("sound/weapons/tink1.wav", false, true);
1066                 cl.sfx_ric1 = S_PrecacheSound("sound/weapons/ric1.wav", false, true);
1067                 cl.sfx_ric2 = S_PrecacheSound("sound/weapons/ric2.wav", false, true);
1068                 cl.sfx_ric3 = S_PrecacheSound("sound/weapons/ric3.wav", false, true);
1069                 cl.sfx_r_exp3 = S_PrecacheSound("sound/weapons/r_exp3.wav", false, true);
1070
1071                 // now we try to load everything that is new
1072
1073                 // world model
1074                 CL_KeepaliveMessage ();
1075                 cl.model_precache[1] = Mod_ForName(cl.model_name[1], false, false, true);
1076                 if (cl.model_precache[1]->Draw == NULL)
1077                         Con_Printf("Map %s not found\n", cl.model_name[1]);
1078
1079                 // normal models
1080                 for (i=2 ; i<nummodels ; i++)
1081                 {
1082                         CL_KeepaliveMessage();
1083                         if ((cl.model_precache[i] = Mod_ForName(cl.model_name[i], false, false, false))->Draw == NULL)
1084                                 Con_Printf("Model %s not found\n", cl.model_name[i]);
1085                 }
1086
1087                 // sounds
1088                 for (i=1 ; i<numsounds ; i++)
1089                 {
1090                         CL_KeepaliveMessage();
1091
1092                         // Don't lock the sfx here, S_ServerSounds already did that
1093                         cl.sound_precache[i] = S_PrecacheSound (cl.sound_name[i], true, false);
1094                 }
1095         }
1096
1097         // local state
1098         ent = &cl_entities[0];
1099         // entire entity array was cleared, so just fill in a few fields
1100         ent->state_current.active = true;
1101         ent->render.model = cl.worldmodel = cl.model_precache[1];
1102         ent->render.scale = 1; // some of the renderer still relies on scale
1103         ent->render.alpha = 1;
1104         ent->render.colormap = -1; // no special coloring
1105         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
1106         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
1107         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1108         CL_BoundingBoxForEntity(&ent->render);
1109
1110         cl_num_entities = 1;
1111
1112         R_Modules_NewMap();
1113         CL_CGVM_Start();
1114
1115         // noclip is turned off at start
1116         noclip_anglehack = false;
1117
1118         // check memory integrity
1119         Mem_CheckSentinelsGlobal();
1120 }
1121
1122 void CL_ValidateState(entity_state_t *s)
1123 {
1124         model_t *model;
1125
1126         if (!s->active)
1127                 return;
1128
1129         if (s->modelindex >= MAX_MODELS && (65536-s->modelindex) >= MAX_MODELS)
1130                 Host_Error("CL_ValidateState: modelindex (%i) >= MAX_MODELS (%i)\n", s->modelindex, MAX_MODELS);
1131
1132         // colormap is client index + 1
1133         if ((!s->flags & RENDER_COLORMAPPED) && s->colormap > cl.maxclients)
1134         {
1135                 Con_DPrintf("CL_ValidateState: colormap (%i) > cl.maxclients (%i)\n", s->colormap, cl.maxclients);
1136                 s->colormap = 0;
1137         }
1138
1139         model = cl.model_precache[s->modelindex];
1140         if (model && model->type && s->frame >= model->numframes)
1141         {
1142                 Con_DPrintf("CL_ValidateState: no such frame %i in \"%s\" (which has %i frames)\n", s->frame, model->name, model->numframes);
1143                 s->frame = 0;
1144         }
1145         if (model && model->type && s->skin > 0 && s->skin >= model->numskins && !(s->lightpflags & PFLAGS_FULLDYNAMIC))
1146         {
1147                 Con_DPrintf("CL_ValidateState: no such skin %i in \"%s\" (which has %i skins)\n", s->skin, model->name, model->numskins);
1148                 s->skin = 0;
1149         }
1150 }
1151
1152 void CL_MoveLerpEntityStates(entity_t *ent)
1153 {
1154         float odelta[3], adelta[3];
1155         CL_ValidateState(&ent->state_current);
1156         VectorSubtract(ent->state_current.origin, ent->persistent.neworigin, odelta);
1157         VectorSubtract(ent->state_current.angles, ent->persistent.newangles, adelta);
1158         if (!ent->state_previous.active || ent->state_previous.modelindex != ent->state_current.modelindex)
1159         {
1160                 // reset all persistent stuff if this is a new entity
1161                 ent->persistent.lerpdeltatime = 0;
1162                 ent->persistent.lerpstarttime = cl.mtime[1];
1163                 VectorCopy(ent->state_current.origin, ent->persistent.oldorigin);
1164                 VectorCopy(ent->state_current.angles, ent->persistent.oldangles);
1165                 VectorCopy(ent->state_current.origin, ent->persistent.neworigin);
1166                 VectorCopy(ent->state_current.angles, ent->persistent.newangles);
1167                 // reset animation interpolation as well
1168                 ent->render.frame = ent->render.frame1 = ent->render.frame2 = ent->state_current.frame;
1169                 ent->render.frame1time = ent->render.frame2time = cl.time;
1170                 ent->render.framelerp = 1;
1171                 // reset various persistent stuff
1172                 ent->persistent.muzzleflash = 0;
1173                 VectorCopy(ent->state_current.origin, ent->persistent.trail_origin);
1174         }
1175         else if (cls.timedemo || cl_nolerp.integer || DotProduct(odelta, odelta) > 1000*1000)
1176         {
1177                 // don't interpolate the move
1178                 ent->persistent.lerpdeltatime = 0;
1179                 ent->persistent.lerpstarttime = cl.mtime[1];
1180                 VectorCopy(ent->state_current.origin, ent->persistent.oldorigin);
1181                 VectorCopy(ent->state_current.angles, ent->persistent.oldangles);
1182                 VectorCopy(ent->state_current.origin, ent->persistent.neworigin);
1183                 VectorCopy(ent->state_current.angles, ent->persistent.newangles);
1184         }
1185         else if (ent->state_current.flags & RENDER_STEP)
1186         {
1187                 // monster interpolation
1188                 if (DotProduct(odelta, odelta) + DotProduct(adelta, adelta) > 0.01)
1189                 {
1190                         ent->persistent.lerpdeltatime = bound(0, cl.mtime[1] - ent->persistent.lerpstarttime, 0.1);
1191                         ent->persistent.lerpstarttime = cl.mtime[1];
1192                         VectorCopy(ent->persistent.neworigin, ent->persistent.oldorigin);
1193                         VectorCopy(ent->persistent.newangles, ent->persistent.oldangles);
1194                         VectorCopy(ent->state_current.origin, ent->persistent.neworigin);
1195                         VectorCopy(ent->state_current.angles, ent->persistent.newangles);
1196                 }
1197         }
1198         else
1199         {
1200                 // not a monster
1201                 ent->persistent.lerpstarttime = ent->state_previous.time;
1202                 // no lerp if it's singleplayer
1203                 if (cl.islocalgame && !sv_fixedframeratesingleplayer.integer)
1204                         ent->persistent.lerpdeltatime = 0;
1205                 else
1206                         ent->persistent.lerpdeltatime = bound(0, ent->state_current.time - ent->state_previous.time, 0.1);
1207                 VectorCopy(ent->persistent.neworigin, ent->persistent.oldorigin);
1208                 VectorCopy(ent->persistent.newangles, ent->persistent.oldangles);
1209                 VectorCopy(ent->state_current.origin, ent->persistent.neworigin);
1210                 VectorCopy(ent->state_current.angles, ent->persistent.newangles);
1211         }
1212 }
1213
1214 /*
1215 ==================
1216 CL_ParseBaseline
1217 ==================
1218 */
1219 void CL_ParseBaseline (entity_t *ent, int large)
1220 {
1221         int i;
1222
1223         ent->state_baseline = defaultstate;
1224         // FIXME: set ent->state_baseline.number?
1225         ent->state_baseline.active = true;
1226         if (large)
1227         {
1228                 ent->state_baseline.modelindex = (unsigned short) MSG_ReadShort ();
1229                 ent->state_baseline.frame = (unsigned short) MSG_ReadShort ();
1230         }
1231         else
1232         {
1233                 ent->state_baseline.modelindex = MSG_ReadByte ();
1234                 ent->state_baseline.frame = MSG_ReadByte ();
1235         }
1236         ent->state_baseline.colormap = MSG_ReadByte();
1237         ent->state_baseline.skin = MSG_ReadByte();
1238         for (i = 0;i < 3;i++)
1239         {
1240                 ent->state_baseline.origin[i] = MSG_ReadCoord(cls.protocol);
1241                 ent->state_baseline.angles[i] = MSG_ReadAngle(cls.protocol);
1242         }
1243         CL_ValidateState(&ent->state_baseline);
1244         ent->state_previous = ent->state_current = ent->state_baseline;
1245 }
1246
1247
1248 /*
1249 ==================
1250 CL_ParseClientdata
1251
1252 Server information pertaining to this client only
1253 ==================
1254 */
1255 void CL_ParseClientdata (void)
1256 {
1257         int i, j, bits;
1258
1259         VectorCopy (cl.mpunchangle[0], cl.mpunchangle[1]);
1260         VectorCopy (cl.mpunchvector[0], cl.mpunchvector[1]);
1261         VectorCopy (cl.mvelocity[0], cl.mvelocity[1]);
1262         cl.mviewzoom[1] = cl.mviewzoom[0];
1263
1264         if (cls.protocol == PROTOCOL_QUAKE || cls.protocol == PROTOCOL_QUAKEDP || cls.protocol == PROTOCOL_NEHAHRAMOVIE || cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4 || cls.protocol == PROTOCOL_DARKPLACES5)
1265         {
1266                 cl.stats[STAT_VIEWHEIGHT] = DEFAULT_VIEWHEIGHT;
1267                 cl.stats[STAT_ITEMS] = 0;
1268                 cl.stats[STAT_VIEWZOOM] = 255;
1269         }
1270         cl.idealpitch = 0;
1271         cl.mpunchangle[0][0] = 0;
1272         cl.mpunchangle[0][1] = 0;
1273         cl.mpunchangle[0][2] = 0;
1274         cl.mpunchvector[0][0] = 0;
1275         cl.mpunchvector[0][1] = 0;
1276         cl.mpunchvector[0][2] = 0;
1277         cl.mvelocity[0][0] = 0;
1278         cl.mvelocity[0][1] = 0;
1279         cl.mvelocity[0][2] = 0;
1280         cl.mviewzoom[0] = 1;
1281
1282         bits = (unsigned short) MSG_ReadShort ();
1283         if (bits & SU_EXTEND1)
1284                 bits |= (MSG_ReadByte() << 16);
1285         if (bits & SU_EXTEND2)
1286                 bits |= (MSG_ReadByte() << 24);
1287
1288         if (bits & SU_VIEWHEIGHT)
1289                 cl.stats[STAT_VIEWHEIGHT] = MSG_ReadChar ();
1290
1291         if (bits & SU_IDEALPITCH)
1292                 cl.idealpitch = MSG_ReadChar ();
1293
1294         for (i = 0;i < 3;i++)
1295         {
1296                 if (bits & (SU_PUNCH1<<i) )
1297                 {
1298                         if (cls.protocol == PROTOCOL_QUAKE || cls.protocol == PROTOCOL_QUAKEDP || cls.protocol == PROTOCOL_NEHAHRAMOVIE)
1299                                 cl.mpunchangle[0][i] = MSG_ReadChar();
1300                         else
1301                                 cl.mpunchangle[0][i] = MSG_ReadAngle16i();
1302                 }
1303                 if (bits & (SU_PUNCHVEC1<<i))
1304                 {
1305                         if (cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4)
1306                                 cl.mpunchvector[0][i] = MSG_ReadCoord16i();
1307                         else
1308                                 cl.mpunchvector[0][i] = MSG_ReadCoord32f();
1309                 }
1310                 if (bits & (SU_VELOCITY1<<i) )
1311                 {
1312                         if (cls.protocol == PROTOCOL_QUAKE || cls.protocol == PROTOCOL_QUAKEDP || cls.protocol == PROTOCOL_NEHAHRAMOVIE || cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4)
1313                                 cl.mvelocity[0][i] = MSG_ReadChar()*16;
1314                         else
1315                                 cl.mvelocity[0][i] = MSG_ReadCoord32f();
1316                 }
1317         }
1318
1319         // LordHavoc: hipnotic demos don't have this bit set but should
1320         if (bits & SU_ITEMS || cls.protocol == PROTOCOL_QUAKE || cls.protocol == PROTOCOL_QUAKEDP || cls.protocol == PROTOCOL_NEHAHRAMOVIE || cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4 || cls.protocol == PROTOCOL_DARKPLACES5)
1321                 cl.stats[STAT_ITEMS] = MSG_ReadLong ();
1322
1323         cl.onground = (bits & SU_ONGROUND) != 0;
1324         csqc_onground = cl.onground;    //[515]: cause without this csqc will receive not right value on svc_print =/
1325         cl.inwater = (bits & SU_INWATER) != 0;
1326
1327         if (cls.protocol == PROTOCOL_DARKPLACES5)
1328         {
1329                 cl.stats[STAT_WEAPONFRAME] = (bits & SU_WEAPONFRAME) ? MSG_ReadShort() : 0;
1330                 cl.stats[STAT_ARMOR] = (bits & SU_ARMOR) ? MSG_ReadShort() : 0;
1331                 cl.stats[STAT_WEAPON] = (bits & SU_WEAPON) ? MSG_ReadShort() : 0;
1332                 cl.stats[STAT_HEALTH] = MSG_ReadShort();
1333                 cl.stats[STAT_AMMO] = MSG_ReadShort();
1334                 cl.stats[STAT_SHELLS] = MSG_ReadShort();
1335                 cl.stats[STAT_NAILS] = MSG_ReadShort();
1336                 cl.stats[STAT_ROCKETS] = MSG_ReadShort();
1337                 cl.stats[STAT_CELLS] = MSG_ReadShort();
1338                 cl.stats[STAT_ACTIVEWEAPON] = (unsigned short) MSG_ReadShort ();
1339         }
1340         else if (cls.protocol == PROTOCOL_QUAKE || cls.protocol == PROTOCOL_QUAKEDP || cls.protocol == PROTOCOL_NEHAHRAMOVIE || cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4)
1341         {
1342                 cl.stats[STAT_WEAPONFRAME] = (bits & SU_WEAPONFRAME) ? MSG_ReadByte() : 0;
1343                 cl.stats[STAT_ARMOR] = (bits & SU_ARMOR) ? MSG_ReadByte() : 0;
1344                 cl.stats[STAT_WEAPON] = (bits & SU_WEAPON) ? MSG_ReadByte() : 0;
1345                 cl.stats[STAT_HEALTH] = MSG_ReadShort();
1346                 cl.stats[STAT_AMMO] = MSG_ReadByte();
1347                 cl.stats[STAT_SHELLS] = MSG_ReadByte();
1348                 cl.stats[STAT_NAILS] = MSG_ReadByte();
1349                 cl.stats[STAT_ROCKETS] = MSG_ReadByte();
1350                 cl.stats[STAT_CELLS] = MSG_ReadByte();
1351                 if (gamemode == GAME_HIPNOTIC || gamemode == GAME_ROGUE || gamemode == GAME_NEXUIZ)
1352                         cl.stats[STAT_ACTIVEWEAPON] = (1<<MSG_ReadByte ());
1353                 else
1354                         cl.stats[STAT_ACTIVEWEAPON] = MSG_ReadByte ();
1355         }
1356
1357         if (bits & SU_VIEWZOOM)
1358         {
1359                 if (cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3 || cls.protocol == PROTOCOL_DARKPLACES4)
1360                         cl.stats[STAT_VIEWZOOM] = MSG_ReadByte();
1361                 else
1362                         cl.stats[STAT_VIEWZOOM] = (unsigned short) MSG_ReadShort();
1363         }
1364
1365         // check for important changes
1366
1367         // set flash times
1368         if (cl.olditems != cl.stats[STAT_ITEMS])
1369                 for (j = 0;j < 32;j++)
1370                         if ((cl.stats[STAT_ITEMS] & (1<<j)) && !(cl.olditems & (1<<j)))
1371                                 cl.item_gettime[j] = cl.time;
1372         cl.olditems = cl.stats[STAT_ITEMS];
1373
1374         // GAME_NEXUIZ hud needs weapon change time
1375         if (cl.activeweapon != cl.stats[STAT_ACTIVEWEAPON])
1376                 cl.weapontime = cl.time;
1377         cl.activeweapon = cl.stats[STAT_ACTIVEWEAPON];
1378
1379         // viewzoom interpolation
1380         cl.mviewzoom[0] = (float) max(cl.stats[STAT_VIEWZOOM], 2) * (1.0f / 255.0f);
1381 }
1382
1383 /*
1384 =====================
1385 CL_ParseStatic
1386 =====================
1387 */
1388 void CL_ParseStatic (int large)
1389 {
1390         entity_t *ent;
1391
1392         if (cl_num_static_entities >= cl_max_static_entities)
1393                 Host_Error ("Too many static entities");
1394         ent = &cl_static_entities[cl_num_static_entities++];
1395         CL_ParseBaseline (ent, large);
1396
1397 // copy it to the current state
1398         ent->render.model = cl.model_precache[ent->state_baseline.modelindex];
1399         ent->render.frame = ent->render.frame1 = ent->render.frame2 = ent->state_baseline.frame;
1400         ent->render.framelerp = 0;
1401         // make torchs play out of sync
1402         ent->render.frame1time = ent->render.frame2time = lhrandom(-10, -1);
1403         ent->render.colormap = -1; // no special coloring
1404         ent->render.skinnum = ent->state_baseline.skin;
1405         ent->render.effects = ent->state_baseline.effects;
1406         ent->render.alpha = 1;
1407         //ent->render.scale = 1;
1408
1409         //VectorCopy (ent->state_baseline.origin, ent->render.origin);
1410         //VectorCopy (ent->state_baseline.angles, ent->render.angles);
1411
1412         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, ent->state_baseline.origin[0], ent->state_baseline.origin[1], ent->state_baseline.origin[2], ent->state_baseline.angles[0], ent->state_baseline.angles[1], ent->state_baseline.angles[2], 1);
1413         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1414         CL_BoundingBoxForEntity(&ent->render);
1415
1416         // This is definitely cheating...
1417         if (ent->render.model == NULL)
1418                 cl_num_static_entities--;
1419 }
1420
1421 /*
1422 ===================
1423 CL_ParseStaticSound
1424 ===================
1425 */
1426 void CL_ParseStaticSound (int large)
1427 {
1428         vec3_t          org;
1429         int                     sound_num, vol, atten;
1430
1431         MSG_ReadVector(org, cls.protocol);
1432         if (large)
1433                 sound_num = (unsigned short) MSG_ReadShort ();
1434         else
1435                 sound_num = MSG_ReadByte ();
1436         vol = MSG_ReadByte ();
1437         atten = MSG_ReadByte ();
1438
1439         S_StaticSound (cl.sound_precache[sound_num], org, vol/255.0f, atten);
1440 }
1441
1442 void CL_ParseEffect (void)
1443 {
1444         vec3_t          org;
1445         int                     modelindex, startframe, framecount, framerate;
1446
1447         MSG_ReadVector(org, cls.protocol);
1448         modelindex = MSG_ReadByte ();
1449         startframe = MSG_ReadByte ();
1450         framecount = MSG_ReadByte ();
1451         framerate = MSG_ReadByte ();
1452
1453         CL_Effect(org, modelindex, startframe, framecount, framerate);
1454 }
1455
1456 void CL_ParseEffect2 (void)
1457 {
1458         vec3_t          org;
1459         int                     modelindex, startframe, framecount, framerate;
1460
1461         MSG_ReadVector(org, cls.protocol);
1462         modelindex = (unsigned short) MSG_ReadShort ();
1463         startframe = (unsigned short) MSG_ReadShort ();
1464         framecount = MSG_ReadByte ();
1465         framerate = MSG_ReadByte ();
1466
1467         CL_Effect(org, modelindex, startframe, framecount, framerate);
1468 }
1469
1470 void CL_ParseBeam (model_t *m, int lightning)
1471 {
1472         int i, ent;
1473         vec3_t start, end;
1474         beam_t *b = NULL;
1475
1476         ent = (unsigned short) MSG_ReadShort ();
1477         MSG_ReadVector(start, cls.protocol);
1478         MSG_ReadVector(end, cls.protocol);
1479
1480         if (ent >= MAX_EDICTS)
1481         {
1482                 Con_Printf("CL_ParseBeam: invalid entity number %i\n", ent);
1483                 ent = 0;
1484         }
1485
1486         if (ent >= cl_max_entities)
1487                 CL_ExpandEntities(ent);
1488
1489         // override any beam with the same entity
1490         i = cl_max_beams;
1491         if (ent)
1492                 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1493                         if (b->entity == ent)
1494                                 break;
1495         // if the entity was not found then just replace an unused beam
1496         if (i == cl_max_beams)
1497                 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1498                         if (!b->model || b->endtime < cl.time)
1499                                 break;
1500         if (i < cl_max_beams)
1501         {
1502                 b->entity = ent;
1503                 b->lightning = lightning;
1504                 b->model = m;
1505                 b->endtime = cl.time + 0.2;
1506                 VectorCopy (start, b->start);
1507                 VectorCopy (end, b->end);
1508                 b->relativestartvalid = 0;
1509                 if (ent && cl_entities[ent].state_current.active)
1510                 {
1511                         entity_state_t *p;
1512                         matrix4x4_t matrix, imatrix;
1513                         if (ent == cl.viewentity && cl.movement)
1514                                 p = &cl_entities[b->entity].state_previous;
1515                         else
1516                                 p = &cl_entities[b->entity].state_current;
1517                         // not really valid yet, we need to get the orientation now
1518                         // (ParseBeam flagged this because it is received before
1519                         //  entities are received, by now they have been received)
1520                         // note: because players create lightning in their think
1521                         // function (which occurs before movement), they actually
1522                         // have some lag in it's location, so compare to the
1523                         // previous player state, not the latest
1524                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2], -p->angles[0], p->angles[1], p->angles[2], 1);
1525                         Matrix4x4_Invert_Simple(&imatrix, &matrix);
1526                         Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
1527                         Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
1528                         b->relativestartvalid = 1;
1529                 }
1530         }
1531         else
1532                 Con_Print("beam list overflow!\n");
1533 }
1534
1535 void CL_ParseTempEntity(void)
1536 {
1537         int type;
1538         vec3_t pos;
1539         vec3_t dir;
1540         vec3_t pos2;
1541         vec3_t color;
1542         int rnd;
1543         int colorStart, colorLength, count;
1544         float velspeed, radius;
1545         unsigned char *tempcolor;
1546         matrix4x4_t tempmatrix;
1547
1548         if (cls.protocol == PROTOCOL_QUAKEWORLD)
1549         {
1550                 type = MSG_ReadByte();
1551                 switch (type)
1552                 {
1553                 case QW_TE_WIZSPIKE:
1554                         // spike hitting wall
1555                         MSG_ReadVector(pos, cls.protocol);
1556                         CL_FindNonSolidLocation(pos, pos, 4);
1557                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1558                         //CL_AllocDlight(NULL, &tempmatrix, 100, 0.12f, 0.50f, 0.12f, 500, 0.2, 0, -1, false, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1559                         CL_RunParticleEffect(pos, vec3_origin, 20, 30);
1560                         S_StartSound(-1, 0, cl.sfx_wizhit, pos, 1, 1);
1561                         break;
1562
1563                 case QW_TE_KNIGHTSPIKE:
1564                         // spike hitting wall
1565                         MSG_ReadVector(pos, cls.protocol);
1566                         CL_FindNonSolidLocation(pos, pos, 4);
1567                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1568                         //CL_AllocDlight(NULL, &tempmatrix, 100, 0.50f, 0.30f, 0.10f, 500, 0.2, 0, -1, false, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1569                         CL_RunParticleEffect(pos, vec3_origin, 226, 20);
1570                         S_StartSound(-1, 0, cl.sfx_knighthit, pos, 1, 1);
1571                         break;
1572
1573                 case QW_TE_SPIKE:
1574                         // spike hitting wall
1575                         MSG_ReadVector(pos, cls.protocol);
1576                         CL_FindNonSolidLocation(pos, pos, 4);
1577                         if (cl_particles_quake.integer)
1578                                 CL_RunParticleEffect(pos, vec3_origin, 0, 10);
1579                         else if (cl_particles_bulletimpacts.integer)
1580                         {
1581                                 CL_SparkShower(pos, vec3_origin, 15, 1, 0);
1582                                 CL_Smoke(pos, vec3_origin, 15, 0);
1583                         }
1584                         CL_BulletMark(pos);
1585                         if (rand() % 5)
1586                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1587                         else
1588                         {
1589                                 rnd = rand() & 3;
1590                                 if (rnd == 1)
1591                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1592                                 else if (rnd == 2)
1593                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1594                                 else
1595                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1596                         }
1597                         break;
1598                 case QW_TE_SUPERSPIKE:
1599                         // super spike hitting wall
1600                         MSG_ReadVector(pos, cls.protocol);
1601                         CL_FindNonSolidLocation(pos, pos, 4);
1602                         if (cl_particles_quake.integer)
1603                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20);
1604                         else if (cl_particles_bulletimpacts.integer)
1605                         {
1606                                 CL_SparkShower(pos, vec3_origin, 30, 1, 0);
1607                                 CL_Smoke(pos, vec3_origin, 30, 0);
1608                         }
1609                         CL_BulletMark(pos);
1610                         if (rand() % 5)
1611                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1612                         else
1613                         {
1614                                 rnd = rand() & 3;
1615                                 if (rnd == 1)
1616                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1617                                 else if (rnd == 2)
1618                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1619                                 else
1620                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1621                         }
1622                         break;
1623
1624                 case QW_TE_EXPLOSION:
1625                         // rocket explosion
1626                         MSG_ReadVector(pos, cls.protocol);
1627                         CL_FindNonSolidLocation(pos, pos, 10);
1628                         CL_ParticleExplosion(pos);
1629                         // LordHavoc: boosted color from 1.0, 0.8, 0.4 to 1.25, 1.0, 0.5
1630                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1631                         CL_AllocDlight(NULL, &tempmatrix, 350, 4.0f, 2.0f, 0.50f, 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1632                         if (gamemode != GAME_NEXUIZ)
1633                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1634                         CL_Effect(pos, cl.qw_modelindex_s_explod, 0, 6, 10);
1635                         break;
1636
1637                 case QW_TE_TAREXPLOSION:
1638                         // tarbaby explosion
1639                         MSG_ReadVector(pos, cls.protocol);
1640                         CL_FindNonSolidLocation(pos, pos, 10);
1641                         CL_BlobExplosion(pos);
1642
1643                         if (gamemode != GAME_NEXUIZ)
1644                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1645                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1646                         CL_AllocDlight(NULL, &tempmatrix, 600, 1.6f, 0.8f, 2.0f, 1200, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1647                         break;
1648
1649                 case QW_TE_LIGHTNING1:
1650                         // lightning bolts
1651                         CL_ParseBeam(cl.model_bolt, true);
1652                         break;
1653
1654                 case QW_TE_LIGHTNING2:
1655                         // lightning bolts
1656                         CL_ParseBeam(cl.model_bolt2, true);
1657                         break;
1658
1659                 case QW_TE_LIGHTNING3:
1660                         // lightning bolts
1661                         CL_ParseBeam(cl.model_bolt3, false);
1662                         break;
1663
1664                 case QW_TE_LAVASPLASH:
1665                         MSG_ReadVector(pos, cls.protocol);
1666                         CL_LavaSplash(pos);
1667                         break;
1668
1669                 case QW_TE_TELEPORT:
1670                         MSG_ReadVector(pos, cls.protocol);
1671                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1672                         CL_AllocDlight(NULL, &tempmatrix, 200, 1.0f, 1.0f, 1.0f, 600, 99.0f, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1673                         CL_TeleportSplash(pos);
1674                         break;
1675
1676                 case QW_TE_GUNSHOT:
1677                         // bullet hitting wall
1678                         radius = MSG_ReadByte();
1679                         MSG_ReadVector(pos, cls.protocol);
1680                         CL_FindNonSolidLocation(pos, pos, 4);
1681                         if (cl_particles_quake.integer)
1682                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20*radius);
1683                         else
1684                         {
1685                                 CL_SparkShower(pos, vec3_origin, 15*radius, 1, radius);
1686                                 CL_Smoke(pos, vec3_origin, 15*radius, radius);
1687                         }
1688                         // TODO: scatter bullet marks throughout the sphere?
1689                         CL_BulletMark(pos);
1690                         break;
1691
1692                 case QW_TE_BLOOD:
1693                         count = MSG_ReadByte();
1694                         MSG_ReadVector(pos, cls.protocol);
1695                         CL_FindNonSolidLocation(pos, pos, 4);
1696                         CL_BloodPuff(pos, vec3_origin, 20*count);
1697                         break;
1698
1699                 case QW_TE_LIGHTNINGBLOOD:
1700                         MSG_ReadVector(pos, cls.protocol);
1701                         CL_FindNonSolidLocation(pos, pos, 4);
1702                         CL_BloodPuff(pos, vec3_origin, 50);
1703                         break;
1704
1705                 default:
1706                         Host_Error("CL_ParseTempEntity: bad type %d (hex %02X)", type, type);
1707                 }
1708         }
1709         else
1710         {
1711                 type = MSG_ReadByte();
1712                 switch (type)
1713                 {
1714                 case TE_WIZSPIKE:
1715                         // spike hitting wall
1716                         MSG_ReadVector(pos, cls.protocol);
1717                         CL_FindNonSolidLocation(pos, pos, 4);
1718                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1719                         //CL_AllocDlight(NULL, &tempmatrix, 100, 0.12f, 0.50f, 0.12f, 500, 0.2, 0, -1, false, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1720                         CL_RunParticleEffect(pos, vec3_origin, 20, 30);
1721                         S_StartSound(-1, 0, cl.sfx_wizhit, pos, 1, 1);
1722                         break;
1723
1724                 case TE_KNIGHTSPIKE:
1725                         // spike hitting wall
1726                         MSG_ReadVector(pos, cls.protocol);
1727                         CL_FindNonSolidLocation(pos, pos, 4);
1728                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1729                         //CL_AllocDlight(NULL, &tempmatrix, 100, 0.50f, 0.30f, 0.10f, 500, 0.2, 0, -1, false, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1730                         CL_RunParticleEffect(pos, vec3_origin, 226, 20);
1731                         S_StartSound(-1, 0, cl.sfx_knighthit, pos, 1, 1);
1732                         break;
1733
1734                 case TE_SPIKE:
1735                         // spike hitting wall
1736                         MSG_ReadVector(pos, cls.protocol);
1737                         CL_FindNonSolidLocation(pos, pos, 4);
1738                         if (cl_particles_quake.integer)
1739                                 CL_RunParticleEffect(pos, vec3_origin, 0, 10);
1740                         else if (cl_particles_bulletimpacts.integer)
1741                         {
1742                                 CL_SparkShower(pos, vec3_origin, 15, 1, 0);
1743                                 CL_Smoke(pos, vec3_origin, 15, 0);
1744                         }
1745                         CL_BulletMark(pos);
1746                         if (rand() % 5)
1747                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1748                         else
1749                         {
1750                                 rnd = rand() & 3;
1751                                 if (rnd == 1)
1752                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1753                                 else if (rnd == 2)
1754                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1755                                 else
1756                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1757                         }
1758                         break;
1759                 case TE_SPIKEQUAD:
1760                         // quad spike hitting wall
1761                         MSG_ReadVector(pos, cls.protocol);
1762                         CL_FindNonSolidLocation(pos, pos, 4);
1763                         if (cl_particles_quake.integer)
1764                                 CL_RunParticleEffect(pos, vec3_origin, 0, 10);
1765                         else if (cl_particles_bulletimpacts.integer)
1766                         {
1767                                 CL_SparkShower(pos, vec3_origin, 15, 1, 0);
1768                                 CL_Smoke(pos, vec3_origin, 15, 0);
1769                         }
1770                         CL_BulletMark(pos);
1771                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1772                         CL_AllocDlight(NULL, &tempmatrix, 100, 0.15f, 0.15f, 1.5f, 500, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1773                         if (rand() % 5)
1774                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1775                         else
1776                         {
1777                                 rnd = rand() & 3;
1778                                 if (rnd == 1)
1779                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1780                                 else if (rnd == 2)
1781                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1782                                 else
1783                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1784                         }
1785                         break;
1786                 case TE_SUPERSPIKE:
1787                         // super spike hitting wall
1788                         MSG_ReadVector(pos, cls.protocol);
1789                         CL_FindNonSolidLocation(pos, pos, 4);
1790                         if (cl_particles_quake.integer)
1791                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20);
1792                         else if (cl_particles_bulletimpacts.integer)
1793                         {
1794                                 CL_SparkShower(pos, vec3_origin, 30, 1, 0);
1795                                 CL_Smoke(pos, vec3_origin, 30, 0);
1796                         }
1797                         CL_BulletMark(pos);
1798                         if (rand() % 5)
1799                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1800                         else
1801                         {
1802                                 rnd = rand() & 3;
1803                                 if (rnd == 1)
1804                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1805                                 else if (rnd == 2)
1806                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1807                                 else
1808                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1809                         }
1810                         break;
1811                 case TE_SUPERSPIKEQUAD:
1812                         // quad super spike hitting wall
1813                         MSG_ReadVector(pos, cls.protocol);
1814                         CL_FindNonSolidLocation(pos, pos, 4);
1815                         if (cl_particles_quake.integer)
1816                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20);
1817                         else if (cl_particles_bulletimpacts.integer)
1818                         {
1819                                 CL_SparkShower(pos, vec3_origin, 30, 1, 0);
1820                                 CL_Smoke(pos, vec3_origin, 30, 0);
1821                         }
1822                         CL_BulletMark(pos);
1823                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1824                         CL_AllocDlight(NULL, &tempmatrix, 100, 0.15f, 0.15f, 1.5f, 500, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1825                         if (rand() % 5)
1826                                 S_StartSound(-1, 0, cl.sfx_tink1, pos, 1, 1);
1827                         else
1828                         {
1829                                 rnd = rand() & 3;
1830                                 if (rnd == 1)
1831                                         S_StartSound(-1, 0, cl.sfx_ric1, pos, 1, 1);
1832                                 else if (rnd == 2)
1833                                         S_StartSound(-1, 0, cl.sfx_ric2, pos, 1, 1);
1834                                 else
1835                                         S_StartSound(-1, 0, cl.sfx_ric3, pos, 1, 1);
1836                         }
1837                         break;
1838                         // LordHavoc: added for improved blood splatters
1839                 case TE_BLOOD:
1840                         // blood puff
1841                         MSG_ReadVector(pos, cls.protocol);
1842                         CL_FindNonSolidLocation(pos, pos, 4);
1843                         dir[0] = MSG_ReadChar();
1844                         dir[1] = MSG_ReadChar();
1845                         dir[2] = MSG_ReadChar();
1846                         count = MSG_ReadByte();
1847                         CL_BloodPuff(pos, dir, count);
1848                         break;
1849                 case TE_SPARK:
1850                         // spark shower
1851                         MSG_ReadVector(pos, cls.protocol);
1852                         CL_FindNonSolidLocation(pos, pos, 4);
1853                         dir[0] = MSG_ReadChar();
1854                         dir[1] = MSG_ReadChar();
1855                         dir[2] = MSG_ReadChar();
1856                         count = MSG_ReadByte();
1857                         CL_SparkShower(pos, dir, count, 1, 0);
1858                         break;
1859                 case TE_PLASMABURN:
1860                         MSG_ReadVector(pos, cls.protocol);
1861                         CL_FindNonSolidLocation(pos, pos, 4);
1862                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1863                         CL_AllocDlight(NULL, &tempmatrix, 200, 1, 1, 1, 1000, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1864                         CL_PlasmaBurn(pos);
1865                         break;
1866                         // LordHavoc: added for improved gore
1867                 case TE_BLOODSHOWER:
1868                         // vaporized body
1869                         MSG_ReadVector(pos, cls.protocol); // mins
1870                         MSG_ReadVector(pos2, cls.protocol); // maxs
1871                         velspeed = MSG_ReadCoord(cls.protocol); // speed
1872                         count = (unsigned short) MSG_ReadShort(); // number of particles
1873                         CL_BloodShower(pos, pos2, velspeed, count);
1874                         break;
1875                 case TE_PARTICLECUBE:
1876                         // general purpose particle effect
1877                         MSG_ReadVector(pos, cls.protocol); // mins
1878                         MSG_ReadVector(pos2, cls.protocol); // maxs
1879                         MSG_ReadVector(dir, cls.protocol); // dir
1880                         count = (unsigned short) MSG_ReadShort(); // number of particles
1881                         colorStart = MSG_ReadByte(); // color
1882                         colorLength = MSG_ReadByte(); // gravity (1 or 0)
1883                         velspeed = MSG_ReadCoord(cls.protocol); // randomvel
1884                         CL_ParticleCube(pos, pos2, dir, count, colorStart, colorLength, velspeed);
1885                         break;
1886
1887                 case TE_PARTICLERAIN:
1888                         // general purpose particle effect
1889                         MSG_ReadVector(pos, cls.protocol); // mins
1890                         MSG_ReadVector(pos2, cls.protocol); // maxs
1891                         MSG_ReadVector(dir, cls.protocol); // dir
1892                         count = (unsigned short) MSG_ReadShort(); // number of particles
1893                         colorStart = MSG_ReadByte(); // color
1894                         CL_ParticleRain(pos, pos2, dir, count, colorStart, 0);
1895                         break;
1896
1897                 case TE_PARTICLESNOW:
1898                         // general purpose particle effect
1899                         MSG_ReadVector(pos, cls.protocol); // mins
1900                         MSG_ReadVector(pos2, cls.protocol); // maxs
1901                         MSG_ReadVector(dir, cls.protocol); // dir
1902                         count = (unsigned short) MSG_ReadShort(); // number of particles
1903                         colorStart = MSG_ReadByte(); // color
1904                         CL_ParticleRain(pos, pos2, dir, count, colorStart, 1);
1905                         break;
1906
1907                 case TE_GUNSHOT:
1908                         // bullet hitting wall
1909                         MSG_ReadVector(pos, cls.protocol);
1910                         CL_FindNonSolidLocation(pos, pos, 4);
1911                         if (cl_particles_quake.integer)
1912                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20);
1913                         else
1914                         {
1915                                 CL_SparkShower(pos, vec3_origin, 15, 1, 0);
1916                                 CL_Smoke(pos, vec3_origin, 15, 0);
1917                         }
1918                         CL_BulletMark(pos);
1919                         break;
1920
1921                 case TE_GUNSHOTQUAD:
1922                         // quad bullet hitting wall
1923                         MSG_ReadVector(pos, cls.protocol);
1924                         CL_FindNonSolidLocation(pos, pos, 4);
1925                         if (cl_particles_quake.integer)
1926                                 CL_RunParticleEffect(pos, vec3_origin, 0, 20);
1927                         else
1928                         {
1929                                 CL_SparkShower(pos, vec3_origin, 15, 1, 0);
1930                                 CL_Smoke(pos, vec3_origin, 15, 0);
1931                         }
1932                         CL_BulletMark(pos);
1933                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1934                         CL_AllocDlight(NULL, &tempmatrix, 100, 0.15f, 0.15f, 1.5f, 500, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1935                         break;
1936
1937                 case TE_EXPLOSION:
1938                         // rocket explosion
1939                         MSG_ReadVector(pos, cls.protocol);
1940                         CL_FindNonSolidLocation(pos, pos, 10);
1941                         CL_ParticleExplosion(pos);
1942                         // LordHavoc: boosted color from 1.0, 0.8, 0.4 to 1.25, 1.0, 0.5
1943                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1944                         CL_AllocDlight(NULL, &tempmatrix, 350, 4.0f, 2.0f, 0.50f, 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1945                         if (gamemode != GAME_NEXUIZ)
1946                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1947                         break;
1948
1949                 case TE_EXPLOSIONQUAD:
1950                         // quad rocket explosion
1951                         MSG_ReadVector(pos, cls.protocol);
1952                         CL_FindNonSolidLocation(pos, pos, 10);
1953                         CL_ParticleExplosion(pos);
1954                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1955                         CL_AllocDlight(NULL, &tempmatrix, 350, 2.5f, 2.0f, 4.0f, 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1956                         if (gamemode != GAME_NEXUIZ)
1957                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1958                         break;
1959
1960                 case TE_EXPLOSION3:
1961                         // Nehahra movie colored lighting explosion
1962                         MSG_ReadVector(pos, cls.protocol);
1963                         CL_FindNonSolidLocation(pos, pos, 10);
1964                         CL_ParticleExplosion(pos);
1965                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1966                         color[0] = MSG_ReadCoord(cls.protocol) * (2.0f / 1.0f);
1967                         color[1] = MSG_ReadCoord(cls.protocol) * (2.0f / 1.0f);
1968                         color[2] = MSG_ReadCoord(cls.protocol) * (2.0f / 1.0f);
1969                         CL_AllocDlight(NULL, &tempmatrix, 350, color[0], color[1], color[2], 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1970                         if (gamemode != GAME_NEXUIZ)
1971                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1972                         break;
1973
1974                 case TE_EXPLOSIONRGB:
1975                         // colored lighting explosion
1976                         MSG_ReadVector(pos, cls.protocol);
1977                         CL_FindNonSolidLocation(pos, pos, 10);
1978                         CL_ParticleExplosion(pos);
1979                         color[0] = MSG_ReadByte() * (2.0f / 255.0f);
1980                         color[1] = MSG_ReadByte() * (2.0f / 255.0f);
1981                         color[2] = MSG_ReadByte() * (2.0f / 255.0f);
1982                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1983                         CL_AllocDlight(NULL, &tempmatrix, 350, color[0], color[1], color[2], 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1984                         if (gamemode != GAME_NEXUIZ)
1985                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1986                         break;
1987
1988                 case TE_TAREXPLOSION:
1989                         // tarbaby explosion
1990                         MSG_ReadVector(pos, cls.protocol);
1991                         CL_FindNonSolidLocation(pos, pos, 10);
1992                         CL_BlobExplosion(pos);
1993
1994                         if (gamemode != GAME_NEXUIZ)
1995                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
1996                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
1997                         CL_AllocDlight(NULL, &tempmatrix, 600, 1.6f, 0.8f, 2.0f, 1200, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1998                         break;
1999
2000                 case TE_SMALLFLASH:
2001                         MSG_ReadVector(pos, cls.protocol);
2002                         CL_FindNonSolidLocation(pos, pos, 10);
2003                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2004                         CL_AllocDlight(NULL, &tempmatrix, 200, 2, 2, 2, 1000, 0.2, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2005                         break;
2006
2007                 case TE_CUSTOMFLASH:
2008                         MSG_ReadVector(pos, cls.protocol);
2009                         CL_FindNonSolidLocation(pos, pos, 4);
2010                         radius = (MSG_ReadByte() + 1) * 8;
2011                         velspeed = (MSG_ReadByte() + 1) * (1.0 / 256.0);
2012                         color[0] = MSG_ReadByte() * (2.0f / 255.0f);
2013                         color[1] = MSG_ReadByte() * (2.0f / 255.0f);
2014                         color[2] = MSG_ReadByte() * (2.0f / 255.0f);
2015                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2016                         CL_AllocDlight(NULL, &tempmatrix, radius, color[0], color[1], color[2], radius / velspeed, velspeed, 0, -1, true, 1, 0.25, 1, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2017                         break;
2018
2019                 case TE_FLAMEJET:
2020                         MSG_ReadVector(pos, cls.protocol);
2021                         MSG_ReadVector(dir, cls.protocol);
2022                         count = MSG_ReadByte();
2023                         CL_Flames(pos, dir, count);
2024                         break;
2025
2026                 case TE_LIGHTNING1:
2027                         // lightning bolts
2028                         CL_ParseBeam(cl.model_bolt, true);
2029                         break;
2030
2031                 case TE_LIGHTNING2:
2032                         // lightning bolts
2033                         CL_ParseBeam(cl.model_bolt2, true);
2034                         break;
2035
2036                 case TE_LIGHTNING3:
2037                         // lightning bolts
2038                         CL_ParseBeam(cl.model_bolt3, false);
2039                         break;
2040
2041         // PGM 01/21/97
2042                 case TE_BEAM:
2043                         // grappling hook beam
2044                         CL_ParseBeam(cl.model_beam, false);
2045                         break;
2046         // PGM 01/21/97
2047
2048         // LordHavoc: for compatibility with the Nehahra movie...
2049                 case TE_LIGHTNING4NEH:
2050                         CL_ParseBeam(Mod_ForName(MSG_ReadString(), true, false, false), false);
2051                         break;
2052
2053                 case TE_LAVASPLASH:
2054                         MSG_ReadVector(pos, cls.protocol);
2055                         CL_LavaSplash(pos);
2056                         break;
2057
2058                 case TE_TELEPORT:
2059                         MSG_ReadVector(pos, cls.protocol);
2060                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2061                         CL_AllocDlight(NULL, &tempmatrix, 200, 1.0f, 1.0f, 1.0f, 600, 99.0f, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2062                         CL_TeleportSplash(pos);
2063                         break;
2064
2065                 case TE_EXPLOSION2:
2066                         // color mapped explosion
2067                         MSG_ReadVector(pos, cls.protocol);
2068                         CL_FindNonSolidLocation(pos, pos, 10);
2069                         colorStart = MSG_ReadByte();
2070                         colorLength = MSG_ReadByte();
2071                         CL_ParticleExplosion2(pos, colorStart, colorLength);
2072                         tempcolor = (unsigned char *)&palette_complete[(rand()%colorLength) + colorStart];
2073                         color[0] = tempcolor[0] * (2.0f / 255.0f);
2074                         color[1] = tempcolor[1] * (2.0f / 255.0f);
2075                         color[2] = tempcolor[2] * (2.0f / 255.0f);
2076                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2077                         CL_AllocDlight(NULL, &tempmatrix, 350, color[0], color[1], color[2], 700, 0.5, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2078                         if (gamemode != GAME_NEXUIZ)
2079                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
2080                         break;
2081
2082                 case TE_TEI_G3:
2083                         MSG_ReadVector(pos, cls.protocol);
2084                         MSG_ReadVector(pos2, cls.protocol);
2085                         MSG_ReadVector(dir, cls.protocol);
2086                         CL_BeamParticle(pos, pos2, 8, 1, 1, 1, 1, 1);
2087                         break;
2088
2089                 case TE_TEI_SMOKE:
2090                         MSG_ReadVector(pos, cls.protocol);
2091                         MSG_ReadVector(dir, cls.protocol);
2092                         count = MSG_ReadByte();
2093                         CL_FindNonSolidLocation(pos, pos, 4);
2094                         CL_Tei_Smoke(pos, dir, count);
2095                         break;
2096
2097                 case TE_TEI_BIGEXPLOSION:
2098                         MSG_ReadVector(pos, cls.protocol);
2099                         CL_FindNonSolidLocation(pos, pos, 10);
2100                         CL_ParticleExplosion(pos);
2101                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2102                         CL_AllocDlight(NULL, &tempmatrix, 500, 2.5f, 2.0f, 1.0f, 500, 9999, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2103                         if (gamemode != GAME_NEXUIZ)
2104                                 S_StartSound(-1, 0, cl.sfx_r_exp3, pos, 1, 1);
2105                         break;
2106
2107                 case TE_TEI_PLASMAHIT:
2108                         MSG_ReadVector(pos, cls.protocol);
2109                         MSG_ReadVector(dir, cls.protocol);
2110                         count = MSG_ReadByte();
2111                         CL_FindNonSolidLocation(pos, pos, 5);
2112                         CL_Tei_PlasmaHit(pos, dir, count);
2113                         Matrix4x4_CreateTranslate(&tempmatrix, pos[0], pos[1], pos[2]);
2114                         CL_AllocDlight(NULL, &tempmatrix, 500, 0.6, 1.2, 2.0f, 2000, 9999, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
2115                         break;
2116
2117                 default:
2118                         Host_Error("CL_ParseTempEntity: bad type %d (hex %02X)", type, type);
2119                 }
2120         }
2121 }
2122
2123 #define SHOWNET(x) if(cl_shownet.integer==2)Con_Printf("%3i:%s\n", msg_readcount-1, x);
2124
2125 //[515]: csqc
2126 void CL_VM_Init (void);
2127 qboolean CL_VM_Parse_TempEntity (void);
2128 void CL_VM_Parse_StuffCmd (const char *msg);
2129 void CL_VM_Parse_CenterPrint (const char *msg);
2130 void CSQC_AddPrintText (const char *msg);
2131 void CSQC_ReadEntities (void);
2132 //
2133 static unsigned char cgamenetbuffer[65536];
2134
2135 /*
2136 =====================
2137 CL_ParseServerMessage
2138 =====================
2139 */
2140 int parsingerror = false;
2141 void CL_ParseServerMessage(void)
2142 {
2143         int                     cmd;
2144         int                     i;
2145         protocolversion_t protocol;
2146         unsigned char           cmdlog[32];
2147         char            *cmdlogname[32], *temp;
2148         int                     cmdindex, cmdcount = 0;
2149
2150         if (cls.demorecording)
2151                 CL_WriteDemoMessage ();
2152
2153         cl.last_received_message = realtime;
2154
2155 //
2156 // if recording demos, copy the message out
2157 //
2158         if (cl_shownet.integer == 1)
2159                 Con_Printf("%f %i\n", realtime, net_message.cursize);
2160         else if (cl_shownet.integer == 2)
2161                 Con_Print("------------------\n");
2162
2163         cl.onground = false;    // unless the server says otherwise
2164 //
2165 // parse the message
2166 //
2167         //MSG_BeginReading ();
2168
2169         parsingerror = true;
2170
2171         if (cls.protocol == PROTOCOL_QUAKEWORLD)
2172         {
2173                 cl.mtime[1] = cl.mtime[0];
2174                 cl.mtime[0] = realtime; // qw has no clock
2175
2176                 while (1)
2177                 {
2178                         if (msg_badread)
2179                                 Host_Error ("CL_ParseServerMessage: Bad QW server message");
2180
2181                         cmd = MSG_ReadByte ();
2182
2183                         if (cmd == -1)
2184                         {
2185                                 SHOWNET("END OF MESSAGE");
2186                                 break;          // end of message
2187                         }
2188
2189                         cmdindex = cmdcount & 31;
2190                         cmdcount++;
2191                         cmdlog[cmdindex] = cmd;
2192
2193                         SHOWNET(qw_svc_strings[cmd]);
2194                         cmdlogname[cmdindex] = qw_svc_strings[cmd];
2195                         if (!cmdlogname[cmdindex])
2196                         {
2197                                 // LordHavoc: fix for bizarre problem in MSVC that I do not understand (if I assign the string pointer directly it ends up storing a NULL pointer)
2198                                 temp = "<unknown>";
2199                                 cmdlogname[cmdindex] = temp;
2200                         }
2201
2202                         // other commands
2203                         switch (cmd)
2204                         {
2205                         default:
2206                                 {
2207                                         char description[32*64], temp[64];
2208                                         int count;
2209                                         strcpy(description, "packet dump: ");
2210                                         i = cmdcount - 32;
2211                                         if (i < 0)
2212                                                 i = 0;
2213                                         count = cmdcount - i;
2214                                         i &= 31;
2215                                         while(count > 0)
2216                                         {
2217                                                 dpsnprintf(temp, sizeof(temp), "%3i:%s ", cmdlog[i], cmdlogname[i]);
2218                                                 strlcat(description, temp, sizeof(description));
2219                                                 count--;
2220                                                 i++;
2221                                                 i &= 31;
2222                                         }
2223                                         description[strlen(description)-1] = '\n'; // replace the last space with a newline
2224                                         Con_Print(description);
2225                                         Host_Error("CL_ParseServerMessage: Illegible server message");
2226                                 }
2227                                 break;
2228
2229                         case qw_svc_nop:
2230                                 //Con_Printf("qw_svc_nop\n");
2231                                 break;
2232
2233                         case qw_svc_disconnect:
2234                                 Con_Printf("Server disconnected\n");
2235                                 if (cls.demonum != -1)
2236                                         CL_NextDemo();
2237                                 else
2238                                         CL_Disconnect();
2239                                 break;
2240
2241                         case qw_svc_print:
2242                                 i = MSG_ReadByte();
2243                                 if (i == 3) // chat
2244                                         CSQC_AddPrintText(va("\1%s", MSG_ReadString()));        //[515]: csqc
2245                                 else
2246                                         CSQC_AddPrintText(MSG_ReadString());
2247                                 break;
2248
2249                         case qw_svc_centerprint:
2250                                 CL_VM_Parse_CenterPrint(MSG_ReadString ());     //[515]: csqc
2251                                 break;
2252
2253                         case qw_svc_stufftext:
2254                                 CL_VM_Parse_StuffCmd(MSG_ReadString ());        //[515]: csqc
2255                                 break;
2256
2257                         case qw_svc_damage:
2258                                 // svc_damage protocol is identical to nq
2259                                 V_ParseDamage ();
2260                                 break;
2261
2262                         case qw_svc_serverdata:
2263                                 //Cbuf_Execute(); // make sure any stuffed commands are done
2264                                 CL_ParseServerInfo();
2265                                 CL_VM_Init();   //[515]: init csqc
2266                                 break;
2267
2268                         case qw_svc_setangle:
2269                                 for (i=0 ; i<3 ; i++)
2270                                         cl.viewangles[i] = MSG_ReadAngle (cls.protocol);
2271                                 break;
2272
2273                         case qw_svc_lightstyle:
2274                                 i = MSG_ReadByte ();
2275                                 if (i >= cl_max_lightstyle)
2276                                 {
2277                                         Con_Printf ("svc_lightstyle >= MAX_LIGHTSTYLES");
2278                                         break;
2279                                 }
2280                                 strlcpy (cl_lightstyle[i].map,  MSG_ReadString(), sizeof (cl_lightstyle[i].map));
2281                                 cl_lightstyle[i].map[MAX_STYLESTRING - 1] = 0;
2282                                 cl_lightstyle[i].length = (int)strlen(cl_lightstyle[i].map);
2283                                 break;
2284
2285                         case qw_svc_sound:
2286                                 CL_ParseStartSoundPacket(false);
2287                                 break;
2288
2289                         case qw_svc_stopsound:
2290                                 i = (unsigned short) MSG_ReadShort();
2291                                 S_StopSound(i>>3, i&7);
2292                                 break;
2293
2294                         case qw_svc_updatefrags:
2295                                 i = MSG_ReadByte();
2296                                 if (i >= cl.maxclients)
2297                                         Host_Error("CL_ParseServerMessage: svc_updatefrags >= cl.maxclients");
2298                                 cl.scores[i].frags = (signed short) MSG_ReadShort();
2299                                 break;
2300
2301                         case qw_svc_updateping:
2302                                 i = MSG_ReadByte();
2303                                 if (i >= cl.maxclients)
2304                                         Host_Error("CL_ParseServerMessage: svc_updateping >= cl.maxclients");
2305                                 cl.scores[i].qw_ping = MSG_ReadShort();
2306                                 break;
2307
2308                         case qw_svc_updatepl:
2309                                 i = MSG_ReadByte();
2310                                 if (i >= cl.maxclients)
2311                                         Host_Error("CL_ParseServerMessage: svc_updatepl >= cl.maxclients");
2312                                 cl.scores[i].qw_packetloss = MSG_ReadByte();
2313                                 break;
2314
2315                         case qw_svc_updateentertime:
2316                                 i = MSG_ReadByte();
2317                                 if (i >= cl.maxclients)
2318                                         Host_Error("CL_ParseServerMessage: svc_updateentertime >= cl.maxclients");
2319                                 // seconds ago
2320                                 cl.scores[i].qw_entertime = realtime - MSG_ReadFloat();
2321                                 break;
2322
2323                         case qw_svc_spawnbaseline:
2324                                 i = (unsigned short) MSG_ReadShort();
2325                                 if (i < 0 || i >= MAX_EDICTS)
2326                                         Host_Error ("CL_ParseServerMessage: svc_spawnbaseline: invalid entity number %i", i);
2327                                 if (i >= cl_max_entities)
2328                                         CL_ExpandEntities(i);
2329                                 CL_ParseBaseline(cl_entities + i, false);
2330                                 break;
2331                         case qw_svc_spawnstatic:
2332                                 CL_ParseStatic(false);
2333                                 break;
2334                         case qw_svc_temp_entity:
2335                                 if(!CL_VM_Parse_TempEntity())
2336                                         CL_ParseTempEntity ();
2337                                 break;
2338
2339                         case qw_svc_killedmonster:
2340                                 cl.stats[STAT_MONSTERS]++;
2341                                 break;
2342
2343                         case qw_svc_foundsecret:
2344                                 cl.stats[STAT_SECRETS]++;
2345                                 break;
2346
2347                         case qw_svc_updatestat:
2348                                 i = MSG_ReadByte ();
2349                                 if (i < 0 || i >= MAX_CL_STATS)
2350                                         Host_Error ("svc_updatestat: %i is invalid", i);
2351                                 cl.stats[i] = MSG_ReadByte ();
2352                                 break;
2353
2354                         case qw_svc_updatestatlong:
2355                                 i = MSG_ReadByte ();
2356                                 if (i < 0 || i >= MAX_CL_STATS)
2357                                         Host_Error ("svc_updatestatlong: %i is invalid", i);
2358                                 cl.stats[i] = MSG_ReadLong ();
2359                                 break;
2360
2361                         case qw_svc_spawnstaticsound:
2362                                 CL_ParseStaticSound (false);
2363                                 break;
2364
2365                         case qw_svc_cdtrack:
2366                                 cl.cdtrack = cl.looptrack = MSG_ReadByte ();
2367                                 if ( (cls.demoplayback || cls.demorecording) && (cls.forcetrack != -1) )
2368                                         CDAudio_Play ((unsigned char)cls.forcetrack, true);
2369                                 else
2370                                         CDAudio_Play ((unsigned char)cl.cdtrack, true);
2371                                 break;
2372
2373                         case qw_svc_intermission:
2374                                 cl.intermission = 1;
2375                                 cl.completed_time = cl.time;
2376                                 MSG_ReadVector(cl.qw_intermission_origin, cls.protocol);
2377                                 for (i = 0;i < 3;i++)
2378                                         cl.qw_intermission_angles[i] = MSG_ReadAngle(cls.protocol);
2379                                 break;
2380
2381                         case qw_svc_finale:
2382                                 cl.intermission = 2;
2383                                 cl.completed_time = cl.time;
2384                                 SCR_CenterPrint(MSG_ReadString ());
2385                                 break;
2386
2387                         case qw_svc_sellscreen:
2388                                 Cmd_ExecuteString ("help", src_command);
2389                                 break;
2390
2391                         case qw_svc_smallkick:
2392                                 Con_Printf("TODO: qw_svc_smallkick\n");
2393                                 break;
2394                         case qw_svc_bigkick:
2395                                 Con_Printf("TODO: qw_svc_bigkick\n");
2396                                 break;
2397
2398                         case qw_svc_muzzleflash:
2399                                 i = (unsigned short) MSG_ReadShort();
2400                                 // NOTE: in QW this only worked on clients
2401                                 if (i < 0 || i >= MAX_EDICTS)
2402                                         Host_Error("CL_ParseServerMessage: svc_spawnbaseline: invalid entity number %i", i);
2403                                 if (i >= cl_max_entities)
2404                                         CL_ExpandEntities(i);
2405                                 cl_entities[i].persistent.muzzleflash = 1.0f;
2406                                 break;
2407
2408                         case qw_svc_updateuserinfo:
2409                                 QW_CL_UpdateUserInfo();
2410                                 break;
2411
2412                         case qw_svc_setinfo:
2413                                 QW_CL_SetInfo();
2414                                 break;
2415
2416                         case qw_svc_serverinfo:
2417                                 QW_CL_ServerInfo();
2418                                 break;
2419
2420                         case qw_svc_download:
2421                                 QW_CL_ParseDownload();
2422                                 break;
2423
2424                         case qw_svc_playerinfo:
2425                                 EntityStateQW_ReadPlayerUpdate();
2426                                 break;
2427
2428                         case qw_svc_nails:
2429                                 QW_CL_ParseNails();
2430                                 break;
2431
2432                         case qw_svc_chokecount:
2433                                 i = MSG_ReadByte();
2434                                 // FIXME: apply to netgraph
2435                                 //for (j = 0;j < i;j++)
2436                                 //      cl.frames[(cls.netcon->qw.incoming_acknowledged-1-j)&QW_UPDATE_MASK].receivedtime = -2;
2437                                 break;
2438
2439                         case qw_svc_modellist:
2440                                 QW_CL_ParseModelList();
2441                                 break;
2442
2443                         case qw_svc_soundlist:
2444                                 QW_CL_ParseSoundList();
2445                                 break;
2446
2447                         case qw_svc_packetentities:
2448                                 EntityFrameQW_CL_ReadFrame(false);
2449                                 // first update is the final signon stage
2450                                 if (cls.signon == SIGNONS - 1)
2451                                         cls.signon = SIGNONS;
2452                                 break;
2453
2454                         case qw_svc_deltapacketentities:
2455                                 EntityFrameQW_CL_ReadFrame(true);
2456                                 // first update is the final signon stage
2457                                 if (cls.signon == SIGNONS - 1)
2458                                         cls.signon = SIGNONS;
2459                                 break;
2460
2461                         case qw_svc_maxspeed:
2462                                 cl.qw_movevars_maxspeed = MSG_ReadFloat();
2463                                 break;
2464
2465                         case qw_svc_entgravity:
2466                                 cl.qw_movevars_entgravity = MSG_ReadFloat();
2467                                 break;
2468
2469                         case qw_svc_setpause:
2470                                 cl.paused = MSG_ReadByte ();
2471                                 if (cl.paused)
2472                                         CDAudio_Pause ();
2473                                 else
2474                                         CDAudio_Resume ();
2475                                 S_PauseGameSounds (cl.paused);
2476                                 break;
2477                         }
2478                 }
2479                 QW_CL_UpdateItemsAndWeapon();
2480         }
2481         else
2482         {
2483                 while (1)
2484                 {
2485                         if (msg_badread)
2486                                 Host_Error ("CL_ParseServerMessage: Bad server message");
2487
2488                         cmd = MSG_ReadByte ();
2489
2490                         if (cmd == -1)
2491                         {
2492                                 SHOWNET("END OF MESSAGE");
2493                                 break;          // end of message
2494                         }
2495
2496                         cmdindex = cmdcount & 31;
2497                         cmdcount++;
2498                         cmdlog[cmdindex] = cmd;
2499
2500                         // if the high bit of the command byte is set, it is a fast update
2501                         if (cmd & 128)
2502                         {
2503                                 // LordHavoc: fix for bizarre problem in MSVC that I do not understand (if I assign the string pointer directly it ends up storing a NULL pointer)
2504                                 temp = "entity";
2505                                 cmdlogname[cmdindex] = temp;
2506                                 SHOWNET("fast update");
2507                                 if (cls.signon == SIGNONS - 1)
2508                                 {
2509                                         // first update is the final signon stage
2510                                         cls.signon = SIGNONS;
2511                                         CL_SignonReply ();
2512                                 }
2513                                 EntityFrameQuake_ReadEntity (cmd&127);
2514                                 continue;
2515                         }
2516
2517                         SHOWNET(svc_strings[cmd]);
2518                         cmdlogname[cmdindex] = svc_strings[cmd];
2519                         if (!cmdlogname[cmdindex])
2520                         {
2521                                 // LordHavoc: fix for bizarre problem in MSVC that I do not understand (if I assign the string pointer directly it ends up storing a NULL pointer)
2522                                 temp = "<unknown>";
2523                                 cmdlogname[cmdindex] = temp;
2524                         }
2525
2526                         // other commands
2527                         switch (cmd)
2528                         {
2529                         default:
2530                                 {
2531                                         char description[32*64], temp[64];
2532                                         int count;
2533                                         strcpy (description, "packet dump: ");
2534                                         i = cmdcount - 32;
2535                                         if (i < 0)
2536                                                 i = 0;
2537                                         count = cmdcount - i;
2538                                         i &= 31;
2539                                         while(count > 0)
2540                                         {
2541                                                 dpsnprintf (temp, sizeof (temp), "%3i:%s ", cmdlog[i], cmdlogname[i]);
2542                                                 strlcat (description, temp, sizeof (description));
2543                                                 count--;
2544                                                 i++;
2545                                                 i &= 31;
2546                                         }
2547                                         description[strlen(description)-1] = '\n'; // replace the last space with a newline
2548                                         Con_Print(description);
2549                                         Host_Error ("CL_ParseServerMessage: Illegible server message");
2550                                 }
2551                                 break;
2552
2553                         case svc_nop:
2554                                 if (cls.signon < SIGNONS)
2555                                         Con_Print("<-- server to client keepalive\n");
2556                                 break;
2557
2558                         case svc_time:
2559                                 cl.mtime[1] = cl.mtime[0];
2560                                 cl.mtime[0] = MSG_ReadFloat ();
2561                                 cl.movement_needupdate = true;
2562                                 break;
2563
2564                         case svc_clientdata:
2565                                 CL_ParseClientdata();
2566                                 break;
2567
2568                         case svc_version:
2569                                 i = MSG_ReadLong ();
2570                                 protocol = Protocol_EnumForNumber(i);
2571                                 if (protocol == PROTOCOL_UNKNOWN)
2572                                         Host_Error("CL_ParseServerMessage: Server is unrecognized protocol number (%i)", i);
2573                                 // hack for unmarked Nehahra movie demos which had a custom protocol
2574                                 if (protocol == PROTOCOL_QUAKEDP && cls.demoplayback && demo_nehahra.integer)
2575                                         protocol = PROTOCOL_NEHAHRAMOVIE;
2576                                 cls.protocol = protocol;
2577                                 break;
2578
2579                         case svc_disconnect:
2580                                 Con_Printf ("Server disconnected\n");
2581                                 if (cls.demonum != -1)
2582                                         CL_NextDemo ();
2583                                 else
2584                                         CL_Disconnect ();
2585                                 break;
2586
2587                         case svc_print:
2588                                 CSQC_AddPrintText(MSG_ReadString());    //[515]: csqc
2589                                 break;
2590
2591                         case svc_centerprint:
2592                                 CL_VM_Parse_CenterPrint(MSG_ReadString ());     //[515]: csqc
2593                                 break;
2594
2595                         case svc_stufftext:
2596                                 CL_VM_Parse_StuffCmd(MSG_ReadString ());        //[515]: csqc
2597                                 break;
2598
2599                         case svc_damage:
2600                                 V_ParseDamage ();
2601                                 break;
2602
2603                         case svc_serverinfo:
2604                                 CL_ParseServerInfo ();
2605                                 CL_VM_Init();   //[515]: init csqc
2606                                 break;
2607
2608                         case svc_setangle:
2609                                 for (i=0 ; i<3 ; i++)
2610                                         cl.viewangles[i] = MSG_ReadAngle (cls.protocol);
2611                                 break;
2612
2613                         case svc_setview:
2614                                 cl.viewentity = (unsigned short)MSG_ReadShort ();
2615                                 if (cl.viewentity >= MAX_EDICTS)
2616                                         Host_Error("svc_setview >= MAX_EDICTS");
2617                                 if (cl.viewentity >= cl_max_entities)
2618                                         CL_ExpandEntities(cl.viewentity);
2619                                 // LordHavoc: assume first setview recieved is the real player entity
2620                                 if (!cl.playerentity)
2621                                         cl.playerentity = cl.viewentity;
2622                                 break;
2623
2624                         case svc_lightstyle:
2625                                 i = MSG_ReadByte ();
2626                                 if (i >= cl_max_lightstyle)
2627                                 {
2628                                         Con_Printf ("svc_lightstyle >= MAX_LIGHTSTYLES");
2629                                         break;
2630                                 }
2631                                 strlcpy (cl_lightstyle[i].map,  MSG_ReadString(), sizeof (cl_lightstyle[i].map));
2632                                 cl_lightstyle[i].map[MAX_STYLESTRING - 1] = 0;
2633                                 cl_lightstyle[i].length = (int)strlen(cl_lightstyle[i].map);
2634                                 break;
2635
2636                         case svc_sound:
2637                                 CL_ParseStartSoundPacket(false);
2638                                 break;
2639
2640                         case svc_precache:
2641                                 if (cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3)
2642                                 {
2643                                         // was svc_sound2 in protocols 1, 2, 3, removed in 4, 5, changed to svc_precache in 6
2644                                         CL_ParseStartSoundPacket(true);
2645                                 }
2646                                 else
2647                                 {
2648                                         int i = (unsigned short)MSG_ReadShort();
2649                                         char *s = MSG_ReadString();
2650                                         if (i < 32768)
2651                                         {
2652                                                 if (i >= 1 && i < MAX_MODELS)
2653                                                 {
2654                                                         model_t *model = Mod_ForName(s, false, false, i == 1);
2655                                                         if (!model)
2656                                                                 Con_Printf("svc_precache: Mod_ForName(\"%s\") failed\n", s);
2657                                                         cl.model_precache[i] = model;
2658                                                 }
2659                                                 else
2660                                                         Con_Printf("svc_precache: index %i outside range %i...%i\n", i, 1, MAX_MODELS);
2661                                         }
2662                                         else
2663                                         {
2664                                                 i -= 32768;
2665                                                 if (i >= 1 && i < MAX_SOUNDS)
2666                                                 {
2667                                                         sfx_t *sfx = S_PrecacheSound (s, true, false);
2668                                                         if (!sfx && snd_initialized.integer)
2669                                                                 Con_Printf("svc_precache: S_PrecacheSound(\"%s\") failed\n", s);
2670                                                         cl.sound_precache[i] = sfx;
2671                                                 }
2672                                                 else
2673                                                         Con_Printf("svc_precache: index %i outside range %i...%i\n", i, 1, MAX_SOUNDS);
2674                                         }
2675                                 }
2676                                 break;
2677
2678                         case svc_stopsound:
2679                                 i = (unsigned short) MSG_ReadShort();
2680                                 S_StopSound(i>>3, i&7);
2681                                 break;
2682
2683                         case svc_updatename:
2684                                 i = MSG_ReadByte ();
2685                                 if (i >= cl.maxclients)
2686                                         Host_Error ("CL_ParseServerMessage: svc_updatename >= cl.maxclients");
2687                                 strlcpy (cl.scores[i].name, MSG_ReadString (), sizeof (cl.scores[i].name));
2688                                 break;
2689
2690                         case svc_updatefrags:
2691                                 i = MSG_ReadByte ();
2692                                 if (i >= cl.maxclients)
2693                                         Host_Error ("CL_ParseServerMessage: svc_updatefrags >= cl.maxclients");
2694                                 cl.scores[i].frags = (signed short) MSG_ReadShort ();
2695                                 break;
2696
2697                         case svc_updatecolors:
2698                                 i = MSG_ReadByte ();
2699                                 if (i >= cl.maxclients)
2700                                         Host_Error ("CL_ParseServerMessage: svc_updatecolors >= cl.maxclients");
2701                                 cl.scores[i].colors = MSG_ReadByte ();
2702                                 break;
2703
2704                         case svc_particle:
2705                                 CL_ParseParticleEffect ();
2706                                 break;
2707
2708                         case svc_effect:
2709                                 CL_ParseEffect ();
2710                                 break;
2711
2712                         case svc_effect2:
2713                                 CL_ParseEffect2 ();
2714                                 break;
2715
2716                         case svc_spawnbaseline:
2717                                 i = (unsigned short) MSG_ReadShort ();
2718                                 if (i < 0 || i >= MAX_EDICTS)
2719                                         Host_Error ("CL_ParseServerMessage: svc_spawnbaseline: invalid entity number %i", i);
2720                                 if (i >= cl_max_entities)
2721                                         CL_ExpandEntities(i);
2722                                 CL_ParseBaseline (cl_entities + i, false);
2723                                 break;
2724                         case svc_spawnbaseline2:
2725                                 i = (unsigned short) MSG_ReadShort ();
2726                                 if (i < 0 || i >= MAX_EDICTS)
2727                                         Host_Error ("CL_ParseServerMessage: svc_spawnbaseline2: invalid entity number %i", i);
2728                                 if (i >= cl_max_entities)
2729                                         CL_ExpandEntities(i);
2730                                 CL_ParseBaseline (cl_entities + i, true);
2731                                 break;
2732                         case svc_spawnstatic:
2733                                 CL_ParseStatic (false);
2734                                 break;
2735                         case svc_spawnstatic2:
2736                                 CL_ParseStatic (true);
2737                                 break;
2738                         case svc_temp_entity:
2739                                 if(!CL_VM_Parse_TempEntity())
2740                                         CL_ParseTempEntity ();
2741                                 break;
2742
2743                         case svc_setpause:
2744                                 cl.paused = MSG_ReadByte ();
2745                                 if (cl.paused)
2746                                         CDAudio_Pause ();
2747                                 else
2748                                         CDAudio_Resume ();
2749                                 S_PauseGameSounds (cl.paused);
2750                                 break;
2751
2752                         case svc_signonnum:
2753                                 i = MSG_ReadByte ();
2754                                 // LordHavoc: it's rude to kick off the client if they missed the
2755                                 // reconnect somehow, so allow signon 1 even if at signon 1
2756                                 if (i <= cls.signon && i != 1)
2757                                         Host_Error ("Received signon %i when at %i", i, cls.signon);
2758                                 cls.signon = i;
2759                                 CL_SignonReply ();
2760                                 break;
2761
2762                         case svc_killedmonster:
2763                                 cl.stats[STAT_MONSTERS]++;
2764                                 break;
2765
2766                         case svc_foundsecret:
2767                                 cl.stats[STAT_SECRETS]++;
2768                                 break;
2769
2770                         case svc_updatestat:
2771                                 i = MSG_ReadByte ();
2772                                 if (i < 0 || i >= MAX_CL_STATS)
2773                                         Host_Error ("svc_updatestat: %i is invalid", i);
2774                                 cl.stats[i] = MSG_ReadLong ();
2775                                 break;
2776
2777                         case svc_updatestatubyte:
2778                                 i = MSG_ReadByte ();
2779                                 if (i < 0 || i >= MAX_CL_STATS)
2780                                         Host_Error ("svc_updatestat: %i is invalid", i);
2781                                 cl.stats[i] = MSG_ReadByte ();
2782                                 break;
2783
2784                         case svc_spawnstaticsound:
2785                                 CL_ParseStaticSound (false);
2786                                 break;
2787
2788                         case svc_spawnstaticsound2:
2789                                 CL_ParseStaticSound (true);
2790                                 break;
2791
2792                         case svc_cdtrack:
2793                                 cl.cdtrack = MSG_ReadByte ();
2794                                 cl.looptrack = MSG_ReadByte ();
2795                                 if ( (cls.demoplayback || cls.demorecording) && (cls.forcetrack != -1) )
2796                                         CDAudio_Play ((unsigned char)cls.forcetrack, true);
2797                                 else
2798                                         CDAudio_Play ((unsigned char)cl.cdtrack, true);
2799                                 break;
2800
2801                         case svc_intermission:
2802                                 cl.intermission = 1;
2803                                 cl.completed_time = cl.time;
2804                                 break;
2805
2806                         case svc_finale:
2807                                 cl.intermission = 2;
2808                                 cl.completed_time = cl.time;
2809                                 SCR_CenterPrint(MSG_ReadString ());
2810                                 break;
2811
2812                         case svc_cutscene:
2813                                 cl.intermission = 3;
2814                                 cl.completed_time = cl.time;
2815                                 SCR_CenterPrint(MSG_ReadString ());
2816                                 break;
2817
2818                         case svc_sellscreen:
2819                                 Cmd_ExecuteString ("help", src_command);
2820                                 break;
2821                         case svc_hidelmp:
2822                                 if (gamemode == GAME_TENEBRAE)
2823                                 {
2824                                         // repeating particle effect
2825                                         MSG_ReadCoord(cls.protocol);
2826                                         MSG_ReadCoord(cls.protocol);
2827                                         MSG_ReadCoord(cls.protocol);
2828                                         MSG_ReadCoord(cls.protocol);
2829                                         MSG_ReadCoord(cls.protocol);
2830                                         MSG_ReadCoord(cls.protocol);
2831                                         MSG_ReadByte();
2832                                         MSG_ReadLong();
2833                                         MSG_ReadLong();
2834                                         MSG_ReadString();
2835                                 }
2836                                 else
2837                                         SHOWLMP_decodehide();
2838                                 break;
2839                         case svc_showlmp:
2840                                 if (gamemode == GAME_TENEBRAE)
2841                                 {
2842                                         // particle effect
2843                                         MSG_ReadCoord(cls.protocol);
2844                                         MSG_ReadCoord(cls.protocol);
2845                                         MSG_ReadCoord(cls.protocol);
2846                                         MSG_ReadByte();
2847                                         MSG_ReadString();
2848                                 }
2849                                 else
2850                                         SHOWLMP_decodeshow();
2851                                 break;
2852                         case svc_skybox:
2853                                 R_SetSkyBox(MSG_ReadString());
2854                                 break;
2855                         case svc_cgame:
2856                                 {
2857                                         int length;
2858                                         length = (int) ((unsigned short) MSG_ReadShort());
2859                                         for (i = 0;i < length;i++)
2860                                                 cgamenetbuffer[i] = MSG_ReadByte();
2861                                         if (!msg_badread)
2862                                                 CL_CGVM_ParseNetwork(cgamenetbuffer, length);
2863                                 }
2864                                 break;
2865                         case svc_entities:
2866                                 if (cls.signon == SIGNONS - 1)
2867                                 {
2868                                         // first update is the final signon stage
2869                                         cls.signon = SIGNONS;
2870                                         CL_SignonReply ();
2871                                 }
2872                                 if (cls.protocol == PROTOCOL_DARKPLACES1 || cls.protocol == PROTOCOL_DARKPLACES2 || cls.protocol == PROTOCOL_DARKPLACES3)
2873                                         EntityFrame_CL_ReadFrame();
2874                                 else if (cls.protocol == PROTOCOL_DARKPLACES4)
2875                                         EntityFrame4_CL_ReadFrame();
2876                                 else
2877                                         EntityFrame5_CL_ReadFrame();
2878                                 break;
2879                         case svc_csqcentities:
2880                                 CSQC_ReadEntities();
2881                                 break;
2882                         }
2883                 }
2884         }
2885
2886         EntityFrameQuake_ISeeDeadEntities();
2887
2888         parsingerror = false;
2889 }
2890
2891 void CL_Parse_DumpPacket(void)
2892 {
2893         if (!parsingerror)
2894                 return;
2895         Con_Print("Packet dump:\n");
2896         SZ_HexDumpToConsole(&net_message);
2897         parsingerror = false;
2898 }
2899
2900 void CL_Parse_ErrorCleanUp(void)
2901 {
2902         if (cls.qw_downloadmemory)
2903         {
2904                 Mem_Free(cls.qw_downloadmemory);
2905                 cls.qw_downloadmemory = NULL;
2906         }
2907         cls.qw_downloadpercent = 0;
2908         QW_CL_StopUpload();
2909 }
2910
2911 void CL_Parse_Init(void)
2912 {
2913         // LordHavoc: added demo_nehahra cvar
2914         Cvar_RegisterVariable (&demo_nehahra);
2915         if (gamemode == GAME_NEHAHRA)
2916                 Cvar_SetValue("demo_nehahra", 1);
2917         Cvar_RegisterVariable(&developer_networkentities);
2918
2919         Cmd_AddCommand("nextul", QW_CL_NextUpload, "sends next fragment of current upload buffer (screenshot for example)");
2920         Cmd_AddCommand("stopul", QW_CL_StopUpload, "aborts current upload (screenshot for example)");
2921 }
2922
2923 void CL_Parse_Shutdown(void)
2924 {
2925 }