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