]> git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_main.c
8ab9bf730417ba593fea2b66cbf8085fdfdaee2c
[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 #include "image.h"
26
27 // we need to declare some mouse variables here, because the menu system
28 // references them even when on a unix system.
29
30 cvar_t cl_shownet = {0, "cl_shownet","0"};
31 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
32
33 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
34 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
35
36 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
37 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
38 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
39
40 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
41 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
42 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
43 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
44
45 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
46
47 cvar_t r_draweffects = {0, "r_draweffects", "1"};
48
49 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
50 cvar_t cl_explosions_alpha_start = {CVAR_SAVE, "cl_explosions_alpha_start", "1.5"};
51 cvar_t cl_explosions_alpha_end = {CVAR_SAVE, "cl_explosions_alpha_end", "0"};
52 cvar_t cl_explosions_size_start = {CVAR_SAVE, "cl_explosions_size_start", "16"};
53 cvar_t cl_explosions_size_end = {CVAR_SAVE, "cl_explosions_size_end", "128"};
54 cvar_t cl_explosions_lifetime = {CVAR_SAVE, "cl_explosions_lifetime", "0.5"};
55
56 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
57
58 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1"};
59 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1"};
60 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0"};
61
62 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0"};
63
64 mempool_t *cl_refdef_mempool;
65 mempool_t *cl_entities_mempool;
66
67 client_static_t cls;
68 client_state_t  cl;
69
70 int cl_max_entities;
71 int cl_max_static_entities;
72 int cl_max_temp_entities;
73 int cl_max_effects;
74 int cl_max_beams;
75 int cl_max_dlights;
76 int cl_max_lightstyle;
77 int cl_max_brushmodel_entities;
78
79 entity_t *cl_entities;
80 qbyte *cl_entities_active;
81 entity_t *cl_static_entities;
82 entity_t *cl_temp_entities;
83 cl_effect_t *cl_effects;
84 beam_t *cl_beams;
85 dlight_t *cl_dlights;
86 lightstyle_t *cl_lightstyle;
87 entity_render_t **cl_brushmodel_entities;
88
89 int cl_num_entities;
90 int cl_num_static_entities;
91 int cl_num_temp_entities;
92 int cl_num_brushmodel_entities;
93
94 /*
95 =====================
96 CL_ClearState
97
98 =====================
99 */
100 void CL_ClearState(void)
101 {
102         int i;
103
104         if (!sv.active)
105                 Host_ClearMemory ();
106
107         // note: this also gets rid of the entity database
108         Mem_EmptyPool(cl_entities_mempool);
109
110 // wipe the entire cl structure
111         memset (&cl, 0, sizeof(cl));
112
113         SZ_Clear (&cls.message);
114
115         cl_num_entities = 0;
116         cl_num_static_entities = 0;
117         cl_num_temp_entities = 0;
118         cl_num_brushmodel_entities = 0;
119
120         // tweak these if the game runs out
121         cl_max_entities = MAX_EDICTS;
122         cl_max_static_entities = 256;
123         cl_max_temp_entities = 512;
124         cl_max_effects = 256;
125         cl_max_beams = 256;
126         cl_max_dlights = MAX_DLIGHTS;
127         cl_max_lightstyle = MAX_LIGHTSTYLES;
128         cl_max_brushmodel_entities = MAX_EDICTS;
129
130         cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
131         cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
132         cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
133         cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
134         cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
135         cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
136         cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
137         cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
138         cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
139
140         CL_Screen_NewMap();
141
142         CL_Particles_Clear();
143
144         // LordHavoc: have to set up the baseline info for alpha and other stuff
145         for (i = 0;i < cl_max_entities;i++)
146         {
147                 ClearStateToDefault(&cl_entities[i].state_baseline);
148                 ClearStateToDefault(&cl_entities[i].state_previous);
149                 ClearStateToDefault(&cl_entities[i].state_current);
150         }
151
152         CL_CGVM_Clear();
153 }
154
155 /*
156 =====================
157 CL_Disconnect
158
159 Sends a disconnect message to the server
160 This is also called on Host_Error, so it shouldn't cause any errors
161 =====================
162 */
163 void CL_Disconnect(void)
164 {
165         if (cls.state == ca_dedicated)
166                 return;
167
168         Con_DPrintf("CL_Disconnect\n");
169
170 // stop sounds (especially looping!)
171         S_StopAllSounds (true);
172
173         // clear contents blends
174         cl.cshifts[0].percent = 0;
175         cl.cshifts[1].percent = 0;
176         cl.cshifts[2].percent = 0;
177         cl.cshifts[3].percent = 0;
178
179         cl.worldmodel = NULL;
180
181         if (cls.demoplayback)
182                 CL_StopPlayback();
183         else if (cls.netcon)
184         {
185                 if (cls.demorecording)
186                         CL_Stop_f();
187
188                 Con_DPrint("Sending clc_disconnect\n");
189                 SZ_Clear(&cls.message);
190                 MSG_WriteByte(&cls.message, clc_disconnect);
191                 NetConn_SendUnreliableMessage(cls.netcon, &cls.message);
192                 SZ_Clear(&cls.message);
193                 NetConn_Close(cls.netcon);
194                 cls.netcon = NULL;
195         }
196         cls.state = ca_disconnected;
197
198         cls.demoplayback = cls.timedemo = false;
199         cls.signon = 0;
200 }
201
202 void CL_Disconnect_f(void)
203 {
204         CL_Disconnect ();
205         if (sv.active)
206                 Host_ShutdownServer (false);
207 }
208
209
210
211
212 /*
213 =====================
214 CL_EstablishConnection
215
216 Host should be either "local" or a net address
217 =====================
218 */
219 void CL_EstablishConnection(const char *host)
220 {
221         if (cls.state == ca_dedicated)
222                 return;
223
224         // clear menu's connect error message
225         m_return_reason[0] = 0;
226         cls.demonum = -1;
227
228         // stop demo loop in case this fails
229         CL_Disconnect();
230         NetConn_ClientFrame();
231         NetConn_ServerFrame();
232         
233         if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
234         {
235                 cls.connect_trying = true;
236                 cls.connect_remainingtries = 3;
237                 cls.connect_nextsendtime = 0;
238                 if (sv.active)
239                 {
240                         NetConn_ClientFrame();
241                         NetConn_ServerFrame();
242                         NetConn_ClientFrame();
243                         NetConn_ServerFrame();
244                         NetConn_ClientFrame();
245                         NetConn_ServerFrame();
246                         NetConn_ClientFrame();
247                         NetConn_ServerFrame();
248                 }
249         }
250         else
251         {
252                 Con_Print("Unable to find a suitable network socket to connect to server.\n");
253                 strcpy(m_return_reason, "No network");
254         }
255 }
256
257 /*
258 ==============
259 CL_PrintEntities_f
260 ==============
261 */
262 static void CL_PrintEntities_f(void)
263 {
264         entity_t *ent;
265         int i, j;
266         char name[32];
267
268         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
269         {
270                 if (!ent->state_current.active)
271                         continue;
272
273                 if (ent->render.model)
274                         strlcpy (name, ent->render.model->name, 25);
275                 else
276                         strcpy(name, "--no model--");
277                 for (j = strlen(name);j < 25;j++)
278                         name[j] = ' ';
279                 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);
280         }
281 }
282
283 //static const vec3_t nomodelmins = {-16, -16, -16};
284 //static const vec3_t nomodelmaxs = {16, 16, 16};
285 void CL_BoundingBoxForEntity(entity_render_t *ent)
286 {
287         if (ent->model)
288         {
289                 //if (ent->angles[0] || ent->angles[2])
290                 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
291                 {
292                         // pitch or roll
293                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
294                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
295                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
296                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
297                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
298                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
299                         //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
300                         //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
301                 }
302                 //else if (ent->angles[1])
303                 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
304                 {
305                         // yaw
306                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
307                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
308                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
309                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
310                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
311                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
312                         //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
313                         //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
314                 }
315                 else
316                 {
317                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
318                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
319                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
320                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
321                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
322                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
323                         //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
324                         //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
325                 }
326         }
327         else
328         {
329                 ent->mins[0] = ent->matrix.m[0][3] - 16;
330                 ent->mins[1] = ent->matrix.m[1][3] - 16;
331                 ent->mins[2] = ent->matrix.m[2][3] - 16;
332                 ent->maxs[0] = ent->matrix.m[0][3] + 16;
333                 ent->maxs[1] = ent->matrix.m[1][3] + 16;
334                 ent->maxs[2] = ent->matrix.m[2][3] + 16;
335                 //VectorAdd(ent->origin, nomodelmins, ent->mins);
336                 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
337         }
338 }
339
340 /*
341 ===============
342 CL_LerpPoint
343
344 Determines the fraction between the last two messages that the objects
345 should be put at.
346 ===============
347 */
348 static float CL_LerpPoint(void)
349 {
350         float f;
351
352         // dropped packet, or start of demo
353         if (cl.mtime[1] < cl.mtime[0] - 0.1)
354                 cl.mtime[1] = cl.mtime[0] - 0.1;
355
356         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
357
358         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
359         f = cl.mtime[0] - cl.mtime[1];
360         if (!f || cl_nolerp.integer || cls.timedemo || cl.islocalgame)
361         {
362                 cl.time = cl.mtime[0];
363                 return 1;
364         }
365
366         f = (cl.time - cl.mtime[1]) / f;
367         return bound(0, f, 1);
368 }
369
370 void CL_ClearTempEntities (void)
371 {
372         cl_num_temp_entities = 0;
373 }
374
375 entity_t *CL_NewTempEntity(void)
376 {
377         entity_t *ent;
378
379         if (r_refdef.numentities >= r_refdef.maxentities)
380                 return NULL;
381         if (cl_num_temp_entities >= cl_max_temp_entities)
382                 return NULL;
383         ent = &cl_temp_entities[cl_num_temp_entities++];
384         memset (ent, 0, sizeof(*ent));
385         r_refdef.entities[r_refdef.numentities++] = &ent->render;
386
387         ent->render.colormap = -1; // no special coloring
388         ent->render.scale = 1;
389         ent->render.alpha = 1;
390         return ent;
391 }
392
393 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
394 {
395         int i;
396         cl_effect_t *e;
397         if (!modelindex) // sanity check
398                 return;
399         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
400         {
401                 if (e->active)
402                         continue;
403                 e->active = true;
404                 VectorCopy(org, e->origin);
405                 e->modelindex = modelindex;
406                 e->starttime = cl.time;
407                 e->startframe = startframe;
408                 e->endframe = startframe + framecount;
409                 e->framerate = framerate;
410
411                 e->frame = 0;
412                 e->frame1time = cl.time;
413                 e->frame2time = cl.time;
414                 break;
415         }
416 }
417
418 void CL_AllocDlight(entity_render_t *ent, matrix4x4_t *matrix, float radius, float red, float green, float blue, float decay, float lifetime, int cubemapnum, int style, int shadowenable, vec_t corona)
419 {
420         int i;
421         dlight_t *dl;
422
423         /*
424 // first look for an exact key match
425         if (ent)
426         {
427                 dl = cl_dlights;
428                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
429                         if (dl->ent == ent)
430                                 goto dlightsetup;
431         }
432         */
433
434 // then look for anything else
435         dl = cl_dlights;
436         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
437                 if (!dl->radius)
438                         goto dlightsetup;
439
440         // unable to find one
441         return;
442
443 dlightsetup:
444         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
445         memset (dl, 0, sizeof(*dl));
446         dl->matrix = *matrix;
447         dl->ent = ent;
448         dl->origin[0] = dl->matrix.m[0][3];
449         dl->origin[1] = dl->matrix.m[1][3];
450         dl->origin[2] = dl->matrix.m[2][3];
451         CL_FindNonSolidLocation(dl->origin, dl->origin, 6);
452         dl->matrix.m[0][3] = dl->origin[0];
453         dl->matrix.m[1][3] = dl->origin[1];
454         dl->matrix.m[2][3] = dl->origin[2];
455         dl->radius = radius;
456         dl->color[0] = red;
457         dl->color[1] = green;
458         dl->color[2] = blue;
459         dl->decay = decay;
460         if (lifetime)
461                 dl->die = cl.time + lifetime;
462         else
463                 dl->die = 0;
464         dl->cubemapnum = cubemapnum;
465         dl->style = style;
466         dl->shadow = shadowenable;
467         dl->corona = corona;
468 }
469
470 void CL_DecayLights(void)
471 {
472         int i;
473         dlight_t *dl;
474         float time;
475
476         time = cl.time - cl.oldtime;
477         for (i = 0, dl = cl_dlights;i < MAX_DLIGHTS;i++, dl++)
478                 if (dl->radius)
479                         dl->radius = (cl.time < dl->die) ? max(0, dl->radius - time * dl->decay) : 0;
480 }
481
482 #define MAXVIEWMODELS 32
483 entity_t *viewmodels[MAXVIEWMODELS];
484 int numviewmodels;
485
486 matrix4x4_t viewmodelmatrix;
487
488 static int entitylinkframenumber;
489
490 static const vec3_t muzzleflashorigin = {18, 0, 0};
491
492 extern void V_DriftPitch(void);
493 extern void V_FadeViewFlashs(void);
494 extern void V_CalcViewBlend(void);
495
496 extern void V_CalcRefdef(void);
497 // note this is a recursive function, but it can never get in a runaway loop (because of the delayedlink flags)
498 void CL_LinkNetworkEntity(entity_t *e)
499 {
500         matrix4x4_t *matrix, blendmatrix, tempmatrix, matrix2;
501         //matrix4x4_t dlightmatrix;
502         int j, k, l, trailtype, temp;
503         float origin[3], angles[3], delta[3], lerp, dlightcolor[3], dlightradius, mins[3], maxs[3], v[3], v2[3], d;
504         entity_t *t;
505         model_t *model;
506         //entity_persistent_t *p = &e->persistent;
507         //entity_render_t *r = &e->render;
508         if (e->persistent.linkframe != entitylinkframenumber)
509         {
510                 e->persistent.linkframe = entitylinkframenumber;
511                 // skip inactive entities and world
512                 if (!e->state_current.active || e == cl_entities)
513                         return;
514                 if (e->render.flags & RENDER_VIEWMODEL)
515                 {
516                         if (!r_drawviewmodel.integer || chase_active.integer || envmap)
517                                 return;
518                         if (cl.viewentity)
519                                 CL_LinkNetworkEntity(cl_entities + cl.viewentity);
520                         matrix = &viewmodelmatrix;
521                         if (e == &cl.viewent && cl.viewentity >= 0 && cl.viewentity < MAX_EDICTS && cl_entities[cl.viewentity].state_current.active)
522                         {
523                                 e->state_current.alpha = cl_entities[cl.viewentity].state_current.alpha;
524                                 e->state_current.effects = EF_NOSHADOW | (cl_entities[cl.viewentity].state_current.effects & (EF_ADDITIVE | EF_REFLECTIVE | EF_FULLBRIGHT));
525                         }
526                 }
527                 else
528                 {
529                         t = cl_entities + e->state_current.tagentity;
530                         if (!t->state_current.active)
531                                 return;
532                         // note: this can link to world
533                         CL_LinkNetworkEntity(t);
534                         // make relative to the entity
535                         matrix = &t->render.matrix;
536                         // if a valid tagindex is used, make it relative to that tag instead
537                         if (e->state_current.tagentity && e->state_current.tagindex >= 1 && (model = t->render.model) && e->state_current.tagindex <= t->render.model->alias.aliasnum_tags)
538                         {
539                                 // blend the matrices
540                                 memset(&blendmatrix, 0, sizeof(blendmatrix));
541                                 for (j = 0;j < 4 && t->render.frameblend[j].lerp > 0;j++)
542                                 {
543                                         matrix = &t->render.model->alias.aliasdata_tags[t->render.frameblend[j].frame * t->render.model->alias.aliasnum_tags + (e->state_current.tagindex - 1)].matrix;
544                                         d = t->render.frameblend[j].lerp;
545                                         for (l = 0;l < 4;l++)
546                                                 for (k = 0;k < 4;k++)
547                                                         blendmatrix.m[l][k] += d * matrix->m[l][k];
548                                 }
549                                 // concat the tag matrices onto the entity matrix
550                                 Matrix4x4_Concat(&tempmatrix, &t->render.matrix, &blendmatrix);
551                                 // use the constructed tag matrix
552                                 matrix = &tempmatrix;
553                         }
554                 }
555                 e->render.alpha = e->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
556                 e->render.scale = e->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
557                 e->render.flags = e->state_current.flags;
558                 if (e - cl_entities == cl.viewentity)
559                         e->render.flags |= RENDER_EXTERIORMODEL;
560                 e->render.effects = e->state_current.effects;
561                 if (e->state_current.flags & RENDER_COLORMAPPED)
562                         e->render.colormap = e->state_current.colormap;
563                 else if (cl.scores != NULL && e->state_current.colormap)
564                         e->render.colormap = cl.scores[e->state_current.colormap - 1].colors; // color it
565                 else
566                         e->render.colormap = -1; // no special coloring
567                 e->render.skinnum = e->state_current.skin;
568                 // set up the render matrix
569                 if (e->state_previous.active && e->state_current.modelindex == e->state_previous.modelindex)
570                 {
571                         // movement lerp
572                         if (e->persistent.lerpdeltatime > 0 && (lerp = (cl.time - e->persistent.lerpstarttime) / e->persistent.lerpdeltatime) < 1)
573                         {
574                                 // interpolate the origin and angles
575                                 VectorLerp(e->persistent.oldorigin, lerp, e->persistent.neworigin, origin);
576                                 VectorSubtract(e->persistent.newangles, e->persistent.oldangles, delta);
577                                 if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
578                                 if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
579                                 if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
580                                 VectorMA(e->persistent.oldangles, lerp, delta, angles);
581                         }
582                         else
583                         {
584                                 // no interpolation
585                                 VectorCopy(e->persistent.neworigin, origin);
586                                 VectorCopy(e->persistent.newangles, angles);
587                         }
588                         // animation lerp
589                         if (e->render.frame2 == e->state_current.frame)
590                         {
591                                 // update frame lerp fraction
592                                 e->render.framelerp = 1;
593                                 if (e->render.frame2time > e->render.frame1time)
594                                 {
595                                         e->render.framelerp = (cl.time - e->render.frame2time) / (e->render.frame2time - e->render.frame1time);
596                                         e->render.framelerp = bound(0, e->render.framelerp, 1);
597                                 }
598                         }
599                         else
600                         {
601                                 // begin a new frame lerp
602                                 e->render.frame1 = e->render.frame2;
603                                 e->render.frame1time = e->render.frame2time;
604                                 e->render.frame = e->render.frame2 = e->state_current.frame;
605                                 e->render.frame2time = cl.time;
606                                 e->render.framelerp = 0;
607                                 // make sure frame lerp won't last longer than 100ms
608                                 // (this mainly helps with models that use framegroups and
609                                 // switch between them infrequently)
610                                 e->render.frame1time = max(e->render.frame1time, e->render.frame2time - 0.1f);
611                         }
612                 }
613                 else
614                 {
615                         // no interpolation
616                         VectorCopy(e->persistent.neworigin, origin);
617                         VectorCopy(e->persistent.newangles, angles);
618                         e->render.frame = e->render.frame1 = e->render.frame2 = e->state_current.frame;
619                         e->render.frame1time = e->render.frame2time = cl.time;
620                         e->render.framelerp = 1;
621                 }
622
623                 e->render.model = cl.model_precache[e->state_current.modelindex];
624                 if (e->render.model)
625                 {
626                         Mod_CheckLoaded(e->render.model);
627                         // if model is alias or this is a tenebrae-like dlight, reverse pitch direction
628                         if (e->render.model->type == mod_alias || (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC))
629                                 angles[0] = -angles[0];
630                         if ((e->render.model->flags & EF_ROTATE) && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
631                         {
632                                 angles[1] = ANGLEMOD(100*cl.time);
633                                 if (cl_itembobheight.value)
634                                         origin[2] += (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
635                         }
636                 }
637
638                 R_LerpAnimation(&e->render);
639
640                 // FIXME: e->render.scale should go away
641                 Matrix4x4_CreateFromQuakeEntity(&matrix2, origin[0], origin[1], origin[2], angles[0], angles[1], angles[2], e->render.scale);
642                 // concat the matrices to make the entity relative to its tag
643                 Matrix4x4_Concat(&e->render.matrix, matrix, &matrix2);
644                 // make the other useful stuff
645                 Matrix4x4_Invert_Simple(&e->render.inversematrix, &e->render.matrix);
646                 CL_BoundingBoxForEntity(&e->render);
647
648                 // handle effects now that we know where this entity is in the world...
649                 origin[0] = e->render.matrix.m[0][3];
650                 origin[1] = e->render.matrix.m[1][3];
651                 origin[2] = e->render.matrix.m[2][3];
652                 trailtype = -1;
653                 dlightradius = 0;
654                 dlightcolor[0] = 0;
655                 dlightcolor[1] = 0;
656                 dlightcolor[2] = 0;
657                 // LordHavoc: if the entity has no effects, don't check each
658                 if (e->render.effects)
659                 {
660                         if (e->render.effects & EF_BRIGHTFIELD)
661                         {
662                                 if (gamemode == GAME_NEXUIZ)
663                                 {
664                                         dlightradius = max(dlightradius, 200);
665                                         dlightcolor[0] += 0.75f;
666                                         dlightcolor[1] += 1.50f;
667                                         dlightcolor[2] += 3.00f;
668                                         trailtype = 8;
669                                 }
670                                 else
671                                         CL_EntityParticles(e);
672                         }
673                         if (e->render.effects & EF_MUZZLEFLASH)
674                                 e->persistent.muzzleflash = 1.0f;
675                         if (e->render.effects & EF_DIMLIGHT)
676                         {
677                                 dlightradius = max(dlightradius, 200);
678                                 dlightcolor[0] += 1.50f;
679                                 dlightcolor[1] += 1.50f;
680                                 dlightcolor[2] += 1.50f;
681                         }
682                         if (e->render.effects & EF_BRIGHTLIGHT)
683                         {
684                                 dlightradius = max(dlightradius, 400);
685                                 dlightcolor[0] += 3.00f;
686                                 dlightcolor[1] += 3.00f;
687                                 dlightcolor[2] += 3.00f;
688                         }
689                         // LordHavoc: more effects
690                         if (e->render.effects & EF_RED) // red
691                         {
692                                 dlightradius = max(dlightradius, 200);
693                                 dlightcolor[0] += 1.50f;
694                                 dlightcolor[1] += 0.15f;
695                                 dlightcolor[2] += 0.15f;
696                         }
697                         if (e->render.effects & EF_BLUE) // blue
698                         {
699                                 dlightradius = max(dlightradius, 200);
700                                 dlightcolor[0] += 0.15f;
701                                 dlightcolor[1] += 0.15f;
702                                 dlightcolor[2] += 1.50f;
703                         }
704                         if (e->render.effects & EF_FLAME)
705                         {
706                                 mins[0] = origin[0] - 16.0f;
707                                 mins[1] = origin[1] - 16.0f;
708                                 mins[2] = origin[2] - 16.0f;
709                                 maxs[0] = origin[0] + 16.0f;
710                                 maxs[1] = origin[1] + 16.0f;
711                                 maxs[2] = origin[2] + 16.0f;
712                                 // how many flames to make
713                                 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
714                                 CL_FlameCube(mins, maxs, temp);
715                                 d = lhrandom(0.75f, 1);
716                                 dlightradius = max(dlightradius, 200);
717                                 dlightcolor[0] += d * 2.0f;
718                                 dlightcolor[1] += d * 1.5f;
719                                 dlightcolor[2] += d * 0.5f;
720                         }
721                         if (e->render.effects & EF_STARDUST)
722                         {
723                                 mins[0] = origin[0] - 16.0f;
724                                 mins[1] = origin[1] - 16.0f;
725                                 mins[2] = origin[2] - 16.0f;
726                                 maxs[0] = origin[0] + 16.0f;
727                                 maxs[1] = origin[1] + 16.0f;
728                                 maxs[2] = origin[2] + 16.0f;
729                                 // how many particles to make
730                                 temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
731                                 CL_Stardust(mins, maxs, temp);
732                                 dlightradius = max(dlightradius, 200);
733                                 dlightcolor[0] += 1.0f;
734                                 dlightcolor[1] += 0.7f;
735                                 dlightcolor[2] += 0.3f;
736                         }
737                 }
738                 // muzzleflash fades over time, and is offset a bit
739                 if (e->persistent.muzzleflash > 0)
740                 {
741                         Matrix4x4_Transform(&e->render.matrix, muzzleflashorigin, v2);
742                         CL_TraceLine(origin, v2, v, NULL, true, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_SKY);
743                         tempmatrix = e->render.matrix;
744                         tempmatrix.m[0][3] = v[0];
745                         tempmatrix.m[1][3] = v[1];
746                         tempmatrix.m[2][3] = v[2];
747                         CL_AllocDlight(NULL, &tempmatrix, 100, e->persistent.muzzleflash, e->persistent.muzzleflash, e->persistent.muzzleflash, 0, 0, 0, 0, true, 0);
748                         e->persistent.muzzleflash -= cl.frametime * 10;
749                 }
750                 // LordHavoc: if the model has no flags, don't check each
751                 if (e->render.model && e->render.model->flags && (!e->state_current.tagentity && !(e->render.flags & RENDER_VIEWMODEL)))
752                 {
753                         if (e->render.model->flags & EF_GIB)
754                                 trailtype = 2;
755                         else if (e->render.model->flags & EF_ZOMGIB)
756                                 trailtype = 4;
757                         else if (e->render.model->flags & EF_TRACER)
758                         {
759                                 trailtype = 3;
760                                 dlightradius = max(dlightradius, 100);
761                                 dlightcolor[0] += 0.25f;
762                                 dlightcolor[1] += 1.00f;
763                                 dlightcolor[2] += 0.25f;
764                         }
765                         else if (e->render.model->flags & EF_TRACER2)
766                         {
767                                 trailtype = 5;
768                                 dlightradius = max(dlightradius, 100);
769                                 dlightcolor[0] += 1.00f;
770                                 dlightcolor[1] += 0.60f;
771                                 dlightcolor[2] += 0.20f;
772                         }
773                         else if (e->render.model->flags & EF_ROCKET)
774                         {
775                                 trailtype = 0;
776                                 dlightradius = max(dlightradius, 200);
777                                 dlightcolor[0] += 3.00f;
778                                 dlightcolor[1] += 1.50f;
779                                 dlightcolor[2] += 0.50f;
780                         }
781                         else if (e->render.model->flags & EF_GRENADE)
782                         {
783                                 // LordHavoc: e->render.alpha == -1 is for Nehahra dem compatibility (cigar smoke)
784                                 trailtype = e->render.alpha == -1 ? 7 : 1;
785                         }
786                         else if (e->render.model->flags & EF_TRACER3)
787                         {
788                                 trailtype = 6;
789                                 if (gamemode == GAME_PRYDON)
790                                 {
791                                         dlightradius = max(dlightradius, 100);
792                                         dlightcolor[0] += 0.30f;
793                                         dlightcolor[1] += 0.60f;
794                                         dlightcolor[2] += 1.20f;
795                                 }
796                                 else
797                                 {
798                                         dlightradius = max(dlightradius, 200);
799                                         dlightcolor[0] += 1.20f;
800                                         dlightcolor[1] += 0.50f;
801                                         dlightcolor[2] += 1.00f;
802                                 }
803                         }
804                 }
805                 // LordHavoc: customizable glow
806                 if (e->state_current.glowsize)
807                 {
808                         // * 4 for the expansion from 0-255 to 0-1023 range,
809                         // / 255 to scale down byte colors
810                         dlightradius = max(dlightradius, e->state_current.glowsize * 4);
811                         VectorMA(dlightcolor, (1.0f / 255.0f), (qbyte *)&palette_complete[e->state_current.glowcolor], dlightcolor);
812                 }
813                 // make the glow dlight
814                 if (dlightradius > 0 && (dlightcolor[0] || dlightcolor[1] || dlightcolor[2]) && !(e->render.flags & RENDER_VIEWMODEL))
815                 {
816                         //dlightmatrix = e->render.matrix;
817                         // hack to make glowing player light shine on their gun
818                         //if ((e - cl_entities) == cl.viewentity/* && !chase_active.integer*/)
819                         //      dlightmatrix.m[2][3] += 30;
820                         CL_AllocDlight(&e->render, &e->render.matrix, dlightradius, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0, 0, 0, true, 1);
821                 }
822                 // custom rtlight
823                 if (e->state_current.lightpflags & PFLAGS_FULLDYNAMIC)
824                 {
825                         float light[4];
826                         VectorScale(e->state_current.light, (1.0f / 256.0f), light);
827                         light[3] = e->state_current.light[3];
828                         if (light[0] == 0 && light[1] == 0 && light[2] == 0)
829                                 VectorSet(light, 1, 1, 1);
830                         if (light[3] == 0)
831                                 light[3] = 350;
832                         CL_AllocDlight(&e->render, &e->render.matrix, light[3], light[0], light[1], light[2], 0, 0, e->state_current.skin, e->state_current.lightstyle, !(e->state_current.lightpflags & PFLAGS_NOSHADOW), (e->state_current.lightpflags & PFLAGS_CORONA) != 0);
833                 }
834                 // trails need the previous frame
835                 if (e->state_previous.active && e->state_previous.modelindex == e->state_current.modelindex)
836                 {
837                         if (e->render.flags & RENDER_GLOWTRAIL)
838                                 CL_RocketTrail2(e->persistent.trail_origin, origin, e->state_current.glowcolor, e);
839                         else if (trailtype >= 0)
840                                 CL_RocketTrail(e->persistent.trail_origin, origin, trailtype, e);
841                 }
842                 VectorCopy(origin, e->persistent.trail_origin);
843                 // note: the cl.viewentity and intermission check is to hide player
844                 // shadow during intermission and during the Nehahra movie and
845                 // Nehahra cinematics
846                 if (!(e->render.effects & (EF_NOSHADOW | EF_ADDITIVE))
847                  && (e->render.alpha == 1)
848                  && !(e->render.flags & RENDER_VIEWMODEL)
849                  && ((e - cl_entities) != cl.viewentity || (!cl.intermission && cl.protocol != PROTOCOL_NEHAHRAMOVIE && !cl_noplayershadow.integer)))
850                         e->render.flags |= RENDER_SHADOW;
851                 if (!(e->render.effects & EF_FULLBRIGHT))
852                         e->render.flags |= RENDER_LIGHT;
853                 // as soon as player is known we can call V_CalcRefDef
854                 if ((e - cl_entities) == cl.viewentity)
855                         V_CalcRefdef();
856                 if (e->render.model && e->render.model->name[0] == '*' && e->render.model->TraceBox)
857                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &e->render;
858                 if (gamemode == GAME_TENEBRAE && e->render.model && e->render.model->type == mod_sprite)
859                         e->render.effects |= EF_ADDITIVE;
860                 // don't show entities with no modelindex (note: this still shows
861                 // entities which have a modelindex that resolved to a NULL model)
862                 if (e->render.model && !(e->render.effects & EF_NODRAW) && r_refdef.numentities < r_refdef.maxentities)
863                         r_refdef.entities[r_refdef.numentities++] = &e->render;
864                 if (cl_num_entities < e->state_current.number + 1)
865                         cl_num_entities = e->state_current.number + 1;
866                 //if (cl.viewentity && e - cl_entities == cl.viewentity)
867                 //      Matrix4x4_Print(&e->render.matrix);
868         }
869 }
870
871 void CL_RelinkWorld(void)
872 {
873         entity_t *ent = &cl_entities[0];
874         if (cl_num_entities < 1)
875                 cl_num_entities = 1;
876         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
877         // FIXME: this should be done at load
878         Matrix4x4_CreateIdentity(&ent->render.matrix);
879         Matrix4x4_CreateIdentity(&ent->render.inversematrix);
880         CL_BoundingBoxForEntity(&ent->render);
881 }
882
883 static void CL_RelinkStaticEntities(void)
884 {
885         int i;
886         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
887         {
888                 Mod_CheckLoaded(cl_static_entities[i].render.model);
889                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
890         }
891 }
892
893 /*
894 ===============
895 CL_RelinkEntities
896 ===============
897 */
898 static void CL_RelinkNetworkEntities(void)
899 {
900         entity_t *ent;
901         int i;
902
903         ent = &cl.viewent;
904         ent->state_previous = ent->state_current;
905         ClearStateToDefault(&ent->state_current);
906         ent->state_current.time = cl.time;
907         ent->state_current.number = -1;
908         ent->state_current.active = true;
909         ent->state_current.modelindex = cl.stats[STAT_WEAPON];
910         ent->state_current.frame = cl.stats[STAT_WEAPONFRAME];
911         ent->state_current.flags = RENDER_VIEWMODEL;
912         if (cl.stats[STAT_HEALTH] <= 0 || cl.intermission)
913                 ent->state_current.modelindex = 0;
914         else if (cl.items & IT_INVISIBILITY)
915         {
916                 if (gamemode == GAME_TRANSFUSION)
917                         ent->state_current.alpha = 128;
918                 else
919                         ent->state_current.modelindex = 0;
920         }
921
922         // start on the entity after the world
923         entitylinkframenumber++;
924         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
925         {
926                 if (cl_entities_active[i])
927                 {
928                         if (ent->state_current.active)
929                                 CL_LinkNetworkEntity(ent);
930                         else
931                                 cl_entities_active[i] = false;
932                 }
933         }
934         CL_LinkNetworkEntity(&cl.viewent);
935 }
936
937 static void CL_RelinkEffects(void)
938 {
939         int i, intframe;
940         cl_effect_t *e;
941         entity_t *ent;
942         float frame;
943
944         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
945         {
946                 if (e->active)
947                 {
948                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
949                         intframe = frame;
950                         if (intframe < 0 || intframe >= e->endframe)
951                         {
952                                 memset(e, 0, sizeof(*e));
953                                 continue;
954                         }
955
956                         if (intframe != e->frame)
957                         {
958                                 e->frame = intframe;
959                                 e->frame1time = e->frame2time;
960                                 e->frame2time = cl.time;
961                         }
962
963                         // if we're drawing effects, get a new temp entity
964                         // (NewTempEntity adds it to the render entities list for us)
965                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
966                         {
967                                 // interpolation stuff
968                                 ent->render.frame1 = intframe;
969                                 ent->render.frame2 = intframe + 1;
970                                 if (ent->render.frame2 >= e->endframe)
971                                         ent->render.frame2 = -1; // disappear
972                                 ent->render.framelerp = frame - intframe;
973                                 ent->render.frame1time = e->frame1time;
974                                 ent->render.frame2time = e->frame2time;
975
976                                 // normal stuff
977                                 ent->render.model = cl.model_precache[e->modelindex];
978                                 ent->render.frame = ent->render.frame2;
979                                 ent->render.colormap = -1; // no special coloring
980                                 ent->render.alpha = 1;
981
982                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
983                                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
984                                 CL_BoundingBoxForEntity(&ent->render);
985                         }
986                 }
987         }
988 }
989
990 void CL_RelinkBeams(void)
991 {
992         int i;
993         beam_t *b;
994         vec3_t dist, org;
995         float d;
996         entity_t *ent;
997         float yaw, pitch;
998         float forward;
999         matrix4x4_t tempmatrix;
1000
1001         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1002         {
1003                 if (!b->model || b->endtime < cl.time)
1004                         continue;
1005
1006                 // if coming from the player, update the start position
1007                 //if (b->entity == cl.viewentity)
1008                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
1009                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
1010                 {
1011                         entity_state_t *p = &cl_entities[b->entity].state_previous;
1012                         //entity_state_t *c = &cl_entities[b->entity].state_current;
1013                         entity_render_t *r = &cl_entities[b->entity].render;
1014                         matrix4x4_t matrix, imatrix;
1015                         if (b->relativestartvalid == 2)
1016                         {
1017                                 // not really valid yet, we need to get the orientation now
1018                                 // (ParseBeam flagged this because it is received before
1019                                 //  entities are received, by now they have been received)
1020                                 // note: because players create lightning in their think
1021                                 // function (which occurs before movement), they actually
1022                                 // have some lag in it's location, so compare to the
1023                                 // previous player state, not the latest
1024                                 if (b->entity == cl.viewentity)
1025                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
1026                                 else
1027                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
1028                                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
1029                                 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
1030                                 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
1031                                 b->relativestartvalid = 1;
1032                         }
1033                         else
1034                         {
1035                                 if (b->entity == cl.viewentity)
1036                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
1037                                 else
1038                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
1039                                 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
1040                                 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
1041                         }
1042                 }
1043
1044                 if (b->lightning)
1045                 {
1046                         if (cl_beams_lightatend.integer)
1047                         {
1048                                 // FIXME: create a matrix from the beam start/end orientation
1049                                 Matrix4x4_CreateTranslate(&tempmatrix, b->end[0], b->end[1], b->end[2]);
1050                                 CL_AllocDlight (NULL, &tempmatrix, 200, 0.3, 0.7, 1, 0, 0, 0, 0, true, 1);
1051                         }
1052                         if (cl_beams_polygons.integer)
1053                                 continue;
1054                 }
1055
1056                 // calculate pitch and yaw
1057                 VectorSubtract (b->end, b->start, dist);
1058
1059                 if (dist[1] == 0 && dist[0] == 0)
1060                 {
1061                         yaw = 0;
1062                         if (dist[2] > 0)
1063                                 pitch = 90;
1064                         else
1065                                 pitch = 270;
1066                 }
1067                 else
1068                 {
1069                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
1070                         if (yaw < 0)
1071                                 yaw += 360;
1072
1073                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
1074                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
1075                         if (pitch < 0)
1076                                 pitch += 360;
1077                 }
1078
1079                 // add new entities for the lightning
1080                 VectorCopy (b->start, org);
1081                 d = VectorNormalizeLength(dist);
1082                 while (d > 0)
1083                 {
1084                         ent = CL_NewTempEntity ();
1085                         if (!ent)
1086                                 return;
1087                         //VectorCopy (org, ent->render.origin);
1088                         ent->render.model = b->model;
1089                         ent->render.effects = EF_FULLBRIGHT;
1090                         //ent->render.angles[0] = pitch;
1091                         //ent->render.angles[1] = yaw;
1092                         //ent->render.angles[2] = rand()%360;
1093                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1094                         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1095                         CL_BoundingBoxForEntity(&ent->render);
1096                         VectorMA(org, 30, dist, org);
1097                         d -= 30;
1098                 }
1099         }
1100 }
1101
1102 void CL_LerpPlayer(float frac)
1103 {
1104         int i;
1105         float d;
1106
1107         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1108
1109         for (i = 0;i < 3;i++)
1110                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1111
1112         if (cls.demoplayback)
1113         {
1114                 // interpolate the angles
1115                 for (i = 0;i < 3;i++)
1116                 {
1117                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1118                         if (d > 180)
1119                                 d -= 360;
1120                         else if (d < -180)
1121                                 d += 360;
1122                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1123                 }
1124         }
1125 }
1126
1127 /*
1128 ===============
1129 CL_ReadFromServer
1130
1131 Read all incoming data from the server
1132 ===============
1133 */
1134 int CL_ReadFromServer(void)
1135 {
1136         CL_ReadDemoMessage();
1137
1138         r_refdef.numentities = 0;
1139         cl_num_entities = 0;
1140         cl_num_brushmodel_entities = 0;
1141
1142         if (cls.state == ca_connected && cls.signon == SIGNONS)
1143         {
1144                 // prepare for a new frame
1145                 CL_LerpPlayer(CL_LerpPoint());
1146                 CL_DecayLights();
1147                 CL_ClearTempEntities();
1148                 V_DriftPitch();
1149                 V_FadeViewFlashs();
1150
1151                 // relink network entities (note: this sets up the view!)
1152                 CL_RelinkNetworkEntities();
1153
1154                 // move particles
1155                 CL_MoveParticles();
1156                 R_MoveExplosions();
1157
1158                 // link stuff
1159                 CL_RelinkWorld();
1160                 CL_RelinkStaticEntities();
1161                 CL_RelinkBeams();
1162                 CL_RelinkEffects();
1163
1164                 // run cgame code (which can add more entities)
1165                 CL_CGVM_Frame();
1166
1167                 // update view blend
1168                 V_CalcViewBlend();
1169         }
1170
1171         return 0;
1172 }
1173
1174 /*
1175 =================
1176 CL_SendCmd
1177 =================
1178 */
1179 void CL_SendCmd(usercmd_t *cmd)
1180 {
1181         if (cls.signon == SIGNONS)
1182                 CL_SendMove(cmd);
1183
1184         if (cls.demoplayback)
1185         {
1186                 SZ_Clear(&cls.message);
1187                 return;
1188         }
1189
1190         // send the reliable message (forwarded commands) if there is one
1191         if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1192         {
1193                 if (developer.integer)
1194                 {
1195                         Con_Print("CL_SendCmd: sending reliable message:\n");
1196                         SZ_HexDumpToConsole(&cls.message);
1197                 }
1198                 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1199                         Host_Error("CL_WriteToServer: lost server connection");
1200                 SZ_Clear(&cls.message);
1201         }
1202 }
1203
1204 // LordHavoc: pausedemo command
1205 static void CL_PauseDemo_f (void)
1206 {
1207         cls.demopaused = !cls.demopaused;
1208         if (cls.demopaused)
1209                 Con_Print("Demo paused\n");
1210         else
1211                 Con_Print("Demo unpaused\n");
1212 }
1213
1214 /*
1215 ======================
1216 CL_Fog_f
1217 ======================
1218 */
1219 static void CL_Fog_f (void)
1220 {
1221         if (Cmd_Argc () == 1)
1222         {
1223                 Con_Printf("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1224                 return;
1225         }
1226         fog_density = atof(Cmd_Argv(1));
1227         fog_red = atof(Cmd_Argv(2));
1228         fog_green = atof(Cmd_Argv(3));
1229         fog_blue = atof(Cmd_Argv(4));
1230 }
1231
1232 /*
1233 =================
1234 CL_Init
1235 =================
1236 */
1237 void CL_Init (void)
1238 {
1239         cl_entities_mempool = Mem_AllocPool("client entities");
1240         cl_refdef_mempool = Mem_AllocPool("refdef");
1241
1242         memset(&r_refdef, 0, sizeof(r_refdef));
1243         // max entities sent to renderer per frame
1244         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1245         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1246         // 256k drawqueue buffer
1247         r_refdef.maxdrawqueuesize = 256 * 1024;
1248         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1249
1250         SZ_Alloc (&cls.message, 1024, "cls.message");
1251
1252         CL_InitInput ();
1253         CL_InitTEnts ();
1254
1255 //
1256 // register our commands
1257 //
1258         Cvar_RegisterVariable (&cl_upspeed);
1259         Cvar_RegisterVariable (&cl_forwardspeed);
1260         Cvar_RegisterVariable (&cl_backspeed);
1261         Cvar_RegisterVariable (&cl_sidespeed);
1262         Cvar_RegisterVariable (&cl_movespeedkey);
1263         Cvar_RegisterVariable (&cl_yawspeed);
1264         Cvar_RegisterVariable (&cl_pitchspeed);
1265         Cvar_RegisterVariable (&cl_anglespeedkey);
1266         Cvar_RegisterVariable (&cl_shownet);
1267         Cvar_RegisterVariable (&cl_nolerp);
1268         Cvar_RegisterVariable (&lookspring);
1269         Cvar_RegisterVariable (&lookstrafe);
1270         Cvar_RegisterVariable (&sensitivity);
1271         Cvar_RegisterVariable (&freelook);
1272
1273         Cvar_RegisterVariable (&m_pitch);
1274         Cvar_RegisterVariable (&m_yaw);
1275         Cvar_RegisterVariable (&m_forward);
1276         Cvar_RegisterVariable (&m_side);
1277
1278         Cvar_RegisterVariable (&cl_itembobspeed);
1279         Cvar_RegisterVariable (&cl_itembobheight);
1280
1281         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1282         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1283         Cmd_AddCommand ("record", CL_Record_f);
1284         Cmd_AddCommand ("stop", CL_Stop_f);
1285         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1286         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1287
1288         Cmd_AddCommand ("fog", CL_Fog_f);
1289
1290         // LordHavoc: added pausedemo
1291         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1292
1293         Cvar_RegisterVariable(&r_draweffects);
1294         Cvar_RegisterVariable(&cl_explosions);
1295         Cvar_RegisterVariable(&cl_explosions_alpha_start);
1296         Cvar_RegisterVariable(&cl_explosions_alpha_end);
1297         Cvar_RegisterVariable(&cl_explosions_size_start);
1298         Cvar_RegisterVariable(&cl_explosions_size_end);
1299         Cvar_RegisterVariable(&cl_explosions_lifetime);
1300         Cvar_RegisterVariable(&cl_stainmaps);
1301         Cvar_RegisterVariable(&cl_beams_polygons);
1302         Cvar_RegisterVariable(&cl_beams_relative);
1303         Cvar_RegisterVariable(&cl_beams_lightatend);
1304         Cvar_RegisterVariable(&cl_noplayershadow);
1305
1306         CL_Parse_Init();
1307         CL_Particles_Init();
1308         CL_Screen_Init();
1309         CL_CGVM_Init();
1310
1311         CL_Video_Init();
1312 }
1313