56a5a779681a7d48c0b7cd3222432a0860673952
[xonotic/darkplaces.git] / cl_main.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20 // cl_main.c  -- client main loop
21
22 #include "quakedef.h"
23
24 // we need to declare some mouse variables here, because the menu system
25 // references them even when on a unix system.
26
27 // these two are not intended to be set directly
28 cvar_t  cl_name = {"_cl_name", "player", true};
29 cvar_t  cl_color = {"_cl_color", "0", true};
30 cvar_t  cl_pmodel = {"_cl_pmodel", "0", true};
31
32 cvar_t  cl_shownet = {"cl_shownet","0"};        // can be 0, 1, or 2
33 cvar_t  cl_nolerp = {"cl_nolerp","0"};
34
35 cvar_t  lookspring = {"lookspring","0", true};
36 cvar_t  lookstrafe = {"lookstrafe","0", true};
37 cvar_t  sensitivity = {"sensitivity","3", true};
38
39 cvar_t  m_pitch = {"m_pitch","0.022", true};
40 cvar_t  m_yaw = {"m_yaw","0.022", true};
41 cvar_t  m_forward = {"m_forward","1", true};
42 cvar_t  m_side = {"m_side","0.8", true};
43
44
45 client_static_t cls;
46 client_state_t  cl;
47 // FIXME: put these on hunk?
48 //efrag_t                       cl_efrags[MAX_EFRAGS];
49 entity_t                cl_entities[MAX_EDICTS];
50 entity_t                cl_static_entities[MAX_STATIC_ENTITIES];
51 lightstyle_t    cl_lightstyle[MAX_LIGHTSTYLES];
52 dlight_t                cl_dlights[MAX_DLIGHTS];
53
54 int                             cl_numvisedicts;
55 entity_t                *cl_visedicts[MAX_VISEDICTS];
56
57 /*
58 =====================
59 CL_ClearState
60
61 =====================
62 */
63 void CL_ClearState (void)
64 {
65         int                     i;
66
67         if (!sv.active)
68                 Host_ClearMemory ();
69
70 // wipe the entire cl structure
71         memset (&cl, 0, sizeof(cl));
72
73         SZ_Clear (&cls.message);
74
75 // clear other arrays   
76 //      memset (cl_efrags, 0, sizeof(cl_efrags));
77         memset (cl_entities, 0, sizeof(cl_entities));
78         memset (cl_dlights, 0, sizeof(cl_dlights));
79         memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
80         memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
81         memset (cl_beams, 0, sizeof(cl_beams));
82         // LordHavoc: have to set up the baseline info for alpha and other stuff
83         for (i = 0;i < MAX_EDICTS;i++)
84         {
85                 cl_entities[i].state_baseline.alpha = 255;
86                 cl_entities[i].state_baseline.scale = 16;
87                 cl_entities[i].state_baseline.glowsize = 0;
88                 cl_entities[i].state_baseline.glowcolor = 254;
89                 cl_entities[i].state_baseline.colormod = 255;
90         }
91
92 ////
93 //// allocate the efrags and chain together into a free list
94 ////
95 //      cl.free_efrags = cl_efrags;
96 //      for (i=0 ; i<MAX_EFRAGS-1 ; i++)
97 //              cl.free_efrags[i].entnext = &cl.free_efrags[i+1];
98 //      cl.free_efrags[i].entnext = NULL;
99 }
100
101 /*
102 =====================
103 CL_Disconnect
104
105 Sends a disconnect message to the server
106 This is also called on Host_Error, so it shouldn't cause any errors
107 =====================
108 */
109 void CL_Disconnect (void)
110 {
111 // stop sounds (especially looping!)
112         S_StopAllSounds (true);
113
114         // clear contents blends
115         cl.cshifts[0].percent = 0;
116         cl.cshifts[1].percent = 0;
117         cl.cshifts[2].percent = 0;
118         cl.cshifts[3].percent = 0;
119
120 // if running a local server, shut it down
121         if (cls.demoplayback)
122                 CL_StopPlayback ();
123         else if (cls.state == ca_connected)
124         {
125                 if (cls.demorecording)
126                         CL_Stop_f ();
127
128                 Con_DPrintf ("Sending clc_disconnect\n");
129                 SZ_Clear (&cls.message);
130                 MSG_WriteByte (&cls.message, clc_disconnect);
131                 NET_SendUnreliableMessage (cls.netcon, &cls.message);
132                 SZ_Clear (&cls.message);
133                 NET_Close (cls.netcon);
134
135                 cls.state = ca_disconnected;
136                 if (sv.active)
137                         Host_ShutdownServer(false);
138         }
139
140         cls.demoplayback = cls.timedemo = false;
141         cls.signon = 0;
142 }
143
144 void CL_Disconnect_f (void)
145 {
146         CL_Disconnect ();
147         if (sv.active)
148                 Host_ShutdownServer (false);
149 }
150
151
152
153
154 /*
155 =====================
156 CL_EstablishConnection
157
158 Host should be either "local" or a net address to be passed on
159 =====================
160 */
161 void CL_EstablishConnection (char *host)
162 {
163         if (cls.state == ca_dedicated)
164                 return;
165
166         if (cls.demoplayback)
167                 return;
168
169         CL_Disconnect ();
170
171         cls.netcon = NET_Connect (host);
172         if (!cls.netcon)
173                 Host_Error ("CL_Connect: connect failed\n");
174         Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
175         
176         cls.demonum = -1;                       // not in the demo loop now
177         cls.state = ca_connected;
178         cls.signon = 0;                         // need all the signon messages before playing
179 }
180
181 /*
182 =====================
183 CL_SignonReply
184
185 An svc_signonnum has been received, perform a client side setup
186 =====================
187 */
188 void CL_SignonReply (void)
189 {
190         char    str[8192];
191
192 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
193
194         switch (cls.signon)
195         {
196         case 1:
197                 MSG_WriteByte (&cls.message, clc_stringcmd);
198                 MSG_WriteString (&cls.message, "prespawn");
199                 break;
200                 
201         case 2:
202                 MSG_WriteByte (&cls.message, clc_stringcmd);
203                 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
204
205                 MSG_WriteByte (&cls.message, clc_stringcmd);
206                 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
207         
208                 if (cl_pmodel.value)
209                 {
210                         MSG_WriteByte (&cls.message, clc_stringcmd);
211                         MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
212                 }
213
214                 MSG_WriteByte (&cls.message, clc_stringcmd);
215                 sprintf (str, "spawn %s", cls.spawnparms);
216                 MSG_WriteString (&cls.message, str);
217                 break;
218                 
219         case 3: 
220                 MSG_WriteByte (&cls.message, clc_stringcmd);
221                 MSG_WriteString (&cls.message, "begin");
222                 Cache_Report ();                // print remaining memory
223                 break;
224                 
225         case 4:
226 //              SCR_EndLoadingPlaque ();                // allow normal screen updates
227                 Con_ClearNotify();
228                 break;
229         }
230 }
231
232 /*
233 =====================
234 CL_NextDemo
235
236 Called to play the next demo in the demo loop
237 =====================
238 */
239 void CL_NextDemo (void)
240 {
241         char    str[1024];
242
243         if (cls.demonum == -1)
244                 return;         // don't play demos
245
246 //      SCR_BeginLoadingPlaque ();
247
248         if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
249         {
250                 cls.demonum = 0;
251                 if (!cls.demos[cls.demonum][0])
252                 {
253                         Con_Printf ("No demos listed with startdemos\n");
254                         cls.demonum = -1;
255                         return;
256                 }
257         }
258
259         sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
260         Cbuf_InsertText (str);
261         cls.demonum++;
262 }
263
264 /*
265 ==============
266 CL_PrintEntities_f
267 ==============
268 */
269 void CL_PrintEntities_f (void)
270 {
271         entity_t        *ent;
272         int                     i;
273         
274         for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
275         {
276                 Con_Printf ("%3i:", i);
277                 if (!ent->render.model)
278                 {
279                         Con_Printf ("EMPTY\n");
280                         continue;
281                 }
282                 Con_Printf ("%s:%2i  (%5.1f,%5.1f,%5.1f) [%5.1f %5.1f %5.1f]\n", ent->render.model->name, ent->render.frame, ent->render.origin[0], ent->render.origin[1], ent->render.origin[2], ent->render.angles[0], ent->render.angles[1], ent->render.angles[2]);
283         }
284 }
285
286
287 /*
288 ===============
289 CL_AllocDlight
290
291 ===============
292 */
293 void CL_AllocDlight (entity_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
294 {
295         int             i;
296         dlight_t        *dl;
297
298 // first look for an exact key match
299         if (ent)
300         {
301                 dl = cl_dlights;
302                 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
303                         if (dl->ent == ent)
304                                 goto dlightsetup;
305         }
306
307 // then look for anything else
308         dl = cl_dlights;
309         for (i = 0;i < MAX_DLIGHTS;i++, dl++)
310                 if (!dl->radius)
311                         goto dlightsetup;
312
313         // unable to find one
314         return;
315
316 dlightsetup:
317         memset (dl, 0, sizeof(*dl));
318         dl->ent = ent;
319         VectorCopy(org, dl->origin);
320         dl->radius = radius;
321         dl->color[0] = red;
322         dl->color[1] = green;
323         dl->color[2] = blue;
324         dl->decay = decay;
325         dl->die = cl.time + lifetime;
326 }
327
328
329 /*
330 ===============
331 CL_DecayLights
332
333 ===============
334 */
335 void CL_DecayLights (void)
336 {
337         int                     i;
338         dlight_t        *dl;
339         float           time;
340         
341         time = cl.time - cl.oldtime;
342
343         c_dlights = 0;
344         dl = cl_dlights;
345         for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
346         {
347                 if (!dl->radius)
348                         continue;
349                 if (dl->die < cl.time)
350                 {
351                         dl->radius = 0;
352                         continue;
353                 }
354
355                 c_dlights++; // count every dlight in use
356
357                 dl->radius -= time*dl->decay;
358                 if (dl->radius < 0)
359                         dl->radius = 0;
360         }
361 }
362
363
364 /*
365 ===============
366 CL_LerpPoint
367
368 Determines the fraction between the last two messages that the objects
369 should be put at.
370 ===============
371 */
372 float   CL_LerpPoint (void)
373 {
374         float   f, frac;
375
376         f = cl.mtime[0] - cl.mtime[1];
377
378         // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
379         if (!f || cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
380         {
381                 cl.time = cl.mtime[0];
382                 return 1;
383         }
384                 
385         if (f > 0.1)
386         {       // dropped packet, or start of demo
387                 cl.mtime[1] = cl.mtime[0] - 0.1;
388                 f = 0.1;
389         }
390         frac = (cl.time - cl.mtime[1]) / f;
391 //      Con_Printf ("frac: %f\n",frac);
392         if (frac < 0)
393         {
394                 if (frac < -0.01)
395                 {
396                         cl.time = cl.mtime[1];
397 //                      Con_Printf ("low frac\n");
398                 }
399                 frac = 0;
400         }
401         else if (frac > 1)
402         {
403                 if (frac > 1.01)
404                 {
405                         cl.time = cl.mtime[0];
406 //                      Con_Printf ("high frac\n");
407                 }
408                 frac = 1;
409         }
410                 
411         return frac;
412 }
413
414 float CL_EntityLerpPoint (entity_t *ent)
415 {
416         float   f;
417
418         if (cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
419                 return 1;
420
421         f = ent->state_current.time - ent->state_previous.time;
422 //      Con_Printf(" %g-%g=%g", ent->state_current.time, ent->state_previous.time, f);
423
424         if (f <= 0)
425                 return 1;
426         if (f >= 0.1)
427                 f = 0.1;
428
429 //      Con_Printf(" %g-%g/%g=%f", cl.time, ent->state_previous.time, f, (cl.time - ent->state_previous.time) / f);
430         f = (cl.time - ent->state_previous.time) / f;
431         return bound(0, f, 1);
432 }
433
434 void CL_RelinkStaticEntities()
435 {
436         entity_t *ent, *endent;
437         if (cl.num_statics > MAX_VISEDICTS)
438                 Host_Error("CL_RelinkStaticEntities: cl.num_statics > MAX_VISEDICTS??\n");
439
440         ent = cl_static_entities;
441         endent = ent + cl.num_statics;
442         for (;ent < endent;ent++)
443                 cl_visedicts[cl_numvisedicts++] = ent;
444 }
445
446 /*
447 ===============
448 CL_RelinkEntities
449 ===============
450 */
451 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
452 void CL_RelinkEntities (void)
453 {
454         entity_t        *ent;
455         int                     i, j;
456         float           frac, f, d, bobjrotate/*, bobjoffset*/, dlightradius;
457         vec3_t          oldorg, delta, dlightcolor;
458
459 // determine partial update time        
460         frac = CL_LerpPoint ();
461
462         cl_numvisedicts = 0;
463
464         CL_RelinkStaticEntities();
465
466 //
467 // interpolate player info
468 //
469         for (i = 0;i < 3;i++)
470                 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
471
472         if (cls.demoplayback)
473         {
474         // interpolate the angles       
475                 for (j = 0;j < 3;j++)
476                 {
477                         d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
478                         if (d > 180)
479                                 d -= 360;
480                         else if (d < -180)
481                                 d += 360;
482                         cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
483                 }
484         }
485         
486         bobjrotate = ANGLEMOD(100*cl.time);
487 //      bobjoffset = cos(180 * cl.time * M_PI / 180) * 4.0f + 4.0f;
488         
489 // start on the entity after the world
490         for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
491         {
492                 // if the object wasn't included in the latest packet, remove it
493                 if (!ent->state_current.active)
494                         continue;
495
496                 VectorCopy (ent->render.origin, oldorg);
497
498                 if (!ent->state_previous.active)
499                 {
500                         // only one state available
501                         VectorCopy (ent->state_current.origin, ent->render.origin);
502                         VectorCopy (ent->state_current.angles, ent->render.angles);
503 //                      Con_Printf(" %i", i);
504                 }
505                 else
506                 {
507                         // if the delta is large, assume a teleport and don't lerp
508                         f = CL_EntityLerpPoint(ent);
509                         if (f < 1)
510                         {
511                                 for (j = 0;j < 3;j++)
512                                 {
513                                         delta[j] = ent->state_current.origin[j] - ent->state_previous.origin[j];
514                                         // LordHavoc: increased lerp tolerance from 100 to 200
515                                         if (delta[j] > 200 || delta[j] < -200)
516                                                 f = 1;
517                                 }
518                         }
519                         if (f >= 1)
520                         {
521                                 // no interpolation
522                                 VectorCopy (ent->state_current.origin, ent->render.origin);
523                                 VectorCopy (ent->state_current.angles, ent->render.angles);
524                         }
525                         else
526                         {
527                                 // interpolate the origin and angles
528                                 for (j = 0;j < 3;j++)
529                                 {
530                                         ent->render.origin[j] = ent->state_previous.origin[j] + f*delta[j];
531
532                                         d = ent->state_current.angles[j] - ent->state_previous.angles[j];
533                                         if (d > 180)
534                                                 d -= 360;
535                                         else if (d < -180)
536                                                 d += 360;
537                                         ent->render.angles[j] = ent->state_previous.angles[j] + f*d;
538                                 }
539                         }
540                 }
541
542                 ent->render.flags = ent->state_current.flags;
543                 ent->render.effects = ent->state_current.effects;
544                 ent->render.model = cl.model_precache[ent->state_current.modelindex];
545                 ent->render.frame = ent->state_current.frame;
546                 if (cl.scores == NULL || !ent->state_current.colormap)
547                         ent->render.colormap = -1; // no special coloring
548                 else
549                         ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
550                 ent->render.skinnum = ent->state_current.skin;
551                 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
552                 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
553                 ent->render.glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
554                 ent->render.glowcolor = ent->state_current.glowcolor;
555                 ent->render.colormod[0] = (float) ((ent->state_current.colormod >> 5) & 7) * (1.0f / 7.0f);
556                 ent->render.colormod[1] = (float) ((ent->state_current.colormod >> 2) & 7) * (1.0f / 7.0f);
557                 ent->render.colormod[2] = (float) (ent->state_current.colormod & 3) * (1.0f / 3.0f);
558
559                 dlightradius = 0;
560                 dlightcolor[0] = 0;
561                 dlightcolor[1] = 0;
562                 dlightcolor[2] = 0;
563
564                 // LordHavoc: if the entity has no effects, don't check each
565                 if (ent->render.effects)
566                 {
567                         if (ent->render.effects & EF_BRIGHTFIELD)
568                                 R_EntityParticles (ent);
569                         if (ent->render.effects & EF_MUZZLEFLASH)
570                         {
571                                 vec3_t v;
572
573                                 AngleVectors (ent->render.angles, v, NULL, NULL);
574
575                                 v[0] = v[0] * 18 + ent->render.origin[0];
576                                 v[1] = v[1] * 18 + ent->render.origin[1];
577                                 v[2] = v[2] * 18 + ent->render.origin[2] + 16;
578
579                                 CL_AllocDlight (NULL, v, 100, 1, 1, 1, 0, 0.1);
580                         }
581                         if (ent->render.effects & EF_DIMLIGHT)
582                         {
583                                 dlightcolor[0] += 200.0f;
584                                 dlightcolor[1] += 200.0f;
585                                 dlightcolor[2] += 200.0f;
586                         }
587                         if (ent->render.effects & EF_BRIGHTLIGHT)
588                         {
589                                 dlightcolor[0] += 400.0f;
590                                 dlightcolor[1] += 400.0f;
591                                 dlightcolor[2] += 400.0f;
592                         }
593                         // LordHavoc: added EF_RED and EF_BLUE
594                         if (ent->render.effects & EF_RED) // red
595                         {
596                                 dlightcolor[0] += 200.0f;
597                                 dlightcolor[1] +=  20.0f;
598                                 dlightcolor[2] +=  20.0f;
599                         }
600                         if (ent->render.effects & EF_BLUE) // blue
601                         {
602                                 dlightcolor[0] +=  20.0f;
603                                 dlightcolor[1] +=  20.0f;
604                                 dlightcolor[2] += 200.0f;
605                         }
606                         else if (ent->render.effects & EF_FLAME)
607                         {
608                                 if (ent->render.model)
609                                 {
610                                         vec3_t mins, maxs;
611                                         int temp;
612                                         VectorAdd(ent->render.origin, ent->render.model->mins, mins);
613                                         VectorAdd(ent->render.origin, ent->render.model->maxs, maxs);
614                                         // how many flames to make
615                                         temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
616                                         R_FlameCube(mins, maxs, temp);
617                                 }
618                                 d = lhrandom(200, 250);
619                                 dlightcolor[0] += d * 1.0f;
620                                 dlightcolor[1] += d * 0.7f;
621                                 dlightcolor[2] += d * 0.3f;
622                         }
623                 }
624
625                 // LordHavoc: if the model has no flags, don't check each
626                 if (ent->render.model && ent->render.model->flags)
627                 {
628                         if (ent->render.model->flags & EF_ROTATE)
629                         {
630                                 ent->render.angles[1] = bobjrotate;
631 //                              ent->render.origin[2] += bobjoffset;
632                         }
633                         // only do trails if present in the previous frame as well
634                         if (ent->state_previous.active)
635                         {
636                                 if (ent->render.model->flags & EF_GIB)
637                                         R_RocketTrail (oldorg, ent->render.origin, 2, ent);
638                                 else if (ent->render.model->flags & EF_ZOMGIB)
639                                         R_RocketTrail (oldorg, ent->render.origin, 4, ent);
640                                 else if (ent->render.model->flags & EF_TRACER)
641                                         R_RocketTrail (oldorg, ent->render.origin, 3, ent);
642                                 else if (ent->render.model->flags & EF_TRACER2)
643                                         R_RocketTrail (oldorg, ent->render.origin, 5, ent);
644                                 else if (ent->render.model->flags & EF_ROCKET)
645                                 {
646                                         R_RocketTrail (oldorg, ent->render.origin, 0, ent);
647                                         dlightcolor[0] += 200.0f;
648                                         dlightcolor[1] += 160.0f;
649                                         dlightcolor[2] +=  80.0f;
650                                 }
651                                 else if (ent->render.model->flags & EF_GRENADE)
652                                 {
653                                         if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
654                                                 R_RocketTrail (oldorg, ent->render.origin, 7, ent);
655                                         else
656                                                 R_RocketTrail (oldorg, ent->render.origin, 1, ent);
657                                 }
658                                 else if (ent->render.model->flags & EF_TRACER3)
659                                         R_RocketTrail (oldorg, ent->render.origin, 6, ent);
660                         }
661                 }
662                 // LordHavoc: customizable glow
663                 if (ent->render.glowsize)
664                 {
665                         byte *tempcolor = (byte *)&d_8to24table[ent->render.glowcolor];
666                         dlightcolor[0] += ent->render.glowsize * tempcolor[0] * (1.0f / 255.0f);
667                         dlightcolor[1] += ent->render.glowsize * tempcolor[1] * (1.0f / 255.0f);
668                         dlightcolor[2] += ent->render.glowsize * tempcolor[2] * (1.0f / 255.0f);
669                 }
670                 // LordHavoc: customizable trail
671                 if (ent->render.flags & RENDER_GLOWTRAIL)
672                         R_RocketTrail2 (oldorg, ent->render.origin, ent->render.glowcolor, ent);
673
674                 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
675                 {
676                         dlightradius = VectorLength(dlightcolor);
677                         d = 1.0f / dlightradius;
678                         CL_AllocDlight (ent, ent->render.origin, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
679                 }
680
681                 if (i == cl.viewentity && !chase_active.value)
682                         continue;
683
684                 if (ent->render.model == NULL)
685                         continue;
686                 if (ent->render.effects & EF_NODRAW)
687                         continue;
688                 if (cl_numvisedicts < MAX_VISEDICTS)
689                         cl_visedicts[cl_numvisedicts++] = ent;
690         }
691 //      Con_Printf("\n");
692 }
693
694
695 // used by cl_shownet
696 int netshown;
697
698 /*
699 ===============
700 CL_ReadFromServer
701
702 Read all incoming data from the server
703 ===============
704 */
705 int CL_ReadFromServer (void)
706 {
707         int             ret;
708
709         cl.oldtime = cl.time;
710         cl.time += cl.frametime;
711         
712         netshown = false;
713         do
714         {
715                 ret = CL_GetMessage ();
716                 if (ret == -1)
717                         Host_Error ("CL_ReadFromServer: lost server connection");
718                 if (!ret)
719                         break;
720                 
721                 cl.last_received_message = realtime;
722                 CL_ParseServerMessage ();
723         } while (ret && cls.state == ca_connected);
724         
725         if (netshown)
726                 Con_Printf ("\n");
727
728         CL_RelinkEntities ();
729         CL_UpdateTEnts ();
730         CL_DoEffects ();
731
732 //
733 // bring the links up to date
734 //
735         return 0;
736 }
737
738 /*
739 =================
740 CL_SendCmd
741 =================
742 */
743 void CL_SendCmd (void)
744 {
745         usercmd_t               cmd;
746
747         if (cls.state != ca_connected)
748                 return;
749
750         if (cls.signon == SIGNONS)
751         {
752         // get basic movement from keyboard
753                 CL_BaseMove (&cmd);
754         
755         // allow mice or other external controllers to add to the move
756                 IN_Move (&cmd);
757         
758         // send the unreliable message
759                 CL_SendMove (&cmd);
760         }
761
762         if (cls.demoplayback)
763         {
764                 SZ_Clear (&cls.message);
765                 return;
766         }
767         
768 // send the reliable message
769         if (!cls.message.cursize)
770                 return;         // no message at all
771         
772         if (!NET_CanSendMessage (cls.netcon))
773         {
774                 Con_DPrintf ("CL_WriteToServer: can't send\n");
775                 return;
776         }
777
778         if (NET_SendMessage (cls.netcon, &cls.message) == -1)
779                 Host_Error ("CL_WriteToServer: lost server connection");
780
781         SZ_Clear (&cls.message);
782 }
783
784 // LordHavoc: pausedemo command
785 void CL_PauseDemo_f (void)
786 {
787         cls.demopaused = !cls.demopaused;
788         if (cls.demopaused)
789                 Con_Printf("Demo paused\n");
790         else
791                 Con_Printf("Demo unpaused\n");
792 }
793
794 /*
795 ======================
796 CL_PModel_f
797 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
798 ======================
799 */
800 void CL_PModel_f (void)
801 {
802         int i;
803         eval_t *val;
804
805         if (Cmd_Argc () == 1)
806         {
807                 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
808                 return;
809         }
810         i = atoi(Cmd_Argv(1));
811
812         if (cmd_source == src_command)
813         {
814                 if (cl_pmodel.value == i)
815                         return;
816                 Cvar_SetValue ("_cl_pmodel", i);
817                 if (cls.state == ca_connected)
818                         Cmd_ForwardToServer ();
819                 return;
820         }
821
822         host_client->pmodel = i;
823         if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
824                 val->_float = i;
825 }
826
827 /*
828 ======================
829 CL_Fog_f
830 ======================
831 */
832 void CL_Fog_f (void)
833 {
834         if (Cmd_Argc () == 1)
835         {
836                 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
837                 return;
838         }
839         fog_density = atof(Cmd_Argv(1));
840         fog_red = atof(Cmd_Argv(2));
841         fog_green = atof(Cmd_Argv(3));
842         fog_blue = atof(Cmd_Argv(4));
843 }
844
845 cvar_t demo_nehahra = {"demo_nehahra", "0"};
846
847 /*
848 =================
849 CL_Init
850 =================
851 */
852 void CL_Init (void)
853 {       
854         SZ_Alloc (&cls.message, 1024);
855
856         CL_InitInput ();
857         CL_InitTEnts ();
858         
859 //
860 // register our commands
861 //
862         Cvar_RegisterVariable (&cl_name);
863         Cvar_RegisterVariable (&cl_color);
864         Cvar_RegisterVariable (&cl_pmodel);
865         Cvar_RegisterVariable (&cl_upspeed);
866         Cvar_RegisterVariable (&cl_forwardspeed);
867         Cvar_RegisterVariable (&cl_backspeed);
868         Cvar_RegisterVariable (&cl_sidespeed);
869         Cvar_RegisterVariable (&cl_movespeedkey);
870         Cvar_RegisterVariable (&cl_yawspeed);
871         Cvar_RegisterVariable (&cl_pitchspeed);
872         Cvar_RegisterVariable (&cl_anglespeedkey);
873         Cvar_RegisterVariable (&cl_shownet);
874         Cvar_RegisterVariable (&cl_nolerp);
875         Cvar_RegisterVariable (&lookspring);
876         Cvar_RegisterVariable (&lookstrafe);
877         Cvar_RegisterVariable (&sensitivity);
878
879         Cvar_RegisterVariable (&m_pitch);
880         Cvar_RegisterVariable (&m_yaw);
881         Cvar_RegisterVariable (&m_forward);
882         Cvar_RegisterVariable (&m_side);
883
884 //      Cvar_RegisterVariable (&cl_autofire);
885         
886         Cmd_AddCommand ("entities", CL_PrintEntities_f);
887         Cmd_AddCommand ("disconnect", CL_Disconnect_f);
888         Cmd_AddCommand ("record", CL_Record_f);
889         Cmd_AddCommand ("stop", CL_Stop_f);
890         Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
891         Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
892
893         Cmd_AddCommand ("fog", CL_Fog_f);
894
895         // LordHavoc: added pausedemo
896         Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
897         // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
898         Cmd_AddCommand ("pmodel", CL_PModel_f);
899         // LordHavoc: added demo_nehahra cvar
900         Cvar_RegisterVariable (&demo_nehahra);
901         if (nehahra)
902                 Cvar_SetValue("demo_nehahra", 1);
903 }
904