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