]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - cl_main.c
This is the network rewrite I've been working on for over a week; multiplayer should...
[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         Mod_FindNonSolidLocation(org, dl->origin, cl.worldmodel, 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         memset(&m, 0, sizeof(m));
1255         m.blendfunc1 = GL_SRC_ALPHA;
1256         m.blendfunc2 = GL_ONE;
1257         if (r_lightningbeam_qmbtexture.integer && r_lightningbeamqmbtexture == NULL)
1258                 r_lightningbeams_setupqmbtexture();
1259         if (!r_lightningbeam_qmbtexture.integer && r_lightningbeamtexture == NULL)
1260                 r_lightningbeams_setuptexture();
1261         if (r_lightningbeam_qmbtexture.integer)
1262                 m.tex[0] = R_GetTexture(r_lightningbeamqmbtexture);
1263         else
1264                 m.tex[0] = R_GetTexture(r_lightningbeamtexture);
1265         R_Mesh_State(&m);
1266         R_Mesh_Matrix(&r_identitymatrix);
1267
1268         // calculate beam direction (beamdir) vector and beam length
1269         // get difference vector
1270         VectorSubtract(b->end, b->start, beamdir);
1271         // find length of difference vector
1272         length = sqrt(DotProduct(beamdir, beamdir));
1273         // calculate scale to make beamdir a unit vector (normalized)
1274         t1 = 1.0f / length;
1275         // scale beamdir so it is now normalized
1276         VectorScale(beamdir, t1, beamdir);
1277
1278         // calculate up vector such that it points toward viewer, and rotates around the beamdir
1279         // get direction from start of beam to viewer
1280         VectorSubtract(r_origin, b->start, up);
1281         // remove the portion of the vector that moves along the beam
1282         // (this leaves only a vector pointing directly away from the beam)
1283         t1 = -DotProduct(up, beamdir);
1284         VectorMA(up, t1, beamdir, up);
1285         // now we have a vector pointing away from the beam, now we need to normalize it
1286         VectorNormalizeFast(up);
1287         // generate right vector from forward and up, the result is already normalized
1288         // (CrossProduct returns a vector of multiplied length of the two inputs)
1289         CrossProduct(beamdir, up, right);
1290
1291         // calculate T coordinate scrolling (start and end texcoord along the beam)
1292         t1 = cl.time * -r_lightningbeam_scroll.value;// + beamrepeatscale * DotProduct(b->start, beamdir);
1293         t1 = t1 - (int) t1;
1294         t2 = t1 + beamrepeatscale * length;
1295
1296         // the beam is 3 polygons in this configuration:
1297         //  *   2
1298         //   * *
1299         // 1******
1300         //   * *
1301         //  *   3
1302         // they are showing different portions of the beam texture, creating an
1303         // illusion of a beam that appears to curl around in 3D space
1304         // (and realize that the whole polygon assembly orients itself to face
1305         //  the viewer)
1306
1307         R_Mesh_GetSpace(12);
1308
1309         // polygon 1, verts 0-3
1310         VectorScale(right, r_lightningbeam_thickness.value, offset);
1311         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f, b->start, b->end, offset);
1312         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0], t1, t2);
1313
1314         // polygon 2, verts 4-7
1315         VectorAdd(right, up, offset);
1316         VectorScale(offset, r_lightningbeam_thickness.value * 0.70710681f, offset);
1317         R_CalcLightningBeamPolygonVertex3f(varray_vertex3f + 12, b->start, b->end, offset);
1318         R_CalcLightningBeamPolygonTexCoord2f(varray_texcoord2f[0] + 8, t1 + 0.33, t2 + 0.33);
1319
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] + 16, t1 + 0.66, t2 + 0.66);
1325
1326         if (fogenabled)
1327         {
1328                 // per vertex colors if fog is used
1329                 GL_UseColorArray();
1330                 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);
1331         }
1332         else
1333         {
1334                 // solid color if fog is not used
1335                 GL_Color(r_lightningbeam_color_red.value, r_lightningbeam_color_green.value, r_lightningbeam_color_blue.value, 1);
1336         }
1337
1338         // draw the 3 polygons as one batch of 6 triangles using the 12 vertices
1339         R_Mesh_Draw(12, 6, r_lightningbeamelements);
1340 }
1341
1342 void R_DrawLightningBeams (void)
1343 {
1344         int i;
1345         beam_t *b;
1346         vec3_t org;
1347
1348         if (!cl_beams_polygons.integer)
1349                 return;
1350
1351         beamrepeatscale = 1.0f / r_lightningbeam_repeatdistance.value;
1352         for (i = 0, b = cl_beams;i < cl_max_beams;i++, b++)
1353         {
1354                 if (b->model && b->endtime >= cl.time && b->lightning)
1355                 {
1356                         VectorAdd(b->start, b->end, org);
1357                         VectorScale(org, 0.5f, org);
1358                         R_MeshQueue_AddTransparent(org, R_DrawLightningBeamCallback, b, 0);
1359                 }
1360         }
1361 }
1362
1363
1364 void CL_LerpPlayer(float frac)
1365 {
1366         int i;
1367         float d;
1368
1369         cl.viewzoom = cl.viewzoomold + frac * (cl.viewzoomnew - cl.viewzoomold);
1370
1371         for (i = 0;i < 3;i++)
1372                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
1373
1374         if (cls.demoplayback)
1375         {
1376                 // interpolate the angles
1377                 for (i = 0;i < 3;i++)
1378                 {
1379                         d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
1380                         if (d > 180)
1381                                 d -= 360;
1382                         else if (d < -180)
1383                                 d += 360;
1384                         cl.viewangles[i] = cl.mviewangles[1][i] + frac * d;
1385                 }
1386         }
1387 }
1388
1389 void CL_RelinkEntities (void)
1390 {
1391         float frac;
1392
1393         // fraction from previous network update to current
1394         frac = CL_LerpPoint();
1395
1396         CL_ClearTempEntities();
1397         CL_DecayLights();
1398         CL_RelinkWorld();
1399         CL_RelinkStaticEntities();
1400         CL_RelinkNetworkEntities();
1401         CL_RelinkEffects();
1402         CL_MoveParticles();
1403
1404         CL_LerpPlayer(frac);
1405
1406         CL_RelinkBeams();
1407 }
1408
1409
1410 /*
1411 ===============
1412 CL_ReadFromServer
1413
1414 Read all incoming data from the server
1415 ===============
1416 */
1417 int CL_ReadFromServer(void)
1418 {
1419         CL_ReadDemoMessage();
1420
1421         r_refdef.numentities = 0;
1422         cl_num_entities = 0;
1423         cl_num_brushmodel_entities = 0;
1424
1425         if (cls.state == ca_connected && cls.signon == SIGNONS)
1426         {
1427                 CL_RelinkEntities();
1428
1429                 // run cgame code (which can add more entities)
1430                 CL_CGVM_Frame();
1431         }
1432
1433         return 0;
1434 }
1435
1436 /*
1437 =================
1438 CL_SendCmd
1439 =================
1440 */
1441 void CL_SendCmd(void)
1442 {
1443         usercmd_t cmd;
1444
1445         if (cls.signon == SIGNONS)
1446         {
1447                 // get basic movement from keyboard
1448                 CL_BaseMove(&cmd);
1449
1450                 // OS independent code
1451                 IN_PreMove();
1452
1453                 // allow mice or other external controllers to add to the move
1454                 IN_Move(&cmd);
1455
1456                 // OS independent code
1457                 IN_PostMove();
1458
1459                 // send the unreliable message
1460                 CL_SendMove(&cmd);
1461         }
1462
1463         if (cls.demoplayback)
1464         {
1465                 SZ_Clear(&cls.message);
1466                 return;
1467         }
1468
1469         // send the reliable message (forwarded commands) if there is one
1470         if (cls.message.cursize && NetConn_CanSendMessage(cls.netcon))
1471         {
1472                 if (developer.integer)
1473                 {
1474                         Con_Printf("CL_SendCmd: sending reliable message:\n");
1475                         SZ_HexDumpToConsole(&cls.message);
1476                 }
1477                 if (NetConn_SendReliableMessage(cls.netcon, &cls.message) == -1)
1478                         Host_Error("CL_WriteToServer: lost server connection");
1479                 SZ_Clear(&cls.message);
1480         }
1481 }
1482
1483 // LordHavoc: pausedemo command
1484 static void CL_PauseDemo_f (void)
1485 {
1486         cls.demopaused = !cls.demopaused;
1487         if (cls.demopaused)
1488                 Con_Printf("Demo paused\n");
1489         else
1490                 Con_Printf("Demo unpaused\n");
1491 }
1492
1493 /*
1494 ======================
1495 CL_Fog_f
1496 ======================
1497 */
1498 static void CL_Fog_f (void)
1499 {
1500         if (Cmd_Argc () == 1)
1501         {
1502                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
1503                 return;
1504         }
1505         fog_density = atof(Cmd_Argv(1));
1506         fog_red = atof(Cmd_Argv(2));
1507         fog_green = atof(Cmd_Argv(3));
1508         fog_blue = atof(Cmd_Argv(4));
1509 }
1510
1511 /*
1512 =================
1513 CL_Init
1514 =================
1515 */
1516 void CL_Init (void)
1517 {
1518         cl_entities_mempool = Mem_AllocPool("client entities");
1519         cl_refdef_mempool = Mem_AllocPool("refdef");
1520
1521         memset(&r_refdef, 0, sizeof(r_refdef));
1522         // max entities sent to renderer per frame
1523         r_refdef.maxentities = MAX_EDICTS + 256 + 512;
1524         r_refdef.entities = Mem_Alloc(cl_refdef_mempool, sizeof(entity_render_t *) * r_refdef.maxentities);
1525         // 256k drawqueue buffer
1526         r_refdef.maxdrawqueuesize = 256 * 1024;
1527         r_refdef.drawqueue = Mem_Alloc(cl_refdef_mempool, r_refdef.maxdrawqueuesize);
1528
1529         SZ_Alloc (&cls.message, 1024, "cls.message");
1530
1531         CL_InitInput ();
1532         CL_InitTEnts ();
1533
1534 //
1535 // register our commands
1536 //
1537         Cvar_RegisterVariable (&cl_upspeed);
1538         Cvar_RegisterVariable (&cl_forwardspeed);
1539         Cvar_RegisterVariable (&cl_backspeed);
1540         Cvar_RegisterVariable (&cl_sidespeed);
1541         Cvar_RegisterVariable (&cl_movespeedkey);
1542         Cvar_RegisterVariable (&cl_yawspeed);
1543         Cvar_RegisterVariable (&cl_pitchspeed);
1544         Cvar_RegisterVariable (&cl_anglespeedkey);
1545         Cvar_RegisterVariable (&cl_shownet);
1546         Cvar_RegisterVariable (&cl_nolerp);
1547         Cvar_RegisterVariable (&lookspring);
1548         Cvar_RegisterVariable (&lookstrafe);
1549         Cvar_RegisterVariable (&sensitivity);
1550         Cvar_RegisterVariable (&freelook);
1551
1552         Cvar_RegisterVariable (&m_pitch);
1553         Cvar_RegisterVariable (&m_yaw);
1554         Cvar_RegisterVariable (&m_forward);
1555         Cvar_RegisterVariable (&m_side);
1556
1557         Cvar_RegisterVariable (&cl_itembobspeed);
1558         Cvar_RegisterVariable (&cl_itembobheight);
1559
1560         Cmd_AddCommand ("entities", CL_PrintEntities_f);
1561         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
1562         Cmd_AddCommand ("record", CL_Record_f);
1563         Cmd_AddCommand ("stop", CL_Stop_f);
1564         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
1565         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
1566
1567         Cmd_AddCommand ("fog", CL_Fog_f);
1568
1569         // LordHavoc: added pausedemo
1570         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
1571
1572         Cvar_RegisterVariable(&r_draweffects);
1573         Cvar_RegisterVariable(&cl_explosions);
1574         Cvar_RegisterVariable(&cl_stainmaps);
1575         Cvar_RegisterVariable(&cl_beams_polygons);
1576         Cvar_RegisterVariable(&cl_beams_relative);
1577         Cvar_RegisterVariable(&cl_beams_lightatend);
1578         Cvar_RegisterVariable(&cl_noplayershadow);
1579
1580         R_LightningBeams_Init();
1581
1582         CL_Parse_Init();
1583         CL_Particles_Init();
1584         CL_Screen_Init();
1585         CL_CGVM_Init();
1586
1587         CL_Video_Init();
1588 }
1589