chthon lightning no longer uses beam polygons
[divverent/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 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1"};
57 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1"};
58
59 mempool_t *cl_scores_mempool;
60 mempool_t *cl_refdef_mempool;
61 mempool_t *cl_entities_mempool;
62
63 client_static_t cls;
64 client_state_t  cl;
65
66 int cl_max_entities;
67 int cl_max_static_entities;
68 int cl_max_temp_entities;
69 int cl_max_effects;
70 int cl_max_beams;
71 int cl_max_dlights;
72 int cl_max_lightstyle;
73 int cl_max_brushmodel_entities;
74
75 entity_t *cl_entities;
76 qbyte *cl_entities_active;
77 entity_t *cl_static_entities;
78 entity_t *cl_temp_entities;
79 cl_effect_t *cl_effects;
80 beam_t *cl_beams;
81 dlight_t *cl_dlights;
82 lightstyle_t *cl_lightstyle;
83 entity_render_t **cl_brushmodel_entities;
84
85 int cl_num_entities;
86 int cl_num_static_entities;
87 int cl_num_temp_entities;
88 int cl_num_brushmodel_entities;
89
90 /*
91 =====================
92 CL_ClearState
93
94 =====================
95 */
96 void CL_ClearState (void)
97 {
98         int i;
99
100         if (!sv.active)
101                 Host_ClearMemory ();
102
103         Mem_EmptyPool(cl_scores_mempool);
104         Mem_EmptyPool(cl_entities_mempool);
105
106 // wipe the entire cl structure
107         memset (&cl, 0, sizeof(cl));
108
109         SZ_Clear (&cls.message);
110
111         cl_num_entities = 0;
112         cl_num_static_entities = 0;
113         cl_num_temp_entities = 0;
114         cl_num_brushmodel_entities = 0;
115
116         // tweak these if the game runs out
117         cl_max_entities = MAX_EDICTS;
118         cl_max_static_entities = 256;
119         cl_max_temp_entities = 512;
120         cl_max_effects = 256;
121         cl_max_beams = 24;
122         cl_max_dlights = MAX_DLIGHTS;
123         cl_max_lightstyle = MAX_LIGHTSTYLES;
124         cl_max_brushmodel_entities = MAX_EDICTS;
125
126         cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
127         cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
128         cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
129         cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
130         cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
131         cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
132         cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
133         cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
134         cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
135
136         CL_Screen_NewMap();
137
138         CL_Particles_Clear();
139
140         // LordHavoc: have to set up the baseline info for alpha and other stuff
141         for (i = 0;i < cl_max_entities;i++)
142         {
143                 ClearStateToDefault(&cl_entities[i].state_baseline);
144                 ClearStateToDefault(&cl_entities[i].state_previous);
145                 ClearStateToDefault(&cl_entities[i].state_current);
146         }
147
148         CL_CGVM_Clear();
149 }
150
151 /*
152 =====================
153 CL_Disconnect
154
155 Sends a disconnect message to the server
156 This is also called on Host_Error, so it shouldn't cause any errors
157 =====================
158 */
159 void CL_Disconnect (void)
160 {
161         if (cls.state == ca_dedicated)
162                 return;
163
164 // stop sounds (especially looping!)
165         S_StopAllSounds (true);
166
167         // clear contents blends
168         cl.cshifts[0].percent = 0;
169         cl.cshifts[1].percent = 0;
170         cl.cshifts[2].percent = 0;
171         cl.cshifts[3].percent = 0;
172
173         cl.worldmodel = NULL;
174
175         if (cls.demoplayback)
176                 CL_StopPlayback ();
177         else if (cls.state == ca_connected)
178         {
179                 if (cls.demorecording)
180                         CL_Stop_f ();
181
182                 Con_DPrintf ("Sending clc_disconnect\n");
183                 SZ_Clear (&cls.message);
184                 MSG_WriteByte (&cls.message, clc_disconnect);
185                 NET_SendUnreliableMessage (cls.netcon, &cls.message);
186                 SZ_Clear (&cls.message);
187                 NET_Close (cls.netcon);
188                 cls.state = ca_disconnected; // prevent this code from executing again during Host_ShutdownServer
189                 // if running a local server, shut it down
190                 if (sv.active)
191                         Host_ShutdownServer(false);
192         }
193         cls.state = ca_disconnected;
194
195         cls.demoplayback = cls.timedemo = false;
196         cls.signon = 0;
197 }
198
199 void CL_Disconnect_f (void)
200 {
201         CL_Disconnect ();
202         if (sv.active)
203                 Host_ShutdownServer (false);
204 }
205
206
207
208
209 /*
210 =====================
211 CL_EstablishConnection
212
213 Host should be either "local" or a net address to be passed on
214 =====================
215 */
216 void CL_EstablishConnection (char *host)
217 {
218         if (cls.state == ca_dedicated)
219                 return;
220
221         if (cls.demoplayback)
222                 return;
223
224         CL_Disconnect ();
225
226         cls.netcon = NET_Connect (host);
227         if (!cls.netcon)
228                 Host_Error ("CL_Connect: connect failed\n");
229         Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
230
231         cls.demonum = -1;                       // not in the demo loop now
232         cls.state = ca_connected;
233         cls.signon = 0;                         // need all the signon messages before playing
234
235         CL_ClearState ();
236 }
237
238 /*
239 ==============
240 CL_PrintEntities_f
241 ==============
242 */
243 static void CL_PrintEntities_f (void)
244 {
245         entity_t *ent;
246         int i, j;
247         char name[32];
248
249         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
250         {
251                 if (!ent->state_current.active)
252                         continue;
253
254                 if (ent->render.model)
255                         strncpy(name, ent->render.model->name, 25);
256                 else
257                         strcpy(name, "--no model--");
258                 name[25] = 0;
259                 for (j = strlen(name);j < 25;j++)
260                         name[j] = ' ';
261                 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);
262         }
263 }
264
265 static const vec3_t nomodelmins = {-16, -16, -16};
266 static const vec3_t nomodelmaxs = {16, 16, 16};
267 void CL_BoundingBoxForEntity(entity_render_t *ent)
268 {
269         if (ent->model)
270         {
271                 if (ent->angles[0] || ent->angles[2])
272                 {
273                         // pitch or roll
274                         VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
275                         VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
276                 }
277                 else if (ent->angles[1])
278                 {
279                         // yaw
280                         VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
281                         VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
282                 }
283                 else
284                 {
285                         VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
286                         VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
287                 }
288         }
289         else
290         {
291                 VectorAdd(ent->origin, nomodelmins, ent->mins);
292                 VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
293         }
294 }
295
296 void CL_LerpUpdate(entity_t *e)
297 {
298         entity_persistent_t *p;
299         entity_render_t *r;
300         p = &e->persistent;
301         r = &e->render;
302
303         if (p->modelindex != e->state_current.modelindex)
304         {
305                 // reset all interpolation information
306                 p->modelindex = e->state_current.modelindex;
307                 p->frame1 = p->frame2 = e->state_current.frame;
308                 p->frame1time = p->frame2time = cl.time;
309                 p->framelerp = 1;
310         }
311         else if (p->frame2 != e->state_current.frame)
312         {
313                 // transition to new frame
314                 p->frame1 = p->frame2;
315                 p->frame1time = p->frame2time;
316                 p->frame2 = e->state_current.frame;
317                 p->frame2time = cl.time;
318                 p->framelerp = 0;
319         }
320         else
321         {
322                 // update transition
323                 p->framelerp = (cl.time - p->frame2time) * 10;
324                 p->framelerp = bound(0, p->framelerp, 1);
325         }
326
327         r->model = cl.model_precache[e->state_current.modelindex];
328         Mod_CheckLoaded(r->model);
329         r->frame = e->state_current.frame;
330         r->frame1 = p->frame1;
331         r->frame2 = p->frame2;
332         r->framelerp = p->framelerp;
333         r->frame1time = p->frame1time;
334         r->frame2time = p->frame2time;
335 }
336
337 /*
338 ===============
339 CL_LerpPoint
340
341 Determines the fraction between the last two messages that the objects
342 should be put at.
343 ===============
344 */
345 static float CL_LerpPoint (void)
346 {
347         float f;
348
349         // dropped packet, or start of demo
350         if (cl.mtime[1] < cl.mtime[0] - 0.1)
351                 cl.mtime[1] = cl.mtime[0] - 0.1;
352
353         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
354
355         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
356         f = cl.mtime[0] - cl.mtime[1];
357         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
358         {
359                 cl.time = cl.mtime[0];
360                 return 1;
361         }
362
363         f = (cl.time - cl.mtime[1]) / f;
364         return bound(0, f, 1);
365 }
366
367 void CL_ClearTempEntities (void)
368 {
369         cl_num_temp_entities = 0;
370 }
371
372 entity_t *CL_NewTempEntity (void)
373 {
374         entity_t *ent;
375
376         if (r_refdef.numentities >= r_refdef.maxentities)
377                 return NULL;
378         if (cl_num_temp_entities >= cl_max_temp_entities)
379                 return NULL;
380         ent = &cl_temp_entities[cl_num_temp_entities++];
381         memset (ent, 0, sizeof(*ent));
382         r_refdef.entities[r_refdef.numentities++] = &ent->render;
383
384         ent->render.colormap = -1; // no special coloring
385         ent->render.scale = 1;
386         ent->render.alpha = 1;
387         return ent;
388 }
389
390 void CL_AllocDlight (entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
391 {
392         int i;
393         dlight_t *dl;
394
395         /*
396 // first look for an exact key match
397         if (ent)
398         {
399                 dl = cl_dlights;
400                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
401                         if (dl->ent == ent)
402                                 goto dlightsetup;
403         }
404         */
405
406 // then look for anything else
407         dl = cl_dlights;
408         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
409                 if (!dl->radius)
410                         goto dlightsetup;
411
412         // unable to find one
413         return;
414
415 dlightsetup:
416         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
417         memset (dl, 0, sizeof(*dl));
418         dl->ent = ent;
419         VectorCopy(org, dl->origin);
420         dl->radius = radius;
421         dl->color[0] = red;
422         dl->color[1] = green;
423         dl->color[2] = blue;
424         dl->decay = decay;
425         if (lifetime)
426                 dl->die = cl.time + lifetime;
427         else
428                 dl->die = 0;
429 }
430
431 void CL_DecayLights (void)
432 {
433         int i;
434         dlight_t *dl;
435         float time;
436
437         time = cl.time - cl.oldtime;
438
439         dl = cl_dlights;
440         for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
441         {
442                 if (!dl->radius)
443                         continue;
444                 if (dl->die < cl.time)
445                 {
446                         dl->radius = 0;
447                         continue;
448                 }
449
450                 dl->radius -= time*dl->decay;
451                 if (dl->radius < 0)
452                         dl->radius = 0;
453         }
454 }
455
456 void CL_RelinkWorld (void)
457 {
458         if (cl_num_entities < 1)
459                 cl_num_entities = 1;
460         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &cl_entities[0].render;
461         CL_BoundingBoxForEntity(&cl_entities[0].render);
462 }
463
464 static void CL_RelinkStaticEntities(void)
465 {
466         int i;
467         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
468         {
469                 Mod_CheckLoaded(cl_static_entities[i].render.model);
470                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
471         }
472 }
473
474 /*
475 ===============
476 CL_RelinkEntities
477 ===============
478 */
479 extern qboolean Nehahrademcompatibility;
480 static void CL_RelinkNetworkEntities(void)
481 {
482         entity_t *ent;
483         int i, effects, temp;
484         float d, bobjrotate, bobjoffset, lerp;
485         vec3_t oldorg, neworg, delta, dlightcolor, v, v2, mins, maxs;
486
487         bobjrotate = ANGLEMOD(100*cl.time);
488         if (cl_itembobheight.value)
489                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
490         else
491                 bobjoffset = 0;
492
493         // start on the entity after the world
494         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
495         {
496                 // if the object isn't active in the current network frame, skip it
497                 if (!cl_entities_active[i])
498                         continue;
499                 if (!ent->state_current.active)
500                 {
501                         cl_entities_active[i] = false;
502                         continue;
503                 }
504
505                 VectorCopy(ent->persistent.trail_origin, oldorg);
506
507                 if (!ent->state_previous.active)
508                 {
509                         // only one state available
510                         VectorCopy (ent->persistent.neworigin, neworg);
511                         VectorCopy (ent->persistent.newangles, ent->render.angles);
512                         VectorCopy (neworg, oldorg);
513                 }
514                 else
515                 {
516                         // if the delta is large, assume a teleport and don't lerp
517                         VectorSubtract(ent->persistent.neworigin, ent->persistent.oldorigin, delta);
518                         if (ent->persistent.lerpdeltatime > 0)
519                         {
520                                 lerp = (cl.time - ent->persistent.lerpstarttime) / ent->persistent.lerpdeltatime;
521                                 if (lerp < 1)
522                                 {
523                                         // interpolate the origin and angles
524                                         VectorMA(ent->persistent.oldorigin, lerp, delta, neworg);
525                                         VectorSubtract(ent->persistent.newangles, ent->persistent.oldangles, delta);
526                                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
527                                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
528                                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
529                                         VectorMA(ent->persistent.oldangles, lerp, delta, ent->render.angles);
530                                 }
531                                 else
532                                 {
533                                         // no interpolation
534                                         VectorCopy (ent->persistent.neworigin, neworg);
535                                         VectorCopy (ent->persistent.newangles, ent->render.angles);
536                                 }
537                         }
538                         else
539                         {
540                                 // no interpolation
541                                 VectorCopy (ent->persistent.neworigin, neworg);
542                                 VectorCopy (ent->persistent.newangles, ent->render.angles);
543                         }
544                 }
545
546                 VectorCopy (neworg, ent->persistent.trail_origin);
547                 // persistent.modelindex will be updated by CL_LerpUpdate
548                 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
549                         VectorCopy(neworg, oldorg);
550
551                 VectorCopy (neworg, ent->render.origin);
552                 ent->render.flags = ent->state_current.flags;
553                 if (i == cl.viewentity)
554                         ent->render.flags |= RENDER_EXTERIORMODEL;
555                 ent->render.effects = effects = ent->state_current.effects;
556                 if (ent->state_current.flags & RENDER_COLORMAPPED)
557                         ent->render.colormap = ent->state_current.colormap;
558                 else if (cl.scores == NULL || !ent->state_current.colormap)
559                         ent->render.colormap = -1; // no special coloring
560                 else
561                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
562                 ent->render.skinnum = ent->state_current.skin;
563                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
564                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
565
566                 // update interpolation info
567                 CL_LerpUpdate(ent);
568
569                 // handle effects now...
570                 dlightcolor[0] = 0;
571                 dlightcolor[1] = 0;
572                 dlightcolor[2] = 0;
573
574                 // LordHavoc: if the entity has no effects, don't check each
575                 if (effects)
576                 {
577                         if (effects & EF_BRIGHTFIELD)
578                         {
579                                 if (gamemode == GAME_NEXIUZ)
580                                 {
581                                         dlightcolor[0] += 100.0f;
582                                         dlightcolor[1] += 200.0f;
583                                         dlightcolor[2] += 400.0f;
584                                         // don't do trail if we have no previous location
585                                         if (ent->state_previous.active)
586                                                 CL_RocketTrail (oldorg, neworg, 8, ent);
587                                 }
588                                 else
589                                         CL_EntityParticles (ent);
590                         }
591                         if (effects & EF_MUZZLEFLASH)
592                                 ent->persistent.muzzleflash = 100.0f;
593                         if (effects & EF_DIMLIGHT)
594                         {
595                                 dlightcolor[0] += 200.0f;
596                                 dlightcolor[1] += 200.0f;
597                                 dlightcolor[2] += 200.0f;
598                         }
599                         if (effects & EF_BRIGHTLIGHT)
600                         {
601                                 dlightcolor[0] += 400.0f;
602                                 dlightcolor[1] += 400.0f;
603                                 dlightcolor[2] += 400.0f;
604                         }
605                         // LordHavoc: added EF_RED and EF_BLUE
606                         if (effects & EF_RED) // red
607                         {
608                                 dlightcolor[0] += 200.0f;
609                                 dlightcolor[1] +=  20.0f;
610                                 dlightcolor[2] +=  20.0f;
611                         }
612                         if (effects & EF_BLUE) // blue
613                         {
614                                 dlightcolor[0] +=  20.0f;
615                                 dlightcolor[1] +=  20.0f;
616                                 dlightcolor[2] += 200.0f;
617                         }
618                         if (effects & EF_FLAME)
619                         {
620                                 if (ent->render.model)
621                                 {
622                                         mins[0] = neworg[0] - 16.0f;
623                                         mins[1] = neworg[1] - 16.0f;
624                                         mins[2] = neworg[2] - 16.0f;
625                                         maxs[0] = neworg[0] + 16.0f;
626                                         maxs[1] = neworg[1] + 16.0f;
627                                         maxs[2] = neworg[2] + 16.0f;
628                                         // how many flames to make
629                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
630                                         CL_FlameCube(mins, maxs, temp);
631                                 }
632                                 d = lhrandom(200, 250);
633                                 dlightcolor[0] += d * 1.0f;
634                                 dlightcolor[1] += d * 0.7f;
635                                 dlightcolor[2] += d * 0.3f;
636                         }
637                         if (effects & EF_STARDUST)
638                         {
639                                 if (ent->render.model)
640                                 {
641                                         mins[0] = neworg[0] - 16.0f;
642                                         mins[1] = neworg[1] - 16.0f;
643                                         mins[2] = neworg[2] - 16.0f;
644                                         maxs[0] = neworg[0] + 16.0f;
645                                         maxs[1] = neworg[1] + 16.0f;
646                                         maxs[2] = neworg[2] + 16.0f;
647                                         // how many particles to make
648                                         temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
649                                         CL_Stardust(mins, maxs, temp);
650                                 }
651                                 d = 100;
652                                 dlightcolor[0] += d * 1.0f;
653                                 dlightcolor[1] += d * 0.7f;
654                                 dlightcolor[2] += d * 0.3f;
655                         }
656                 }
657
658                 if (ent->persistent.muzzleflash > 0)
659                 {
660                         AngleVectors (ent->render.angles, v, NULL, NULL);
661
662                         v2[0] = v[0] * 18 + neworg[0];
663                         v2[1] = v[1] * 18 + neworg[1];
664                         v2[2] = v[2] * 18 + neworg[2] + 16;
665                         CL_TraceLine(neworg, v2, v, NULL, 0, true, NULL);
666
667                         CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
668                         ent->persistent.muzzleflash -= cl.frametime * 1000;
669                 }
670
671                 // LordHavoc: if the model has no flags, don't check each
672                 if (ent->render.model && ent->render.model->flags)
673                 {
674                         if (ent->render.model->flags & EF_ROTATE)
675                         {
676                                 ent->render.angles[1] = bobjrotate;
677                                 ent->render.origin[2] += bobjoffset;
678                         }
679                         // only do trails if present in the previous frame as well
680                         if (ent->state_previous.active)
681                         {
682                                 if (ent->render.model->flags & EF_GIB)
683                                         CL_RocketTrail (oldorg, neworg, 2, ent);
684                                 else if (ent->render.model->flags & EF_ZOMGIB)
685                                         CL_RocketTrail (oldorg, neworg, 4, ent);
686                                 else if (ent->render.model->flags & EF_TRACER)
687                                 {
688                                         CL_RocketTrail (oldorg, neworg, 3, ent);
689                                         dlightcolor[0] += 0x10;
690                                         dlightcolor[1] += 0x40;
691                                         dlightcolor[2] += 0x10;
692                                 }
693                                 else if (ent->render.model->flags & EF_TRACER2)
694                                 {
695                                         CL_RocketTrail (oldorg, neworg, 5, ent);
696                                         dlightcolor[0] += 0x50;
697                                         dlightcolor[1] += 0x30;
698                                         dlightcolor[2] += 0x10;
699                                 }
700                                 else if (ent->render.model->flags & EF_ROCKET)
701                                 {
702                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
703                                         dlightcolor[0] += 200.0f;
704                                         dlightcolor[1] += 160.0f;
705                                         dlightcolor[2] +=  80.0f;
706                                 }
707                                 else if (ent->render.model->flags & EF_GRENADE)
708                                 {
709                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility (cigar smoke)
710                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
711                                         else
712                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
713                                 }
714                                 else if (ent->render.model->flags & EF_TRACER3)
715                                 {
716                                         CL_RocketTrail (oldorg, neworg, 6, ent);
717                                         dlightcolor[0] += 0x50;
718                                         dlightcolor[1] += 0x20;
719                                         dlightcolor[2] += 0x40;
720                                 }
721                         }
722                 }
723                 // LordHavoc: customizable glow
724                 if (ent->state_current.glowsize)
725                 {
726                         // * 4 for the expansion from 0-255 to 0-1023 range,
727                         // / 255 to scale down byte colors
728                         VectorMA(dlightcolor, ent->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[ent->state_current.glowcolor], dlightcolor);
729                 }
730                 // LordHavoc: customizable trail
731                 if (ent->render.flags & RENDER_GLOWTRAIL)
732                         CL_RocketTrail2 (oldorg, neworg, ent->state_current.glowcolor, ent);
733
734                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
735                 {
736                         VectorCopy(neworg, v);
737                         // hack to make glowing player light shine on their gun
738                         if (i == cl.viewentity/* && !chase_active.integer*/)
739                                 v[2] += 30;
740                         CL_AllocDlight (&ent->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
741                 }
742
743                 if (chase_active.integer && (ent->render.flags & RENDER_VIEWMODEL))
744                         continue;
745
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 (!ent->state_current.modelindex)
749                         continue;
750                 if (effects & EF_NODRAW)
751                         continue;
752
753                 CL_BoundingBoxForEntity(&ent->render);
754                 if (ent->render.model && ent->render.model->name[0] == '*' && ent->render.model->type == mod_brush)
755                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
756
757                 // note: the cl.viewentity and intermission check is to hide player
758                 // shadow during intermission and during the Nehahra movie and
759                 // Nehahra cinematics
760                 if (!(ent->state_current.effects & EF_NOSHADOW)
761                  && !(ent->state_current.effects & EF_ADDITIVE)
762                  && (ent->state_current.alpha == 255)
763                  && !(ent->render.flags & RENDER_VIEWMODEL)
764                  && (i != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility)))
765                         ent->render.flags |= RENDER_SHADOW;
766
767                 if (r_refdef.numentities < r_refdef.maxentities)
768                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
769
770                 if (cl_num_entities < i + 1)
771                         cl_num_entities = i + 1;
772         }
773 }
774
775 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
776 {
777         int i;
778         cl_effect_t *e;
779         if (!modelindex) // sanity check
780                 return;
781         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
782         {
783                 if (e->active)
784                         continue;
785                 e->active = true;
786                 VectorCopy(org, e->origin);
787                 e->modelindex = modelindex;
788                 e->starttime = cl.time;
789                 e->startframe = startframe;
790                 e->endframe = startframe + framecount;
791                 e->framerate = framerate;
792
793                 e->frame = 0;
794                 e->frame1time = cl.time;
795                 e->frame2time = cl.time;
796                 break;
797         }
798 }
799
800 static void CL_RelinkEffects(void)
801 {
802         int i, intframe;
803         cl_effect_t *e;
804         entity_t *ent;
805         float frame;
806
807         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
808         {
809                 if (e->active)
810                 {
811                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
812                         intframe = frame;
813                         if (intframe < 0 || intframe >= e->endframe)
814                         {
815                                 e->active = false;
816                                 memset(e, 0, sizeof(*e));
817                                 continue;
818                         }
819
820                         if (intframe != e->frame)
821                         {
822                                 e->frame = intframe;
823                                 e->frame1time = e->frame2time;
824                                 e->frame2time = cl.time;
825                         }
826
827                         // if we're drawing effects, get a new temp entity
828                         // (NewTempEntity adds it to the render entities list for us)
829                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
830                         {
831                                 // interpolation stuff
832                                 ent->render.frame1 = intframe;
833                                 ent->render.frame2 = intframe + 1;
834                                 if (ent->render.frame2 >= e->endframe)
835                                         ent->render.frame2 = -1; // disappear
836                                 ent->render.framelerp = frame - intframe;
837                                 ent->render.frame1time = e->frame1time;
838                                 ent->render.frame2time = e->frame2time;
839
840                                 // normal stuff
841                                 VectorCopy(e->origin, ent->render.origin);
842                                 ent->render.model = cl.model_precache[e->modelindex];
843                                 ent->render.frame = ent->render.frame2;
844                                 ent->render.colormap = -1; // no special coloring
845                                 ent->render.scale = 1;
846                                 ent->render.alpha = 1;
847
848                                 CL_BoundingBoxForEntity(&ent->render);
849                         }
850                 }
851         }
852 }
853
854 void CL_RelinkBeams (void)
855 {
856         int i;
857         beam_t *b;
858         vec3_t dist, org;
859         float d;
860         entity_t *ent;
861         float yaw, pitch;
862         float forward;
863
864         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
865         {
866                 if (!b->model || b->endtime < cl.time)
867                         continue;
868
869                 // if coming from the player, update the start position
870                 //if (b->entity == cl.viewentity)
871                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
872                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
873                 {
874                         entity_state_t *p = &cl_entities[b->entity].state_previous;
875                         //entity_state_t *c = &cl_entities[b->entity].state_current;
876                         entity_render_t *r = &cl_entities[b->entity].render;
877                         matrix4x4_t matrix, imatrix;
878                         if (b->relativestartvalid == 2)
879                         {
880                                 // not really valid yet, we need to get the orientation now
881                                 // (ParseBeam flagged this because it is received before
882                                 //  entities are received, by now they have been received)
883                                 // note: because players create lightning in their think
884                                 // function (which occurs before movement), they actually
885                                 // have some lag in it's location, so compare to the
886                                 // previous player state, not the latest
887                                 if (b->entity == cl.viewentity)
888                                         Matrix4x4_CreateFromQuakeEntity(&matrix, cl.viewentoriginold[0], cl.viewentoriginold[1], cl.viewentoriginold[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
889                                 else
890                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
891                                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
892                                 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
893                                 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
894                                 b->relativestartvalid = 1;
895                         }
896                         else
897                         {
898                                 if (b->entity == cl.viewentity)
899                                         Matrix4x4_CreateFromQuakeEntity(&matrix, cl.viewentorigin[0], cl.viewentorigin[1], cl.viewentorigin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
900                                 else
901                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
902                                 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
903                                 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
904                         }
905                 }
906
907                 if (b->lightning && cl_beams_polygons.integer)
908                         continue;
909
910                 // calculate pitch and yaw
911                 VectorSubtract (b->end, b->start, dist);
912
913                 if (dist[1] == 0 && dist[0] == 0)
914                 {
915                         yaw = 0;
916                         if (dist[2] > 0)
917                                 pitch = 90;
918                         else
919                                 pitch = 270;
920                 }
921                 else
922                 {
923                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
924                         if (yaw < 0)
925                                 yaw += 360;
926
927                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
928                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
929                         if (pitch < 0)
930                                 pitch += 360;
931                 }
932
933                 // add new entities for the lightning
934                 VectorCopy (b->start, org);
935                 d = VectorNormalizeLength(dist);
936                 while (d > 0)
937                 {
938                         ent = CL_NewTempEntity ();
939                         if (!ent)
940                                 return;
941                         VectorCopy (org, ent->render.origin);
942                         ent->render.model = b->model;
943                         ent->render.effects = EF_FULLBRIGHT;
944                         ent->render.angles[0] = pitch;
945                         ent->render.angles[1] = yaw;
946                         ent->render.angles[2] = rand()%360;
947                         CL_BoundingBoxForEntity(&ent->render);
948                         VectorMA(org, 30, dist, org);
949                         d -= 30;
950                 }
951         }
952 }
953
954 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "4"};
955 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5"};
956 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "1024"};
957 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1"};
958 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1"};
959 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1"};
960
961 rtexture_t *r_lightningbeamtexture;
962 rtexturepool_t *r_lightningbeamtexturepool;
963
964 int r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
965
966 void r_lightningbeams_start(void)
967 {
968         float r, g, b, intensity, fx, width, center;
969         int x, y;
970         qbyte *data, *noise1, *noise2;
971         data = Mem_Alloc(tempmempool, 32 * 512 * 4);
972         noise1 = Mem_Alloc(tempmempool, 512 * 512);
973         noise2 = Mem_Alloc(tempmempool, 512 * 512);
974         fractalnoise(noise1, 512, 8);
975         fractalnoise(noise2, 512, 16);
976
977         for (y = 0;y < 512;y++)
978         {
979                 width = noise1[y * 512] * (1.0f / 256.0f) * 3.0f + 3.0f;
980                 center = (noise1[y * 512 + 64] / 256.0f) * (32.0f - (width + 1.0f) * 2.0f) + (width + 1.0f);
981                 for (x = 0;x < 32;x++, fx++)
982                 {
983                         fx = (x - center) / width;
984                         intensity = (1.0f - fx * fx) * (noise2[y*512+x] * (1.0f / 256.0f) * 0.33f + 0.66f);
985                         intensity = bound(0, intensity, 1);
986                         r = intensity * 2.0f - 1.0f;
987                         g = intensity * 3.0f - 1.0f;
988                         b = intensity * 3.0f;
989                         data[(y * 32 + x) * 4 + 0] = (qbyte)(bound(0, r, 1) * 255.0f);
990                         data[(y * 32 + x) * 4 + 1] = (qbyte)(bound(0, g, 1) * 255.0f);
991                         data[(y * 32 + x) * 4 + 2] = (qbyte)(bound(0, b, 1) * 255.0f);
992                         data[(y * 32 + x) * 4 + 3] = (qbyte)255;
993                 }
994         }
995
996         r_lightningbeamtexturepool = R_AllocTexturePool();
997         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", 32, 512, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
998         Mem_Free(noise1);
999         Mem_Free(noise2);
1000         Mem_Free(data);
1001 }
1002
1003 void r_lightningbeams_shutdown(void)
1004 {
1005         r_lightningbeamtexture = NULL;
1006         R_FreeTexturePool(&r_lightningbeamtexturepool);
1007 }
1008
1009 void r_lightningbeams_newmap(void)
1010 {
1011 }
1012
1013 void R_LightningBeams_Init(void)
1014 {
1015         Cvar_RegisterVariable(&r_lightningbeam_thickness);
1016         Cvar_RegisterVariable(&r_lightningbeam_scroll);
1017         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
1018         Cvar_RegisterVariable(&r_lightningbeam_color_red);
1019         Cvar_RegisterVariable(&r_lightningbeam_color_green);
1020         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
1021         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
1022 }
1023
1024 void R_CalcLightningBeamPolygonVertices(float *v, float *tc, const float *start, const float *end, const float *offset, float t1, float t2)
1025 {
1026         // near right corner
1027         VectorAdd     (start, offset, (v +  0));tc[ 0] = 0;tc[ 1] = t1;
1028         // near left corner
1029         VectorSubtract(start, offset, (v +  4));tc[ 4] = 1;tc[ 5] = t1;
1030         // far left corner
1031         VectorSubtract(end  , offset, (v +  8));tc[ 8] = 1;tc[ 9] = t2;
1032         // far right corner
1033         VectorAdd     (end  , offset, (v + 12));tc[12] = 0;tc[13] = t2;
1034 }
1035
1036 void R_FogLightningBeamColors(const float *v, float *c, int numverts, float r, float g, float b, float a)
1037 {
1038         int i;
1039         vec3_t fogvec;
1040         float ifog;
1041         for (i = 0;i < numverts;i++, v += 4, c += 4)
1042         {
1043                 VectorSubtract(v, r_origin, fogvec);
1044                 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
1045                 c[0] = r * ifog;
1046                 c[1] = g * ifog;
1047                 c[2] = b * ifog;
1048                 c[3] = a;
1049         }
1050 }
1051
1052 float beamrepeatscale;
1053
1054 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
1055 {
1056         const beam_t *b = calldata1;
1057         rmeshstate_t m;
1058         vec3_t beamdir, right, up, offset;
1059         float length, t1, t2;
1060         memset(&m, 0, sizeof(m));
1061         m.blendfunc1 = GL_SRC_ALPHA;
1062         m.blendfunc2 = GL_ONE;
1063         m.tex[0] = R_GetTexture(r_lightningbeamtexture);
1064         R_Mesh_State(&m);
1065         R_Mesh_Matrix(&r_identitymatrix);
1066
1067         // calculate beam direction (beamdir) vector and beam length
1068         // get difference vector
1069         VectorSubtract(b->end, b->start, beamdir);
1070         // find length of difference vector
1071         length = sqrt(DotProduct(beamdir, beamdir));
1072         // calculate scale to make beamdir a unit vector (normalized)
1073         t1 = 1.0f / length;
1074         // scale beamdir so it is now normalized
1075         VectorScale(beamdir, t1, beamdir);
1076
1077         // calculate up vector such that it points toward viewer, and rotates around the beamdir
1078         // get direction from start of beam to viewer
1079         VectorSubtract(r_origin, b->start, up);
1080         // remove the portion of the vector that moves along the beam
1081         // (this leaves only a vector pointing directly away from the beam)
1082         t1 = -DotProduct(up, beamdir);
1083         VectorMA(up, t1, beamdir, up);
1084         // now we have a vector pointing away from the beam, now we need to normalize it
1085         VectorNormalizeFast(up);
1086         // generate right vector from forward and up, the result is already normalized
1087         // (CrossProduct returns a vector of multiplied length of the two inputs)
1088         CrossProduct(beamdir, up, right);
1089
1090         // calculate T coordinate scrolling (start and end texcoord along the beam)
1091         t1 = cl.time * -r_lightningbeam_scroll.value + beamrepeatscale * DotProduct(b->start, beamdir);
1092         t1 = t1 - (int) t1;
1093         t2 = t1 + beamrepeatscale * length;
1094
1095         // the beam is 3 polygons in this configuration:
1096         //  *   2
1097         //   * *
1098         // 1******
1099         //   * *
1100         //  *   3
1101         // they are showing different portions of the beam texture, creating an
1102         // illusion of a beam that appears to curl around in 3D space
1103         // (and realize that the whole polygon assembly orients itself to face
1104         //  the viewer)
1105
1106         // polygon 1, verts 0-3
1107         VectorScale(right, r_lightningbeam_thickness.value, offset);
1108         R_CalcLightningBeamPolygonVertices(varray_vertex, varray_texcoord[0], b->start, b->end, offset, t1, t2);
1109
1110         // polygon 2, verts 4-7
1111         VectorAdd(right, up, offset);
1112         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1113         R_CalcLightningBeamPolygonVertices(varray_vertex + 16, varray_texcoord[0] + 16, b->start, b->end, offset, t1 + 0.33, t2 + 0.33);
1114
1115         // polygon 3, verts 8-11
1116         VectorSubtract(right, up, offset);
1117         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1118         R_CalcLightningBeamPolygonVertices(varray_vertex + 32, varray_texcoord[0] + 32, b->start, b->end, offset, t1 + 0.66, t2 + 0.66);
1119
1120         if (fogenabled)
1121         {
1122                 // per vertex colors if fog is used
1123                 GL_UseColorArray();
1124                 R_FogLightningBeamColors(varray_vertex, varray_color, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1125         }
1126         else
1127         {
1128                 // solid color if fog is not used
1129                 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1130         }
1131
1132         // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
1133         R_Mesh_Draw(12, 6, r_lightningbeamelements);
1134 }
1135
1136 void R_DrawLightningBeams (void)
1137 {
1138         int i;
1139         beam_t *b;
1140         vec3_t org;
1141
1142         if (!cl_beams_polygons.integer)
1143                 return;
1144
1145         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
1146         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1147         {
1148                 if (b->model && b->endtime >= cl.time && b->lightning)
1149                 {
1150                         VectorAdd(b->start, b->end, org);
1151                         VectorScale(org, 0.5f, org);
1152                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
1153                 }
1154         }
1155 }
1156
1157
1158 void CL_LerpPlayer(float frac)
1159 {
1160         int i;
1161         float d;
1162
1163         if (cl.entitydatabase.numframes && cl.viewentity == cl.playerentity)
1164         {
1165                 cl.viewentorigin[0] = cl.viewentoriginold[0] + frac * (cl.viewentoriginnew[0] - cl.viewentoriginold[0]);
1166                 cl.viewentorigin[1] = cl.viewentoriginold[1] + frac * (cl.viewentoriginnew[1] - cl.viewentoriginold[1]);
1167                 cl.viewentorigin[2] = cl.viewentoriginold[2] + frac * (cl.viewentoriginnew[2] - cl.viewentoriginold[2]);
1168         }
1169         else
1170         {
1171                 VectorCopy (cl_entities[cl.viewentity].state_previous.origin, cl.viewentoriginold);
1172                 VectorCopy (cl_entities[cl.viewentity].state_current.origin, cl.viewentoriginnew);
1173                 VectorCopy (cl_entities[cl.viewentity].render.origin, cl.viewentorigin);
1174         }
1175
1176         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1177
1178         for (i = 0;i < 3;i++)
1179                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1180
1181         if (cls.demoplayback)
1182         {
1183                 // interpolate the angles
1184                 for (i = 0;i < 3;i++)
1185                 {
1186                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1187                         if (d > 180)
1188                                 d -= 360;
1189                         else if (d < -180)
1190                                 d += 360;
1191                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1192                 }
1193         }
1194 }
1195
1196 void CL_RelinkEntities (void)
1197 {
1198         float frac;
1199
1200         // fraction from previous network update to current
1201         frac = CL_LerpPoint();
1202
1203         CL_ClearTempEntities();
1204         CL_DecayLights();
1205         CL_RelinkWorld();
1206         CL_RelinkStaticEntities();
1207         CL_RelinkNetworkEntities();
1208         CL_RelinkEffects();
1209         CL_MoveParticles();
1210
1211         CL_LerpPlayer(frac);
1212
1213         CL_RelinkBeams();
1214 }
1215
1216
1217 /*
1218 ===============
1219 CL_ReadFromServer
1220
1221 Read all incoming data from the server
1222 ===============
1223 */
1224 int CL_ReadFromServer (void)
1225 {
1226         int ret, netshown;
1227
1228         cl.oldtime = cl.time;
1229         cl.time += cl.frametime;
1230
1231         netshown = false;
1232         do
1233         {
1234                 ret = CL_GetMessage ();
1235                 if (ret == -1)
1236                         Host_Error ("CL_ReadFromServer: lost server connection");
1237                 if (!ret)
1238                         break;
1239
1240                 cl.last_received_message = realtime;
1241
1242                 if (cl_shownet.integer)
1243                         netshown = true;
1244
1245                 CL_ParseServerMessage ();
1246         }
1247         while (ret && cls.state == ca_connected);
1248
1249         if (netshown)
1250                 Con_Printf ("\n");
1251
1252         r_refdef.numentities = 0;
1253         cl_num_entities = 0;
1254         cl_num_brushmodel_entities = 0;
1255
1256         if (cls.state == ca_connected && cls.signon == SIGNONS)
1257         {
1258                 CL_RelinkEntities ();
1259
1260                 // run cgame code (which can add more entities)
1261                 CL_CGVM_Frame();
1262         }
1263
1264 //
1265 // bring the links up to date
1266 //
1267         return 0;
1268 }
1269
1270 /*
1271 =================
1272 CL_SendCmd
1273 =================
1274 */
1275 void CL_SendCmd (void)
1276 {
1277         usercmd_t               cmd;
1278
1279         if (cls.state != ca_connected)
1280                 return;
1281
1282         if (cls.signon == SIGNONS)
1283         {
1284         // get basic movement from keyboard
1285                 CL_BaseMove (&cmd);
1286
1287                 IN_PreMove(); // OS independent code
1288
1289         // allow mice or other external controllers to add to the move
1290                 IN_Move (&cmd);
1291
1292                 IN_PostMove(); // OS independent code
1293
1294         // send the unreliable message
1295                 CL_SendMove (&cmd);
1296         }
1297 #ifndef NOROUTINGFIX
1298         else if (cls.signon == 0 && !cls.demoplayback)
1299         {
1300                 // LordHavoc: fix for NAT routing of netquake:
1301                 // bounce back a clc_nop message to the newly allocated server port,
1302                 // to establish a routing connection for incoming frames,
1303                 // the server waits for this before sending anything
1304                 if (realtime > cl.sendnoptime)
1305                 {
1306                         cl.sendnoptime = realtime + 3;
1307                         Con_DPrintf("sending clc_nop to get server's attention\n");
1308                         {
1309                                 sizebuf_t buf;
1310                                 qbyte data[128];
1311                                 buf.maxsize = 128;
1312                                 buf.cursize = 0;
1313                                 buf.data = data;
1314                                 MSG_WriteByte(&buf, clc_nop);
1315                                 if (NET_SendUnreliableMessage (cls.netcon, &buf) == -1)
1316                                 {
1317                                         Con_Printf ("CL_SendCmd: lost server connection\n");
1318                                         CL_Disconnect ();
1319                                 }
1320                         }
1321                 }
1322         }
1323 #endif
1324
1325         if (cls.demoplayback)
1326         {
1327                 SZ_Clear (&cls.message);
1328                 return;
1329         }
1330
1331 // send the reliable message
1332         if (!cls.message.cursize)
1333                 return;         // no message at all
1334
1335         if (!NET_CanSendMessage (cls.netcon))
1336         {
1337                 Con_DPrintf ("CL_WriteToServer: can't send\n");
1338                 if (developer.integer)
1339                         SZ_HexDumpToConsole(&cls.message);
1340                 return;
1341         }
1342
1343         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
1344                 Host_Error ("CL_WriteToServer: lost server connection");
1345
1346         SZ_Clear (&cls.message);
1347 }
1348
1349 // LordHavoc: pausedemo command
1350 static void CL_PauseDemo_f (void)
1351 {
1352         cls.demopaused = !cls.demopaused;
1353         if (cls.demopaused)
1354                 Con_Printf("Demo paused\n");
1355         else
1356                 Con_Printf("Demo unpaused\n");
1357 }
1358
1359 /*
1360 ======================
1361 CL_PModel_f
1362 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
1363 ======================
1364 */
1365 static void CL_PModel_f (void)
1366 {
1367         int i;
1368         eval_t *val;
1369
1370         if (Cmd_Argc () == 1)
1371         {
1372                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
1373                 return;
1374         }
1375         i = atoi(Cmd_Argv(1));
1376
1377         if (cmd_source == src_command)
1378         {
1379                 if (cl_pmodel.integer == i)
1380                         return;
1381                 Cvar_SetValue ("_cl_pmodel", i);
1382                 if (cls.state == ca_connected)
1383                         Cmd_ForwardToServer ();
1384                 return;
1385         }
1386
1387         host_client->pmodel = i;
1388         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
1389                 val->_float = i;
1390 }
1391
1392 /*
1393 ======================
1394 CL_Fog_f
1395 ======================
1396 */
1397 static void CL_Fog_f (void)
1398 {
1399         if (Cmd_Argc () == 1)
1400         {
1401                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1402                 return;
1403         }
1404         fog_density = atof(Cmd_Argv(1));
1405         fog_red = atof(Cmd_Argv(2));
1406         fog_green = atof(Cmd_Argv(3));
1407         fog_blue = atof(Cmd_Argv(4));
1408 }
1409
1410 /*
1411 =================
1412 CL_Init
1413 =================
1414 */
1415 void CL_Init (void)
1416 {
1417         cl_scores_mempool = Mem_AllocPool("client player info");
1418         cl_entities_mempool = Mem_AllocPool("client entities");
1419         cl_refdef_mempool = Mem_AllocPool("refdef");
1420
1421         memset(&r_refdef, 0, sizeof(r_refdef));
1422         // max entities sent to renderer per frame
1423         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1424         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1425         // 256k drawqueue buffer
1426         r_refdef.maxdrawqueuesize = 256 * 1024;
1427         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1428
1429         SZ_Alloc (&cls.message, 1024, "cls.message");
1430
1431         CL_InitInput ();
1432         CL_InitTEnts ();
1433
1434 //
1435 // register our commands
1436 //
1437         Cvar_RegisterVariable (&cl_name);
1438         Cvar_RegisterVariable (&cl_color);
1439         if (gamemode == GAME_NEHAHRA)
1440                 Cvar_RegisterVariable (&cl_pmodel);
1441         Cvar_RegisterVariable (&cl_upspeed);
1442         Cvar_RegisterVariable (&cl_forwardspeed);
1443         Cvar_RegisterVariable (&cl_backspeed);
1444         Cvar_RegisterVariable (&cl_sidespeed);
1445         Cvar_RegisterVariable (&cl_movespeedkey);
1446         Cvar_RegisterVariable (&cl_yawspeed);
1447         Cvar_RegisterVariable (&cl_pitchspeed);
1448         Cvar_RegisterVariable (&cl_anglespeedkey);
1449         Cvar_RegisterVariable (&cl_shownet);
1450         Cvar_RegisterVariable (&cl_nolerp);
1451         Cvar_RegisterVariable (&lookspring);
1452         Cvar_RegisterVariable (&lookstrafe);
1453         Cvar_RegisterVariable (&sensitivity);
1454         Cvar_RegisterVariable (&freelook);
1455
1456         Cvar_RegisterVariable (&m_pitch);
1457         Cvar_RegisterVariable (&m_yaw);
1458         Cvar_RegisterVariable (&m_forward);
1459         Cvar_RegisterVariable (&m_side);
1460
1461         Cvar_RegisterVariable (&cl_itembobspeed);
1462         Cvar_RegisterVariable (&cl_itembobheight);
1463
1464         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1465         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1466         Cmd_AddCommand ("record", CL_Record_f);
1467         Cmd_AddCommand ("stop", CL_Stop_f);
1468         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1469         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1470
1471         Cmd_AddCommand ("fog", CL_Fog_f);
1472
1473         // LordHavoc: added pausedemo
1474         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1475         if (gamemode == GAME_NEHAHRA)
1476                 Cmd_AddCommand ("pmodel", CL_PModel_f);
1477
1478         Cvar_RegisterVariable(&r_draweffects);
1479         Cvar_RegisterVariable(&cl_explosions);
1480         Cvar_RegisterVariable(&cl_stainmaps);
1481         Cvar_RegisterVariable(&cl_beams_polygons);
1482         Cvar_RegisterVariable(&cl_beams_relative);
1483
1484         R_LightningBeams_Init();
1485
1486         CL_Parse_Init();
1487         CL_Particles_Init();
1488         CL_Screen_Init();
1489         CL_CGVM_Init();
1490
1491         CL_Video_Init();
1492 }
1493