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