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