]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_main.c
11eeadc76eaaf0a79d6f2058ca72a50e573ed5b0
[xonotic/darkplaces.git] / cl_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_main.c  -- client main loop
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "cl_video.h"
25
26 // we need to declare some mouse variables here, because the menu system
27 // references them even when on a unix system.
28
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"};
33
34 cvar_t cl_shownet = {0, "cl_shownet","0"};
35 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
36
37 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
38 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
39
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};
43
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"};
48
49 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
50
51 cvar_t r_draweffects = {0, "r_draweffects", "1"};
52
53 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
54 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
55
56 mempool_t *cl_scores_mempool;
57 mempool_t *cl_refdef_mempool;
58 mempool_t *cl_entities_mempool;
59
60 client_static_t cls;
61 client_state_t  cl;
62
63 int cl_max_entities;
64 int cl_max_static_entities;
65 int cl_max_temp_entities;
66 int cl_max_effects;
67 int cl_max_beams;
68 int cl_max_dlights;
69 int cl_max_lightstyle;
70 int cl_max_brushmodel_entities;
71
72 entity_t *cl_entities;
73 qbyte *cl_entities_active;
74 entity_t *cl_static_entities;
75 entity_t *cl_temp_entities;
76 cl_effect_t *cl_effects;
77 beam_t *cl_beams;
78 dlight_t *cl_dlights;
79 lightstyle_t *cl_lightstyle;
80 entity_render_t **cl_brushmodel_entities;
81
82 int cl_num_entities;
83 int cl_num_static_entities;
84 int cl_num_temp_entities;
85 int cl_num_brushmodel_entities;
86
87 /*
88 =====================
89 CL_ClearState
90
91 =====================
92 */
93 void CL_ClearState (void)
94 {
95         int i;
96
97         if (!sv.active)
98                 Host_ClearMemory ();
99
100         Mem_EmptyPool(cl_scores_mempool);
101         Mem_EmptyPool(cl_entities_mempool);
102
103 // wipe the entire cl structure
104         memset (&cl, 0, sizeof(cl));
105
106         SZ_Clear (&cls.message);
107
108         cl_num_entities = 0;
109         cl_num_static_entities = 0;
110         cl_num_temp_entities = 0;
111         cl_num_brushmodel_entities = 0;
112
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;
118         cl_max_beams = 24;
119         cl_max_dlights = MAX_DLIGHTS;
120         cl_max_lightstyle = MAX_LIGHTSTYLES;
121         cl_max_brushmodel_entities = MAX_EDICTS;
122
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 *));
132
133         CL_Screen_NewMap();
134
135         CL_Particles_Clear();
136
137         // LordHavoc: have to set up the baseline info for alpha and other stuff
138         for (i = 0;i < cl_max_entities;i++)
139         {
140                 ClearStateToDefault(&cl_entities[i].state_baseline);
141                 ClearStateToDefault(&cl_entities[i].state_previous);
142                 ClearStateToDefault(&cl_entities[i].state_current);
143         }
144
145         CL_CGVM_Clear();
146 }
147
148 /*
149 =====================
150 CL_Disconnect
151
152 Sends a disconnect message to the server
153 This is also called on Host_Error, so it shouldn't cause any errors
154 =====================
155 */
156 void CL_Disconnect (void)
157 {
158         if (cls.state == ca_dedicated)
159                 return;
160
161 // stop sounds (especially looping!)
162         S_StopAllSounds (true);
163
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;
169
170         cl.worldmodel = NULL;
171
172         if (cls.demoplayback)
173                 CL_StopPlayback ();
174         else if (cls.state == ca_connected)
175         {
176                 if (cls.demorecording)
177                         CL_Stop_f ();
178
179                 Con_DPrintf ("Sending clc_disconnect\n");
180                 SZ_Clear (&cls.message);
181                 MSG_WriteByte (&cls.message, clc_disconnect);
182                 NET_SendUnreliableMessage (cls.netcon, &cls.message);
183                 SZ_Clear (&cls.message);
184                 NET_Close (cls.netcon);
185                 cls.state = ca_disconnected; // prevent this code from executing again during Host_ShutdownServer
186                 // if running a local server, shut it down
187                 if (sv.active)
188                         Host_ShutdownServer(false);
189         }
190         cls.state = ca_disconnected;
191
192         cls.demoplayback = cls.timedemo = false;
193         cls.signon = 0;
194 }
195
196 void CL_Disconnect_f (void)
197 {
198         CL_Disconnect ();
199         if (sv.active)
200                 Host_ShutdownServer (false);
201 }
202
203
204
205
206 /*
207 =====================
208 CL_EstablishConnection
209
210 Host should be either "local" or a net address to be passed on
211 =====================
212 */
213 void CL_EstablishConnection (char *host)
214 {
215         if (cls.state == ca_dedicated)
216                 return;
217
218         if (cls.demoplayback)
219                 return;
220
221         CL_Disconnect ();
222
223         cls.netcon = NET_Connect (host);
224         if (!cls.netcon)
225                 Host_Error ("CL_Connect: connect failed\n");
226         Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
227
228         cls.demonum = -1;                       // not in the demo loop now
229         cls.state = ca_connected;
230         cls.signon = 0;                         // need all the signon messages before playing
231
232         CL_ClearState ();
233 }
234
235 /*
236 ==============
237 CL_PrintEntities_f
238 ==============
239 */
240 static void CL_PrintEntities_f (void)
241 {
242         entity_t *ent;
243         int i, j;
244         char name[32];
245
246         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
247         {
248                 if (!ent->state_current.active)
249                         continue;
250
251                 if (ent->render.model)
252                         strncpy(name, ent->render.model->name, 25);
253                 else
254                         strcpy(name, "--no model--");
255                 name[25] = 0;
256                 for (j = strlen(name);j < 25;j++)
257                         name[j] = ' ';
258                 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);
259         }
260 }
261
262 static const vec3_t nomodelmins = {-16, -16, -16};
263 static const vec3_t nomodelmaxs = {16, 16, 16};
264 void CL_BoundingBoxForEntity(entity_render_t *ent)
265 {
266         if (ent->model)
267         {
268                 if (ent->angles[0] || ent->angles[2])
269                 {
270                         // pitch or roll
271                         VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
272                         VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
273                 }
274                 else if (ent->angles[1])
275                 {
276                         // yaw
277                         VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
278                         VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
279                 }
280                 else
281                 {
282                         VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
283                         VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
284                 }
285         }
286         else
287         {
288                 VectorAdd(ent->origin, nomodelmins, ent->mins);
289                 VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
290         }
291 }
292
293 void CL_LerpUpdate(entity_t *e)
294 {
295         entity_persistent_t *p;
296         entity_render_t *r;
297         p = &e->persistent;
298         r = &e->render;
299
300         if (p->modelindex != e->state_current.modelindex)
301         {
302                 // reset all interpolation information
303                 p->modelindex = e->state_current.modelindex;
304                 p->frame1 = p->frame2 = e->state_current.frame;
305                 p->frame1time = p->frame2time = cl.time;
306                 p->framelerp = 1;
307         }
308         else if (p->frame2 != e->state_current.frame)
309         {
310                 // transition to new frame
311                 p->frame1 = p->frame2;
312                 p->frame1time = p->frame2time;
313                 p->frame2 = e->state_current.frame;
314                 p->frame2time = cl.time;
315                 p->framelerp = 0;
316         }
317         else
318         {
319                 // update transition
320                 p->framelerp = (cl.time - p->frame2time) * 10;
321                 p->framelerp = bound(0, p->framelerp, 1);
322         }
323
324         r->model = cl.model_precache[e->state_current.modelindex];
325         Mod_CheckLoaded(r->model);
326         r->frame = e->state_current.frame;
327         r->frame1 = p->frame1;
328         r->frame2 = p->frame2;
329         r->framelerp = p->framelerp;
330         r->frame1time = p->frame1time;
331         r->frame2time = p->frame2time;
332 }
333
334 /*
335 ===============
336 CL_LerpPoint
337
338 Determines the fraction between the last two messages that the objects
339 should be put at.
340 ===============
341 */
342 static float CL_LerpPoint (void)
343 {
344         float f;
345
346         // dropped packet, or start of demo
347         if (cl.mtime[1] < cl.mtime[0] - 0.1)
348                 cl.mtime[1] = cl.mtime[0] - 0.1;
349
350         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
351
352         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
353         f = cl.mtime[0] - cl.mtime[1];
354         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
355         {
356                 cl.time = cl.mtime[0];
357                 return 1;
358         }
359
360         f = (cl.time - cl.mtime[1]) / f;
361         return bound(0, f, 1);
362 }
363
364 void CL_ClearTempEntities (void)
365 {
366         cl_num_temp_entities = 0;
367 }
368
369 entity_t *CL_NewTempEntity (void)
370 {
371         entity_t *ent;
372
373         if (r_refdef.numentities >= r_refdef.maxentities)
374                 return NULL;
375         if (cl_num_temp_entities >= cl_max_temp_entities)
376                 return NULL;
377         ent = &cl_temp_entities[cl_num_temp_entities++];
378         memset (ent, 0, sizeof(*ent));
379         r_refdef.entities[r_refdef.numentities++] = &ent->render;
380
381         ent->render.colormap = -1; // no special coloring
382         ent->render.scale = 1;
383         ent->render.alpha = 1;
384         return ent;
385 }
386
387 void CL_AllocDlight (entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
388 {
389         int i;
390         dlight_t *dl;
391
392         /*
393 // first look for an exact key match
394         if (ent)
395         {
396                 dl = cl_dlights;
397                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
398                         if (dl->ent == ent)
399                                 goto dlightsetup;
400         }
401         */
402
403 // then look for anything else
404         dl = cl_dlights;
405         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
406                 if (!dl->radius)
407                         goto dlightsetup;
408
409         // unable to find one
410         return;
411
412 dlightsetup:
413         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
414         memset (dl, 0, sizeof(*dl));
415         dl->ent = ent;
416         VectorCopy(org, dl->origin);
417         dl->radius = radius;
418         dl->color[0] = red;
419         dl->color[1] = green;
420         dl->color[2] = blue;
421         dl->decay = decay;
422         if (lifetime)
423                 dl->die = cl.time + lifetime;
424         else
425                 dl->die = 0;
426 }
427
428 void CL_DecayLights (void)
429 {
430         int i;
431         dlight_t *dl;
432         float time;
433
434         time = cl.time - cl.oldtime;
435
436         dl = cl_dlights;
437         for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
438         {
439                 if (!dl->radius)
440                         continue;
441                 if (dl->die < cl.time)
442                 {
443                         dl->radius = 0;
444                         continue;
445                 }
446
447                 dl->radius -= time*dl->decay;
448                 if (dl->radius < 0)
449                         dl->radius = 0;
450         }
451 }
452
453 void CL_RelinkWorld (void)
454 {
455         if (cl_num_entities < 1)
456                 cl_num_entities = 1;
457         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &cl_entities[0].render;
458         CL_BoundingBoxForEntity(&cl_entities[0].render);
459 }
460
461 static void CL_RelinkStaticEntities(void)
462 {
463         int i;
464         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
465         {
466                 Mod_CheckLoaded(cl_static_entities[i].render.model);
467                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
468         }
469 }
470
471 /*
472 ===============
473 CL_RelinkEntities
474 ===============
475 */
476 extern qboolean Nehahrademcompatibility;
477 static void CL_RelinkNetworkEntities(void)
478 {
479         entity_t *ent;
480         int i, effects, temp;
481         float d, bobjrotate, bobjoffset, lerp;
482         vec3_t oldorg, neworg, delta, dlightcolor, v, v2, mins, maxs;
483
484         bobjrotate = ANGLEMOD(100*cl.time);
485         if (cl_itembobheight.value)
486                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
487         else
488                 bobjoffset = 0;
489
490         // start on the entity after the world
491         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
492         {
493                 // if the object isn't active in the current network frame, skip it
494                 if (!cl_entities_active[i])
495                         continue;
496                 if (!ent->state_current.active)
497                 {
498                         cl_entities_active[i] = false;
499                         continue;
500                 }
501
502                 VectorCopy(ent->persistent.trail_origin, oldorg);
503
504                 if (!ent->state_previous.active)
505                 {
506                         // only one state available
507                         VectorCopy (ent->persistent.neworigin, neworg);
508                         VectorCopy (ent->persistent.newangles, ent->render.angles);
509                         VectorCopy (neworg, oldorg);
510                 }
511                 else
512                 {
513                         // if the delta is large, assume a teleport and don't lerp
514                         VectorSubtract(ent->persistent.neworigin, ent->persistent.oldorigin, delta);
515                         if (ent->persistent.lerpdeltatime > 0)
516                         {
517                                 lerp = (cl.time - ent->persistent.lerpstarttime) / ent->persistent.lerpdeltatime;
518                                 if (lerp < 1)
519                                 {
520                                         // interpolate the origin and angles
521                                         VectorMA(ent->persistent.oldorigin, lerp, delta, neworg);
522                                         VectorSubtract(ent->persistent.newangles, ent->persistent.oldangles, delta);
523                                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
524                                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
525                                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
526                                         VectorMA(ent->persistent.oldangles, lerp, delta, ent->render.angles);
527                                 }
528                                 else
529                                 {
530                                         // no interpolation
531                                         VectorCopy (ent->persistent.neworigin, neworg);
532                                         VectorCopy (ent->persistent.newangles, ent->render.angles);
533                                 }
534                         }
535                         else
536                         {
537                                 // no interpolation
538                                 VectorCopy (ent->persistent.neworigin, neworg);
539                                 VectorCopy (ent->persistent.newangles, ent->render.angles);
540                         }
541                 }
542
543                 VectorCopy (neworg, ent->persistent.trail_origin);
544                 // persistent.modelindex will be updated by CL_LerpUpdate
545                 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
546                         VectorCopy(neworg, oldorg);
547
548                 VectorCopy (neworg, ent->render.origin);
549                 ent->render.flags = ent->state_current.flags;
550                 if (i == cl.viewentity)
551                         ent->render.flags |= RENDER_EXTERIORMODEL;
552                 ent->render.effects = effects = ent->state_current.effects;
553                 if (ent->state_current.flags & RENDER_COLORMAPPED)
554                         ent->render.colormap = ent->state_current.colormap;
555                 else if (cl.scores == NULL || !ent->state_current.colormap)
556                         ent->render.colormap = -1; // no special coloring
557                 else
558                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
559                 ent->render.skinnum = ent->state_current.skin;
560                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
561                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
562
563                 // update interpolation info
564                 CL_LerpUpdate(ent);
565
566                 // handle effects now...
567                 dlightcolor[0] = 0;
568                 dlightcolor[1] = 0;
569                 dlightcolor[2] = 0;
570
571                 // LordHavoc: if the entity has no effects, don't check each
572                 if (effects)
573                 {
574                         if (effects & EF_BRIGHTFIELD)
575                         {
576                                 if (gamemode == GAME_NEXIUZ)
577                                 {
578                                         dlightcolor[0] += 100.0f;
579                                         dlightcolor[1] += 200.0f;
580                                         dlightcolor[2] += 400.0f;
581                                         // don't do trail if we have no previous location
582                                         if (ent->state_previous.active)
583                                                 CL_RocketTrail (oldorg, neworg, 8, ent);
584                                 }
585                                 else
586                                         CL_EntityParticles (ent);
587                         }
588                         if (effects & EF_MUZZLEFLASH)
589                                 ent->persistent.muzzleflash = 100.0f;
590                         if (effects & EF_DIMLIGHT)
591                         {
592                                 dlightcolor[0] += 200.0f;
593                                 dlightcolor[1] += 200.0f;
594                                 dlightcolor[2] += 200.0f;
595                         }
596                         if (effects & EF_BRIGHTLIGHT)
597                         {
598                                 dlightcolor[0] += 400.0f;
599                                 dlightcolor[1] += 400.0f;
600                                 dlightcolor[2] += 400.0f;
601                         }
602                         // LordHavoc: added EF_RED and EF_BLUE
603                         if (effects & EF_RED) // red
604                         {
605                                 dlightcolor[0] += 200.0f;
606                                 dlightcolor[1] +=  20.0f;
607                                 dlightcolor[2] +=  20.0f;
608                         }
609                         if (effects & EF_BLUE) // blue
610                         {
611                                 dlightcolor[0] +=  20.0f;
612                                 dlightcolor[1] +=  20.0f;
613                                 dlightcolor[2] += 200.0f;
614                         }
615                         if (effects & EF_FLAME)
616                         {
617                                 if (ent->render.model)
618                                 {
619                                         mins[0] = neworg[0] - 16.0f;
620                                         mins[1] = neworg[1] - 16.0f;
621                                         mins[2] = neworg[2] - 16.0f;
622                                         maxs[0] = neworg[0] + 16.0f;
623                                         maxs[1] = neworg[1] + 16.0f;
624                                         maxs[2] = neworg[2] + 16.0f;
625                                         // how many flames to make
626                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
627                                         CL_FlameCube(mins, maxs, temp);
628                                 }
629                                 d = lhrandom(200, 250);
630                                 dlightcolor[0] += d * 1.0f;
631                                 dlightcolor[1] += d * 0.7f;
632                                 dlightcolor[2] += d * 0.3f;
633                         }
634                         if (effects & EF_STARDUST)
635                         {
636                                 if (ent->render.model)
637                                 {
638                                         mins[0] = neworg[0] - 16.0f;
639                                         mins[1] = neworg[1] - 16.0f;
640                                         mins[2] = neworg[2] - 16.0f;
641                                         maxs[0] = neworg[0] + 16.0f;
642                                         maxs[1] = neworg[1] + 16.0f;
643                                         maxs[2] = neworg[2] + 16.0f;
644                                         // how many particles to make
645                                         temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
646                                         CL_Stardust(mins, maxs, temp);
647                                 }
648                                 d = 100;
649                                 dlightcolor[0] += d * 1.0f;
650                                 dlightcolor[1] += d * 0.7f;
651                                 dlightcolor[2] += d * 0.3f;
652                         }
653                 }
654
655                 if (ent->persistent.muzzleflash > 0)
656                 {
657                         AngleVectors (ent->render.angles, v, NULL, NULL);
658
659                         v2[0] = v[0] * 18 + neworg[0];
660                         v2[1] = v[1] * 18 + neworg[1];
661                         v2[2] = v[2] * 18 + neworg[2] + 16;
662                         CL_TraceLine(neworg, v2, v, NULL, 0, true, NULL);
663
664                         CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
665                         ent->persistent.muzzleflash -= cl.frametime * 1000;
666                 }
667
668                 // LordHavoc: if the model has no flags, don't check each
669                 if (ent->render.model && ent->render.model->flags)
670                 {
671                         if (ent->render.model->flags & EF_ROTATE)
672                         {
673                                 ent->render.angles[1] = bobjrotate;
674                                 ent->render.origin[2] += bobjoffset;
675                         }
676                         // only do trails if present in the previous frame as well
677                         if (ent->state_previous.active)
678                         {
679                                 if (ent->render.model->flags & EF_GIB)
680                                         CL_RocketTrail (oldorg, neworg, 2, ent);
681                                 else if (ent->render.model->flags & EF_ZOMGIB)
682                                         CL_RocketTrail (oldorg, neworg, 4, ent);
683                                 else if (ent->render.model->flags & EF_TRACER)
684                                 {
685                                         CL_RocketTrail (oldorg, neworg, 3, ent);
686                                         dlightcolor[0] += 0x10;
687                                         dlightcolor[1] += 0x40;
688                                         dlightcolor[2] += 0x10;
689                                 }
690                                 else if (ent->render.model->flags & EF_TRACER2)
691                                 {
692                                         CL_RocketTrail (oldorg, neworg, 5, ent);
693                                         dlightcolor[0] += 0x50;
694                                         dlightcolor[1] += 0x30;
695                                         dlightcolor[2] += 0x10;
696                                 }
697                                 else if (ent->render.model->flags & EF_ROCKET)
698                                 {
699                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
700                                         dlightcolor[0] += 200.0f;
701                                         dlightcolor[1] += 160.0f;
702                                         dlightcolor[2] +=  80.0f;
703                                 }
704                                 else if (ent->render.model->flags & EF_GRENADE)
705                                 {
706                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility (cigar smoke)
707                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
708                                         else
709                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
710                                 }
711                                 else if (ent->render.model->flags & EF_TRACER3)
712                                 {
713                                         CL_RocketTrail (oldorg, neworg, 6, ent);
714                                         dlightcolor[0] += 0x50;
715                                         dlightcolor[1] += 0x20;
716                                         dlightcolor[2] += 0x40;
717                                 }
718                         }
719                 }
720                 // LordHavoc: customizable glow
721                 if (ent->state_current.glowsize)
722                 {
723                         // * 4 for the expansion from 0-255 to 0-1023 range,
724                         // / 255 to scale down byte colors
725                         VectorMA(dlightcolor, ent->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[ent->state_current.glowcolor], dlightcolor);
726                 }
727                 // LordHavoc: customizable trail
728                 if (ent->render.flags & RENDER_GLOWTRAIL)
729                         CL_RocketTrail2 (oldorg, neworg, ent->state_current.glowcolor, ent);
730
731                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
732                 {
733                         VectorCopy(neworg, v);
734                         // hack to make glowing player light shine on their gun
735                         if (i == cl.viewentity/* && !chase_active.integer*/)
736                                 v[2] += 30;
737                         CL_AllocDlight (&ent->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
738                 }
739
740                 if (chase_active.integer && (ent->render.flags & RENDER_VIEWMODEL))
741                         continue;
742
743                 // don't show entities with no modelindex (note: this still shows
744                 // entities which have a modelindex that resolved to a NULL model)
745                 if (!ent->state_current.modelindex)
746                         continue;
747                 if (effects & EF_NODRAW)
748                         continue;
749
750                 CL_BoundingBoxForEntity(&ent->render);
751                 if (ent->render.model && ent->render.model->name[0] == '*' && ent->render.model->type == mod_brush)
752                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
753
754                 // note: the cl.viewentity and intermission check is to hide player
755                 // shadow during intermission and during the Nehahra movie and
756                 // Nehahra cinematics
757                 if (!(ent->state_current.effects & EF_NOSHADOW)
758                  && !(ent->state_current.effects & EF_ADDITIVE)
759                  && (ent->state_current.alpha == 255)
760                  && !(ent->render.flags & RENDER_VIEWMODEL)
761                  && (i != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility)))
762                         ent->render.flags |= RENDER_SHADOW;
763
764                 if (r_refdef.numentities < r_refdef.maxentities)
765                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
766
767                 if (cl_num_entities < i + 1)
768                         cl_num_entities = i + 1;
769         }
770 }
771
772 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
773 {
774         int i;
775         cl_effect_t *e;
776         if (!modelindex) // sanity check
777                 return;
778         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
779         {
780                 if (e->active)
781                         continue;
782                 e->active = true;
783                 VectorCopy(org, e->origin);
784                 e->modelindex = modelindex;
785                 e->starttime = cl.time;
786                 e->startframe = startframe;
787                 e->endframe = startframe + framecount;
788                 e->framerate = framerate;
789
790                 e->frame = 0;
791                 e->frame1time = cl.time;
792                 e->frame2time = cl.time;
793                 break;
794         }
795 }
796
797 static void CL_RelinkEffects(void)
798 {
799         int i, intframe;
800         cl_effect_t *e;
801         entity_t *ent;
802         float frame;
803
804         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
805         {
806                 if (e->active)
807                 {
808                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
809                         intframe = frame;
810                         if (intframe < 0 || intframe >= e->endframe)
811                         {
812                                 e->active = false;
813                                 memset(e, 0, sizeof(*e));
814                                 continue;
815                         }
816
817                         if (intframe != e->frame)
818                         {
819                                 e->frame = intframe;
820                                 e->frame1time = e->frame2time;
821                                 e->frame2time = cl.time;
822                         }
823
824                         // if we're drawing effects, get a new temp entity
825                         // (NewTempEntity adds it to the render entities list for us)
826                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
827                         {
828                                 // interpolation stuff
829                                 ent->render.frame1 = intframe;
830                                 ent->render.frame2 = intframe + 1;
831                                 if (ent->render.frame2 >= e->endframe)
832                                         ent->render.frame2 = -1; // disappear
833                                 ent->render.framelerp = frame - intframe;
834                                 ent->render.frame1time = e->frame1time;
835                                 ent->render.frame2time = e->frame2time;
836
837                                 // normal stuff
838                                 VectorCopy(e->origin, ent->render.origin);
839                                 ent->render.model = cl.model_precache[e->modelindex];
840                                 ent->render.frame = ent->render.frame2;
841                                 ent->render.colormap = -1; // no special coloring
842                                 ent->render.scale = 1;
843                                 ent->render.alpha = 1;
844
845                                 CL_BoundingBoxForEntity(&ent->render);
846                         }
847                 }
848         }
849 }
850
851 void CL_RelinkBeams (void)
852 {
853         int i;
854         beam_t *b;
855         vec3_t dist, org;
856         float d;
857         entity_t *ent;
858         float yaw, pitch;
859         float forward;
860
861         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
862         {
863                 if (!b->model || b->endtime < cl.time)
864                         continue;
865
866                 // if coming from the player, update the start position
867                 //if (b->entity == cl.viewentity)
868                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
869                 if (b->entity && cl_entities[b->entity].state_current.active)
870                 {
871                         VectorCopy (cl_entities[b->entity].render.origin, b->start);
872                         b->start[2] += 16;
873                 }
874
875                 // calculate pitch and yaw
876                 VectorSubtract (b->end, b->start, dist);
877
878                 if (dist[1] == 0 && dist[0] == 0)
879                 {
880                         yaw = 0;
881                         if (dist[2] > 0)
882                                 pitch = 90;
883                         else
884                                 pitch = 270;
885                 }
886                 else
887                 {
888                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
889                         if (yaw < 0)
890                                 yaw += 360;
891
892                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
893                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
894                         if (pitch < 0)
895                                 pitch += 360;
896                 }
897
898                 // add new entities for the lightning
899                 VectorCopy (b->start, org);
900                 d = VectorNormalizeLength(dist);
901                 while (d > 0)
902                 {
903                         ent = CL_NewTempEntity ();
904                         if (!ent)
905                                 return;
906                         VectorCopy (org, ent->render.origin);
907                         ent->render.model = b->model;
908                         ent->render.effects = EF_FULLBRIGHT;
909                         ent->render.angles[0] = pitch;
910                         ent->render.angles[1] = yaw;
911                         ent->render.angles[2] = rand()%360;
912                         CL_BoundingBoxForEntity(&ent->render);
913                         VectorMA(org, 30, dist, org);
914                         d -= 30;
915                 }
916         }
917 }
918
919 void CL_LerpPlayer(float frac)
920 {
921         int i;
922         float d;
923
924         if (cl.entitydatabase.numframes && cl.viewentity == cl.playerentity)
925         {
926                 cl.viewentorigin[0] = cl.viewentoriginold[0] + frac * (cl.viewentoriginnew[0] - cl.viewentoriginold[0]);
927                 cl.viewentorigin[1] = cl.viewentoriginold[1] + frac * (cl.viewentoriginnew[1] - cl.viewentoriginold[1]);
928                 cl.viewentorigin[2] = cl.viewentoriginold[2] + frac * (cl.viewentoriginnew[2] - cl.viewentoriginold[2]);
929         }
930         else
931                 VectorCopy (cl_entities[cl.viewentity].render.origin, cl.viewentorigin);
932
933         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
934
935         for (i = 0;i < 3;i++)
936                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
937
938         if (cls.demoplayback)
939         {
940                 // interpolate the angles
941                 for (i = 0;i < 3;i++)
942                 {
943                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
944                         if (d > 180)
945                                 d -= 360;
946                         else if (d < -180)
947                                 d += 360;
948                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
949                 }
950         }
951 }
952
953 void CL_RelinkEntities (void)
954 {
955         float frac;
956
957         // fraction from previous network update to current
958         frac = CL_LerpPoint();
959
960         CL_ClearTempEntities();
961         CL_DecayLights();
962         CL_RelinkWorld();
963         CL_RelinkStaticEntities();
964         CL_RelinkNetworkEntities();
965         CL_RelinkEffects();
966         CL_RelinkBeams();
967         CL_MoveParticles();
968
969         CL_LerpPlayer(frac);
970 }
971
972
973 /*
974 ===============
975 CL_ReadFromServer
976
977 Read all incoming data from the server
978 ===============
979 */
980 int CL_ReadFromServer (void)
981 {
982         int ret, netshown;
983
984         cl.oldtime = cl.time;
985         cl.time += cl.frametime;
986
987         netshown = false;
988         do
989         {
990                 ret = CL_GetMessage ();
991                 if (ret == -1)
992                         Host_Error ("CL_ReadFromServer: lost server connection");
993                 if (!ret)
994                         break;
995
996                 cl.last_received_message = realtime;
997
998                 if (cl_shownet.integer)
999                         netshown = true;
1000
1001                 CL_ParseServerMessage ();
1002         }
1003         while (ret && cls.state == ca_connected);
1004
1005         if (netshown)
1006                 Con_Printf ("\n");
1007
1008         r_refdef.numentities = 0;
1009         cl_num_entities = 0;
1010         cl_num_brushmodel_entities = 0;
1011
1012         if (cls.state == ca_connected && cls.signon == SIGNONS)
1013         {
1014                 CL_RelinkEntities ();
1015
1016                 // run cgame code (which can add more entities)
1017                 CL_CGVM_Frame();
1018         }
1019
1020 //
1021 // bring the links up to date
1022 //
1023         return 0;
1024 }
1025
1026 /*
1027 =================
1028 CL_SendCmd
1029 =================
1030 */
1031 void CL_SendCmd (void)
1032 {
1033         usercmd_t               cmd;
1034
1035         if (cls.state != ca_connected)
1036                 return;
1037
1038         if (cls.signon == SIGNONS)
1039         {
1040         // get basic movement from keyboard
1041                 CL_BaseMove (&cmd);
1042
1043                 IN_PreMove(); // OS independent code
1044
1045         // allow mice or other external controllers to add to the move
1046                 IN_Move (&cmd);
1047
1048                 IN_PostMove(); // OS independent code
1049
1050         // send the unreliable message
1051                 CL_SendMove (&cmd);
1052         }
1053 #ifndef NOROUTINGFIX
1054         else if (cls.signon == 0 && !cls.demoplayback)
1055         {
1056                 // LordHavoc: fix for NAT routing of netquake:
1057                 // bounce back a clc_nop message to the newly allocated server port,
1058                 // to establish a routing connection for incoming frames,
1059                 // the server waits for this before sending anything
1060                 if (realtime > cl.sendnoptime)
1061                 {
1062                         cl.sendnoptime = realtime + 3;
1063                         Con_DPrintf("sending clc_nop to get server's attention\n");
1064                         {
1065                                 sizebuf_t buf;
1066                                 qbyte data[128];
1067                                 buf.maxsize = 128;
1068                                 buf.cursize = 0;
1069                                 buf.data = data;
1070                                 MSG_WriteByte(&buf, clc_nop);
1071                                 if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
1072                                 {
1073                                         Con_Printf ("CL_SendCmd: lost server connection\n");
1074                                         CL_Disconnect ();
1075                                 }
1076                         }
1077                 }
1078         }
1079 #endif
1080
1081         if (cls.demoplayback)
1082         {
1083                 SZ_Clear (&cls.message);
1084                 return;
1085         }
1086
1087 // send the reliable message
1088         if (!cls.message.cursize)
1089                 return;         // no message at all
1090
1091         if (!NET_CanSendMessage (cls.netcon))
1092         {
1093                 Con_DPrintf ("CL_WriteToServer: can't send\n");
1094                 if (developer.integer)
1095                         SZ_HexDumpToConsole(&cls.message);
1096                 return;
1097         }
1098
1099         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
1100                 Host_Error ("CL_WriteToServer: lost server connection");
1101
1102         SZ_Clear (&cls.message);
1103 }
1104
1105 // LordHavoc: pausedemo command
1106 static void CL_PauseDemo_f (void)
1107 {
1108         cls.demopaused = !cls.demopaused;
1109         if (cls.demopaused)
1110                 Con_Printf("Demo paused\n");
1111         else
1112                 Con_Printf("Demo unpaused\n");
1113 }
1114
1115 /*
1116 ======================
1117 CL_PModel_f
1118 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
1119 ======================
1120 */
1121 static void CL_PModel_f (void)
1122 {
1123         int i;
1124         eval_t *val;
1125
1126         if (Cmd_Argc () == 1)
1127         {
1128                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
1129                 return;
1130         }
1131         i = atoi(Cmd_Argv(1));
1132
1133         if (cmd_source == src_command)
1134         {
1135                 if (cl_pmodel.integer == i)
1136                         return;
1137                 Cvar_SetValue ("_cl_pmodel", i);
1138                 if (cls.state == ca_connected)
1139                         Cmd_ForwardToServer ();
1140                 return;
1141         }
1142
1143         host_client->pmodel = i;
1144         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
1145                 val->_float = i;
1146 }
1147
1148 /*
1149 ======================
1150 CL_Fog_f
1151 ======================
1152 */
1153 static void CL_Fog_f (void)
1154 {
1155         if (Cmd_Argc () == 1)
1156         {
1157                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1158                 return;
1159         }
1160         fog_density = atof(Cmd_Argv(1));
1161         fog_red = atof(Cmd_Argv(2));
1162         fog_green = atof(Cmd_Argv(3));
1163         fog_blue = atof(Cmd_Argv(4));
1164 }
1165
1166 /*
1167 =================
1168 CL_Init
1169 =================
1170 */
1171 void CL_Init (void)
1172 {
1173         cl_scores_mempool = Mem_AllocPool("client player info");
1174         cl_entities_mempool = Mem_AllocPool("client entities");
1175         cl_refdef_mempool = Mem_AllocPool("refdef");
1176
1177         memset(&r_refdef, 0, sizeof(r_refdef));
1178         // max entities sent to renderer per frame
1179         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1180         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1181         // 256k drawqueue buffer
1182         r_refdef.maxdrawqueuesize = 256 * 1024;
1183         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1184
1185         SZ_Alloc (&cls.message, 1024, "cls.message");
1186
1187         CL_InitInput ();
1188         CL_InitTEnts ();
1189
1190 //
1191 // register our commands
1192 //
1193         Cvar_RegisterVariable (&cl_name);
1194         Cvar_RegisterVariable (&cl_color);
1195         if (gamemode == GAME_NEHAHRA)
1196                 Cvar_RegisterVariable (&cl_pmodel);
1197         Cvar_RegisterVariable (&cl_upspeed);
1198         Cvar_RegisterVariable (&cl_forwardspeed);
1199         Cvar_RegisterVariable (&cl_backspeed);
1200         Cvar_RegisterVariable (&cl_sidespeed);
1201         Cvar_RegisterVariable (&cl_movespeedkey);
1202         Cvar_RegisterVariable (&cl_yawspeed);
1203         Cvar_RegisterVariable (&cl_pitchspeed);
1204         Cvar_RegisterVariable (&cl_anglespeedkey);
1205         Cvar_RegisterVariable (&cl_shownet);
1206         Cvar_RegisterVariable (&cl_nolerp);
1207         Cvar_RegisterVariable (&lookspring);
1208         Cvar_RegisterVariable (&lookstrafe);
1209         Cvar_RegisterVariable (&sensitivity);
1210         Cvar_RegisterVariable (&freelook);
1211
1212         Cvar_RegisterVariable (&m_pitch);
1213         Cvar_RegisterVariable (&m_yaw);
1214         Cvar_RegisterVariable (&m_forward);
1215         Cvar_RegisterVariable (&m_side);
1216
1217         Cvar_RegisterVariable (&cl_itembobspeed);
1218         Cvar_RegisterVariable (&cl_itembobheight);
1219
1220         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1221         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1222         Cmd_AddCommand ("record", CL_Record_f);
1223         Cmd_AddCommand ("stop", CL_Stop_f);
1224         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1225         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1226
1227         Cmd_AddCommand ("fog", CL_Fog_f);
1228
1229         // LordHavoc: added pausedemo
1230         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1231         if (gamemode == GAME_NEHAHRA)
1232                 Cmd_AddCommand ("pmodel", CL_PModel_f);
1233
1234         Cvar_RegisterVariable(&r_draweffects);
1235         Cvar_RegisterVariable(&cl_explosions);
1236         Cvar_RegisterVariable(&cl_stainmaps);
1237
1238         CL_Parse_Init();
1239         CL_Particles_Init();
1240         CL_Screen_Init();
1241         CL_CGVM_Init();
1242
1243         CL_Video_Init();
1244 }
1245