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