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