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