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