]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_main.c
DP code cleanup, stage one - all headers that can be protected by ifdef
[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
25 // we need to declare some mouse variables here, because the menu system
26 // references them even when on a unix system.
27
28 // these two are not intended to be set directly
29 cvar_t cl_name = {CVAR_SAVE, "_cl_name", "player"};
30 cvar_t cl_color = {CVAR_SAVE, "_cl_color", "0"};
31 cvar_t cl_pmodel = {CVAR_SAVE, "_cl_pmodel", "0"};
32
33 cvar_t cl_shownet = {0, "cl_shownet","0"};
34 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
35
36 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
37 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
38
39 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
40 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
41 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
42
43 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
44 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
45 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
46 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
47
48 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
49
50 cvar_t cl_draweffects = {0, "cl_draweffects", "1"};
51
52 mempool_t *cl_scores_mempool;
53 mempool_t *cl_refdef_mempool;
54
55 client_static_t cls;
56 client_state_t  cl;
57 // FIXME: put these on hunk?
58 entity_t                cl_entities[MAX_EDICTS];
59 entity_t                cl_static_entities[MAX_STATIC_ENTITIES];
60 lightstyle_t    cl_lightstyle[MAX_LIGHTSTYLES];
61
62 typedef struct effect_s
63 {
64         int active;
65         vec3_t origin;
66         float starttime;
67         float framerate;
68         int modelindex;
69         int startframe;
70         int endframe;
71         // these are for interpolation
72         int frame;
73         double frame1time;
74         double frame2time;
75 }
76 cl_effect_t;
77
78 #define MAX_EFFECTS 256
79
80 static cl_effect_t cl_effect[MAX_EFFECTS];
81
82
83 /*
84 =====================
85 CL_ClearState
86
87 =====================
88 */
89 void CL_ClearState (void)
90 {
91         int                     i;
92
93         if (!sv.active)
94                 Host_ClearMemory ();
95
96         Mem_EmptyPool(cl_scores_mempool);
97
98 // wipe the entire cl structure
99         memset (&cl, 0, sizeof(cl));
100
101         SZ_Clear (&cls.message);
102
103 // clear other arrays
104         memset(cl_entities, 0, sizeof(cl_entities));
105         memset(cl_lightstyle, 0, sizeof(cl_lightstyle));
106         memset(cl_temp_entities, 0, sizeof(cl_temp_entities));
107         memset(cl_beams, 0, sizeof(cl_beams));
108         memset(cl_dlights, 0, sizeof(cl_dlights));
109         memset(cl_effect, 0, sizeof(cl_effect));
110         CL_Screen_NewMap();
111         CL_Particles_Clear();
112         // LordHavoc: have to set up the baseline info for alpha and other stuff
113         for (i = 0;i < MAX_EDICTS;i++)
114         {
115                 ClearStateToDefault(&cl_entities[i].state_baseline);
116                 ClearStateToDefault(&cl_entities[i].state_previous);
117                 ClearStateToDefault(&cl_entities[i].state_current);
118         }
119
120         CL_CGVM_Clear();
121 }
122
123 void CL_LerpUpdate(entity_t *e)
124 {
125         entity_persistent_t *p;
126         entity_render_t *r;
127         p = &e->persistent;
128         r = &e->render;
129
130         if (p->modelindex != e->state_current.modelindex)
131         {
132                 // reset all interpolation information
133                 p->modelindex = e->state_current.modelindex;
134                 p->frame1 = p->frame2 = e->state_current.frame;
135                 p->frame1time = p->frame2time = cl.time;
136                 p->framelerp = 1;
137         }
138         else if (p->frame2 != e->state_current.frame)
139         {
140                 // transition to new frame
141                 p->frame1 = p->frame2;
142                 p->frame1time = p->frame2time;
143                 p->frame2 = e->state_current.frame;
144                 p->frame2time = cl.time;
145                 p->framelerp = 0;
146         }
147         else
148         {
149                 // update transition
150                 p->framelerp = (cl.time - p->frame2time) * 10;
151                 p->framelerp = bound(0, p->framelerp, 1);
152         }
153
154         r->model = cl.model_precache[e->state_current.modelindex];
155         Mod_CheckLoaded(r->model);
156         r->frame = e->state_current.frame;
157         r->frame1 = p->frame1;
158         r->frame2 = p->frame2;
159         r->framelerp = p->framelerp;
160         r->frame1time = p->frame1time;
161         r->frame2time = p->frame2time;
162 }
163
164 /*
165 =====================
166 CL_Disconnect
167
168 Sends a disconnect message to the server
169 This is also called on Host_Error, so it shouldn't cause any errors
170 =====================
171 */
172 void CL_Disconnect (void)
173 {
174         if (cls.state == ca_dedicated)
175                 return;
176
177 // stop sounds (especially looping!)
178         S_StopAllSounds (true);
179
180         // clear contents blends
181         cl.cshifts[0].percent = 0;
182         cl.cshifts[1].percent = 0;
183         cl.cshifts[2].percent = 0;
184         cl.cshifts[3].percent = 0;
185
186         cl.worldmodel = NULL;
187
188         if (cls.demoplayback)
189                 CL_StopPlayback ();
190         else if (cls.state == ca_connected)
191         {
192                 if (cls.demorecording)
193                         CL_Stop_f ();
194
195                 Con_DPrintf ("Sending clc_disconnect\n");
196                 SZ_Clear (&cls.message);
197                 MSG_WriteByte (&cls.message, clc_disconnect);
198                 NET_SendUnreliableMessage (cls.netcon, &cls.message);
199                 SZ_Clear (&cls.message);
200                 NET_Close (cls.netcon);
201                 cls.state = ca_disconnected; // prevent this code from executing again during Host_ShutdownServer
202                 // if running a local server, shut it down
203                 if (sv.active)
204                         Host_ShutdownServer(false);
205         }
206         cls.state = ca_disconnected;
207
208         cls.demoplayback = cls.timedemo = false;
209         cls.signon = 0;
210 }
211
212 void CL_Disconnect_f (void)
213 {
214         CL_Disconnect ();
215         if (sv.active)
216                 Host_ShutdownServer (false);
217 }
218
219
220
221
222 /*
223 =====================
224 CL_EstablishConnection
225
226 Host should be either "local" or a net address to be passed on
227 =====================
228 */
229 void CL_EstablishConnection (char *host)
230 {
231         if (cls.state == ca_dedicated)
232                 return;
233
234         if (cls.demoplayback)
235                 return;
236
237         CL_Disconnect ();
238
239         cls.netcon = NET_Connect (host);
240         if (!cls.netcon)
241                 Host_Error ("CL_Connect: connect failed\n");
242         Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
243
244         cls.demonum = -1;                       // not in the demo loop now
245         cls.state = ca_connected;
246         cls.signon = 0;                         // need all the signon messages before playing
247
248         CL_ClearState ();
249 }
250
251 /*
252 ==============
253 CL_PrintEntities_f
254 ==============
255 */
256 static void CL_PrintEntities_f (void)
257 {
258         entity_t        *ent;
259         int                     i, j;
260         char            name[32];
261
262         for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
263         {
264                 if (!ent->state_current.active)
265                         continue;
266                 if (!ent->render.model)
267                         continue;
268
269                 Con_Printf ("%3i:", i);
270                 if (!ent->render.model)
271                 {
272                         Con_Printf ("EMPTY\n");
273                         continue;
274                 }
275                 strncpy(name, ent->render.model->name, 25);
276                 name[25] = 0;
277                 for (j = strlen(name);j < 25;j++)
278                         name[j] = ' ';
279                 Con_Printf ("%s:%04i (%5i %5i %5i) [%3i %3i %3i] %4.2f %5.3f\n", 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);
280         }
281 }
282
283
284 /*
285 ===============
286 CL_LerpPoint
287
288 Determines the fraction between the last two messages that the objects
289 should be put at.
290 ===============
291 */
292 static float CL_LerpPoint (void)
293 {
294         float   f;
295
296         // dropped packet, or start of demo
297         if (cl.mtime[1] < cl.mtime[0] - 0.1)
298                 cl.mtime[1] = cl.mtime[0] - 0.1;
299
300         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
301
302         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
303         f = cl.mtime[0] - cl.mtime[1];
304         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
305         {
306                 cl.time = cl.mtime[0];
307                 return 1;
308         }
309
310         f = (cl.time - cl.mtime[1]) / f;
311         return bound(0, f, 1);
312 }
313
314 static void CL_RelinkStaticEntities(void)
315 {
316         int i;
317         for (i = 0;i < cl.num_statics && r_refdef.numentities < MAX_VISEDICTS;i++)
318         {
319                 Mod_CheckLoaded(cl_static_entities[i].render.model);
320                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
321         }
322 }
323
324 /*
325 ===============
326 CL_RelinkEntities
327 ===============
328 */
329 static void CL_RelinkNetworkEntities()
330 {
331         entity_t        *ent;
332         int                     i, glowcolor, effects;
333         float           f, d, bobjrotate, bobjoffset, dlightradius, glowsize, lerp;
334         vec3_t          oldorg, neworg, delta, dlightcolor;
335
336         bobjrotate = ANGLEMOD(100*cl.time);
337         if (cl_itembobheight.value)
338                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
339         else
340                 bobjoffset = 0;
341
342 // start on the entity after the world
343         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
344         {
345                 // if the object wasn't included in the latest packet, remove it
346                 if (!ent->state_current.active)
347                         continue;
348
349                 VectorCopy(ent->persistent.trail_origin, oldorg);
350
351                 if (!ent->state_previous.active)
352                 {
353                         // only one state available
354                         lerp = 1;
355                         VectorCopy (ent->state_current.origin, oldorg); // skip trails
356                         VectorCopy (ent->state_current.origin, neworg);
357                         VectorCopy (ent->state_current.angles, ent->render.angles);
358
359                         /*
360                         // monster interpolation
361                         ent->persistent.steplerptime = 0;
362                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
363                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
364                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
365                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
366                         */
367                 }
368                 /*
369                 else if ((ent->state_current.flags & ent->state_previous.flags) & ENTFLAG_STEP)
370                 {
371                         if (ent->state_current.origin[0] != ent->persistent.steporigin[0]
372                          || ent->state_current.origin[1] != ent->persistent.steporigin[1]
373                          || ent->state_current.origin[2] != ent->persistent.steporigin[2]
374                          || ent->state_current.angles[0] != ent->persistent.stepangles[0]
375                          || ent->state_current.angles[1] != ent->persistent.stepangles[1]
376                          || ent->state_current.angles[2] != ent->persistent.stepangles[2])
377                         {
378                                 // update lerp positions
379                                 ent->clientpersistent.steplerptime = sv.time;
380                                 VectorCopy(ent->steporigin, ent->stepoldorigin);
381                                 VectorCopy(ent->stepangles, ent->stepoldangles);
382                                 VectorCopy(ent->v.origin, ent->steporigin);
383                                 VectorCopy(ent->v.angles, ent->stepangles);
384                         }
385                         lerp = (cl.time - ent->persistent.steplerptime) * 10.0;
386                         if (lerp < 1)
387                         {
388                                 // origin
389                                 VectorSubtract(ent->persistent.steporigin, ent->persistent.stepoldorigin, delta);
390                                 VectorMA(ent->persistent.stepoldorigin, lerp, delta, neworg);
391
392                                 // angles
393                                 VectorSubtract(ent->persistent.stepangles, ent->persistent.stepoldangles, delta);
394                                 // choose shortest rotate (to avoid 'spin around' situations)
395                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
396                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
397                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
398                                 VectorMA(ent->stepoldangles, lerp, delta, ent->render.angles);
399                         }
400                         else
401                         {
402                                 VectorCopy(ent->persistent.steporigin, neworg);
403                                 VectorCopy(ent->persistent.stepangles, ent->render.angles);
404                         }
405                 }
406                 */
407                 else
408                 {
409                         /*
410                         // monster interpolation
411                         ent->persistent.steplerptime = 0;
412                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
413                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
414                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
415                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
416                         */
417
418                         // if the delta is large, assume a teleport and don't lerp
419                         VectorSubtract(ent->state_current.origin, ent->state_previous.origin, delta);
420                         // LordHavoc: increased tolerance from 100 to 200, and now to 1000
421                         if ((sv.active && svs.maxclients == 1 && !(ent->state_current.flags & RENDER_STEP)) || cls.timedemo || DotProduct(delta, delta) > 1000*1000 || cl_nolerp.integer)
422                                 lerp = 1;
423                         else
424                         {
425                                 f = ent->state_current.time - ent->state_previous.time;
426                                 if (f > 0)
427                                         lerp = (cl.time - ent->state_previous.time) / f;
428                                 else
429                                         lerp = 1;
430                         }
431                         if (lerp >= 1)
432                         {
433                                 // no interpolation
434                                 VectorCopy (ent->state_current.origin, neworg);
435                                 VectorCopy (ent->state_current.angles, ent->render.angles);
436                         }
437                         else
438                         {
439                                 // interpolate the origin and angles
440                                 VectorMA(ent->state_previous.origin, lerp, delta, neworg);
441                                 VectorSubtract(ent->state_current.angles, ent->state_previous.angles, delta);
442                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
443                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
444                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
445                                 VectorMA(ent->state_previous.angles, lerp, delta, ent->render.angles);
446                         }
447                 }
448
449                 VectorCopy (neworg, ent->persistent.trail_origin);
450                 // persistent.modelindex will be updated by CL_LerpUpdate
451                 if (ent->state_current.modelindex != ent->persistent.modelindex)
452                         VectorCopy(neworg, oldorg);
453
454                 VectorCopy (neworg, ent->render.origin);
455                 ent->render.flags = ent->state_current.flags;
456                 ent->render.effects = effects = ent->state_current.effects;
457                 if (cl.scores == NULL || !ent->state_current.colormap)
458                         ent->render.colormap = -1; // no special coloring
459                 else
460                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
461                 ent->render.skinnum = ent->state_current.skin;
462                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
463                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
464
465                 // update interpolation info
466                 CL_LerpUpdate(ent);
467
468                 // handle effects now...
469                 dlightradius = 0;
470                 dlightcolor[0] = 0;
471                 dlightcolor[1] = 0;
472                 dlightcolor[2] = 0;
473
474                 // LordHavoc: if the entity has no effects, don't check each
475                 if (effects)
476                 {
477                         if (effects & EF_BRIGHTFIELD)
478                                 CL_EntityParticles (ent);
479                         if (effects & EF_MUZZLEFLASH)
480                                 ent->persistent.muzzleflash = 100.0f;
481                         if (effects & EF_DIMLIGHT)
482                         {
483                                 dlightcolor[0] += 200.0f;
484                                 dlightcolor[1] += 200.0f;
485                                 dlightcolor[2] += 200.0f;
486                         }
487                         if (effects & EF_BRIGHTLIGHT)
488                         {
489                                 dlightcolor[0] += 400.0f;
490                                 dlightcolor[1] += 400.0f;
491                                 dlightcolor[2] += 400.0f;
492                         }
493                         // LordHavoc: added EF_RED and EF_BLUE
494                         if (effects & EF_RED) // red
495                         {
496                                 dlightcolor[0] += 200.0f;
497                                 dlightcolor[1] +=  20.0f;
498                                 dlightcolor[2] +=  20.0f;
499                         }
500                         if (effects & EF_BLUE) // blue
501                         {
502                                 dlightcolor[0] +=  20.0f;
503                                 dlightcolor[1] +=  20.0f;
504                                 dlightcolor[2] += 200.0f;
505                         }
506                         if (effects & EF_FLAME)
507                         {
508                                 if (ent->render.model)
509                                 {
510                                         vec3_t mins, maxs;
511                                         int temp;
512                                         /*
513                                         if (ent->render.angles[0] || ent->render.angles[2])
514                                         {
515                                                 VectorMA(neworg, 0.25f, ent->render.model->rotatedmins, mins);
516                                                 VectorMA(neworg, 0.25f, ent->render.model->rotatedmaxs, maxs);
517                                         }
518                                         else if (ent->render.angles[1])
519                                         {
520                                                 VectorMA(neworg, 0.25f, ent->render.model->yawmins, mins);
521                                                 VectorMA(neworg, 0.25f, ent->render.model->yawmaxs, maxs);
522                                         }
523                                         else
524                                         {
525                                                 VectorMA(neworg, 0.25f, ent->render.model->normalmins, mins);
526                                                 VectorMA(neworg, 0.25f, ent->render.model->normalmaxs, maxs);
527                                         }
528                                         */
529                                         mins[0] = neworg[0] - 16.0f;
530                                         mins[1] = neworg[1] - 16.0f;
531                                         mins[2] = neworg[2] - 16.0f;
532                                         maxs[0] = neworg[0] + 16.0f;
533                                         maxs[1] = neworg[1] + 16.0f;
534                                         maxs[2] = neworg[2] + 16.0f;
535                                         // how many flames to make
536                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
537                                         CL_FlameCube(mins, maxs, temp);
538                                 }
539                                 d = lhrandom(200, 250);
540                                 dlightcolor[0] += d * 1.0f;
541                                 dlightcolor[1] += d * 0.7f;
542                                 dlightcolor[2] += d * 0.3f;
543                         }
544                         if (effects & EF_STARDUST)
545                         {
546                                 if (ent->render.model)
547                                 {
548                                         vec3_t mins, maxs;
549                                         int temp;
550                                         /*
551                                         if (ent->render.angles[0] || ent->render.angles[2])
552                                         {
553                                                 VectorMA(neworg, 0.25f, ent->render.model->rotatedmins, mins);
554                                                 VectorMA(neworg, 0.25f, ent->render.model->rotatedmaxs, maxs);
555                                         }
556                                         else if (ent->render.angles[1])
557                                         {
558                                                 VectorMA(neworg, 0.25f, ent->render.model->yawmins, mins);
559                                                 VectorMA(neworg, 0.25f, ent->render.model->yawmaxs, maxs);
560                                         }
561                                         else
562                                         {
563                                                 VectorMA(neworg, 0.25f, ent->render.model->normalmins, mins);
564                                                 VectorMA(neworg, 0.25f, ent->render.model->normalmaxs, maxs);
565                                         }
566                                         */
567                                         mins[0] = neworg[0] - 16.0f;
568                                         mins[1] = neworg[1] - 16.0f;
569                                         mins[2] = neworg[2] - 16.0f;
570                                         maxs[0] = neworg[0] + 16.0f;
571                                         maxs[1] = neworg[1] + 16.0f;
572                                         maxs[2] = neworg[2] + 16.0f;
573                                         // how many particles to make
574                                         temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
575                                         CL_Stardust(mins, maxs, temp);
576                                 }
577                                 d = 100;
578                                 dlightcolor[0] += d * 1.0f;
579                                 dlightcolor[1] += d * 0.7f;
580                                 dlightcolor[2] += d * 0.3f;
581                         }
582                 }
583
584                 if (ent->persistent.muzzleflash > 0)
585                 {
586                         vec3_t v, v2;
587
588                         AngleVectors (ent->render.angles, v, NULL, NULL);
589
590                         v2[0] = v[0] * 18 + neworg[0];
591                         v2[1] = v[1] * 18 + neworg[1];
592                         v2[2] = v[2] * 18 + neworg[2] + 16;
593                         CL_TraceLine(neworg, v2, v, NULL, 0, true);
594
595                         CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
596                         ent->persistent.muzzleflash -= cl.frametime * 1000;
597                 }
598
599                 // LordHavoc: if the model has no flags, don't check each
600                 if (ent->render.model && ent->render.model->flags)
601                 {
602                         if (ent->render.model->flags & EF_ROTATE)
603                         {
604                                 ent->render.angles[1] = bobjrotate;
605                                 ent->render.origin[2] += bobjoffset;
606                         }
607                         // only do trails if present in the previous frame as well
608                         if (ent->state_previous.active)
609                         {
610                                 if (ent->render.model->flags & EF_GIB)
611                                         CL_RocketTrail (oldorg, neworg, 2, ent);
612                                 else if (ent->render.model->flags & EF_ZOMGIB)
613                                         CL_RocketTrail (oldorg, neworg, 4, ent);
614                                 else if (ent->render.model->flags & EF_TRACER)
615                                         CL_RocketTrail (oldorg, neworg, 3, ent);
616                                 else if (ent->render.model->flags & EF_TRACER2)
617                                         CL_RocketTrail (oldorg, neworg, 5, ent);
618                                 else if (ent->render.model->flags & EF_ROCKET)
619                                 {
620                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
621                                         dlightcolor[0] += 200.0f;
622                                         dlightcolor[1] += 160.0f;
623                                         dlightcolor[2] +=  80.0f;
624                                 }
625                                 else if (ent->render.model->flags & EF_GRENADE)
626                                 {
627                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
628                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
629                                         else
630                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
631                                 }
632                                 else if (ent->render.model->flags & EF_TRACER3)
633                                         CL_RocketTrail (oldorg, neworg, 6, ent);
634                         }
635                 }
636                 // LordHavoc: customizable glow
637                 glowsize = ent->state_current.glowsize; // FIXME: interpolate?
638                 glowcolor = ent->state_current.glowcolor;
639                 if (glowsize)
640                 {
641                         qbyte *tempcolor = (qbyte *)&d_8to24table[glowcolor];
642                         // * 4 for the expansion from 0-255 to 0-1023 range,
643                         // / 255 to scale down byte colors
644                         glowsize *= (4.0f / 255.0f);
645                         VectorMA(dlightcolor, glowsize, tempcolor, dlightcolor);
646                 }
647                 // LordHavoc: customizable trail
648                 if (ent->render.flags & RENDER_GLOWTRAIL)
649                         CL_RocketTrail2 (oldorg, neworg, glowcolor, ent);
650
651                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
652                 {
653                         vec3_t vec;
654                         VectorCopy(neworg, vec);
655                         // hack to make glowing player light shine on their gun
656                         if (i == cl.viewentity && !chase_active.integer)
657                                 vec[2] += 30;
658                         CL_AllocDlight (/*&ent->render*/ NULL, vec, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
659                 }
660
661                 if (chase_active.integer)
662                 {
663                         if (ent->render.flags & RENDER_VIEWMODEL)
664                                 continue;
665                 }
666                 else
667                 {
668                         if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
669                                 continue;
670                 }
671
672                 if (ent->render.model == NULL)
673                         continue;
674                 if (effects & EF_NODRAW)
675                         continue;
676                 if (r_refdef.numentities < MAX_VISEDICTS)
677                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
678         }
679 }
680
681 void CL_LerpPlayer(float frac)
682 {
683         int i;
684         float d;
685
686         if (cl.entitydatabase.numframes)
687         {
688                 cl.viewentorigin[0] = cl.viewentoriginold[0] + frac * (cl.viewentoriginnew[0] - cl.viewentoriginold[0]);
689                 cl.viewentorigin[1] = cl.viewentoriginold[1] + frac * (cl.viewentoriginnew[1] - cl.viewentoriginold[1]);
690                 cl.viewentorigin[2] = cl.viewentoriginold[2] + frac * (cl.viewentoriginnew[2] - cl.viewentoriginold[2]);
691         }
692         else
693                 VectorCopy (cl_entities[cl.viewentity].render.origin, cl.viewentorigin);
694
695         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
696
697         for (i = 0;i < 3;i++)
698                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
699
700         if (cls.demoplayback)
701         {
702                 // interpolate the angles
703                 for (i = 0;i < 3;i++)
704                 {
705                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
706                         if (d > 180)
707                                 d -= 360;
708                         else if (d < -180)
709                                 d += 360;
710                         cl.viewangles[i] = cl.mviewangles[1][i] + frac*d;
711                 }
712         }
713 }
714
715 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
716 {
717         int i;
718         cl_effect_t *e;
719         if (!modelindex) // sanity check
720                 return;
721         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
722         {
723                 if (e->active)
724                         continue;
725                 e->active = true;
726                 VectorCopy(org, e->origin);
727                 e->modelindex = modelindex;
728                 e->starttime = cl.time;
729                 e->startframe = startframe;
730                 e->endframe = startframe + framecount;
731                 e->framerate = framerate;
732
733                 e->frame = 0;
734                 e->frame1time = cl.time;
735                 e->frame2time = cl.time;
736                 break;
737         }
738 }
739
740 static void CL_RelinkEffects()
741 {
742         int i, intframe;
743         cl_effect_t *e;
744         entity_t *vis;
745         float frame;
746
747         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
748         {
749                 if (e->active)
750                 {
751                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
752                         intframe = frame;
753                         if (intframe < 0 || intframe >= e->endframe)
754                         {
755                                 e->active = false;
756                                 memset(e, 0, sizeof(*e));
757                                 continue;
758                         }
759
760                         if (intframe != e->frame)
761                         {
762                                 e->frame = intframe;
763                                 e->frame1time = e->frame2time;
764                                 e->frame2time = cl.time;
765                         }
766
767                         if ((vis = CL_NewTempEntity()))
768                         {
769                                 // interpolation stuff
770                                 vis->render.frame1 = intframe;
771                                 vis->render.frame2 = intframe + 1;
772                                 if (vis->render.frame2 >= e->endframe)
773                                         vis->render.frame2 = -1; // disappear
774                                 vis->render.framelerp = frame - intframe;
775                                 vis->render.frame1time = e->frame1time;
776                                 vis->render.frame2time = e->frame2time;
777
778                                 // normal stuff
779                                 VectorCopy(e->origin, vis->render.origin);
780                                 vis->render.model = cl.model_precache[e->modelindex];
781                                 vis->render.frame = vis->render.frame2;
782                                 vis->render.colormap = -1; // no special coloring
783                                 vis->render.scale = 1;
784                                 vis->render.alpha = 1;
785                         }
786                 }
787         }
788 }
789
790 void CL_RelinkEntities (void)
791 {
792         float frac;
793
794         // fraction from previous network update to current
795         frac = CL_LerpPoint ();
796
797         CL_DecayLights ();
798         CL_RelinkStaticEntities();
799         CL_RelinkNetworkEntities();
800         CL_TraceLine_ScanForBModels();
801         CL_RelinkEffects();
802         CL_MoveParticles();
803         CL_UpdateTEnts();
804
805         CL_LerpPlayer(frac);
806 }
807
808
809 /*
810 ===============
811 CL_ReadFromServer
812
813 Read all incoming data from the server
814 ===============
815 */
816 int CL_ReadFromServer (void)
817 {
818         int ret, netshown;
819
820         cl.oldtime = cl.time;
821         cl.time += cl.frametime;
822
823         netshown = false;
824         do
825         {
826                 ret = CL_GetMessage ();
827                 if (ret == -1)
828                         Host_Error ("CL_ReadFromServer: lost server connection");
829                 if (!ret)
830                         break;
831
832                 cl.last_received_message = realtime;
833
834                 if (cl_shownet.integer)
835                         netshown = true;
836
837                 CL_ParseServerMessage ();
838         }
839         while (ret && cls.state == ca_connected);
840
841         if (netshown)
842                 Con_Printf ("\n");
843
844         r_refdef.numentities = 0;
845         if (cls.state == ca_connected && cl.worldmodel)
846         {
847                 CL_RelinkEntities ();
848
849                 // run cgame code (which can add more entities)
850                 CL_CGVM_Frame();
851         }
852
853 //
854 // bring the links up to date
855 //
856         return 0;
857 }
858
859 /*
860 =================
861 CL_SendCmd
862 =================
863 */
864 void CL_SendCmd (void)
865 {
866         usercmd_t               cmd;
867
868         if (cls.state != ca_connected)
869                 return;
870
871         if (cls.signon == SIGNONS)
872         {
873         // get basic movement from keyboard
874                 CL_BaseMove (&cmd);
875
876                 IN_PreMove(); // OS independent code
877
878         // allow mice or other external controllers to add to the move
879                 IN_Move (&cmd);
880
881                 IN_PostMove(); // OS independent code
882
883         // send the unreliable message
884                 CL_SendMove (&cmd);
885         }
886 #ifndef NOROUTINGFIX
887         else
888         {
889                 // LordHavoc: fix for NAT routing of netquake:
890                 // bounce back a clc_nop message to the newly allocated server port,
891                 // to establish a routing connection for incoming frames,
892                 // the server waits for this before sending anything
893                 if (realtime > cl.sendnoptime)
894                 {
895                         Con_DPrintf("sending clc_nop to get server's attention\n");
896                         cl.sendnoptime = realtime + 3;
897                         MSG_WriteByte(&cls.message, clc_nop);
898                 }
899         }
900 #endif
901
902         if (cls.demoplayback)
903         {
904                 SZ_Clear (&cls.message);
905                 return;
906         }
907
908 // send the reliable message
909         if (!cls.message.cursize)
910                 return;         // no message at all
911
912         if (!NET_CanSendMessage (cls.netcon))
913         {
914                 Con_DPrintf ("CL_WriteToServer: can't send\n");
915                 return;
916         }
917
918         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
919                 Host_Error ("CL_WriteToServer: lost server connection");
920
921         SZ_Clear (&cls.message);
922 }
923
924 // LordHavoc: pausedemo command
925 static void CL_PauseDemo_f (void)
926 {
927         cls.demopaused = !cls.demopaused;
928         if (cls.demopaused)
929                 Con_Printf("Demo paused\n");
930         else
931                 Con_Printf("Demo unpaused\n");
932 }
933
934 /*
935 ======================
936 CL_PModel_f
937 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
938 ======================
939 */
940 static void CL_PModel_f (void)
941 {
942         int i;
943         eval_t *val;
944
945         if (Cmd_Argc () == 1)
946         {
947                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
948                 return;
949         }
950         i = atoi(Cmd_Argv(1));
951
952         if (cmd_source == src_command)
953         {
954                 if (cl_pmodel.integer == i)
955                         return;
956                 Cvar_SetValue ("_cl_pmodel", i);
957                 if (cls.state == ca_connected)
958                         Cmd_ForwardToServer ();
959                 return;
960         }
961
962         host_client->pmodel = i;
963         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
964                 val->_float = i;
965 }
966
967 /*
968 ======================
969 CL_Fog_f
970 ======================
971 */
972 static void CL_Fog_f (void)
973 {
974         if (Cmd_Argc () == 1)
975         {
976                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
977                 return;
978         }
979         fog_density = atof(Cmd_Argv(1));
980         fog_red = atof(Cmd_Argv(2));
981         fog_green = atof(Cmd_Argv(3));
982         fog_blue = atof(Cmd_Argv(4));
983 }
984
985 /*
986 =================
987 CL_Init
988 =================
989 */
990 void CL_Init (void)
991 {
992         cl_scores_mempool = Mem_AllocPool("client player info");
993
994         cl_refdef_mempool = Mem_AllocPool("refdef");
995         memset(&r_refdef, 0, sizeof(r_refdef));
996         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * MAX_VISEDICTS);
997
998         SZ_Alloc (&cls.message, 1024, "cls.message");
999
1000         CL_InitInput ();
1001         CL_InitTEnts ();
1002
1003 //
1004 // register our commands
1005 //
1006         Cvar_RegisterVariable (&cl_name);
1007         Cvar_RegisterVariable (&cl_color);
1008         if (gamemode == GAME_NEHAHRA)
1009                 Cvar_RegisterVariable (&cl_pmodel);
1010         Cvar_RegisterVariable (&cl_upspeed);
1011         Cvar_RegisterVariable (&cl_forwardspeed);
1012         Cvar_RegisterVariable (&cl_backspeed);
1013         Cvar_RegisterVariable (&cl_sidespeed);
1014         Cvar_RegisterVariable (&cl_movespeedkey);
1015         Cvar_RegisterVariable (&cl_yawspeed);
1016         Cvar_RegisterVariable (&cl_pitchspeed);
1017         Cvar_RegisterVariable (&cl_anglespeedkey);
1018         Cvar_RegisterVariable (&cl_shownet);
1019         Cvar_RegisterVariable (&cl_nolerp);
1020         Cvar_RegisterVariable (&lookspring);
1021         Cvar_RegisterVariable (&lookstrafe);
1022         Cvar_RegisterVariable (&sensitivity);
1023         Cvar_RegisterVariable (&freelook);
1024
1025         Cvar_RegisterVariable (&m_pitch);
1026         Cvar_RegisterVariable (&m_yaw);
1027         Cvar_RegisterVariable (&m_forward);
1028         Cvar_RegisterVariable (&m_side);
1029
1030         Cvar_RegisterVariable (&cl_itembobspeed);
1031         Cvar_RegisterVariable (&cl_itembobheight);
1032
1033         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1034         Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
1035         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1036         Cmd_AddCommand ("record", CL_Record_f);
1037         Cmd_AddCommand ("stop", CL_Stop_f);
1038         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1039         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1040
1041         Cmd_AddCommand ("fog", CL_Fog_f);
1042
1043         // LordHavoc: added pausedemo
1044         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1045         if (gamemode == GAME_NEHAHRA)
1046                 Cmd_AddCommand ("pmodel", CL_PModel_f);
1047
1048         Cvar_RegisterVariable(&cl_draweffects);
1049
1050         CL_Parse_Init();
1051         CL_Particles_Init();
1052         CL_Screen_Init();
1053         CL_CGVM_Init();
1054 }