]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_main.c
coronas now scale with the light radius
[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         CL_Decals_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                 // 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
248 /*
249 ==============
250 CL_PrintEntities_f
251 ==============
252 */
253 static void CL_PrintEntities_f (void)
254 {
255         entity_t        *ent;
256         int                     i, j;
257         char            name[32];
258
259         for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
260         {
261                 if (!ent->state_current.active)
262                         continue;
263                 if (!ent->render.model)
264                         continue;
265
266                 Con_Printf ("%3i:", i);
267                 if (!ent->render.model)
268                 {
269                         Con_Printf ("EMPTY\n");
270                         continue;
271                 }
272                 strncpy(name, ent->render.model->name, 25);
273                 name[25] = 0;
274                 for (j = strlen(name);j < 25;j++)
275                         name[j] = ' ';
276                 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);
277         }
278 }
279
280
281 /*
282 ===============
283 CL_LerpPoint
284
285 Determines the fraction between the last two messages that the objects
286 should be put at.
287 ===============
288 */
289 static float CL_LerpPoint (void)
290 {
291         float   f, frac;
292
293         f = cl.mtime[0] - cl.mtime[1];
294
295         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
296         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
297         {
298                 cl.time = cl.mtime[0];
299                 return 1;
300         }
301
302         if (f > 0.1)
303         {       // dropped packet, or start of demo
304                 cl.mtime[1] = cl.mtime[0] - 0.1;
305                 f = 0.1;
306         }
307         frac = (cl.time - cl.mtime[1]) / f;
308 //      Con_Printf ("frac: %f\n",frac);
309         if (frac < 0)
310         {
311                 if (frac < -0.01)
312                 {
313                         cl.time = cl.mtime[1];
314 //                      Con_Printf ("low frac\n");
315                 }
316                 frac = 0;
317         }
318         else if (frac > 1)
319         {
320                 if (frac > 1.01)
321                 {
322                         cl.time = cl.mtime[0];
323 //                      Con_Printf ("high frac\n");
324                 }
325                 frac = 1;
326         }
327
328         return frac;
329 }
330
331 static void CL_RelinkStaticEntities(void)
332 {
333         int i;
334         for (i = 0;i < cl.num_statics && r_refdef.numentities < MAX_VISEDICTS;i++)
335         {
336                 Mod_CheckLoaded(cl_static_entities[i].render.model);
337                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
338         }
339 }
340
341 /*
342 ===============
343 CL_RelinkEntities
344 ===============
345 */
346 static void CL_RelinkNetworkEntities()
347 {
348         entity_t        *ent;
349         int                     i, glowcolor, effects;
350         float           f, d, bobjrotate, bobjoffset, dlightradius, glowsize, lerp;
351         vec3_t          oldorg, neworg, delta, dlightcolor;
352
353         bobjrotate = ANGLEMOD(100*cl.time);
354         if (cl_itembobheight.value)
355                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
356         else
357                 bobjoffset = 0;
358
359         CL_RelinkStaticEntities();
360
361 // start on the entity after the world
362         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
363         {
364                 // if the object wasn't included in the latest packet, remove it
365                 if (!ent->state_current.active)
366                         continue;
367
368                 VectorCopy(ent->persistent.trail_origin, oldorg);
369
370                 if (!ent->state_previous.active)
371                 {
372                         // only one state available
373                         lerp = 1;
374                         VectorCopy (ent->state_current.origin, oldorg); // skip trails
375                         VectorCopy (ent->state_current.origin, neworg);
376                         VectorCopy (ent->state_current.angles, ent->render.angles);
377
378                         /*
379                         // monster interpolation
380                         ent->persistent.steplerptime = 0;
381                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
382                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
383                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
384                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
385                         */
386                 }
387                 /*
388                 else if ((ent->state_current.flags & ent->state_previous.flags) & ENTFLAG_STEP)
389                 {
390                         if (ent->state_current.origin[0] != ent->persistent.steporigin[0]
391                          || ent->state_current.origin[1] != ent->persistent.steporigin[1]
392                          || ent->state_current.origin[2] != ent->persistent.steporigin[2]
393                          || ent->state_current.angles[0] != ent->persistent.stepangles[0]
394                          || ent->state_current.angles[1] != ent->persistent.stepangles[1]
395                          || ent->state_current.angles[2] != ent->persistent.stepangles[2])
396                         {
397                                 // update lerp positions
398                                 ent->clientpersistent.steplerptime = sv.time;
399                                 VectorCopy(ent->steporigin, ent->stepoldorigin);
400                                 VectorCopy(ent->stepangles, ent->stepoldangles);
401                                 VectorCopy(ent->v.origin, ent->steporigin);
402                                 VectorCopy(ent->v.angles, ent->stepangles);
403                         }
404                         lerp = (cl.time - ent->persistent.steplerptime) * 10.0;
405                         if (lerp < 1)
406                         {
407                                 // origin
408                                 VectorSubtract(ent->persistent.steporigin, ent->persistent.stepoldorigin, delta);
409                                 VectorMA(ent->persistent.stepoldorigin, lerp, delta, neworg);
410
411                                 // angles
412                                 VectorSubtract(ent->persistent.stepangles, ent->persistent.stepoldangles, delta);
413                                 // choose shortest rotate (to avoid 'spin around' situations)
414                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
415                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
416                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
417                                 VectorMA(ent->stepoldangles, lerp, delta, ent->render.angles);
418                         }
419                         else
420                         {
421                                 VectorCopy(ent->persistent.steporigin, neworg);
422                                 VectorCopy(ent->persistent.stepangles, ent->render.angles);
423                         }
424                 }
425                 */
426                 else
427                 {
428                         /*
429                         // monster interpolation
430                         ent->persistent.steplerptime = 0;
431                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
432                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
433                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
434                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
435                         */
436
437                         // if the delta is large, assume a teleport and don't lerp
438                         VectorSubtract(ent->state_current.origin, ent->state_previous.origin, delta);
439                         // LordHavoc: increased tolerance from 100 to 200, and now to 1000
440                         if ((sv.active && svs.maxclients == 1 && !(ent->state_current.flags & RENDER_STEP)) || cls.timedemo || DotProduct(delta, delta) > 1000*1000 || cl_nolerp.integer)
441                                 lerp = 1;
442                         else
443                         {
444                                 f = ent->state_current.time - ent->state_previous.time;
445                                 if (f > 0)
446                                         lerp = (cl.time - ent->state_previous.time) / f;
447                                 else
448                                         lerp = 1;
449                         }
450                         if (lerp >= 1)
451                         {
452                                 // no interpolation
453                                 VectorCopy (ent->state_current.origin, neworg);
454                                 VectorCopy (ent->state_current.angles, ent->render.angles);
455                         }
456                         else
457                         {
458                                 // interpolate the origin and angles
459                                 VectorMA(ent->state_previous.origin, lerp, delta, neworg);
460                                 VectorSubtract(ent->state_current.angles, ent->state_previous.angles, delta);
461                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
462                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
463                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
464                                 VectorMA(ent->state_previous.angles, lerp, delta, ent->render.angles);
465                         }
466                 }
467
468                 VectorCopy (neworg, ent->persistent.trail_origin);
469                 // persistent.modelindex will be updated by CL_LerpUpdate
470                 if (ent->state_current.modelindex != ent->persistent.modelindex)
471                         VectorCopy(neworg, oldorg);
472
473                 VectorCopy (neworg, ent->render.origin);
474                 ent->render.flags = ent->state_current.flags;
475                 ent->render.effects = effects = ent->state_current.effects;
476                 if (cl.scores == NULL || !ent->state_current.colormap)
477                         ent->render.colormap = -1; // no special coloring
478                 else
479                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
480                 ent->render.skinnum = ent->state_current.skin;
481                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
482                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
483
484                 // update interpolation info
485                 CL_LerpUpdate(ent);
486
487                 // handle effects now...
488                 dlightradius = 0;
489                 dlightcolor[0] = 0;
490                 dlightcolor[1] = 0;
491                 dlightcolor[2] = 0;
492
493                 // LordHavoc: if the entity has no effects, don't check each
494                 if (effects)
495                 {
496                         if (effects & EF_BRIGHTFIELD)
497                                 CL_EntityParticles (ent);
498                         if (effects & EF_MUZZLEFLASH)
499                         {
500                                 vec3_t v, v2;
501
502                                 AngleVectors (ent->render.angles, v, NULL, NULL);
503
504                                 v2[0] = v[0] * 18 + neworg[0];
505                                 v2[1] = v[1] * 18 + neworg[1];
506                                 v2[2] = v[2] * 18 + neworg[2] + 16;
507                                 TraceLine(neworg, v2, v, NULL, 0, true);
508
509                                 CL_AllocDlight (NULL, v, 100, 1, 1, 1, 0, 0.1);
510                         }
511                         if (effects & EF_DIMLIGHT)
512                         {
513                                 dlightcolor[0] += 200.0f;
514                                 dlightcolor[1] += 200.0f;
515                                 dlightcolor[2] += 200.0f;
516                         }
517                         if (effects & EF_BRIGHTLIGHT)
518                         {
519                                 dlightcolor[0] += 400.0f;
520                                 dlightcolor[1] += 400.0f;
521                                 dlightcolor[2] += 400.0f;
522                         }
523                         // LordHavoc: added EF_RED and EF_BLUE
524                         if (effects & EF_RED) // red
525                         {
526                                 dlightcolor[0] += 200.0f;
527                                 dlightcolor[1] +=  20.0f;
528                                 dlightcolor[2] +=  20.0f;
529                         }
530                         if (effects & EF_BLUE) // blue
531                         {
532                                 dlightcolor[0] +=  20.0f;
533                                 dlightcolor[1] +=  20.0f;
534                                 dlightcolor[2] += 200.0f;
535                         }
536                         if (effects & EF_FLAME)
537                         {
538                                 if (ent->render.model)
539                                 {
540                                         vec3_t mins, maxs;
541                                         int temp;
542                                         if (ent->render.angles[0] || ent->render.angles[2])
543                                         {
544                                                 VectorAdd(neworg, ent->render.model->rotatedmins, mins);
545                                                 VectorAdd(neworg, ent->render.model->rotatedmaxs, maxs);
546                                         }
547                                         else if (ent->render.angles[1])
548                                         {
549                                                 VectorAdd(neworg, ent->render.model->yawmins, mins);
550                                                 VectorAdd(neworg, ent->render.model->yawmaxs, maxs);
551                                         }
552                                         else
553                                         {
554                                                 VectorAdd(neworg, ent->render.model->normalmins, mins);
555                                                 VectorAdd(neworg, ent->render.model->normalmaxs, maxs);
556                                         }
557                                         // how many flames to make
558                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
559                                         CL_FlameCube(mins, maxs, temp);
560                                 }
561                                 d = lhrandom(200, 250);
562                                 dlightcolor[0] += d * 1.0f;
563                                 dlightcolor[1] += d * 0.7f;
564                                 dlightcolor[2] += d * 0.3f;
565                         }
566                 }
567
568                 // LordHavoc: if the model has no flags, don't check each
569                 if (ent->render.model && ent->render.model->flags)
570                 {
571                         if (ent->render.model->flags & EF_ROTATE)
572                         {
573                                 ent->render.angles[1] = bobjrotate;
574                                 ent->render.origin[2] += bobjoffset;
575                         }
576                         // only do trails if present in the previous frame as well
577                         if (ent->state_previous.active)
578                         {
579                                 if (ent->render.model->flags & EF_GIB)
580                                         CL_RocketTrail (oldorg, neworg, 2, ent);
581                                 else if (ent->render.model->flags & EF_ZOMGIB)
582                                         CL_RocketTrail (oldorg, neworg, 4, ent);
583                                 else if (ent->render.model->flags & EF_TRACER)
584                                         CL_RocketTrail (oldorg, neworg, 3, ent);
585                                 else if (ent->render.model->flags & EF_TRACER2)
586                                         CL_RocketTrail (oldorg, neworg, 5, ent);
587                                 else if (ent->render.model->flags & EF_ROCKET)
588                                 {
589                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
590                                         // LordHavoc: changed from 200, 160, 80 to 250, 200, 100
591                                         dlightcolor[0] += 250.0f;
592                                         dlightcolor[1] += 200.0f;
593                                         dlightcolor[2] += 100.0f;
594                                 }
595                                 else if (ent->render.model->flags & EF_GRENADE)
596                                 {
597                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
598                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
599                                         else
600                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
601                                 }
602                                 else if (ent->render.model->flags & EF_TRACER3)
603                                         CL_RocketTrail (oldorg, neworg, 6, ent);
604                         }
605                 }
606                 // LordHavoc: customizable glow
607                 glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
608                 glowcolor = ent->state_current.glowcolor;
609                 if (glowsize)
610                 {
611                         byte *tempcolor = (byte *)&d_8to24table[glowcolor];
612                         dlightcolor[0] += glowsize * tempcolor[0] * (1.0f / 255.0f);
613                         dlightcolor[1] += glowsize * tempcolor[1] * (1.0f / 255.0f);
614                         dlightcolor[2] += glowsize * tempcolor[2] * (1.0f / 255.0f);
615                 }
616                 // LordHavoc: customizable trail
617                 if (ent->render.flags & RENDER_GLOWTRAIL)
618                         CL_RocketTrail2 (oldorg, neworg, glowcolor, ent);
619
620                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
621                 {
622                         vec3_t vec;
623                         dlightradius = VectorLength(dlightcolor);
624                         d = 1.0f / dlightradius;
625                         VectorCopy(neworg, vec);
626                         // hack to make glowing player light shine on their gun
627                         if (i == cl.viewentity && !chase_active.integer)
628                                 vec[2] += 30;
629                         CL_AllocDlight (&ent->render, vec, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
630                 }
631
632                 if (chase_active.integer)
633                 {
634                         if (ent->render.flags & RENDER_VIEWMODEL)
635                                 continue;
636                 }
637                 else
638                 {
639                         if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
640                                 continue;
641                 }
642
643                 if (ent->render.model == NULL)
644                         continue;
645                 if (effects & EF_NODRAW)
646                         continue;
647                 if (r_refdef.numentities < MAX_VISEDICTS)
648                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
649         }
650 }
651
652 static void CL_LerpPlayerVelocity (void)
653 {
654         int i;
655         float frac, d;
656
657         // fraction from previous network update to current
658         frac = CL_LerpPoint ();
659
660         for (i = 0;i < 3;i++)
661                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
662
663         if (cls.demoplayback)
664         {
665                 // interpolate the angles
666                 for (i = 0;i < 3;i++)
667                 {
668                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
669                         if (d > 180)
670                                 d -= 360;
671                         else if (d < -180)
672                                 d += 360;
673                         cl.viewangles[i] = cl.mviewangles[1][i] + frac*d;
674                 }
675         }
676 }
677
678 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
679 {
680         int i;
681         cl_effect_t *e;
682         if (!modelindex) // sanity check
683                 return;
684         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
685         {
686                 if (e->active)
687                         continue;
688                 e->active = true;
689                 VectorCopy(org, e->origin);
690                 e->modelindex = modelindex;
691                 e->starttime = cl.time;
692                 e->startframe = startframe;
693                 e->endframe = startframe + framecount;
694                 e->framerate = framerate;
695
696                 e->frame = 0;
697                 e->frame1time = cl.time;
698                 e->frame2time = cl.time;
699                 break;
700         }
701 }
702
703 static void CL_RelinkEffects()
704 {
705         int i, intframe;
706         cl_effect_t *e;
707         entity_t *vis;
708         float frame;
709
710         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
711         {
712                 if (e->active)
713                 {
714                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
715                         intframe = frame;
716                         if (intframe < 0 || intframe >= e->endframe)
717                         {
718                                 e->active = false;
719                                 memset(e, 0, sizeof(*e));
720                                 continue;
721                         }
722
723                         if (intframe != e->frame)
724                         {
725                                 e->frame = intframe;
726                                 e->frame1time = e->frame2time;
727                                 e->frame2time = cl.time;
728                         }
729
730                         if ((vis = CL_NewTempEntity()))
731                         {
732                                 // interpolation stuff
733                                 vis->render.frame1 = intframe;
734                                 vis->render.frame2 = intframe + 1;
735                                 if (vis->render.frame2 >= e->endframe)
736                                         vis->render.frame2 = -1; // disappear
737                                 vis->render.framelerp = frame - intframe;
738                                 vis->render.frame1time = e->frame1time;
739                                 vis->render.frame2time = e->frame2time;
740
741                                 // normal stuff
742                                 VectorCopy(e->origin, vis->render.origin);
743                                 vis->render.model = cl.model_precache[e->modelindex];
744                                 vis->render.frame = vis->render.frame2;
745                                 vis->render.colormap = -1; // no special coloring
746                                 vis->render.scale = 1;
747                                 vis->render.alpha = 1;
748                         }
749                 }
750         }
751 }
752
753 void CL_RelinkEntities (void)
754 {
755         CL_LerpPlayerVelocity();
756         CL_RelinkNetworkEntities();
757         TraceLine_ScanForBModels();
758         CL_RelinkEffects();
759         CL_MoveParticles();
760         CL_UpdateDecals();
761         CL_UpdateTEnts();
762 }
763
764
765 /*
766 ===============
767 CL_ReadFromServer
768
769 Read all incoming data from the server
770 ===============
771 */
772 int CL_ReadFromServer (void)
773 {
774         int ret, netshown;
775
776         cl.oldtime = cl.time;
777         cl.time += cl.frametime;
778
779         netshown = false;
780         do
781         {
782                 ret = CL_GetMessage ();
783                 if (ret == -1)
784                         Host_Error ("CL_ReadFromServer: lost server connection");
785                 if (!ret)
786                         break;
787
788                 cl.last_received_message = realtime;
789
790                 if (cl_shownet.integer)
791                         netshown = true;
792
793                 CL_ParseServerMessage ();
794         }
795         while (ret && cls.state == ca_connected);
796
797         if (netshown)
798                 Con_Printf ("\n");
799
800         r_refdef.numentities = 0;
801         if (cls.state == ca_connected && cl.worldmodel)
802         {
803                 CL_RelinkEntities ();
804
805                 // run cgame code (which can add more entities)
806                 CL_CGVM_Frame();
807         }
808
809 //
810 // bring the links up to date
811 //
812         return 0;
813 }
814
815 /*
816 =================
817 CL_SendCmd
818 =================
819 */
820 void CL_SendCmd (void)
821 {
822         usercmd_t               cmd;
823
824         if (cls.state != ca_connected)
825                 return;
826
827         if (cls.signon == SIGNONS)
828         {
829         // get basic movement from keyboard
830                 CL_BaseMove (&cmd);
831
832         // allow mice or other external controllers to add to the move
833                 IN_Move (&cmd);
834
835         // send the unreliable message
836                 CL_SendMove (&cmd);
837         }
838
839         if (cls.demoplayback)
840         {
841                 SZ_Clear (&cls.message);
842                 return;
843         }
844
845 // send the reliable message
846         if (!cls.message.cursize)
847                 return;         // no message at all
848
849         if (!NET_CanSendMessage (cls.netcon))
850         {
851                 Con_DPrintf ("CL_WriteToServer: can't send\n");
852                 return;
853         }
854
855         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
856                 Host_Error ("CL_WriteToServer: lost server connection");
857
858         SZ_Clear (&cls.message);
859 }
860
861 // LordHavoc: pausedemo command
862 static void CL_PauseDemo_f (void)
863 {
864         cls.demopaused = !cls.demopaused;
865         if (cls.demopaused)
866                 Con_Printf("Demo paused\n");
867         else
868                 Con_Printf("Demo unpaused\n");
869 }
870
871 /*
872 ======================
873 CL_PModel_f
874 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
875 ======================
876 */
877 static void CL_PModel_f (void)
878 {
879         int i;
880         eval_t *val;
881
882         if (Cmd_Argc () == 1)
883         {
884                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
885                 return;
886         }
887         i = atoi(Cmd_Argv(1));
888
889         if (cmd_source == src_command)
890         {
891                 if (cl_pmodel.integer == i)
892                         return;
893                 Cvar_SetValue ("_cl_pmodel", i);
894                 if (cls.state == ca_connected)
895                         Cmd_ForwardToServer ();
896                 return;
897         }
898
899         host_client->pmodel = i;
900         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
901                 val->_float = i;
902 }
903
904 /*
905 ======================
906 CL_Fog_f
907 ======================
908 */
909 static void CL_Fog_f (void)
910 {
911         if (Cmd_Argc () == 1)
912         {
913                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
914                 return;
915         }
916         fog_density = atof(Cmd_Argv(1));
917         fog_red = atof(Cmd_Argv(2));
918         fog_green = atof(Cmd_Argv(3));
919         fog_blue = atof(Cmd_Argv(4));
920 }
921
922 /*
923 =================
924 CL_Init
925 =================
926 */
927 void CL_Init (void)
928 {
929         cl_scores_mempool = Mem_AllocPool("client player info");
930
931         cl_refdef_mempool = Mem_AllocPool("refdef");
932         memset(&r_refdef, 0, sizeof(r_refdef));
933         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * MAX_VISEDICTS);
934
935         SZ_Alloc (&cls.message, 1024, "cls.message");
936
937         CL_InitInput ();
938         CL_InitTEnts ();
939
940 //
941 // register our commands
942 //
943         Cvar_RegisterVariable (&cl_name);
944         Cvar_RegisterVariable (&cl_color);
945         if (gamemode == GAME_NEHAHRA)
946                 Cvar_RegisterVariable (&cl_pmodel);
947         Cvar_RegisterVariable (&cl_upspeed);
948         Cvar_RegisterVariable (&cl_forwardspeed);
949         Cvar_RegisterVariable (&cl_backspeed);
950         Cvar_RegisterVariable (&cl_sidespeed);
951         Cvar_RegisterVariable (&cl_movespeedkey);
952         Cvar_RegisterVariable (&cl_yawspeed);
953         Cvar_RegisterVariable (&cl_pitchspeed);
954         Cvar_RegisterVariable (&cl_anglespeedkey);
955         Cvar_RegisterVariable (&cl_shownet);
956         Cvar_RegisterVariable (&cl_nolerp);
957         Cvar_RegisterVariable (&lookspring);
958         Cvar_RegisterVariable (&lookstrafe);
959         Cvar_RegisterVariable (&sensitivity);
960         Cvar_RegisterVariable (&freelook);
961
962         Cvar_RegisterVariable (&m_pitch);
963         Cvar_RegisterVariable (&m_yaw);
964         Cvar_RegisterVariable (&m_forward);
965         Cvar_RegisterVariable (&m_side);
966
967         Cvar_RegisterVariable (&cl_itembobspeed);
968         Cvar_RegisterVariable (&cl_itembobheight);
969
970         Cmd_AddCommand ("entities", CL_PrintEntities_f);
971         Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
972         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
973         Cmd_AddCommand ("record", CL_Record_f);
974         Cmd_AddCommand ("stop", CL_Stop_f);
975         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
976         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
977
978         Cmd_AddCommand ("fog", CL_Fog_f);
979
980         // LordHavoc: added pausedemo
981         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
982         if (gamemode == GAME_NEHAHRA)
983                 Cmd_AddCommand ("pmodel", CL_PModel_f);
984
985         Cvar_RegisterVariable(&cl_draweffects);
986
987         CL_Parse_Init();
988         CL_Particles_Init();
989         CL_Decals_Init();
990         CL_Screen_Init();
991         CL_CGVM_Init();
992 }