]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_main.c
A minor removal of a few pieces of dead code. Nothing major. This is
[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
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                                         CL_RocketTrail (oldorg, neworg, 3, ent);
525                                 else if (ent->render.model->flags & EF_TRACER2)
526                                         CL_RocketTrail (oldorg, neworg, 5, ent);
527                                 else if (ent->render.model->flags & EF_ROCKET)
528                                 {
529                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
530                                         dlightcolor[0] += 200.0f;
531                                         dlightcolor[1] += 160.0f;
532                                         dlightcolor[2] +=  80.0f;
533                                 }
534                                 else if (ent->render.model->flags & EF_GRENADE)
535                                 {
536                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
537                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
538                                         else
539                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
540                                 }
541                                 else if (ent->render.model->flags & EF_TRACER3)
542                                         CL_RocketTrail (oldorg, neworg, 6, ent);
543                         }
544                 }
545                 // LordHavoc: customizable glow
546                 glowsize = ent->state_current.glowsize; // FIXME: interpolate?
547                 glowcolor = ent->state_current.glowcolor;
548                 if (glowsize)
549                 {
550                         qbyte *tempcolor = (qbyte *)&d_8to24table[glowcolor];
551                         // * 4 for the expansion from 0-255 to 0-1023 range,
552                         // / 255 to scale down byte colors
553                         glowsize *= (4.0f / 255.0f);
554                         VectorMA(dlightcolor, glowsize, tempcolor, dlightcolor);
555                 }
556                 // LordHavoc: customizable trail
557                 if (ent->render.flags & RENDER_GLOWTRAIL)
558                         CL_RocketTrail2 (oldorg, neworg, glowcolor, ent);
559
560                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
561                 {
562                         vec3_t vec;
563                         VectorCopy(neworg, vec);
564                         // hack to make glowing player light shine on their gun
565                         if (i == cl.viewentity && !chase_active.integer)
566                                 vec[2] += 30;
567                         CL_AllocDlight (NULL, vec, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
568                 }
569
570                 if (chase_active.integer)
571                 {
572                         if (ent->render.flags & RENDER_VIEWMODEL)
573                                 continue;
574                 }
575                 else
576                 {
577                         if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
578                                 continue;
579                 }
580
581                 if (ent->render.model == NULL)
582                         continue;
583                 if (effects & EF_NODRAW)
584                         continue;
585                 if (r_refdef.numentities < MAX_VISEDICTS)
586                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
587         }
588 }
589
590 void CL_LerpPlayer(float frac)
591 {
592         int i;
593         float d;
594
595         if (cl.entitydatabase.numframes)
596         {
597                 cl.viewentorigin[0] = cl.viewentoriginold[0] + frac * (cl.viewentoriginnew[0] - cl.viewentoriginold[0]);
598                 cl.viewentorigin[1] = cl.viewentoriginold[1] + frac * (cl.viewentoriginnew[1] - cl.viewentoriginold[1]);
599                 cl.viewentorigin[2] = cl.viewentoriginold[2] + frac * (cl.viewentoriginnew[2] - cl.viewentoriginold[2]);
600         }
601         else
602                 VectorCopy (cl_entities[cl.viewentity].render.origin, cl.viewentorigin);
603
604         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
605
606         for (i = 0;i < 3;i++)
607                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
608
609         if (cls.demoplayback)
610         {
611                 // interpolate the angles
612                 for (i = 0;i < 3;i++)
613                 {
614                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
615                         if (d > 180)
616                                 d -= 360;
617                         else if (d < -180)
618                                 d += 360;
619                         cl.viewangles[i] = cl.mviewangles[1][i] + frac*d;
620                 }
621         }
622 }
623
624 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
625 {
626         int i;
627         cl_effect_t *e;
628         if (!modelindex) // sanity check
629                 return;
630         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
631         {
632                 if (e->active)
633                         continue;
634                 e->active = true;
635                 VectorCopy(org, e->origin);
636                 e->modelindex = modelindex;
637                 e->starttime = cl.time;
638                 e->startframe = startframe;
639                 e->endframe = startframe + framecount;
640                 e->framerate = framerate;
641
642                 e->frame = 0;
643                 e->frame1time = cl.time;
644                 e->frame2time = cl.time;
645                 break;
646         }
647 }
648
649 static void CL_RelinkEffects()
650 {
651         int i, intframe;
652         cl_effect_t *e;
653         entity_t *vis;
654         float frame;
655
656         for (i = 0, e = cl_effect;i < MAX_EFFECTS;i++, e++)
657         {
658                 if (e->active)
659                 {
660                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
661                         intframe = frame;
662                         if (intframe < 0 || intframe >= e->endframe)
663                         {
664                                 e->active = false;
665                                 memset(e, 0, sizeof(*e));
666                                 continue;
667                         }
668
669                         if (intframe != e->frame)
670                         {
671                                 e->frame = intframe;
672                                 e->frame1time = e->frame2time;
673                                 e->frame2time = cl.time;
674                         }
675
676                         if ((vis = CL_NewTempEntity()))
677                         {
678                                 // interpolation stuff
679                                 vis->render.frame1 = intframe;
680                                 vis->render.frame2 = intframe + 1;
681                                 if (vis->render.frame2 >= e->endframe)
682                                         vis->render.frame2 = -1; // disappear
683                                 vis->render.framelerp = frame - intframe;
684                                 vis->render.frame1time = e->frame1time;
685                                 vis->render.frame2time = e->frame2time;
686
687                                 // normal stuff
688                                 VectorCopy(e->origin, vis->render.origin);
689                                 vis->render.model = cl.model_precache[e->modelindex];
690                                 vis->render.frame = vis->render.frame2;
691                                 vis->render.colormap = -1; // no special coloring
692                                 vis->render.scale = 1;
693                                 vis->render.alpha = 1;
694                         }
695                 }
696         }
697 }
698
699 void CL_RelinkEntities (void)
700 {
701         float frac;
702
703         // fraction from previous network update to current
704         frac = CL_LerpPoint ();
705
706         CL_DecayLights ();
707         CL_RelinkStaticEntities();
708         CL_RelinkNetworkEntities();
709         CL_TraceLine_ScanForBModels();
710         CL_RelinkEffects();
711         CL_MoveParticles();
712         CL_UpdateTEnts();
713
714         CL_LerpPlayer(frac);
715 }
716
717
718 /*
719 ===============
720 CL_ReadFromServer
721
722 Read all incoming data from the server
723 ===============
724 */
725 int CL_ReadFromServer (void)
726 {
727         int ret, netshown;
728
729         cl.oldtime = cl.time;
730         cl.time += cl.frametime;
731
732         netshown = false;
733         do
734         {
735                 ret = CL_GetMessage ();
736                 if (ret == -1)
737                         Host_Error ("CL_ReadFromServer: lost server connection");
738                 if (!ret)
739                         break;
740
741                 cl.last_received_message = realtime;
742
743                 if (cl_shownet.integer)
744                         netshown = true;
745
746                 CL_ParseServerMessage ();
747         }
748         while (ret && cls.state == ca_connected);
749
750         if (netshown)
751                 Con_Printf ("\n");
752
753         r_refdef.numentities = 0;
754         if (cls.state == ca_connected && cl.worldmodel)
755         {
756                 CL_RelinkEntities ();
757
758                 // run cgame code (which can add more entities)
759                 CL_CGVM_Frame();
760         }
761
762 //
763 // bring the links up to date
764 //
765         return 0;
766 }
767
768 /*
769 =================
770 CL_SendCmd
771 =================
772 */
773 void CL_SendCmd (void)
774 {
775         usercmd_t               cmd;
776
777         if (cls.state != ca_connected)
778                 return;
779
780         if (cls.signon == SIGNONS)
781         {
782         // get basic movement from keyboard
783                 CL_BaseMove (&cmd);
784
785                 IN_PreMove(); // OS independent code
786
787         // allow mice or other external controllers to add to the move
788                 IN_Move (&cmd);
789
790                 IN_PostMove(); // OS independent code
791
792         // send the unreliable message
793                 CL_SendMove (&cmd);
794         }
795 #ifndef NOROUTINGFIX
796         else
797         {
798                 // LordHavoc: fix for NAT routing of netquake:
799                 // bounce back a clc_nop message to the newly allocated server port,
800                 // to establish a routing connection for incoming frames,
801                 // the server waits for this before sending anything
802                 if (realtime > cl.sendnoptime)
803                 {
804                         Con_DPrintf("sending clc_nop to get server's attention\n");
805                         cl.sendnoptime = realtime + 3;
806                         MSG_WriteByte(&cls.message, clc_nop);
807                 }
808         }
809 #endif
810
811         if (cls.demoplayback)
812         {
813                 SZ_Clear (&cls.message);
814                 return;
815         }
816
817 // send the reliable message
818         if (!cls.message.cursize)
819                 return;         // no message at all
820
821         if (!NET_CanSendMessage (cls.netcon))
822         {
823                 Con_DPrintf ("CL_WriteToServer: can't send\n");
824                 return;
825         }
826
827         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
828                 Host_Error ("CL_WriteToServer: lost server connection");
829
830         SZ_Clear (&cls.message);
831 }
832
833 // LordHavoc: pausedemo command
834 static void CL_PauseDemo_f (void)
835 {
836         cls.demopaused = !cls.demopaused;
837         if (cls.demopaused)
838                 Con_Printf("Demo paused\n");
839         else
840                 Con_Printf("Demo unpaused\n");
841 }
842
843 /*
844 ======================
845 CL_PModel_f
846 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
847 ======================
848 */
849 static void CL_PModel_f (void)
850 {
851         int i;
852         eval_t *val;
853
854         if (Cmd_Argc () == 1)
855         {
856                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
857                 return;
858         }
859         i = atoi(Cmd_Argv(1));
860
861         if (cmd_source == src_command)
862         {
863                 if (cl_pmodel.integer == i)
864                         return;
865                 Cvar_SetValue ("_cl_pmodel", i);
866                 if (cls.state == ca_connected)
867                         Cmd_ForwardToServer ();
868                 return;
869         }
870
871         host_client->pmodel = i;
872         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
873                 val->_float = i;
874 }
875
876 /*
877 ======================
878 CL_Fog_f
879 ======================
880 */
881 static void CL_Fog_f (void)
882 {
883         if (Cmd_Argc () == 1)
884         {
885                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
886                 return;
887         }
888         fog_density = atof(Cmd_Argv(1));
889         fog_red = atof(Cmd_Argv(2));
890         fog_green = atof(Cmd_Argv(3));
891         fog_blue = atof(Cmd_Argv(4));
892 }
893
894 /*
895 =================
896 CL_Init
897 =================
898 */
899 void CL_Init (void)
900 {
901         cl_scores_mempool = Mem_AllocPool("client player info");
902
903         cl_refdef_mempool = Mem_AllocPool("refdef");
904         memset(&r_refdef, 0, sizeof(r_refdef));
905         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * MAX_VISEDICTS);
906
907         SZ_Alloc (&cls.message, 1024, "cls.message");
908
909         CL_InitInput ();
910         CL_InitTEnts ();
911
912 //
913 // register our commands
914 //
915         Cvar_RegisterVariable (&cl_name);
916         Cvar_RegisterVariable (&cl_color);
917         if (gamemode == GAME_NEHAHRA)
918                 Cvar_RegisterVariable (&cl_pmodel);
919         Cvar_RegisterVariable (&cl_upspeed);
920         Cvar_RegisterVariable (&cl_forwardspeed);
921         Cvar_RegisterVariable (&cl_backspeed);
922         Cvar_RegisterVariable (&cl_sidespeed);
923         Cvar_RegisterVariable (&cl_movespeedkey);
924         Cvar_RegisterVariable (&cl_yawspeed);
925         Cvar_RegisterVariable (&cl_pitchspeed);
926         Cvar_RegisterVariable (&cl_anglespeedkey);
927         Cvar_RegisterVariable (&cl_shownet);
928         Cvar_RegisterVariable (&cl_nolerp);
929         Cvar_RegisterVariable (&lookspring);
930         Cvar_RegisterVariable (&lookstrafe);
931         Cvar_RegisterVariable (&sensitivity);
932         Cvar_RegisterVariable (&freelook);
933
934         Cvar_RegisterVariable (&m_pitch);
935         Cvar_RegisterVariable (&m_yaw);
936         Cvar_RegisterVariable (&m_forward);
937         Cvar_RegisterVariable (&m_side);
938
939         Cvar_RegisterVariable (&cl_itembobspeed);
940         Cvar_RegisterVariable (&cl_itembobheight);
941
942         Cmd_AddCommand ("entities", CL_PrintEntities_f);
943         Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
944         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
945         Cmd_AddCommand ("record", CL_Record_f);
946         Cmd_AddCommand ("stop", CL_Stop_f);
947         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
948         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
949
950         Cmd_AddCommand ("fog", CL_Fog_f);
951
952         // LordHavoc: added pausedemo
953         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
954         if (gamemode == GAME_NEHAHRA)
955                 Cmd_AddCommand ("pmodel", CL_PModel_f);
956
957         Cvar_RegisterVariable(&cl_draweffects);
958
959         CL_Parse_Init();
960         CL_Particles_Init();
961         CL_Screen_Init();
962         CL_CGVM_Init();
963 }
964