implemented csprogs.dat downloading with special dlcache/name.size.crc file naming...
[divverent/darkplaces.git] / cl_main.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_main.c  -- client main loop
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "cl_video.h"
25 #include "image.h"
26 #include "csprogs.h"
27 #include "r_shadow.h"
28 #include "libcurl.h"
29
30 // we need to declare some mouse variables here, because the menu system
31 // references them even when on a unix system.
32
33 cvar_t csqc_progname = {0, "csqc_progname","csprogs.dat","name of csprogs.dat file to load"};   //[515]: csqc crc check and right csprogs name according to progs.dat
34 cvar_t csqc_progcrc = {CVAR_READONLY, "csqc_progcrc","-1","CRC of csprogs.dat file to load (-1 is none), only used during level changes and then reset to -1"};
35 cvar_t csqc_progsize = {CVAR_READONLY, "csqc_progsize","-1","file size of csprogs.dat file to load (-1 is none), only used during level changes and then reset to -1"};
36
37 cvar_t cl_shownet = {0, "cl_shownet","0","1 = print packet size, 2 = print packet message list"};
38 cvar_t cl_nolerp = {0, "cl_nolerp", "0","network update smoothing"};
39
40 cvar_t cl_itembobheight = {0, "cl_itembobheight", "0","how much items bob up and down (try 8)"};
41 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5","how frequently items bob up and down"};
42
43 cvar_t lookspring = {CVAR_SAVE, "lookspring","0","returns pitch to level with the floor when no longer holding a pitch key"};
44 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0","move instead of turning"};
45 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3","mouse speed multiplier"};
46
47 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022","mouse pitch speed multiplier"};
48 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022","mouse yaw speed multiplier"};
49 cvar_t m_forward = {CVAR_SAVE, "m_forward","1","mouse forward speed multiplier"};
50 cvar_t m_side = {CVAR_SAVE, "m_side","0.8","mouse side speed multiplier"};
51
52 cvar_t freelook = {CVAR_SAVE, "freelook", "1","mouse controls pitch instead of forward/back"};
53
54 #ifdef AUTODEMO_BROKEN
55 cvar_t cl_autodemo = {0, "cl_autodemo", "0", "records every game played, using the date/time and map name to name the demo file" };
56 cvar_t cl_autodemo_nameformat = {0, "cl_autodemo_nameformat", "%Y-%m-%d_%H-%M", "The format of the cl_autodemo filename, followed by the map name" };
57 #endif
58
59 cvar_t r_draweffects = {0, "r_draweffects", "1","renders temporary sprite effects"};
60
61 cvar_t cl_explosions_alpha_start = {CVAR_SAVE, "cl_explosions_alpha_start", "1.5","starting alpha of an explosion shell"};
62 cvar_t cl_explosions_alpha_end = {CVAR_SAVE, "cl_explosions_alpha_end", "0","end alpha of an explosion shell (just before it disappears)"};
63 cvar_t cl_explosions_size_start = {CVAR_SAVE, "cl_explosions_size_start", "16","starting size of an explosion shell"};
64 cvar_t cl_explosions_size_end = {CVAR_SAVE, "cl_explosions_size_end", "128","ending alpha of an explosion shell (just before it disappears)"};
65 cvar_t cl_explosions_lifetime = {CVAR_SAVE, "cl_explosions_lifetime", "0.5","how long an explosion shell lasts"};
66
67 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1","stains lightmaps, much faster than decals but blurred"};
68 cvar_t cl_stainmaps_clearonload = {CVAR_SAVE, "cl_stainmaps_clearonload", "1","clear stainmaps on map restart"};
69
70 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1","use beam polygons instead of models"};
71 cvar_t cl_beams_quakepositionhack = {CVAR_SAVE, "cl_beams_quakepositionhack", "1", "makes your lightning gun appear to fire from your waist (as in Quake and QuakeWorld)"};
72 cvar_t cl_beams_instantaimhack = {CVAR_SAVE, "cl_beams_instantaimhack", "1", "makes your lightning gun aiming update instantly"};
73 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0", "make a light at the end of the beam"};
74
75 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0","hide player shadow"};
76
77 cvar_t qport = {0, "qport", "0", "identification key for playing on qw servers (allows you to maintain a connection to a quakeworld server even if your port changes)"};
78
79 cvar_t cl_prydoncursor = {0, "cl_prydoncursor", "0", "enables a mouse pointer which is able to click on entities in the world, useful for point and click mods, see PRYDON_CLIENTCURSOR extension in dpextensions.qc"};
80
81 cvar_t cl_deathnoviewmodel = {0, "cl_deathnoviewmodel", "1", "hides gun model when dead"};
82
83 client_static_t cls;
84 client_state_t  cl;
85
86 #define MAX_PARTICLES                   32768   // default max # of particles at one time
87 #define ABSOLUTE_MIN_PARTICLES  512             // no fewer than this no matter what's on the command line
88
89 /*
90 =====================
91 CL_ClearState
92
93 =====================
94 */
95 void CL_ClearState(void)
96 {
97         int i;
98         entity_t *ent;
99
100 // wipe the entire cl structure
101         Mem_EmptyPool(cls.levelmempool);
102         memset (&cl, 0, sizeof(cl));
103
104         S_StopAllSounds();
105
106         // reset the view zoom interpolation
107         cl.mviewzoom[0] = cl.mviewzoom[1] = 1;
108
109         cl.num_entities = 0;
110         cl.num_csqcentities = 0;        //[515]: csqc
111         cl.num_static_entities = 0;
112         cl.num_temp_entities = 0;
113         cl.num_brushmodel_entities = 0;
114
115         // tweak these if the game runs out
116         cl.max_entities = 256;
117         cl.max_csqcentities = 256;      //[515]: csqc
118         cl.max_static_entities = 256;
119         cl.max_temp_entities = 512;
120         cl.max_effects = 256;
121         cl.max_beams = 256;
122         cl.max_dlights = MAX_DLIGHTS;
123         cl.max_lightstyle = MAX_LIGHTSTYLES;
124         cl.max_brushmodel_entities = MAX_EDICTS;
125         cl.max_particles = MAX_PARTICLES;
126
127 // COMMANDLINEOPTION: Client: -particles <number> changes maximum number of particles at once, default 32768
128         i = COM_CheckParm ("-particles");
129         if (i && i < com_argc - 1)
130         {
131                 cl.max_particles = (int)(atoi(com_argv[i+1]));
132                 if (cl.max_particles < ABSOLUTE_MIN_PARTICLES)
133                         cl.max_particles = ABSOLUTE_MIN_PARTICLES;
134         }
135
136         cl.num_dlights = 0;
137         cl.num_effects = 0;
138         cl.num_beams = 0;
139
140         cl.entities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_entities * sizeof(entity_t));
141         cl.csqcentities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_csqcentities * sizeof(entity_t));      //[515]: csqc
142         cl.entities_active = (unsigned char *)Mem_Alloc(cls.levelmempool, cl.max_brushmodel_entities * sizeof(unsigned char));
143         cl.csqcentities_active = (unsigned char *)Mem_Alloc(cls.levelmempool, cl.max_brushmodel_entities * sizeof(unsigned char));      //[515]: csqc
144         cl.static_entities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_static_entities * sizeof(entity_t));
145         cl.temp_entities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_temp_entities * sizeof(entity_t));
146         cl.effects = (cl_effect_t *)Mem_Alloc(cls.levelmempool, cl.max_effects * sizeof(cl_effect_t));
147         cl.beams = (beam_t *)Mem_Alloc(cls.levelmempool, cl.max_beams * sizeof(beam_t));
148         cl.dlights = (dlight_t *)Mem_Alloc(cls.levelmempool, cl.max_dlights * sizeof(dlight_t));
149         cl.lightstyle = (lightstyle_t *)Mem_Alloc(cls.levelmempool, cl.max_lightstyle * sizeof(lightstyle_t));
150         cl.brushmodel_entities = (int *)Mem_Alloc(cls.levelmempool, cl.max_brushmodel_entities * sizeof(int));
151         cl.particles = (particle_t *) Mem_Alloc(cls.levelmempool, cl.max_particles * sizeof(particle_t));
152
153         // LordHavoc: have to set up the baseline info for alpha and other stuff
154         for (i = 0;i < cl.max_entities;i++)
155         {
156                 cl.entities[i].state_baseline = defaultstate;
157                 cl.entities[i].state_previous = defaultstate;
158                 cl.entities[i].state_current = defaultstate;
159         }
160
161         for (i = 0;i < cl.max_csqcentities;i++)
162         {
163                 cl.csqcentities[i].state_baseline = defaultstate;       //[515]: csqc
164                 cl.csqcentities[i].state_previous = defaultstate;       //[515]: csqc
165                 cl.csqcentities[i].state_current = defaultstate;        //[515]: csqc
166                 cl.csqcentities[i].csqc = true;
167                 cl.csqcentities[i].state_current.number = -i;
168         }
169
170         if (gamemode == GAME_NEXUIZ)
171         {
172                 VectorSet(cl.playerstandmins, -16, -16, -24);
173                 VectorSet(cl.playerstandmaxs, 16, 16, 45);
174                 VectorSet(cl.playercrouchmins, -16, -16, -24);
175                 VectorSet(cl.playercrouchmaxs, 16, 16, 25);
176         }
177         else
178         {
179                 VectorSet(cl.playerstandmins, -16, -16, -24);
180                 VectorSet(cl.playerstandmaxs, 16, 16, 24);
181                 VectorSet(cl.playercrouchmins, -16, -16, -24);
182                 VectorSet(cl.playercrouchmaxs, 16, 16, 24);
183         }
184
185         // disable until we get textures for it
186         R_ResetSkyBox();
187
188         ent = &cl.entities[0];
189         // entire entity array was cleared, so just fill in a few fields
190         ent->state_current.active = true;
191         ent->render.model = cl.worldmodel = NULL; // no world model yet
192         ent->render.alpha = 1;
193         ent->render.colormap = -1; // no special coloring
194         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
195         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
196         CL_UpdateRenderEntity(&ent->render);
197
198         // noclip is turned off at start
199         noclip_anglehack = false;
200
201         // mark all frames invalid for delta
202         memset(cl.qw_deltasequence, -1, sizeof(cl.qw_deltasequence));
203
204         CL_Screen_NewMap();
205 }
206
207 void CL_SetInfo(const char *key, const char *value, qboolean send, qboolean allowstarkey, qboolean allowmodel, qboolean quiet)
208 {
209         if (strchr(key, '\"') || strchr(value, '\"') || (!allowstarkey && key[0] == '*') || (!allowmodel && (!strcasecmp(key, "pmodel") || !strcasecmp(key, "emodel"))))
210         {
211                 if (!quiet)
212                         Con_Printf("Can't setinfo \"%s\" \"%s\"\n", key, value);
213                 return;
214         }
215         InfoString_SetValue(cls.userinfo, sizeof(cls.userinfo), key, value);
216         if (cls.state == ca_connected && cls.netcon)
217         {
218                 if (cls.protocol == PROTOCOL_QUAKEWORLD)
219                 {
220                         MSG_WriteByte(&cls.netcon->message, qw_clc_stringcmd);
221                         MSG_WriteString(&cls.netcon->message, va("setinfo \"%s\" \"%s\"", key, value));
222                 }
223                 else if (!strcasecmp(key, "name"))
224                 {
225                         MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
226                         MSG_WriteString(&cls.netcon->message, va("name \"%s\"", value));
227                 }
228                 else if (!strcasecmp(key, "playermodel"))
229                 {
230                         MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
231                         MSG_WriteString(&cls.netcon->message, va("playermodel \"%s\"", value));
232                 }
233                 else if (!strcasecmp(key, "playerskin"))
234                 {
235                         MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
236                         MSG_WriteString(&cls.netcon->message, va("playerskin \"%s\"", value));
237                 }
238                 else if (!strcasecmp(key, "topcolor"))
239                 {
240                         // don't send anything, the combined color code will be updated manually
241                 }
242                 else if (!strcasecmp(key, "bottomcolor"))
243                 {
244                         // don't send anything, the combined color code will be updated manually
245                 }
246                 else if (!strcasecmp(key, "rate"))
247                 {
248                         MSG_WriteByte(&cls.netcon->message, clc_stringcmd);
249                         MSG_WriteString(&cls.netcon->message, va("rate \"%s\"", value));
250                 }
251         }
252 }
253
254 void CL_ExpandEntities(int num)
255 {
256         int i, oldmaxentities;
257         entity_t *oldentities;
258         if (num >= cl.max_entities)
259         {
260                 if (!cl.entities)
261                         Sys_Error("CL_ExpandEntities: cl.entities not initialized");
262                 if (num >= MAX_EDICTS)
263                         Host_Error("CL_ExpandEntities: num %i >= %i", num, MAX_EDICTS);
264                 oldmaxentities = cl.max_entities;
265                 oldentities = cl.entities;
266                 cl.max_entities = (num & ~255) + 256;
267                 cl.entities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_entities * sizeof(entity_t));
268                 memcpy(cl.entities, oldentities, oldmaxentities * sizeof(entity_t));
269                 Mem_Free(oldentities);
270                 for (i = oldmaxentities;i < cl.max_entities;i++)
271                 {
272                         cl.entities[i].state_baseline = defaultstate;
273                         cl.entities[i].state_previous = defaultstate;
274                         cl.entities[i].state_current = defaultstate;
275                 }
276         }
277 }
278
279 void CL_ExpandCSQCEntities(int num)
280 {
281         int i, oldmaxentities;
282         entity_t *oldentities;
283         if (num >= cl.max_csqcentities)
284         {
285                 if (!cl.csqcentities)
286                         Sys_Error("CL_ExpandCSQCEntities: cl.csqcentities not initialized\n");
287                 if (num >= MAX_EDICTS)
288                         Host_Error("CL_ExpandCSQCEntities: num %i >= %i\n", num, MAX_EDICTS);
289                 oldmaxentities = cl.max_csqcentities;
290                 oldentities = cl.csqcentities;
291                 cl.max_csqcentities = (num & ~255) + 256;
292                 cl.csqcentities = (entity_t *)Mem_Alloc(cls.levelmempool, cl.max_csqcentities * sizeof(entity_t));
293                 memcpy(cl.csqcentities, oldentities, oldmaxentities * sizeof(entity_t));
294                 Mem_Free(oldentities);
295                 for (i = oldmaxentities;i < cl.max_csqcentities;i++)
296                 {
297                         cl.csqcentities[i].state_baseline = defaultstate;
298                         cl.csqcentities[i].state_previous = defaultstate;
299                         cl.csqcentities[i].state_current = defaultstate;
300                         cl.csqcentities[i].csqc = true;
301                         cl.csqcentities[i].state_current.number = -i;
302                 }
303         }
304 }
305
306 void CL_VM_ShutDown (void);
307 /*
308 =====================
309 CL_Disconnect
310
311 Sends a disconnect message to the server
312 This is also called on Host_Error, so it shouldn't cause any errors
313 =====================
314 */
315 void CL_Disconnect(void)
316 {
317         if (cls.state == ca_dedicated)
318                 return;
319
320         Con_DPrintf("CL_Disconnect\n");
321
322         CL_VM_ShutDown();
323 // stop sounds (especially looping!)
324         S_StopAllSounds ();
325
326         cl.parsingtextexpectingpingforscores = 0; // just in case no reply has come yet
327
328         // clear contents blends
329         cl.cshifts[0].percent = 0;
330         cl.cshifts[1].percent = 0;
331         cl.cshifts[2].percent = 0;
332         cl.cshifts[3].percent = 0;
333
334         cl.worldmodel = NULL;
335
336         CL_Parse_ErrorCleanUp();
337
338         if (cls.demoplayback)
339                 CL_StopPlayback();
340         else if (cls.netcon)
341         {
342                 sizebuf_t buf;
343                 unsigned char bufdata[8];
344                 if (cls.demorecording)
345                         CL_Stop_f();
346
347                 // send disconnect message 3 times to improve chances of server
348                 // receiving it (but it still fails sometimes)
349                 memset(&buf, 0, sizeof(buf));
350                 buf.data = bufdata;
351                 buf.maxsize = sizeof(bufdata);
352                 if (cls.protocol == PROTOCOL_QUAKEWORLD)
353                 {
354                         Con_DPrint("Sending drop command\n");
355                         MSG_WriteByte(&buf, qw_clc_stringcmd);
356                         MSG_WriteString(&buf, "drop");
357                 }
358                 else
359                 {
360                         Con_DPrint("Sending clc_disconnect\n");
361                         MSG_WriteByte(&buf, clc_disconnect);
362                 }
363                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
364                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
365                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
366                 NetConn_Close(cls.netcon);
367                 cls.netcon = NULL;
368         }
369         cls.state = ca_disconnected;
370
371         cls.demoplayback = cls.timedemo = false;
372         cls.signon = 0;
373 }
374
375 void CL_Disconnect_f(void)
376 {
377         CL_Disconnect ();
378         if (sv.active)
379                 Host_ShutdownServer ();
380 }
381
382
383
384
385 /*
386 =====================
387 CL_EstablishConnection
388
389 Host should be either "local" or a net address
390 =====================
391 */
392 void CL_EstablishConnection(const char *host)
393 {
394         if (cls.state == ca_dedicated)
395                 return;
396
397         // clear menu's connect error message
398         M_Update_Return_Reason("");
399         cls.demonum = -1;
400
401         // stop demo loop in case this fails
402         CL_Disconnect();
403
404         // if downloads are running, cancel their finishing action
405         Curl_Clear_forthismap();
406
407         // make sure the client ports are open before attempting to connect
408         NetConn_UpdateSockets();
409
410         // run a network frame
411         //NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
412
413         if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
414         {
415                 cls.connect_trying = true;
416                 cls.connect_remainingtries = 3;
417                 cls.connect_nextsendtime = 0;
418                 M_Update_Return_Reason("Trying to connect...");
419                 // run several network frames to jump into the game quickly
420                 //if (sv.active)
421                 //{
422                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
423                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
424                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
425                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
426                 //}
427         }
428         else
429         {
430                 Con_Print("Unable to find a suitable network socket to connect to server.\n");
431                 M_Update_Return_Reason("No network");
432         }
433 }
434
435 /*
436 ==============
437 CL_PrintEntities_f
438 ==============
439 */
440 static void CL_PrintEntities_f(void)
441 {
442         entity_t *ent;
443         int i, j;
444         char name[32];
445
446         for (i = 0, ent = cl.entities;i < cl.num_entities;i++, ent++)
447         {
448                 const char* modelname;
449
450                 if (!ent->state_current.active)
451                         continue;
452
453                 if (ent->render.model)
454                         modelname = ent->render.model->name;
455                 else
456                         modelname = "--no model--";
457                 strlcpy(name, modelname, 25);
458                 for (j = (int)strlen(name);j < 25;j++)
459                         name[j] = ' ';
460                 Con_Printf("%3i: %s:%4i (%5i %5i %5i) [%3i %3i %3i] %4.2f %5.3f\n", i, name, ent->render.frame, (int) ent->state_current.origin[0], (int) ent->state_current.origin[1], (int) ent->state_current.origin[2], (int) ent->state_current.angles[0] % 360, (int) ent->state_current.angles[1] % 360, (int) ent->state_current.angles[2] % 360, ent->render.scale, ent->render.alpha);
461         }
462 }
463
464 //static const vec3_t nomodelmins = {-16, -16, -16};
465 //static const vec3_t nomodelmaxs = {16, 16, 16};
466 void CL_UpdateRenderEntity(entity_render_t *ent)
467 {
468         vec3_t org;
469         vec_t scale;
470         model_t *model = ent->model;
471         // update the inverse matrix for the renderer
472         Matrix4x4_Invert_Simple(&ent->inversematrix, &ent->matrix);
473         // update the animation blend state
474         R_LerpAnimation(ent);
475         // we need the matrix origin to center the box
476         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
477         // update entity->render.scale because the renderer needs it
478         ent->scale = scale = Matrix4x4_ScaleFromMatrix(&ent->matrix);
479         if (model)
480         {
481                 // NOTE: this directly extracts vector components from the matrix, which relies on the matrix orientation!
482 #ifdef MATRIX4x4_OPENGLORIENTATION
483                 if (ent->matrix.m[0][2] != 0 || ent->matrix.m[1][2] != 0)
484 #else
485                 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
486 #endif
487                 {
488                         // pitch or roll
489                         VectorMA(org, scale, model->rotatedmins, ent->mins);
490                         VectorMA(org, scale, model->rotatedmaxs, ent->maxs);
491                 }
492 #ifdef MATRIX4x4_OPENGLORIENTATION
493                 else if (ent->matrix.m[1][0] != 0 || ent->matrix.m[0][1] != 0)
494 #else
495                 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
496 #endif
497                 {
498                         // yaw
499                         VectorMA(org, scale, model->yawmins, ent->mins);
500                         VectorMA(org, scale, model->yawmaxs, ent->maxs);
501                 }
502                 else
503                 {
504                         VectorMA(org, scale, model->normalmins, ent->mins);
505                         VectorMA(org, scale, model->normalmaxs, ent->maxs);
506                 }
507         }
508         else
509         {
510                 ent->mins[0] = org[0] - 16;
511                 ent->mins[1] = org[1] - 16;
512                 ent->mins[2] = org[2] - 16;
513                 ent->maxs[0] = org[0] + 16;
514                 ent->maxs[1] = org[1] + 16;
515                 ent->maxs[2] = org[2] + 16;
516         }
517 }
518
519 /*
520 ===============
521 CL_LerpPoint
522
523 Determines the fraction between the last two messages that the objects
524 should be put at.
525 ===============
526 */
527 static float CL_LerpPoint(void)
528 {
529         float f;
530
531         // dropped packet, or start of demo
532         if (cl.mtime[1] < cl.mtime[0] - 0.1)
533                 cl.mtime[1] = cl.mtime[0] - 0.1;
534
535         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
536
537         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
538         f = cl.mtime[0] - cl.mtime[1];
539         if (!f || cl_nolerp.integer || cls.timedemo || (cl.islocalgame && !sv_fixedframeratesingleplayer.integer))
540         {
541                 cl.time = cl.mtime[0];
542                 return 1;
543         }
544
545         f = (cl.time - cl.mtime[1]) / f;
546         return bound(0, f, 1);
547 }
548
549 void CL_ClearTempEntities (void)
550 {
551         cl.num_temp_entities = 0;
552 }
553
554 entity_t *CL_NewTempEntity(void)
555 {
556         entity_t *ent;
557
558         if (r_refdef.numentities >= r_refdef.maxentities)
559                 return NULL;
560         if (cl.num_temp_entities >= cl.max_temp_entities)
561                 return NULL;
562         ent = &cl.temp_entities[cl.num_temp_entities++];
563         memset (ent, 0, sizeof(*ent));
564         r_refdef.entities[r_refdef.numentities++] = &ent->render;
565
566         ent->render.colormap = -1; // no special coloring
567         ent->render.alpha = 1;
568         VectorSet(ent->render.colormod, 1, 1, 1);
569         return ent;
570 }
571
572 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
573 {
574         int i;
575         cl_effect_t *e;
576         if (!modelindex) // sanity check
577                 return;
578         if (framerate < 1)
579         {
580                 Con_Printf("CL_Effect: framerate %f is < 1\n", framerate);
581                 return;
582         }
583         if (framecount < 1)
584         {
585                 Con_Printf("CL_Effect: framecount %i is < 1\n", framecount);
586                 return;
587         }
588         for (i = 0, e = cl.effects;i < cl.max_effects;i++, e++)
589         {
590                 if (e->active)
591                         continue;
592                 e->active = true;
593                 VectorCopy(org, e->origin);
594                 e->modelindex = modelindex;
595                 e->starttime = cl.time;
596                 e->startframe = startframe;
597                 e->endframe = startframe + framecount;
598                 e->framerate = framerate;
599
600                 e->frame = 0;
601                 e->frame1time = cl.time;
602                 e->frame2time = cl.time;
603                 cl.num_effects = max(cl.num_effects, i + 1);
604                 break;
605         }
606 }
607
608 void CL_AllocDlight(entity_render_t *ent, matrix4x4_t *matrix, float radius, float red, float green, float blue, float decay, float lifetime, int cubemapnum, int style, int shadowenable, vec_t corona, vec_t coronasizescale, vec_t ambientscale, vec_t diffusescale, vec_t specularscale, int flags)
609 {
610         int i;
611         dlight_t *dl;
612
613         /*
614 // first look for an exact key match
615         if (ent)
616         {
617                 dl = cl.dlights;
618                 for (i = 0;i < cl.num_dlights;i++, dl++)
619                         if (dl->ent == ent)
620                                 goto dlightsetup;
621         }
622         */
623
624 // then look for anything else
625         dl = cl.dlights;
626         for (i = 0;i < cl.num_dlights;i++, dl++)
627                 if (!dl->radius)
628                         goto dlightsetup;
629         // if we hit the end of the active dlights and found no gaps, add a new one
630         if (i < MAX_DLIGHTS)
631         {
632                 cl.num_dlights = i + 1;
633                 goto dlightsetup;
634         }
635
636         // unable to find one
637         return;
638
639 dlightsetup:
640         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
641         memset (dl, 0, sizeof(*dl));
642         Matrix4x4_Normalize(&dl->matrix, matrix);
643         dl->ent = ent;
644         Matrix4x4_OriginFromMatrix(&dl->matrix, dl->origin);
645         CL_FindNonSolidLocation(dl->origin, dl->origin, 6);
646         Matrix4x4_SetOrigin(&dl->matrix, dl->origin[0], dl->origin[1], dl->origin[2]);
647         dl->radius = radius;
648         dl->color[0] = red;
649         dl->color[1] = green;
650         dl->color[2] = blue;
651         dl->initialradius = radius;
652         dl->initialcolor[0] = red;
653         dl->initialcolor[1] = green;
654         dl->initialcolor[2] = blue;
655         dl->decay = decay / radius; // changed decay to be a percentage decrease
656         dl->intensity = 1; // this is what gets decayed
657         if (lifetime)
658                 dl->die = cl.time + lifetime;
659         else
660                 dl->die = 0;
661         dl->cubemapnum = cubemapnum;
662         dl->style = style;
663         dl->shadow = shadowenable;
664         dl->corona = corona;
665         dl->flags = flags;
666         dl->coronasizescale = coronasizescale;
667         dl->ambientscale = ambientscale;
668         dl->diffusescale = diffusescale;
669         dl->specularscale = specularscale;
670 }
671
672 // called before entity relinking
673 void CL_DecayLights(void)
674 {
675         int i, oldmax;
676         dlight_t *dl;
677         float time;
678
679         time = bound(0, cl.time - cl.oldtime, 0.1);
680         oldmax = cl.num_dlights;
681         cl.num_dlights = 0;
682         for (i = 0, dl = cl.dlights;i < oldmax;i++, dl++)
683         {
684                 if (dl->radius)
685                 {
686                         dl->intensity -= time * dl->decay;
687                         if (cl.time < dl->die && dl->intensity > 0)
688                         {
689                                 //dl->radius = dl->initialradius * dl->intensity;
690                                 VectorScale(dl->initialcolor, dl->intensity, dl->color);
691                                 cl.num_dlights = i + 1;
692                         }
693                         else
694                                 dl->radius = 0;
695                 }
696         }
697 }
698
699 // called after entity relinking
700 void CL_UpdateLights(void)
701 {
702         int i, j, k, l;
703         dlight_t *dl;
704         float frac, f;
705
706         r_refdef.numlights = 0;
707         if (r_dynamic.integer)
708         {
709                 for (i = 0, dl = cl.dlights;i < cl.num_dlights;i++, dl++)
710                 {
711                         if (dl->radius)
712                         {
713                                 R_RTLight_Update(dl, false);
714                                 r_refdef.lights[r_refdef.numlights++] = dl;
715                         }
716                 }
717         }
718
719 // light animations
720 // 'm' is normal light, 'a' is no light, 'z' is double bright
721         f = cl.time * 10;
722         i = (int)floor(f);
723         frac = f - i;
724         for (j = 0;j < cl.max_lightstyle;j++)
725         {
726                 if (!cl.lightstyle || !cl.lightstyle[j].length)
727                 {
728                         r_refdef.lightstylevalue[j] = 256;
729                         continue;
730                 }
731                 k = i % cl.lightstyle[j].length;
732                 l = (i-1) % cl.lightstyle[j].length;
733                 k = cl.lightstyle[j].map[k] - 'a';
734                 l = cl.lightstyle[j].map[l] - 'a';
735                 r_refdef.lightstylevalue[j] = (unsigned short)(((k*frac)+(l*(1-frac)))*22);
736         }
737 }
738
739 void CL_AddQWCTFFlagModel(entity_t *player, int skin)
740 {
741         float f;
742         entity_t *flag;
743         matrix4x4_t flagmatrix;
744
745         // this code taken from QuakeWorld
746         f = 14;
747         if (player->render.frame2 >= 29 && player->render.frame2 <= 40)
748         {
749                 if (player->render.frame2 >= 29 && player->render.frame2 <= 34)
750                 { //axpain
751                         if      (player->render.frame2 == 29) f = f + 2;
752                         else if (player->render.frame2 == 30) f = f + 8;
753                         else if (player->render.frame2 == 31) f = f + 12;
754                         else if (player->render.frame2 == 32) f = f + 11;
755                         else if (player->render.frame2 == 33) f = f + 10;
756                         else if (player->render.frame2 == 34) f = f + 4;
757                 }
758                 else if (player->render.frame2 >= 35 && player->render.frame2 <= 40)
759                 { // pain
760                         if      (player->render.frame2 == 35) f = f + 2;
761                         else if (player->render.frame2 == 36) f = f + 10;
762                         else if (player->render.frame2 == 37) f = f + 10;
763                         else if (player->render.frame2 == 38) f = f + 8;
764                         else if (player->render.frame2 == 39) f = f + 4;
765                         else if (player->render.frame2 == 40) f = f + 2;
766                 }
767         }
768         else if (player->render.frame2 >= 103 && player->render.frame2 <= 118)
769         {
770                 if      (player->render.frame2 >= 103 && player->render.frame2 <= 104) f = f + 6;  //nailattack
771                 else if (player->render.frame2 >= 105 && player->render.frame2 <= 106) f = f + 6;  //light
772                 else if (player->render.frame2 >= 107 && player->render.frame2 <= 112) f = f + 7;  //rocketattack
773                 else if (player->render.frame2 >= 112 && player->render.frame2 <= 118) f = f + 7;  //shotattack
774         }
775         // end of code taken from QuakeWorld
776
777         flag = CL_NewTempEntity();
778         if (!flag)
779                 return;
780
781         flag->render.model = cl.model_precache[cl.qw_modelindex_flag];
782         flag->render.skinnum = skin;
783         flag->render.colormap = -1; // no special coloring
784         flag->render.alpha = 1;
785         VectorSet(flag->render.colormod, 1, 1, 1);
786         // attach the flag to the player matrix
787         Matrix4x4_CreateFromQuakeEntity(&flagmatrix, -f, -22, 0, 0, 0, -45, 1);
788         Matrix4x4_Concat(&flag->render.matrix, &player->render.matrix, &flagmatrix);
789         CL_UpdateRenderEntity(&flag->render);
790 }
791
792 #define MAXVIEWMODELS 32
793 entity_t *viewmodels[MAXVIEWMODELS];
794 int numviewmodels;
795
796 matrix4x4_t viewmodelmatrix;
797
798 static int entitylinkframenumber;
799
800 static const vec3_t muzzleflashorigin = {18, 0, 0};
801
802 extern void V_DriftPitch(void);
803 extern void V_FadeViewFlashs(void);
804 extern void V_CalcViewBlend(void);
805
806 extern void V_CalcRefdef(void);
807 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
808 void CL_UpdateNetworkEntity(entity_t *e)
809 {
810         matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2;
811         //matrix4x4_t dlightmatrix;
812         int j, k, l;
813         effectnameindex_t trailtype;
814         float origin[3], angles[3], delta[3], lerp, dlightcolor[3], dlightradius, v2[3], d;
815         entity_t *t;
816         model_t *model;
817         trace_t trace;
818         //entity_persistent_t *p = &e->persistent;
819         //entity_render_t *r = &e->render;
820         if (e->persistent.linkframe != entitylinkframenumber)
821         {
822                 e->persistent.linkframe = entitylinkframenumber;
823                 // skip inactive entities and world
824                 if (!e->state_current.active || e == cl.entities || e == cl.csqcentities)
825                         return;
826                 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
827                 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
828                 e->render.flags = e->state_current.flags;
829                 e->render.effects = e->state_current.effects;
830                 VectorScale(e->state_current.colormod, (1.0f / 32.0f), e->render.colormod);
831                 if (e->state_current.flags & RENDER_COLORMAPPED)
832                 {
833                         int cb;
834                         unsigned char *cbcolor;
835                         e->render.colormap = e->state_current.colormap;
836                         cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
837                         cbcolor = (unsigned char *) (&palette_complete[cb]);
838                         e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
839                         e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
840                         e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
841                         cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
842                         cbcolor = (unsigned char *) (&palette_complete[cb]);
843                         e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
844                         e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
845                         e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
846                 }
847                 else if (e->state_current.colormap && cl.scores != NULL)
848                 {
849                         int cb;
850                         unsigned char *cbcolor;
851                         e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
852                         cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
853                         cbcolor = (unsigned char *) (&palette_complete[cb]);
854                         e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
855                         e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
856                         e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
857                         cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
858                         cbcolor = (unsigned char *) (&palette_complete[cb]);
859                         e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
860                         e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
861                         e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
862                 }
863                 else
864                 {
865                         e->render.colormap = -1; // no special coloring
866                         VectorClear(e->render.colormap_pantscolor);
867                         VectorClear(e->render.colormap_shirtcolor);
868                 }
869                 e->render.skinnum = e->state_current.skin;
870                 if (e->render.flags & RENDER_VIEWMODEL && !e->state_current.tagentity)
871                 {
872                         if (!r_drawviewmodel.integer || chase_active.integer || r_refdef.envmap)// || csqc_loaded)
873                                 return;
874                         if (!e->csqc)
875                         {
876                                 if (cl.viewentity)
877                                         CL_UpdateNetworkEntity(cl.entities + cl.viewentity);
878                                 if (e == &cl.viewent && cl.entities[cl.viewentity].state_current.active)
879                                 {
880                                         e->state_current.alpha = cl.entities[cl.viewentity].state_current.alpha;
881                                         e->state_current.effects = EF_NOSHADOW | (cl.entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT | EF_NODEPTHTEST));
882                                 }
883                         }
884                         matrix = &viewmodelmatrix;
885                 }
886                 else
887                 {
888                         // if the tag entity is currently impossible, skip it
889                         if (!e->csqc)
890                         {
891                                 if (e->state_current.tagentity >= cl.num_entities)
892                                         return;
893                                 t = cl.entities + e->state_current.tagentity;
894                         }
895                         else
896                         {
897                                 if (e->state_current.tagentity >= cl.num_csqcentities)
898                                         return;
899                                 t = cl.csqcentities + e->state_current.tagentity;
900                         }
901                         // if the tag entity is inactive, skip it
902                         if (!t->state_current.active)
903                                 return;
904                         // note: this can link to world
905                         CL_UpdateNetworkEntity(t);
906                         // make relative to the entity
907                         matrix = &t->render.matrix;
908                         // some properties of the tag entity carry over
909                         e->render.flags |= t->render.flags & (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL);
910                         // if a valid tagindex is used, make it relative to that tag instead
911                         // FIXME: use a model function to get tag info (need to handle skeletal)
912                         if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model))
913                         {
914                                 // blend the matrices
915                                 memset(&blendmatrix, 0, sizeof(blendmatrix));
916                                 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
917                                 {
918                                         matrix4x4_t tagmatrix;
919                                         Mod_Alias_GetTagMatrix(model, t->render.frameblend[j].frame, e->state_current.tagindex - 1, &tagmatrix);
920                                         d = t->render.frameblend[j].lerp;
921                                         for (l = 0;l < 4;l++)
922                                                 for (k = 0;k < 4;k++)
923                                                         blendmatrix.m[l][k] += d * tagmatrix.m[l][k];
924                                 }
925                                 // concat the tag matrices onto the entity matrix
926                                 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
927                                 // use the constructed tag matrix
928                                 matrix = &tempmatrix;
929                         }
930                 }
931
932                 // movement lerp
933                 // if it's the player entity, update according to client movement
934                 if (e == cl.entities + cl.playerentity && cl.movement_predicted)// && !e->csqc)
935                 {
936                         lerp = (cl.time - cl.movement_time[1]) / (cl.movement_time[0] - cl.movement_time[1]);
937                         lerp = bound(0, lerp, 1);
938                         VectorLerp(cl.movement_oldorigin, lerp, cl.movement_origin, origin);
939                         VectorSet(angles, 0, cl.viewangles[1], 0);
940                 }
941                 else if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
942                 {
943                         // interpolate the origin and angles
944                         VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
945                         VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
946                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
947                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
948                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
949                         VectorMA(e->persistent.oldangles, lerp, delta, angles);
950                 }
951                 else
952                 {
953                         // no interpolation
954                         VectorCopy(e->persistent.neworigin, origin);
955                         VectorCopy(e->persistent.newangles, angles);
956                 }
957
958                 // model setup and some modelflags
959                 if(e->state_current.modelindex < MAX_MODELS)
960                         e->render.model = cl.model_precache[e->state_current.modelindex];
961                 else
962                         e->render.model = cl.csqc_model_precache[65536-e->state_current.modelindex];
963                 if (e->render.model)
964                 {
965                         // if model is alias or this is a tenebrae-like dlight, reverse pitch direction
966                         if (e->render.model->type == mod_alias)
967                                 angles[0] = -angles[0];
968                         if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
969                         {
970                                 angles[1] = ANGLEMOD(100*cl.time);
971                                 if (cl_itembobheight.value)
972                                         origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
973                         }
974                         // transfer certain model flags to effects
975                         e->render.effects |= e->render.model->flags2 & (EF_FULLBRIGHT | EF_ADDITIVE);
976                         if ((e->render.effects & EF_SELECTABLE) && cl.cmd.cursor_entitynumber == e->state_current.number)
977                                 VectorScale(e->render.colormod, 2, e->render.colormod);
978                 }
979                 // if model is alias or this is a tenebrae-like dlight, reverse pitch direction
980                 else if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
981                         angles[0] = -angles[0];
982
983                 // animation lerp
984                 if (e->render.frame2 == e->state_current.frame)
985                 {
986                         // update frame lerp fraction
987                         e->render.framelerp = 1;
988                         if (e->render.frame2time > e->render.frame1time)
989                         {
990                                 // make sure frame lerp won't last longer than 100ms
991                                 // (this mainly helps with models that use framegroups and
992                                 // switch between them infrequently)
993                                 e->render.framelerp = (cl.time - e->render.frame2time) / min(e->render.frame2time - e->render.frame1time, 0.1);
994                                 e->render.framelerp = bound(0, e->render.framelerp, 1);
995                         }
996                 }
997                 else
998                 {
999                         // begin a new frame lerp
1000                         e->render.frame1 = e->render.frame2;
1001                         e->render.frame1time = e->render.frame2time;
1002                         e->render.frame = e->render.frame2 = e->state_current.frame;
1003                         e->render.frame2time = cl.time;
1004                         e->render.framelerp = 0;
1005                 }
1006
1007                 // set up the render matrix
1008                 // FIXME: e->render.scale should go away
1009                 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
1010                 // concat the matrices to make the entity relative to its tag
1011                 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
1012                 // make the other useful stuff
1013                 CL_UpdateRenderEntity(&e->render);
1014
1015                 // handle effects now that we know where this entity is in the world...
1016                 if (e->render.model && e->render.model->soundfromcenter)
1017                 {
1018                         // bmodels are treated specially since their origin is usually '0 0 0'
1019                         vec3_t o;
1020                         VectorMAM(0.5f, e->render.model->normalmins, 0.5f, e->render.model->normalmaxs, o);
1021                         Matrix4x4_Transform(&e->render.matrix, o, origin);
1022                 }
1023                 else
1024                         Matrix4x4_OriginFromMatrix(&e->render.matrix, origin);
1025                 trailtype = EFFECT_NONE;
1026                 dlightradius = 0;
1027                 dlightcolor[0] = 0;
1028                 dlightcolor[1] = 0;
1029                 dlightcolor[2] = 0;
1030                 // LordHavoc: if the entity has no effects, don't check each
1031                 if (e->render.effects)
1032                 {
1033                         if (e->render.effects & EF_BRIGHTFIELD)
1034                         {
1035                                 if (gamemode == GAME_NEXUIZ)
1036                                         trailtype = EFFECT_TR_NEXUIZPLASMA;
1037                                 else
1038                                         CL_EntityParticles(e);
1039                         }
1040                         if (e->render.effects & EF_DIMLIGHT)
1041                         {
1042                                 dlightradius = max(dlightradius, 200);
1043                                 dlightcolor[0] += 1.50f;
1044                                 dlightcolor[1] += 1.50f;
1045                                 dlightcolor[2] += 1.50f;
1046                         }
1047                         if (e->render.effects & EF_BRIGHTLIGHT)
1048                         {
1049                                 dlightradius = max(dlightradius, 400);
1050                                 dlightcolor[0] += 3.00f;
1051                                 dlightcolor[1] += 3.00f;
1052                                 dlightcolor[2] += 3.00f;
1053                         }
1054                         // LordHavoc: more effects
1055                         if (e->render.effects & EF_RED) // red
1056                         {
1057                                 dlightradius = max(dlightradius, 200);
1058                                 dlightcolor[0] += 1.50f;
1059                                 dlightcolor[1] += 0.15f;
1060                                 dlightcolor[2] += 0.15f;
1061                         }
1062                         if (e->render.effects & EF_BLUE) // blue
1063                         {
1064                                 dlightradius = max(dlightradius, 200);
1065                                 dlightcolor[0] += 0.15f;
1066                                 dlightcolor[1] += 0.15f;
1067                                 dlightcolor[2] += 1.50f;
1068                         }
1069                         if (e->render.effects & EF_FLAME)
1070                                 CL_ParticleEffect(EFFECT_EF_FLAME, bound(0, cl.time - cl.oldtime, 0.1), origin, origin, vec3_origin, vec3_origin, NULL, 0);
1071                         if (e->render.effects & EF_STARDUST)
1072                                 CL_ParticleEffect(EFFECT_EF_STARDUST, bound(0, cl.time - cl.oldtime, 0.1), origin, origin, vec3_origin, vec3_origin, NULL, 0);
1073                         if (e->render.effects & (EF_FLAG1QW | EF_FLAG2QW))
1074                         {
1075                                 // these are only set on player entities
1076                                 CL_AddQWCTFFlagModel(e, (e->render.effects & EF_FLAG2QW) != 0);
1077                         }
1078                 }
1079                 // muzzleflash fades over time, and is offset a bit
1080                 if (e->persistent.muzzleflash > 0)
1081                 {
1082                         Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
1083                         trace = CL_TraceBox(origin, vec3_origin, vec3_origin, v2, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY, false);
1084                         tempmatrix = e->render.matrix;
1085                         Matrix4x4_SetOrigin(&tempmatrix, trace.endpos[0], trace.endpos[1], trace.endpos[2]);
1086                         CL_AllocDlight(NULL, &tempmatrix, 150, e->persistent.muzzleflash * 4.0f, e->persistent.muzzleflash * 4.0f, e->persistent.muzzleflash * 4.0f, 0, 0, 0, -1, true, 0, 0.25, 0, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1087                         e->persistent.muzzleflash -= bound(0, cl.time - cl.oldtime, 0.1) * 20;
1088                 }
1089                 // LordHavoc: if the model has no flags, don't check each
1090                 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
1091                 {
1092                         if (e->render.model->flags & EF_GIB)
1093                                 trailtype = EFFECT_TR_BLOOD;
1094                         else if (e->render.model->flags & EF_ZOMGIB)
1095                                 trailtype = EFFECT_TR_SLIGHTBLOOD;
1096                         else if (e->render.model->flags & EF_TRACER)
1097                                 trailtype = EFFECT_TR_WIZSPIKE;
1098                         else if (e->render.model->flags & EF_TRACER2)
1099                                 trailtype = EFFECT_TR_KNIGHTSPIKE;
1100                         else if (e->render.model->flags & EF_ROCKET)
1101                                 trailtype = EFFECT_TR_ROCKET;
1102                         else if (e->render.model->flags & EF_GRENADE)
1103                         {
1104                                 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
1105                                 trailtype = e->render.alpha == -1 ? EFFECT_TR_NEHAHRASMOKE : EFFECT_TR_GRENADE;
1106                         }
1107                         else if (e->render.model->flags & EF_TRACER3)
1108                                 trailtype = EFFECT_TR_VORESPIKE;
1109                 }
1110                 // LordHavoc: customizable glow
1111                 if (e->state_current.glowsize)
1112                 {
1113                         // * 4 for the expansion from 0-255 to 0-1023 range,
1114                         // / 255 to scale down byte colors
1115                         dlightradius = max(dlightradius, e->state_current.glowsize * 4);
1116                         VectorMA(dlightcolor, (1.0f / 255.0f), (unsigned char *)&palette_complete[e->state_current.glowcolor], dlightcolor);
1117                 }
1118                 // make the glow dlight
1119                 if (dlightradius > 0 && (dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL))
1120                 {
1121                         //dlightmatrix = e->render.matrix;
1122                         // hack to make glowing player light shine on their gun
1123                         //if (e->state_current.number == cl.viewentity/* && !chase_active.integer*/)
1124                         //      Matrix4x4_AdjustOrigin(&dlightmatrix, 0, 0, 30);
1125                         CL_AllocDlight(&e->render, &e->render.matrix, dlightradius, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0, 0, -1, true, 1, 0.25, 0.25, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1126                 }
1127                 // custom rtlight
1128                 if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
1129                 {
1130                         float light[4];
1131                         VectorScale(e->state_current.light, (1.0f / 256.0f), light);
1132                         light[3] = e->state_current.light[3];
1133                         if (light[0] == 0 && light[1] == 0 && light[2] == 0)
1134                                 VectorSet(light, 1, 1, 1);
1135                         if (light[3] == 0)
1136                                 light[3] = 350;
1137                         // FIXME: add ambient/diffuse/specular scales as an extension ontop of TENEBRAE_GFX_DLIGHTS?
1138                         CL_AllocDlight(&e->render, &e->render.matrix, light[3], light[0], light[1], light[2], 0, 0, e->state_current.skin, e->state_current.lightstyle, !(e->state_current.lightpflags & PFLAGS_NOSHADOW), (e->state_current.lightpflags & PFLAGS_CORONA) != 0, 0.25, 0, 1, 1, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1139                 }
1140                 // do trails
1141                 if (e->render.flags & RENDER_GLOWTRAIL)
1142                         trailtype = EFFECT_TR_GLOWTRAIL;
1143                 if (trailtype)
1144                 {
1145                         float len;
1146                         vec3_t vel;
1147                         VectorSubtract(e->state_current.origin, e->state_previous.origin, vel);
1148                         len = e->state_current.time - e->state_previous.time;
1149                         if (len > 0)
1150                                 len = 1.0f / len;
1151                         VectorScale(vel, len, vel);
1152                         CL_ParticleEffect(trailtype, 1, e->persistent.trail_origin, origin, vel, vel, e, e->state_current.glowcolor);
1153                 }
1154                 VectorCopy(origin, e->persistent.trail_origin);
1155                 // tenebrae's sprites are all additive mode (weird)
1156                 if (gamemode == GAME_TENEBRAE && e->render.model && e->render.model->type == mod_sprite)
1157                         e->render.effects |= EF_ADDITIVE;
1158                 // player model is only shown with chase_active on
1159                 if (!e->csqc)
1160                 if (e->state_current.number == cl.viewentity)
1161                         e->render.flags |= RENDER_EXTERIORMODEL;
1162                 // transparent stuff can't be lit during the opaque stage
1163                 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1164                         e->render.flags |= RENDER_TRANSPARENT;
1165                 // double sided rendering mode causes backfaces to be visible
1166                 // (mostly useful on transparent stuff)
1167                 if (e->render.effects & EF_DOUBLESIDED)
1168                         e->render.flags |= RENDER_NOCULLFACE;
1169                 // either fullbright or lit
1170                 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1171                         e->render.flags |= RENDER_LIGHT;
1172                 // hide player shadow during intermission or nehahra movie
1173                 if (!(e->render.effects & EF_NOSHADOW)
1174                  && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
1175                  && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
1176                         e->render.flags |= RENDER_SHADOW;
1177                 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
1178                         cl.brushmodel_entities[cl.num_brushmodel_entities++] = e->state_current.number;
1179                 // because the player may be attached to another entity, V_CalcRefdef must be deferred until here...
1180                 if (e->state_current.number == cl.viewentity)
1181                         V_CalcRefdef();
1182         }
1183 }
1184
1185
1186 /*
1187 ===============
1188 CL_UpdateEntities
1189 ===============
1190 */
1191 static void CL_UpdateEntities(void)
1192 {
1193         entity_t *ent;
1194         int i;
1195
1196         ent = &cl.viewent;
1197         ent->state_previous = ent->state_current;
1198         ent->state_current = defaultstate;
1199         ent->state_current.time = cl.time;
1200         ent->state_current.number = -1;
1201         ent->state_current.active = true;
1202         ent->state_current.modelindex = cl.stats[STAT_WEAPON];
1203         ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
1204         ent->state_current.flags = RENDER_VIEWMODEL;
1205         if ((cl.stats[STAT_HEALTH] <= 0 && cl_deathnoviewmodel.integer) || cl.intermission)
1206                 ent->state_current.modelindex = 0;
1207         else if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1208         {
1209                 if (gamemode == GAME_TRANSFUSION)
1210                         ent->state_current.alpha = 128;
1211                 else
1212                         ent->state_current.modelindex = 0;
1213         }
1214
1215         // reset animation interpolation on weaponmodel if model changed
1216         if (ent->state_previous.modelindex != ent->state_current.modelindex)
1217         {
1218                 ent->render.frame = ent->render.frame1 = ent->render.frame2 = ent->state_current.frame;
1219                 ent->render.frame1time = ent->render.frame2time = cl.time;
1220                 ent->render.framelerp = 1;
1221         }
1222
1223         // start on the entity after the world
1224         entitylinkframenumber++;
1225         for (i = 1;i < cl.num_entities;i++)
1226         {
1227                 if (cl.entities_active[i])
1228                 {
1229                         ent = cl.entities + i;
1230                         if (ent->state_current.active)
1231                                 CL_UpdateNetworkEntity(ent);
1232                         else
1233                                 cl.entities_active[i] = false;
1234                 }
1235         }
1236         CL_UpdateNetworkEntity(&cl.viewent);
1237 }
1238
1239 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
1240 void CL_LinkNetworkEntity(entity_t *e)
1241 {
1242         entity_t *t;
1243         if (e->persistent.linkframe != entitylinkframenumber)
1244         {
1245                 e->persistent.linkframe = entitylinkframenumber;
1246                 // skip inactive entities and world
1247                 if (!e->state_current.active || e == cl.entities || e == cl.csqcentities)
1248                         return;
1249                 if (e->render.flags & RENDER_VIEWMODEL && !e->state_current.tagentity)
1250                 {
1251                         if (!r_drawviewmodel.integer || chase_active.integer || r_refdef.envmap)
1252                                 return;
1253                         if (!e->csqc)
1254                         if (cl.viewentity)
1255                                 CL_LinkNetworkEntity(cl.entities + cl.viewentity);
1256                 }
1257                 else
1258                 {
1259                         // if the tag entity is currently impossible, skip it
1260                         if (!e->csqc)
1261                         {
1262                                 if (e->state_current.tagentity >= cl.num_entities)
1263                                         return;
1264                                 t = cl.entities + e->state_current.tagentity;
1265                         }
1266                         else
1267                         {
1268                                 if (e->state_current.tagentity >= cl.num_csqcentities)
1269                                         return;
1270                                 t = cl.csqcentities + e->state_current.tagentity;
1271                         }
1272                         // if the tag entity is inactive, skip it
1273                         if (!t->state_current.active)
1274                                 return;
1275                         // note: this can link to world
1276                         CL_LinkNetworkEntity(t);
1277                 }
1278
1279                 // don't show entities with no modelindex (note: this still shows
1280                 // entities which have a modelindex that resolved to a NULL model)
1281                 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
1282                         r_refdef.entities[r_refdef.numentities++] = &e->render;
1283                 //if (cl.viewentity && e->state_current.number == cl.viewentity)
1284                 //      Matrix4x4_Print(&e->render.matrix);
1285         }
1286 }
1287
1288 void CL_RelinkWorld(void)
1289 {
1290         entity_t *ent = &cl.entities[0];
1291         cl.brushmodel_entities[cl.num_brushmodel_entities++] = 0;
1292         // FIXME: this should be done at load
1293         ent->render.matrix = identitymatrix;
1294         CL_UpdateRenderEntity(&ent->render);
1295         ent->render.flags = RENDER_SHADOW;
1296         if (!r_fullbright.integer)
1297                 ent->render.flags |= RENDER_LIGHT;
1298         VectorSet(ent->render.colormod, 1, 1, 1);
1299         r_refdef.worldentity = &ent->render;
1300         r_refdef.worldmodel = cl.worldmodel;
1301 }
1302
1303 static void CL_RelinkStaticEntities(void)
1304 {
1305         int i;
1306         entity_t *e;
1307         for (i = 0, e = cl.static_entities;i < cl.num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++, e++)
1308         {
1309                 e->render.flags = 0;
1310                 // if the model was not loaded when the static entity was created we
1311                 // need to re-fetch the model pointer
1312                 e->render.model = cl.model_precache[e->state_baseline.modelindex];
1313                 // transparent stuff can't be lit during the opaque stage
1314                 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1315                         e->render.flags |= RENDER_TRANSPARENT;
1316                 // either fullbright or lit
1317                 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1318                         e->render.flags |= RENDER_LIGHT;
1319                 // hide player shadow during intermission or nehahra movie
1320                 if (!(e->render.effects & EF_NOSHADOW) && !(e->render.flags & RENDER_TRANSPARENT))
1321                         e->render.flags |= RENDER_SHADOW;
1322                 VectorSet(e->render.colormod, 1, 1, 1);
1323                 R_LerpAnimation(&e->render);
1324                 r_refdef.entities[r_refdef.numentities++] = &e->render;
1325         }
1326 }
1327
1328 /*
1329 ===============
1330 CL_RelinkEntities
1331 ===============
1332 */
1333 static void CL_RelinkNetworkEntities(void)
1334 {
1335         entity_t *ent;
1336         int i;
1337
1338         // start on the entity after the world
1339         for (i = 1;i < cl.num_entities;i++)
1340         {
1341                 if (cl.entities_active[i])
1342                 {
1343                         ent = cl.entities + i;
1344                         if (ent->state_current.active)
1345                                 CL_LinkNetworkEntity(ent);
1346                         else
1347                                 cl.entities_active[i] = false;
1348                 }
1349         }
1350 }
1351
1352 static void CL_RelinkEffects(void)
1353 {
1354         int i, intframe;
1355         cl_effect_t *e;
1356         entity_t *ent;
1357         float frame;
1358
1359         for (i = 0, e = cl.effects;i < cl.num_effects;i++, e++)
1360         {
1361                 if (e->active)
1362                 {
1363                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
1364                         intframe = (int)frame;
1365                         if (intframe < 0 || intframe >= e->endframe)
1366                         {
1367                                 memset(e, 0, sizeof(*e));
1368                                 while (cl.num_effects > 0 && !cl.effects[cl.num_effects - 1].active)
1369                                         cl.num_effects--;
1370                                 continue;
1371                         }
1372
1373                         if (intframe != e->frame)
1374                         {
1375                                 e->frame = intframe;
1376                                 e->frame1time = e->frame2time;
1377                                 e->frame2time = cl.time;
1378                         }
1379
1380                         // if we're drawing effects, get a new temp entity
1381                         // (NewTempEntity adds it to the render entities list for us)
1382                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
1383                         {
1384                                 // interpolation stuff
1385                                 ent->render.frame1 = intframe;
1386                                 ent->render.frame2 = intframe + 1;
1387                                 if (ent->render.frame2 >= e->endframe)
1388                                         ent->render.frame2 = -1; // disappear
1389                                 ent->render.framelerp = frame - intframe;
1390                                 ent->render.frame1time = e->frame1time;
1391                                 ent->render.frame2time = e->frame2time;
1392
1393                                 // normal stuff
1394                                 if(e->modelindex < MAX_MODELS)
1395                                         ent->render.model = cl.model_precache[e->modelindex];
1396                                 else
1397                                         ent->render.model = cl.csqc_model_precache[65536-e->modelindex];
1398                                 ent->render.frame = ent->render.frame2;
1399                                 ent->render.colormap = -1; // no special coloring
1400                                 ent->render.alpha = 1;
1401                                 VectorSet(ent->render.colormod, 1, 1, 1);
1402
1403                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
1404                                 CL_UpdateRenderEntity(&ent->render);
1405                         }
1406                 }
1407         }
1408 }
1409
1410 void CL_Beam_CalculatePositions(const beam_t *b, vec3_t start, vec3_t end)
1411 {
1412         VectorCopy(b->start, start);
1413         VectorCopy(b->end, end);
1414
1415         // if coming from the player, update the start position
1416         if (b->entity == cl.viewentity)
1417         {
1418                 if (cl_beams_quakepositionhack.integer && !chase_active.integer)
1419                 {
1420                         // LordHavoc: this is a stupid hack from Quake that makes your
1421                         // lightning appear to come from your waist and cover less of your
1422                         // view
1423                         // in Quake this hack was applied to all players (causing the
1424                         // infamous crotch-lightning), but in darkplaces and QuakeWorld it
1425                         // only applies to your own lightning, and only in first person
1426                         Matrix4x4_OriginFromMatrix(&cl.entities[cl.viewentity].render.matrix, start);
1427                 }
1428                 if (cl_beams_instantaimhack.integer)
1429                 {
1430                         vec3_t dir, localend;
1431                         vec_t len;
1432                         // LordHavoc: this updates the beam direction to match your
1433                         // viewangles
1434                         VectorSubtract(end, start, dir);
1435                         len = VectorLength(dir);
1436                         VectorNormalize(dir);
1437                         VectorSet(localend, len, 0, 0);
1438                         Matrix4x4_Transform(&r_view.matrix, localend, end);
1439                 }
1440         }
1441 }
1442
1443 void CL_RelinkBeams(void)
1444 {
1445         int i;
1446         beam_t *b;
1447         vec3_t dist, org, start, end;
1448         float d;
1449         entity_t *ent;
1450         float yaw, pitch;
1451         float forward;
1452         matrix4x4_t tempmatrix;
1453
1454         for (i = 0, b = cl.beams;i < cl.num_beams;i++, b++)
1455         {
1456                 if (!b->model)
1457                         continue;
1458                 if (b->endtime < cl.time)
1459                 {
1460                         b->model = NULL;
1461                         continue;
1462                 }
1463
1464                 CL_Beam_CalculatePositions(b, start, end);
1465
1466                 if (b->lightning)
1467                 {
1468                         if (cl_beams_lightatend.integer)
1469                         {
1470                                 // FIXME: create a matrix from the beam start/end orientation
1471                                 Matrix4x4_CreateTranslate(&tempmatrix, end[0], end[1], end[2]);
1472                                 CL_AllocDlight (NULL, &tempmatrix, 200, 0.3, 0.7, 1, 0, 0, 0, -1, true, 1, 0.25, 1, 0, 0, LIGHTFLAG_NORMALMODE | LIGHTFLAG_REALTIMEMODE);
1473                         }
1474                         if (cl_beams_polygons.integer)
1475                                 continue;
1476                 }
1477
1478                 // calculate pitch and yaw
1479                 // (this is similar to the QuakeC builtin function vectoangles)
1480                 VectorSubtract(end, start, dist);
1481                 if (dist[1] == 0 && dist[0] == 0)
1482                 {
1483                         yaw = 0;
1484                         if (dist[2] > 0)
1485                                 pitch = 90;
1486                         else
1487                                 pitch = 270;
1488                 }
1489                 else
1490                 {
1491                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1492                         if (yaw < 0)
1493                                 yaw += 360;
1494
1495                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1496                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1497                         if (pitch < 0)
1498                                 pitch += 360;
1499                 }
1500
1501                 // add new entities for the lightning
1502                 VectorCopy (start, org);
1503                 d = VectorNormalizeLength(dist);
1504                 while (d > 0)
1505                 {
1506                         ent = CL_NewTempEntity ();
1507                         if (!ent)
1508                                 return;
1509                         //VectorCopy (org, ent->render.origin);
1510                         ent->render.model = b->model;
1511                         //ent->render.effects = EF_FULLBRIGHT;
1512                         //ent->render.angles[0] = pitch;
1513                         //ent->render.angles[1] = yaw;
1514                         //ent->render.angles[2] = rand()%360;
1515                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1516                         CL_UpdateRenderEntity(&ent->render);
1517                         VectorMA(org, 30, dist, org);
1518                         d -= 30;
1519                 }
1520         }
1521
1522         while (cl.num_beams > 0 && !cl.beams[cl.num_beams - 1].model)
1523                 cl.num_beams--;
1524 }
1525
1526 static void CL_RelinkQWNails(void)
1527 {
1528         int i;
1529         vec_t *v;
1530         entity_t *ent;
1531
1532         for (i = 0;i < cl.qw_num_nails;i++)
1533         {
1534                 v = cl.qw_nails[i];
1535
1536                 // if we're drawing effects, get a new temp entity
1537                 // (NewTempEntity adds it to the render entities list for us)
1538                 if (!(ent = CL_NewTempEntity()))
1539                         continue;
1540
1541                 // normal stuff
1542                 ent->render.model = cl.model_precache[cl.qw_modelindex_spike];
1543                 ent->render.colormap = -1; // no special coloring
1544                 ent->render.alpha = 1;
1545                 VectorSet(ent->render.colormod, 1, 1, 1);
1546
1547                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, v[0], v[1], v[2], v[3], v[4], v[5], 1);
1548                 CL_UpdateRenderEntity(&ent->render);
1549         }
1550 }
1551
1552 void CL_LerpPlayer(float frac)
1553 {
1554         int i;
1555
1556         cl.viewzoom = cl.mviewzoom[1] + frac * (cl.mviewzoom[0] - cl.mviewzoom[1]);
1557         for (i = 0;i < 3;i++)
1558         {
1559                 cl.punchangle[i] = cl.mpunchangle[1][i] + frac * (cl.mpunchangle[0][i] - cl.mpunchangle[1][i]);
1560                 cl.punchvector[i] = cl.mpunchvector[1][i] + frac * (cl.mpunchvector[0][i] - cl.mpunchvector[1][i]);
1561                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1562         }
1563 }
1564
1565 void CSQC_RelinkAllEntities (int drawmask)
1566 {
1567         // process network entities (note: this sets up the view!)
1568         cl.num_brushmodel_entities = 0;
1569         CL_UpdateEntities();
1570
1571         entitylinkframenumber++;
1572         // link stuff
1573         CL_RelinkWorld();
1574         CL_RelinkStaticEntities();
1575         CL_RelinkBeams();
1576         CL_RelinkEffects();
1577
1578         // link stuff
1579         if (drawmask & ENTMASK_ENGINE)
1580         {
1581                 CL_RelinkNetworkEntities();
1582                 CL_RelinkQWNails();
1583         }
1584
1585         if (drawmask & ENTMASK_ENGINEVIEWMODELS)
1586                 CL_LinkNetworkEntity(&cl.viewent); // link gun model
1587
1588         // update view blend
1589         V_CalcViewBlend();
1590 }
1591
1592 /*
1593 ===============
1594 CL_ReadFromServer
1595
1596 Read all incoming data from the server
1597 ===============
1598 */
1599 extern void CL_ClientMovement_Replay(void);
1600 extern void CL_StairSmoothing(void);//view.c
1601
1602 int CL_ReadFromServer(void)
1603 {
1604         CL_ReadDemoMessage();
1605         CL_SendMove();
1606
1607         r_refdef.time = cl.time;
1608         r_refdef.extraupdate = !r_speeds.integer;
1609         r_refdef.numentities = 0;
1610         r_view.matrix = identitymatrix;
1611
1612         if (cls.state == ca_connected && cls.signon == SIGNONS)
1613         {
1614                 // prepare for a new frame
1615                 CL_LerpPlayer(CL_LerpPoint());
1616                 CL_DecayLights();
1617                 CL_ClearTempEntities();
1618                 V_DriftPitch();
1619                 V_FadeViewFlashs();
1620
1621                 // move particles
1622                 CL_MoveParticles();
1623                 R_MoveExplosions();
1624
1625                 // predict current player location
1626                 CL_ClientMovement_Replay();
1627
1628                 // now that the player entity has been updated we can call V_CalcRefdef
1629                 V_CalcRefdef();
1630
1631                 if(!csqc_loaded)        //[515]: csqc
1632                 {
1633                         // process network entities (note: this sets up the view!)
1634                         cl.num_brushmodel_entities = 0;
1635                         CL_UpdateEntities();
1636
1637                         entitylinkframenumber++;
1638                         // link stuff
1639                         CL_RelinkWorld();
1640                         CL_RelinkStaticEntities();
1641                         CL_RelinkBeams();
1642                         CL_RelinkEffects();
1643
1644                         CL_RelinkNetworkEntities();
1645                         CL_LinkNetworkEntity(&cl.viewent); // link gun model
1646                         CL_RelinkQWNails();
1647
1648                         // update view blend
1649                         V_CalcViewBlend();
1650                 }
1651                 else
1652                         csqc_frame = true;
1653
1654                 CL_UpdateLights();
1655                 CL_StairSmoothing();
1656
1657                 // update the r_refdef time again because cl.time may have changed
1658                 r_refdef.time = cl.time;
1659         }
1660
1661         return 0;
1662 }
1663
1664 // LordHavoc: pausedemo command
1665 static void CL_PauseDemo_f (void)
1666 {
1667         cls.demopaused = !cls.demopaused;
1668         if (cls.demopaused)
1669                 Con_Print("Demo paused\n");
1670         else
1671                 Con_Print("Demo unpaused\n");
1672 }
1673
1674 /*
1675 ======================
1676 CL_Fog_f
1677 ======================
1678 */
1679 static void CL_Fog_f (void)
1680 {
1681         if (Cmd_Argc () == 1)
1682         {
1683                 Con_Printf("\"fog\" is \"%f %f %f %f\"\n", r_refdef.fog_density, r_refdef.fog_red, r_refdef.fog_green, r_refdef.fog_blue);
1684                 return;
1685         }
1686         r_refdef.fog_density = atof(Cmd_Argv(1));
1687         r_refdef.fog_red = atof(Cmd_Argv(2));
1688         r_refdef.fog_green = atof(Cmd_Argv(3));
1689         r_refdef.fog_blue = atof(Cmd_Argv(4));
1690 }
1691
1692 /*
1693 ====================
1694 CL_TimeRefresh_f
1695
1696 For program optimization
1697 ====================
1698 */
1699 static void CL_TimeRefresh_f (void)
1700 {
1701         int i;
1702         float timestart, timedelta, oldangles[3];
1703
1704         r_refdef.extraupdate = false;
1705         VectorCopy(cl.viewangles, oldangles);
1706         VectorClear(cl.viewangles);
1707
1708         timestart = Sys_DoubleTime();
1709         for (i = 0;i < 128;i++)
1710         {
1711                 Matrix4x4_CreateFromQuakeEntity(&r_view.matrix, r_view.origin[0], r_view.origin[1], r_view.origin[2], 0, i / 128.0 * 360.0, 0, 1);
1712                 CL_UpdateScreen();
1713         }
1714         timedelta = Sys_DoubleTime() - timestart;
1715
1716         VectorCopy(oldangles, cl.viewangles);
1717         Con_Printf("%f seconds (%f fps)\n", timedelta, 128/timedelta);
1718 }
1719
1720 /*
1721 ===========
1722 CL_Shutdown
1723 ===========
1724 */
1725 void CL_Shutdown (void)
1726 {
1727         CL_Particles_Shutdown();
1728         CL_Parse_Shutdown();
1729
1730         Mem_FreePool (&cls.permanentmempool);
1731         Mem_FreePool (&cls.levelmempool);
1732 }
1733
1734 /*
1735 =================
1736 CL_Init
1737 =================
1738 */
1739 void CL_Init (void)
1740 {
1741         cls.levelmempool = Mem_AllocPool("client (per-level memory)", 0, NULL);
1742         cls.permanentmempool = Mem_AllocPool("client (long term memory)", 0, NULL);
1743
1744         memset(&r_refdef, 0, sizeof(r_refdef));
1745         // max entities sent to renderer per frame
1746         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1747         r_refdef.entities = (entity_render_t **)Mem_Alloc(cls.permanentmempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1748
1749         CL_InitInput ();
1750
1751 //
1752 // register our commands
1753 //
1754         Cvar_RegisterVariable (&cl_upspeed);
1755         Cvar_RegisterVariable (&cl_forwardspeed);
1756         Cvar_RegisterVariable (&cl_backspeed);
1757         Cvar_RegisterVariable (&cl_sidespeed);
1758         Cvar_RegisterVariable (&cl_movespeedkey);
1759         Cvar_RegisterVariable (&cl_yawspeed);
1760         Cvar_RegisterVariable (&cl_pitchspeed);
1761         Cvar_RegisterVariable (&cl_anglespeedkey);
1762         Cvar_RegisterVariable (&cl_shownet);
1763         Cvar_RegisterVariable (&cl_nolerp);
1764         Cvar_RegisterVariable (&lookspring);
1765         Cvar_RegisterVariable (&lookstrafe);
1766         Cvar_RegisterVariable (&sensitivity);
1767         Cvar_RegisterVariable (&freelook);
1768
1769         Cvar_RegisterVariable (&m_pitch);
1770         Cvar_RegisterVariable (&m_yaw);
1771         Cvar_RegisterVariable (&m_forward);
1772         Cvar_RegisterVariable (&m_side);
1773
1774         Cvar_RegisterVariable (&cl_itembobspeed);
1775         Cvar_RegisterVariable (&cl_itembobheight);
1776
1777         Cmd_AddCommand ("entities", CL_PrintEntities_f, "print information on network entities known to client");
1778         Cmd_AddCommand ("disconnect", CL_Disconnect_f, "disconnect from server (or disconnect all clients if running a server)");
1779         Cmd_AddCommand ("record", CL_Record_f, "record a demo");
1780         Cmd_AddCommand ("stop", CL_Stop_f, "stop recording or playing a demo");
1781         Cmd_AddCommand ("playdemo", CL_PlayDemo_f, "watch a demo file");
1782         Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "play back a demo as fast as possible and save statistics to benchmark.log");
1783
1784 #ifdef AUTODEMO_BROKEN
1785         Cvar_RegisterVariable (&cl_autodemo);
1786         Cvar_RegisterVariable (&cl_autodemo_nameformat);
1787 #endif
1788
1789         Cmd_AddCommand ("fog", CL_Fog_f, "set global fog parameters (density red green blue)");
1790
1791         // LordHavoc: added pausedemo
1792         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f, "pause demo playback (can also safely pause demo recording if using QUAKE, QUAKEDP or NEHAHRAMOVIE protocol, useful for making movies)");
1793
1794         Cvar_RegisterVariable(&r_draweffects);
1795         Cvar_RegisterVariable(&cl_explosions_alpha_start);
1796         Cvar_RegisterVariable(&cl_explosions_alpha_end);
1797         Cvar_RegisterVariable(&cl_explosions_size_start);
1798         Cvar_RegisterVariable(&cl_explosions_size_end);
1799         Cvar_RegisterVariable(&cl_explosions_lifetime);
1800         Cvar_RegisterVariable(&cl_stainmaps);
1801         Cvar_RegisterVariable(&cl_stainmaps_clearonload);
1802         Cvar_RegisterVariable(&cl_beams_polygons);
1803         Cvar_RegisterVariable(&cl_beams_quakepositionhack);
1804         Cvar_RegisterVariable(&cl_beams_instantaimhack);
1805         Cvar_RegisterVariable(&cl_beams_lightatend);
1806         Cvar_RegisterVariable(&cl_noplayershadow);
1807
1808         Cvar_RegisterVariable(&cl_prydoncursor);
1809
1810         Cvar_RegisterVariable(&cl_deathnoviewmodel);
1811
1812         // for QW connections
1813         Cvar_RegisterVariable(&qport);
1814         Cvar_SetValueQuick(&qport, (rand() * RAND_MAX + rand()) & 0xffff);
1815
1816         Cmd_AddCommand("timerefresh", CL_TimeRefresh_f, "turn quickly and print rendering statistcs");
1817
1818         CL_Parse_Init();
1819         CL_Particles_Init();
1820         CL_Screen_Init();
1821
1822         CL_Video_Init();
1823 }
1824
1825
1826