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