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