dlights using light_lev now need the PFLAG_FULLDYNAMIC flag set to operate, otherwise...
[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
27 // we need to declare some mouse variables here, because the menu system
28 // references them even when on a unix system.
29
30 cvar_t cl_shownet = {0, "cl_shownet","0"};
31 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
32
33 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
34 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
35
36 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
37 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
38 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
39
40 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
41 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
42 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
43 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
44
45 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
46
47 cvar_t r_draweffects = {0, "r_draweffects", "1"};
48
49 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
50 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
51
52 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1"};
53 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1"};
54 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0"};
55
56 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0"};
57
58 mempool_t *cl_refdef_mempool;
59 mempool_t *cl_entities_mempool;
60
61 client_static_t cls;
62 client_state_t  cl;
63
64 int cl_max_entities;
65 int cl_max_static_entities;
66 int cl_max_temp_entities;
67 int cl_max_effects;
68 int cl_max_beams;
69 int cl_max_dlights;
70 int cl_max_lightstyle;
71 int cl_max_brushmodel_entities;
72
73 entity_t *cl_entities;
74 qbyte *cl_entities_active;
75 entity_t *cl_static_entities;
76 entity_t *cl_temp_entities;
77 cl_effect_t *cl_effects;
78 beam_t *cl_beams;
79 dlight_t *cl_dlights;
80 lightstyle_t *cl_lightstyle;
81 entity_render_t **cl_brushmodel_entities;
82
83 int cl_num_entities;
84 int cl_num_static_entities;
85 int cl_num_temp_entities;
86 int cl_num_brushmodel_entities;
87
88 /*
89 =====================
90 CL_ClearState
91
92 =====================
93 */
94 void CL_ClearState(void)
95 {
96         int i;
97
98         if (!sv.active)
99                 Host_ClearMemory ();
100
101         // note: this also gets rid of the entity database
102         Mem_EmptyPool(cl_entities_mempool);
103
104 // wipe the entire cl structure
105         memset (&cl, 0, sizeof(cl));
106
107         SZ_Clear (&cls.message);
108
109         cl_num_entities = 0;
110         cl_num_static_entities = 0;
111         cl_num_temp_entities = 0;
112         cl_num_brushmodel_entities = 0;
113
114         // tweak these if the game runs out
115         cl_max_entities = MAX_EDICTS;
116         cl_max_static_entities = 256;
117         cl_max_temp_entities = 512;
118         cl_max_effects = 256;
119         cl_max_beams = 24;
120         cl_max_dlights = MAX_DLIGHTS;
121         cl_max_lightstyle = MAX_LIGHTSTYLES;
122         cl_max_brushmodel_entities = MAX_EDICTS;
123
124         cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
125         cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
126         cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
127         cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
128         cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
129         cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
130         cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
131         cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
132         cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
133
134         CL_Screen_NewMap();
135
136         CL_Particles_Clear();
137
138         // LordHavoc: have to set up the baseline info for alpha and other stuff
139         for (i = 0;i < cl_max_entities;i++)
140         {
141                 ClearStateToDefault(&cl_entities[i].state_baseline);
142                 ClearStateToDefault(&cl_entities[i].state_previous);
143                 ClearStateToDefault(&cl_entities[i].state_current);
144         }
145
146         CL_CGVM_Clear();
147 }
148
149 /*
150 =====================
151 CL_Disconnect
152
153 Sends a disconnect message to the server
154 This is also called on Host_Error, so it shouldn't cause any errors
155 =====================
156 */
157 void CL_Disconnect(void)
158 {
159         if (cls.state == ca_dedicated)
160                 return;
161
162 // stop sounds (especially looping!)
163         S_StopAllSounds (true);
164
165         // clear contents blends
166         cl.cshifts[0].percent = 0;
167         cl.cshifts[1].percent = 0;
168         cl.cshifts[2].percent = 0;
169         cl.cshifts[3].percent = 0;
170
171         cl.worldmodel = NULL;
172
173         if (cls.demoplayback)
174                 CL_StopPlayback();
175         else if (cls.netcon)
176         {
177                 if (cls.demorecording)
178                         CL_Stop_f();
179
180                 Con_DPrintf("Sending clc_disconnect\n");
181                 SZ_Clear(&cls.message);
182                 MSG_WriteByte(&cls.message, clc_disconnect);
183                 NetConn_SendUnreliableMessage(cls.netcon, &cls.message);
184                 SZ_Clear(&cls.message);
185                 NetConn_Close(cls.netcon);
186                 cls.netcon = NULL;
187                 // if running a local server, shut it down
188                 if (sv.active)
189                 {
190                         // prevent this code from executing again during Host_ShutdownServer
191                         cls.state = ca_disconnected;
192                         Host_ShutdownServer(false);
193                 }
194         }
195         cls.state = ca_disconnected;
196
197         cls.demoplayback = cls.timedemo = false;
198         cls.signon = 0;
199 }
200
201 void CL_Disconnect_f(void)
202 {
203         CL_Disconnect ();
204         if (sv.active)
205                 Host_ShutdownServer (false);
206 }
207
208
209
210
211 /*
212 =====================
213 CL_EstablishConnection
214
215 Host should be either "local" or a net address
216 =====================
217 */
218 void CL_EstablishConnection(const char *host)
219 {
220         if (cls.state == ca_dedicated)
221                 return;
222
223         // clear menu's connect error message
224         m_return_reason[0] = 0;
225         cls.demonum = -1;
226
227         // stop demo loop in case this fails
228         CL_Disconnect();
229         NetConn_ClientFrame();
230         NetConn_ServerFrame();
231         
232         if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
233         {
234                 cls.connect_trying = true;
235                 cls.connect_remainingtries = 3;
236                 cls.connect_nextsendtime = 0;
237                 if (sv.active)
238                 {
239                         NetConn_ClientFrame();
240                         NetConn_ServerFrame();
241                         NetConn_ClientFrame();
242                         NetConn_ServerFrame();
243                         NetConn_ClientFrame();
244                         NetConn_ServerFrame();
245                         NetConn_ClientFrame();
246                         NetConn_ServerFrame();
247                 }
248         }
249         else
250         {
251                 Con_Printf("Unable to find a suitable network socket to connect to server.\n");
252                 strcpy(m_return_reason, "No network");
253         }
254 }
255
256 /*
257 ==============
258 CL_PrintEntities_f
259 ==============
260 */
261 static void CL_PrintEntities_f(void)
262 {
263         entity_t *ent;
264         int i, j;
265         char name[32];
266
267         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
268         {
269                 if (!ent->state_current.active)
270                         continue;
271
272                 if (ent->render.model)
273                         strlcpy (name, ent->render.model->name, 25);
274                 else
275                         strcpy(name, "--no model--");
276                 for (j = strlen(name);j < 25;j++)
277                         name[j] = ' ';
278                 Con_Printf ("%3i: %s:%04i (%5i %5i %5i) [%3i %3i %3i] %4.2f %5.3f\n", i, name, ent->render.frame, (int) ent->render.origin[0], (int) ent->render.origin[1], (int) ent->render.origin[2], (int) ent->render.angles[0] % 360, (int) ent->render.angles[1] % 360, (int) ent->render.angles[2] % 360, ent->render.scale, ent->render.alpha);
279         }
280 }
281
282 //static const vec3_t nomodelmins = {-16, -16, -16};
283 //static const vec3_t nomodelmaxs = {16, 16, 16};
284 void CL_BoundingBoxForEntity(entity_render_t *ent)
285 {
286         if (ent->model)
287         {
288                 //if (ent->angles[0] || ent->angles[2])
289                 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
290                 {
291                         // pitch or roll
292                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
293                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
294                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
295                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
296                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
297                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
298                         //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
299                         //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
300                 }
301                 //else if (ent->angles[1])
302                 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
303                 {
304                         // yaw
305                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
306                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
307                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
308                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
309                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
310                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
311                         //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
312                         //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
313                 }
314                 else
315                 {
316                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
317                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
318                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
319                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
320                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
321                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
322                         //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
323                         //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
324                 }
325         }
326         else
327         {
328                 ent->mins[0] = ent->matrix.m[0][3] - 16;
329                 ent->mins[1] = ent->matrix.m[1][3] - 16;
330                 ent->mins[2] = ent->matrix.m[2][3] - 16;
331                 ent->maxs[0] = ent->matrix.m[0][3] + 16;
332                 ent->maxs[1] = ent->matrix.m[1][3] + 16;
333                 ent->maxs[2] = ent->matrix.m[2][3] + 16;
334                 //VectorAdd(ent->origin, nomodelmins, ent->mins);
335                 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
336         }
337 }
338
339 /*
340 ===============
341 CL_LerpPoint
342
343 Determines the fraction between the last two messages that the objects
344 should be put at.
345 ===============
346 */
347 static float CL_LerpPoint(void)
348 {
349         float f;
350
351         // dropped packet, or start of demo
352         if (cl.mtime[1] < cl.mtime[0] - 0.1)
353                 cl.mtime[1] = cl.mtime[0] - 0.1;
354
355         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
356
357         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
358         f = cl.mtime[0] - cl.mtime[1];
359         if (!f || cl_nolerp.integer || cls.timedemo || cl.islocalgame)
360         {
361                 cl.time = cl.mtime[0];
362                 return 1;
363         }
364
365         f = (cl.time - cl.mtime[1]) / f;
366         return bound(0, f, 1);
367 }
368
369 void CL_ClearTempEntities (void)
370 {
371         cl_num_temp_entities = 0;
372 }
373
374 entity_t *CL_NewTempEntity(void)
375 {
376         entity_t *ent;
377
378         if (r_refdef.numentities >= r_refdef.maxentities)
379                 return NULL;
380         if (cl_num_temp_entities >= cl_max_temp_entities)
381                 return NULL;
382         ent = &cl_temp_entities[cl_num_temp_entities++];
383         memset (ent, 0, sizeof(*ent));
384         r_refdef.entities[r_refdef.numentities++] = &ent->render;
385
386         ent->render.colormap = -1; // no special coloring
387         ent->render.scale = 1;
388         ent->render.alpha = 1;
389         return ent;
390 }
391
392 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
393 {
394         int i;
395         cl_effect_t *e;
396         if (!modelindex) // sanity check
397                 return;
398         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
399         {
400                 if (e->active)
401                         continue;
402                 e->active = true;
403                 VectorCopy(org, e->origin);
404                 e->modelindex = modelindex;
405                 e->starttime = cl.time;
406                 e->startframe = startframe;
407                 e->endframe = startframe + framecount;
408                 e->framerate = framerate;
409
410                 e->frame = 0;
411                 e->frame1time = cl.time;
412                 e->frame2time = cl.time;
413                 break;
414         }
415 }
416
417 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)
418 {
419         int i;
420         dlight_t *dl;
421
422         /*
423 // first look for an exact key match
424         if (ent)
425         {
426                 dl = cl_dlights;
427                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
428                         if (dl->ent == ent)
429                                 goto dlightsetup;
430         }
431         */
432
433 // then look for anything else
434         dl = cl_dlights;
435         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
436                 if (!dl->radius)
437                         goto dlightsetup;
438
439         // unable to find one
440         return;
441
442 dlightsetup:
443         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
444         memset (dl, 0, sizeof(*dl));
445         dl->matrix = *matrix;
446         dl->ent = ent;
447         dl->origin[0] = dl->matrix.m[0][3];
448         dl->origin[1] = dl->matrix.m[1][3];
449         dl->origin[2] = dl->matrix.m[2][3];
450         CL_FindNonSolidLocation(dl->origin, dl->origin, 6);
451         dl->matrix.m[0][3] = dl->origin[0];
452         dl->matrix.m[1][3] = dl->origin[1];
453         dl->matrix.m[2][3] = dl->origin[2];
454         dl->radius = radius;
455         dl->color[0] = red;
456         dl->color[1] = green;
457         dl->color[2] = blue;
458         dl->decay = decay;
459         if (lifetime)
460                 dl->die = cl.time + lifetime;
461         else
462                 dl->die = 0;
463         dl->cubemapnum = cubemapnum;
464         dl->style = style;
465         dl->shadow = shadowenable;
466         dl->corona = corona;
467 }
468
469 void CL_DecayLights(void)
470 {
471         int i;
472         dlight_t *dl;
473         float time;
474
475         time = cl.time - cl.oldtime;
476         for (i = 0, dl = cl_dlights;i < MAX_DLIGHTS;i++, dl++)
477                 if (dl->radius)
478                         dl->radius = (cl.time < dl->die) ? max(0, dl->radius - time * dl->decay) : 0;
479 }
480
481 #define MAXVIEWMODELS 32
482 entity_t *viewmodels[MAXVIEWMODELS];
483 int numviewmodels;
484
485 matrix4x4_t viewmodelmatrix;
486
487 static int entitylinkframenumber;
488
489 static const vec3_t muzzleflashorigin = {18, 0, 0};
490
491 extern void V_DriftPitch(void);
492 extern void V_FadeViewFlashs(void);
493 extern void V_CalcViewBlend(void);
494
495 extern void V_CalcRefdef(void);
496 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
497 void CL_LinkNetworkEntity(entity_t *e)
498 {
499         matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2, dlightmatrix;
500         int j, k, l, trailtype, temp;
501         float origin[3], angles[3], delta[3], lerp, dlightcolor[3], dlightradius, mins[3], maxs[3], v[3], v2[3], d;
502         entity_t *t;
503         model_t *model;
504         //entity_persistent_t *p = &e->persistent;
505         //entity_render_t *r = &e->render;
506         if (e->persistent.linkframe != entitylinkframenumber)
507         {
508                 e->persistent.linkframe = entitylinkframenumber;
509                 // skip inactive entities and world
510                 if (!e->state_current.active || e == cl_entities)
511                         return;
512                 if (e->render.flags & RENDER_VIEWMODEL)
513                 {
514                         if (!r_drawviewmodel.integer || chase_active.integer || envmap)
515                                 return;
516                         if (cl.viewentity)
517                                 CL_LinkNetworkEntity(cl_entities + cl.viewentity);
518                         matrix = &viewmodelmatrix;
519                         if (e == &cl.viewent && cl.viewentity >= 0 && cl.viewentity < MAX_EDICTS && cl_entities[cl.viewentity].state_current.active)
520                         {
521                                 e->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
522                                 e->state_current.effects = EF_NOSHADOW | (cl_entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT));
523                         }
524                 }
525                 else
526                 {
527                         t = cl_entities + e->state_current.tagentity;
528                         if (!t->state_current.active)
529                                 return;
530                         // note: this can link to world
531                         CL_LinkNetworkEntity(t);
532                         // make relative to the entity
533                         matrix = &t->render.matrix;
534                         // if a valid tagindex is used, make it relative to that tag instead
535                         if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model) && e->state_current.tagindex <= t->render.model->alias.aliasnum_tags)
536                         {
537                                 // blend the matrices
538                                 memset(&blendmatrix, 0, sizeof(blendmatrix));
539                                 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
540                                 {
541                                         matrix = &t->render.model->alias.aliasdata_tags[t->render.frameblend[j].frame * t->render.model->alias.aliasnum_tags + (e->state_current.tagindex - 1)].matrix;
542                                         d = t->render.frameblend[j].lerp;
543                                         for (l = 0;l < 4;l++)
544                                                 for (k = 0;k < 4;k++)
545                                                         blendmatrix.m[l][k] += d * matrix->m[l][k];
546                                 }
547                                 // concat the tag matrices onto the entity matrix
548                                 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
549                                 // use the constructed tag matrix
550                                 matrix = &tempmatrix;
551                         }
552                 }
553                 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
554                 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
555                 e->render.flags = e->state_current.flags;
556                 if (e - cl_entities == cl.viewentity)
557                         e->render.flags |= RENDER_EXTERIORMODEL;
558                 e->render.effects = e->state_current.effects;
559                 if (e->state_current.flags & RENDER_COLORMAPPED)
560                         e->render.colormap = e->state_current.colormap;
561                 else if (cl.scores != NULL && e->state_current.colormap)
562                         e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
563                 else
564                         e->render.colormap = -1; // no special coloring
565                 e->render.skinnum = e->state_current.skin;
566                 // set up the render matrix
567                 if (e->state_previous.active && e->state_current.modelindex == e->state_previous.modelindex)
568                 {
569                         // movement lerp
570                         if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
571                         {
572                                 // interpolate the origin and angles
573                                 VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
574                                 VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
575                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
576                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
577                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
578                                 VectorMA(e->persistent.oldangles, lerp, delta, angles);
579                         }
580                         else
581                         {
582                                 // no interpolation
583                                 VectorCopy(e->persistent.neworigin, origin);
584                                 VectorCopy(e->persistent.newangles, angles);
585                         }
586                         // animation lerp
587                         if (e->render.frame2 == e->state_current.frame)
588                         {
589                                 // update frame lerp fraction
590                                 e->render.framelerp = 1;
591                                 if (e->render.frame2time > e->render.frame1time)
592                                 {
593                                         e->render.framelerp = (cl.time - e->render.frame2time) / (e->render.frame2time - e->render.frame1time);
594                                         e->render.framelerp = bound(0, e->render.framelerp, 1);
595                                 }
596                         }
597                         else
598                         {
599                                 // begin a new frame lerp
600                                 e->render.frame1 = e->render.frame2;
601                                 e->render.frame1time = e->render.frame2time;
602                                 e->render.frame = e->render.frame2 = e->state_current.frame;
603                                 e->render.frame2time = cl.time;
604                                 e->render.framelerp = 0;
605                                 // make sure frame lerp won't last longer than 100ms
606                                 // (this mainly helps with models that use framegroups and
607                                 // switch between them infrequently)
608                                 e->render.frame1time = max(e->render.frame1time, e->render.frame2time - 0.1f);
609                         }
610                 }
611                 else
612                 {
613                         // no interpolation
614                         VectorCopy(e->persistent.neworigin, origin);
615                         VectorCopy(e->persistent.newangles, angles);
616                         e->render.frame = e->render.frame1 = e->render.frame2 = e->state_current.frame;
617                         e->render.frame1time = e->render.frame2time = cl.time;
618                         e->render.framelerp = 1;
619                 }
620
621                 e->render.model = cl.model_precache[e->state_current.modelindex];
622                 if (e->render.model)
623                 {
624                         Mod_CheckLoaded(e->render.model);
625                         if (e->render.model->type == mod_alias || e->render.model->type == mod_sprite)
626                                 angles[0] = -angles[0];
627                         if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
628                         {
629                                 angles[1] = ANGLEMOD(100*cl.time);
630                                 if (cl_itembobheight.value)
631                                         origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
632                         }
633                 }
634
635                 R_LerpAnimation(&e->render);
636
637                 // FIXME: e->render.scale should go away
638                 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
639                 // concat the matrices to make the entity relative to its tag
640                 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
641                 // make the other useful stuff
642                 Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
643                 CL_BoundingBoxForEntity(&e->render);
644
645                 // handle effects now that we know where this entity is in the world...
646                 origin[0] = e->render.matrix.m[0][3];
647                 origin[1] = e->render.matrix.m[1][3];
648                 origin[2] = e->render.matrix.m[2][3];
649                 trailtype = -1;
650                 dlightradius = 0;
651                 dlightcolor[0] = 0;
652                 dlightcolor[1] = 0;
653                 dlightcolor[2] = 0;
654                 // LordHavoc: if the entity has no effects, don't check each
655                 if (e->render.effects)
656                 {
657                         if (e->render.effects & EF_BRIGHTFIELD)
658                         {
659                                 if (gamemode == GAME_NEXUIZ)
660                                 {
661                                         dlightradius = max(dlightradius, 200);
662                                         dlightcolor[0] += 0.390625f;
663                                         dlightcolor[1] += 0.781250f;
664                                         dlightcolor[2] += 1.562500f;
665                                         trailtype = 8;
666                                 }
667                                 else
668                                         CL_EntityParticles(e);
669                         }
670                         if (e->render.effects & EF_MUZZLEFLASH)
671                                 e->persistent.muzzleflash = 100.0f;
672                         if (e->render.effects & EF_DIMLIGHT)
673                         {
674                                 dlightradius = max(dlightradius, 200);
675                                 dlightcolor[0] += 0.781250f;
676                                 dlightcolor[1] += 0.781250f;
677                                 dlightcolor[2] += 0.781250f;
678                         }
679                         if (e->render.effects & EF_BRIGHTLIGHT)
680                         {
681                                 dlightradius = max(dlightradius, 400);
682                                 dlightcolor[0] += 1.562500f;
683                                 dlightcolor[1] += 1.562500f;
684                                 dlightcolor[2] += 1.562500f;
685                         }
686                         // LordHavoc: more effects
687                         if (e->render.effects & EF_RED) // red
688                         {
689                                 dlightradius = max(dlightradius, 200);
690                                 dlightcolor[0] += 0.781250f;
691                                 dlightcolor[1] += 0.078125f;
692                                 dlightcolor[2] += 0.078125f;
693                         }
694                         if (e->render.effects & EF_BLUE) // blue
695                         {
696                                 dlightradius = max(dlightradius, 200);
697                                 dlightcolor[0] += 0.078125f;
698                                 dlightcolor[1] += 0.078125f;
699                                 dlightcolor[2] += 0.781250f;
700                         }
701                         if (e->render.effects & EF_FLAME)
702                         {
703                                 mins[0] = origin[0] - 16.0f;
704                                 mins[1] = origin[1] - 16.0f;
705                                 mins[2] = origin[2] - 16.0f;
706                                 maxs[0] = origin[0] + 16.0f;
707                                 maxs[1] = origin[1] + 16.0f;
708                                 maxs[2] = origin[2] + 16.0f;
709                                 // how many flames to make
710                                 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
711                                 CL_FlameCube(mins, maxs, temp);
712                                 d = lhrandom(0.75f, 1);
713                                 dlightradius = max(dlightradius, 200);
714                                 dlightcolor[0] += d * 1.0f;
715                                 dlightcolor[1] += d * 0.7f;
716                                 dlightcolor[2] += d * 0.3f;
717                         }
718                         if (e->render.effects & EF_STARDUST)
719                         {
720                                 mins[0] = origin[0] - 16.0f;
721                                 mins[1] = origin[1] - 16.0f;
722                                 mins[2] = origin[2] - 16.0f;
723                                 maxs[0] = origin[0] + 16.0f;
724                                 maxs[1] = origin[1] + 16.0f;
725                                 maxs[2] = origin[2] + 16.0f;
726                                 // how many particles to make
727                                 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
728                                 CL_Stardust(mins, maxs, temp);
729                                 d = 0.390625f;
730                                 dlightradius = max(dlightradius, 200);
731                                 dlightcolor[0] += d * 1.0f;
732                                 dlightcolor[1] += d * 0.7f;
733                                 dlightcolor[2] += d * 0.3f;
734                         }
735                 }
736                 // muzzleflash fades over time, and is offset a bit
737                 if (e->persistent.muzzleflash > 0)
738                 {
739                         Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
740                         CL_TraceLine(origin, v2, v, NULL, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY);
741                         tempmatrix = e->render.matrix;
742                         tempmatrix.m[0][3] = v[0];
743                         tempmatrix.m[1][3] = v[1];
744                         tempmatrix.m[2][3] = v[2];
745                         CL_AllocDlight(NULL, &tempmatrix, e->persistent.muzzleflash, 1, 1, 1, 0, 0, 0, 0, true, 1);
746                         e->persistent.muzzleflash -= cl.frametime * 1000;
747                 }
748                 // LordHavoc: if the model has no flags, don't check each
749                 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
750                 {
751                         if (e->render.model->flags & EF_GIB)
752                                 trailtype = 2;
753                         else if (e->render.model->flags & EF_ZOMGIB)
754                                 trailtype = 4;
755                         else if (e->render.model->flags & EF_TRACER)
756                         {
757                                 trailtype = 3;
758                                 dlightradius = max(dlightradius, 100);
759                                 dlightcolor[0] += 0.060000f;
760                                 dlightcolor[1] += 0.250000f;
761                                 dlightcolor[2] += 0.062500f;
762                         }
763                         else if (e->render.model->flags & EF_TRACER2)
764                         {
765                                 trailtype = 5;
766                                 dlightradius = max(dlightradius, 100);
767                                 dlightcolor[0] += 0.312500f;
768                                 dlightcolor[1] += 0.187500f;
769                                 dlightcolor[2] += 0.062500f;
770                         }
771                         else if (e->render.model->flags & EF_ROCKET)
772                         {
773                                 trailtype = 0;
774                                 dlightradius = max(dlightradius, 200);
775                                 dlightcolor[0] += 0.781250f;
776                                 dlightcolor[1] += 0.625000f;
777                                 dlightcolor[2] += 0.312500f;
778                         }
779                         else if (e->render.model->flags & EF_GRENADE)
780                         {
781                                 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
782                                 trailtype = e->render.alpha == -1 ? 7 : 1;
783                         }
784                         else if (e->render.model->flags & EF_TRACER3)
785                         {
786                                 trailtype = 6;
787                                 dlightradius = max(dlightradius, 200);
788                                 dlightcolor[0] += 0.312500f;
789                                 dlightcolor[1] += 0.125000f;
790                                 dlightcolor[2] += 0.250000f;
791                         }
792                 }
793                 // LordHavoc: customizable glow
794                 if (e->state_current.glowsize)
795                 {
796                         // * 4 for the expansion from 0-255 to 0-1023 range,
797                         // / 255 to scale down byte colors
798                         dlightradius = max(dlightradius, e->state_current.glowsize * 4);
799                         VectorMA(dlightcolor, (1.0f / 255.0f), (qbyte *)&palette_complete[e->state_current.glowcolor], dlightcolor);
800                 }
801                 if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
802                 {
803                         if (e->state_current.light[3])
804                                 dlightradius = max(dlightradius, e->state_current.light[3]);
805                         else
806                                 dlightradius = max(dlightradius, 350);
807                         if (VectorLength2(dlightcolor) == 0)
808                                 (dlightcolor[0] += 1, dlightcolor[1] += 1, dlightcolor[2] += 1);
809                         else
810                                 VectorMA(dlightcolor, (1.0f/256.0f), e->state_current.light, dlightcolor);
811                 }
812                 // make the dlight
813                 if (dlightradius > 0 && (dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL))
814                 {
815                         dlightmatrix = e->render.matrix;
816                         // hack to make glowing player light shine on their gun
817                         if ((e - cl_entities) == cl.viewentity/* && !chase_active.integer*/)
818                                 dlightmatrix.m[2][3] += 30;
819                         CL_AllocDlight(&e->render, &dlightmatrix, dlightradius, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0, e->state_current.skin >= 16 ? e->state_current.skin : 0, e->state_current.lightstyle, !(e->state_current.lightpflags & PFLAGS_NOSHADOW), (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC) ? ((e->state_current.lightpflags & PFLAGS_CORONA) != 0) : 1);
820                 }
821                 // trails need the previous frame
822                 if (e->state_previous.active && e->state_previous.modelindex == e->state_current.modelindex)
823                 {
824                         if (e->render.flags & RENDER_GLOWTRAIL)
825                                 CL_RocketTrail2(e->persistent.trail_origin, origin, e->state_current.glowcolor, e);
826                         else if (trailtype >= 0)
827                                 CL_RocketTrail(e->persistent.trail_origin, origin, trailtype, e);
828                 }
829                 VectorCopy(origin, e->persistent.trail_origin);
830                 // note: the cl.viewentity and intermission check is to hide player
831                 // shadow during intermission and during the Nehahra movie and
832                 // Nehahra cinematics
833                 if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE))
834                  && (e->render.alpha == 1)
835                  && !(e->render.flags & RENDER_VIEWMODEL)
836                  && ((e - cl_entities) != cl.viewentity || (!cl.intermission && cl.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
837                         e->render.flags |= RENDER_SHADOW;
838                 if (!(e->render.effects & EF_FULLBRIGHT))
839                         e->render.flags |= RENDER_LIGHT;
840                 // as soon as player is known we can call V_CalcRefDef
841                 if ((e - cl_entities) == cl.viewentity)
842                         V_CalcRefdef();
843                 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
844                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &e->render;
845                 if (gamemode == GAME_TENEBRAE && e->render.model && e->render.model->type == mod_sprite)
846                         e->render.effects |= EF_ADDITIVE;
847                 // don't show entities with no modelindex (note: this still shows
848                 // entities which have a modelindex that resolved to a NULL model)
849                 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
850                         r_refdef.entities[r_refdef.numentities++] = &e->render;
851                 if (cl_num_entities < e->state_current.number + 1)
852                         cl_num_entities = e->state_current.number + 1;
853                 //if (cl.viewentity && e - cl_entities == cl.viewentity)
854                 //      Matrix4x4_Print(&e->render.matrix);
855         }
856 }
857
858 void CL_RelinkWorld(void)
859 {
860         entity_t *ent = &cl_entities[0];
861         if (cl_num_entities < 1)
862                 cl_num_entities = 1;
863         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
864         // FIXME: this should be done at load
865         Matrix4x4_CreateIdentity(&ent->render.matrix);
866         Matrix4x4_CreateIdentity(&ent->render.inversematrix);
867         CL_BoundingBoxForEntity(&ent->render);
868 }
869
870 static void CL_RelinkStaticEntities(void)
871 {
872         int i;
873         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
874         {
875                 Mod_CheckLoaded(cl_static_entities[i].render.model);
876                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
877         }
878 }
879
880 /*
881 ===============
882 CL_RelinkEntities
883 ===============
884 */
885 static void CL_RelinkNetworkEntities(void)
886 {
887         entity_t *ent;
888         int i;
889
890         ent = &cl.viewent;
891         ent->state_previous = ent->state_current;
892         ClearStateToDefault(&ent->state_current);
893         ent->state_current.time = cl.time;
894         ent->state_current.number = -1;
895         ent->state_current.active = true;
896         ent->state_current.modelindex = cl.stats[STAT_WEAPON];
897         ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
898         ent->state_current.flags = RENDER_VIEWMODEL;
899         if (cl.stats[STAT_HEALTH] <= 0 || cl.intermission)
900                 ent->state_current.modelindex = 0;
901         else if (cl.items & IT_INVISIBILITY)
902         {
903                 if (gamemode == GAME_TRANSFUSION)
904                         ent->state_current.alpha = 128;
905                 else
906                         ent->state_current.modelindex = 0;
907         }
908
909         // start on the entity after the world
910         entitylinkframenumber++;
911         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
912         {
913                 if (cl_entities_active[i])
914                 {
915                         if (ent->state_current.active)
916                                 CL_LinkNetworkEntity(ent);
917                         else
918                                 cl_entities_active[i] = false;
919                 }
920         }
921         CL_LinkNetworkEntity(&cl.viewent);
922 }
923
924 static void CL_RelinkEffects(void)
925 {
926         int i, intframe;
927         cl_effect_t *e;
928         entity_t *ent;
929         float frame;
930
931         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
932         {
933                 if (e->active)
934                 {
935                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
936                         intframe = frame;
937                         if (intframe < 0 || intframe >= e->endframe)
938                         {
939                                 memset(e, 0, sizeof(*e));
940                                 continue;
941                         }
942
943                         if (intframe != e->frame)
944                         {
945                                 e->frame = intframe;
946                                 e->frame1time = e->frame2time;
947                                 e->frame2time = cl.time;
948                         }
949
950                         // if we're drawing effects, get a new temp entity
951                         // (NewTempEntity adds it to the render entities list for us)
952                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
953                         {
954                                 // interpolation stuff
955                                 ent->render.frame1 = intframe;
956                                 ent->render.frame2 = intframe + 1;
957                                 if (ent->render.frame2 >= e->endframe)
958                                         ent->render.frame2 = -1; // disappear
959                                 ent->render.framelerp = frame - intframe;
960                                 ent->render.frame1time = e->frame1time;
961                                 ent->render.frame2time = e->frame2time;
962
963                                 // normal stuff
964                                 ent->render.model = cl.model_precache[e->modelindex];
965                                 ent->render.frame = ent->render.frame2;
966                                 ent->render.colormap = -1; // no special coloring
967                                 ent->render.alpha = 1;
968
969                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
970                                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
971                                 CL_BoundingBoxForEntity(&ent->render);
972                         }
973                 }
974         }
975 }
976
977 void CL_RelinkBeams(void)
978 {
979         int i;
980         beam_t *b;
981         vec3_t dist, org;
982         float d;
983         entity_t *ent;
984         float yaw, pitch;
985         float forward;
986         matrix4x4_t tempmatrix;
987
988         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
989         {
990                 if (!b->model || b->endtime < cl.time)
991                         continue;
992
993                 // if coming from the player, update the start position
994                 //if (b->entity == cl.viewentity)
995                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
996                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
997                 {
998                         entity_state_t *p = &cl_entities[b->entity].state_previous;
999                         //entity_state_t *c = &cl_entities[b->entity].state_current;
1000                         entity_render_t *r = &cl_entities[b->entity].render;
1001                         matrix4x4_t matrix, imatrix;
1002                         if (b->relativestartvalid == 2)
1003                         {
1004                                 // not really valid yet, we need to get the orientation now
1005                                 // (ParseBeam flagged this because it is received before
1006                                 //  entities are received, by now they have been received)
1007                                 // note: because players create lightning in their think
1008                                 // function (which occurs before movement), they actually
1009                                 // have some lag in it's location, so compare to the
1010                                 // previous player state, not the latest
1011                                 if (b->entity == cl.viewentity)
1012                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
1013                                 else
1014                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
1015                                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
1016                                 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
1017                                 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
1018                                 b->relativestartvalid = 1;
1019                         }
1020                         else
1021                         {
1022                                 if (b->entity == cl.viewentity)
1023                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
1024                                 else
1025                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
1026                                 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
1027                                 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
1028                         }
1029                 }
1030
1031                 if (b->lightning)
1032                 {
1033                         if (cl_beams_lightatend.integer)
1034                         {
1035                                 // FIXME: create a matrix from the beam start/end orientation
1036                                 Matrix4x4_CreateTranslate(&tempmatrix, b->end[0], b->end[1], b->end[2]);
1037                                 CL_AllocDlight (NULL, &tempmatrix, 200, 0.3, 0.7, 1, 0, 0, 0, 0, true, 1);
1038                         }
1039                         if (cl_beams_polygons.integer)
1040                                 continue;
1041                 }
1042
1043                 // calculate pitch and yaw
1044                 VectorSubtract (b->end, b->start, dist);
1045
1046                 if (dist[1] == 0 && dist[0] == 0)
1047                 {
1048                         yaw = 0;
1049                         if (dist[2] > 0)
1050                                 pitch = 90;
1051                         else
1052                                 pitch = 270;
1053                 }
1054                 else
1055                 {
1056                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1057                         if (yaw < 0)
1058                                 yaw += 360;
1059
1060                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1061                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1062                         if (pitch < 0)
1063                                 pitch += 360;
1064                 }
1065
1066                 // add new entities for the lightning
1067                 VectorCopy (b->start, org);
1068                 d = VectorNormalizeLength(dist);
1069                 while (d > 0)
1070                 {
1071                         ent = CL_NewTempEntity ();
1072                         if (!ent)
1073                                 return;
1074                         //VectorCopy (org, ent->render.origin);
1075                         ent->render.model = b->model;
1076                         ent->render.effects = EF_FULLBRIGHT;
1077                         //ent->render.angles[0] = pitch;
1078                         //ent->render.angles[1] = yaw;
1079                         //ent->render.angles[2] = rand()%360;
1080                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1081                         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1082                         CL_BoundingBoxForEntity(&ent->render);
1083                         VectorMA(org, 30, dist, org);
1084                         d -= 30;
1085                 }
1086         }
1087 }
1088
1089 void CL_LerpPlayer(float frac)
1090 {
1091         int i;
1092         float d;
1093
1094         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1095
1096         for (i = 0;i < 3;i++)
1097                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1098
1099         if (cls.demoplayback)
1100         {
1101                 // interpolate the angles
1102                 for (i = 0;i < 3;i++)
1103                 {
1104                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1105                         if (d > 180)
1106                                 d -= 360;
1107                         else if (d < -180)
1108                                 d += 360;
1109                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1110                 }
1111         }
1112 }
1113
1114 /*
1115 ===============
1116 CL_ReadFromServer
1117
1118 Read all incoming data from the server
1119 ===============
1120 */
1121 int CL_ReadFromServer(void)
1122 {
1123         CL_ReadDemoMessage();
1124
1125         r_refdef.numentities = 0;
1126         cl_num_entities = 0;
1127         cl_num_brushmodel_entities = 0;
1128
1129         if (cls.state == ca_connected && cls.signon == SIGNONS)
1130         {
1131                 // prepare for a new frame
1132                 CL_LerpPlayer(CL_LerpPoint());
1133                 CL_DecayLights();
1134                 CL_ClearTempEntities();
1135                 V_DriftPitch();
1136                 V_FadeViewFlashs();
1137
1138                 // relink network entities (note: this sets up the view!)
1139                 CL_RelinkNetworkEntities();
1140
1141                 // move particles
1142                 CL_MoveParticles();
1143                 R_MoveExplosions();
1144
1145                 // link stuff
1146                 CL_RelinkWorld();
1147                 CL_RelinkStaticEntities();
1148                 CL_RelinkBeams();
1149                 CL_RelinkEffects();
1150
1151                 // run cgame code (which can add more entities)
1152                 CL_CGVM_Frame();
1153
1154                 // update view blend
1155                 V_CalcViewBlend();
1156         }
1157
1158         return 0;
1159 }
1160
1161 /*
1162 =================
1163 CL_SendCmd
1164 =================
1165 */
1166 void CL_SendCmd(usercmd_t *cmd)
1167 {
1168         if (cls.signon == SIGNONS)
1169                 CL_SendMove(cmd);
1170
1171         if (cls.demoplayback)
1172         {
1173                 SZ_Clear(&cls.message);
1174                 return;
1175         }
1176
1177         // send the reliable message (forwarded commands) if there is one
1178         if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1179         {
1180                 if (developer.integer)
1181                 {
1182                         Con_Printf("CL_SendCmd: sending reliable message:\n");
1183                         SZ_HexDumpToConsole(&cls.message);
1184                 }
1185                 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1186                         Host_Error("CL_WriteToServer: lost server connection");
1187                 SZ_Clear(&cls.message);
1188         }
1189 }
1190
1191 // LordHavoc: pausedemo command
1192 static void CL_PauseDemo_f (void)
1193 {
1194         cls.demopaused = !cls.demopaused;
1195         if (cls.demopaused)
1196                 Con_Printf("Demo paused\n");
1197         else
1198                 Con_Printf("Demo unpaused\n");
1199 }
1200
1201 /*
1202 ======================
1203 CL_Fog_f
1204 ======================
1205 */
1206 static void CL_Fog_f (void)
1207 {
1208         if (Cmd_Argc () == 1)
1209         {
1210                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1211                 return;
1212         }
1213         fog_density = atof(Cmd_Argv(1));
1214         fog_red = atof(Cmd_Argv(2));
1215         fog_green = atof(Cmd_Argv(3));
1216         fog_blue = atof(Cmd_Argv(4));
1217 }
1218
1219 /*
1220 =================
1221 CL_Init
1222 =================
1223 */
1224 void CL_Init (void)
1225 {
1226         cl_entities_mempool = Mem_AllocPool("client entities");
1227         cl_refdef_mempool = Mem_AllocPool("refdef");
1228
1229         memset(&r_refdef, 0, sizeof(r_refdef));
1230         // max entities sent to renderer per frame
1231         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1232         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1233         // 256k drawqueue buffer
1234         r_refdef.maxdrawqueuesize = 256 * 1024;
1235         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1236
1237         SZ_Alloc (&cls.message, 1024, "cls.message");
1238
1239         CL_InitInput ();
1240         CL_InitTEnts ();
1241
1242 //
1243 // register our commands
1244 //
1245         Cvar_RegisterVariable (&cl_upspeed);
1246         Cvar_RegisterVariable (&cl_forwardspeed);
1247         Cvar_RegisterVariable (&cl_backspeed);
1248         Cvar_RegisterVariable (&cl_sidespeed);
1249         Cvar_RegisterVariable (&cl_movespeedkey);
1250         Cvar_RegisterVariable (&cl_yawspeed);
1251         Cvar_RegisterVariable (&cl_pitchspeed);
1252         Cvar_RegisterVariable (&cl_anglespeedkey);
1253         Cvar_RegisterVariable (&cl_shownet);
1254         Cvar_RegisterVariable (&cl_nolerp);
1255         Cvar_RegisterVariable (&lookspring);
1256         Cvar_RegisterVariable (&lookstrafe);
1257         Cvar_RegisterVariable (&sensitivity);
1258         Cvar_RegisterVariable (&freelook);
1259
1260         Cvar_RegisterVariable (&m_pitch);
1261         Cvar_RegisterVariable (&m_yaw);
1262         Cvar_RegisterVariable (&m_forward);
1263         Cvar_RegisterVariable (&m_side);
1264
1265         Cvar_RegisterVariable (&cl_itembobspeed);
1266         Cvar_RegisterVariable (&cl_itembobheight);
1267
1268         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1269         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1270         Cmd_AddCommand ("record", CL_Record_f);
1271         Cmd_AddCommand ("stop", CL_Stop_f);
1272         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1273         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1274
1275         Cmd_AddCommand ("fog", CL_Fog_f);
1276
1277         // LordHavoc: added pausedemo
1278         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1279
1280         Cvar_RegisterVariable(&r_draweffects);
1281         Cvar_RegisterVariable(&cl_explosions);
1282         Cvar_RegisterVariable(&cl_stainmaps);
1283         Cvar_RegisterVariable(&cl_beams_polygons);
1284         Cvar_RegisterVariable(&cl_beams_relative);
1285         Cvar_RegisterVariable(&cl_beams_lightatend);
1286         Cvar_RegisterVariable(&cl_noplayershadow);
1287
1288         CL_Parse_Init();
1289         CL_Particles_Init();
1290         CL_Screen_Init();
1291         CL_CGVM_Init();
1292
1293         CL_Video_Init();
1294 }
1295