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