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