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