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