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