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