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