]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_main.c
removed cgame and ui code (both unused), this reduces memory use a bit
[xonotic/darkplaces.git] / cl_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_main.c  -- client main loop
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "cl_video.h"
25 #include "image.h"
26 #include "csprogs.h"
27 #include "r_shadow.h"
28
29 // we need to declare some mouse variables here, because the menu system
30 // references them even when on a unix system.
31
32 cvar_t csqc_progname = {0, "csqc_progname","csprogs.dat","name of csprogs.dat file to load"};   //[515]: csqc crc check and right csprogs name according to progs.dat
33 cvar_t csqc_progcrc = {CVAR_READONLY, "csqc_progcrc","0","CRC of csprogs.dat file to load"};
34
35 cvar_t cl_shownet = {0, "cl_shownet","0","1 = print packet size, 2 = print packet message list"};
36 cvar_t cl_nolerp = {0, "cl_nolerp", "0","network update smoothing"};
37
38 cvar_t cl_itembobheight = {0, "cl_itembobheight", "0","how much items bob up and down (try 8)"};
39 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5","how frequently items bob up and down"};
40
41 cvar_t lookspring = {CVAR_SAVE, "lookspring","0","returns pitch to level with the floor when no longer holding a pitch key"};
42 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0","move instead of turning"};
43 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3","mouse speed multiplier"};
44
45 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022","mouse pitch speed multiplier"};
46 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022","mouse yaw speed multiplier"};
47 cvar_t m_forward = {CVAR_SAVE, "m_forward","1","mouse forward speed multiplier"};
48 cvar_t m_side = {CVAR_SAVE, "m_side","0.8","mouse side speed multiplier"};
49
50 cvar_t freelook = {CVAR_SAVE, "freelook", "1","mouse controls pitch instead of forward/back"};
51
52 cvar_t r_draweffects = {0, "r_draweffects", "1","renders temporary sprite effects"};
53
54 cvar_t cl_explosions_alpha_start = {CVAR_SAVE, "cl_explosions_alpha_start", "1.5","starting alpha of an explosion shell"};
55 cvar_t cl_explosions_alpha_end = {CVAR_SAVE, "cl_explosions_alpha_end", "0","end alpha of an explosion shell (just before it disappears)"};
56 cvar_t cl_explosions_size_start = {CVAR_SAVE, "cl_explosions_size_start", "16","starting size of an explosion shell"};
57 cvar_t cl_explosions_size_end = {CVAR_SAVE, "cl_explosions_size_end", "128","ending alpha of an explosion shell (just before it disappears)"};
58 cvar_t cl_explosions_lifetime = {CVAR_SAVE, "cl_explosions_lifetime", "0.5","how long an explosion shell lasts"};
59
60 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1","stains lightmaps, much faster than decals but blurred"};
61 cvar_t cl_stainmaps_clearonload = {CVAR_SAVE, "cl_stainmaps_clearonload", "1","clear stainmaps on map restart"};
62
63 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1","use beam polygons instead of models"};
64 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1","beams are relative to owner (smooth sweeps)"};
65 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0","make a light at the end of the beam"};
66
67 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0","hide player shadow"};
68
69 cvar_t qport = {0, "qport", "0", "identification key for playing on qw servers (allows you to maintain a connection to a quakeworld server even if your port changes)"};
70
71 cvar_t cl_prydoncursor = {0, "cl_prydoncursor", "0", "enables a mouse pointer which is able to click on entities in the world, useful for point and click mods, see PRYDON_CLIENTCURSOR extension in dpextensions.qc"};
72
73 cvar_t cl_deathnoviewmodel = {0, "cl_deathnoviewmodel", "1", "hides gun model when dead"};
74
75 vec3_t cl_playerstandmins;
76 vec3_t cl_playerstandmaxs;
77 vec3_t cl_playercrouchmins;
78 vec3_t cl_playercrouchmaxs;
79
80 mempool_t *cl_mempool;
81
82 client_static_t cls;
83 client_state_t  cl;
84
85 int cl_max_entities;
86 int cl_max_csqcentities;
87 int cl_max_static_entities;
88 int cl_max_temp_entities;
89 int cl_max_effects;
90 int cl_max_beams;
91 int cl_max_dlights;
92 int cl_max_lightstyle;
93 int cl_max_brushmodel_entities;
94 int cl_activedlights;
95 int cl_activeeffects;
96 int cl_activebeams;
97
98 entity_t *cl_entities;
99 entity_t *cl_csqcentities;      //[515]: csqc
100 unsigned char *cl_entities_active;
101 unsigned char *cl_csqcentities_active;  //[515]: csqc
102 entity_t *cl_static_entities;
103 entity_t *cl_temp_entities;
104 cl_effect_t *cl_effects;
105 beam_t *cl_beams;
106 dlight_t *cl_dlights;
107 lightstyle_t *cl_lightstyle;
108 int *cl_brushmodel_entities;
109
110 int cl_num_entities;
111 int cl_num_csqcentities;        //[515]: csqc
112 int cl_num_static_entities;
113 int cl_num_temp_entities;
114 int cl_num_brushmodel_entities;
115
116 // keep track of quake entities because they need to be killed if they get stale
117 extern int cl_lastquakeentity;
118 extern unsigned char cl_isquakeentity[MAX_EDICTS];
119
120 /*
121 =====================
122 CL_ClearState
123
124 =====================
125 */
126 void CL_ClearState(void)
127 {
128         int i;
129         entity_t *ent;
130
131         if (cl_entities) Mem_Free(cl_entities);cl_entities = NULL;
132         if (cl_csqcentities) Mem_Free(cl_csqcentities);cl_csqcentities = NULL;  //[515]: csqc
133         if (cl_entities_active) Mem_Free(cl_entities_active);cl_entities_active = NULL;
134         if (cl_csqcentities_active) Mem_Free(cl_csqcentities_active);cl_csqcentities_active = NULL;     //[515]: csqc
135         if (cl_static_entities) Mem_Free(cl_static_entities);cl_static_entities = NULL;
136         if (cl_temp_entities) Mem_Free(cl_temp_entities);cl_temp_entities = NULL;
137         if (cl_effects) Mem_Free(cl_effects);cl_effects = NULL;
138         if (cl_beams) Mem_Free(cl_beams);cl_beams = NULL;
139         if (cl_dlights) Mem_Free(cl_dlights);cl_dlights = NULL;
140         if (cl_lightstyle) Mem_Free(cl_lightstyle);cl_lightstyle = NULL;
141         if (cl_brushmodel_entities) Mem_Free(cl_brushmodel_entities);cl_brushmodel_entities = NULL;
142         if (cl.entitydatabase) EntityFrame_FreeDatabase(cl.entitydatabase);cl.entitydatabase = NULL;
143         if (cl.entitydatabase4) EntityFrame4_FreeDatabase(cl.entitydatabase4);cl.entitydatabase4 = NULL;
144         if (cl.entitydatabaseqw) EntityFrameQW_FreeDatabase(cl.entitydatabaseqw);cl.entitydatabaseqw = NULL;
145         if (cl.scores) Mem_Free(cl.scores);cl.scores = NULL;
146
147         if (!sv.active)
148                 Host_ClearMemory ();
149
150 // wipe the entire cl structure
151         memset (&cl, 0, sizeof(cl));
152
153         S_StopAllSounds();
154
155         // reset the view zoom interpolation
156         cl.mviewzoom[0] = cl.mviewzoom[1] = 1;
157
158         cl_num_entities = 0;
159         cl_num_csqcentities = 0;        //[515]: csqc
160         cl_num_static_entities = 0;
161         cl_num_temp_entities = 0;
162         cl_num_brushmodel_entities = 0;
163
164         // tweak these if the game runs out
165         cl_max_entities = 256;
166         cl_max_csqcentities = 256;      //[515]: csqc
167         cl_max_static_entities = 256;
168         cl_max_temp_entities = 512;
169         cl_max_effects = 256;
170         cl_max_beams = 256;
171         cl_max_dlights = MAX_DLIGHTS;
172         cl_max_lightstyle = MAX_LIGHTSTYLES;
173         cl_max_brushmodel_entities = MAX_EDICTS;
174         cl_activedlights = 0;
175         cl_activeeffects = 0;
176         cl_activebeams = 0;
177
178         cl_entities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_entities * sizeof(entity_t));
179         cl_csqcentities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_csqcentities * sizeof(entity_t));    //[515]: csqc
180         cl_entities_active = (unsigned char *)Mem_Alloc(cl_mempool, cl_max_brushmodel_entities * sizeof(unsigned char));
181         cl_csqcentities_active = (unsigned char *)Mem_Alloc(cl_mempool, cl_max_brushmodel_entities * sizeof(unsigned char));    //[515]: csqc
182         cl_static_entities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_static_entities * sizeof(entity_t));
183         cl_temp_entities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_temp_entities * sizeof(entity_t));
184         cl_effects = (cl_effect_t *)Mem_Alloc(cl_mempool, cl_max_effects * sizeof(cl_effect_t));
185         cl_beams = (beam_t *)Mem_Alloc(cl_mempool, cl_max_beams * sizeof(beam_t));
186         cl_dlights = (dlight_t *)Mem_Alloc(cl_mempool, cl_max_dlights * sizeof(dlight_t));
187         cl_lightstyle = (lightstyle_t *)Mem_Alloc(cl_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
188         cl_brushmodel_entities = (int *)Mem_Alloc(cl_mempool, cl_max_brushmodel_entities * sizeof(int));
189
190         cl_lastquakeentity = 0;
191         memset(cl_isquakeentity, 0, sizeof(cl_isquakeentity));
192
193         // LordHavoc: have to set up the baseline info for alpha and other stuff
194         for (i = 0;i < cl_max_entities;i++)
195         {
196                 cl_entities[i].state_baseline = defaultstate;
197                 cl_entities[i].state_previous = defaultstate;
198                 cl_entities[i].state_current = defaultstate;
199         }
200
201         for (i = 0;i < cl_max_csqcentities;i++)
202         {
203                 cl_csqcentities[i].state_baseline = defaultstate;       //[515]: csqc
204                 cl_csqcentities[i].state_previous = defaultstate;       //[515]: csqc
205                 cl_csqcentities[i].state_current = defaultstate;        //[515]: csqc
206                 cl_csqcentities[i].csqc = true;
207                 cl_csqcentities[i].state_current.number = -i;
208         }
209
210         if (gamemode == GAME_NEXUIZ)
211         {
212                 VectorSet(cl_playerstandmins, -16, -16, -24);
213                 VectorSet(cl_playerstandmaxs, 16, 16, 45);
214                 VectorSet(cl_playercrouchmins, -16, -16, -24);
215                 VectorSet(cl_playercrouchmaxs, 16, 16, 25);
216         }
217         else
218         {
219                 VectorSet(cl_playerstandmins, -16, -16, -24);
220                 VectorSet(cl_playerstandmaxs, 16, 16, 24);
221                 VectorSet(cl_playercrouchmins, -16, -16, -24);
222                 VectorSet(cl_playercrouchmaxs, 16, 16, 24);
223         }
224
225         // disable until we get textures for it
226         R_ResetSkyBox();
227
228         ent = &cl_entities[0];
229         // entire entity array was cleared, so just fill in a few fields
230         ent->state_current.active = true;
231         ent->render.model = cl.worldmodel = NULL; // no world model yet
232         ent->render.scale = 1; // some of the renderer still relies on scale
233         ent->render.alpha = 1;
234         ent->render.colormap = -1; // no special coloring
235         ent->render.flags = RENDER_SHADOW | RENDER_LIGHT;
236         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, 0, 0, 0, 0, 0, 0, 1);
237         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
238         CL_BoundingBoxForEntity(&ent->render);
239
240         // noclip is turned off at start
241         noclip_anglehack = false;
242
243         // mark all frames invalid for delta
244         memset(cl.qw_deltasequence, -1, sizeof(cl.qw_deltasequence));
245
246         CL_Screen_NewMap();
247         CL_Particles_Clear();
248 }
249
250 void CL_ExpandEntities(int num)
251 {
252         int i, oldmaxentities;
253         entity_t *oldentities;
254         if (num >= cl_max_entities)
255         {
256                 if (!cl_entities)
257                         Sys_Error("CL_ExpandEntities: cl_entities not initialized");
258                 if (num >= MAX_EDICTS)
259                         Host_Error("CL_ExpandEntities: num %i >= %i", num, MAX_EDICTS);
260                 oldmaxentities = cl_max_entities;
261                 oldentities = cl_entities;
262                 cl_max_entities = (num & ~255) + 256;
263                 cl_entities = (entity_t *)Mem_Alloc(cl_mempool, cl_max_entities * sizeof(entity_t));
264                 memcpy(cl_entities, oldentities, oldmaxentities * sizeof(entity_t));
265                 Mem_Free(oldentities);
266                 for (i = oldmaxentities;i < cl_max_entities;i++)
267                 {
268                         cl_entities[i].state_baseline = defaultstate;
269                         cl_entities[i].state_previous = defaultstate;
270                         cl_entities[i].state_current = defaultstate;
271                 }
272         }
273 }
274
275 void CL_ExpandCSQCEntities(int num)
276 {
277         int i, oldmaxentities;
278         entity_t *oldentities;
279         if (num >= cl_max_csqcentities)
280         {
281                 if (!cl_csqcentities)
282                         Sys_Error("CL_ExpandCSQCEntities: cl_csqcentities not initialized\n");
283                 if (num >= MAX_EDICTS)
284                         Host_Error("CL_ExpandCSQCEntities: num %i >= %i\n", num, MAX_EDICTS);
285                 oldmaxentities = cl_max_csqcentities;
286                 oldentities = cl_csqcentities;
287                 cl_max_csqcentities = (num & ~255) + 256;
288                 cl_csqcentities = Mem_Alloc(cl_mempool, cl_max_csqcentities * sizeof(entity_t));
289                 memcpy(cl_csqcentities, oldentities, oldmaxentities * sizeof(entity_t));
290                 Mem_Free(oldentities);
291                 for (i = oldmaxentities;i < cl_max_csqcentities;i++)
292                 {
293                         cl_csqcentities[i].state_baseline = defaultstate;
294                         cl_csqcentities[i].state_previous = defaultstate;
295                         cl_csqcentities[i].state_current = defaultstate;
296                         cl_csqcentities[i].csqc = true;
297                         cl_csqcentities[i].state_current.number = -i;
298                 }
299         }
300 }
301
302 void CL_VM_ShutDown (void);
303 /*
304 =====================
305 CL_Disconnect
306
307 Sends a disconnect message to the server
308 This is also called on Host_Error, so it shouldn't cause any errors
309 =====================
310 */
311 void CL_Disconnect(void)
312 {
313         if (cls.state == ca_dedicated)
314                 return;
315
316         Con_DPrintf("CL_Disconnect\n");
317
318         CL_VM_ShutDown();
319 // stop sounds (especially looping!)
320         S_StopAllSounds ();
321
322         // clear contents blends
323         cl.cshifts[0].percent = 0;
324         cl.cshifts[1].percent = 0;
325         cl.cshifts[2].percent = 0;
326         cl.cshifts[3].percent = 0;
327
328         cl.worldmodel = NULL;
329
330         if (cls.demoplayback)
331                 CL_StopPlayback();
332         else if (cls.netcon)
333         {
334                 sizebuf_t buf;
335                 unsigned char bufdata[8];
336                 if (cls.demorecording)
337                         CL_Stop_f();
338
339                 // send disconnect message 3 times to improve chances of server
340                 // receiving it (but it still fails sometimes)
341                 memset(&buf, 0, sizeof(buf));
342                 buf.data = bufdata;
343                 buf.maxsize = sizeof(bufdata);
344                 if (cls.protocol == PROTOCOL_QUAKEWORLD)
345                 {
346                         Con_DPrint("Sending drop command\n");
347                         MSG_WriteByte(&buf, qw_clc_stringcmd);
348                         MSG_WriteString(&buf, "drop");
349                 }
350                 else
351                 {
352                         Con_DPrint("Sending clc_disconnect\n");
353                         MSG_WriteByte(&buf, clc_disconnect);
354                 }
355                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
356                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
357                 NetConn_SendUnreliableMessage(cls.netcon, &buf, cls.protocol);
358                 NetConn_Close(cls.netcon);
359                 cls.netcon = NULL;
360         }
361         cls.state = ca_disconnected;
362
363         cls.demoplayback = cls.timedemo = false;
364         cls.signon = 0;
365 }
366
367 void CL_Disconnect_f(void)
368 {
369         CL_Disconnect ();
370         if (sv.active)
371                 Host_ShutdownServer ();
372 }
373
374
375
376
377 /*
378 =====================
379 CL_EstablishConnection
380
381 Host should be either "local" or a net address
382 =====================
383 */
384 void CL_EstablishConnection(const char *host)
385 {
386         if (cls.state == ca_dedicated)
387                 return;
388
389         // clear menu's connect error message
390         M_Update_Return_Reason("");
391         cls.demonum = -1;
392
393         // stop demo loop in case this fails
394         CL_Disconnect();
395
396         // make sure the client ports are open before attempting to connect
397         NetConn_UpdateSockets();
398
399         // run a network frame
400         //NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
401
402         if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
403         {
404                 cls.connect_trying = true;
405                 cls.connect_remainingtries = 3;
406                 cls.connect_nextsendtime = 0;
407                 M_Update_Return_Reason("Trying to connect...");
408                 // run several network frames to jump into the game quickly
409                 //if (sv.active)
410                 //{
411                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
412                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
413                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
414                 //      NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
415                 //}
416         }
417         else
418         {
419                 Con_Print("Unable to find a suitable network socket to connect to server.\n");
420                 M_Update_Return_Reason("No network");
421         }
422 }
423
424 /*
425 ==============
426 CL_PrintEntities_f
427 ==============
428 */
429 static void CL_PrintEntities_f(void)
430 {
431         entity_t *ent;
432         int i, j;
433         char name[32];
434
435         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
436         {
437                 if (!ent->state_current.active)
438                         continue;
439
440                 if (ent->render.model)
441                         strlcpy (name, ent->render.model->name, 25);
442                 else
443                         strcpy(name, "--no model--");
444                 for (j = (int)strlen(name);j < 25;j++)
445                         name[j] = ' ';
446                 Con_Printf("%3i: %s:%4i (%5i %5i %5i) [%3i %3i %3i] %4.2f %5.3f\n", i, name, ent->render.frame, (int) ent->render.matrix.m[0][3], (int) ent->render.matrix.m[1][3], (int) ent->render.matrix.m[2][3], (int) ent->render.angles[0] % 360, (int) ent->render.angles[1] % 360, (int) ent->render.angles[2] % 360, ent->render.scale, ent->render.alpha);
447         }
448 }
449
450 //static const vec3_t nomodelmins = {-16, -16, -16};
451 //static const vec3_t nomodelmaxs = {16, 16, 16};
452 void CL_BoundingBoxForEntity(entity_render_t *ent)
453 {
454         if (ent->model)
455         {
456                 //if (ent->angles[0] || ent->angles[2])
457                 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
458                 {
459                         // pitch or roll
460                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
461                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
462                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
463                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
464                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
465                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
466                         //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
467                         //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
468                 }
469                 //else if (ent->angles[1])
470                 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
471                 {
472                         // yaw
473                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
474                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
475                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
476                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
477                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
478                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
479                         //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
480                         //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
481                 }
482                 else
483                 {
484                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
485                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
486                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
487                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
488                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
489                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
490                         //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
491                         //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
492                 }
493         }
494         else
495         {
496                 ent->mins[0] = ent->matrix.m[0][3] - 16;
497                 ent->mins[1] = ent->matrix.m[1][3] - 16;
498                 ent->mins[2] = ent->matrix.m[2][3] - 16;
499                 ent->maxs[0] = ent->matrix.m[0][3] + 16;
500                 ent->maxs[1] = ent->matrix.m[1][3] + 16;
501                 ent->maxs[2] = ent->matrix.m[2][3] + 16;
502                 //VectorAdd(ent->origin, nomodelmins, ent->mins);
503                 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
504         }
505 }
506
507 /*
508 ===============
509 CL_LerpPoint
510
511 Determines the fraction between the last two messages that the objects
512 should be put at.
513 ===============
514 */
515 static float CL_LerpPoint(void)
516 {
517         float f;
518
519         // dropped packet, or start of demo
520         if (cl.mtime[1] < cl.mtime[0] - 0.1)
521                 cl.mtime[1] = cl.mtime[0] - 0.1;
522
523         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
524
525         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
526         f = cl.mtime[0] - cl.mtime[1];
527         if (!f || cl_nolerp.integer || cls.timedemo || (cl.islocalgame && !sv_fixedframeratesingleplayer.integer))
528         {
529                 cl.time = cl.mtime[0];
530                 return 1;
531         }
532
533         f = (cl.time - cl.mtime[1]) / f;
534         return bound(0, f, 1);
535 }
536
537 void CL_ClearTempEntities (void)
538 {
539         cl_num_temp_entities = 0;
540 }
541
542 entity_t *CL_NewTempEntity(void)
543 {
544         entity_t *ent;
545
546         if (r_refdef.numentities >= r_refdef.maxentities)
547                 return NULL;
548         if (cl_num_temp_entities >= cl_max_temp_entities)
549                 return NULL;
550         ent = &cl_temp_entities[cl_num_temp_entities++];
551         memset (ent, 0, sizeof(*ent));
552         r_refdef.entities[r_refdef.numentities++] = &ent->render;
553
554         ent->render.colormap = -1; // no special coloring
555         ent->render.scale = 1;
556         ent->render.alpha = 1;
557         VectorSet(ent->render.colormod, 1, 1, 1);
558         return ent;
559 }
560
561 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
562 {
563         int i;
564         cl_effect_t *e;
565         if (!modelindex) // sanity check
566                 return;
567         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
568         {
569                 if (e->active)
570                         continue;
571                 e->active = true;
572                 VectorCopy(org, e->origin);
573                 e->modelindex = modelindex;
574                 e->starttime = cl.time;
575                 e->startframe = startframe;
576                 e->endframe = startframe + framecount;
577                 e->framerate = framerate;
578
579                 e->frame = 0;
580                 e->frame1time = cl.time;
581                 e->frame2time = cl.time;
582                 cl_activeeffects = max(cl_activeeffects, i + 1);
583                 break;
584         }
585 }
586
587 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)
588 {
589         int i;
590         dlight_t *dl;
591
592         /*
593 // first look for an exact key match
594         if (ent)
595         {
596                 dl = cl_dlights;
597                 for (i = 0;i < cl_activedlights;i++, dl++)
598                         if (dl->ent == ent)
599                                 goto dlightsetup;
600         }
601         */
602
603 // then look for anything else
604         dl = cl_dlights;
605         for (i = 0;i < cl_activedlights;i++, dl++)
606                 if (!dl->radius)
607                         goto dlightsetup;
608         // if we hit the end of the active dlights and found no gaps, add a new one
609         if (i < MAX_DLIGHTS)
610         {
611                 cl_activedlights = i + 1;
612                 goto dlightsetup;
613         }
614
615         // unable to find one
616         return;
617
618 dlightsetup:
619         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
620         memset (dl, 0, sizeof(*dl));
621         Matrix4x4_Normalize(&dl->matrix, matrix);
622         dl->ent = ent;
623         dl->origin[0] = dl->matrix.m[0][3];
624         dl->origin[1] = dl->matrix.m[1][3];
625         dl->origin[2] = dl->matrix.m[2][3];
626         CL_FindNonSolidLocation(dl->origin, dl->origin, 6);
627         dl->matrix.m[0][3] = dl->origin[0];
628         dl->matrix.m[1][3] = dl->origin[1];
629         dl->matrix.m[2][3] = dl->origin[2];
630         dl->radius = radius;
631         dl->color[0] = red;
632         dl->color[1] = green;
633         dl->color[2] = blue;
634         dl->decay = decay;
635         if (lifetime)
636                 dl->die = cl.time + lifetime;
637         else
638                 dl->die = 0;
639         dl->cubemapnum = cubemapnum;
640         dl->style = style;
641         dl->shadow = shadowenable;
642         dl->corona = corona;
643         dl->flags = flags;
644         dl->coronasizescale = coronasizescale;
645         dl->ambientscale = ambientscale;
646         dl->diffusescale = diffusescale;
647         dl->specularscale = specularscale;
648 }
649
650 // called before entity relinking
651 void CL_DecayLights(void)
652 {
653         int i, oldmax;
654         dlight_t *dl;
655         float time, f;
656
657         time = cl.time - cl.oldtime;
658         oldmax = cl_activedlights;
659         cl_activedlights = 0;
660         for (i = 0, dl = cl_dlights;i < oldmax;i++, dl++)
661         {
662                 if (dl->radius)
663                 {
664                         f = dl->radius - time * dl->decay;
665                         if (cl.time < dl->die && f > 0)
666                         {
667                                 dl->radius = dl->radius - time * dl->decay;
668                                 cl_activedlights = i + 1;
669                         }
670                         else
671                                 dl->radius = 0;
672                 }
673         }
674 }
675
676 // called after entity relinking
677 void CL_UpdateLights(void)
678 {
679         int i, j, k, l;
680         dlight_t *dl;
681         float frac, f;
682
683         r_refdef.numlights = 0;
684         if (r_dynamic.integer)
685         {
686                 for (i = 0, dl = cl_dlights;i < cl_activedlights;i++, dl++)
687                 {
688                         if (dl->radius)
689                         {
690                                 R_RTLight_Update(dl, false);
691                                 r_refdef.lights[r_refdef.numlights++] = dl;
692                         }
693                 }
694         }
695
696 // light animations
697 // 'm' is normal light, 'a' is no light, 'z' is double bright
698         f = cl.time * 10;
699         i = (int)floor(f);
700         frac = f - i;
701         for (j = 0;j < cl_max_lightstyle;j++)
702         {
703                 if (!cl_lightstyle || !cl_lightstyle[j].length)
704                 {
705                         r_refdef.lightstylevalue[j] = 256;
706                         continue;
707                 }
708                 k = i % cl_lightstyle[j].length;
709                 l = (i-1) % cl_lightstyle[j].length;
710                 k = cl_lightstyle[j].map[k] - 'a';
711                 l = cl_lightstyle[j].map[l] - 'a';
712                 r_refdef.lightstylevalue[j] = ((k*frac)+(l*(1-frac)))*22;
713         }
714 }
715
716 void CL_AddQWCTFFlagModel(entity_t *player, int skin)
717 {
718         float f;
719         entity_t *flag;
720         matrix4x4_t flagmatrix;
721
722         // this code taken from QuakeWorld
723         f = 14;
724         if (player->render.frame2 >= 29 && player->render.frame2 <= 40)
725         {
726                 if (player->render.frame2 >= 29 && player->render.frame2 <= 34)
727                 { //axpain
728                         if      (player->render.frame2 == 29) f = f + 2;
729                         else if (player->render.frame2 == 30) f = f + 8;
730                         else if (player->render.frame2 == 31) f = f + 12;
731                         else if (player->render.frame2 == 32) f = f + 11;
732                         else if (player->render.frame2 == 33) f = f + 10;
733                         else if (player->render.frame2 == 34) f = f + 4;
734                 }
735                 else if (player->render.frame2 >= 35 && player->render.frame2 <= 40)
736                 { // pain
737                         if      (player->render.frame2 == 35) f = f + 2;
738                         else if (player->render.frame2 == 36) f = f + 10;
739                         else if (player->render.frame2 == 37) f = f + 10;
740                         else if (player->render.frame2 == 38) f = f + 8;
741                         else if (player->render.frame2 == 39) f = f + 4;
742                         else if (player->render.frame2 == 40) f = f + 2;
743                 }
744         }
745         else if (player->render.frame2 >= 103 && player->render.frame2 <= 118)
746         {
747                 if      (player->render.frame2 >= 103 && player->render.frame2 <= 104) f = f + 6;  //nailattack
748                 else if (player->render.frame2 >= 105 && player->render.frame2 <= 106) f = f + 6;  //light
749                 else if (player->render.frame2 >= 107 && player->render.frame2 <= 112) f = f + 7;  //rocketattack
750                 else if (player->render.frame2 >= 112 && player->render.frame2 <= 118) f = f + 7;  //shotattack
751         }
752         // end of code taken from QuakeWorld
753
754         flag = CL_NewTempEntity();
755         if (!flag)
756                 return;
757
758         flag->render.model = cl.model_precache[cl.qw_modelindex_flag];
759         flag->render.skinnum = skin;
760         flag->render.colormap = -1; // no special coloring
761         flag->render.alpha = 1;
762         VectorSet(flag->render.colormod, 1, 1, 1);
763         // attach the flag to the player matrix
764         Matrix4x4_CreateFromQuakeEntity(&flagmatrix, -f, -22, 0, 0, 0, -45, 1);
765         Matrix4x4_Concat(&flag->render.matrix, &player->render.matrix, &flagmatrix);
766         Matrix4x4_Invert_Simple(&flag->render.inversematrix, &flag->render.matrix);
767         R_LerpAnimation(&flag->render);
768         CL_BoundingBoxForEntity(&flag->render);
769 }
770
771 #define MAXVIEWMODELS 32
772 entity_t *viewmodels[MAXVIEWMODELS];
773 int numviewmodels;
774
775 matrix4x4_t viewmodelmatrix;
776
777 static int entitylinkframenumber;
778
779 static const vec3_t muzzleflashorigin = {18, 0, 0};
780
781 extern void V_DriftPitch(void);
782 extern void V_FadeViewFlashs(void);
783 extern void V_CalcViewBlend(void);
784
785 extern void V_CalcRefdef(void);
786 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
787 void CL_LinkNetworkEntity(entity_t *e)
788 {
789         matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2;
790         //matrix4x4_t dlightmatrix;
791         int j, k, l, trailtype, temp;
792         float origin[3], angles[3], delta[3], lerp, dlightcolor[3], dlightradius, mins[3], maxs[3], v2[3], d;
793         entity_t *t;
794         model_t *model;
795         trace_t trace;
796         //entity_persistent_t *p = &e->persistent;
797         //entity_render_t *r = &e->render;
798         if (e->persistent.linkframe != entitylinkframenumber)
799         {
800                 e->persistent.linkframe = entitylinkframenumber;
801                 // skip inactive entities and world
802                 if (!e->state_current.active || e == cl_entities || e == cl_csqcentities)
803                         return;
804                 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
805                 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
806                 e->render.flags = e->state_current.flags;
807                 e->render.effects = e->state_current.effects;
808                 VectorScale(e->state_current.colormod, (1.0f / 32.0f), e->render.colormod);
809                 if (e->state_current.flags & RENDER_COLORMAPPED)
810                 {
811                         int cb;
812                         unsigned char *cbcolor;
813                         e->render.colormap = e->state_current.colormap;
814                         cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
815                         cbcolor = (unsigned char *) (&palette_complete[cb]);
816                         e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
817                         e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
818                         e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
819                         cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
820                         cbcolor = (unsigned char *) (&palette_complete[cb]);
821                         e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
822                         e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
823                         e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
824                 }
825                 else if (e->state_current.colormap && cl.scores != NULL)
826                 {
827                         int cb;
828                         unsigned char *cbcolor;
829                         e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
830                         cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
831                         cbcolor = (unsigned char *) (&palette_complete[cb]);
832                         e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
833                         e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
834                         e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
835                         cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
836                         cbcolor = (unsigned char *) (&palette_complete[cb]);
837                         e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
838                         e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
839                         e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
840                 }
841                 else
842                 {
843                         e->render.colormap = -1; // no special coloring
844                         VectorClear(e->render.colormap_pantscolor);
845                         VectorClear(e->render.colormap_shirtcolor);
846                 }
847                 e->render.skinnum = e->state_current.skin;
848                 if (e->render.flags & RENDER_VIEWMODEL && !e->state_current.tagentity)
849                 {
850                         if (!r_drawviewmodel.integer || chase_active.integer || envmap)// || csqc_loaded)
851                                 return;
852                         if (!e->csqc)
853                         {
854                                 if (cl.viewentity)
855                                         CL_LinkNetworkEntity(cl_entities + cl.viewentity);
856                                 if (e == &cl.viewent && cl_entities[cl.viewentity].state_current.active)
857                                 {
858                                         e->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
859                                         e->state_current.effects = EF_NOSHADOW | (cl_entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT | EF_NODEPTHTEST));
860                                 }
861                         }
862                         matrix = &viewmodelmatrix;
863                 }
864                 else
865                 {
866                         // if the tag entity is currently impossible, skip it
867                         if (!e->csqc)
868                         {
869                                 if (e->state_current.tagentity >= cl_num_entities)
870                                         return;
871                                 t = cl_entities + e->state_current.tagentity;
872                         }
873                         else
874                         {
875                                 if (e->state_current.tagentity >= cl_num_csqcentities)
876                                         return;
877                                 t = cl_csqcentities + e->state_current.tagentity;
878                         }
879                         // if the tag entity is inactive, skip it
880                         if (!t->state_current.active)
881                                 return;
882                         // note: this can link to world
883                         CL_LinkNetworkEntity(t);
884                         // make relative to the entity
885                         matrix = &t->render.matrix;
886                         // some properties of the tag entity carry over
887                         e->render.flags |= t->render.flags & (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL);
888                         // if a valid tagindex is used, make it relative to that tag instead
889                         // FIXME: use a model function to get tag info (need to handle skeletal)
890                         if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model))
891                         {
892                                 // blend the matrices
893                                 memset(&blendmatrix, 0, sizeof(blendmatrix));
894                                 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
895                                 {
896                                         matrix4x4_t tagmatrix;
897                                         Mod_Alias_GetTagMatrix(model, t->render.frameblend[j].frame, e->state_current.tagindex - 1, &tagmatrix);
898                                         d = t->render.frameblend[j].lerp;
899                                         for (l = 0;l < 4;l++)
900                                                 for (k = 0;k < 4;k++)
901                                                         blendmatrix.m[l][k] += d * tagmatrix.m[l][k];
902                                 }
903                                 // concat the tag matrices onto the entity matrix
904                                 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
905                                 // use the constructed tag matrix
906                                 matrix = &tempmatrix;
907                         }
908                 }
909
910                 // movement lerp
911                 // if it's the player entity, update according to client movement
912                 if (e == cl_entities + cl.playerentity && cl.movement)// && !e->csqc)
913                 {
914                         lerp = (cl.time - cl.mtime[1]) / (cl.mtime[0] - cl.mtime[1]);
915                         lerp = bound(0, lerp, 1);
916                         VectorLerp(cl.movement_oldorigin, lerp, cl.movement_origin, origin);
917                         VectorSet(angles, 0, cl.viewangles[1], 0);
918                 }
919                 else if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
920                 {
921                         // interpolate the origin and angles
922                         VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
923                         VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
924                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
925                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
926                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
927                         VectorMA(e->persistent.oldangles, lerp, delta, angles);
928                 }
929                 else
930                 {
931                         // no interpolation
932                         VectorCopy(e->persistent.neworigin, origin);
933                         VectorCopy(e->persistent.newangles, angles);
934                 }
935
936                 // model setup and some modelflags
937                 if(e->state_current.modelindex < MAX_MODELS)
938                         e->render.model = cl.model_precache[e->state_current.modelindex];
939                 else
940                         e->render.model = cl.csqc_model_precache[65536-e->state_current.modelindex];
941                 if (e->render.model)
942                 {
943                         // if model is alias or this is a tenebrae-like dlight, reverse pitch direction
944                         if (e->render.model->type == mod_alias || (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC))
945                                 angles[0] = -angles[0];
946                         if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
947                         {
948                                 angles[1] = ANGLEMOD(100*cl.time);
949                                 if (cl_itembobheight.value)
950                                         origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
951                         }
952                         // transfer certain model flags to effects
953                         e->render.effects |= e->render.model->flags2 & (EF_FULLBRIGHT | EF_ADDITIVE);
954                         if (cl_prydoncursor.integer && (e->render.effects & EF_SELECTABLE) && cl.cmd.cursor_entitynumber == e->state_current.number)
955                                 VectorScale(e->render.colormod, 2, e->render.colormod);
956                 }
957
958                 // animation lerp
959                 if (e->render.frame2 == e->state_current.frame)
960                 {
961                         // update frame lerp fraction
962                         e->render.framelerp = 1;
963                         if (e->render.frame2time > e->render.frame1time)
964                         {
965                                 // make sure frame lerp won't last longer than 100ms
966                                 // (this mainly helps with models that use framegroups and
967                                 // switch between them infrequently)
968                                 e->render.framelerp = (cl.time - e->render.frame2time) / min(e->render.frame2time - e->render.frame1time, 0.1);
969                                 e->render.framelerp = bound(0, e->render.framelerp, 1);
970                         }
971                 }
972                 else
973                 {
974                         // begin a new frame lerp
975                         e->render.frame1 = e->render.frame2;
976                         e->render.frame1time = e->render.frame2time;
977                         e->render.frame = e->render.frame2 = e->state_current.frame;
978                         e->render.frame2time = cl.time;
979                         e->render.framelerp = 0;
980                 }
981                 R_LerpAnimation(&e->render);
982
983                 // set up the render matrix
984                 // FIXME: e->render.scale should go away
985                 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
986                 // concat the matrices to make the entity relative to its tag
987                 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
988                 // make the other useful stuff
989                 Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
990                 CL_BoundingBoxForEntity(&e->render);
991
992                 // handle effects now that we know where this entity is in the world...
993                 if (e->render.model && e->render.model->soundfromcenter)
994                 {
995                         // bmodels are treated specially since their origin is usually '0 0 0'
996                         vec3_t o;
997                         VectorMAM(0.5f, e->render.model->normalmins, 0.5f, e->render.model->normalmaxs, o);
998                         Matrix4x4_Transform(&e->render.matrix, o, origin);
999                 }
1000                 else
1001                 {
1002                         origin[0] = e->render.matrix.m[0][3];
1003                         origin[1] = e->render.matrix.m[1][3];
1004                         origin[2] = e->render.matrix.m[2][3];
1005                 }
1006                 trailtype = -1;
1007                 dlightradius = 0;
1008                 dlightcolor[0] = 0;
1009                 dlightcolor[1] = 0;
1010                 dlightcolor[2] = 0;
1011                 // LordHavoc: if the entity has no effects, don't check each
1012                 if (e->render.effects)
1013                 {
1014                         if (e->render.effects & EF_BRIGHTFIELD)
1015                         {
1016                                 if (gamemode == GAME_NEXUIZ)
1017                                 {
1018                                         dlightradius = max(dlightradius, 200);
1019                                         dlightcolor[0] += 0.75f;
1020                                         dlightcolor[1] += 1.50f;
1021                                         dlightcolor[2] += 3.00f;
1022                                         trailtype = 8;
1023                                 }
1024                                 else
1025                                         CL_EntityParticles(e);
1026                         }
1027                         if (e->render.effects & EF_MUZZLEFLASH)
1028                                 e->persistent.muzzleflash = 1.0f;
1029                         if (e->render.effects & EF_DIMLIGHT)
1030                         {
1031                                 dlightradius = max(dlightradius, 200);
1032                                 dlightcolor[0] += 1.50f;
1033                                 dlightcolor[1] += 1.50f;
1034                                 dlightcolor[2] += 1.50f;
1035                         }
1036                         if (e->render.effects & EF_BRIGHTLIGHT)
1037                         {
1038                                 dlightradius = max(dlightradius, 400);
1039                                 dlightcolor[0] += 3.00f;
1040                                 dlightcolor[1] += 3.00f;
1041                                 dlightcolor[2] += 3.00f;
1042                         }
1043                         // LordHavoc: more effects
1044                         if (e->render.effects & EF_RED) // red
1045                         {
1046                                 dlightradius = max(dlightradius, 200);
1047                                 dlightcolor[0] += 1.50f;
1048                                 dlightcolor[1] += 0.15f;
1049                                 dlightcolor[2] += 0.15f;
1050                         }
1051                         if (e->render.effects & EF_BLUE) // blue
1052                         {
1053                                 dlightradius = max(dlightradius, 200);
1054                                 dlightcolor[0] += 0.15f;
1055                                 dlightcolor[1] += 0.15f;
1056                                 dlightcolor[2] += 1.50f;
1057                         }
1058                         if (e->render.effects & EF_FLAME)
1059                         {
1060                                 mins[0] = origin[0] - 16.0f;
1061                                 mins[1] = origin[1] - 16.0f;
1062                                 mins[2] = origin[2] - 16.0f;
1063                                 maxs[0] = origin[0] + 16.0f;
1064                                 maxs[1] = origin[1] + 16.0f;
1065                                 maxs[2] = origin[2] + 16.0f;
1066                                 // how many flames to make
1067                                 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
1068                                 CL_FlameCube(mins, maxs, temp);
1069                                 d = lhrandom(0.75f, 1);
1070                                 dlightradius = max(dlightradius, 200);
1071                                 dlightcolor[0] += d * 2.0f;
1072                                 dlightcolor[1] += d * 1.5f;
1073                                 dlightcolor[2] += d * 0.5f;
1074                         }
1075                         if (e->render.effects & EF_STARDUST)
1076                         {
1077                                 mins[0] = origin[0] - 16.0f;
1078                                 mins[1] = origin[1] - 16.0f;
1079                                 mins[2] = origin[2] - 16.0f;
1080                                 maxs[0] = origin[0] + 16.0f;
1081                                 maxs[1] = origin[1] + 16.0f;
1082                                 maxs[2] = origin[2] + 16.0f;
1083                                 // how many particles to make
1084                                 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
1085                                 CL_Stardust(mins, maxs, temp);
1086                                 dlightradius = max(dlightradius, 200);
1087                                 dlightcolor[0] += 1.0f;
1088                                 dlightcolor[1] += 0.7f;
1089                                 dlightcolor[2] += 0.3f;
1090                         }
1091                         if (e->render.effects & (EF_FLAG1QW | EF_FLAG2QW))
1092                         {
1093                                 // these are only set on player entities
1094                                 CL_AddQWCTFFlagModel(e, (e->render.effects & EF_FLAG2QW) != 0);
1095                         }
1096                 }
1097                 // muzzleflash fades over time, and is offset a bit
1098                 if (e->persistent.muzzleflash > 0)
1099                 {
1100                         Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
1101                         trace = CL_TraceBox(origin, vec3_origin, vec3_origin, v2, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY, false);
1102                         tempmatrix = e->render.matrix;
1103                         tempmatrix.m[0][3] = trace.endpos[0];
1104                         tempmatrix.m[1][3] = trace.endpos[1];
1105                         tempmatrix.m[2][3] = trace.endpos[2];
1106                         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);
1107                         e->persistent.muzzleflash -= cl.frametime * 10;
1108                 }
1109                 // LordHavoc: if the model has no flags, don't check each
1110                 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
1111                 {
1112                         if (e->render.model->flags & EF_GIB)
1113                                 trailtype = 2;
1114                         else if (e->render.model->flags & EF_ZOMGIB)
1115                                 trailtype = 4;
1116                         else if (e->render.model->flags & EF_TRACER)
1117                         {
1118                                 trailtype = 3;
1119                                 //dlightradius = max(dlightradius, 100);
1120                                 //dlightcolor[0] += 0.25f;
1121                                 //dlightcolor[1] += 1.00f;
1122                                 //dlightcolor[2] += 0.25f;
1123                         }
1124                         else if (e->render.model->flags & EF_TRACER2)
1125                         {
1126                                 trailtype = 5;
1127                                 //dlightradius = max(dlightradius, 100);
1128                                 //dlightcolor[0] += 1.00f;
1129                                 //dlightcolor[1] += 0.60f;
1130                                 //dlightcolor[2] += 0.20f;
1131                         }
1132                         else if (e->render.model->flags & EF_ROCKET)
1133                         {
1134                                 trailtype = 0;
1135                                 dlightradius = max(dlightradius, 200);
1136                                 dlightcolor[0] += 3.00f;
1137                                 dlightcolor[1] += 1.50f;
1138                                 dlightcolor[2] += 0.50f;
1139                         }
1140                         else if (e->render.model->flags & EF_GRENADE)
1141                         {
1142                                 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
1143                                 trailtype = e->render.alpha == -1 ? 7 : 1;
1144                         }
1145                         else if (e->render.model->flags & EF_TRACER3)
1146                         {
1147                                 trailtype = 6;
1148                                 if (gamemode == GAME_PRYDON)
1149                                 {
1150                                         dlightradius = max(dlightradius, 100);
1151                                         dlightcolor[0] += 0.30f;
1152                                         dlightcolor[1] += 0.60f;
1153                                         dlightcolor[2] += 1.20f;
1154                                 }
1155                                 else
1156                                 {
1157                                         dlightradius = max(dlightradius, 200);
1158                                         dlightcolor[0] += 1.20f;
1159                                         dlightcolor[1] += 0.50f;
1160                                         dlightcolor[2] += 1.00f;
1161                                 }
1162                         }
1163                 }
1164                 // LordHavoc: customizable glow
1165                 if (e->state_current.glowsize)
1166                 {
1167                         // * 4 for the expansion from 0-255 to 0-1023 range,
1168                         // / 255 to scale down byte colors
1169                         dlightradius = max(dlightradius, e->state_current.glowsize * 4);
1170                         VectorMA(dlightcolor, (1.0f / 255.0f), (unsigned char *)&palette_complete[e->state_current.glowcolor], dlightcolor);
1171                 }
1172                 // make the glow dlight
1173                 if (dlightradius > 0 && (dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL))
1174                 {
1175                         //dlightmatrix = e->render.matrix;
1176                         // hack to make glowing player light shine on their gun
1177                         //if (e->state_current.number == cl.viewentity/* && !chase_active.integer*/)
1178                         //      dlightmatrix.m[2][3] += 30;
1179                         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);
1180                 }
1181                 // custom rtlight
1182                 if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
1183                 {
1184                         float light[4];
1185                         VectorScale(e->state_current.light, (1.0f / 256.0f), light);
1186                         light[3] = e->state_current.light[3];
1187                         if (light[0] == 0 && light[1] == 0 && light[2] == 0)
1188                                 VectorSet(light, 1, 1, 1);
1189                         if (light[3] == 0)
1190                                 light[3] = 350;
1191                         // FIXME: add ambient/diffuse/specular scales as an extension ontop of TENEBRAE_GFX_DLIGHTS?
1192                         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);
1193                 }
1194                 // do trails
1195                 if (e->render.flags & RENDER_GLOWTRAIL)
1196                         trailtype = 9;
1197                 if (trailtype >= 0)
1198                         CL_RocketTrail(e->persistent.trail_origin, origin, trailtype, e->state_current.glowcolor, e);
1199                 VectorCopy(origin, e->persistent.trail_origin);
1200                 // tenebrae's sprites are all additive mode (weird)
1201                 if (gamemode == GAME_TENEBRAE && e->render.model && e->render.model->type == mod_sprite)
1202                         e->render.effects |= EF_ADDITIVE;
1203                 // player model is only shown with chase_active on
1204                 if (!e->csqc)
1205                 if (e->state_current.number == cl.viewentity)
1206                         e->render.flags |= RENDER_EXTERIORMODEL;
1207                 // transparent stuff can't be lit during the opaque stage
1208                 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1209                         e->render.flags |= RENDER_TRANSPARENT;
1210                 // double sided rendering mode causes backfaces to be visible
1211                 // (mostly useful on transparent stuff)
1212                 if (e->render.effects & EF_DOUBLESIDED)
1213                         e->render.flags |= RENDER_NOCULLFACE;
1214                 // either fullbright or lit
1215                 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1216                         e->render.flags |= RENDER_LIGHT;
1217                 // hide player shadow during intermission or nehahra movie
1218                 if (!(e->render.effects & EF_NOSHADOW)
1219                  && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
1220                  && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cls.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
1221                         e->render.flags |= RENDER_SHADOW;
1222                 // as soon as player is known we can call V_CalcRefDef
1223                 if (!csqc_loaded)
1224                 if (e->state_current.number == cl.viewentity)
1225                         V_CalcRefdef();
1226                 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
1227                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = e->state_current.number;
1228                 // don't show entities with no modelindex (note: this still shows
1229                 // entities which have a modelindex that resolved to a NULL model)
1230                 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
1231                         r_refdef.entities[r_refdef.numentities++] = &e->render;
1232                 //if (cl.viewentity && e->state_current.number == cl.viewentity)
1233                 //      Matrix4x4_Print(&e->render.matrix);
1234         }
1235 }
1236
1237 void CL_RelinkWorld(void)
1238 {
1239         entity_t *ent = &cl_entities[0];
1240         cl_brushmodel_entities[cl_num_brushmodel_entities++] = 0;
1241         // FIXME: this should be done at load
1242         ent->render.matrix = identitymatrix;
1243         ent->render.inversematrix = identitymatrix;
1244         R_LerpAnimation(&ent->render);
1245         CL_BoundingBoxForEntity(&ent->render);
1246         ent->render.flags = RENDER_SHADOW;
1247         if (!r_fullbright.integer)
1248                 ent->render.flags |= RENDER_LIGHT;
1249         VectorSet(ent->render.colormod, 1, 1, 1);
1250         r_refdef.worldentity = &ent->render;
1251         r_refdef.worldmodel = cl.worldmodel;
1252 }
1253
1254 void CL_RelinkCSQCWorld(void)   //[515]: csqc
1255 {
1256         entity_t *ent = &cl_csqcentities[0];
1257         if(!csqc_loaded)
1258                 return;
1259 //      cl_brushmodel_entities[cl_num_brushmodel_entities++] = 0;
1260         // FIXME: this should be done at load
1261         ent->render.matrix = identitymatrix;
1262         ent->render.inversematrix = identitymatrix;
1263         R_LerpAnimation(&ent->render);
1264         CL_BoundingBoxForEntity(&ent->render);
1265         ent->render.flags = RENDER_SHADOW;
1266         if (!r_fullbright.integer)
1267                 ent->render.flags |= RENDER_LIGHT;
1268         VectorSet(ent->render.colormod, 1, 1, 1);
1269 //      r_refdef.worldentity = &ent->render;
1270 //      r_refdef.worldmodel = cl.worldmodel;
1271 }
1272
1273 static void CL_RelinkStaticEntities(void)
1274 {
1275         int i;
1276         entity_t *e;
1277         for (i = 0, e = cl_static_entities;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++, e++)
1278         {
1279                 e->render.flags = 0;
1280                 // transparent stuff can't be lit during the opaque stage
1281                 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1282                         e->render.flags |= RENDER_TRANSPARENT;
1283                 // either fullbright or lit
1284                 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1285                         e->render.flags |= RENDER_LIGHT;
1286                 // hide player shadow during intermission or nehahra movie
1287                 if (!(e->render.effects & EF_NOSHADOW) && !(e->render.flags & RENDER_TRANSPARENT))
1288                         e->render.flags |= RENDER_SHADOW;
1289                 VectorSet(e->render.colormod, 1, 1, 1);
1290                 R_LerpAnimation(&e->render);
1291                 r_refdef.entities[r_refdef.numentities++] = &e->render;
1292         }
1293 }
1294
1295 /*
1296 ===============
1297 CL_RelinkEntities
1298 ===============
1299 */
1300 static void CL_RelinkNetworkEntities(int drawmask)
1301 {
1302         entity_t *ent;
1303         int i, k;
1304
1305         if(!csqc_loaded)
1306         {
1307                 ent = &cl.viewent;
1308                 ent->state_previous = ent->state_current;
1309                 ent->state_current = defaultstate;
1310                 ent->state_current.time = cl.time;
1311                 ent->state_current.number = -1;
1312                 ent->state_current.active = true;
1313                 ent->state_current.modelindex = cl.stats[STAT_WEAPON];
1314                 ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
1315                 ent->state_current.flags = RENDER_VIEWMODEL;
1316                 if ((cl.stats[STAT_HEALTH] <= 0 && cl_deathnoviewmodel.integer) || cl.intermission)
1317                         ent->state_current.modelindex = 0;
1318                 else if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1319                 {
1320                         if (gamemode == GAME_TRANSFUSION)
1321                                 ent->state_current.alpha = 128;
1322                         else
1323                                 ent->state_current.modelindex = 0;
1324                 }
1325
1326                 // reset animation interpolation on weaponmodel if model changed
1327                 if (ent->state_previous.modelindex != ent->state_current.modelindex)
1328                 {
1329                         ent->render.frame = ent->render.frame1 = ent->render.frame2 = ent->state_current.frame;
1330                         ent->render.frame1time = ent->render.frame2time = cl.time;
1331                         ent->render.framelerp = 1;
1332                 }
1333         }
1334
1335         // start on the entity after the world
1336         entitylinkframenumber++;
1337         if(drawmask & ENTMASK_ENGINE || !csqc_loaded)
1338         {
1339                 for (i = 1;i < cl_num_entities;i++)
1340                 {
1341                         if (cl_entities_active[i])
1342                         {
1343                                 ent = cl_entities + i;
1344                                 if (!(drawmask & ENTMASK_ENGINEVIEWMODELS))
1345                                 if (ent->state_current.flags & RENDER_VIEWMODEL)        //[515]: csqc drawmask
1346                                 {
1347                                         cl_entities_active[i] = false;
1348                                         continue;
1349                                 }
1350                                 if (ent->state_current.active)
1351                                         CL_LinkNetworkEntity(ent);
1352                                 else
1353                                         cl_entities_active[i] = false;
1354                         }
1355                 }
1356         }
1357
1358         //[515]: csqc
1359         if(csqc_loaded)
1360         {
1361                 for (i=1,k=cl_num_csqcentities;k;i++)
1362                 {
1363                         if (cl_csqcentities_active[i])
1364                         {
1365                                 --k;
1366                                 ent = cl_csqcentities + i;
1367                                 if (ent->state_current.active)
1368                                         CL_LinkNetworkEntity(ent);
1369                                 else
1370                                         cl_csqcentities_active[i] = false;
1371                         }
1372                 }
1373         }
1374         else
1375                 CL_LinkNetworkEntity(&cl.viewent);
1376 }
1377
1378 static void CL_RelinkEffects(void)
1379 {
1380         int i, intframe;
1381         cl_effect_t *e;
1382         entity_t *ent;
1383         float frame;
1384
1385         for (i = 0, e = cl_effects;i < cl_activeeffects;i++, e++)
1386         {
1387                 if (e->active)
1388                 {
1389                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
1390                         intframe = frame;
1391                         if (intframe < 0 || intframe >= e->endframe)
1392                         {
1393                                 memset(e, 0, sizeof(*e));
1394                                 while (cl_activeeffects > 0 && !cl_effects[cl_activeeffects - 1].active)
1395                                         cl_activeeffects--;
1396                                 continue;
1397                         }
1398
1399                         if (intframe != e->frame)
1400                         {
1401                                 e->frame = intframe;
1402                                 e->frame1time = e->frame2time;
1403                                 e->frame2time = cl.time;
1404                         }
1405
1406                         // if we're drawing effects, get a new temp entity
1407                         // (NewTempEntity adds it to the render entities list for us)
1408                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
1409                         {
1410                                 // interpolation stuff
1411                                 ent->render.frame1 = intframe;
1412                                 ent->render.frame2 = intframe + 1;
1413                                 if (ent->render.frame2 >= e->endframe)
1414                                         ent->render.frame2 = -1; // disappear
1415                                 ent->render.framelerp = frame - intframe;
1416                                 ent->render.frame1time = e->frame1time;
1417                                 ent->render.frame2time = e->frame2time;
1418
1419                                 // normal stuff
1420                                 if(e->modelindex < MAX_MODELS)
1421                                         ent->render.model = cl.model_precache[e->modelindex];
1422                                 else
1423                                         ent->render.model = cl.csqc_model_precache[65536-e->modelindex];
1424                                 ent->render.frame = ent->render.frame2;
1425                                 ent->render.colormap = -1; // no special coloring
1426                                 ent->render.alpha = 1;
1427                                 VectorSet(ent->render.colormod, 1, 1, 1);
1428
1429                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
1430                                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1431                                 R_LerpAnimation(&ent->render);
1432                                 CL_BoundingBoxForEntity(&ent->render);
1433                         }
1434                 }
1435         }
1436 }
1437
1438 void CL_RelinkBeams(void)
1439 {
1440         int i;
1441         beam_t *b;
1442         vec3_t dist, org;
1443         float d;
1444         entity_t *ent;
1445         float yaw, pitch;
1446         float forward;
1447         matrix4x4_t tempmatrix;
1448
1449         for (i = 0, b = cl_beams;i < cl_activebeams;i++, b++)
1450         {
1451                 if (!b->model)
1452                         continue;
1453                 if (b->endtime < cl.time)
1454                 {
1455                         b->model = NULL;
1456                         continue;
1457                 }
1458
1459                 // if coming from the player, update the start position
1460                 //if (b->entity == cl.viewentity)
1461                 //      Matrix4x4_OriginFromMatrix(&cl_entities[cl.viewentity].render.matrix, b->start);
1462                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
1463                 {
1464                         entity_render_t *r = &cl_entities[b->entity].render;
1465                         //Matrix4x4_OriginFromMatrix(&r->matrix, origin);
1466                         //Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
1467                         Matrix4x4_Transform(&r->matrix, b->relativestart, b->start);
1468                         Matrix4x4_Transform(&r->matrix, b->relativeend, b->end);
1469                 }
1470
1471                 if (b->lightning)
1472                 {
1473                         if (cl_beams_lightatend.integer)
1474                         {
1475                                 // FIXME: create a matrix from the beam start/end orientation
1476                                 Matrix4x4_CreateTranslate(&tempmatrix, b->end[0], b->end[1], b->end[2]);
1477                                 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);
1478                         }
1479                         if (cl_beams_polygons.integer)
1480                                 continue;
1481                 }
1482
1483                 // calculate pitch and yaw
1484                 VectorSubtract (b->end, b->start, dist);
1485
1486                 if (dist[1] == 0 && dist[0] == 0)
1487                 {
1488                         yaw = 0;
1489                         if (dist[2] > 0)
1490                                 pitch = 90;
1491                         else
1492                                 pitch = 270;
1493                 }
1494                 else
1495                 {
1496                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1497                         if (yaw < 0)
1498                                 yaw += 360;
1499
1500                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1501                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1502                         if (pitch < 0)
1503                                 pitch += 360;
1504                 }
1505
1506                 // add new entities for the lightning
1507                 VectorCopy (b->start, org);
1508                 d = VectorNormalizeLength(dist);
1509                 while (d > 0)
1510                 {
1511                         ent = CL_NewTempEntity ();
1512                         if (!ent)
1513                                 return;
1514                         //VectorCopy (org, ent->render.origin);
1515                         ent->render.model = b->model;
1516                         //ent->render.effects = EF_FULLBRIGHT;
1517                         //ent->render.angles[0] = pitch;
1518                         //ent->render.angles[1] = yaw;
1519                         //ent->render.angles[2] = rand()%360;
1520                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1521                         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1522                         R_LerpAnimation(&ent->render);
1523                         CL_BoundingBoxForEntity(&ent->render);
1524                         VectorMA(org, 30, dist, org);
1525                         d -= 30;
1526                 }
1527         }
1528
1529         while (cl_activebeams > 0 && !cl_beams[cl_activebeams - 1].model)
1530                 cl_activebeams--;
1531 }
1532
1533 static void CL_RelinkQWNails(void)
1534 {
1535         int i;
1536         vec_t *v;
1537         entity_t *ent;
1538
1539         for (i = 0;i < cl.qw_num_nails;i++)
1540         {
1541                 v = cl.qw_nails[i];
1542
1543                 // if we're drawing effects, get a new temp entity
1544                 // (NewTempEntity adds it to the render entities list for us)
1545                 if (!(ent = CL_NewTempEntity()))
1546                         continue;
1547
1548                 // normal stuff
1549                 ent->render.model = cl.model_precache[cl.qw_modelindex_spike];
1550                 ent->render.colormap = -1; // no special coloring
1551                 ent->render.alpha = 1;
1552                 VectorSet(ent->render.colormod, 1, 1, 1);
1553
1554                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, v[0], v[1], v[2], v[3], v[4], v[5], 1);
1555                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1556                 R_LerpAnimation(&ent->render);
1557                 CL_BoundingBoxForEntity(&ent->render);
1558         }
1559 }
1560
1561 void CL_LerpPlayer(float frac)
1562 {
1563         int i;
1564         float d;
1565
1566         cl.viewzoom = cl.mviewzoom[1] + frac * (cl.mviewzoom[0] - cl.mviewzoom[1]);
1567         for (i = 0;i < 3;i++)
1568         {
1569                 cl.punchangle[i] = cl.mpunchangle[1][i] + frac * (cl.mpunchangle[0][i] - cl.mpunchangle[1][i]);
1570                 cl.punchvector[i] = cl.mpunchvector[1][i] + frac * (cl.mpunchvector[0][i] - cl.mpunchvector[1][i]);
1571                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1572         }
1573
1574         if (cls.demoplayback)
1575         {
1576                 // interpolate the angles
1577                 for (i = 0;i < 3;i++)
1578                 {
1579                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1580                         if (d > 180)
1581                                 d -= 360;
1582                         else if (d < -180)
1583                                 d += 360;
1584                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1585                 }
1586         }
1587 }
1588
1589 void CSQC_RelinkAllEntities (int drawmask)
1590 {
1591         CL_RelinkNetworkEntities(drawmask);
1592         if(drawmask & ENTMASK_ENGINE)
1593         {
1594                 // move particles
1595                 CL_MoveParticles();
1596                 R_MoveExplosions();
1597         }
1598
1599         // link stuff
1600         CL_RelinkWorld();
1601         CL_RelinkCSQCWorld();   //[515]: csqc
1602         if(drawmask & ENTMASK_ENGINE)
1603         {
1604                 CL_RelinkStaticEntities();
1605                 CL_RelinkBeams();
1606                 CL_RelinkEffects();
1607                 CL_RelinkQWNails();
1608         }
1609 }
1610
1611 /*
1612 ===============
1613 CL_ReadFromServer
1614
1615 Read all incoming data from the server
1616 ===============
1617 */
1618 extern void CL_ClientMovement_Replay();
1619
1620 int CL_ReadFromServer(void)
1621 {
1622         CL_ReadDemoMessage();
1623         CL_SendMove();
1624
1625         r_refdef.time = cl.time;
1626         r_refdef.extraupdate = !r_speeds.integer;
1627         r_refdef.numentities = 0;
1628         r_refdef.viewentitymatrix = identitymatrix;
1629         cl_num_brushmodel_entities = 0;
1630
1631         if (cls.state == ca_connected && cls.signon == SIGNONS)
1632         {
1633                 // prepare for a new frame
1634                 CL_LerpPlayer(CL_LerpPoint());
1635                 CL_DecayLights();
1636                 CL_ClearTempEntities();
1637                 V_DriftPitch();
1638                 V_FadeViewFlashs();
1639
1640                 // relink network entities (note: this sets up the view!)
1641                 CL_ClientMovement_Replay();
1642                 if(!csqc_loaded)        //[515]: csqc
1643                 {
1644                         CL_RelinkNetworkEntities(65535);
1645
1646                         // move particles
1647                         CL_MoveParticles();
1648                         R_MoveExplosions();
1649
1650                         // link stuff
1651                         CL_RelinkWorld();
1652                         CL_RelinkCSQCWorld();   //[515]: csqc
1653                         CL_RelinkStaticEntities();
1654                         CL_RelinkBeams();
1655                         CL_RelinkEffects();
1656                         CL_RelinkQWNails();
1657                 }
1658                 else
1659                         csqc_frame = true;
1660
1661                 CL_UpdateLights();
1662
1663                 // update view blend
1664                 V_CalcViewBlend();
1665         }
1666
1667         return 0;
1668 }
1669
1670 // LordHavoc: pausedemo command
1671 static void CL_PauseDemo_f (void)
1672 {
1673         cls.demopaused = !cls.demopaused;
1674         if (cls.demopaused)
1675                 Con_Print("Demo paused\n");
1676         else
1677                 Con_Print("Demo unpaused\n");
1678 }
1679
1680 /*
1681 ======================
1682 CL_Fog_f
1683 ======================
1684 */
1685 static void CL_Fog_f (void)
1686 {
1687         if (Cmd_Argc () == 1)
1688         {
1689                 Con_Printf("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1690                 return;
1691         }
1692         fog_density = atof(Cmd_Argv(1));
1693         fog_red = atof(Cmd_Argv(2));
1694         fog_green = atof(Cmd_Argv(3));
1695         fog_blue = atof(Cmd_Argv(4));
1696 }
1697
1698 /*
1699 ====================
1700 CL_TimeRefresh_f
1701
1702 For program optimization
1703 ====================
1704 */
1705 static void CL_TimeRefresh_f (void)
1706 {
1707         int i;
1708         float timestart, timedelta, oldangles[3];
1709
1710         r_refdef.extraupdate = false;
1711         VectorCopy(cl.viewangles, oldangles);
1712         VectorClear(cl.viewangles);
1713
1714         timestart = Sys_DoubleTime();
1715         for (i = 0;i < 128;i++)
1716         {
1717                 Matrix4x4_CreateFromQuakeEntity(&r_refdef.viewentitymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2], 0, i / 128.0 * 360.0, 0, 1);
1718                 CL_UpdateScreen();
1719         }
1720         timedelta = Sys_DoubleTime() - timestart;
1721
1722         VectorCopy(oldangles, cl.viewangles);
1723         Con_Printf("%f seconds (%f fps)\n", timedelta, 128/timedelta);
1724 }
1725
1726 /*
1727 ===========
1728 CL_Shutdown
1729 ===========
1730 */
1731 void CL_Shutdown (void)
1732 {
1733         CL_Particles_Shutdown();
1734         CL_Parse_Shutdown();
1735
1736         Mem_FreePool (&cl_mempool);
1737 }
1738
1739 /*
1740 =================
1741 CL_Init
1742 =================
1743 */
1744 void CL_Init (void)
1745 {
1746         cl_mempool = Mem_AllocPool("client", 0, NULL);
1747
1748         memset(&r_refdef, 0, sizeof(r_refdef));
1749         // max entities sent to renderer per frame
1750         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1751         r_refdef.entities = (entity_render_t **)Mem_Alloc(cl_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1752         // 256k drawqueue buffer
1753         // TODO: make dynamic
1754         r_refdef.maxdrawqueuesize = 256 * 1024;
1755         r_refdef.drawqueue = (unsigned char *)Mem_Alloc(cl_mempool, r_refdef.maxdrawqueuesize);
1756
1757         CL_InitInput ();
1758
1759 //
1760 // register our commands
1761 //
1762         Cvar_RegisterVariable (&csqc_progname);
1763         Cvar_RegisterVariable (&csqc_progcrc);
1764
1765         Cvar_RegisterVariable (&cl_upspeed);
1766         Cvar_RegisterVariable (&cl_forwardspeed);
1767         Cvar_RegisterVariable (&cl_backspeed);
1768         Cvar_RegisterVariable (&cl_sidespeed);
1769         Cvar_RegisterVariable (&cl_movespeedkey);
1770         Cvar_RegisterVariable (&cl_yawspeed);
1771         Cvar_RegisterVariable (&cl_pitchspeed);
1772         Cvar_RegisterVariable (&cl_anglespeedkey);
1773         Cvar_RegisterVariable (&cl_shownet);
1774         Cvar_RegisterVariable (&cl_nolerp);
1775         Cvar_RegisterVariable (&lookspring);
1776         Cvar_RegisterVariable (&lookstrafe);
1777         Cvar_RegisterVariable (&sensitivity);
1778         Cvar_RegisterVariable (&freelook);
1779
1780         Cvar_RegisterVariable (&m_pitch);
1781         Cvar_RegisterVariable (&m_yaw);
1782         Cvar_RegisterVariable (&m_forward);
1783         Cvar_RegisterVariable (&m_side);
1784
1785         Cvar_RegisterVariable (&cl_itembobspeed);
1786         Cvar_RegisterVariable (&cl_itembobheight);
1787
1788         Cmd_AddCommand ("entities", CL_PrintEntities_f, "print information on network entities known to client");
1789         Cmd_AddCommand ("disconnect", CL_Disconnect_f, "disconnect from server (or disconnect all clients if running a server)");
1790         Cmd_AddCommand ("record", CL_Record_f, "record a demo");
1791         Cmd_AddCommand ("stop", CL_Stop_f, "stop recording or playing a demo");
1792         Cmd_AddCommand ("playdemo", CL_PlayDemo_f, "watch a demo file");
1793         Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "play back a demo as fast as possible and save statistics to benchmark.log");
1794
1795         Cmd_AddCommand ("fog", CL_Fog_f, "set global fog parameters (density red green blue)");
1796
1797         // LordHavoc: added pausedemo
1798         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f, "pause demo playback (can also safely pause demo recording if using QUAKE, QUAKEDP or NEHAHRAMOVIE protocol, useful for making movies)");
1799
1800         Cvar_RegisterVariable(&r_draweffects);
1801         Cvar_RegisterVariable(&cl_explosions_alpha_start);
1802         Cvar_RegisterVariable(&cl_explosions_alpha_end);
1803         Cvar_RegisterVariable(&cl_explosions_size_start);
1804         Cvar_RegisterVariable(&cl_explosions_size_end);
1805         Cvar_RegisterVariable(&cl_explosions_lifetime);
1806         Cvar_RegisterVariable(&cl_stainmaps);
1807         Cvar_RegisterVariable(&cl_stainmaps_clearonload);
1808         Cvar_RegisterVariable(&cl_beams_polygons);
1809         Cvar_RegisterVariable(&cl_beams_relative);
1810         Cvar_RegisterVariable(&cl_beams_lightatend);
1811         Cvar_RegisterVariable(&cl_noplayershadow);
1812
1813         Cvar_RegisterVariable(&cl_prydoncursor);
1814
1815         Cvar_RegisterVariable(&cl_deathnoviewmodel);
1816
1817         // for QW connections
1818         Cvar_RegisterVariable(&qport);
1819         Cvar_SetValueQuick(&qport, (rand() * RAND_MAX + rand()) & 0xffff);
1820
1821         Cmd_AddCommand("timerefresh", CL_TimeRefresh_f, "turn quickly and print rendering statistcs");
1822
1823         CL_Parse_Init();
1824         CL_Particles_Init();
1825         CL_Screen_Init();
1826
1827         CL_Video_Init();
1828 }
1829
1830
1831