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