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