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