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