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