rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[xonotic/darkplaces.git] / cl_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_main.c  -- client main loop
21
22 #include "quakedef.h"
23
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
53 client_static_t cls;
54 client_state_t  cl;
55 // FIXME: put these on hunk?
56 entity_t                cl_entities[MAX_EDICTS];
57 entity_t                cl_static_entities[MAX_STATIC_ENTITIES];
58 lightstyle_t    cl_lightstyle[MAX_LIGHTSTYLES];
59
60 int                             cl_numvisedicts;
61 entity_t                *cl_visedicts[MAX_VISEDICTS];
62
63 typedef struct effect_s
64 {
65         int active;
66         vec3_t origin;
67         float starttime;
68         float framerate;
69         int modelindex;
70         int startframe;
71         int endframe;
72         // these are for interpolation
73         int frame;
74         double frame1time;
75         double frame2time;
76 }
77 cl_effect_t;
78
79 #define MAX_EFFECTS 256
80
81 static cl_effect_t cl_effect[MAX_EFFECTS];
82
83
84 /*
85 =====================
86 CL_ClearState
87
88 =====================
89 */
90 void CL_ClearState (void)
91 {
92         int                     i;
93
94         if (!sv.active)
95                 Host_ClearMemory ();
96
97         Mem_EmptyPool(cl_scores_mempool);
98
99 // wipe the entire cl structure
100         memset (&cl, 0, sizeof(cl));
101
102         SZ_Clear (&cls.message);
103
104 // clear other arrays
105         memset(cl_entities, 0, sizeof(cl_entities));
106         memset(cl_lightstyle, 0, sizeof(cl_lightstyle));
107         memset(cl_temp_entities, 0, sizeof(cl_temp_entities));
108         memset(cl_beams, 0, sizeof(cl_beams));
109         memset(cl_dlights, 0, sizeof(cl_dlights));
110         memset(cl_effect, 0, sizeof(cl_effect));
111         CL_Particles_Clear();
112         CL_Decals_Clear();
113         // LordHavoc: have to set up the baseline info for alpha and other stuff
114         for (i = 0;i < MAX_EDICTS;i++)
115         {
116                 ClearStateToDefault(&cl_entities[i].state_baseline);
117                 ClearStateToDefault(&cl_entities[i].state_previous);
118                 ClearStateToDefault(&cl_entities[i].state_current);
119         }
120 }
121
122 void CL_LerpUpdate(entity_t *e, int frame, int modelindex)
123 {
124         entity_persistent_t *p;
125         entity_render_t *r;
126         p = &e->persistent;
127         r = &e->render;
128
129         if (p->modelindex != modelindex)
130         {
131                 // reset all interpolation information
132                 p->modelindex = modelindex;
133                 p->frame1 = p->frame2 = frame;
134                 p->frame1time = p->frame2time = cl.time;
135                 p->framelerp = 1;
136         }
137         else if (p->frame2 != frame)
138         {
139                 // transition to new frame
140                 p->frame1 = p->frame2;
141                 p->frame1time = p->frame2time;
142                 p->frame2 = 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->frame1 = p->frame1;
154         r->frame2 = p->frame2;
155         r->framelerp = p->framelerp;
156         r->frame1time = p->frame1time;
157         r->frame2time = p->frame2time;
158 }
159
160 /*
161 =====================
162 CL_Disconnect
163
164 Sends a disconnect message to the server
165 This is also called on Host_Error, so it shouldn't cause any errors
166 =====================
167 */
168 void CL_Disconnect (void)
169 {
170 // stop sounds (especially looping!)
171         S_StopAllSounds (true);
172
173         // clear contents blends
174         cl.cshifts[0].percent = 0;
175         cl.cshifts[1].percent = 0;
176         cl.cshifts[2].percent = 0;
177         cl.cshifts[3].percent = 0;
178
179         cl.worldmodel = NULL;
180
181         if (cls.demoplayback)
182                 CL_StopPlayback ();
183         else if (cls.state == ca_connected)
184         {
185                 if (cls.demorecording)
186                         CL_Stop_f ();
187
188                 Con_DPrintf ("Sending clc_disconnect\n");
189                 SZ_Clear (&cls.message);
190                 MSG_WriteByte (&cls.message, clc_disconnect);
191                 NET_SendUnreliableMessage (cls.netcon, &cls.message);
192                 SZ_Clear (&cls.message);
193                 NET_Close (cls.netcon);
194                 // if running a local server, shut it down
195                 if (sv.active)
196                         Host_ShutdownServer(false);
197         }
198         cls.state = ca_disconnected;
199
200         cls.demoplayback = cls.timedemo = false;
201         cls.signon = 0;
202 }
203
204 void CL_Disconnect_f (void)
205 {
206         CL_Disconnect ();
207         if (sv.active)
208                 Host_ShutdownServer (false);
209 }
210
211
212
213
214 /*
215 =====================
216 CL_EstablishConnection
217
218 Host should be either "local" or a net address to be passed on
219 =====================
220 */
221 void CL_EstablishConnection (char *host)
222 {
223         if (cls.state == ca_dedicated)
224                 return;
225
226         if (cls.demoplayback)
227                 return;
228
229         CL_Disconnect ();
230
231         cls.netcon = NET_Connect (host);
232         if (!cls.netcon)
233                 Host_Error ("CL_Connect: connect failed\n");
234         Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
235
236         cls.demonum = -1;                       // not in the demo loop now
237         cls.state = ca_connected;
238         cls.signon = 0;                         // need all the signon messages before playing
239 }
240
241 /*
242 ==============
243 CL_PrintEntities_f
244 ==============
245 */
246 static void CL_PrintEntities_f (void)
247 {
248         entity_t        *ent;
249         int                     i, j;
250         char            name[32];
251
252         for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
253         {
254                 if (!ent->state_current.active)
255                         continue;
256                 if (!ent->render.model)
257                         continue;
258
259                 Con_Printf ("%3i:", i);
260                 if (!ent->render.model)
261                 {
262                         Con_Printf ("EMPTY\n");
263                         continue;
264                 }
265                 strncpy(name, ent->render.model->name, 30);
266                 name[30] = 0;
267                 for (j = strlen(name);j < 30;j++)
268                         name[j] = ' ';
269                 Con_Printf ("%s:%04i (%5i %5i %5i) [%3i %3i %3i]\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);
270         }
271 }
272
273
274 /*
275 ===============
276 CL_LerpPoint
277
278 Determines the fraction between the last two messages that the objects
279 should be put at.
280 ===============
281 */
282 static float CL_LerpPoint (void)
283 {
284         float   f, frac;
285
286         f = cl.mtime[0] - cl.mtime[1];
287
288         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
289         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
290         {
291                 cl.time = cl.mtime[0];
292                 return 1;
293         }
294
295         if (f > 0.1)
296         {       // dropped packet, or start of demo
297                 cl.mtime[1] = cl.mtime[0] - 0.1;
298                 f = 0.1;
299         }
300         frac = (cl.time - cl.mtime[1]) / f;
301 //      Con_Printf ("frac: %f\n",frac);
302         if (frac < 0)
303         {
304                 if (frac < -0.01)
305                 {
306                         cl.time = cl.mtime[1];
307 //                      Con_Printf ("low frac\n");
308                 }
309                 frac = 0;
310         }
311         else if (frac > 1)
312         {
313                 if (frac > 1.01)
314                 {
315                         cl.time = cl.mtime[0];
316 //                      Con_Printf ("high frac\n");
317                 }
318                 frac = 1;
319         }
320
321         return frac;
322 }
323
324 static void CL_RelinkStaticEntities(void)
325 {
326         int i;
327         for (i = 0;i < cl.num_statics && cl_numvisedicts < MAX_VISEDICTS;i++)
328         {
329                 Mod_CheckLoaded(cl_static_entities[i].render.model);
330                 cl_visedicts[cl_numvisedicts++] = &cl_static_entities[i];
331         }
332 }
333
334 /*
335 ===============
336 CL_RelinkEntities
337 ===============
338 */
339 static void CL_RelinkNetworkEntities()
340 {
341         entity_t        *ent;
342         int                     i, glowcolor, effects;
343         float           f, d, bobjrotate, bobjoffset, dlightradius, glowsize, lerp;
344         vec3_t          oldorg, neworg, delta, dlightcolor;
345
346         bobjrotate = ANGLEMOD(100*cl.time);
347         if (cl_itembobheight.value)
348                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
349         else
350                 bobjoffset = 0;
351
352         CL_RelinkStaticEntities();
353
354 // start on the entity after the world
355         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
356         {
357                 // if the object wasn't included in the latest packet, remove it
358                 if (!ent->state_current.active)
359                         continue;
360
361                 VectorCopy(ent->persistent.trail_origin, oldorg);
362
363                 if (!ent->state_previous.active)
364                 {
365                         // only one state available
366                         lerp = 1;
367                         VectorCopy (ent->state_current.origin, oldorg); // skip trails
368                         VectorCopy (ent->state_current.origin, neworg);
369                         VectorCopy (ent->state_current.angles, ent->render.angles);
370
371                         /*
372                         // monster interpolation
373                         ent->persistent.steplerptime = 0;
374                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
375                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
376                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
377                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
378                         */
379                 }
380                 /*
381                 else if ((ent->state_current.flags & ent->state_previous.flags) & ENTFLAG_STEP)
382                 {
383                         if (ent->state_current.origin[0] != ent->persistent.steporigin[0]
384                          || ent->state_current.origin[1] != ent->persistent.steporigin[1]
385                          || ent->state_current.origin[2] != ent->persistent.steporigin[2]
386                          || ent->state_current.angles[0] != ent->persistent.stepangles[0]
387                          || ent->state_current.angles[1] != ent->persistent.stepangles[1]
388                          || ent->state_current.angles[2] != ent->persistent.stepangles[2])
389                         {
390                                 // update lerp positions
391                                 ent->clientpersistent.steplerptime = sv.time;
392                                 VectorCopy(ent->steporigin, ent->stepoldorigin);
393                                 VectorCopy(ent->stepangles, ent->stepoldangles);
394                                 VectorCopy(ent->v.origin, ent->steporigin);
395                                 VectorCopy(ent->v.angles, ent->stepangles);
396                         }
397                         lerp = (cl.time - ent->persistent.steplerptime) * 10.0;
398                         if (lerp < 1)
399                         {
400                                 // origin
401                                 VectorSubtract(ent->persistent.steporigin, ent->persistent.stepoldorigin, delta);
402                                 VectorMA(ent->persistent.stepoldorigin, lerp, delta, neworg);
403
404                                 // angles
405                                 VectorSubtract(ent->persistent.stepangles, ent->persistent.stepoldangles, delta);
406                                 // choose shortest rotate (to avoid 'spin around' situations)
407                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
408                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
409                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
410                                 VectorMA(ent->stepoldangles, lerp, delta, ent->render.angles);
411                         }
412                         else
413                         {
414                                 VectorCopy(ent->persistent.steporigin, neworg);
415                                 VectorCopy(ent->persistent.stepangles, ent->render.angles);
416                         }
417                 }
418                 */
419                 else
420                 {
421                         /*
422                         // monster interpolation
423                         ent->persistent.steplerptime = 0;
424                         VectorCopy(ent->state_current.origin, ent->persistent.stepoldorigin);
425                         VectorCopy(ent->state_current.angles, ent->persistent.stepoldangles);
426                         VectorCopy(ent->state_current.origin, ent->persistent.steporigin);
427                         VectorCopy(ent->state_current.angles, ent->persistent.stepangles);
428                         */
429
430                         // if the delta is large, assume a teleport and don't lerp
431                         VectorSubtract(ent->state_current.origin, ent->state_previous.origin, delta);
432                         // LordHavoc: increased tolerance from 100 to 200
433                         if ((sv.active && svs.maxclients == 1 && !(ent->state_current.flags & RENDER_STEP)) || cls.timedemo || DotProduct(delta, delta) > 200*200 || cl_nolerp.integer)
434                                 lerp = 1;
435                         else
436                         {
437                                 f = ent->state_current.time - ent->state_previous.time;
438                                 if (f > 0)
439                                         lerp = (cl.time - ent->state_previous.time) / f;
440                                 else
441                                         lerp = 1;
442                         }
443                         if (lerp >= 1)
444                         {
445                                 // no interpolation
446                                 VectorCopy (ent->state_current.origin, neworg);
447                                 VectorCopy (ent->state_current.angles, ent->render.angles);
448                         }
449                         else
450                         {
451                                 // interpolate the origin and angles
452                                 VectorMA(ent->state_previous.origin, lerp, delta, neworg);
453                                 VectorSubtract(ent->state_current.angles, ent->state_previous.angles, delta);
454                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
455                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
456                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
457                                 VectorMA(ent->state_previous.angles, lerp, delta, ent->render.angles);
458                         }
459                 }
460
461                 VectorCopy (neworg, ent->persistent.trail_origin);
462                 // persistent.modelindex will be updated by CL_LerpUpdate
463                 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
464                         VectorCopy(neworg, oldorg);
465
466                 VectorCopy (neworg, ent->render.origin);
467                 ent->render.flags = ent->state_current.flags;
468                 ent->render.effects = effects = ent->state_current.effects;
469                 ent->render.model = cl.model_precache[ent->state_current.modelindex];
470                 Mod_CheckLoaded(ent->render.model);
471                 ent->render.frame = ent->state_current.frame;
472                 if (cl.scores == NULL || !ent->state_current.colormap)
473                         ent->render.colormap = -1; // no special coloring
474                 else
475                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
476                 ent->render.skinnum = ent->state_current.skin;
477                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
478                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
479                 glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
480                 glowcolor = ent->state_current.glowcolor;
481
482                 // update interpolation info
483                 CL_LerpUpdate(ent, ent->state_current.frame, ent->state_current.modelindex);
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                                         dlightcolor[0] += 200.0f;
589                                         dlightcolor[1] += 160.0f;
590                                         dlightcolor[2] +=  80.0f;
591                                 }
592                                 else if (ent->render.model->flags & EF_GRENADE)
593                                 {
594                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
595                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
596                                         else
597                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
598                                 }
599                                 else if (ent->render.model->flags & EF_TRACER3)
600                                         CL_RocketTrail (oldorg, neworg, 6, ent);
601                         }
602                 }
603                 // LordHavoc: customizable glow
604                 if (glowsize)
605                 {
606                         byte *tempcolor = (byte *)&d_8to24table[glowcolor];
607                         dlightcolor[0] += glowsize * tempcolor[0] * (1.0f / 255.0f);
608                         dlightcolor[1] += glowsize * tempcolor[1] * (1.0f / 255.0f);
609                         dlightcolor[2] += glowsize * tempcolor[2] * (1.0f / 255.0f);
610                 }
611                 // LordHavoc: customizable trail
612                 if (ent->render.flags & RENDER_GLOWTRAIL)
613                         CL_RocketTrail2 (oldorg, neworg, glowcolor, ent);
614
615                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
616                 {
617                         vec3_t vec;
618                         dlightradius = VectorLength(dlightcolor);
619                         d = 1.0f / dlightradius;
620                         VectorCopy(neworg, vec);
621                         // hack to make glowing player light shine on their gun
622                         if (i == cl.viewentity && !chase_active.integer)
623                                 vec[2] += 30;
624                         CL_AllocDlight (&ent->render, vec, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
625                 }
626
627                 if (chase_active.integer)
628                 {
629                         if (ent->render.flags & RENDER_VIEWMODEL)
630                                 continue;
631                 }
632                 else
633                 {
634                         if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
635                                 continue;
636                 }
637
638                 if (ent->render.model == NULL)
639                         continue;
640                 if (effects & EF_NODRAW)
641                         continue;
642                 if (cl_numvisedicts < MAX_VISEDICTS)
643                         cl_visedicts[cl_numvisedicts++] = ent;
644         }
645 }
646
647 static void CL_LerpPlayerVelocity (void)
648 {
649         int i;
650         float frac, d;
651
652         // fraction from previous network update to current
653         frac = CL_LerpPoint ();
654
655         for (i = 0;i < 3;i++)
656                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
657
658         if (cls.demoplayback)
659         {
660                 // interpolate the angles
661                 for (i = 0;i < 3;i++)
662                 {
663                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
664                         if (d > 180)
665                                 d -= 360;
666                         else if (d < -180)
667                                 d += 360;
668                         cl.viewangles[i] = cl.mviewangles[1][i] + frac*d;
669                 }
670         }
671 }
672
673 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
674 {
675         int i;
676         cl_effect_t *e;
677         if (!modelindex) // sanity check
678                 return;
679         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
680         {
681                 if (e->active)
682                         continue;
683                 e->active = true;
684                 VectorCopy(org, e->origin);
685                 e->modelindex = modelindex;
686                 e->starttime = cl.time;
687                 e->startframe = startframe;
688                 e->endframe = startframe + framecount;
689                 e->framerate = framerate;
690
691                 e->frame = 0;
692                 e->frame1time = cl.time;
693                 e->frame2time = cl.time;
694                 break;
695         }
696 }
697
698 static void CL_RelinkEffects()
699 {
700         int i, intframe;
701         cl_effect_t *e;
702         entity_t *vis;
703         float frame;
704
705         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
706         {
707                 if (e->active)
708                 {
709                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
710                         intframe = frame;
711                         if (intframe < 0 || intframe >= e->endframe)
712                         {
713                                 e->active = false;
714                                 memset(e, 0, sizeof(*e));
715                                 continue;
716                         }
717
718                         if (intframe != e->frame)
719                         {
720                                 e->frame = intframe;
721                                 e->frame1time = e->frame2time;
722                                 e->frame2time = cl.time;
723                         }
724
725                         if ((vis = CL_NewTempEntity()))
726                         {
727                                 // interpolation stuff
728                                 vis->render.frame1 = intframe;
729                                 vis->render.frame2 = intframe + 1;
730                                 if (vis->render.frame2 >= e->endframe)
731                                         vis->render.frame2 = -1; // disappear
732                                 vis->render.framelerp = frame - intframe;
733                                 vis->render.frame1time = e->frame1time;
734                                 vis->render.frame2time = e->frame2time;
735
736                                 // normal stuff
737                                 VectorCopy(e->origin, vis->render.origin);
738                                 vis->render.model = cl.model_precache[e->modelindex];
739                                 vis->render.frame = vis->render.frame2;
740                                 vis->render.colormap = -1; // no special coloring
741                                 vis->render.scale = 1;
742                                 vis->render.alpha = 1;
743                         }
744                 }
745         }
746 }
747
748 void CL_RelinkEntities (void)
749 {
750         cl_numvisedicts = 0;
751
752         CL_LerpPlayerVelocity();
753         CL_RelinkNetworkEntities();
754         CL_RelinkEffects();
755         CL_MoveParticles();
756         CL_UpdateDecals();
757 }
758
759
760 /*
761 ===============
762 CL_ReadFromServer
763
764 Read all incoming data from the server
765 ===============
766 */
767 int CL_ReadFromServer (void)
768 {
769         int ret, netshown;
770
771         cl.oldtime = cl.time;
772         cl.time += cl.frametime;
773
774         netshown = false;
775         do
776         {
777                 ret = CL_GetMessage ();
778                 if (ret == -1)
779                         Host_Error ("CL_ReadFromServer: lost server connection");
780                 if (!ret)
781                         break;
782
783                 cl.last_received_message = realtime;
784
785                 if (cl_shownet.integer)
786                         netshown = true;
787
788                 CL_ParseServerMessage ();
789         }
790         while (ret && cls.state == ca_connected);
791
792         if (netshown)
793                 Con_Printf ("\n");
794
795         CL_RelinkEntities ();
796         CL_UpdateTEnts ();
797
798 //
799 // bring the links up to date
800 //
801         return 0;
802 }
803
804 /*
805 =================
806 CL_SendCmd
807 =================
808 */
809 void CL_SendCmd (void)
810 {
811         usercmd_t               cmd;
812
813         if (cls.state != ca_connected)
814                 return;
815
816         if (cls.signon == SIGNONS)
817         {
818         // get basic movement from keyboard
819                 CL_BaseMove (&cmd);
820
821         // allow mice or other external controllers to add to the move
822                 IN_Move (&cmd);
823
824         // send the unreliable message
825                 CL_SendMove (&cmd);
826         }
827
828         if (cls.demoplayback)
829         {
830                 SZ_Clear (&cls.message);
831                 return;
832         }
833
834 // send the reliable message
835         if (!cls.message.cursize)
836                 return;         // no message at all
837
838         if (!NET_CanSendMessage (cls.netcon))
839         {
840                 Con_DPrintf ("CL_WriteToServer: can't send\n");
841                 return;
842         }
843
844         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
845                 Host_Error ("CL_WriteToServer: lost server connection");
846
847         SZ_Clear (&cls.message);
848 }
849
850 // LordHavoc: pausedemo command
851 static void CL_PauseDemo_f (void)
852 {
853         cls.demopaused = !cls.demopaused;
854         if (cls.demopaused)
855                 Con_Printf("Demo paused\n");
856         else
857                 Con_Printf("Demo unpaused\n");
858 }
859
860 /*
861 ======================
862 CL_PModel_f
863 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
864 ======================
865 */
866 static void CL_PModel_f (void)
867 {
868         int i;
869         eval_t *val;
870
871         if (Cmd_Argc () == 1)
872         {
873                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
874                 return;
875         }
876         i = atoi(Cmd_Argv(1));
877
878         if (cmd_source == src_command)
879         {
880                 if (cl_pmodel.integer == i)
881                         return;
882                 Cvar_SetValue ("_cl_pmodel", i);
883                 if (cls.state == ca_connected)
884                         Cmd_ForwardToServer ();
885                 return;
886         }
887
888         host_client->pmodel = i;
889         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
890                 val->_float = i;
891 }
892
893 /*
894 ======================
895 CL_Fog_f
896 ======================
897 */
898 static void CL_Fog_f (void)
899 {
900         if (Cmd_Argc () == 1)
901         {
902                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
903                 return;
904         }
905         fog_density = atof(Cmd_Argv(1));
906         fog_red = atof(Cmd_Argv(2));
907         fog_green = atof(Cmd_Argv(3));
908         fog_blue = atof(Cmd_Argv(4));
909 }
910
911 /*
912 =================
913 CL_Init
914 =================
915 */
916 void CL_Init (void)
917 {
918         SZ_Alloc (&cls.message, 1024, "cls.message");
919
920         CL_InitInput ();
921         CL_InitTEnts ();
922
923 //
924 // register our commands
925 //
926         Cvar_RegisterVariable (&cl_name);
927         Cvar_RegisterVariable (&cl_color);
928         if (gamemode == GAME_NEHAHRA)
929                 Cvar_RegisterVariable (&cl_pmodel);
930         Cvar_RegisterVariable (&cl_upspeed);
931         Cvar_RegisterVariable (&cl_forwardspeed);
932         Cvar_RegisterVariable (&cl_backspeed);
933         Cvar_RegisterVariable (&cl_sidespeed);
934         Cvar_RegisterVariable (&cl_movespeedkey);
935         Cvar_RegisterVariable (&cl_yawspeed);
936         Cvar_RegisterVariable (&cl_pitchspeed);
937         Cvar_RegisterVariable (&cl_anglespeedkey);
938         Cvar_RegisterVariable (&cl_shownet);
939         Cvar_RegisterVariable (&cl_nolerp);
940         Cvar_RegisterVariable (&lookspring);
941         Cvar_RegisterVariable (&lookstrafe);
942         Cvar_RegisterVariable (&sensitivity);
943         Cvar_RegisterVariable (&freelook);
944
945         Cvar_RegisterVariable (&m_pitch);
946         Cvar_RegisterVariable (&m_yaw);
947         Cvar_RegisterVariable (&m_forward);
948         Cvar_RegisterVariable (&m_side);
949
950         Cvar_RegisterVariable (&cl_itembobspeed);
951         Cvar_RegisterVariable (&cl_itembobheight);
952
953         Cmd_AddCommand ("entities", CL_PrintEntities_f);
954         Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
955         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
956         Cmd_AddCommand ("record", CL_Record_f);
957         Cmd_AddCommand ("stop", CL_Stop_f);
958         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
959         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
960
961         Cmd_AddCommand ("fog", CL_Fog_f);
962
963         // LordHavoc: added pausedemo
964         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
965         if (gamemode == GAME_NEHAHRA)
966                 Cmd_AddCommand ("pmodel", CL_PModel_f);
967
968         cl_scores_mempool = Mem_AllocPool("client player info");
969
970         Cvar_RegisterVariable(&cl_draweffects);
971
972         CL_Parse_Init();
973         CL_Particles_Init();
974         CL_Decals_Init();
975 }