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