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 Mem_EmptyPool(cl_entities_mempool);
103 // wipe the entire cl structure
104 memset (&cl, 0, sizeof(cl));
106 SZ_Clear (&cls.message);
109 cl_num_static_entities = 0;
110 cl_num_temp_entities = 0;
111 cl_num_brushmodel_entities = 0;
113 // tweak these if the game runs out
114 cl_max_entities = MAX_EDICTS;
115 cl_max_static_entities = 256;
116 cl_max_temp_entities = 512;
117 cl_max_effects = 256;
119 cl_max_dlights = MAX_DLIGHTS;
120 cl_max_lightstyle = MAX_LIGHTSTYLES;
121 cl_max_brushmodel_entities = MAX_EDICTS;
123 cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
124 cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
125 cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
126 cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
127 cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
128 cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
129 cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
130 cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
131 cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
135 CL_Particles_Clear();
137 // LordHavoc: have to set up the baseline info for alpha and other stuff
138 for (i = 0;i < cl_max_entities;i++)
140 ClearStateToDefault(&cl_entities[i].state_baseline);
141 ClearStateToDefault(&cl_entities[i].state_previous);
142 ClearStateToDefault(&cl_entities[i].state_current);
149 =====================
152 Sends a disconnect message to the server
153 This is also called on Host_Error, so it shouldn't cause any errors
154 =====================
156 void CL_Disconnect(void)
158 if (cls.state == ca_dedicated)
161 // stop sounds (especially looping!)
162 S_StopAllSounds (true);
164 // clear contents blends
165 cl.cshifts[0].percent = 0;
166 cl.cshifts[1].percent = 0;
167 cl.cshifts[2].percent = 0;
168 cl.cshifts[3].percent = 0;
170 cl.worldmodel = NULL;
172 if (cls.demoplayback)
176 if (cls.demorecording)
179 Con_DPrintf("Sending clc_disconnect\n");
180 SZ_Clear(&cls.message);
181 MSG_WriteByte(&cls.message, clc_disconnect);
182 NetConn_SendUnreliableMessage(cls.netcon, &cls.message);
183 SZ_Clear(&cls.message);
184 NetConn_Close(cls.netcon);
186 // if running a local server, shut it down
189 // prevent this code from executing again during Host_ShutdownServer
190 cls.state = ca_disconnected;
191 Host_ShutdownServer(false);
194 cls.state = ca_disconnected;
196 cls.demoplayback = cls.timedemo = false;
200 void CL_Disconnect_f(void)
204 Host_ShutdownServer (false);
211 =====================
212 CL_EstablishConnection
214 Host should be either "local" or a net address
215 =====================
217 void CL_EstablishConnection(const char *host)
219 if (cls.state == ca_dedicated)
222 // clear menu's connect error message
223 m_return_reason[0] = 0;
225 // stop demo loop in case this fails
229 if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
231 cls.connect_trying = true;
232 cls.connect_remainingtries = 3;
233 cls.connect_nextsendtime = 0;
236 NetConn_ClientFrame();
237 NetConn_ServerFrame();
238 NetConn_ClientFrame();
239 NetConn_ServerFrame();
240 NetConn_ClientFrame();
241 NetConn_ServerFrame();
242 NetConn_ClientFrame();
243 NetConn_ServerFrame();
253 static void CL_PrintEntities_f(void)
259 for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
261 if (!ent->state_current.active)
264 if (ent->render.model)
265 strncpy(name, ent->render.model->name, 25);
267 strcpy(name, "--no model--");
269 for (j = strlen(name);j < 25;j++)
271 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);
275 //static const vec3_t nomodelmins = {-16, -16, -16};
276 //static const vec3_t nomodelmaxs = {16, 16, 16};
277 void CL_BoundingBoxForEntity(entity_render_t *ent)
281 //if (ent->angles[0] || ent->angles[2])
282 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
285 ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
286 ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
287 ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
288 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
289 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
290 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
291 //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
292 //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
294 //else if (ent->angles[1])
295 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
298 ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
299 ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
300 ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
301 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
302 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
303 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
304 //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
305 //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
309 ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
310 ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
311 ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
312 ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
313 ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
314 ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
315 //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
316 //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
321 ent->mins[0] = ent->matrix.m[0][3] - 16;
322 ent->mins[1] = ent->matrix.m[1][3] - 16;
323 ent->mins[2] = ent->matrix.m[2][3] - 16;
324 ent->maxs[0] = ent->matrix.m[0][3] + 16;
325 ent->maxs[1] = ent->matrix.m[1][3] + 16;
326 ent->maxs[2] = ent->matrix.m[2][3] + 16;
327 //VectorAdd(ent->origin, nomodelmins, ent->mins);
328 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
336 Determines the fraction between the last two messages that the objects
340 static float CL_LerpPoint(void)
344 // dropped packet, or start of demo
345 if (cl.mtime[1] < cl.mtime[0] - 0.1)
346 cl.mtime[1] = cl.mtime[0] - 0.1;
348 cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
350 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
351 f = cl.mtime[0] - cl.mtime[1];
352 if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
354 cl.time = cl.mtime[0];
358 f = (cl.time - cl.mtime[1]) / f;
359 return bound(0, f, 1);
362 void CL_ClearTempEntities (void)
364 cl_num_temp_entities = 0;
367 entity_t *CL_NewTempEntity(void)
371 if (r_refdef.numentities >= r_refdef.maxentities)
373 if (cl_num_temp_entities >= cl_max_temp_entities)
375 ent = &cl_temp_entities[cl_num_temp_entities++];
376 memset (ent, 0, sizeof(*ent));
377 r_refdef.entities[r_refdef.numentities++] = &ent->render;
379 ent->render.colormap = -1; // no special coloring
380 ent->render.scale = 1;
381 ent->render.alpha = 1;
385 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
389 if (!modelindex) // sanity check
391 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
396 VectorCopy(org, e->origin);
397 e->modelindex = modelindex;
398 e->starttime = cl.time;
399 e->startframe = startframe;
400 e->endframe = startframe + framecount;
401 e->framerate = framerate;
404 e->frame1time = cl.time;
405 e->frame2time = cl.time;
410 void CL_AllocDlight(entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
416 // first look for an exact key match
420 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
426 // then look for anything else
428 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
432 // unable to find one
436 //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
437 memset (dl, 0, sizeof(*dl));
439 CL_FindNonSolidLocation(org, dl->origin, 6);
440 //VectorCopy(org, dl->origin);
443 dl->color[1] = green;
447 dl->die = cl.time + lifetime;
452 void CL_DecayLights(void)
458 time = cl.time - cl.oldtime;
461 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
465 if (dl->die < cl.time)
471 dl->radius -= time*dl->decay;
477 extern qboolean Nehahrademcompatibility;
478 #define MAXVIEWMODELS 32
479 entity_t *viewmodels[MAXVIEWMODELS];
481 void CL_RelinkNetworkEntity(entity_t *e)
484 float oldorg[3], delta[3], lerp, dlightcolor[3], mins[3], maxs[3], v[3], v2[3], d;
485 entity_persistent_t *p;
490 if (e->state_previous.active && e->state_current.modelindex == e->state_previous.modelindex)
493 if (p->lerpdeltatime > 0 && (lerp = (cl.time - p->lerpstarttime) / p->lerpdeltatime) < 1)
495 // read the old origin
496 VectorCopy(r->origin, oldorg);
497 // interpolate the origin and angles
498 r->origin[0] = p->oldorigin[0] + lerp * (p->neworigin[0] - p->oldorigin[0]);
499 r->origin[1] = p->oldorigin[1] + lerp * (p->neworigin[1] - p->oldorigin[1]);
500 r->origin[2] = p->oldorigin[2] + lerp * (p->neworigin[2] - p->oldorigin[2]);
501 VectorSubtract(p->newangles, p->oldangles, delta);
502 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
503 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
504 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
505 VectorMA(p->oldangles, lerp, delta, r->angles);
510 VectorCopy(p->neworigin, r->origin);
511 VectorCopy(p->neworigin, oldorg);
512 VectorCopy(p->newangles, r->angles);
515 if (r->frame2 != e->state_current.frame)
517 // begin a new frame lerp
518 r->frame1 = r->frame2;
519 r->frame1time = r->frame2time;
520 r->frame = r->frame2 = e->state_current.frame;
521 r->frame2time = cl.time;
526 // update frame lerp fraction
527 r->framelerp = r->frame2time > r->frame1time ? ((cl.time - r->frame2time) / (r->frame2time - r->frame1time)) : 1;
528 r->framelerp = bound(0, r->framelerp, 1);
534 VectorCopy(p->neworigin, r->origin);
535 VectorCopy(p->neworigin, oldorg);
536 VectorCopy(p->newangles, r->angles);
537 r->frame = r->frame1 = r->frame2 = e->state_current.frame;
538 r->frame1time = r->frame2time = cl.time;
542 r->alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
543 r->scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
544 r->flags = e->state_current.flags;
545 if (e - cl_entities == cl.viewentity)
546 r->flags |= RENDER_EXTERIORMODEL;
547 r->effects = e->state_current.effects;
548 if (e->state_current.flags & RENDER_COLORMAPPED)
549 r->colormap = e->state_current.colormap;
550 else if (cl.scores != NULL && e->state_current.colormap)
551 r->colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
553 r->colormap = -1; // no special coloring
554 r->skinnum = e->state_current.skin;
556 // handle effects now...
562 // LordHavoc: if the entity has no effects, don't check each
565 if (r->effects & EF_BRIGHTFIELD)
567 if (gamemode == GAME_NEXUIZ)
569 dlightcolor[0] += 100.0f;
570 dlightcolor[1] += 200.0f;
571 dlightcolor[2] += 400.0f;
575 CL_EntityParticles(e);
577 if (r->effects & EF_MUZZLEFLASH)
578 p->muzzleflash = 100.0f;
579 if (r->effects & EF_DIMLIGHT)
581 dlightcolor[0] += 200.0f;
582 dlightcolor[1] += 200.0f;
583 dlightcolor[2] += 200.0f;
585 if (r->effects & EF_BRIGHTLIGHT)
587 dlightcolor[0] += 400.0f;
588 dlightcolor[1] += 400.0f;
589 dlightcolor[2] += 400.0f;
591 // LordHavoc: added EF_RED and EF_BLUE
592 if (r->effects & EF_RED) // red
594 dlightcolor[0] += 200.0f;
595 dlightcolor[1] += 20.0f;
596 dlightcolor[2] += 20.0f;
598 if (r->effects & EF_BLUE) // blue
600 dlightcolor[0] += 20.0f;
601 dlightcolor[1] += 20.0f;
602 dlightcolor[2] += 200.0f;
604 if (r->effects & EF_FLAME)
606 mins[0] = r->origin[0] - 16.0f;
607 mins[1] = r->origin[1] - 16.0f;
608 mins[2] = r->origin[2] - 16.0f;
609 maxs[0] = r->origin[0] + 16.0f;
610 maxs[1] = r->origin[1] + 16.0f;
611 maxs[2] = r->origin[2] + 16.0f;
612 // how many flames to make
613 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
614 CL_FlameCube(mins, maxs, temp);
615 d = lhrandom(200, 250);
616 dlightcolor[0] += d * 1.0f;
617 dlightcolor[1] += d * 0.7f;
618 dlightcolor[2] += d * 0.3f;
620 if (r->effects & EF_STARDUST)
622 mins[0] = r->origin[0] - 16.0f;
623 mins[1] = r->origin[1] - 16.0f;
624 mins[2] = r->origin[2] - 16.0f;
625 maxs[0] = r->origin[0] + 16.0f;
626 maxs[1] = r->origin[1] + 16.0f;
627 maxs[2] = r->origin[2] + 16.0f;
628 // how many particles to make
629 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
630 CL_Stardust(mins, maxs, temp);
632 dlightcolor[0] += d * 1.0f;
633 dlightcolor[1] += d * 0.7f;
634 dlightcolor[2] += d * 0.3f;
638 r->model = cl.model_precache[e->state_current.modelindex];
641 Mod_CheckLoaded(r->model);
642 if (r->model->type != mod_brush)
643 r->angles[0] = -r->angles[0];
644 // LordHavoc: if the model has no flags, don't check each
645 if (r->model->flags & EF_ROTATE)
647 r->angles[1] = ANGLEMOD(100*cl.time);
648 if (cl_itembobheight.value)
649 r->origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
653 Matrix4x4_CreateFromQuakeEntity(&r->matrix, r->origin[0], r->origin[1], r->origin[2], r->angles[0], r->angles[1], r->angles[2], r->scale);
654 Matrix4x4_Invert_Simple(&r->inversematrix, &r->matrix);
655 CL_BoundingBoxForEntity(&e->render);
657 // LordHavoc: if the model has no flags, don't check each
658 if (r->model && r->model->flags)
660 if (r->model->flags & EF_GIB)
662 else if (r->model->flags & EF_ZOMGIB)
664 else if (r->model->flags & EF_TRACER)
667 dlightcolor[0] += 0x10;
668 dlightcolor[1] += 0x40;
669 dlightcolor[2] += 0x10;
671 else if (r->model->flags & EF_TRACER2)
674 dlightcolor[0] += 0x50;
675 dlightcolor[1] += 0x30;
676 dlightcolor[2] += 0x10;
678 else if (r->model->flags & EF_ROCKET)
681 dlightcolor[0] += 200.0f;
682 dlightcolor[1] += 160.0f;
683 dlightcolor[2] += 80.0f;
685 else if (r->model->flags & EF_GRENADE)
687 // LordHavoc: r->alpha == -1 is for Nehahra dem compatibility (cigar smoke)
688 trailtype = r->alpha == -1 ? 7 : 1;
690 else if (r->model->flags & EF_TRACER3)
693 dlightcolor[0] += 0x50;
694 dlightcolor[1] += 0x20;
695 dlightcolor[2] += 0x40;
699 // LordHavoc: customizable glow
700 if (e->state_current.glowsize)
702 // * 4 for the expansion from 0-255 to 0-1023 range,
703 // / 255 to scale down byte colors
704 VectorMA(dlightcolor, e->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[e->state_current.glowcolor], dlightcolor);
707 // trails need a previous frame...
708 if (e->state_previous.active)
710 // LordHavoc: customizable trail
711 if (r->flags & RENDER_GLOWTRAIL)
712 CL_RocketTrail2(oldorg, r->origin, e->state_current.glowcolor, e);
713 else if (trailtype >= 0)
714 CL_RocketTrail(oldorg, r->origin, trailtype, e);
717 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
719 VectorCopy(r->origin, v);
720 // hack to make glowing player light shine on their gun
721 if ((e - cl_entities) == cl.viewentity/* && !chase_active.integer*/)
723 CL_AllocDlight(r, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
726 if (p->muzzleflash > 0)
728 v2[0] = r->matrix.m[0][0] * 18 + r->matrix.m[0][3];
729 v2[1] = r->matrix.m[0][1] * 18 + r->matrix.m[1][3];
730 v2[2] = r->matrix.m[0][2] * 18 + r->matrix.m[2][3];
731 CL_TraceLine(r->origin, v2, v, NULL, 0, true, NULL);
733 CL_AllocDlight(NULL, v, p->muzzleflash, 1, 1, 1, 0, 0);
734 p->muzzleflash -= cl.frametime * 1000;
737 // note: the cl.viewentity and intermission check is to hide player
738 // shadow during intermission and during the Nehahra movie and
739 // Nehahra cinematics
740 if (!(r->effects & (EF_NOSHADOW | EF_ADDITIVE))
742 && !(r->flags & RENDER_VIEWMODEL)
743 && ((e - cl_entities) != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility && !cl_noplayershadow.integer)))
744 r->flags |= RENDER_SHADOW;
746 // don't show entities with no modelindex (note: this still shows
747 // entities which have a modelindex that resolved to a NULL model)
748 if (r->model && !(r->effects & EF_NODRAW))
750 if (r->flags & RENDER_VIEWMODEL)
752 // store a list of view-relative entities for later adjustment in view code
753 if (numviewmodels < MAXVIEWMODELS)
754 viewmodels[numviewmodels++] = e;
758 if (r->model->name[0] == '*' && r->model->type == mod_brush)
759 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &e->render;
760 if (r_refdef.numentities < r_refdef.maxentities)
761 r_refdef.entities[r_refdef.numentities++] = &e->render;
762 if (cl_num_entities < e->state_current.number + 1)
763 cl_num_entities = e->state_current.number + 1;
768 void CL_RelinkWorld (void)
770 entity_t *ent = &cl_entities[0];
771 if (cl_num_entities < 1)
773 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
774 Matrix4x4_CreateIdentity(&ent->render.matrix);
775 Matrix4x4_CreateIdentity(&ent->render.inversematrix);
776 CL_BoundingBoxForEntity(&ent->render);
779 static void CL_RelinkStaticEntities(void)
782 for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
784 Mod_CheckLoaded(cl_static_entities[i].render.model);
785 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
794 static void CL_RelinkNetworkEntities(void)
799 // start on the entity after the world
800 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
802 if (cl_entities_active[i])
804 if (ent->state_current.active)
805 CL_RelinkNetworkEntity(ent);
807 cl_entities_active[i] = false;
812 static void CL_RelinkEffects(void)
819 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
823 frame = (cl.time - e->starttime) * e->framerate + e->startframe;
825 if (intframe < 0 || intframe >= e->endframe)
827 memset(e, 0, sizeof(*e));
831 if (intframe != e->frame)
834 e->frame1time = e->frame2time;
835 e->frame2time = cl.time;
838 // if we're drawing effects, get a new temp entity
839 // (NewTempEntity adds it to the render entities list for us)
840 if (r_draweffects.integer && (ent = CL_NewTempEntity()))
842 // interpolation stuff
843 ent->render.frame1 = intframe;
844 ent->render.frame2 = intframe + 1;
845 if (ent->render.frame2 >= e->endframe)
846 ent->render.frame2 = -1; // disappear
847 ent->render.framelerp = frame - intframe;
848 ent->render.frame1time = e->frame1time;
849 ent->render.frame2time = e->frame2time;
852 ent->render.model = cl.model_precache[e->modelindex];
853 ent->render.frame = ent->render.frame2;
854 ent->render.colormap = -1; // no special coloring
855 ent->render.alpha = 1;
857 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
858 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
859 CL_BoundingBoxForEntity(&ent->render);
865 void CL_RelinkBeams(void)
875 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
877 if (!b->model || b->endtime < cl.time)
880 // if coming from the player, update the start position
881 //if (b->entity == cl.viewentity)
882 // VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
883 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
885 entity_state_t *p = &cl_entities[b->entity].state_previous;
886 //entity_state_t *c = &cl_entities[b->entity].state_current;
887 entity_render_t *r = &cl_entities[b->entity].render;
888 matrix4x4_t matrix, imatrix;
889 if (b->relativestartvalid == 2)
891 // not really valid yet, we need to get the orientation now
892 // (ParseBeam flagged this because it is received before
893 // entities are received, by now they have been received)
894 // note: because players create lightning in their think
895 // function (which occurs before movement), they actually
896 // have some lag in it's location, so compare to the
897 // previous player state, not the latest
898 if (b->entity == cl.viewentity)
899 Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
901 Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
902 Matrix4x4_Invert_Simple(&imatrix, &matrix);
903 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
904 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
905 b->relativestartvalid = 1;
909 if (b->entity == cl.viewentity)
910 Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
912 Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
913 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
914 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
920 if (cl_beams_lightatend.integer)
921 CL_AllocDlight (NULL, b->end, 200, 0.3, 0.7, 1, 0, 0);
922 if (cl_beams_polygons.integer)
926 // calculate pitch and yaw
927 VectorSubtract (b->end, b->start, dist);
929 if (dist[1] == 0 && dist[0] == 0)
939 yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
943 forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
944 pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
949 // add new entities for the lightning
950 VectorCopy (b->start, org);
951 d = VectorNormalizeLength(dist);
954 ent = CL_NewTempEntity ();
957 //VectorCopy (org, ent->render.origin);
958 ent->render.model = b->model;
959 ent->render.effects = EF_FULLBRIGHT;
960 //ent->render.angles[0] = pitch;
961 //ent->render.angles[1] = yaw;
962 //ent->render.angles[2] = rand()%360;
963 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
964 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
965 CL_BoundingBoxForEntity(&ent->render);
966 VectorMA(org, 30, dist, org);
972 void CL_LerpPlayer(float frac)
978 cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
980 for (i = 0;i < 3;i++)
981 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
983 if (cls.demoplayback)
985 // interpolate the angles
986 for (i = 0;i < 3;i++)
988 d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
993 cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
999 ent->state_previous = ent->state_current;
1000 ClearStateToDefault(&ent->state_current);
1001 ent->state_current.time = cl.time;
1002 ent->state_current.number = -1;
1003 ent->state_current.active = true;
1004 ent->state_current.modelindex = cl.stats[STAT_WEAPON];
1005 ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
1006 if (cl.viewentity >= 0 && cl.viewentity < MAX_EDICTS && cl_entities[cl.viewentity].state_current.active)
1008 ent->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
1009 ent->state_current.effects = cl_entities[cl.viewentity].state_current.effects;
1011 ent->state_current.flags = RENDER_VIEWMODEL;
1012 CL_RelinkNetworkEntity(ent);
1015 void CL_RelinkEntities(void)
1021 // fraction from previous network update to current
1022 frac = CL_LerpPoint();
1024 CL_ClearTempEntities();
1027 CL_RelinkStaticEntities();
1028 CL_RelinkNetworkEntities();
1032 CL_LerpPlayer(frac);
1042 Read all incoming data from the server
1045 int CL_ReadFromServer(void)
1047 CL_ReadDemoMessage();
1049 r_refdef.numentities = 0;
1050 cl_num_entities = 0;
1051 cl_num_brushmodel_entities = 0;
1053 if (cls.state == ca_connected && cls.signon == SIGNONS)
1055 CL_RelinkEntities();
1057 // run cgame code (which can add more entities)
1069 void CL_SendCmd(void)
1073 if (cls.signon == SIGNONS)
1075 // get basic movement from keyboard
1078 // OS independent code
1081 // allow mice or other external controllers to add to the move
1084 // OS independent code
1087 // send the unreliable message
1091 if (cls.demoplayback)
1093 SZ_Clear(&cls.message);
1097 // send the reliable message (forwarded commands) if there is one
1098 if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1100 if (developer.integer)
1102 Con_Printf("CL_SendCmd: sending reliable message:\n");
1103 SZ_HexDumpToConsole(&cls.message);
1105 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1106 Host_Error("CL_WriteToServer: lost server connection");
1107 SZ_Clear(&cls.message);
1111 // LordHavoc: pausedemo command
1112 static void CL_PauseDemo_f (void)
1114 cls.demopaused = !cls.demopaused;
1116 Con_Printf("Demo paused\n");
1118 Con_Printf("Demo unpaused\n");
1122 ======================
1124 ======================
1126 static void CL_Fog_f (void)
1128 if (Cmd_Argc () == 1)
1130 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1133 fog_density = atof(Cmd_Argv(1));
1134 fog_red = atof(Cmd_Argv(2));
1135 fog_green = atof(Cmd_Argv(3));
1136 fog_blue = atof(Cmd_Argv(4));
1146 cl_entities_mempool = Mem_AllocPool("client entities");
1147 cl_refdef_mempool = Mem_AllocPool("refdef");
1149 memset(&r_refdef, 0, sizeof(r_refdef));
1150 // max entities sent to renderer per frame
1151 r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1152 r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1153 // 256k drawqueue buffer
1154 r_refdef.maxdrawqueuesize = 256 * 1024;
1155 r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1157 SZ_Alloc (&cls.message, 1024, "cls.message");
1163 // register our commands
1165 Cvar_RegisterVariable (&cl_upspeed);
1166 Cvar_RegisterVariable (&cl_forwardspeed);
1167 Cvar_RegisterVariable (&cl_backspeed);
1168 Cvar_RegisterVariable (&cl_sidespeed);
1169 Cvar_RegisterVariable (&cl_movespeedkey);
1170 Cvar_RegisterVariable (&cl_yawspeed);
1171 Cvar_RegisterVariable (&cl_pitchspeed);
1172 Cvar_RegisterVariable (&cl_anglespeedkey);
1173 Cvar_RegisterVariable (&cl_shownet);
1174 Cvar_RegisterVariable (&cl_nolerp);
1175 Cvar_RegisterVariable (&lookspring);
1176 Cvar_RegisterVariable (&lookstrafe);
1177 Cvar_RegisterVariable (&sensitivity);
1178 Cvar_RegisterVariable (&freelook);
1180 Cvar_RegisterVariable (&m_pitch);
1181 Cvar_RegisterVariable (&m_yaw);
1182 Cvar_RegisterVariable (&m_forward);
1183 Cvar_RegisterVariable (&m_side);
1185 Cvar_RegisterVariable (&cl_itembobspeed);
1186 Cvar_RegisterVariable (&cl_itembobheight);
1188 Cmd_AddCommand ("entities", CL_PrintEntities_f);
1189 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1190 Cmd_AddCommand ("record", CL_Record_f);
1191 Cmd_AddCommand ("stop", CL_Stop_f);
1192 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1193 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1195 Cmd_AddCommand ("fog", CL_Fog_f);
1197 // LordHavoc: added pausedemo
1198 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1200 Cvar_RegisterVariable(&r_draweffects);
1201 Cvar_RegisterVariable(&cl_explosions);
1202 Cvar_RegisterVariable(&cl_stainmaps);
1203 Cvar_RegisterVariable(&cl_beams_polygons);
1204 Cvar_RegisterVariable(&cl_beams_relative);
1205 Cvar_RegisterVariable(&cl_beams_lightatend);
1206 Cvar_RegisterVariable(&cl_noplayershadow);
1209 CL_Particles_Init();