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