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