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