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