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"
26 // we need to declare some mouse variables here, because the menu system
27 // references them even when on a unix system.
29 // these two are not intended to be set directly
30 cvar_t cl_name = {CVAR_SAVE, "_cl_name", "player"};
31 cvar_t cl_color = {CVAR_SAVE, "_cl_color", "0"};
32 cvar_t cl_pmodel = {CVAR_SAVE, "_cl_pmodel", "0"};
34 cvar_t cl_shownet = {0, "cl_shownet","0"};
35 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
37 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
38 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
40 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
41 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
42 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
44 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
45 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
46 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
47 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
49 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
51 cvar_t r_draweffects = {0, "r_draweffects", "1"};
53 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
54 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
56 cvar_t cl_beampolygons = {CVAR_SAVE, "cl_beampolygons", "1"};
58 mempool_t *cl_scores_mempool;
59 mempool_t *cl_refdef_mempool;
60 mempool_t *cl_entities_mempool;
66 int cl_max_static_entities;
67 int cl_max_temp_entities;
71 int cl_max_lightstyle;
72 int cl_max_brushmodel_entities;
74 entity_t *cl_entities;
75 qbyte *cl_entities_active;
76 entity_t *cl_static_entities;
77 entity_t *cl_temp_entities;
78 cl_effect_t *cl_effects;
81 lightstyle_t *cl_lightstyle;
82 entity_render_t **cl_brushmodel_entities;
85 int cl_num_static_entities;
86 int cl_num_temp_entities;
87 int cl_num_brushmodel_entities;
95 void CL_ClearState (void)
102 Mem_EmptyPool(cl_scores_mempool);
103 Mem_EmptyPool(cl_entities_mempool);
105 // wipe the entire cl structure
106 memset (&cl, 0, sizeof(cl));
108 SZ_Clear (&cls.message);
111 cl_num_static_entities = 0;
112 cl_num_temp_entities = 0;
113 cl_num_brushmodel_entities = 0;
115 // tweak these if the game runs out
116 cl_max_entities = MAX_EDICTS;
117 cl_max_static_entities = 256;
118 cl_max_temp_entities = 512;
119 cl_max_effects = 256;
121 cl_max_dlights = MAX_DLIGHTS;
122 cl_max_lightstyle = MAX_LIGHTSTYLES;
123 cl_max_brushmodel_entities = MAX_EDICTS;
125 cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
126 cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
127 cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
128 cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
129 cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
130 cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
131 cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
132 cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
133 cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
137 CL_Particles_Clear();
139 // LordHavoc: have to set up the baseline info for alpha and other stuff
140 for (i = 0;i < cl_max_entities;i++)
142 ClearStateToDefault(&cl_entities[i].state_baseline);
143 ClearStateToDefault(&cl_entities[i].state_previous);
144 ClearStateToDefault(&cl_entities[i].state_current);
151 =====================
154 Sends a disconnect message to the server
155 This is also called on Host_Error, so it shouldn't cause any errors
156 =====================
158 void CL_Disconnect (void)
160 if (cls.state == ca_dedicated)
163 // stop sounds (especially looping!)
164 S_StopAllSounds (true);
166 // clear contents blends
167 cl.cshifts[0].percent = 0;
168 cl.cshifts[1].percent = 0;
169 cl.cshifts[2].percent = 0;
170 cl.cshifts[3].percent = 0;
172 cl.worldmodel = NULL;
174 if (cls.demoplayback)
176 else if (cls.state == ca_connected)
178 if (cls.demorecording)
181 Con_DPrintf ("Sending clc_disconnect\n");
182 SZ_Clear (&cls.message);
183 MSG_WriteByte (&cls.message, clc_disconnect);
184 NET_SendUnreliableMessage (cls.netcon, &cls.message);
185 SZ_Clear (&cls.message);
186 NET_Close (cls.netcon);
187 cls.state = ca_disconnected; // prevent this code from executing again during Host_ShutdownServer
188 // if running a local server, shut it down
190 Host_ShutdownServer(false);
192 cls.state = ca_disconnected;
194 cls.demoplayback = cls.timedemo = false;
198 void CL_Disconnect_f (void)
202 Host_ShutdownServer (false);
209 =====================
210 CL_EstablishConnection
212 Host should be either "local" or a net address to be passed on
213 =====================
215 void CL_EstablishConnection (char *host)
217 if (cls.state == ca_dedicated)
220 if (cls.demoplayback)
225 cls.netcon = NET_Connect (host);
227 Host_Error ("CL_Connect: connect failed\n");
228 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
230 cls.demonum = -1; // not in the demo loop now
231 cls.state = ca_connected;
232 cls.signon = 0; // need all the signon messages before playing
242 static void CL_PrintEntities_f (void)
248 for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
250 if (!ent->state_current.active)
253 if (ent->render.model)
254 strncpy(name, ent->render.model->name, 25);
256 strcpy(name, "--no model--");
258 for (j = strlen(name);j < 25;j++)
260 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);
264 static const vec3_t nomodelmins = {-16, -16, -16};
265 static const vec3_t nomodelmaxs = {16, 16, 16};
266 void CL_BoundingBoxForEntity(entity_render_t *ent)
270 if (ent->angles[0] || ent->angles[2])
273 VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
274 VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
276 else if (ent->angles[1])
279 VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
280 VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
284 VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
285 VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
290 VectorAdd(ent->origin, nomodelmins, ent->mins);
291 VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
295 void CL_LerpUpdate(entity_t *e)
297 entity_persistent_t *p;
302 if (p->modelindex != e->state_current.modelindex)
304 // reset all interpolation information
305 p->modelindex = e->state_current.modelindex;
306 p->frame1 = p->frame2 = e->state_current.frame;
307 p->frame1time = p->frame2time = cl.time;
310 else if (p->frame2 != e->state_current.frame)
312 // transition to new frame
313 p->frame1 = p->frame2;
314 p->frame1time = p->frame2time;
315 p->frame2 = e->state_current.frame;
316 p->frame2time = cl.time;
322 p->framelerp = (cl.time - p->frame2time) * 10;
323 p->framelerp = bound(0, p->framelerp, 1);
326 r->model = cl.model_precache[e->state_current.modelindex];
327 Mod_CheckLoaded(r->model);
328 r->frame = e->state_current.frame;
329 r->frame1 = p->frame1;
330 r->frame2 = p->frame2;
331 r->framelerp = p->framelerp;
332 r->frame1time = p->frame1time;
333 r->frame2time = p->frame2time;
340 Determines the fraction between the last two messages that the objects
344 static float CL_LerpPoint (void)
348 // dropped packet, or start of demo
349 if (cl.mtime[1] < cl.mtime[0] - 0.1)
350 cl.mtime[1] = cl.mtime[0] - 0.1;
352 cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
354 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
355 f = cl.mtime[0] - cl.mtime[1];
356 if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
358 cl.time = cl.mtime[0];
362 f = (cl.time - cl.mtime[1]) / f;
363 return bound(0, f, 1);
366 void CL_ClearTempEntities (void)
368 cl_num_temp_entities = 0;
371 entity_t *CL_NewTempEntity (void)
375 if (r_refdef.numentities >= r_refdef.maxentities)
377 if (cl_num_temp_entities >= cl_max_temp_entities)
379 ent = &cl_temp_entities[cl_num_temp_entities++];
380 memset (ent, 0, sizeof(*ent));
381 r_refdef.entities[r_refdef.numentities++] = &ent->render;
383 ent->render.colormap = -1; // no special coloring
384 ent->render.scale = 1;
385 ent->render.alpha = 1;
389 void CL_AllocDlight (entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
395 // first look for an exact key match
399 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
405 // then look for anything else
407 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
411 // unable to find one
415 //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
416 memset (dl, 0, sizeof(*dl));
418 VectorCopy(org, dl->origin);
421 dl->color[1] = green;
425 dl->die = cl.time + lifetime;
430 void CL_DecayLights (void)
436 time = cl.time - cl.oldtime;
439 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
443 if (dl->die < cl.time)
449 dl->radius -= time*dl->decay;
455 void CL_RelinkWorld (void)
457 if (cl_num_entities < 1)
459 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &cl_entities[0].render;
460 CL_BoundingBoxForEntity(&cl_entities[0].render);
463 static void CL_RelinkStaticEntities(void)
466 for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
468 Mod_CheckLoaded(cl_static_entities[i].render.model);
469 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
478 extern qboolean Nehahrademcompatibility;
479 static void CL_RelinkNetworkEntities(void)
482 int i, effects, temp;
483 float d, bobjrotate, bobjoffset, lerp;
484 vec3_t oldorg, neworg, delta, dlightcolor, v, v2, mins, maxs;
486 bobjrotate = ANGLEMOD(100*cl.time);
487 if (cl_itembobheight.value)
488 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
492 // start on the entity after the world
493 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
495 // if the object isn't active in the current network frame, skip it
496 if (!cl_entities_active[i])
498 if (!ent->state_current.active)
500 cl_entities_active[i] = false;
504 VectorCopy(ent->persistent.trail_origin, oldorg);
506 if (!ent->state_previous.active)
508 // only one state available
509 VectorCopy (ent->persistent.neworigin, neworg);
510 VectorCopy (ent->persistent.newangles, ent->render.angles);
511 VectorCopy (neworg, oldorg);
515 // if the delta is large, assume a teleport and don't lerp
516 VectorSubtract(ent->persistent.neworigin, ent->persistent.oldorigin, delta);
517 if (ent->persistent.lerpdeltatime > 0)
519 lerp = (cl.time - ent->persistent.lerpstarttime) / ent->persistent.lerpdeltatime;
522 // interpolate the origin and angles
523 VectorMA(ent->persistent.oldorigin, lerp, delta, neworg);
524 VectorSubtract(ent->persistent.newangles, ent->persistent.oldangles, delta);
525 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
526 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
527 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
528 VectorMA(ent->persistent.oldangles, lerp, delta, ent->render.angles);
533 VectorCopy (ent->persistent.neworigin, neworg);
534 VectorCopy (ent->persistent.newangles, ent->render.angles);
540 VectorCopy (ent->persistent.neworigin, neworg);
541 VectorCopy (ent->persistent.newangles, ent->render.angles);
545 VectorCopy (neworg, ent->persistent.trail_origin);
546 // persistent.modelindex will be updated by CL_LerpUpdate
547 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
548 VectorCopy(neworg, oldorg);
550 VectorCopy (neworg, ent->render.origin);
551 ent->render.flags = ent->state_current.flags;
552 if (i == cl.viewentity)
553 ent->render.flags |= RENDER_EXTERIORMODEL;
554 ent->render.effects = effects = ent->state_current.effects;
555 if (ent->state_current.flags & RENDER_COLORMAPPED)
556 ent->render.colormap = ent->state_current.colormap;
557 else if (cl.scores == NULL || !ent->state_current.colormap)
558 ent->render.colormap = -1; // no special coloring
560 ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
561 ent->render.skinnum = ent->state_current.skin;
562 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
563 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
565 // update interpolation info
568 // handle effects now...
573 // LordHavoc: if the entity has no effects, don't check each
576 if (effects & EF_BRIGHTFIELD)
578 if (gamemode == GAME_NEXIUZ)
580 dlightcolor[0] += 100.0f;
581 dlightcolor[1] += 200.0f;
582 dlightcolor[2] += 400.0f;
583 // don't do trail if we have no previous location
584 if (ent->state_previous.active)
585 CL_RocketTrail (oldorg, neworg, 8, ent);
588 CL_EntityParticles (ent);
590 if (effects & EF_MUZZLEFLASH)
591 ent->persistent.muzzleflash = 100.0f;
592 if (effects & EF_DIMLIGHT)
594 dlightcolor[0] += 200.0f;
595 dlightcolor[1] += 200.0f;
596 dlightcolor[2] += 200.0f;
598 if (effects & EF_BRIGHTLIGHT)
600 dlightcolor[0] += 400.0f;
601 dlightcolor[1] += 400.0f;
602 dlightcolor[2] += 400.0f;
604 // LordHavoc: added EF_RED and EF_BLUE
605 if (effects & EF_RED) // red
607 dlightcolor[0] += 200.0f;
608 dlightcolor[1] += 20.0f;
609 dlightcolor[2] += 20.0f;
611 if (effects & EF_BLUE) // blue
613 dlightcolor[0] += 20.0f;
614 dlightcolor[1] += 20.0f;
615 dlightcolor[2] += 200.0f;
617 if (effects & EF_FLAME)
619 if (ent->render.model)
621 mins[0] = neworg[0] - 16.0f;
622 mins[1] = neworg[1] - 16.0f;
623 mins[2] = neworg[2] - 16.0f;
624 maxs[0] = neworg[0] + 16.0f;
625 maxs[1] = neworg[1] + 16.0f;
626 maxs[2] = neworg[2] + 16.0f;
627 // how many flames to make
628 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
629 CL_FlameCube(mins, maxs, temp);
631 d = lhrandom(200, 250);
632 dlightcolor[0] += d * 1.0f;
633 dlightcolor[1] += d * 0.7f;
634 dlightcolor[2] += d * 0.3f;
636 if (effects & EF_STARDUST)
638 if (ent->render.model)
640 mins[0] = neworg[0] - 16.0f;
641 mins[1] = neworg[1] - 16.0f;
642 mins[2] = neworg[2] - 16.0f;
643 maxs[0] = neworg[0] + 16.0f;
644 maxs[1] = neworg[1] + 16.0f;
645 maxs[2] = neworg[2] + 16.0f;
646 // how many particles to make
647 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
648 CL_Stardust(mins, maxs, temp);
651 dlightcolor[0] += d * 1.0f;
652 dlightcolor[1] += d * 0.7f;
653 dlightcolor[2] += d * 0.3f;
657 if (ent->persistent.muzzleflash > 0)
659 AngleVectors (ent->render.angles, v, NULL, NULL);
661 v2[0] = v[0] * 18 + neworg[0];
662 v2[1] = v[1] * 18 + neworg[1];
663 v2[2] = v[2] * 18 + neworg[2] + 16;
664 CL_TraceLine(neworg, v2, v, NULL, 0, true, NULL);
666 CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
667 ent->persistent.muzzleflash -= cl.frametime * 1000;
670 // LordHavoc: if the model has no flags, don't check each
671 if (ent->render.model && ent->render.model->flags)
673 if (ent->render.model->flags & EF_ROTATE)
675 ent->render.angles[1] = bobjrotate;
676 ent->render.origin[2] += bobjoffset;
678 // only do trails if present in the previous frame as well
679 if (ent->state_previous.active)
681 if (ent->render.model->flags & EF_GIB)
682 CL_RocketTrail (oldorg, neworg, 2, ent);
683 else if (ent->render.model->flags & EF_ZOMGIB)
684 CL_RocketTrail (oldorg, neworg, 4, ent);
685 else if (ent->render.model->flags & EF_TRACER)
687 CL_RocketTrail (oldorg, neworg, 3, ent);
688 dlightcolor[0] += 0x10;
689 dlightcolor[1] += 0x40;
690 dlightcolor[2] += 0x10;
692 else if (ent->render.model->flags & EF_TRACER2)
694 CL_RocketTrail (oldorg, neworg, 5, ent);
695 dlightcolor[0] += 0x50;
696 dlightcolor[1] += 0x30;
697 dlightcolor[2] += 0x10;
699 else if (ent->render.model->flags & EF_ROCKET)
701 CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
702 dlightcolor[0] += 200.0f;
703 dlightcolor[1] += 160.0f;
704 dlightcolor[2] += 80.0f;
706 else if (ent->render.model->flags & EF_GRENADE)
708 if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility (cigar smoke)
709 CL_RocketTrail (oldorg, neworg, 7, ent);
711 CL_RocketTrail (oldorg, neworg, 1, ent);
713 else if (ent->render.model->flags & EF_TRACER3)
715 CL_RocketTrail (oldorg, neworg, 6, ent);
716 dlightcolor[0] += 0x50;
717 dlightcolor[1] += 0x20;
718 dlightcolor[2] += 0x40;
722 // LordHavoc: customizable glow
723 if (ent->state_current.glowsize)
725 // * 4 for the expansion from 0-255 to 0-1023 range,
726 // / 255 to scale down byte colors
727 VectorMA(dlightcolor, ent->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[ent->state_current.glowcolor], dlightcolor);
729 // LordHavoc: customizable trail
730 if (ent->render.flags & RENDER_GLOWTRAIL)
731 CL_RocketTrail2 (oldorg, neworg, ent->state_current.glowcolor, ent);
733 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
735 VectorCopy(neworg, v);
736 // hack to make glowing player light shine on their gun
737 if (i == cl.viewentity/* && !chase_active.integer*/)
739 CL_AllocDlight (&ent->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
742 if (chase_active.integer && (ent->render.flags & RENDER_VIEWMODEL))
745 // don't show entities with no modelindex (note: this still shows
746 // entities which have a modelindex that resolved to a NULL model)
747 if (!ent->state_current.modelindex)
749 if (effects & EF_NODRAW)
752 CL_BoundingBoxForEntity(&ent->render);
753 if (ent->render.model && ent->render.model->name[0] == '*' && ent->render.model->type == mod_brush)
754 cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
756 // note: the cl.viewentity and intermission check is to hide player
757 // shadow during intermission and during the Nehahra movie and
758 // Nehahra cinematics
759 if (!(ent->state_current.effects & EF_NOSHADOW)
760 && !(ent->state_current.effects & EF_ADDITIVE)
761 && (ent->state_current.alpha == 255)
762 && !(ent->render.flags & RENDER_VIEWMODEL)
763 && (i != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility)))
764 ent->render.flags |= RENDER_SHADOW;
766 if (r_refdef.numentities < r_refdef.maxentities)
767 r_refdef.entities[r_refdef.numentities++] = &ent->render;
769 if (cl_num_entities < i + 1)
770 cl_num_entities = i + 1;
774 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
778 if (!modelindex) // sanity check
780 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
785 VectorCopy(org, e->origin);
786 e->modelindex = modelindex;
787 e->starttime = cl.time;
788 e->startframe = startframe;
789 e->endframe = startframe + framecount;
790 e->framerate = framerate;
793 e->frame1time = cl.time;
794 e->frame2time = cl.time;
799 static void CL_RelinkEffects(void)
806 for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
810 frame = (cl.time - e->starttime) * e->framerate + e->startframe;
812 if (intframe < 0 || intframe >= e->endframe)
815 memset(e, 0, sizeof(*e));
819 if (intframe != e->frame)
822 e->frame1time = e->frame2time;
823 e->frame2time = cl.time;
826 // if we're drawing effects, get a new temp entity
827 // (NewTempEntity adds it to the render entities list for us)
828 if (r_draweffects.integer && (ent = CL_NewTempEntity()))
830 // interpolation stuff
831 ent->render.frame1 = intframe;
832 ent->render.frame2 = intframe + 1;
833 if (ent->render.frame2 >= e->endframe)
834 ent->render.frame2 = -1; // disappear
835 ent->render.framelerp = frame - intframe;
836 ent->render.frame1time = e->frame1time;
837 ent->render.frame2time = e->frame2time;
840 VectorCopy(e->origin, ent->render.origin);
841 ent->render.model = cl.model_precache[e->modelindex];
842 ent->render.frame = ent->render.frame2;
843 ent->render.colormap = -1; // no special coloring
844 ent->render.scale = 1;
845 ent->render.alpha = 1;
847 CL_BoundingBoxForEntity(&ent->render);
853 void CL_RelinkBeams (void)
863 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
865 if (!b->model || b->endtime < cl.time)
868 // if coming from the player, update the start position
869 //if (b->entity == cl.viewentity)
870 // VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
871 if (b->entity && cl_entities[b->entity].state_current.active)
873 VectorCopy (cl_entities[b->entity].render.origin, b->start);
877 if (cl_beampolygons.integer)
880 // calculate pitch and yaw
881 VectorSubtract (b->end, b->start, dist);
883 if (dist[1] == 0 && dist[0] == 0)
893 yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
897 forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
898 pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
903 // add new entities for the lightning
904 VectorCopy (b->start, org);
905 d = VectorNormalizeLength(dist);
908 ent = CL_NewTempEntity ();
911 VectorCopy (org, ent->render.origin);
912 ent->render.model = b->model;
913 ent->render.effects = EF_FULLBRIGHT;
914 ent->render.angles[0] = pitch;
915 ent->render.angles[1] = yaw;
916 ent->render.angles[2] = rand()%360;
917 CL_BoundingBoxForEntity(&ent->render);
918 VectorMA(org, 30, dist, org);
924 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "4"};
925 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5"};
926 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "1024"};
927 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1"};
928 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1"};
929 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1"};
931 rtexture_t *r_lightningbeamtexture;
932 rtexturepool_t *r_lightningbeamtexturepool;
934 int r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
936 void r_lightningbeams_start(void)
938 float r, g, b, intensity, fx, width, center;
940 qbyte *data, *noise1, *noise2;
941 data = Mem_Alloc(tempmempool, 32 * 512 * 4);
942 noise1 = Mem_Alloc(tempmempool, 512 * 512);
943 noise2 = Mem_Alloc(tempmempool, 512 * 512);
944 fractalnoise(noise1, 512, 8);
945 fractalnoise(noise2, 512, 16);
947 for (y = 0;y < 512;y++)
949 width = noise1[y * 512] * (1.0f / 256.0f) * 3.0f + 3.0f;
950 center = (noise1[y * 512 + 64] / 256.0f) * (32.0f - (width + 1.0f) * 2.0f) + (width + 1.0f);
951 for (x = 0;x < 32;x++, fx++)
953 fx = (x - center) / width;
954 intensity = (1.0f - fx * fx) * (noise2[y*512+x] * (1.0f / 256.0f) * 0.33f + 0.66f);
955 intensity = bound(0, intensity, 1);
956 r = intensity * 2.0f - 1.0f;
957 g = intensity * 3.0f - 1.0f;
958 b = intensity * 3.0f;
959 data[(y * 32 + x) * 4 + 0] = (qbyte)(bound(0, r, 1) * 255.0f);
960 data[(y * 32 + x) * 4 + 1] = (qbyte)(bound(0, g, 1) * 255.0f);
961 data[(y * 32 + x) * 4 + 2] = (qbyte)(bound(0, b, 1) * 255.0f);
962 data[(y * 32 + x) * 4 + 3] = (qbyte)255;
966 r_lightningbeamtexturepool = R_AllocTexturePool();
967 r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", 32, 512, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
973 void r_lightningbeams_shutdown(void)
975 r_lightningbeamtexture = NULL;
976 R_FreeTexturePool(&r_lightningbeamtexturepool);
979 void r_lightningbeams_newmap(void)
983 void R_LightningBeams_Init(void)
985 Cvar_RegisterVariable(&r_lightningbeam_thickness);
986 Cvar_RegisterVariable(&r_lightningbeam_scroll);
987 Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
988 Cvar_RegisterVariable(&r_lightningbeam_color_red);
989 Cvar_RegisterVariable(&r_lightningbeam_color_green);
990 Cvar_RegisterVariable(&r_lightningbeam_color_blue);
991 R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
994 void R_CalcLightningBeamPolygonVertices(float *v, float *tc, const float *start, const float *end, const float *offset, float t1, float t2)
997 VectorAdd (start, offset, (v + 0));tc[ 0] = 0;tc[ 1] = t1;
999 VectorSubtract(start, offset, (v + 4));tc[ 4] = 1;tc[ 5] = t1;
1001 VectorSubtract(end , offset, (v + 8));tc[ 8] = 1;tc[ 9] = t2;
1003 VectorAdd (end , offset, (v + 12));tc[12] = 0;tc[13] = t2;
1006 void R_FogLightningBeamColors(const float *v, float *c, int numverts, float r, float g, float b, float a)
1011 for (i = 0;i < numverts;i++, v += 4, c += 4)
1013 VectorSubtract(v, r_origin, fogvec);
1014 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
1022 float beamrepeatscale;
1024 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
1026 const beam_t *b = calldata1;
1028 vec3_t beamdir, right, up, offset;
1029 float length, t1, t2;
1030 memset(&m, 0, sizeof(m));
1031 m.blendfunc1 = GL_SRC_ALPHA;
1032 m.blendfunc2 = GL_ONE;
1033 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
1035 R_Mesh_Matrix(&r_identitymatrix);
1037 // calculate beam direction (beamdir) vector and beam length
1038 // get difference vector
1039 VectorSubtract(b->end, b->start, beamdir);
1040 // find length of difference vector
1041 length = sqrt(DotProduct(beamdir, beamdir));
1042 // calculate scale to make beamdir a unit vector (normalized)
1044 // scale beamdir so it is now normalized
1045 VectorScale(beamdir, t1, beamdir);
1047 // calculate up vector such that it points toward viewer, and rotates around the beamdir
1048 // get direction from start of beam to viewer
1049 VectorSubtract(r_origin, b->start, up);
1050 // remove the portion of the vector that moves along the beam
1051 // (this leaves only a vector pointing directly away from the beam)
1052 t1 = -DotProduct(up, beamdir);
1053 VectorMA(up, t1, beamdir, up);
1054 // now we have a vector pointing away from the beam, now we need to normalize it
1055 VectorNormalizeFast(up);
1056 // generate right vector from forward and up, the result is already normalized
1057 // (CrossProduct returns a vector of multiplied length of the two inputs)
1058 CrossProduct(beamdir, up, right);
1060 // calculate T coordinate scrolling (start and end texcoord along the beam)
1061 t1 = cl.time * -r_lightningbeam_scroll.value;
1063 t2 = t1 + beamrepeatscale * length;
1065 // the beam is 3 polygons in this configuration:
1071 // they are showing different portions of the beam texture, creating an
1072 // illusion of a beam that appears to curl around in 3D space
1073 // (and realize that the whole polygon assembly orients itself to face
1076 // polygon 1, verts 0-3
1077 VectorScale(right, r_lightningbeam_thickness.value, offset);
1078 R_CalcLightningBeamPolygonVertices(varray_vertex, varray_texcoord[0], b->start, b->end, offset, t1, t2);
1080 // polygon 2, verts 4-7
1081 VectorAdd(right, up, offset);
1082 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1083 R_CalcLightningBeamPolygonVertices(varray_vertex + 16, varray_texcoord[0] + 16, b->start, b->end, offset, t1 + 0.33, t2 + 0.33);
1085 // polygon 3, verts 8-11
1086 VectorSubtract(right, up, offset);
1087 VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1088 R_CalcLightningBeamPolygonVertices(varray_vertex + 32, varray_texcoord[0] + 32, b->start, b->end, offset, t1 + 0.66, t2 + 0.66);
1092 // per vertex colors if fog is used
1094 R_FogLightningBeamColors(varray_vertex, varray_color, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1098 // solid color if fog is not used
1099 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1102 // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
1103 R_Mesh_Draw(12, 6, r_lightningbeamelements);
1106 void R_DrawLightningBeams (void)
1112 if (!cl_beampolygons.integer)
1115 beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
1116 for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1118 if (b->model && b->endtime >= cl.time)
1120 VectorAdd(b->start, b->end, org);
1121 VectorScale(org, 0.5f, org);
1122 R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
1128 void CL_LerpPlayer(float frac)
1133 if (cl.entitydatabase.numframes && cl.viewentity == cl.playerentity)
1135 cl.viewentorigin[0] = cl.viewentoriginold[0] + frac * (cl.viewentoriginnew[0] - cl.viewentoriginold[0]);
1136 cl.viewentorigin[1] = cl.viewentoriginold[1] + frac * (cl.viewentoriginnew[1] - cl.viewentoriginold[1]);
1137 cl.viewentorigin[2] = cl.viewentoriginold[2] + frac * (cl.viewentoriginnew[2] - cl.viewentoriginold[2]);
1140 VectorCopy (cl_entities[cl.viewentity].render.origin, cl.viewentorigin);
1142 cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1144 for (i = 0;i < 3;i++)
1145 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1147 if (cls.demoplayback)
1149 // interpolate the angles
1150 for (i = 0;i < 3;i++)
1152 d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1157 cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1162 void CL_RelinkEntities (void)
1166 // fraction from previous network update to current
1167 frac = CL_LerpPoint();
1169 CL_ClearTempEntities();
1172 CL_RelinkStaticEntities();
1173 CL_RelinkNetworkEntities();
1178 CL_LerpPlayer(frac);
1186 Read all incoming data from the server
1189 int CL_ReadFromServer (void)
1193 cl.oldtime = cl.time;
1194 cl.time += cl.frametime;
1199 ret = CL_GetMessage ();
1201 Host_Error ("CL_ReadFromServer: lost server connection");
1205 cl.last_received_message = realtime;
1207 if (cl_shownet.integer)
1210 CL_ParseServerMessage ();
1212 while (ret && cls.state == ca_connected);
1217 r_refdef.numentities = 0;
1218 cl_num_entities = 0;
1219 cl_num_brushmodel_entities = 0;
1221 if (cls.state == ca_connected && cls.signon == SIGNONS)
1223 CL_RelinkEntities ();
1225 // run cgame code (which can add more entities)
1230 // bring the links up to date
1240 void CL_SendCmd (void)
1244 if (cls.state != ca_connected)
1247 if (cls.signon == SIGNONS)
1249 // get basic movement from keyboard
1252 IN_PreMove(); // OS independent code
1254 // allow mice or other external controllers to add to the move
1257 IN_PostMove(); // OS independent code
1259 // send the unreliable message
1262 #ifndef NOROUTINGFIX
1263 else if (cls.signon == 0 && !cls.demoplayback)
1265 // LordHavoc: fix for NAT routing of netquake:
1266 // bounce back a clc_nop message to the newly allocated server port,
1267 // to establish a routing connection for incoming frames,
1268 // the server waits for this before sending anything
1269 if (realtime > cl.sendnoptime)
1271 cl.sendnoptime = realtime + 3;
1272 Con_DPrintf("sending clc_nop to get server's attention\n");
1279 MSG_WriteByte(&buf, clc_nop);
1280 if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
1282 Con_Printf ("CL_SendCmd: lost server connection\n");
1290 if (cls.demoplayback)
1292 SZ_Clear (&cls.message);
1296 // send the reliable message
1297 if (!cls.message.cursize)
1298 return; // no message at all
1300 if (!NET_CanSendMessage (cls.netcon))
1302 Con_DPrintf ("CL_WriteToServer: can't send\n");
1303 if (developer.integer)
1304 SZ_HexDumpToConsole(&cls.message);
1308 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
1309 Host_Error ("CL_WriteToServer: lost server connection");
1311 SZ_Clear (&cls.message);
1314 // LordHavoc: pausedemo command
1315 static void CL_PauseDemo_f (void)
1317 cls.demopaused = !cls.demopaused;
1319 Con_Printf("Demo paused\n");
1321 Con_Printf("Demo unpaused\n");
1325 ======================
1327 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
1328 ======================
1330 static void CL_PModel_f (void)
1335 if (Cmd_Argc () == 1)
1337 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
1340 i = atoi(Cmd_Argv(1));
1342 if (cmd_source == src_command)
1344 if (cl_pmodel.integer == i)
1346 Cvar_SetValue ("_cl_pmodel", i);
1347 if (cls.state == ca_connected)
1348 Cmd_ForwardToServer ();
1352 host_client->pmodel = i;
1353 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
1358 ======================
1360 ======================
1362 static void CL_Fog_f (void)
1364 if (Cmd_Argc () == 1)
1366 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1369 fog_density = atof(Cmd_Argv(1));
1370 fog_red = atof(Cmd_Argv(2));
1371 fog_green = atof(Cmd_Argv(3));
1372 fog_blue = atof(Cmd_Argv(4));
1382 cl_scores_mempool = Mem_AllocPool("client player info");
1383 cl_entities_mempool = Mem_AllocPool("client entities");
1384 cl_refdef_mempool = Mem_AllocPool("refdef");
1386 memset(&r_refdef, 0, sizeof(r_refdef));
1387 // max entities sent to renderer per frame
1388 r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1389 r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1390 // 256k drawqueue buffer
1391 r_refdef.maxdrawqueuesize = 256 * 1024;
1392 r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1394 SZ_Alloc (&cls.message, 1024, "cls.message");
1400 // register our commands
1402 Cvar_RegisterVariable (&cl_name);
1403 Cvar_RegisterVariable (&cl_color);
1404 if (gamemode == GAME_NEHAHRA)
1405 Cvar_RegisterVariable (&cl_pmodel);
1406 Cvar_RegisterVariable (&cl_upspeed);
1407 Cvar_RegisterVariable (&cl_forwardspeed);
1408 Cvar_RegisterVariable (&cl_backspeed);
1409 Cvar_RegisterVariable (&cl_sidespeed);
1410 Cvar_RegisterVariable (&cl_movespeedkey);
1411 Cvar_RegisterVariable (&cl_yawspeed);
1412 Cvar_RegisterVariable (&cl_pitchspeed);
1413 Cvar_RegisterVariable (&cl_anglespeedkey);
1414 Cvar_RegisterVariable (&cl_shownet);
1415 Cvar_RegisterVariable (&cl_nolerp);
1416 Cvar_RegisterVariable (&lookspring);
1417 Cvar_RegisterVariable (&lookstrafe);
1418 Cvar_RegisterVariable (&sensitivity);
1419 Cvar_RegisterVariable (&freelook);
1421 Cvar_RegisterVariable (&m_pitch);
1422 Cvar_RegisterVariable (&m_yaw);
1423 Cvar_RegisterVariable (&m_forward);
1424 Cvar_RegisterVariable (&m_side);
1426 Cvar_RegisterVariable (&cl_itembobspeed);
1427 Cvar_RegisterVariable (&cl_itembobheight);
1429 Cmd_AddCommand ("entities", CL_PrintEntities_f);
1430 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1431 Cmd_AddCommand ("record", CL_Record_f);
1432 Cmd_AddCommand ("stop", CL_Stop_f);
1433 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1434 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1436 Cmd_AddCommand ("fog", CL_Fog_f);
1438 // LordHavoc: added pausedemo
1439 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1440 if (gamemode == GAME_NEHAHRA)
1441 Cmd_AddCommand ("pmodel", CL_PModel_f);
1443 Cvar_RegisterVariable(&r_draweffects);
1444 Cvar_RegisterVariable(&cl_explosions);
1445 Cvar_RegisterVariable(&cl_stainmaps);
1446 Cvar_RegisterVariable(&cl_beampolygons);
1448 R_LightningBeams_Init();
1451 CL_Particles_Init();