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