changed brush model API - now uses function pointers for some of the brush model...
[xonotic/darkplaces.git] / cl_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_main.c  -- client main loop
21
22 #include "quakedef.h"
23 #include "cl_collision.h"
24 #include "cl_video.h"
25 #include "image.h"
26
27 // we need to declare some mouse variables here, because the menu system
28 // references them even when on a unix system.
29
30 cvar_t cl_shownet = {0, "cl_shownet","0"};
31 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
32
33 cvar_t cl_itembobheight = {0, "cl_itembobheight", "8"};
34 cvar_t cl_itembobspeed = {0, "cl_itembobspeed", "0.5"};
35
36 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
37 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
38 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
39
40 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
41 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
42 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
43 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
44
45 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
46
47 cvar_t r_draweffects = {0, "r_draweffects", "1"};
48
49 cvar_t cl_explosions = {CVAR_SAVE, "cl_explosions", "1"};
50 cvar_t cl_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         cl.worldmodel->FindNonSolidLocation(cl.worldmodel, org, dl->origin, 6);
456         //VectorCopy(org, dl->origin);
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 }
467
468 void CL_DecayLights (void)
469 {
470         int i;
471         dlight_t *dl;
472         float time;
473
474         time = cl.time - cl.oldtime;
475
476         dl = cl_dlights;
477         for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
478         {
479                 if (!dl->radius)
480                         continue;
481                 if (dl->die < cl.time)
482                 {
483                         dl->radius = 0;
484                         continue;
485                 }
486
487                 dl->radius -= time*dl->decay;
488                 if (dl->radius < 0)
489                         dl->radius = 0;
490         }
491 }
492
493 void CL_RelinkWorld (void)
494 {
495         entity_t *ent = &cl_entities[0];
496         if (cl_num_entities < 1)
497                 cl_num_entities = 1;
498         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
499         Matrix4x4_CreateIdentity(&ent->render.matrix);
500         Matrix4x4_CreateIdentity(&ent->render.inversematrix);
501         CL_BoundingBoxForEntity(&ent->render);
502 }
503
504 static void CL_RelinkStaticEntities(void)
505 {
506         int i;
507         for (i = 0;i < cl_num_static_entities && r_refdef.numentities < r_refdef.maxentities;i++)
508         {
509                 Mod_CheckLoaded(cl_static_entities[i].render.model);
510                 r_refdef.entities[r_refdef.numentities++] = &cl_static_entities[i].render;
511         }
512 }
513
514 /*
515 ===============
516 CL_RelinkEntities
517 ===============
518 */
519 extern qboolean Nehahrademcompatibility;
520 #define MAXVIEWMODELS 32
521 entity_t *viewmodels[MAXVIEWMODELS];
522 int numviewmodels;
523 static void CL_RelinkNetworkEntities(void)
524 {
525         entity_t *ent;
526         int i, effects, temp;
527         float d, bobjrotate, bobjoffset, lerp;
528         vec3_t oldorg, neworg, delta, dlightcolor, v, v2, mins, maxs;
529
530         numviewmodels = 0;
531
532         bobjrotate = ANGLEMOD(100*cl.time);
533         if (cl_itembobheight.value)
534                 bobjoffset = (cos(cl.time * cl_itembobspeed.value * (2.0 * M_PI)) + 1.0) * 0.5 * cl_itembobheight.value;
535         else
536                 bobjoffset = 0;
537
538         // start on the entity after the world
539         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS;i++, ent++)
540         {
541                 // if the object isn't active in the current network frame, skip it
542                 if (!cl_entities_active[i])
543                         continue;
544                 if (!ent->state_current.active)
545                 {
546                         cl_entities_active[i] = false;
547                         continue;
548                 }
549
550                 VectorCopy(ent->persistent.trail_origin, oldorg);
551
552                 if (!ent->state_previous.active)
553                 {
554                         // only one state available
555                         VectorCopy (ent->persistent.neworigin, neworg);
556                         VectorCopy (ent->persistent.newangles, ent->render.angles);
557                         VectorCopy (neworg, oldorg);
558                 }
559                 else
560                 {
561                         // if the delta is large, assume a teleport and don't lerp
562                         VectorSubtract(ent->persistent.neworigin, ent->persistent.oldorigin, delta);
563                         if (ent->persistent.lerpdeltatime > 0)
564                         {
565                                 lerp = (cl.time - ent->persistent.lerpstarttime) / ent->persistent.lerpdeltatime;
566                                 if (lerp < 1)
567                                 {
568                                         // interpolate the origin and angles
569                                         VectorMA(ent->persistent.oldorigin, lerp, delta, neworg);
570                                         VectorSubtract(ent->persistent.newangles, ent->persistent.oldangles, delta);
571                                         if (delta[0] < -180) delta[0] += 360;else if (delta[0] >= 180) delta[0] -= 360;
572                                         if (delta[1] < -180) delta[1] += 360;else if (delta[1] >= 180) delta[1] -= 360;
573                                         if (delta[2] < -180) delta[2] += 360;else if (delta[2] >= 180) delta[2] -= 360;
574                                         VectorMA(ent->persistent.oldangles, lerp, delta, ent->render.angles);
575                                 }
576                                 else
577                                 {
578                                         // no interpolation
579                                         VectorCopy (ent->persistent.neworigin, neworg);
580                                         VectorCopy (ent->persistent.newangles, ent->render.angles);
581                                 }
582                         }
583                         else
584                         {
585                                 // no interpolation
586                                 VectorCopy (ent->persistent.neworigin, neworg);
587                                 VectorCopy (ent->persistent.newangles, ent->render.angles);
588                         }
589                 }
590
591                 if (!ent->render.model || ent->render.model->type != mod_brush)
592                         ent->render.angles[0] = -ent->render.angles[0];
593
594                 VectorCopy (neworg, ent->persistent.trail_origin);
595                 // persistent.modelindex will be updated by CL_LerpUpdate
596                 if (ent->state_current.modelindex != ent->persistent.modelindex || !ent->state_previous.active)
597                         VectorCopy(neworg, oldorg);
598
599                 VectorCopy (neworg, ent->render.origin);
600                 ent->render.flags = ent->state_current.flags;
601                 if (i == cl.viewentity)
602                         ent->render.flags |= RENDER_EXTERIORMODEL;
603                 ent->render.effects = effects = ent->state_current.effects;
604                 if (ent->state_current.flags & RENDER_COLORMAPPED)
605                         ent->render.colormap = ent->state_current.colormap;
606                 else if (cl.scores == NULL || !ent->state_current.colormap)
607                         ent->render.colormap = -1; // no special coloring
608                 else
609                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
610                 ent->render.skinnum = ent->state_current.skin;
611                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
612                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
613
614                 if (ent->render.model && ent->render.model->flags & EF_ROTATE)
615                 {
616                         ent->render.angles[1] = bobjrotate;
617                         ent->render.origin[2] += bobjoffset;
618                 }
619
620                 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);
621
622                 // update interpolation info
623                 CL_LerpUpdate(ent);
624
625                 // handle effects now...
626                 dlightcolor[0] = 0;
627                 dlightcolor[1] = 0;
628                 dlightcolor[2] = 0;
629
630                 // LordHavoc: if the entity has no effects, don't check each
631                 if (effects)
632                 {
633                         if (effects & EF_BRIGHTFIELD)
634                         {
635                                 if (gamemode == GAME_NEXUIZ)
636                                 {
637                                         dlightcolor[0] += 100.0f;
638                                         dlightcolor[1] += 200.0f;
639                                         dlightcolor[2] += 400.0f;
640                                         // don't do trail if we have no previous location
641                                         if (ent->state_previous.active)
642                                                 CL_RocketTrail (oldorg, neworg, 8, ent);
643                                 }
644                                 else
645                                         CL_EntityParticles (ent);
646                         }
647                         if (effects & EF_MUZZLEFLASH)
648                                 ent->persistent.muzzleflash = 100.0f;
649                         if (effects & EF_DIMLIGHT)
650                         {
651                                 dlightcolor[0] += 200.0f;
652                                 dlightcolor[1] += 200.0f;
653                                 dlightcolor[2] += 200.0f;
654                         }
655                         if (effects & EF_BRIGHTLIGHT)
656                         {
657                                 dlightcolor[0] += 400.0f;
658                                 dlightcolor[1] += 400.0f;
659                                 dlightcolor[2] += 400.0f;
660                         }
661                         // LordHavoc: added EF_RED and EF_BLUE
662                         if (effects & EF_RED) // red
663                         {
664                                 dlightcolor[0] += 200.0f;
665                                 dlightcolor[1] +=  20.0f;
666                                 dlightcolor[2] +=  20.0f;
667                         }
668                         if (effects & EF_BLUE) // blue
669                         {
670                                 dlightcolor[0] +=  20.0f;
671                                 dlightcolor[1] +=  20.0f;
672                                 dlightcolor[2] += 200.0f;
673                         }
674                         if (effects & EF_FLAME)
675                         {
676                                 if (ent->render.model)
677                                 {
678                                         mins[0] = neworg[0] - 16.0f;
679                                         mins[1] = neworg[1] - 16.0f;
680                                         mins[2] = neworg[2] - 16.0f;
681                                         maxs[0] = neworg[0] + 16.0f;
682                                         maxs[1] = neworg[1] + 16.0f;
683                                         maxs[2] = neworg[2] + 16.0f;
684                                         // how many flames to make
685                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
686                                         CL_FlameCube(mins, maxs, temp);
687                                 }
688                                 d = lhrandom(200, 250);
689                                 dlightcolor[0] += d * 1.0f;
690                                 dlightcolor[1] += d * 0.7f;
691                                 dlightcolor[2] += d * 0.3f;
692                         }
693                         if (effects & EF_STARDUST)
694                         {
695                                 if (ent->render.model)
696                                 {
697                                         mins[0] = neworg[0] - 16.0f;
698                                         mins[1] = neworg[1] - 16.0f;
699                                         mins[2] = neworg[2] - 16.0f;
700                                         maxs[0] = neworg[0] + 16.0f;
701                                         maxs[1] = neworg[1] + 16.0f;
702                                         maxs[2] = neworg[2] + 16.0f;
703                                         // how many particles to make
704                                         temp = (int) (cl.time * 200) - (int) (cl.oldtime * 200);
705                                         CL_Stardust(mins, maxs, temp);
706                                 }
707                                 d = 100;
708                                 dlightcolor[0] += d * 1.0f;
709                                 dlightcolor[1] += d * 0.7f;
710                                 dlightcolor[2] += d * 0.3f;
711                         }
712                 }
713
714                 if (ent->persistent.muzzleflash > 0)
715                 {
716                         v2[0] = ent->render.matrix.m[0][0] * 18 + neworg[0];
717                         v2[1] = ent->render.matrix.m[0][1] * 18 + neworg[1];
718                         v2[2] = ent->render.matrix.m[0][2] * 18 + neworg[2] + 16;
719                         CL_TraceLine(neworg, v2, v, NULL, 0, true, NULL);
720
721                         CL_AllocDlight (NULL, v, ent->persistent.muzzleflash, 1, 1, 1, 0, 0);
722                         ent->persistent.muzzleflash -= cl.frametime * 1000;
723                 }
724
725                 // LordHavoc: if the model has no flags, don't check each
726                 if (ent->render.model && ent->render.model->flags)
727                 {
728                         // note: EF_ROTATE handled above, above matrix calculation
729                         // only do trails if present in the previous frame as well
730                         if (ent->state_previous.active)
731                         {
732                                 if (ent->render.model->flags & EF_GIB)
733                                         CL_RocketTrail (oldorg, neworg, 2, ent);
734                                 else if (ent->render.model->flags & EF_ZOMGIB)
735                                         CL_RocketTrail (oldorg, neworg, 4, ent);
736                                 else if (ent->render.model->flags & EF_TRACER)
737                                 {
738                                         CL_RocketTrail (oldorg, neworg, 3, ent);
739                                         dlightcolor[0] += 0x10;
740                                         dlightcolor[1] += 0x40;
741                                         dlightcolor[2] += 0x10;
742                                 }
743                                 else if (ent->render.model->flags & EF_TRACER2)
744                                 {
745                                         CL_RocketTrail (oldorg, neworg, 5, ent);
746                                         dlightcolor[0] += 0x50;
747                                         dlightcolor[1] += 0x30;
748                                         dlightcolor[2] += 0x10;
749                                 }
750                                 else if (ent->render.model->flags & EF_ROCKET)
751                                 {
752                                         CL_RocketTrail (oldorg, ent->render.origin, 0, ent);
753                                         dlightcolor[0] += 200.0f;
754                                         dlightcolor[1] += 160.0f;
755                                         dlightcolor[2] +=  80.0f;
756                                 }
757                                 else if (ent->render.model->flags & EF_GRENADE)
758                                 {
759                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility (cigar smoke)
760                                                 CL_RocketTrail (oldorg, neworg, 7, ent);
761                                         else
762                                                 CL_RocketTrail (oldorg, neworg, 1, ent);
763                                 }
764                                 else if (ent->render.model->flags & EF_TRACER3)
765                                 {
766                                         CL_RocketTrail (oldorg, neworg, 6, ent);
767                                         dlightcolor[0] += 0x50;
768                                         dlightcolor[1] += 0x20;
769                                         dlightcolor[2] += 0x40;
770                                 }
771                         }
772                 }
773                 // LordHavoc: customizable glow
774                 if (ent->state_current.glowsize)
775                 {
776                         // * 4 for the expansion from 0-255 to 0-1023 range,
777                         // / 255 to scale down byte colors
778                         VectorMA(dlightcolor, ent->state_current.glowsize * (4.0f / 255.0f), (qbyte *)&palette_complete[ent->state_current.glowcolor], dlightcolor);
779                 }
780                 // LordHavoc: customizable trail
781                 if (ent->render.flags & RENDER_GLOWTRAIL)
782                         CL_RocketTrail2 (oldorg, neworg, ent->state_current.glowcolor, ent);
783
784                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
785                 {
786                         VectorCopy(neworg, v);
787                         // hack to make glowing player light shine on their gun
788                         if (i == cl.viewentity/* && !chase_active.integer*/)
789                                 v[2] += 30;
790                         CL_AllocDlight (&ent->render, v, 1, dlightcolor[0], dlightcolor[1], dlightcolor[2], 0, 0);
791                 }
792
793                 if (chase_active.integer && (ent->render.flags & RENDER_VIEWMODEL))
794                         continue;
795
796                 // don't show entities with no modelindex (note: this still shows
797                 // entities which have a modelindex that resolved to a NULL model)
798                 if (!ent->state_current.modelindex)
799                         continue;
800                 if (effects & EF_NODRAW)
801                         continue;
802
803                 // store a list of view-relative entities for later adjustment in view code
804                 if (ent->render.flags & RENDER_VIEWMODEL)
805                 {
806                         if (numviewmodels < MAXVIEWMODELS)
807                                 viewmodels[numviewmodels++] = ent;
808                         continue;
809                 }
810
811                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
812
813                 CL_BoundingBoxForEntity(&ent->render);
814                 if (ent->render.model && ent->render.model->name[0] == '*' && ent->render.model->type == mod_brush)
815                         cl_brushmodel_entities[cl_num_brushmodel_entities++] = &ent->render;
816
817                 // note: the cl.viewentity and intermission check is to hide player
818                 // shadow during intermission and during the Nehahra movie and
819                 // Nehahra cinematics
820                 if (!(ent->state_current.effects & EF_NOSHADOW)
821                  && !(ent->state_current.effects & EF_ADDITIVE)
822                  && (ent->state_current.alpha == 255)
823                  && !(ent->render.flags & RENDER_VIEWMODEL)
824                  && (i != cl.viewentity || (!cl.intermission && !Nehahrademcompatibility && !cl_noplayershadow.integer)))
825                         ent->render.flags |= RENDER_SHADOW;
826
827                 if (r_refdef.numentities < r_refdef.maxentities)
828                         r_refdef.entities[r_refdef.numentities++] = &ent->render;
829
830                 if (cl_num_entities < i + 1)
831                         cl_num_entities = i + 1;
832         }
833 }
834
835 void CL_Effect(vec3_t org, int modelindex, int startframe, int framecount, float framerate)
836 {
837         int i;
838         cl_effect_t *e;
839         if (!modelindex) // sanity check
840                 return;
841         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
842         {
843                 if (e->active)
844                         continue;
845                 e->active = true;
846                 VectorCopy(org, e->origin);
847                 e->modelindex = modelindex;
848                 e->starttime = cl.time;
849                 e->startframe = startframe;
850                 e->endframe = startframe + framecount;
851                 e->framerate = framerate;
852
853                 e->frame = 0;
854                 e->frame1time = cl.time;
855                 e->frame2time = cl.time;
856                 break;
857         }
858 }
859
860 static void CL_RelinkEffects(void)
861 {
862         int i, intframe;
863         cl_effect_t *e;
864         entity_t *ent;
865         float frame;
866
867         for (i = 0, e = cl_effects;i < cl_max_effects;i++, e++)
868         {
869                 if (e->active)
870                 {
871                         frame = (cl.time - e->starttime) * e->framerate + e->startframe;
872                         intframe = frame;
873                         if (intframe < 0 || intframe >= e->endframe)
874                         {
875                                 e->active = false;
876                                 memset(e, 0, sizeof(*e));
877                                 continue;
878                         }
879
880                         if (intframe != e->frame)
881                         {
882                                 e->frame = intframe;
883                                 e->frame1time = e->frame2time;
884                                 e->frame2time = cl.time;
885                         }
886
887                         // if we're drawing effects, get a new temp entity
888                         // (NewTempEntity adds it to the render entities list for us)
889                         if (r_draweffects.integer && (ent = CL_NewTempEntity()))
890                         {
891                                 // interpolation stuff
892                                 ent->render.frame1 = intframe;
893                                 ent->render.frame2 = intframe + 1;
894                                 if (ent->render.frame2 >= e->endframe)
895                                         ent->render.frame2 = -1; // disappear
896                                 ent->render.framelerp = frame - intframe;
897                                 ent->render.frame1time = e->frame1time;
898                                 ent->render.frame2time = e->frame2time;
899
900                                 // normal stuff
901                                 //VectorCopy(e->origin, ent->render.origin);
902                                 ent->render.model = cl.model_precache[e->modelindex];
903                                 ent->render.frame = ent->render.frame2;
904                                 ent->render.colormap = -1; // no special coloring
905                                 //ent->render.scale = 1;
906                                 ent->render.alpha = 1;
907
908                                 Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, e->origin[0], e->origin[1], e->origin[2], 0, 0, 0, 1);
909                                 Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
910                                 CL_BoundingBoxForEntity(&ent->render);
911                         }
912                 }
913         }
914 }
915
916 void CL_RelinkBeams (void)
917 {
918         int i;
919         beam_t *b;
920         vec3_t dist, org;
921         float d;
922         entity_t *ent;
923         float yaw, pitch;
924         float forward;
925
926         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
927         {
928                 if (!b->model || b->endtime < cl.time)
929                         continue;
930
931                 // if coming from the player, update the start position
932                 //if (b->entity == cl.viewentity)
933                 //      VectorCopy (cl_entities[cl.viewentity].render.origin, b->start);
934                 if (cl_beams_relative.integer && b->entity && cl_entities[b->entity].state_current.active && b->relativestartvalid)
935                 {
936                         entity_state_t *p = &cl_entities[b->entity].state_previous;
937                         //entity_state_t *c = &cl_entities[b->entity].state_current;
938                         entity_render_t *r = &cl_entities[b->entity].render;
939                         matrix4x4_t matrix, imatrix;
940                         if (b->relativestartvalid == 2)
941                         {
942                                 // not really valid yet, we need to get the orientation now
943                                 // (ParseBeam flagged this because it is received before
944                                 //  entities are received, by now they have been received)
945                                 // note: because players create lightning in their think
946                                 // function (which occurs before movement), they actually
947                                 // have some lag in it's location, so compare to the
948                                 // previous player state, not the latest
949                                 if (b->entity == cl.viewentity)
950                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
951                                 else
952                                         Matrix4x4_CreateFromQuakeEntity(&matrix, p->origin[0], p->origin[1], p->origin[2] + 16, p->angles[0], p->angles[1], p->angles[2], 1);
953                                 Matrix4x4_Invert_Simple(&imatrix, &matrix);
954                                 Matrix4x4_Transform(&imatrix, b->start, b->relativestart);
955                                 Matrix4x4_Transform(&imatrix, b->end, b->relativeend);
956                                 b->relativestartvalid = 1;
957                         }
958                         else
959                         {
960                                 if (b->entity == cl.viewentity)
961                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, cl.viewangles[0], cl.viewangles[1], cl.viewangles[2], 1);
962                                 else
963                                         Matrix4x4_CreateFromQuakeEntity(&matrix, r->origin[0], r->origin[1], r->origin[2] + 16, r->angles[0], r->angles[1], r->angles[2], 1);
964                                 Matrix4x4_Transform(&matrix, b->relativestart, b->start);
965                                 Matrix4x4_Transform(&matrix, b->relativeend, b->end);
966                         }
967                 }
968
969                 if (b->lightning)
970                 {
971                         if (cl_beams_lightatend.integer)
972                                 CL_AllocDlight (NULL, b->end, 200, 0.3, 0.7, 1, 0, 0);
973                         if (cl_beams_polygons.integer)
974                                 continue;
975                 }
976
977                 // calculate pitch and yaw
978                 VectorSubtract (b->end, b->start, dist);
979
980                 if (dist[1] == 0 && dist[0] == 0)
981                 {
982                         yaw = 0;
983                         if (dist[2] > 0)
984                                 pitch = 90;
985                         else
986                                 pitch = 270;
987                 }
988                 else
989                 {
990                         yaw = (int) (atan2(dist[1], dist[0]) * 180 / M_PI);
991                         if (yaw < 0)
992                                 yaw += 360;
993
994                         forward = sqrt (dist[0]*dist[0] + dist[1]*dist[1]);
995                         pitch = (int) (atan2(dist[2], forward) * 180 / M_PI);
996                         if (pitch < 0)
997                                 pitch += 360;
998                 }
999
1000                 // add new entities for the lightning
1001                 VectorCopy (b->start, org);
1002                 d = VectorNormalizeLength(dist);
1003                 while (d > 0)
1004                 {
1005                         ent = CL_NewTempEntity ();
1006                         if (!ent)
1007                                 return;
1008                         //VectorCopy (org, ent->render.origin);
1009                         ent->render.model = b->model;
1010                         ent->render.effects = EF_FULLBRIGHT;
1011                         //ent->render.angles[0] = pitch;
1012                         //ent->render.angles[1] = yaw;
1013                         //ent->render.angles[2] = rand()%360;
1014                         Matrix4x4_CreateFromQuakeEntity(&ent->render.matrix, org[0], org[1], org[2], -pitch, yaw, lhrandom(0, 360), 1);
1015                         Matrix4x4_Invert_Simple(&ent->render.inversematrix, &ent->render.matrix);
1016                         CL_BoundingBoxForEntity(&ent->render);
1017                         VectorMA(org, 30, dist, org);
1018                         d -= 30;
1019                 }
1020         }
1021 }
1022
1023 cvar_t r_lightningbeam_thickness = {CVAR_SAVE, "r_lightningbeam_thickness", "4"};
1024 cvar_t r_lightningbeam_scroll = {CVAR_SAVE, "r_lightningbeam_scroll", "5"};
1025 cvar_t r_lightningbeam_repeatdistance = {CVAR_SAVE, "r_lightningbeam_repeatdistance", "1024"};
1026 cvar_t r_lightningbeam_color_red = {CVAR_SAVE, "r_lightningbeam_color_red", "1"};
1027 cvar_t r_lightningbeam_color_green = {CVAR_SAVE, "r_lightningbeam_color_green", "1"};
1028 cvar_t r_lightningbeam_color_blue = {CVAR_SAVE, "r_lightningbeam_color_blue", "1"};
1029 cvar_t r_lightningbeam_qmbtexture = {CVAR_SAVE, "r_lightningbeam_qmbtexture", "0"};
1030
1031 rtexture_t *r_lightningbeamtexture;
1032 rtexture_t *r_lightningbeamqmbtexture;
1033 rtexturepool_t *r_lightningbeamtexturepool;
1034
1035 int r_lightningbeamelements[18] = {0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11};
1036
1037 void r_lightningbeams_start(void)
1038 {
1039         r_lightningbeamtexturepool = R_AllocTexturePool();
1040         r_lightningbeamtexture = NULL;
1041         r_lightningbeamqmbtexture = NULL;
1042 }
1043
1044 void r_lightningbeams_setupqmbtexture(void)
1045 {
1046         r_lightningbeamqmbtexture = loadtextureimage(r_lightningbeamtexturepool, "textures/particles/lightning.pcx", 0, 0, false, TEXF_ALPHA | TEXF_PRECACHE);
1047         if (r_lightningbeamqmbtexture == NULL)
1048                 Cvar_SetValueQuick(&r_lightningbeam_qmbtexture, false);
1049 }
1050
1051 void r_lightningbeams_setuptexture(void)
1052 {
1053 #if 0
1054 #define BEAMWIDTH 128
1055 #define BEAMHEIGHT 64
1056 #define PATHPOINTS 8
1057         int i, j, px, py, nearestpathindex, imagenumber;
1058         float particlex, particley, particlexv, particleyv, dx, dy, s, maxpathstrength;
1059         qbyte *pixels;
1060         int *image;
1061         struct {float x, y, strength;} path[PATHPOINTS], temppath;
1062
1063         image = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
1064         pixels = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * sizeof(qbyte[4]));
1065
1066         for (imagenumber = 0, maxpathstrength = 0.0339476;maxpathstrength < 0.5;imagenumber++, maxpathstrength += 0.01)
1067         {
1068         for (i = 0;i < PATHPOINTS;i++)
1069         {
1070                 path[i].x = lhrandom(0, 1);
1071                 path[i].y = lhrandom(0.2, 0.8);
1072                 path[i].strength = lhrandom(0, 1);
1073         }
1074         for (i = 0;i < PATHPOINTS;i++)
1075         {
1076                 for (j = i + 1;j < PATHPOINTS;j++)
1077                 {
1078                         if (path[j].x < path[i].x)
1079                         {
1080                                 temppath = path[j];
1081                                 path[j] = path[i];
1082                                 path[i] = temppath;
1083                         }
1084                 }
1085         }
1086         particlex = path[0].x;
1087         particley = path[0].y;
1088         particlexv = lhrandom(0, 0.02);
1089         particlexv = lhrandom(-0.02, 0.02);
1090         memset(image, 0, BEAMWIDTH * BEAMHEIGHT * sizeof(int));
1091         for (i = 0;i < 65536;i++)
1092         {
1093                 for (nearestpathindex = 0;nearestpathindex < PATHPOINTS;nearestpathindex++)
1094                         if (path[nearestpathindex].x > particlex)
1095                                 break;
1096                 nearestpathindex %= PATHPOINTS;
1097                 dx = path[nearestpathindex].x + lhrandom(-0.01, 0.01);dx = bound(0, dx, 1) - particlex;if (dx < 0) dx += 1;
1098                 dy = path[nearestpathindex].y + lhrandom(-0.01, 0.01);dy = bound(0, dy, 1) - particley;
1099                 s = path[nearestpathindex].strength / sqrt(dx*dx+dy*dy);
1100                 particlexv = particlexv /* (1 - lhrandom(0.08, 0.12))*/ + dx * s;
1101                 particleyv = particleyv /* (1 - lhrandom(0.08, 0.12))*/ + dy * s;
1102                 particlex += particlexv * maxpathstrength;particlex -= (int) particlex;
1103                 particley += particleyv * maxpathstrength;particley = bound(0, particley, 1);
1104                 px = particlex * BEAMWIDTH;
1105                 py = particley * BEAMHEIGHT;
1106                 if (px >= 0 && py >= 0 && px < BEAMWIDTH && py < BEAMHEIGHT)
1107                         image[py*BEAMWIDTH+px] += 16;
1108         }
1109
1110         for (py = 0;py < BEAMHEIGHT;py++)
1111         {
1112                 for (px = 0;px < BEAMWIDTH;px++)
1113                 {
1114                         pixels[(py*BEAMWIDTH+px)*4+0] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1115                         pixels[(py*BEAMWIDTH+px)*4+1] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1116                         pixels[(py*BEAMWIDTH+px)*4+2] = bound(0, image[py*BEAMWIDTH+px] * 1.0f, 255.0f);
1117                         pixels[(py*BEAMWIDTH+px)*4+3] = 255;
1118                 }
1119         }
1120
1121         Image_WriteTGARGBA(va("lightningbeam%i.tga", imagenumber), BEAMWIDTH, BEAMHEIGHT, pixels);
1122         }
1123
1124         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, pixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
1125
1126         Mem_Free(pixels);
1127         Mem_Free(image);
1128 #else
1129 #define BEAMWIDTH 64
1130 #define BEAMHEIGHT 128
1131         float r, g, b, intensity, fx, width, center;
1132         int x, y;
1133         qbyte *data, *noise1, *noise2;
1134
1135         data = Mem_Alloc(tempmempool, BEAMWIDTH * BEAMHEIGHT * 4);
1136         noise1 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
1137         noise2 = Mem_Alloc(tempmempool, BEAMHEIGHT * BEAMHEIGHT);
1138         fractalnoise(noise1, BEAMHEIGHT, BEAMHEIGHT / 8);
1139         fractalnoise(noise2, BEAMHEIGHT, BEAMHEIGHT / 16);
1140
1141         for (y = 0;y < BEAMHEIGHT;y++)
1142         {
1143                 width = 0.15;//((noise1[y * BEAMHEIGHT] * (1.0f / 256.0f)) * 0.1f + 0.1f);
1144                 center = (noise1[y * BEAMHEIGHT + (BEAMHEIGHT / 2)] / 256.0f) * (1.0f - width * 2.0f) + width;
1145                 for (x = 0;x < BEAMWIDTH;x++, fx++)
1146                 {
1147                         fx = (((float) x / BEAMWIDTH) - center) / width;
1148                         intensity = 1.0f - sqrt(fx * fx);
1149                         if (intensity > 0)
1150                                 intensity = pow(intensity, 2) * ((noise2[y * BEAMHEIGHT + x] * (1.0f / 256.0f)) * 0.33f + 0.66f);
1151                         intensity = bound(0, intensity, 1);
1152                         r = intensity * 1.0f;
1153                         g = intensity * 1.0f;
1154                         b = intensity * 1.0f;
1155                         data[(y * BEAMWIDTH + x) * 4 + 0] = (qbyte)(bound(0, r, 1) * 255.0f);
1156                         data[(y * BEAMWIDTH + x) * 4 + 1] = (qbyte)(bound(0, g, 1) * 255.0f);
1157                         data[(y * BEAMWIDTH + x) * 4 + 2] = (qbyte)(bound(0, b, 1) * 255.0f);
1158                         data[(y * BEAMWIDTH + x) * 4 + 3] = (qbyte)255;
1159                 }
1160         }
1161
1162         r_lightningbeamtexture = R_LoadTexture2D(r_lightningbeamtexturepool, "lightningbeam", BEAMWIDTH, BEAMHEIGHT, data, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
1163         Mem_Free(noise1);
1164         Mem_Free(noise2);
1165         Mem_Free(data);
1166 #endif
1167 }
1168
1169 void r_lightningbeams_shutdown(void)
1170 {
1171         r_lightningbeamtexture = NULL;
1172         r_lightningbeamqmbtexture = NULL;
1173         R_FreeTexturePool(&r_lightningbeamtexturepool);
1174 }
1175
1176 void r_lightningbeams_newmap(void)
1177 {
1178 }
1179
1180 void R_LightningBeams_Init(void)
1181 {
1182         Cvar_RegisterVariable(&r_lightningbeam_thickness);
1183         Cvar_RegisterVariable(&r_lightningbeam_scroll);
1184         Cvar_RegisterVariable(&r_lightningbeam_repeatdistance);
1185         Cvar_RegisterVariable(&r_lightningbeam_color_red);
1186         Cvar_RegisterVariable(&r_lightningbeam_color_green);
1187         Cvar_RegisterVariable(&r_lightningbeam_color_blue);
1188         Cvar_RegisterVariable(&r_lightningbeam_qmbtexture);
1189         R_RegisterModule("R_LightningBeams", r_lightningbeams_start, r_lightningbeams_shutdown, r_lightningbeams_newmap);
1190 }
1191
1192 void R_CalcLightningBeamPolygonVertex3f(float *v, const float *start, const float *end, const float *offset)
1193 {
1194         // near right corner
1195         VectorAdd     (start, offset, (v + 0));
1196         // near left corner
1197         VectorSubtract(start, offset, (v + 3));
1198         // far left corner
1199         VectorSubtract(end  , offset, (v + 6));
1200         // far right corner
1201         VectorAdd     (end  , offset, (v + 9));
1202 }
1203
1204 void R_CalcLightningBeamPolygonTexCoord2f(float *tc, float t1, float t2)
1205 {
1206         if (r_lightningbeam_qmbtexture.integer)
1207         {
1208                 // near right corner
1209                 tc[0] = t1;tc[1] = 0;
1210                 // near left corner
1211                 tc[2] = t1;tc[3] = 1;
1212                 // far left corner
1213                 tc[4] = t2;tc[5] = 1;
1214                 // far right corner
1215                 tc[6] = t2;tc[7] = 0;
1216         }
1217         else
1218         {
1219                 // near right corner
1220                 tc[0] = 0;tc[1] = t1;
1221                 // near left corner
1222                 tc[2] = 1;tc[3] = t1;
1223                 // far left corner
1224                 tc[4] = 1;tc[5] = t2;
1225                 // far right corner
1226                 tc[6] = 0;tc[7] = t2;
1227         }
1228 }
1229
1230 void R_FogLightningBeam_Vertex3f_Color4f(const float *v, float *c, int numverts, float r, float g, float b, float a)
1231 {
1232         int i;
1233         vec3_t fogvec;
1234         float ifog;
1235         for (i = 0;i < numverts;i++, v += 3, c += 4)
1236         {
1237                 VectorSubtract(v, r_origin, fogvec);
1238                 ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
1239                 c[0] = r * ifog;
1240                 c[1] = g * ifog;
1241                 c[2] = b * ifog;
1242                 c[3] = a;
1243         }
1244 }
1245
1246 float beamrepeatscale;
1247
1248 void R_DrawLightningBeamCallback(const void *calldata1, int calldata2)
1249 {
1250         const beam_t *b = calldata1;
1251         rmeshstate_t m;
1252         vec3_t beamdir, right, up, offset;
1253         float length, t1, t2;
1254
1255         R_Mesh_Matrix(&r_identitymatrix);
1256
1257         // calculate beam direction (beamdir) vector and beam length
1258         // get difference vector
1259         VectorSubtract(b->end, b->start, beamdir);
1260         // find length of difference vector
1261         length = sqrt(DotProduct(beamdir, beamdir));
1262         // calculate scale to make beamdir a unit vector (normalized)
1263         t1 = 1.0f / length;
1264         // scale beamdir so it is now normalized
1265         VectorScale(beamdir, t1, beamdir);
1266
1267         // calculate up vector such that it points toward viewer, and rotates around the beamdir
1268         // get direction from start of beam to viewer
1269         VectorSubtract(r_origin, b->start, up);
1270         // remove the portion of the vector that moves along the beam
1271         // (this leaves only a vector pointing directly away from the beam)
1272         t1 = -DotProduct(up, beamdir);
1273         VectorMA(up, t1, beamdir, up);
1274         // now we have a vector pointing away from the beam, now we need to normalize it
1275         VectorNormalizeFast(up);
1276         // generate right vector from forward and up, the result is already normalized
1277         // (CrossProduct returns a vector of multiplied length of the two inputs)
1278         CrossProduct(beamdir, up, right);
1279
1280         // calculate T coordinate scrolling (start and end texcoord along the beam)
1281         t1 = cl.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(b->start, beamdir);
1282         t1 = t1 - (int) t1;
1283         t2 = t1 + beamrepeatscale * length;
1284
1285         // the beam is 3 polygons in this configuration:
1286         //  *   2
1287         //   * *
1288         // 1******
1289         //   * *
1290         //  *   3
1291         // they are showing different portions of the beam texture, creating an
1292         // illusion of a beam that appears to curl around in 3D space
1293         // (and realize that the whole polygon assembly orients itself to face
1294         //  the viewer)
1295
1296         memset(&m, 0, sizeof(m));
1297         if (r_lightningbeam_qmbtexture.integer)
1298                 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
1299         else
1300                 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
1301         m.pointer_texcoord[0] = varray_texcoord2f[0];
1302         R_Mesh_State_Texture(&m);
1303
1304         GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
1305         GL_DepthMask(false);
1306         GL_DepthTest(true);
1307         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
1308                 r_lightningbeams_setupqmbtexture();
1309         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
1310                 r_lightningbeams_setuptexture();
1311
1312         // polygon 1, verts 0-3
1313         VectorScale(right, r_lightningbeam_thickness.value, offset);
1314         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 0, b->start, b->end, offset);
1315         // polygon 2, verts 4-7
1316         VectorAdd(right, up, offset);
1317         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1318         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 12, b->start, b->end, offset);
1319         // polygon 3, verts 8-11
1320         VectorSubtract(right, up, offset);
1321         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1322         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 24, b->start, b->end, offset);
1323         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 0, t1, t2);
1324         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 8, t1 + 0.33, t2 + 0.33);
1325         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 16, t1 + 0.66, t2 + 0.66);
1326         GL_VertexPointer(varray_vertex3f);
1327
1328         if (fogenabled)
1329         {
1330                 // per vertex colors if fog is used
1331                 GL_ColorPointer(varray_color4f);
1332                 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);
1333         }
1334         else
1335         {
1336                 // solid color if fog is not used
1337                 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1338         }
1339
1340         // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
1341         R_Mesh_Draw(12, 6, r_lightningbeamelements);
1342 }
1343
1344 void R_DrawLightningBeams (void)
1345 {
1346         int i;
1347         beam_t *b;
1348         vec3_t org;
1349
1350         if (!cl_beams_polygons.integer)
1351                 return;
1352
1353         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
1354         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1355         {
1356                 if (b->model && b->endtime >= cl.time && b->lightning)
1357                 {
1358                         VectorAdd(b->start, b->end, org);
1359                         VectorScale(org, 0.5f, org);
1360                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
1361                 }
1362         }
1363 }
1364
1365
1366 void CL_LerpPlayer(float frac)
1367 {
1368         int i;
1369         float d;
1370
1371         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1372
1373         for (i = 0;i < 3;i++)
1374                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1375
1376         if (cls.demoplayback)
1377         {
1378                 // interpolate the angles
1379                 for (i = 0;i < 3;i++)
1380                 {
1381                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1382                         if (d > 180)
1383                                 d -= 360;
1384                         else if (d < -180)
1385                                 d += 360;
1386                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1387                 }
1388         }
1389 }
1390
1391 void CL_RelinkEntities (void)
1392 {
1393         float frac;
1394
1395         // fraction from previous network update to current
1396         frac = CL_LerpPoint();
1397
1398         CL_ClearTempEntities();
1399         CL_DecayLights();
1400         CL_RelinkWorld();
1401         CL_RelinkStaticEntities();
1402         CL_RelinkNetworkEntities();
1403         CL_RelinkEffects();
1404         CL_MoveParticles();
1405
1406         CL_LerpPlayer(frac);
1407
1408         CL_RelinkBeams();
1409 }
1410
1411
1412 /*
1413 ===============
1414 CL_ReadFromServer
1415
1416 Read all incoming data from the server
1417 ===============
1418 */
1419 int CL_ReadFromServer(void)
1420 {
1421         CL_ReadDemoMessage();
1422
1423         r_refdef.numentities = 0;
1424         cl_num_entities = 0;
1425         cl_num_brushmodel_entities = 0;
1426
1427         if (cls.state == ca_connected && cls.signon == SIGNONS)
1428         {
1429                 CL_RelinkEntities();
1430
1431                 // run cgame code (which can add more entities)
1432                 CL_CGVM_Frame();
1433         }
1434
1435         return 0;
1436 }
1437
1438 /*
1439 =================
1440 CL_SendCmd
1441 =================
1442 */
1443 void CL_SendCmd(void)
1444 {
1445         usercmd_t cmd;
1446
1447         if (cls.signon == SIGNONS)
1448         {
1449                 // get basic movement from keyboard
1450                 CL_BaseMove(&cmd);
1451
1452                 // OS independent code
1453                 IN_PreMove();
1454
1455                 // allow mice or other external controllers to add to the move
1456                 IN_Move(&cmd);
1457
1458                 // OS independent code
1459                 IN_PostMove();
1460
1461                 // send the unreliable message
1462                 CL_SendMove(&cmd);
1463         }
1464
1465         if (cls.demoplayback)
1466         {
1467                 SZ_Clear(&cls.message);
1468                 return;
1469         }
1470
1471         // send the reliable message (forwarded commands) if there is one
1472         if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1473         {
1474                 if (developer.integer)
1475                 {
1476                         Con_Printf("CL_SendCmd: sending reliable message:\n");
1477                         SZ_HexDumpToConsole(&cls.message);
1478                 }
1479                 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1480                         Host_Error("CL_WriteToServer: lost server connection");
1481                 SZ_Clear(&cls.message);
1482         }
1483 }
1484
1485 // LordHavoc: pausedemo command
1486 static void CL_PauseDemo_f (void)
1487 {
1488         cls.demopaused = !cls.demopaused;
1489         if (cls.demopaused)
1490                 Con_Printf("Demo paused\n");
1491         else
1492                 Con_Printf("Demo unpaused\n");
1493 }
1494
1495 /*
1496 ======================
1497 CL_Fog_f
1498 ======================
1499 */
1500 static void CL_Fog_f (void)
1501 {
1502         if (Cmd_Argc () == 1)
1503         {
1504                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1505                 return;
1506         }
1507         fog_density = atof(Cmd_Argv(1));
1508         fog_red = atof(Cmd_Argv(2));
1509         fog_green = atof(Cmd_Argv(3));
1510         fog_blue = atof(Cmd_Argv(4));
1511 }
1512
1513 /*
1514 =================
1515 CL_Init
1516 =================
1517 */
1518 void CL_Init (void)
1519 {
1520         cl_entities_mempool = Mem_AllocPool("client entities");
1521         cl_refdef_mempool = Mem_AllocPool("refdef");
1522
1523         memset(&r_refdef, 0, sizeof(r_refdef));
1524         // max entities sent to renderer per frame
1525         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1526         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1527         // 256k drawqueue buffer
1528         r_refdef.maxdrawqueuesize = 256 * 1024;
1529         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1530
1531         SZ_Alloc (&cls.message, 1024, "cls.message");
1532
1533         CL_InitInput ();
1534         CL_InitTEnts ();
1535
1536 //
1537 // register our commands
1538 //
1539         Cvar_RegisterVariable (&cl_upspeed);
1540         Cvar_RegisterVariable (&cl_forwardspeed);
1541         Cvar_RegisterVariable (&cl_backspeed);
1542         Cvar_RegisterVariable (&cl_sidespeed);
1543         Cvar_RegisterVariable (&cl_movespeedkey);
1544         Cvar_RegisterVariable (&cl_yawspeed);
1545         Cvar_RegisterVariable (&cl_pitchspeed);
1546         Cvar_RegisterVariable (&cl_anglespeedkey);
1547         Cvar_RegisterVariable (&cl_shownet);
1548         Cvar_RegisterVariable (&cl_nolerp);
1549         Cvar_RegisterVariable (&lookspring);
1550         Cvar_RegisterVariable (&lookstrafe);
1551         Cvar_RegisterVariable (&sensitivity);
1552         Cvar_RegisterVariable (&freelook);
1553
1554         Cvar_RegisterVariable (&m_pitch);
1555         Cvar_RegisterVariable (&m_yaw);
1556         Cvar_RegisterVariable (&m_forward);
1557         Cvar_RegisterVariable (&m_side);
1558
1559         Cvar_RegisterVariable (&cl_itembobspeed);
1560         Cvar_RegisterVariable (&cl_itembobheight);
1561
1562         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1563         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1564         Cmd_AddCommand ("record", CL_Record_f);
1565         Cmd_AddCommand ("stop", CL_Stop_f);
1566         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1567         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1568
1569         Cmd_AddCommand ("fog", CL_Fog_f);
1570
1571         // LordHavoc: added pausedemo
1572         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1573
1574         Cvar_RegisterVariable(&r_draweffects);
1575         Cvar_RegisterVariable(&cl_explosions);
1576         Cvar_RegisterVariable(&cl_stainmaps);
1577         Cvar_RegisterVariable(&cl_beams_polygons);
1578         Cvar_RegisterVariable(&cl_beams_relative);
1579         Cvar_RegisterVariable(&cl_beams_lightatend);
1580         Cvar_RegisterVariable(&cl_noplayershadow);
1581
1582         R_LightningBeams_Init();
1583
1584         CL_Parse_Init();
1585         CL_Particles_Init();
1586         CL_Screen_Init();
1587         CL_CGVM_Init();
1588
1589         CL_Video_Init();
1590 }
1591