2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // cl_main.c -- client main loop
23 #include "cl_collision.h"
29 // we need to declare some mouse variables here, because the menu system
30 // references them even when on a unix system.
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"};
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"};
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"};
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"};
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"};
50 cvar_t freelook = {CVAR_SAVE, "freelook", "1","mouse controls pitch instead of forward/back"};
52 cvar_t r_draweffects = {0, "r_draweffects", "1","renders temporary sprite effects"};
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"};
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"};
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"};
67 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0","hide player shadow"};
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"};
71 cvar_t cl_deathnoviewmodel = {0, "cl_deathnoviewmodel", "1", "hides gun model when dead"};
73 vec3_t cl_playerstandmins;
74 vec3_t cl_playerstandmaxs;
75 vec3_t cl_playercrouchmins;
76 vec3_t cl_playercrouchmaxs;
78 mempool_t *cl_mempool;
84 int cl_max_csqcentities;
85 int cl_max_static_entities;
86 int cl_max_temp_entities;
90 int cl_max_lightstyle;
91 int cl_max_brushmodel_entities;
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;
102 dlight_t *cl_dlights;
103 lightstyle_t *cl_lightstyle;
104 int *cl_brushmodel_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;
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];
117 =====================
120 =====================
122 void CL_ClearState(void)
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;
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;
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;
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;
162 cl_max_dlights = MAX_DLIGHTS;
163 cl_max_lightstyle = MAX_LIGHTSTYLES;
164 cl_max_brushmodel_entities = MAX_EDICTS;
165 cl_activedlights = 0;
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));
179 cl_lastquakeentity = 0;
180 memset(cl_isquakeentity, 0, sizeof(cl_isquakeentity));
182 // LordHavoc: have to set up the baseline info for alpha and other stuff
183 for (i = 0;i < cl_max_entities;i++)
185 cl_entities[i].state_baseline = defaultstate;
186 cl_entities[i].state_previous = defaultstate;
187 cl_entities[i].state_current = defaultstate;
190 for (i = 0;i < cl_max_csqcentities;i++)
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;
199 if (gamemode == GAME_NEXUIZ)
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);
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);
215 CL_Particles_Clear();
219 void CL_ExpandEntities(int num)
221 int i, oldmaxentities;
222 entity_t *oldentities;
223 if (num >= cl_max_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++)
237 cl_entities[i].state_baseline = defaultstate;
238 cl_entities[i].state_previous = defaultstate;
239 cl_entities[i].state_current = defaultstate;
244 void CL_ExpandCSQCEntities(int num)
246 int i, oldmaxentities;
247 entity_t *oldentities;
248 if (num >= cl_max_csqcentities)
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++)
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;
271 void CL_VM_ShutDown (void);
273 =====================
276 Sends a disconnect message to the server
277 This is also called on Host_Error, so it shouldn't cause any errors
278 =====================
280 void CL_Disconnect(void)
282 if (cls.state == ca_dedicated)
285 Con_DPrintf("CL_Disconnect\n");
288 // stop sounds (especially looping!)
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;
297 cl.worldmodel = NULL;
299 if (cls.demoplayback)
304 unsigned char bufdata[8];
305 if (cls.demorecording)
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));
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);
321 cls.state = ca_disconnected;
323 cls.demoplayback = cls.timedemo = false;
327 void CL_Disconnect_f(void)
331 Host_ShutdownServer ();
338 =====================
339 CL_EstablishConnection
341 Host should be either "local" or a net address
342 =====================
344 void CL_EstablishConnection(const char *host)
346 if (cls.state == ca_dedicated)
349 // clear menu's connect error message
350 M_Update_Return_Reason("");
353 // stop demo loop in case this fails
356 // make sure the client ports are open before attempting to connect
357 NetConn_UpdateSockets();
359 // run a network frame
360 //NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
362 if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
364 cls.connect_trying = true;
365 cls.connect_remainingtries = 3;
366 cls.connect_nextsendtime = 0;
367 M_Update_Return_Reason("Trying to connect...");
368 // run several network frames to jump into the game quickly
371 // NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
372 // NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
373 // NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
374 // NetConn_ClientFrame();SV_VM_Begin();NetConn_ServerFrame();SV_VM_End();
379 Con_Print("Unable to find a suitable network socket to connect to server.\n");
380 M_Update_Return_Reason("No network");
389 static void CL_PrintEntities_f(void)
395 for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
397 if (!ent->state_current.active)
400 if (ent->render.model)
401 strlcpy (name, ent->render.model->name, 25);
403 strcpy(name, "--no model--");
404 for (j = (int)strlen(name);j < 25;j++)
406 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);
410 //static const vec3_t nomodelmins = {-16, -16, -16};
411 //static const vec3_t nomodelmaxs = {16, 16, 16};
412 void CL_BoundingBoxForEntity(entity_render_t *ent)
416 //if (ent->angles[0] || ent->angles[2])
417 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
420 ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
421 ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
422 ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
423 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
424 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
425 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
426 //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
427 //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
429 //else if (ent->angles[1])
430 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
433 ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
434 ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
435 ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
436 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
437 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
438 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
439 //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
440 //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
444 ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
445 ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
446 ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
447 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
448 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
449 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
450 //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
451 //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
456 ent->mins[0] = ent->matrix.m[0][3] - 16;
457 ent->mins[1] = ent->matrix.m[1][3] - 16;
458 ent->mins[2] = ent->matrix.m[2][3] - 16;
459 ent->maxs[0] = ent->matrix.m[0][3] + 16;
460 ent->maxs[1] = ent->matrix.m[1][3] + 16;
461 ent->maxs[2] = ent->matrix.m[2][3] + 16;
462 //VectorAdd(ent->origin, nomodelmins, ent->mins);
463 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
471 Determines the fraction between the last two messages that the objects
475 static float CL_LerpPoint(void)
479 // dropped packet, or start of demo
480 if (cl.mtime[1] < cl.mtime[0] - 0.1)
481 cl.mtime[1] = cl.mtime[0] - 0.1;
483 cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
485 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
486 f = cl.mtime[0] - cl.mtime[1];
487 if (!f || cl_nolerp.integer || cls.timedemo || (cl.islocalgame && !sv_fixedframeratesingleplayer.integer))
489 cl.time = cl.mtime[0];
493 f = (cl.time - cl.mtime[1]) / f;
494 return bound(0, f, 1);
497 void CL_ClearTempEntities (void)
499 cl_num_temp_entities = 0;
502 entity_t *CL_NewTempEntity(void)
506 if (r_refdef.numentities >= r_refdef.maxentities)
508 if (cl_num_temp_entities >= cl_max_temp_entities)
510 ent = &cl_temp_entities[cl_num_temp_entities++];
511 memset (ent, 0, sizeof(*ent));
512 r_refdef.entities[r_refdef.numentities++] = &ent->render;
514 ent->render.colormap = -1; // no special coloring
515 ent->render.scale = 1;
516 ent->render.alpha = 1;
517 VectorSet(ent->render.colormod, 1, 1, 1);
521 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
525 if (!modelindex) // sanity check
527 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
532 VectorCopy(org, e->origin);
533 e->modelindex = modelindex;
534 e->starttime = cl.time;
535 e->startframe = startframe;
536 e->endframe = startframe + framecount;
537 e->framerate = framerate;
540 e->frame1time = cl.time;
541 e->frame2time = cl.time;
546 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)
552 // first look for an exact key match
556 for (i = 0;i < cl_activedlights;i++, dl++)
562 // then look for anything else
564 for (i = 0;i < cl_activedlights;i++, dl++)
567 // if we hit the end of the active dlights and found no gaps, add a new one
570 cl_activedlights = i + 1;
574 // unable to find one
578 //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
579 memset (dl, 0, sizeof(*dl));
580 Matrix4x4_Normalize(&dl->matrix, matrix);
582 dl->origin[0] = dl->matrix.m[0][3];
583 dl->origin[1] = dl->matrix.m[1][3];
584 dl->origin[2] = dl->matrix.m[2][3];
585 CL_FindNonSolidLocation(dl->origin, dl->origin, 6);
586 dl->matrix.m[0][3] = dl->origin[0];
587 dl->matrix.m[1][3] = dl->origin[1];
588 dl->matrix.m[2][3] = dl->origin[2];
591 dl->color[1] = green;
595 dl->die = cl.time + lifetime;
598 dl->cubemapnum = cubemapnum;
600 dl->shadow = shadowenable;
603 dl->coronasizescale = coronasizescale;
604 dl->ambientscale = ambientscale;
605 dl->diffusescale = diffusescale;
606 dl->specularscale = specularscale;
609 // called before entity relinking
610 void CL_DecayLights(void)
616 time = cl.time - cl.oldtime;
617 oldmax = cl_activedlights;
618 cl_activedlights = 0;
619 for (i = 0, dl = cl_dlights;i < oldmax;i++, dl++)
623 f = dl->radius - time * dl->decay;
624 if (cl.time < dl->die && f > 0)
626 dl->radius = dl->radius - time * dl->decay;
627 cl_activedlights = i + 1;
635 // called after entity relinking
636 void CL_UpdateLights(void)
642 r_refdef.numlights = 0;
643 if (r_dynamic.integer)
645 for (i = 0, dl = cl_dlights;i < cl_activedlights;i++, dl++)
649 R_RTLight_Update(dl, false);
650 r_refdef.lights[r_refdef.numlights++] = dl;
656 // 'm' is normal light, 'a' is no light, 'z' is double bright
660 for (j = 0;j < cl_max_lightstyle;j++)
662 if (!cl_lightstyle || !cl_lightstyle[j].length)
664 r_refdef.lightstylevalue[j] = 256;
667 k = i % cl_lightstyle[j].length;
668 l = (i-1) % cl_lightstyle[j].length;
669 k = cl_lightstyle[j].map[k] - 'a';
670 l = cl_lightstyle[j].map[l] - 'a';
671 r_refdef.lightstylevalue[j] = ((k*frac)+(l*(1-frac)))*22;
675 #define MAXVIEWMODELS 32
676 entity_t *viewmodels[MAXVIEWMODELS];
679 matrix4x4_t viewmodelmatrix;
681 static int entitylinkframenumber;
683 static const vec3_t muzzleflashorigin = {18, 0, 0};
685 extern void V_DriftPitch(void);
686 extern void V_FadeViewFlashs(void);
687 extern void V_CalcViewBlend(void);
689 extern void V_CalcRefdef(void);
690 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
691 void CL_LinkNetworkEntity(entity_t *e)
693 matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2;
694 //matrix4x4_t dlightmatrix;
695 int j, k, l, trailtype, temp;
696 float origin[3], angles[3], delta[3], lerp, dlightcolor[3], dlightradius, mins[3], maxs[3], v2[3], d;
700 //entity_persistent_t *p = &e->persistent;
701 //entity_render_t *r = &e->render;
702 if (e->persistent.linkframe != entitylinkframenumber)
704 e->persistent.linkframe = entitylinkframenumber;
705 // skip inactive entities and world
706 if (!e->state_current.active || e == cl_entities || e == cl_csqcentities)
708 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
709 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
710 e->render.flags = e->state_current.flags;
711 e->render.effects = e->state_current.effects;
712 VectorScale(e->state_current.colormod, (1.0f / 32.0f), e->render.colormod);
713 if (e->state_current.flags & RENDER_COLORMAPPED)
716 unsigned char *cbcolor;
717 e->render.colormap = e->state_current.colormap;
718 cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
719 cbcolor = (unsigned char *) (&palette_complete[cb]);
720 e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
721 e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
722 e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
723 cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
724 cbcolor = (unsigned char *) (&palette_complete[cb]);
725 e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
726 e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
727 e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
729 else if (e->state_current.colormap && cl.scores != NULL)
732 unsigned char *cbcolor;
733 e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
734 cb = (e->render.colormap & 0xF) << 4;cb += (cb >= 128 && cb < 224) ? 4 : 12;
735 cbcolor = (unsigned char *) (&palette_complete[cb]);
736 e->render.colormap_pantscolor[0] = cbcolor[0] * (1.0f / 255.0f);
737 e->render.colormap_pantscolor[1] = cbcolor[1] * (1.0f / 255.0f);
738 e->render.colormap_pantscolor[2] = cbcolor[2] * (1.0f / 255.0f);
739 cb = (e->render.colormap & 0xF0);cb += (cb >= 128 && cb < 224) ? 4 : 12;
740 cbcolor = (unsigned char *) (&palette_complete[cb]);
741 e->render.colormap_shirtcolor[0] = cbcolor[0] * (1.0f / 255.0f);
742 e->render.colormap_shirtcolor[1] = cbcolor[1] * (1.0f / 255.0f);
743 e->render.colormap_shirtcolor[2] = cbcolor[2] * (1.0f / 255.0f);
747 e->render.colormap = -1; // no special coloring
748 VectorClear(e->render.colormap_pantscolor);
749 VectorClear(e->render.colormap_shirtcolor);
751 e->render.skinnum = e->state_current.skin;
752 if (e->render.flags & RENDER_VIEWMODEL && !e->state_current.tagentity)
754 if (!r_drawviewmodel.integer || chase_active.integer || envmap)// || csqc_loaded)
759 CL_LinkNetworkEntity(cl_entities + cl.viewentity);
760 if (e == &cl.viewent && cl_entities[cl.viewentity].state_current.active)
762 e->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
763 e->state_current.effects = EF_NOSHADOW | (cl_entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT | EF_NODEPTHTEST));
766 matrix = &viewmodelmatrix;
770 // if the tag entity is currently impossible, skip it
773 if (e->state_current.tagentity >= cl_num_entities)
775 t = cl_entities + e->state_current.tagentity;
779 if (e->state_current.tagentity >= cl_num_csqcentities)
781 t = cl_csqcentities + e->state_current.tagentity;
783 // if the tag entity is inactive, skip it
784 if (!t->state_current.active)
786 // note: this can link to world
787 CL_LinkNetworkEntity(t);
788 // make relative to the entity
789 matrix = &t->render.matrix;
790 // some properties of the tag entity carry over
791 e->render.flags |= t->render.flags & (RENDER_EXTERIORMODEL | RENDER_VIEWMODEL);
792 // if a valid tagindex is used, make it relative to that tag instead
793 // FIXME: use a model function to get tag info (need to handle skeletal)
794 if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model))
796 // blend the matrices
797 memset(&blendmatrix, 0, sizeof(blendmatrix));
798 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
800 matrix4x4_t tagmatrix;
801 Mod_Alias_GetTagMatrix(model, t->render.frameblend[j].frame, e->state_current.tagindex - 1, &tagmatrix);
802 d = t->render.frameblend[j].lerp;
803 for (l = 0;l < 4;l++)
804 for (k = 0;k < 4;k++)
805 blendmatrix.m[l][k] += d * tagmatrix.m[l][k];
807 // concat the tag matrices onto the entity matrix
808 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
809 // use the constructed tag matrix
810 matrix = &tempmatrix;
815 // if it's the player entity, update according to client movement
816 if (e == cl_entities + cl.playerentity && cl.movement)// && !e->csqc)
818 lerp = (cl.time - cl.mtime[1]) / (cl.mtime[0] - cl.mtime[1]);
819 lerp = bound(0, lerp, 1);
820 VectorLerp(cl.movement_oldorigin, lerp, cl.movement_origin, origin);
821 VectorSet(angles, 0, cl.viewangles[1], 0);
823 else if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
825 // interpolate the origin and angles
826 VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
827 VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
828 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
829 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
830 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
831 VectorMA(e->persistent.oldangles, lerp, delta, angles);
836 VectorCopy(e->persistent.neworigin, origin);
837 VectorCopy(e->persistent.newangles, angles);
840 // model setup and some modelflags
841 if(e->state_current.modelindex < MAX_MODELS)
842 e->render.model = cl.model_precache[e->state_current.modelindex];
844 e->render.model = cl.csqc_model_precache[65536-e->state_current.modelindex];
847 // if model is alias or this is a tenebrae-like dlight, reverse pitch direction
848 if (e->render.model->type == mod_alias || (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC))
849 angles[0] = -angles[0];
850 if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
852 angles[1] = ANGLEMOD(100*cl.time);
853 if (cl_itembobheight.value)
854 origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
856 // transfer certain model flags to effects
857 e->render.effects |= e->render.model->flags2 & (EF_FULLBRIGHT | EF_ADDITIVE);
858 if (cl_prydoncursor.integer && (e->render.effects & EF_SELECTABLE) && cl.cmd.cursor_entitynumber == e->state_current.number)
859 VectorScale(e->render.colormod, 2, e->render.colormod);
863 if (e->render.frame2 == e->state_current.frame)
865 // update frame lerp fraction
866 e->render.framelerp = 1;
867 if (e->render.frame2time > e->render.frame1time)
869 // make sure frame lerp won't last longer than 100ms
870 // (this mainly helps with models that use framegroups and
871 // switch between them infrequently)
872 e->render.framelerp = (cl.time - e->render.frame2time) / min(e->render.frame2time - e->render.frame1time, 0.1);
873 e->render.framelerp = bound(0, e->render.framelerp, 1);
878 // begin a new frame lerp
879 e->render.frame1 = e->render.frame2;
880 e->render.frame1time = e->render.frame2time;
881 e->render.frame = e->render.frame2 = e->state_current.frame;
882 e->render.frame2time = cl.time;
883 e->render.framelerp = 0;
885 R_LerpAnimation(&e->render);
887 // set up the render matrix
888 // FIXME: e->render.scale should go away
889 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
890 // concat the matrices to make the entity relative to its tag
891 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
892 // make the other useful stuff
893 Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
894 CL_BoundingBoxForEntity(&e->render);
896 // handle effects now that we know where this entity is in the world...
897 if (e->render.model && e->render.model->soundfromcenter)
899 // bmodels are treated specially since their origin is usually '0 0 0'
901 VectorMAM(0.5f, e->render.model->normalmins, 0.5f, e->render.model->normalmaxs, o);
902 Matrix4x4_Transform(&e->render.matrix, o, origin);
906 origin[0] = e->render.matrix.m[0][3];
907 origin[1] = e->render.matrix.m[1][3];
908 origin[2] = e->render.matrix.m[2][3];
915 // LordHavoc: if the entity has no effects, don't check each
916 if (e->render.effects)
918 if (e->render.effects & EF_BRIGHTFIELD)
920 if (gamemode == GAME_NEXUIZ)
922 dlightradius = max(dlightradius, 200);
923 dlightcolor[0] += 0.75f;
924 dlightcolor[1] += 1.50f;
925 dlightcolor[2] += 3.00f;
929 CL_EntityParticles(e);
931 if (e->render.effects & EF_MUZZLEFLASH)
932 e->persistent.muzzleflash = 1.0f;
933 if (e->render.effects & EF_DIMLIGHT)
935 dlightradius = max(dlightradius, 200);
936 dlightcolor[0] += 1.50f;
937 dlightcolor[1] += 1.50f;
938 dlightcolor[2] += 1.50f;
940 if (e->render.effects & EF_BRIGHTLIGHT)
942 dlightradius = max(dlightradius, 400);
943 dlightcolor[0] += 3.00f;
944 dlightcolor[1] += 3.00f;
945 dlightcolor[2] += 3.00f;
947 // LordHavoc: more effects
948 if (e->render.effects & EF_RED) // red
950 dlightradius = max(dlightradius, 200);
951 dlightcolor[0] += 1.50f;
952 dlightcolor[1] += 0.15f;
953 dlightcolor[2] += 0.15f;
955 if (e->render.effects & EF_BLUE) // blue
957 dlightradius = max(dlightradius, 200);
958 dlightcolor[0] += 0.15f;
959 dlightcolor[1] += 0.15f;
960 dlightcolor[2] += 1.50f;
962 if (e->render.effects & EF_FLAME)
964 mins[0] = origin[0] - 16.0f;
965 mins[1] = origin[1] - 16.0f;
966 mins[2] = origin[2] - 16.0f;
967 maxs[0] = origin[0] + 16.0f;
968 maxs[1] = origin[1] + 16.0f;
969 maxs[2] = origin[2] + 16.0f;
970 // how many flames to make
971 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
972 CL_FlameCube(mins, maxs, temp);
973 d = lhrandom(0.75f, 1);
974 dlightradius = max(dlightradius, 200);
975 dlightcolor[0] += d * 2.0f;
976 dlightcolor[1] += d * 1.5f;
977 dlightcolor[2] += d * 0.5f;
979 if (e->render.effects & EF_STARDUST)
981 mins[0] = origin[0] - 16.0f;
982 mins[1] = origin[1] - 16.0f;
983 mins[2] = origin[2] - 16.0f;
984 maxs[0] = origin[0] + 16.0f;
985 maxs[1] = origin[1] + 16.0f;
986 maxs[2] = origin[2] + 16.0f;
987 // how many particles to make
988 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
989 CL_Stardust(mins, maxs, temp);
990 dlightradius = max(dlightradius, 200);
991 dlightcolor[0] += 1.0f;
992 dlightcolor[1] += 0.7f;
993 dlightcolor[2] += 0.3f;
996 // muzzleflash fades over time, and is offset a bit
997 if (e->persistent.muzzleflash > 0)
999 Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
1000 trace = CL_TraceBox(origin, vec3_origin, vec3_origin, v2, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY, false);
1001 tempmatrix = e->render.matrix;
1002 tempmatrix.m[0][3] = trace.endpos[0];
1003 tempmatrix.m[1][3] = trace.endpos[1];
1004 tempmatrix.m[2][3] = trace.endpos[2];
1005 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);
1006 e->persistent.muzzleflash -= cl.frametime * 10;
1008 // LordHavoc: if the model has no flags, don't check each
1009 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
1011 if (e->render.model->flags & EF_GIB)
1013 else if (e->render.model->flags & EF_ZOMGIB)
1015 else if (e->render.model->flags & EF_TRACER)
1018 //dlightradius = max(dlightradius, 100);
1019 //dlightcolor[0] += 0.25f;
1020 //dlightcolor[1] += 1.00f;
1021 //dlightcolor[2] += 0.25f;
1023 else if (e->render.model->flags & EF_TRACER2)
1026 //dlightradius = max(dlightradius, 100);
1027 //dlightcolor[0] += 1.00f;
1028 //dlightcolor[1] += 0.60f;
1029 //dlightcolor[2] += 0.20f;
1031 else if (e->render.model->flags & EF_ROCKET)
1034 dlightradius = max(dlightradius, 200);
1035 dlightcolor[0] += 3.00f;
1036 dlightcolor[1] += 1.50f;
1037 dlightcolor[2] += 0.50f;
1039 else if (e->render.model->flags & EF_GRENADE)
1041 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
1042 trailtype = e->render.alpha == -1 ? 7 : 1;
1044 else if (e->render.model->flags & EF_TRACER3)
1047 if (gamemode == GAME_PRYDON)
1049 dlightradius = max(dlightradius, 100);
1050 dlightcolor[0] += 0.30f;
1051 dlightcolor[1] += 0.60f;
1052 dlightcolor[2] += 1.20f;
1056 dlightradius = max(dlightradius, 200);
1057 dlightcolor[0] += 1.20f;
1058 dlightcolor[1] += 0.50f;
1059 dlightcolor[2] += 1.00f;
1063 // LordHavoc: customizable glow
1064 if (e->state_current.glowsize)
1066 // * 4 for the expansion from 0-255 to 0-1023 range,
1067 // / 255 to scale down byte colors
1068 dlightradius = max(dlightradius, e->state_current.glowsize * 4);
1069 VectorMA(dlightcolor, (1.0f / 255.0f), (unsigned char *)&palette_complete[e->state_current.glowcolor], dlightcolor);
1071 // make the glow dlight
1072 if (dlightradius > 0 && (dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL))
1074 //dlightmatrix = e->render.matrix;
1075 // hack to make glowing player light shine on their gun
1076 //if (e->state_current.number == cl.viewentity/* && !chase_active.integer*/)
1077 // dlightmatrix.m[2][3] += 30;
1078 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);
1081 if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
1084 VectorScale(e->state_current.light, (1.0f / 256.0f), light);
1085 light[3] = e->state_current.light[3];
1086 if (light[0] == 0 && light[1] == 0 && light[2] == 0)
1087 VectorSet(light, 1, 1, 1);
1090 // FIXME: add ambient/diffuse/specular scales as an extension ontop of TENEBRAE_GFX_DLIGHTS?
1091 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);
1094 if (e->render.flags & RENDER_GLOWTRAIL)
1097 CL_RocketTrail(e->persistent.trail_origin, origin, trailtype, e->state_current.glowcolor, e);
1098 VectorCopy(origin, e->persistent.trail_origin);
1099 // tenebrae's sprites are all additive mode (weird)
1100 if (gamemode == GAME_TENEBRAE && e->render.model && e->render.model->type == mod_sprite)
1101 e->render.effects |= EF_ADDITIVE;
1102 // player model is only shown with chase_active on
1104 if (e->state_current.number == cl.viewentity)
1105 e->render.flags |= RENDER_EXTERIORMODEL;
1106 // transparent stuff can't be lit during the opaque stage
1107 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1108 e->render.flags |= RENDER_TRANSPARENT;
1109 // double sided rendering mode causes backfaces to be visible
1110 // (mostly useful on transparent stuff)
1111 if (e->render.effects & EF_DOUBLESIDED)
1112 e->render.flags |= RENDER_NOCULLFACE;
1113 // either fullbright or lit
1114 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1115 e->render.flags |= RENDER_LIGHT;
1116 // hide player shadow during intermission or nehahra movie
1117 if (!(e->render.effects & EF_NOSHADOW)
1118 && !(e->render.flags & (RENDER_VIEWMODEL | RENDER_TRANSPARENT))
1119 && (!(e->render.flags & RENDER_EXTERIORMODEL) || (!cl.intermission && cl.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
1120 e->render.flags |= RENDER_SHADOW;
1121 // as soon as player is known we can call V_CalcRefDef
1123 if (e->state_current.number == cl.viewentity)
1125 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
1126 cl_brushmodel_entities[cl_num_brushmodel_entities++] = e->state_current.number;
1127 // don't show entities with no modelindex (note: this still shows
1128 // entities which have a modelindex that resolved to a NULL model)
1129 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
1130 r_refdef.entities[r_refdef.numentities++] = &e->render;
1131 //if (cl.viewentity && e->state_current.number == cl.viewentity)
1132 // Matrix4x4_Print(&e->render.matrix);
1136 void CL_RelinkWorld(void)
1138 entity_t *ent = &cl_entities[0];
1139 cl_brushmodel_entities[cl_num_brushmodel_entities++] = 0;
1140 // FIXME: this should be done at load
1141 Matrix4x4_CreateIdentity(&ent->render.matrix);
1142 Matrix4x4_CreateIdentity(&ent->render.inversematrix);
1143 R_LerpAnimation(&ent->render);
1144 CL_BoundingBoxForEntity(&ent->render);
1145 ent->render.flags = RENDER_SHADOW;
1146 if (!r_fullbright.integer)
1147 ent->render.flags |= RENDER_LIGHT;
1148 VectorSet(ent->render.colormod, 1, 1, 1);
1149 r_refdef.worldentity = &ent->render;
1150 r_refdef.worldmodel = cl.worldmodel;
1153 void CL_RelinkCSQCWorld(void) //[515]: csqc
1155 entity_t *ent = &cl_csqcentities[0];
1158 // cl_brushmodel_entities[cl_num_brushmodel_entities++] = 0;
1159 // FIXME: this should be done at load
1160 Matrix4x4_CreateIdentity(&ent->render.matrix);
1161 Matrix4x4_CreateIdentity(&ent->render.inversematrix);
1162 R_LerpAnimation(&ent->render);
1163 CL_BoundingBoxForEntity(&ent->render);
1164 ent->render.flags = RENDER_SHADOW;
1165 if (!r_fullbright.integer)
1166 ent->render.flags |= RENDER_LIGHT;
1167 VectorSet(ent->render.colormod, 1, 1, 1);
1168 // r_refdef.worldentity = &ent->render;
1169 // r_refdef.worldmodel = cl.worldmodel;
1172 static void CL_RelinkStaticEntities(void)
1176 for (i = 0, e = cl_static_entities;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++, e++)
1178 e->render.flags = 0;
1179 // transparent stuff can't be lit during the opaque stage
1180 if (e->render.effects & (EF_ADDITIVE | EF_NODEPTHTEST) || e->render.alpha < 1)
1181 e->render.flags |= RENDER_TRANSPARENT;
1182 // either fullbright or lit
1183 if (!(e->render.effects & EF_FULLBRIGHT) && !r_fullbright.integer)
1184 e->render.flags |= RENDER_LIGHT;
1185 // hide player shadow during intermission or nehahra movie
1186 if (!(e->render.effects & EF_NOSHADOW) && !(e->render.flags & RENDER_TRANSPARENT))
1187 e->render.flags |= RENDER_SHADOW;
1188 VectorSet(e->render.colormod, 1, 1, 1);
1189 R_LerpAnimation(&e->render);
1190 r_refdef.entities[r_refdef.numentities++] = &e->render;
1199 static void CL_RelinkNetworkEntities(int drawmask)
1207 ent->state_previous = ent->state_current;
1208 ent->state_current = defaultstate;
1209 ent->state_current.time = cl.time;
1210 ent->state_current.number = -1;
1211 ent->state_current.active = true;
1212 ent->state_current.modelindex = cl.stats[STAT_WEAPON];
1213 ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
1214 ent->state_current.flags = RENDER_VIEWMODEL;
1215 if ((cl.stats[STAT_HEALTH] <= 0 && cl_deathnoviewmodel.integer) || cl.intermission)
1216 ent->state_current.modelindex = 0;
1217 else if (cl.stats[STAT_ITEMS] & IT_INVISIBILITY)
1219 if (gamemode == GAME_TRANSFUSION)
1220 ent->state_current.alpha = 128;
1222 ent->state_current.modelindex = 0;
1225 // reset animation interpolation on weaponmodel if model changed
1226 if (ent->state_previous.modelindex != ent->state_current.modelindex)
1228 ent->render.frame = ent->render.frame1 = ent->render.frame2 = ent->state_current.frame;
1229 ent->render.frame1time = ent->render.frame2time = cl.time;
1230 ent->render.framelerp = 1;
1234 // start on the entity after the world
1235 entitylinkframenumber++;
1236 if(drawmask & ENTMASK_ENGINE || !csqc_loaded)
1238 for (i = 1;i < cl_num_entities;i++)
1240 if (cl_entities_active[i])
1242 ent = cl_entities + i;
1243 if (!(drawmask & ENTMASK_ENGINEVIEWMODELS))
1244 if (ent->state_current.flags & RENDER_VIEWMODEL) //[515]: csqc drawmask
1246 cl_entities_active[i] = false;
1249 if (ent->state_current.active)
1250 CL_LinkNetworkEntity(ent);
1252 cl_entities_active[i] = false;
1260 for (i=1,k=cl_num_csqcentities;k;i++)
1262 if (cl_csqcentities_active[i])
1265 ent = cl_csqcentities + i;
1266 if (ent->state_current.active)
1267 CL_LinkNetworkEntity(ent);
1269 cl_csqcentities_active[i] = false;
1274 CL_LinkNetworkEntity(&cl.viewent);
1277 static void CL_RelinkEffects(void)
1284 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
1288 frame = (cl.time - e->starttime) * e->framerate + e->startframe;
1290 if (intframe < 0 || intframe >= e->endframe)
1292 memset(e, 0, sizeof(*e));
1296 if (intframe != e->frame)
1298 e->frame = intframe;
1299 e->frame1time = e->frame2time;
1300 e->frame2time = cl.time;
1303 // if we're drawing effects, get a new temp entity
1304 // (NewTempEntity adds it to the render entities list for us)
1305 if (r_draweffects.integer && (ent = CL_NewTempEntity()))
1307 // interpolation stuff
1308 ent->render.frame1 = intframe;
1309 ent->render.frame2 = intframe + 1;
1310 if (ent->render.frame2 >= e->endframe)
1311 ent->render.frame2 = -1; // disappear
1312 ent->render.framelerp = frame - intframe;
1313 ent->render.frame1time = e->frame1time;
1314 ent->render.frame2time = e->frame2time;
1317 if(e->modelindex < MAX_MODELS)
1318 ent->render.model = cl.model_precache[e->modelindex];
1320 ent->render.model = cl.csqc_model_precache[65536-e->modelindex];
1321 ent->render.frame = ent->render.frame2;
1322 ent->render.colormap = -1; // no special coloring
1323 ent->render.alpha = 1;
1324 VectorSet(ent->render.colormod, 1, 1, 1);
1326 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
1327 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1328 R_LerpAnimation(&ent->render);
1329 CL_BoundingBoxForEntity(&ent->render);
1335 void CL_RelinkBeams(void)
1344 matrix4x4_t tempmatrix;
1346 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1348 if (!b->model || b->endtime < cl.time)
1351 // if coming from the player, update the start position
1352 //if (b->entity == cl.viewentity)
1353 // Matrix4x4_OriginFromMatrix(&cl_entities[cl.viewentity].render.matrix, b->start);
1354 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
1356 entity_render_t *r = &cl_entities[b->entity].render;
1357 //Matrix4x4_OriginFromMatrix(&r->matrix, origin);
1358 //Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
1359 Matrix4x4_Transform(&r->matrix, b->relativestart, b->start);
1360 Matrix4x4_Transform(&r->matrix, b->relativeend, b->end);
1365 if (cl_beams_lightatend.integer)
1367 // FIXME: create a matrix from the beam start/end orientation
1368 Matrix4x4_CreateTranslate(&tempmatrix, b->end[0], b->end[1], b->end[2]);
1369 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);
1371 if (cl_beams_polygons.integer)
1375 // calculate pitch and yaw
1376 VectorSubtract (b->end, b->start, dist);
1378 if (dist[1] == 0 && dist[0] == 0)
1388 yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1392 forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1393 pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1398 // add new entities for the lightning
1399 VectorCopy (b->start, org);
1400 d = VectorNormalizeLength(dist);
1403 ent = CL_NewTempEntity ();
1406 //VectorCopy (org, ent->render.origin);
1407 ent->render.model = b->model;
1408 //ent->render.effects = EF_FULLBRIGHT;
1409 //ent->render.angles[0] = pitch;
1410 //ent->render.angles[1] = yaw;
1411 //ent->render.angles[2] = rand()%360;
1412 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1413 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1414 R_LerpAnimation(&ent->render);
1415 CL_BoundingBoxForEntity(&ent->render);
1416 VectorMA(org, 30, dist, org);
1422 void CL_LerpPlayer(float frac)
1427 cl.viewzoom = cl.mviewzoom[1] + frac * (cl.mviewzoom[0] - cl.mviewzoom[1]);
1428 for (i = 0;i < 3;i++)
1430 cl.punchangle[i] = cl.mpunchangle[1][i] + frac * (cl.mpunchangle[0][i] - cl.mpunchangle[1][i]);
1431 cl.punchvector[i] = cl.mpunchvector[1][i] + frac * (cl.mpunchvector[0][i] - cl.mpunchvector[1][i]);
1432 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1435 if (cls.demoplayback)
1437 // interpolate the angles
1438 for (i = 0;i < 3;i++)
1440 d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1445 cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1450 void CSQC_RelinkAllEntities (int drawmask)
1452 CL_RelinkNetworkEntities(drawmask);
1453 if(drawmask & ENTMASK_ENGINE)
1462 CL_RelinkCSQCWorld(); //[515]: csqc
1463 if(drawmask & ENTMASK_ENGINE)
1465 CL_RelinkStaticEntities();
1475 Read all incoming data from the server
1478 extern void CL_ClientMovement_Replay();
1480 int CL_ReadFromServer(void)
1482 CL_ReadDemoMessage();
1485 r_refdef.time = cl.time;
1486 r_refdef.extraupdate = !r_speeds.integer;
1487 r_refdef.numentities = 0;
1488 Matrix4x4_CreateIdentity(&r_refdef.viewentitymatrix);
1489 cl_num_brushmodel_entities = 0;
1491 if (cls.state == ca_connected && cls.signon == SIGNONS)
1493 // prepare for a new frame
1494 CL_LerpPlayer(CL_LerpPoint());
1496 CL_ClearTempEntities();
1500 // relink network entities (note: this sets up the view!)
1501 CL_ClientMovement_Replay();
1502 if(!csqc_loaded) //[515]: csqc
1504 CL_RelinkNetworkEntities(65535);
1512 CL_RelinkCSQCWorld(); //[515]: csqc
1513 CL_RelinkStaticEntities();
1520 // run cgame code (which can add more entities)
1525 // update view blend
1532 // LordHavoc: pausedemo command
1533 static void CL_PauseDemo_f (void)
1535 cls.demopaused = !cls.demopaused;
1537 Con_Print("Demo paused\n");
1539 Con_Print("Demo unpaused\n");
1543 ======================
1545 ======================
1547 static void CL_Fog_f (void)
1549 if (Cmd_Argc () == 1)
1551 Con_Printf("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1554 fog_density = atof(Cmd_Argv(1));
1555 fog_red = atof(Cmd_Argv(2));
1556 fog_green = atof(Cmd_Argv(3));
1557 fog_blue = atof(Cmd_Argv(4));
1561 ====================
1564 For program optimization
1565 ====================
1567 static void CL_TimeRefresh_f (void)
1570 float timestart, timedelta, oldangles[3];
1572 r_refdef.extraupdate = false;
1573 VectorCopy(cl.viewangles, oldangles);
1574 VectorClear(cl.viewangles);
1576 timestart = Sys_DoubleTime();
1577 for (i = 0;i < 128;i++)
1579 Matrix4x4_CreateFromQuakeEntity(&r_refdef.viewentitymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2], 0, i / 128.0 * 360.0, 0, 1);
1582 timedelta = Sys_DoubleTime() - timestart;
1584 VectorCopy(oldangles, cl.viewangles);
1585 Con_Printf("%f seconds (%f fps)\n", timedelta, 128/timedelta);
1593 void CL_Shutdown (void)
1596 CL_Particles_Shutdown();
1597 CL_Parse_Shutdown();
1599 Mem_FreePool (&cl_mempool);
1609 cl_mempool = Mem_AllocPool("client", 0, NULL);
1611 memset(&r_refdef, 0, sizeof(r_refdef));
1612 // max entities sent to renderer per frame
1613 r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1614 r_refdef.entities = (entity_render_t **)Mem_Alloc(cl_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1615 // 256k drawqueue buffer
1616 // TODO: make dynamic
1617 r_refdef.maxdrawqueuesize = 256 * 1024;
1618 r_refdef.drawqueue = (unsigned char *)Mem_Alloc(cl_mempool, r_refdef.maxdrawqueuesize);
1623 // register our commands
1625 Cvar_RegisterVariable (&csqc_progname);
1626 Cvar_RegisterVariable (&csqc_progcrc);
1628 Cvar_RegisterVariable (&cl_upspeed);
1629 Cvar_RegisterVariable (&cl_forwardspeed);
1630 Cvar_RegisterVariable (&cl_backspeed);
1631 Cvar_RegisterVariable (&cl_sidespeed);
1632 Cvar_RegisterVariable (&cl_movespeedkey);
1633 Cvar_RegisterVariable (&cl_yawspeed);
1634 Cvar_RegisterVariable (&cl_pitchspeed);
1635 Cvar_RegisterVariable (&cl_anglespeedkey);
1636 Cvar_RegisterVariable (&cl_shownet);
1637 Cvar_RegisterVariable (&cl_nolerp);
1638 Cvar_RegisterVariable (&lookspring);
1639 Cvar_RegisterVariable (&lookstrafe);
1640 Cvar_RegisterVariable (&sensitivity);
1641 Cvar_RegisterVariable (&freelook);
1643 Cvar_RegisterVariable (&m_pitch);
1644 Cvar_RegisterVariable (&m_yaw);
1645 Cvar_RegisterVariable (&m_forward);
1646 Cvar_RegisterVariable (&m_side);
1648 Cvar_RegisterVariable (&cl_itembobspeed);
1649 Cvar_RegisterVariable (&cl_itembobheight);
1651 Cmd_AddCommand ("entities", CL_PrintEntities_f, "print information on network entities known to client");
1652 Cmd_AddCommand ("disconnect", CL_Disconnect_f, "disconnect from server (or disconnect all clients if running a server)");
1653 Cmd_AddCommand ("record", CL_Record_f, "record a demo");
1654 Cmd_AddCommand ("stop", CL_Stop_f, "stop recording or playing a demo");
1655 Cmd_AddCommand ("playdemo", CL_PlayDemo_f, "watch a demo file");
1656 Cmd_AddCommand ("timedemo", CL_TimeDemo_f, "play back a demo as fast as possible and save statistics to benchmark.log");
1658 Cmd_AddCommand ("fog", CL_Fog_f, "set global fog parameters (density red green blue)");
1660 // LordHavoc: added pausedemo
1661 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)");
1663 Cvar_RegisterVariable(&r_draweffects);
1664 Cvar_RegisterVariable(&cl_explosions_alpha_start);
1665 Cvar_RegisterVariable(&cl_explosions_alpha_end);
1666 Cvar_RegisterVariable(&cl_explosions_size_start);
1667 Cvar_RegisterVariable(&cl_explosions_size_end);
1668 Cvar_RegisterVariable(&cl_explosions_lifetime);
1669 Cvar_RegisterVariable(&cl_stainmaps);
1670 Cvar_RegisterVariable(&cl_stainmaps_clearonload);
1671 Cvar_RegisterVariable(&cl_beams_polygons);
1672 Cvar_RegisterVariable(&cl_beams_relative);
1673 Cvar_RegisterVariable(&cl_beams_lightatend);
1674 Cvar_RegisterVariable(&cl_noplayershadow);
1676 Cvar_RegisterVariable(&cl_prydoncursor);
1678 Cvar_RegisterVariable(&cl_deathnoviewmodel);
1680 Cmd_AddCommand("timerefresh", CL_TimeRefresh_f, "turn quickly and print rendering statistcs");
1683 CL_Particles_Init();