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