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