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