]> icculus.org git repositories - divverent/darkplaces.git/blob - cl_main.c
fix a bunch of things that should have been checking if the model (usually cl.worldmo...
[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 = {CVAR_SAVE, "cl_explosions", "1"};
50 cvar_t cl_stainmaps = {CVAR_SAVE, "cl_stainmaps", "1"};
51
52 cvar_t cl_beams_polygons = {CVAR_SAVE, "cl_beams_polygons", "1"};
53 cvar_t cl_beams_relative = {CVAR_SAVE, "cl_beams_relative", "1"};
54 cvar_t cl_beams_lightatend = {CVAR_SAVE, "cl_beams_lightatend", "0"};
55
56 cvar_t cl_noplayershadow = {CVAR_SAVE, "cl_noplayershadow", "0"};
57
58 mempool_t *cl_refdef_mempool;
59 mempool_t *cl_entities_mempool;
60
61 client_static_t cls;
62 client_state_t  cl;
63
64 int cl_max_entities;
65 int cl_max_static_entities;
66 int cl_max_temp_entities;
67 int cl_max_effects;
68 int cl_max_beams;
69 int cl_max_dlights;
70 int cl_max_lightstyle;
71 int cl_max_brushmodel_entities;
72
73 entity_t *cl_entities;
74 qbyte *cl_entities_active;
75 entity_t *cl_static_entities;
76 entity_t *cl_temp_entities;
77 cl_effect_t *cl_effects;
78 beam_t *cl_beams;
79 dlight_t *cl_dlights;
80 lightstyle_t *cl_lightstyle;
81 entity_render_t **cl_brushmodel_entities;
82
83 int cl_num_entities;
84 int cl_num_static_entities;
85 int cl_num_temp_entities;
86 int cl_num_brushmodel_entities;
87
88 /*
89 =====================
90 CL_ClearState
91
92 =====================
93 */
94 void CL_ClearState (void)
95 {
96         int i;
97
98         if (!sv.active)
99                 Host_ClearMemory ();
100
101         Mem_EmptyPool(cl_entities_mempool);
102
103 // wipe the entire cl structure
104         memset (&cl, 0, sizeof(cl));
105
106         SZ_Clear (&cls.message);
107
108         cl_num_entities = 0;
109         cl_num_static_entities = 0;
110         cl_num_temp_entities = 0;
111         cl_num_brushmodel_entities = 0;
112
113         // tweak these if the game runs out
114         cl_max_entities = MAX_EDICTS;
115         cl_max_static_entities = 256;
116         cl_max_temp_entities = 512;
117         cl_max_effects = 256;
118         cl_max_beams = 24;
119         cl_max_dlights = MAX_DLIGHTS;
120         cl_max_lightstyle = MAX_LIGHTSTYLES;
121         cl_max_brushmodel_entities = MAX_EDICTS;
122
123         cl_entities = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(entity_t));
124         cl_entities_active = Mem_Alloc(cl_entities_mempool, cl_max_entities * sizeof(qbyte));
125         cl_static_entities = Mem_Alloc(cl_entities_mempool, cl_max_static_entities * sizeof(entity_t));
126         cl_temp_entities = Mem_Alloc(cl_entities_mempool, cl_max_temp_entities * sizeof(entity_t));
127         cl_effects = Mem_Alloc(cl_entities_mempool, cl_max_effects * sizeof(cl_effect_t));
128         cl_beams = Mem_Alloc(cl_entities_mempool, cl_max_beams * sizeof(beam_t));
129         cl_dlights = Mem_Alloc(cl_entities_mempool, cl_max_dlights * sizeof(dlight_t));
130         cl_lightstyle = Mem_Alloc(cl_entities_mempool, cl_max_lightstyle * sizeof(lightstyle_t));
131         cl_brushmodel_entities = Mem_Alloc(cl_entities_mempool, cl_max_brushmodel_entities * sizeof(entity_render_t *));
132
133         CL_Screen_NewMap();
134
135         CL_Particles_Clear();
136
137         // LordHavoc: have to set up the baseline info for alpha and other stuff
138         for (i = 0;i < cl_max_entities;i++)
139         {
140                 ClearStateToDefault(&cl_entities[i].state_baseline);
141                 ClearStateToDefault(&cl_entities[i].state_previous);
142                 ClearStateToDefault(&cl_entities[i].state_current);
143         }
144
145         CL_CGVM_Clear();
146 }
147
148 /*
149 =====================
150 CL_Disconnect
151
152 Sends a disconnect message to the server
153 This is also called on Host_Error, so it shouldn't cause any errors
154 =====================
155 */
156 void CL_Disconnect(void)
157 {
158         if (cls.state == ca_dedicated)
159                 return;
160
161 // stop sounds (especially looping!)
162         S_StopAllSounds (true);
163
164         // clear contents blends
165         cl.cshifts[0].percent = 0;
166         cl.cshifts[1].percent = 0;
167         cl.cshifts[2].percent = 0;
168         cl.cshifts[3].percent = 0;
169
170         cl.worldmodel = NULL;
171
172         if (cls.demoplayback)
173                 CL_StopPlayback();
174         else if (cls.netcon)
175         {
176                 if (cls.demorecording)
177                         CL_Stop_f();
178
179                 Con_DPrintf("Sending clc_disconnect\n");
180                 SZ_Clear(&cls.message);
181                 MSG_WriteByte(&cls.message, clc_disconnect);
182                 NetConn_SendUnreliableMessage(cls.netcon, &cls.message);
183                 SZ_Clear(&cls.message);
184                 NetConn_Close(cls.netcon);
185                 cls.netcon = NULL;
186                 // if running a local server, shut it down
187                 if (sv.active)
188                 {
189                         // prevent this code from executing again during Host_ShutdownServer
190                         cls.state = ca_disconnected;
191                         Host_ShutdownServer(false);
192                 }
193         }
194         cls.state = ca_disconnected;
195
196         cls.demoplayback = cls.timedemo = false;
197         cls.signon = 0;
198 }
199
200 void CL_Disconnect_f (void)
201 {
202         CL_Disconnect ();
203         if (sv.active)
204                 Host_ShutdownServer (false);
205 }
206
207
208
209
210 /*
211 =====================
212 CL_EstablishConnection
213
214 Host should be either "local" or a net address
215 =====================
216 */
217 void CL_EstablishConnection(const char *host)
218 {
219         if (cls.state == ca_dedicated)
220                 return;
221
222         // clear menu's connect error message
223         m_return_reason[0] = 0;
224
225         // stop demo loop in case this fails
226         cls.demonum = -1;
227         CL_Disconnect();
228
229         if (LHNETADDRESS_FromString(&cls.connect_address, host, 26000) && (cls.connect_mysocket = NetConn_ChooseClientSocketForAddress(&cls.connect_address)))
230         {
231                 cls.connect_trying = true;
232                 cls.connect_remainingtries = 3;
233                 cls.connect_nextsendtime = 0;
234                 if (sv.active)
235                 {
236                         NetConn_ClientFrame();
237                         NetConn_ServerFrame();
238                         NetConn_ClientFrame();
239                         NetConn_ServerFrame();
240                         NetConn_ClientFrame();
241                         NetConn_ServerFrame();
242                         NetConn_ClientFrame();
243                         NetConn_ServerFrame();
244                 }
245         }
246 }
247
248 /*
249 ==============
250 CL_PrintEntities_f
251 ==============
252 */
253 static void CL_PrintEntities_f (void)
254 {
255         entity_t *ent;
256         int i, j;
257         char name[32];
258
259         for (i = 0, ent = cl_entities;i < cl_num_entities;i++, ent++)
260         {
261                 if (!ent->state_current.active)
262                         continue;
263
264                 if (ent->render.model)
265                         strncpy(name, ent->render.model->name, 25);
266                 else
267                         strcpy(name, "--no model--");
268                 name[25] = 0;
269                 for (j = strlen(name);j < 25;j++)
270                         name[j] = ' ';
271                 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);
272         }
273 }
274
275 //static const vec3_t nomodelmins = {-16, -16, -16};
276 //static const vec3_t nomodelmaxs = {16, 16, 16};
277 void CL_BoundingBoxForEntity(entity_render_t *ent)
278 {
279         if (ent->model)
280         {
281                 //if (ent->angles[0] || ent->angles[2])
282                 if (ent->matrix.m[2][0] != 0 || ent->matrix.m[2][1] != 0)
283                 {
284                         // pitch or roll
285                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->rotatedmins[0];
286                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->rotatedmins[1];
287                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->rotatedmins[2];
288                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->rotatedmaxs[0];
289                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->rotatedmaxs[1];
290                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->rotatedmaxs[2];
291                         //VectorAdd(ent->origin, ent->model->rotatedmins, ent->mins);
292                         //VectorAdd(ent->origin, ent->model->rotatedmaxs, ent->maxs);
293                 }
294                 //else if (ent->angles[1])
295                 else if (ent->matrix.m[0][1] != 0 || ent->matrix.m[1][0] != 0)
296                 {
297                         // yaw
298                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->yawmins[0];
299                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->yawmins[1];
300                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->yawmins[2];
301                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->yawmaxs[0];
302                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->yawmaxs[1];
303                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->yawmaxs[2];
304                         //VectorAdd(ent->origin, ent->model->yawmins, ent->mins);
305                         //VectorAdd(ent->origin, ent->model->yawmaxs, ent->maxs);
306                 }
307                 else
308                 {
309                         ent->mins[0] = ent->matrix.m[0][3] + ent->model->normalmins[0];
310                         ent->mins[1] = ent->matrix.m[1][3] + ent->model->normalmins[1];
311                         ent->mins[2] = ent->matrix.m[2][3] + ent->model->normalmins[2];
312                         ent->maxs[0] = ent->matrix.m[0][3] + ent->model->normalmaxs[0];
313                         ent->maxs[1] = ent->matrix.m[1][3] + ent->model->normalmaxs[1];
314                         ent->maxs[2] = ent->matrix.m[2][3] + ent->model->normalmaxs[2];
315                         //VectorAdd(ent->origin, ent->model->normalmins, ent->mins);
316                         //VectorAdd(ent->origin, ent->model->normalmaxs, ent->maxs);
317                 }
318         }
319         else
320         {
321                 ent->mins[0] = ent->matrix.m[0][3] - 16;
322                 ent->mins[1] = ent->matrix.m[1][3] - 16;
323                 ent->mins[2] = ent->matrix.m[2][3] - 16;
324                 ent->maxs[0] = ent->matrix.m[0][3] + 16;
325                 ent->maxs[1] = ent->matrix.m[1][3] + 16;
326                 ent->maxs[2] = ent->matrix.m[2][3] + 16;
327                 //VectorAdd(ent->origin, nomodelmins, ent->mins);
328                 //VectorAdd(ent->origin, nomodelmaxs, ent->maxs);
329         }
330 }
331
332 void CL_LerpUpdate(entity_t *e)
333 {
334         entity_persistent_t *p;
335         entity_render_t *r;
336         p = &e->persistent;
337         r = &e->render;
338
339         if (p->modelindex != e->state_current.modelindex)
340         {
341                 // reset all interpolation information
342                 p->modelindex = e->state_current.modelindex;
343                 p->frame1 = p->frame2 = e->state_current.frame;
344                 p->frame1time = p->frame2time = cl.time;
345                 p->framelerp = 1;
346         }
347         else if (p->frame2 != e->state_current.frame)
348         {
349                 // transition to new frame
350                 p->frame1 = p->frame2;
351                 p->frame1time = p->frame2time;
352                 p->frame2 = e->state_current.frame;
353                 p->frame2time = cl.time;
354                 p->framelerp = 0;
355         }
356         else
357         {
358                 // update transition
359                 p->framelerp = (cl.time - p->frame2time) * 10;
360                 p->framelerp = bound(0, p->framelerp, 1);
361         }
362
363         r->model = cl.model_precache[e->state_current.modelindex];
364         Mod_CheckLoaded(r->model);
365         r->frame = e->state_current.frame;
366         r->frame1 = p->frame1;
367         r->frame2 = p->frame2;
368         r->framelerp = p->framelerp;
369         r->frame1time = p->frame1time;
370         r->frame2time = p->frame2time;
371 }
372
373 /*
374 ===============
375 CL_LerpPoint
376
377 Determines the fraction between the last two messages that the objects
378 should be put at.
379 ===============
380 */
381 static float CL_LerpPoint (void)
382 {
383         float f;
384
385         // dropped packet, or start of demo
386         if (cl.mtime[1] < cl.mtime[0] - 0.1)
387                 cl.mtime[1] = cl.mtime[0] - 0.1;
388
389         cl.time = bound(cl.mtime[1], cl.time, cl.mtime[0]);
390
391         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
392         f = cl.mtime[0] - cl.mtime[1];
393         if (!f || cl_nolerp.integer || cls.timedemo || (sv.active && svs.maxclients == 1))
394         {
395                 cl.time = cl.mtime[0];
396                 return 1;
397         }
398
399         f = (cl.time - cl.mtime[1]) / f;
400         return bound(0, f, 1);
401 }
402
403 void CL_ClearTempEntities (void)
404 {
405         cl_num_temp_entities = 0;
406 }
407
408 entity_t *CL_NewTempEntity (void)
409 {
410         entity_t *ent;
411
412         if (r_refdef.numentities >= r_refdef.maxentities)
413                 return NULL;
414         if (cl_num_temp_entities >= cl_max_temp_entities)
415                 return NULL;
416         ent = &cl_temp_entities[cl_num_temp_entities++];
417         memset (ent, 0, sizeof(*ent));
418         r_refdef.entities[r_refdef.numentities++] = &ent->render;
419
420         ent->render.colormap = -1; // no special coloring
421         ent->render.scale = 1;
422         ent->render.alpha = 1;
423         return ent;
424 }
425
426 void CL_AllocDlight (entity_render_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
427 {
428         int i;
429         dlight_t *dl;
430
431         /*
432 // first look for an exact key match
433         if (ent)
434         {
435                 dl = cl_dlights;
436                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
437                         if (dl->ent == ent)
438                                 goto dlightsetup;
439         }
440         */
441
442 // then look for anything else
443         dl = cl_dlights;
444         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
445                 if (!dl->radius)
446                         goto dlightsetup;
447
448         // unable to find one
449         return;
450
451 dlightsetup:
452         //Con_Printf("dlight %i : %f %f %f : %f %f %f\n", i, org[0], org[1], org[2], red * radius, green * radius, blue * radius);
453         memset (dl, 0, sizeof(*dl));
454         dl->ent = ent;
455         if (cl.worldmodel)
456                 cl.worldmodel->FindNonSolidLocation(cl.worldmodel, org, dl->origin, 6);
457         //VectorCopy(org, dl->origin);
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 }
468
469 void CL_DecayLights (void)
470 {
471         int i;
472         dlight_t *dl;
473         float time;
474
475         time = cl.time - cl.oldtime;
476
477         dl = cl_dlights;
478         for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
479         {
480                 if (!dl->radius)
481                         continue;
482                 if (dl->die < cl.time)
483                 {
484                         dl->radius = 0;
485                         continue;
486                 }
487
488                 dl->radius -= time*dl->decay;
489                 if (dl->radius < 0)
490                         dl->radius = 0;
491         }
492 }
493
494 void CL_RelinkWorld (void)
495 {
496         entity_t *ent = &cl_entities[0];
497         if (cl_num_entities < 1)
498                 cl_num_entities = 1;
499         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
500         Matrix4x4_CreateIdentity(&ent->render.matrix);
501         Matrix4x4_CreateIdentity(&ent->render.inversematrix);
502         CL_BoundingBoxForEntity(&ent->render);
503 }
504
505 static void CL_RelinkStaticEntities(void)
506 {
507         int i;
508         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
509         {
510                 Mod_CheckLoaded(cl_static_entities[i].render.model);
511                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
512         }
513 }
514
515 /*
516 ===============
517 CL_RelinkEntities
518 ===============
519 */
520 extern qboolean Nehahrademcompatibility;
521 #define MAXVIEWMODELS 32
522 entity_t *viewmodels[MAXVIEWMODELS];
523 int numviewmodels;
524 static void CL_RelinkNetworkEntities(void)
525 {
526         entity_t *ent;
527         int i, effects, temp;
528         float d, bobjrotate, bobjoffset, lerp;
529         vec3_t oldorg, neworg, delta, dlightcolor, v, v2, mins, maxs;
530
531         numviewmodels = 0;
532
533         bobjrotate = ANGLEMOD(100*cl.time);
534         if (cl_itembobheight.value)
535                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
536         else
537                 bobjoffset = 0;
538
539         // start on the entity after the world
540         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
541         {
542                 // if the object isn't active in the current network frame, skip it
543                 if (!cl_entities_active[i])
544                         continue;
545                 if (!ent->state_current.active)
546                 {
547                         cl_entities_active[i] = false;
548                         continue;
549                 }
550
551                 VectorCopy(ent->persistent.trail_origin, oldorg);
552
553                 if (!ent->state_previous.active)
554                 {
555                         // only one state available
556                         VectorCopy (ent->persistent.neworigin, neworg);
557                         VectorCopy (ent->persistent.newangles, ent->render.angles);
558                         VectorCopy (neworg, oldorg);
559                 }
560                 else
561                 {
562                         // if the delta is large, assume a teleport and don't lerp
563                         VectorSubtract(ent->persistent.neworigin, ent->persistent.oldorigin, delta);
564                         if (ent->persistent.lerpdeltatime > 0)
565                         {
566                                 lerp = (cl.time - ent->persistent.lerpstarttime) / ent->persistent.lerpdeltatime;
567                                 if (lerp < 1)
568                                 {
569                                         // interpolate the origin and angles
570                                         VectorMA(ent->persistent.oldorigin, lerp, delta, neworg);
571                                         VectorSubtract(ent->persistent.newangles, ent->persistent.oldangles, delta);
572                                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
573                                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
574                                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
575                                         VectorMA(ent->persistent.oldangles, lerp, delta, ent->render.angles);
576                                 }
577                                 else
578                                 {
579                                         // no interpolation
580                                         VectorCopy (ent->persistent.neworigin, neworg);
581                                         VectorCopy (ent->persistent.newangles, ent->render.angles);
582                                 }
583                         }
584                         else
585                         {
586                                 // no interpolation
587                                 VectorCopy (ent->persistent.neworigin, neworg);
588                                 VectorCopy (ent->persistent.newangles, ent->render.angles);
589                         }
590                 }
591
592                 if (!ent->render.model || ent->render.model->type != mod_brush)
593                         ent->render.angles[0] = -ent->render.angles[0];
594
595                 VectorCopy (neworg, ent->persistent.trail_origin);
596                 // persistent.modelindex will be updated by CL_LerpUpdate
597                 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
598                         VectorCopy(neworg, oldorg);
599
600                 VectorCopy (neworg, ent->render.origin);
601                 ent->render.flags = ent->state_current.flags;
602                 if (i == cl.viewentity)
603                         ent->render.flags |= RENDER_EXTERIORMODEL;
604                 ent->render.effects = effects = ent->state_current.effects;
605                 if (ent->state_current.flags & RENDER_COLORMAPPED)
606                         ent->render.colormap = ent->state_current.colormap;
607                 else if (cl.scores == NULL || !ent->state_current.colormap)
608                         ent->render.colormap = -1; // no special coloring
609                 else
610                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
611                 ent->render.skinnum = ent->state_current.skin;
612                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
613                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
614
615                 if (ent->render.model && ent->render.model->flags & EF_ROTATE)
616                 {
617                         ent->render.angles[1] = bobjrotate;
618                         ent->render.origin[2] += bobjoffset;
619                 }
620
621                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, ent->render.origin[0], ent->render.origin[1], ent->render.origin[2], ent->render.angles[0], ent->render.angles[1], ent->render.angles[2], ent->render.scale);
622
623                 // update interpolation info
624                 CL_LerpUpdate(ent);
625
626                 // handle effects now...
627                 dlightcolor[0] = 0;
628                 dlightcolor[1] = 0;
629                 dlightcolor[2] = 0;
630
631                 // LordHavoc: if the entity has no effects, don't check each
632                 if (effects)
633                 {
634                         if (effects & EF_BRIGHTFIELD)
635                         {
636                                 if (gamemode == GAME_NEXUIZ)
637                                 {
638                                         dlightcolor[0] += 100.0f;
639                                         dlightcolor[1] += 200.0f;
640                                         dlightcolor[2] += 400.0f;
641                                         // don't do trail if we have no previous location
642                                         if (ent->state_previous.active)
643                                                 CL_RocketTrail (oldorg, neworg, 8, ent);
644                                 }
645                                 else
646                                         CL_EntityParticles (ent);
647                         }
648                         if (effects & EF_MUZZLEFLASH)
649                                 ent->persistent.muzzleflash = 100.0f;
650                         if (effects & EF_DIMLIGHT)
651                         {
652                                 dlightcolor[0] += 200.0f;
653                                 dlightcolor[1] += 200.0f;
654                                 dlightcolor[2] += 200.0f;
655                         }
656                         if (effects & EF_BRIGHTLIGHT)
657                         {
658                                 dlightcolor[0] += 400.0f;
659                                 dlightcolor[1] += 400.0f;
660                                 dlightcolor[2] += 400.0f;
661                         }
662                         // LordHavoc: added EF_RED and EF_BLUE
663                         if (effects & EF_RED) // red
664                         {
665                                 dlightcolor[0] += 200.0f;
666                                 dlightcolor[1] +=  20.0f;
667                                 dlightcolor[2] +=  20.0f;
668                         }
669                         if (effects & EF_BLUE) // blue
670                         {
671                                 dlightcolor[0] +=  20.0f;
672                                 dlightcolor[1] +=  20.0f;
673                                 dlightcolor[2] += 200.0f;
674                         }
675                         if (effects & EF_FLAME)
676                         {
677                                 if (ent->render.model)
678                                 {
679                                         mins[0] = neworg[0] - 16.0f;
680                                         mins[1] = neworg[1] - 16.0f;
681                                         mins[2] = neworg[2] - 16.0f;
682                                         maxs[0] = neworg[0] + 16.0f;
683                                         maxs[1] = neworg[1] + 16.0f;
684                                         maxs[2] = neworg[2] + 16.0f;
685                                         // how many flames to make
686                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
687                                         CL_FlameCube(mins, maxs, temp);
688                                 }
689                                 d = lhrandom(200, 250);
690                                 dlightcolor[0] += d * 1.0f;
691                                 dlightcolor[1] += d * 0.7f;
692                                 dlightcolor[2] += d * 0.3f;
693                         }
694                         if (effects & EF_STARDUST)
695                         {
696                                 if (ent->render.model)
697                                 {
698                                         mins[0] = neworg[0] - 16.0f;
699                                         mins[1] = neworg[1] - 16.0f;
700                                         mins[2] = neworg[2] - 16.0f;
701                                         maxs[0] = neworg[0] + 16.0f;
702                                         maxs[1] = neworg[1] + 16.0f;
703                                         maxs[2] = neworg[2] + 16.0f;
704                                         // how many particles to make
705                                         temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
706                                         CL_Stardust(mins, maxs, temp);
707                                 }
708                                 d = 100;
709                                 dlightcolor[0] += d * 1.0f;
710                                 dlightcolor[1] += d * 0.7f;
711                                 dlightcolor[2] += d * 0.3f;
712                         }
713                 }
714
715                 if (ent->persistent.muzzleflash > 0)
716                 {
717                         v2[0] = ent->render.matrix.m[0][0] * 18 + neworg[0];
718                         v2[1] = ent->render.matrix.m[0][1] * 18 + neworg[1];
719                         v2[2] = ent->render.matrix.m[0][2] * 18 + neworg[2] + 16;
720                         CL_TraceLine(neworg, v2, v, NULL, 0, true, NULL);
721
722                         CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
723                         ent->persistent.muzzleflash -= cl.frametime * 1000;
724                 }
725
726                 // LordHavoc: if the model has no flags, don't check each
727                 if (ent->render.model && ent->render.model->flags)
728                 {
729                         // note: EF_ROTATE handled above, above matrix calculation
730                         // only do trails if present in the previous frame as well
731                         if (ent->state_previous.active)
732                         {
733                                 if (ent->render.model->flags & EF_GIB)
734                                         CL_RocketTrail (oldorg, neworg, 2, ent);
735                                 else if (ent->render.model->flags & EF_ZOMGIB)
736                                         CL_RocketTrail (oldorg, neworg, 4, ent);
737                                 else if (ent->render.model->flags & EF_TRACER)
738                                 {
739                                         CL_RocketTrail (oldorg, neworg, 3, ent);
740                                         dlightcolor[0] += 0x10;
741                                         dlightcolor[1] += 0x40;
742                                         dlightcolor[2] += 0x10;
743                                 }
744                                 else if (ent->render.model->flags & EF_TRACER2)
745                                 {
746                                         CL_RocketTrail (oldorg, neworg, 5, ent);
747                                         dlightcolor[0] += 0x50;
748                                         dlightcolor[1] += 0x30;
749                                         dlightcolor[2] += 0x10;
750                                 }
751                                 else if (ent->render.model->flags & EF_ROCKET)
752                                 {
753                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
754                                         dlightcolor[0] += 200.0f;
755                                         dlightcolor[1] += 160.0f;
756                                         dlightcolor[2] +=  80.0f;
757                                 }
758                                 else if (ent->render.model->flags & EF_GRENADE)
759                                 {
760                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility (cigar smoke)
761                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
762                                         else
763                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
764                                 }
765                                 else if (ent->render.model->flags & EF_TRACER3)
766                                 {
767                                         CL_RocketTrail (oldorg, neworg, 6, ent);
768                                         dlightcolor[0] += 0x50;
769                                         dlightcolor[1] += 0x20;
770                                         dlightcolor[2] += 0x40;
771                                 }
772                         }
773                 }
774                 // LordHavoc: customizable glow
775                 if (ent->state_current.glowsize)
776                 {
777                         // * 4 for the expansion from 0-255 to 0-1023 range,
778                         // / 255 to scale down byte colors
779                         VectorMA(dlightcolor, ent->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[ent->state_current.glowcolor], dlightcolor);
780                 }
781                 // LordHavoc: customizable trail
782                 if (ent->render.flags & RENDER_GLOWTRAIL)
783                         CL_RocketTrail2 (oldorg, neworg, ent->state_current.glowcolor, ent);
784
785                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
786                 {
787                         VectorCopy(neworg, v);
788                         // hack to make glowing player light shine on their gun
789                         if (i == cl.viewentity/* && !chase_active.integer*/)
790                                 v[2] += 30;
791                         CL_AllocDlight (&ent->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
792                 }
793
794                 if (chase_active.integer && (ent->render.flags & RENDER_VIEWMODEL))
795                         continue;
796
797                 // don't show entities with no modelindex (note: this still shows
798                 // entities which have a modelindex that resolved to a NULL model)
799                 if (!ent->state_current.modelindex)
800                         continue;
801                 if (effects & EF_NODRAW)
802                         continue;
803
804                 // store a list of view-relative entities for later adjustment in view code
805                 if (ent->render.flags & RENDER_VIEWMODEL)
806                 {
807                         if (numviewmodels < MAXVIEWMODELS)
808                                 viewmodels[numviewmodels++] = ent;
809                         continue;
810                 }
811
812                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
813
814                 CL_BoundingBoxForEntity(&ent->render);
815                 if (ent->render.model && ent->render.model->name[0] == '*' && ent->render.model->type == mod_brush)
816                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
817
818                 // note: the cl.viewentity and intermission check is to hide player
819                 // shadow during intermission and during the Nehahra movie and
820                 // Nehahra cinematics
821                 if (!(ent->state_current.effects & EF_NOSHADOW)
822                  && !(ent->state_current.effects & EF_ADDITIVE)
823                  && (ent->state_current.alpha == 255)
824                  && !(ent->render.flags & RENDER_VIEWMODEL)
825                  && (i != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility && !cl_noplayershadow.integer)))
826                         ent->render.flags |= RENDER_SHADOW;
827
828                 if (r_refdef.numentities < r_refdef.maxentities)
829                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
830
831                 if (cl_num_entities < i + 1)
832                         cl_num_entities = i + 1;
833         }
834 }
835
836 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
837 {
838         int i;
839         cl_effect_t *e;
840         if (!modelindex) // sanity check
841                 return;
842         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
843         {
844                 if (e->active)
845                         continue;
846                 e->active = true;
847                 VectorCopy(org, e->origin);
848                 e->modelindex = modelindex;
849                 e->starttime = cl.time;
850                 e->startframe = startframe;
851                 e->endframe = startframe + framecount;
852                 e->framerate = framerate;
853
854                 e->frame = 0;
855                 e->frame1time = cl.time;
856                 e->frame2time = cl.time;
857                 break;
858         }
859 }
860
861 static void CL_RelinkEffects(void)
862 {
863         int i, intframe;
864         cl_effect_t *e;
865         entity_t *ent;
866         float frame;
867
868         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
869         {
870                 if (e->active)
871                 {
872                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
873                         intframe = frame;
874                         if (intframe < 0 || intframe >= e->endframe)
875                         {
876                                 e->active = false;
877                                 memset(e, 0, sizeof(*e));
878                                 continue;
879                         }
880
881                         if (intframe != e->frame)
882                         {
883                                 e->frame = intframe;
884                                 e->frame1time = e->frame2time;
885                                 e->frame2time = cl.time;
886                         }
887
888                         // if we're drawing effects, get a new temp entity
889                         // (NewTempEntity adds it to the render entities list for us)
890                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
891                         {
892                                 // interpolation stuff
893                                 ent->render.frame1 = intframe;
894                                 ent->render.frame2 = intframe + 1;
895                                 if (ent->render.frame2 >= e->endframe)
896                                         ent->render.frame2 = -1; // disappear
897                                 ent->render.framelerp = frame - intframe;
898                                 ent->render.frame1time = e->frame1time;
899                                 ent->render.frame2time = e->frame2time;
900
901                                 // normal stuff
902                                 //VectorCopy(e->origin, ent->render.origin);
903                                 ent->render.model = cl.model_precache[e->modelindex];
904                                 ent->render.frame = ent->render.frame2;
905                                 ent->render.colormap = -1; // no special coloring
906                                 //ent->render.scale = 1;
907                                 ent->render.alpha = 1;
908
909                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
910                                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
911                                 CL_BoundingBoxForEntity(&ent->render);
912                         }
913                 }
914         }
915 }
916
917 void CL_RelinkBeams (void)
918 {
919         int i;
920         beam_t *b;
921         vec3_t dist, org;
922         float d;
923         entity_t *ent;
924         float yaw, pitch;
925         float forward;
926
927         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
928         {
929                 if (!b->model || b->endtime < cl.time)
930                         continue;
931
932                 // if coming from the player, update the start position
933                 //if (b->entity == cl.viewentity)
934                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
935                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
936                 {
937                         entity_state_t *p = &cl_entities[b->entity].state_previous;
938                         //entity_state_t *c = &cl_entities[b->entity].state_current;
939                         entity_render_t *r = &cl_entities[b->entity].render;
940                         matrix4x4_t matrix, imatrix;
941                         if (b->relativestartvalid == 2)
942                         {
943                                 // not really valid yet, we need to get the orientation now
944                                 // (ParseBeam flagged this because it is received before
945                                 //  entities are received, by now they have been received)
946                                 // note: because players create lightning in their think
947                                 // function (which occurs before movement), they actually
948                                 // have some lag in it's location, so compare to the
949                                 // previous player state, not the latest
950                                 if (b->entity == cl.viewentity)
951                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
952                                 else
953                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
954                                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
955                                 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
956                                 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
957                                 b->relativestartvalid = 1;
958                         }
959                         else
960                         {
961                                 if (b->entity == cl.viewentity)
962                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
963                                 else
964                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
965                                 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
966                                 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
967                         }
968                 }
969
970                 if (b->lightning)
971                 {
972                         if (cl_beams_lightatend.integer)
973                                 CL_AllocDlight (NULL, b->end, 200, 0.3, 0.7, 1, 0, 0);
974                         if (cl_beams_polygons.integer)
975                                 continue;
976                 }
977
978                 // calculate pitch and yaw
979                 VectorSubtract (b->end, b->start, dist);
980
981                 if (dist[1] == 0 && dist[0] == 0)
982                 {
983                         yaw = 0;
984                         if (dist[2] > 0)
985                                 pitch = 90;
986                         else
987                                 pitch = 270;
988                 }
989                 else
990                 {
991                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
992                         if (yaw < 0)
993                                 yaw += 360;
994
995                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
996                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
997                         if (pitch < 0)
998                                 pitch += 360;
999                 }
1000
1001                 // add new entities for the lightning
1002                 VectorCopy (b->start, org);
1003                 d = VectorNormalizeLength(dist);
1004                 while (d > 0)
1005                 {
1006                         ent = CL_NewTempEntity ();
1007                         if (!ent)
1008                                 return;
1009                         //VectorCopy (org, ent->render.origin);
1010                         ent->render.model = b->model;
1011                         ent->render.effects = EF_FULLBRIGHT;
1012                         //ent->render.angles[0] = pitch;
1013                         //ent->render.angles[1] = yaw;
1014                         //ent->render.angles[2] = rand()%360;
1015                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1016                         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1017                         CL_BoundingBoxForEntity(&ent->render);
1018                         VectorMA(org, 30, dist, org);
1019                         d -= 30;
1020                 }
1021         }
1022 }
1023
1024 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "4"};
1025 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5"};
1026 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "1024"};
1027 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1"};
1028 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1"};
1029 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1"};
1030 cvar_t r_lightningbeam_qmbtexture = {CVAR_SAVE, "r_lightningbeam_qmbtexture", "0"};
1031
1032 rtexture_t *r_lightningbeamtexture;
1033 rtexture_t *r_lightningbeamqmbtexture;
1034 rtexturepool_t *r_lightningbeamtexturepool;
1035
1036 int r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
1037
1038 void r_lightningbeams_start(void)
1039 {
1040         r_lightningbeamtexturepool = R_AllocTexturePool();
1041         r_lightningbeamtexture = NULL;
1042         r_lightningbeamqmbtexture = NULL;
1043 }
1044
1045 void r_lightningbeams_setupqmbtexture(void)
1046 {
1047         r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
1048         if (r_lightningbeamqmbtexture == NULL)
1049                 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
1050 }
1051
1052 void r_lightningbeams_setuptexture(void)
1053 {
1054 #if 0
1055 #define BEAMWIDTH 128
1056 #define BEAMHEIGHT 64
1057 #define PATHPOINTS 8
1058         int i, j, px, py, nearestpathindex, imagenumber;
1059         float particlex, particley, particlexv, particleyv, dx, dy, s, maxpathstrength;
1060         qbyte *pixels;
1061         int *image;
1062         struct {float x, y, strength;} path[PATHPOINTS], temppath;
1063
1064         image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
1065         pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(qbyte[4]));
1066
1067         for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
1068         {
1069         for (i = 0;i < PATHPOINTS;i++)
1070         {
1071                 path[i].x = lhrandom(0, 1);
1072                 path[i].y = lhrandom(0.2, 0.8);
1073                 path[i].strength = lhrandom(0, 1);
1074         }
1075         for (i = 0;i < PATHPOINTS;i++)
1076         {
1077                 for (j = i + 1;j < PATHPOINTS;j++)
1078                 {
1079                         if (path[j].x < path[i].x)
1080                         {
1081                                 temppath = path[j];
1082                                 path[j] = path[i];
1083                                 path[i] = temppath;
1084                         }
1085                 }
1086         }
1087         particlex = path[0].x;
1088         particley = path[0].y;
1089         particlexv = lhrandom(0, 0.02);
1090         particlexv = lhrandom(-0.02, 0.02);
1091         memset(image, 0, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
1092         for (i = 0;i < 65536;i++)
1093         {
1094                 for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
1095                         if (path[nearestpathindex].x > particlex)
1096                                 break;
1097                 nearestpathindex %= PATHPOINTS;
1098                 dx = path[nearestpathindex].x + lhrandom(-0.01, 0.01);dx = bound(0, dx, 1) - particlex;if (dx < 0) dx += 1;
1099                 dy = path[nearestpathindex].y + lhrandom(-0.01, 0.01);dy = bound(0, dy, 1) - particley;
1100                 s = path[nearestpathindex].strength / sqrt(dx*dx+dy*dy);
1101                 particlexv = particlexv /* (1 - lhrandom(0.08, 0.12))*/ + dx * s;
1102                 particleyv = particleyv /* (1 - lhrandom(0.08, 0.12))*/ + dy * s;
1103                 particlex += particlexv * maxpathstrength;particlex -= (int) particlex;
1104                 particley += particleyv * maxpathstrength;particley = bound(0, particley, 1);
1105                 px = particlex * BEAMWIDTH;
1106                 py = particley * BEAMHEIGHT;
1107                 if (px >= 0 && py >= 0 && px < BEAMWIDTH && py < BEAMHEIGHT)
1108                         image[py*BEAMWIDTH+px] += 16;
1109         }
1110
1111         for (py = 0;py < BEAMHEIGHT;py++)
1112         {
1113                 for (px = 0;px < BEAMWIDTH;px++)
1114                 {
1115                         pixels[(py*BEAMWIDTH+px)*4+0] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1116                         pixels[(py*BEAMWIDTH+px)*4+1] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1117                         pixels[(py*BEAMWIDTH+px)*4+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1118                         pixels[(py*BEAMWIDTH+px)*4+3] = 255;
1119                 }
1120         }
1121
1122         Image_WriteTGARGBA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
1123         }
1124
1125         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
1126
1127         Mem_Free(pixels);
1128         Mem_Free(image);
1129 #else
1130 #define BEAMWIDTH 64
1131 #define BEAMHEIGHT 128
1132         float r, g, b, intensity, fx, width, center;
1133         int x, y;
1134         qbyte *data, *noise1, *noise2;
1135
1136         data = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * 4);
1137         noise1 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
1138         noise2 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
1139         fractalnoise(noise1, BEAMHEIGHT, BEAMHEIGHT / 8);
1140         fractalnoise(noise2, BEAMHEIGHT, BEAMHEIGHT / 16);
1141
1142         for (y = 0;y < BEAMHEIGHT;y++)
1143         {
1144                 width = 0.15;//((noise1[y * BEAMHEIGHT] * (1.0f / 256.0f)) * 0.1f + 0.1f);
1145                 center = (noise1[y * BEAMHEIGHT + (BEAMHEIGHT / 2)] / 256.0f) * (1.0f - width * 2.0f) + width;
1146                 for (x = 0;x < BEAMWIDTH;x++, fx++)
1147                 {
1148                         fx = (((float) x / BEAMWIDTH) - center) / width;
1149                         intensity = 1.0f - sqrt(fx * fx);
1150                         if (intensity > 0)
1151                                 intensity = pow(intensity, 2) * ((noise2[y * BEAMHEIGHT + x] * (1.0f / 256.0f)) * 0.33f + 0.66f);
1152                         intensity = bound(0, intensity, 1);
1153                         r = intensity * 1.0f;
1154                         g = intensity * 1.0f;
1155                         b = intensity * 1.0f;
1156                         data[(y * BEAMWIDTH + x) * 4 + 0] = (qbyte)(bound(0, r, 1) * 255.0f);
1157                         data[(y * BEAMWIDTH + x) * 4 + 1] = (qbyte)(bound(0, g, 1) * 255.0f);
1158                         data[(y * BEAMWIDTH + x) * 4 + 2] = (qbyte)(bound(0, b, 1) * 255.0f);
1159                         data[(y * BEAMWIDTH + x) * 4 + 3] = (qbyte)255;
1160                 }
1161         }
1162
1163         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
1164         Mem_Free(noise1);
1165         Mem_Free(noise2);
1166         Mem_Free(data);
1167 #endif
1168 }
1169
1170 void r_lightningbeams_shutdown(void)
1171 {
1172         r_lightningbeamtexture = NULL;
1173         r_lightningbeamqmbtexture = NULL;
1174         R_FreeTexturePool(&r_lightningbeamtexturepool);
1175 }
1176
1177 void r_lightningbeams_newmap(void)
1178 {
1179 }
1180
1181 void R_LightningBeams_Init(void)
1182 {
1183         Cvar_RegisterVariable(&r_lightningbeam_thickness);
1184         Cvar_RegisterVariable(&r_lightningbeam_scroll);
1185         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
1186         Cvar_RegisterVariable(&r_lightningbeam_color_red);
1187         Cvar_RegisterVariable(&r_lightningbeam_color_green);
1188         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
1189         Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
1190         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
1191 }
1192
1193 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
1194 {
1195         // near right corner
1196         VectorAdd     (start, offset, (v + 0));
1197         // near left corner
1198         VectorSubtract(start, offset, (v + 3));
1199         // far left corner
1200         VectorSubtract(end  , offset, (v + 6));
1201         // far right corner
1202         VectorAdd     (end  , offset, (v + 9));
1203 }
1204
1205 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
1206 {
1207         if (r_lightningbeam_qmbtexture.integer)
1208         {
1209                 // near right corner
1210                 tc[0] = t1;tc[1] = 0;
1211                 // near left corner
1212                 tc[2] = t1;tc[3] = 1;
1213                 // far left corner
1214                 tc[4] = t2;tc[5] = 1;
1215                 // far right corner
1216                 tc[6] = t2;tc[7] = 0;
1217         }
1218         else
1219         {
1220                 // near right corner
1221                 tc[0] = 0;tc[1] = t1;
1222                 // near left corner
1223                 tc[2] = 1;tc[3] = t1;
1224                 // far left corner
1225                 tc[4] = 1;tc[5] = t2;
1226                 // far right corner
1227                 tc[6] = 0;tc[7] = t2;
1228         }
1229 }
1230
1231 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
1232 {
1233         int i;
1234         vec3_t fogvec;
1235         float ifog;
1236         for (i = 0;i < numverts;i++, v += 3, c += 4)
1237         {
1238                 VectorSubtract(v, r_origin, fogvec);
1239                 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
1240                 c[0] = r * ifog;
1241                 c[1] = g * ifog;
1242                 c[2] = b * ifog;
1243                 c[3] = a;
1244         }
1245 }
1246
1247 float beamrepeatscale;
1248
1249 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
1250 {
1251         const beam_t *b = calldata1;
1252         rmeshstate_t m;
1253         vec3_t beamdir, right, up, offset;
1254         float length, t1, t2;
1255
1256         R_Mesh_Matrix(&r_identitymatrix);
1257
1258         // calculate beam direction (beamdir) vector and beam length
1259         // get difference vector
1260         VectorSubtract(b->end, b->start, beamdir);
1261         // find length of difference vector
1262         length = sqrt(DotProduct(beamdir, beamdir));
1263         // calculate scale to make beamdir a unit vector (normalized)
1264         t1 = 1.0f / length;
1265         // scale beamdir so it is now normalized
1266         VectorScale(beamdir, t1, beamdir);
1267
1268         // calculate up vector such that it points toward viewer, and rotates around the beamdir
1269         // get direction from start of beam to viewer
1270         VectorSubtract(r_origin, b->start, up);
1271         // remove the portion of the vector that moves along the beam
1272         // (this leaves only a vector pointing directly away from the beam)
1273         t1 = -DotProduct(up, beamdir);
1274         VectorMA(up, t1, beamdir, up);
1275         // now we have a vector pointing away from the beam, now we need to normalize it
1276         VectorNormalizeFast(up);
1277         // generate right vector from forward and up, the result is already normalized
1278         // (CrossProduct returns a vector of multiplied length of the two inputs)
1279         CrossProduct(beamdir, up, right);
1280
1281         // calculate T coordinate scrolling (start and end texcoord along the beam)
1282         t1 = cl.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(b->start, beamdir);
1283         t1 = t1 - (int) t1;
1284         t2 = t1 + beamrepeatscale * length;
1285
1286         // the beam is 3 polygons in this configuration:
1287         //  *   2
1288         //   * *
1289         // 1******
1290         //   * *
1291         //  *   3
1292         // they are showing different portions of the beam texture, creating an
1293         // illusion of a beam that appears to curl around in 3D space
1294         // (and realize that the whole polygon assembly orients itself to face
1295         //  the viewer)
1296
1297         memset(&m, 0, sizeof(m));
1298         if (r_lightningbeam_qmbtexture.integer)
1299                 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
1300         else
1301                 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
1302         m.pointer_texcoord[0] = varray_texcoord2f[0];
1303         R_Mesh_State_Texture(&m);
1304
1305         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1306         GL_DepthMask(false);
1307         GL_DepthTest(true);
1308         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
1309                 r_lightningbeams_setupqmbtexture();
1310         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
1311                 r_lightningbeams_setuptexture();
1312
1313         // polygon 1, verts 0-3
1314         VectorScale(right, r_lightningbeam_thickness.value, offset);
1315         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 0, b->start, b->end, offset);
1316         // polygon 2, verts 4-7
1317         VectorAdd(right, up, offset);
1318         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1319         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 12, b->start, b->end, offset);
1320         // polygon 3, verts 8-11
1321         VectorSubtract(right, up, offset);
1322         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1323         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 24, b->start, b->end, offset);
1324         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 0, t1, t2);
1325         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 8, t1 + 0.33, t2 + 0.33);
1326         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 16, t1 + 0.66, t2 + 0.66);
1327         GL_VertexPointer(varray_vertex3f);
1328
1329         if (fogenabled)
1330         {
1331                 // per vertex colors if fog is used
1332                 GL_ColorPointer(varray_color4f);
1333                 R_FogLightningBeam_Vertex3f_Color4f(varray_vertex3f, varray_color4f, 12, r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1334         }
1335         else
1336         {
1337                 // solid color if fog is not used
1338                 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1339         }
1340
1341         // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
1342         R_Mesh_Draw(12, 6, r_lightningbeamelements);
1343 }
1344
1345 void R_DrawLightningBeams (void)
1346 {
1347         int i;
1348         beam_t *b;
1349         vec3_t org;
1350
1351         if (!cl_beams_polygons.integer)
1352                 return;
1353
1354         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
1355         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1356         {
1357                 if (b->model && b->endtime >= cl.time && b->lightning)
1358                 {
1359                         VectorAdd(b->start, b->end, org);
1360                         VectorScale(org, 0.5f, org);
1361                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
1362                 }
1363         }
1364 }
1365
1366
1367 void CL_LerpPlayer(float frac)
1368 {
1369         int i;
1370         float d;
1371
1372         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1373
1374         for (i = 0;i < 3;i++)
1375                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1376
1377         if (cls.demoplayback)
1378         {
1379                 // interpolate the angles
1380                 for (i = 0;i < 3;i++)
1381                 {
1382                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1383                         if (d > 180)
1384                                 d -= 360;
1385                         else if (d < -180)
1386                                 d += 360;
1387                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1388                 }
1389         }
1390 }
1391
1392 void CL_RelinkEntities (void)
1393 {
1394         float frac;
1395
1396         // fraction from previous network update to current
1397         frac = CL_LerpPoint();
1398
1399         CL_ClearTempEntities();
1400         CL_DecayLights();
1401         CL_RelinkWorld();
1402         CL_RelinkStaticEntities();
1403         CL_RelinkNetworkEntities();
1404         CL_RelinkEffects();
1405         CL_MoveParticles();
1406
1407         CL_LerpPlayer(frac);
1408
1409         CL_RelinkBeams();
1410 }
1411
1412
1413 /*
1414 ===============
1415 CL_ReadFromServer
1416
1417 Read all incoming data from the server
1418 ===============
1419 */
1420 int CL_ReadFromServer(void)
1421 {
1422         CL_ReadDemoMessage();
1423
1424         r_refdef.numentities = 0;
1425         cl_num_entities = 0;
1426         cl_num_brushmodel_entities = 0;
1427
1428         if (cls.state == ca_connected && cls.signon == SIGNONS)
1429         {
1430                 CL_RelinkEntities();
1431
1432                 // run cgame code (which can add more entities)
1433                 CL_CGVM_Frame();
1434         }
1435
1436         return 0;
1437 }
1438
1439 /*
1440 =================
1441 CL_SendCmd
1442 =================
1443 */
1444 void CL_SendCmd(void)
1445 {
1446         usercmd_t cmd;
1447
1448         if (cls.signon == SIGNONS)
1449         {
1450                 // get basic movement from keyboard
1451                 CL_BaseMove(&cmd);
1452
1453                 // OS independent code
1454                 IN_PreMove();
1455
1456                 // allow mice or other external controllers to add to the move
1457                 IN_Move(&cmd);
1458
1459                 // OS independent code
1460                 IN_PostMove();
1461
1462                 // send the unreliable message
1463                 CL_SendMove(&cmd);
1464         }
1465
1466         if (cls.demoplayback)
1467         {
1468                 SZ_Clear(&cls.message);
1469                 return;
1470         }
1471
1472         // send the reliable message (forwarded commands) if there is one
1473         if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1474         {
1475                 if (developer.integer)
1476                 {
1477                         Con_Printf("CL_SendCmd: sending reliable message:\n");
1478                         SZ_HexDumpToConsole(&cls.message);
1479                 }
1480                 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1481                         Host_Error("CL_WriteToServer: lost server connection");
1482                 SZ_Clear(&cls.message);
1483         }
1484 }
1485
1486 // LordHavoc: pausedemo command
1487 static void CL_PauseDemo_f (void)
1488 {
1489         cls.demopaused = !cls.demopaused;
1490         if (cls.demopaused)
1491                 Con_Printf("Demo paused\n");
1492         else
1493                 Con_Printf("Demo unpaused\n");
1494 }
1495
1496 /*
1497 ======================
1498 CL_Fog_f
1499 ======================
1500 */
1501 static void CL_Fog_f (void)
1502 {
1503         if (Cmd_Argc () == 1)
1504         {
1505                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1506                 return;
1507         }
1508         fog_density = atof(Cmd_Argv(1));
1509         fog_red = atof(Cmd_Argv(2));
1510         fog_green = atof(Cmd_Argv(3));
1511         fog_blue = atof(Cmd_Argv(4));
1512 }
1513
1514 /*
1515 =================
1516 CL_Init
1517 =================
1518 */
1519 void CL_Init (void)
1520 {
1521         cl_entities_mempool = Mem_AllocPool("client entities");
1522         cl_refdef_mempool = Mem_AllocPool("refdef");
1523
1524         memset(&r_refdef, 0, sizeof(r_refdef));
1525         // max entities sent to renderer per frame
1526         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1527         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1528         // 256k drawqueue buffer
1529         r_refdef.maxdrawqueuesize = 256 * 1024;
1530         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1531
1532         SZ_Alloc (&cls.message, 1024, "cls.message");
1533
1534         CL_InitInput ();
1535         CL_InitTEnts ();
1536
1537 //
1538 // register our commands
1539 //
1540         Cvar_RegisterVariable (&cl_upspeed);
1541         Cvar_RegisterVariable (&cl_forwardspeed);
1542         Cvar_RegisterVariable (&cl_backspeed);
1543         Cvar_RegisterVariable (&cl_sidespeed);
1544         Cvar_RegisterVariable (&cl_movespeedkey);
1545         Cvar_RegisterVariable (&cl_yawspeed);
1546         Cvar_RegisterVariable (&cl_pitchspeed);
1547         Cvar_RegisterVariable (&cl_anglespeedkey);
1548         Cvar_RegisterVariable (&cl_shownet);
1549         Cvar_RegisterVariable (&cl_nolerp);
1550         Cvar_RegisterVariable (&lookspring);
1551         Cvar_RegisterVariable (&lookstrafe);
1552         Cvar_RegisterVariable (&sensitivity);
1553         Cvar_RegisterVariable (&freelook);
1554
1555         Cvar_RegisterVariable (&m_pitch);
1556         Cvar_RegisterVariable (&m_yaw);
1557         Cvar_RegisterVariable (&m_forward);
1558         Cvar_RegisterVariable (&m_side);
1559
1560         Cvar_RegisterVariable (&cl_itembobspeed);
1561         Cvar_RegisterVariable (&cl_itembobheight);
1562
1563         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1564         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1565         Cmd_AddCommand ("record", CL_Record_f);
1566         Cmd_AddCommand ("stop", CL_Stop_f);
1567         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1568         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1569
1570         Cmd_AddCommand ("fog", CL_Fog_f);
1571
1572         // LordHavoc: added pausedemo
1573         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1574
1575         Cvar_RegisterVariable(&r_draweffects);
1576         Cvar_RegisterVariable(&cl_explosions);
1577         Cvar_RegisterVariable(&cl_stainmaps);
1578         Cvar_RegisterVariable(&cl_beams_polygons);
1579         Cvar_RegisterVariable(&cl_beams_relative);
1580         Cvar_RegisterVariable(&cl_beams_lightatend);
1581         Cvar_RegisterVariable(&cl_noplayershadow);
1582
1583         R_LightningBeams_Init();
1584
1585         CL_Parse_Init();
1586         CL_Particles_Init();
1587         CL_Screen_Init();
1588         CL_CGVM_Init();
1589
1590         CL_Video_Init();
1591 }
1592