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