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"
27 // we need to declare some mouse variables here, because the menu system
28 // references them even when on a unix system.
30 cvar_t cl_shownet = {0, "cl_shownet","0"};
31 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
33 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
34 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
36 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
37 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
38 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
40 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
41 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
42 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
43 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
45 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
47 cvar_t r_draweffects = {0, "r_draweffects", "1"};
49 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
50 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
52 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1"};
53 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1"};
54 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0"};
56 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0"};
58 mempool_t *cl_refdef_mempool;
59 mempool_t *cl_entities_mempool;
65 int cl_max_static_entities;
66 int cl_max_temp_entities;
70 int cl_max_lightstyle;
71 int cl_max_brushmodel_entities;
73 entity_t *cl_entities;
74 qbyte *cl_entities_active;
75 entity_t *cl_static_entities;
76 entity_t *cl_temp_entities;
77 cl_effect_t *cl_effects;
80 lightstyle_t *cl_lightstyle;
81 entity_render_t **cl_brushmodel_entities;
84 int cl_num_static_entities;
85 int cl_num_temp_entities;
86 int cl_num_brushmodel_entities;
94 void CL_ClearState(void)
101 // note: this also gets rid of the entity database
102 Mem_EmptyPool(cl_entities_mempool);
104 // wipe the entire cl structure
105 memset (&cl, 0, sizeof(cl));
107 SZ_Clear (&cls.message);
110 cl_num_static_entities = 0;
111 cl_num_temp_entities = 0;
112 cl_num_brushmodel_entities = 0;
114 // tweak these if the game runs out
115 cl_max_entities = MAX_EDICTS;
116 cl_max_static_entities = 256;
117 cl_max_temp_entities = 512;
118 cl_max_effects = 256;
120 cl_max_dlights = MAX_DLIGHTS;
121 cl_max_lightstyle = MAX_LIGHTSTYLES;
122 cl_max_brushmodel_entities = MAX_EDICTS;
124 cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
125 cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
126 cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
127 cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
128 cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
129 cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
130 cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
131 cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
132 cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
136 CL_Particles_Clear();
138 // LordHavoc: have to set up the baseline info for alpha and other stuff
139 for (i = 0;i < cl_max_entities;i++)
141 ClearStateToDefault(&cl_entities[i].state_baseline);
142 ClearStateToDefault(&cl_entities[i].state_previous);
143 ClearStateToDefault(&cl_entities[i].state_current);
150 =====================
153 Sends a disconnect message to the server
154 This is also called on Host_Error, so it shouldn't cause any errors
155 =====================
157 void CL_Disconnect(void)
159 if (cls.state == ca_dedicated)
162 // stop sounds (especially looping!)
163 S_StopAllSounds (true);
165 // clear contents blends
166 cl.cshifts[0].percent = 0;
167 cl.cshifts[1].percent = 0;
168 cl.cshifts[2].percent = 0;
169 cl.cshifts[3].percent = 0;
171 cl.worldmodel = NULL;
173 if (cls.demoplayback)
177 if (cls.demorecording)
180 Con_DPrintf("Sending clc_disconnect\n");
181 SZ_Clear(&cls.message);
182 MSG_WriteByte(&cls.message, clc_disconnect);
183 NetConn_SendUnreliableMessage(cls.netcon, &cls.message);
184 SZ_Clear(&cls.message);
185 NetConn_Close(cls.netcon);
187 // if running a local server, shut it down
190 // prevent this code from executing again during Host_ShutdownServer
191 cls.state = ca_disconnected;
192 Host_ShutdownServer(false);
195 cls.state = ca_disconnected;
197 cls.demoplayback = cls.timedemo = false;
201 void CL_Disconnect_f(void)
205 Host_ShutdownServer (false);
212 =====================
213 CL_EstablishConnection
215 Host should be either "local" or a net address
216 =====================
218 void CL_EstablishConnection(const char *host)
220 if (cls.state == ca_dedicated)
223 // clear menu's connect error message
224 m_return_reason[0] = 0;
227 // stop demo loop in case this fails
229 NetConn_ClientFrame();
230 NetConn_ServerFrame();
232 if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
234 cls.connect_trying = true;
235 cls.connect_remainingtries = 3;
236 cls.connect_nextsendtime = 0;
239 NetConn_ClientFrame();
240 NetConn_ServerFrame();
241 NetConn_ClientFrame();
242 NetConn_ServerFrame();
243 NetConn_ClientFrame();
244 NetConn_ServerFrame();
245 NetConn_ClientFrame();
246 NetConn_ServerFrame();
251 Con_Printf("Unable to find a suitable network socket to connect to server.\n");
252 strcpy(m_return_reason, "No network");
261 static void CL_PrintEntities_f(void)
267 for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
269 if (!ent->state_current.active)
272 if (ent->render.model)
273 strlcpy (name, ent->render.model->name, 25);
275 strcpy(name, "--no model--");
276 for (j = strlen(name);j < 25;j++)
278 Con_Printf ("%3i: %s:%04i (%5i %5i %5i) [%3i %3i %3i] %4.2f %5.3f\n", i, name, ent->render.frame, (int) ent->render.origin[0], (int) ent->render.origin[1], (int) ent->render.origin[2], (int) ent->render.angles[0] % 360, (int) ent->render.angles[1] % 360, (int) ent->render.angles[2] % 360, ent->render.scale, ent->render.alpha);
282 //static const vec3_t nomodelmins = {-16, -16, -16};
283 //static const vec3_t nomodelmaxs = {16, 16, 16};
284 void CL_BoundingBoxForEntity(entity_render_t *ent)
288 //if (ent->angles[0] || ent->angles[2])
289 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
292 ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
293 ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
294 ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
295 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
296 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
297 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
298 //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
299 //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
301 //else if (ent->angles[1])
302 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
305 ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
306 ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
307 ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
308 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
309 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
310 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
311 //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
312 //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
316 ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
317 ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
318 ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
319 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
320 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
321 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
322 //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
323 //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
328 ent->mins[0] = ent->matrix.m[0][3] - 16;
329 ent->mins[1] = ent->matrix.m[1][3] - 16;
330 ent->mins[2] = ent->matrix.m[2][3] - 16;
331 ent->maxs[0] = ent->matrix.m[0][3] + 16;
332 ent->maxs[1] = ent->matrix.m[1][3] + 16;
333 ent->maxs[2] = ent->matrix.m[2][3] + 16;
334 //VectorAdd(ent->origin, nomodelmins, ent->mins);
335 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
343 Determines the fraction between the last two messages that the objects
347 static float CL_LerpPoint(void)
351 // dropped packet, or start of demo
352 if (cl.mtime[1] < cl.mtime[0] - 0.1)
353 cl.mtime[1] = cl.mtime[0] - 0.1;
355 cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
357 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
358 f = cl.mtime[0] - cl.mtime[1];
359 if (!f || cl_nolerp.integer || cls.timedemo || cl.islocalgame)
361 cl.time = cl.mtime[0];
365 f = (cl.time - cl.mtime[1]) / f;
366 return bound(0, f, 1);
369 void CL_ClearTempEntities (void)
371 cl_num_temp_entities = 0;
374 entity_t *CL_NewTempEntity(void)
378 if (r_refdef.numentities >= r_refdef.maxentities)
380 if (cl_num_temp_entities >= cl_max_temp_entities)
382 ent = &cl_temp_entities[cl_num_temp_entities++];
383 memset (ent, 0, sizeof(*ent));
384 r_refdef.entities[r_refdef.numentities++] = &ent->render;
386 ent->render.colormap = -1; // no special coloring
387 ent->render.scale = 1;
388 ent->render.alpha = 1;
392 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
396 if (!modelindex) // sanity check
398 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
403 VectorCopy(org, e->origin);
404 e->modelindex = modelindex;
405 e->starttime = cl.time;
406 e->startframe = startframe;
407 e->endframe = startframe + framecount;
408 e->framerate = framerate;
411 e->frame1time = cl.time;
412 e->frame2time = cl.time;
417 void CL_AllocDlight(entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
423 // first look for an exact key match
427 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
433 // then look for anything else
435 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
439 // unable to find one
443 //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
444 memset (dl, 0, sizeof(*dl));
446 CL_FindNonSolidLocation(org, dl->origin, 6);
447 //VectorCopy(org, dl->origin);
450 dl->color[1] = green;
454 dl->die = cl.time + lifetime;
459 void CL_DecayLights(void)
465 time = cl.time - cl.oldtime;
466 for (i = 0, dl = cl_dlights;i < MAX_DLIGHTS;i++, dl++)
468 dl->radius = (cl.time < dl->die) ? max(0, dl->radius - time * dl->decay) : 0;
471 #define MAXVIEWMODELS 32
472 entity_t *viewmodels[MAXVIEWMODELS];
475 matrix4x4_t viewmodelmatrix;
477 static int entitylinkframenumber;
479 static const vec3_t muzzleflashorigin = {18, 0, 0};
481 extern void V_DriftPitch(void);
482 extern void V_FadeViewFlashs(void);
483 extern void V_CalcViewBlend(void);
485 extern void V_CalcRefdef(void);
486 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
487 void CL_LinkNetworkEntity(entity_t *e)
489 matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2;
490 int j, k, l, trailtype, temp;
491 float origin[3], angles[3], delta[3], lerp, dlightcolor[3], mins[3], maxs[3], v[3], v2[3], d;
494 //entity_persistent_t *p = &e->persistent;
495 //entity_render_t *r = &e->render;
496 if (e->persistent.linkframe != entitylinkframenumber)
498 e->persistent.linkframe = entitylinkframenumber;
499 // skip inactive entities and world
500 if (!e->state_current.active || e == cl_entities)
502 if (e->render.flags & RENDER_VIEWMODEL)
504 if (!r_drawviewmodel.integer || chase_active.integer || envmap)
507 CL_LinkNetworkEntity(cl_entities + cl.viewentity);
508 matrix = &viewmodelmatrix;
509 if (e == &cl.viewent && cl.viewentity >= 0 && cl.viewentity < MAX_EDICTS && cl_entities[cl.viewentity].state_current.active)
511 e->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
512 e->state_current.effects = EF_NOSHADOW | (cl_entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT));
517 t = cl_entities + e->state_current.tagentity;
518 if (!t->state_current.active)
520 // note: this can link to world
521 CL_LinkNetworkEntity(t);
522 // make relative to the entity
523 matrix = &t->render.matrix;
524 // if a valid tagindex is used, make it relative to that tag instead
525 if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model) && e->state_current.tagindex <= t->render.model->alias.aliasnum_tags)
527 // blend the matrices
528 memset(&blendmatrix, 0, sizeof(blendmatrix));
529 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
531 matrix = &t->render.model->alias.aliasdata_tags[t->render.frameblend[j].frame * t->render.model->alias.aliasnum_tags + (e->state_current.tagindex - 1)].matrix;
532 d = t->render.frameblend[j].lerp;
533 for (l = 0;l < 4;l++)
534 for (k = 0;k < 4;k++)
535 blendmatrix.m[l][k] += d * matrix->m[l][k];
537 // concat the tag matrices onto the entity matrix
538 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
539 // use the constructed tag matrix
540 matrix = &tempmatrix;
543 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
544 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
545 e->render.flags = e->state_current.flags;
546 if (e - cl_entities == cl.viewentity)
547 e->render.flags |= RENDER_EXTERIORMODEL;
548 e->render.effects = e->state_current.effects;
549 if (e->state_current.flags & RENDER_COLORMAPPED)
550 e->render.colormap = e->state_current.colormap;
551 else if (cl.scores != NULL && e->state_current.colormap)
552 e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
554 e->render.colormap = -1; // no special coloring
555 e->render.skinnum = e->state_current.skin;
556 // set up the render matrix
557 if (e->state_previous.active && e->state_current.modelindex == e->state_previous.modelindex)
560 if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
562 // interpolate the origin and angles
563 VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
564 VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
565 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
566 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
567 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
568 VectorMA(e->persistent.oldangles, lerp, delta, angles);
573 VectorCopy(e->persistent.neworigin, origin);
574 VectorCopy(e->persistent.newangles, angles);
577 if (e->render.frame2 == e->state_current.frame)
579 // update frame lerp fraction
580 e->render.framelerp = 1;
581 if (e->render.frame2time > e->render.frame1time)
583 e->render.framelerp = (cl.time - e->render.frame2time) / (e->render.frame2time - e->render.frame1time);
584 e->render.framelerp = bound(0, e->render.framelerp, 1);
589 // begin a new frame lerp
590 e->render.frame1 = e->render.frame2;
591 e->render.frame1time = e->render.frame2time;
592 e->render.frame = e->render.frame2 = e->state_current.frame;
593 e->render.frame2time = cl.time;
594 e->render.framelerp = 0;
595 // make sure frame lerp won't last longer than 100ms
596 // (this mainly helps with models that use framegroups and
597 // switch between them infrequently)
598 e->render.frame1time = max(e->render.frame1time, e->render.frame2time - 0.1f);
604 VectorCopy(e->persistent.neworigin, origin);
605 VectorCopy(e->persistent.newangles, angles);
606 e->render.frame = e->render.frame1 = e->render.frame2 = e->state_current.frame;
607 e->render.frame1time = e->render.frame2time = cl.time;
608 e->render.framelerp = 1;
611 e->render.model = cl.model_precache[e->state_current.modelindex];
614 Mod_CheckLoaded(e->render.model);
615 if (e->render.model->type == mod_alias || e->render.model->type == mod_sprite)
616 angles[0] = -angles[0];
617 if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
619 angles[1] = ANGLEMOD(100*cl.time);
620 if (cl_itembobheight.value)
621 origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
625 R_LerpAnimation(&e->render);
627 // FIXME: e->render.scale should go away
628 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
629 // concat the matrices to make the entity relative to its tag
630 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
631 // make the other useful stuff
632 Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
633 CL_BoundingBoxForEntity(&e->render);
635 // handle effects now that we know where this entity is in the world...
636 origin[0] = e->render.matrix.m[0][3];
637 origin[1] = e->render.matrix.m[1][3];
638 origin[2] = e->render.matrix.m[2][3];
643 // LordHavoc: if the entity has no effects, don't check each
644 if (e->render.effects)
646 if (e->render.effects & EF_BRIGHTFIELD)
648 if (gamemode == GAME_NEXUIZ)
650 dlightcolor[0] += 100.0f;
651 dlightcolor[1] += 200.0f;
652 dlightcolor[2] += 400.0f;
656 CL_EntityParticles(e);
658 if (e->render.effects & EF_MUZZLEFLASH)
659 e->persistent.muzzleflash = 100.0f;
660 if (e->render.effects & EF_DIMLIGHT)
662 dlightcolor[0] += 200.0f;
663 dlightcolor[1] += 200.0f;
664 dlightcolor[2] += 200.0f;
666 if (e->render.effects & EF_BRIGHTLIGHT)
668 dlightcolor[0] += 400.0f;
669 dlightcolor[1] += 400.0f;
670 dlightcolor[2] += 400.0f;
672 // LordHavoc: more effects
673 if (e->render.effects & EF_RED) // red
675 dlightcolor[0] += 200.0f;
676 dlightcolor[1] += 20.0f;
677 dlightcolor[2] += 20.0f;
679 if (e->render.effects & EF_BLUE) // blue
681 dlightcolor[0] += 20.0f;
682 dlightcolor[1] += 20.0f;
683 dlightcolor[2] += 200.0f;
685 if (e->render.effects & EF_FLAME)
687 mins[0] = origin[0] - 16.0f;
688 mins[1] = origin[1] - 16.0f;
689 mins[2] = origin[2] - 16.0f;
690 maxs[0] = origin[0] + 16.0f;
691 maxs[1] = origin[1] + 16.0f;
692 maxs[2] = origin[2] + 16.0f;
693 // how many flames to make
694 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
695 CL_FlameCube(mins, maxs, temp);
696 d = lhrandom(200, 250);
697 dlightcolor[0] += d * 1.0f;
698 dlightcolor[1] += d * 0.7f;
699 dlightcolor[2] += d * 0.3f;
701 if (e->render.effects & EF_STARDUST)
703 mins[0] = origin[0] - 16.0f;
704 mins[1] = origin[1] - 16.0f;
705 mins[2] = origin[2] - 16.0f;
706 maxs[0] = origin[0] + 16.0f;
707 maxs[1] = origin[1] + 16.0f;
708 maxs[2] = origin[2] + 16.0f;
709 // how many particles to make
710 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
711 CL_Stardust(mins, maxs, temp);
713 dlightcolor[0] += d * 1.0f;
714 dlightcolor[1] += d * 0.7f;
715 dlightcolor[2] += d * 0.3f;
718 // muzzleflash fades over time, and is offset a bit
719 if (e->persistent.muzzleflash > 0)
721 Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
722 CL_TraceLine(origin, v2, v, NULL, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY);
723 CL_AllocDlight(NULL, v, e->persistent.muzzleflash, 1, 1, 1, 0, 0);
724 e->persistent.muzzleflash -= cl.frametime * 1000;
726 // LordHavoc: if the model has no flags, don't check each
727 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
729 if (e->render.model->flags & EF_GIB)
731 else if (e->render.model->flags & EF_ZOMGIB)
733 else if (e->render.model->flags & EF_TRACER)
736 dlightcolor[0] += 0x10;
737 dlightcolor[1] += 0x40;
738 dlightcolor[2] += 0x10;
740 else if (e->render.model->flags & EF_TRACER2)
743 dlightcolor[0] += 0x50;
744 dlightcolor[1] += 0x30;
745 dlightcolor[2] += 0x10;
747 else if (e->render.model->flags & EF_ROCKET)
750 dlightcolor[0] += 200.0f;
751 dlightcolor[1] += 160.0f;
752 dlightcolor[2] += 80.0f;
754 else if (e->render.model->flags & EF_GRENADE)
756 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
757 trailtype = e->render.alpha == -1 ? 7 : 1;
759 else if (e->render.model->flags & EF_TRACER3)
762 dlightcolor[0] += 0x50;
763 dlightcolor[1] += 0x20;
764 dlightcolor[2] += 0x40;
767 // LordHavoc: customizable glow
768 if (e->state_current.glowsize)
770 // * 4 for the expansion from 0-255 to 0-1023 range,
771 // / 255 to scale down byte colors
772 VectorMA(dlightcolor, e->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[e->state_current.glowcolor], dlightcolor);
775 if ((dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL) && !e->state_current.tagentity)
777 VectorCopy(origin, v);
778 // hack to make glowing player light shine on their gun
779 if ((e - cl_entities) == cl.viewentity/* && !chase_active.integer*/)
781 CL_AllocDlight(&e->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
783 // trails need the previous frame
784 if (e->state_previous.active && e->state_previous.modelindex == e->state_current.modelindex)
786 if (e->render.flags & RENDER_GLOWTRAIL)
787 CL_RocketTrail2(e->persistent.trail_origin, origin, e->state_current.glowcolor, e);
788 else if (trailtype >= 0)
789 CL_RocketTrail(e->persistent.trail_origin, origin, trailtype, e);
791 VectorCopy(origin, e->persistent.trail_origin);
792 // note: the cl.viewentity and intermission check is to hide player
793 // shadow during intermission and during the Nehahra movie and
794 // Nehahra cinematics
795 if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE))
796 && (e->render.alpha == 1)
797 && !(e->render.flags & RENDER_VIEWMODEL)
798 && ((e - cl_entities) != cl.viewentity || (!cl.intermission && cl.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
799 e->render.flags |= RENDER_SHADOW;
800 // as soon as player is known we can call V_CalcRefDef
801 if ((e - cl_entities) == cl.viewentity)
803 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
804 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &e->render;
805 // don't show entities with no modelindex (note: this still shows
806 // entities which have a modelindex that resolved to a NULL model)
807 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
808 r_refdef.entities[r_refdef.numentities++] = &e->render;
809 if (cl_num_entities < e->state_current.number + 1)
810 cl_num_entities = e->state_current.number + 1;
814 void CL_RelinkWorld(void)
816 entity_t *ent = &cl_entities[0];
817 if (cl_num_entities < 1)
819 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
820 // FIXME: this should be done at load
821 Matrix4x4_CreateIdentity(&ent->render.matrix);
822 Matrix4x4_CreateIdentity(&ent->render.inversematrix);
823 CL_BoundingBoxForEntity(&ent->render);
826 static void CL_RelinkStaticEntities(void)
829 for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
831 Mod_CheckLoaded(cl_static_entities[i].render.model);
832 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
841 static void CL_RelinkNetworkEntities(void)
847 ent->state_previous = ent->state_current;
848 ClearStateToDefault(&ent->state_current);
849 ent->state_current.time = cl.time;
850 ent->state_current.number = -1;
851 ent->state_current.active = true;
852 ent->state_current.modelindex = cl.stats[STAT_WEAPON];
853 ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
854 ent->state_current.flags = RENDER_VIEWMODEL;
855 if (cl.stats[STAT_HEALTH] <= 0 || cl.intermission)
856 ent->state_current.modelindex = 0;
857 else if (cl.items & IT_INVISIBILITY)
859 if (gamemode == GAME_TRANSFUSION)
860 ent->state_current.alpha = 128;
862 ent->state_current.modelindex = 0;
865 // start on the entity after the world
866 entitylinkframenumber++;
867 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
869 if (cl_entities_active[i])
871 if (ent->state_current.active)
872 CL_LinkNetworkEntity(ent);
874 cl_entities_active[i] = false;
877 CL_LinkNetworkEntity(&cl.viewent);
880 static void CL_RelinkEffects(void)
887 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
891 frame = (cl.time - e->starttime) * e->framerate + e->startframe;
893 if (intframe < 0 || intframe >= e->endframe)
895 memset(e, 0, sizeof(*e));
899 if (intframe != e->frame)
902 e->frame1time = e->frame2time;
903 e->frame2time = cl.time;
906 // if we're drawing effects, get a new temp entity
907 // (NewTempEntity adds it to the render entities list for us)
908 if (r_draweffects.integer && (ent = CL_NewTempEntity()))
910 // interpolation stuff
911 ent->render.frame1 = intframe;
912 ent->render.frame2 = intframe + 1;
913 if (ent->render.frame2 >= e->endframe)
914 ent->render.frame2 = -1; // disappear
915 ent->render.framelerp = frame - intframe;
916 ent->render.frame1time = e->frame1time;
917 ent->render.frame2time = e->frame2time;
920 ent->render.model = cl.model_precache[e->modelindex];
921 ent->render.frame = ent->render.frame2;
922 ent->render.colormap = -1; // no special coloring
923 ent->render.alpha = 1;
925 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
926 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
927 CL_BoundingBoxForEntity(&ent->render);
933 void CL_RelinkBeams(void)
943 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
945 if (!b->model || b->endtime < cl.time)
948 // if coming from the player, update the start position
949 //if (b->entity == cl.viewentity)
950 // VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
951 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
953 entity_state_t *p = &cl_entities[b->entity].state_previous;
954 //entity_state_t *c = &cl_entities[b->entity].state_current;
955 entity_render_t *r = &cl_entities[b->entity].render;
956 matrix4x4_t matrix, imatrix;
957 if (b->relativestartvalid == 2)
959 // not really valid yet, we need to get the orientation now
960 // (ParseBeam flagged this because it is received before
961 // entities are received, by now they have been received)
962 // note: because players create lightning in their think
963 // function (which occurs before movement), they actually
964 // have some lag in it's location, so compare to the
965 // previous player state, not the latest
966 if (b->entity == cl.viewentity)
967 Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
969 Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
970 Matrix4x4_Invert_Simple(&imatrix, &matrix);
971 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
972 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
973 b->relativestartvalid = 1;
977 if (b->entity == cl.viewentity)
978 Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
980 Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
981 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
982 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
988 if (cl_beams_lightatend.integer)
989 CL_AllocDlight (NULL, b->end, 200, 0.3, 0.7, 1, 0, 0);
990 if (cl_beams_polygons.integer)
994 // calculate pitch and yaw
995 VectorSubtract (b->end, b->start, dist);
997 if (dist[1] == 0 && dist[0] == 0)
1007 yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1011 forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1012 pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1017 // add new entities for the lightning
1018 VectorCopy (b->start, org);
1019 d = VectorNormalizeLength(dist);
1022 ent = CL_NewTempEntity ();
1025 //VectorCopy (org, ent->render.origin);
1026 ent->render.model = b->model;
1027 ent->render.effects = EF_FULLBRIGHT;
1028 //ent->render.angles[0] = pitch;
1029 //ent->render.angles[1] = yaw;
1030 //ent->render.angles[2] = rand()%360;
1031 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1032 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1033 CL_BoundingBoxForEntity(&ent->render);
1034 VectorMA(org, 30, dist, org);
1040 void CL_LerpPlayer(float frac)
1045 cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1047 for (i = 0;i < 3;i++)
1048 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1050 if (cls.demoplayback)
1052 // interpolate the angles
1053 for (i = 0;i < 3;i++)
1055 d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1060 cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1069 Read all incoming data from the server
1072 int CL_ReadFromServer(void)
1074 CL_ReadDemoMessage();
1076 r_refdef.numentities = 0;
1077 cl_num_entities = 0;
1078 cl_num_brushmodel_entities = 0;
1080 if (cls.state == ca_connected && cls.signon == SIGNONS)
1082 // prepare for a new frame
1083 CL_LerpPlayer(CL_LerpPoint());
1085 CL_ClearTempEntities();
1089 // relink network entities (note: this sets up the view!)
1090 CL_RelinkNetworkEntities();
1097 CL_RelinkStaticEntities();
1101 // run cgame code (which can add more entities)
1104 // update view blend
1116 void CL_SendCmd(usercmd_t *cmd)
1118 if (cls.signon == SIGNONS)
1121 if (cls.demoplayback)
1123 SZ_Clear(&cls.message);
1127 // send the reliable message (forwarded commands) if there is one
1128 if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1130 if (developer.integer)
1132 Con_Printf("CL_SendCmd: sending reliable message:\n");
1133 SZ_HexDumpToConsole(&cls.message);
1135 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1136 Host_Error("CL_WriteToServer: lost server connection");
1137 SZ_Clear(&cls.message);
1141 // LordHavoc: pausedemo command
1142 static void CL_PauseDemo_f (void)
1144 cls.demopaused = !cls.demopaused;
1146 Con_Printf("Demo paused\n");
1148 Con_Printf("Demo unpaused\n");
1152 ======================
1154 ======================
1156 static void CL_Fog_f (void)
1158 if (Cmd_Argc () == 1)
1160 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1163 fog_density = atof(Cmd_Argv(1));
1164 fog_red = atof(Cmd_Argv(2));
1165 fog_green = atof(Cmd_Argv(3));
1166 fog_blue = atof(Cmd_Argv(4));
1176 cl_entities_mempool = Mem_AllocPool("client entities");
1177 cl_refdef_mempool = Mem_AllocPool("refdef");
1179 memset(&r_refdef, 0, sizeof(r_refdef));
1180 // max entities sent to renderer per frame
1181 r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1182 r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1183 // 256k drawqueue buffer
1184 r_refdef.maxdrawqueuesize = 256 * 1024;
1185 r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1187 SZ_Alloc (&cls.message, 1024, "cls.message");
1193 // register our commands
1195 Cvar_RegisterVariable (&cl_upspeed);
1196 Cvar_RegisterVariable (&cl_forwardspeed);
1197 Cvar_RegisterVariable (&cl_backspeed);
1198 Cvar_RegisterVariable (&cl_sidespeed);
1199 Cvar_RegisterVariable (&cl_movespeedkey);
1200 Cvar_RegisterVariable (&cl_yawspeed);
1201 Cvar_RegisterVariable (&cl_pitchspeed);
1202 Cvar_RegisterVariable (&cl_anglespeedkey);
1203 Cvar_RegisterVariable (&cl_shownet);
1204 Cvar_RegisterVariable (&cl_nolerp);
1205 Cvar_RegisterVariable (&lookspring);
1206 Cvar_RegisterVariable (&lookstrafe);
1207 Cvar_RegisterVariable (&sensitivity);
1208 Cvar_RegisterVariable (&freelook);
1210 Cvar_RegisterVariable (&m_pitch);
1211 Cvar_RegisterVariable (&m_yaw);
1212 Cvar_RegisterVariable (&m_forward);
1213 Cvar_RegisterVariable (&m_side);
1215 Cvar_RegisterVariable (&cl_itembobspeed);
1216 Cvar_RegisterVariable (&cl_itembobheight);
1218 Cmd_AddCommand ("entities", CL_PrintEntities_f);
1219 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1220 Cmd_AddCommand ("record", CL_Record_f);
1221 Cmd_AddCommand ("stop", CL_Stop_f);
1222 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1223 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1225 Cmd_AddCommand ("fog", CL_Fog_f);
1227 // LordHavoc: added pausedemo
1228 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1230 Cvar_RegisterVariable(&r_draweffects);
1231 Cvar_RegisterVariable(&cl_explosions);
1232 Cvar_RegisterVariable(&cl_stainmaps);
1233 Cvar_RegisterVariable(&cl_beams_polygons);
1234 Cvar_RegisterVariable(&cl_beams_relative);
1235 Cvar_RegisterVariable(&cl_beams_lightatend);
1236 Cvar_RegisterVariable(&cl_noplayershadow);
1239 CL_Particles_Init();