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