2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // cl_main.c -- client main loop
24 // we need to declare some mouse variables here, because the menu system
25 // references them even when on a unix system.
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};
32 cvar_t cl_shownet = {"cl_shownet","0"}; // can be 0, 1, or 2
33 cvar_t cl_nolerp = {"cl_nolerp","0"};
35 cvar_t lookspring = {"lookspring","0", true};
36 cvar_t lookstrafe = {"lookstrafe","0", true};
37 cvar_t sensitivity = {"sensitivity","3", true};
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};
44 cvar_t freelook = {"freelook", "1", true};
48 // FIXME: put these on hunk?
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];
55 entity_t *cl_visedicts[MAX_VISEDICTS];
63 void CL_ClearState (void)
70 // wipe the entire cl structure
71 memset (&cl, 0, sizeof(cl));
73 SZ_Clear (&cls.message);
76 memset (cl_entities, 0, sizeof(cl_entities));
77 memset (cl_dlights, 0, sizeof(cl_dlights));
78 memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
79 memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
80 memset (cl_beams, 0, sizeof(cl_beams));
81 // LordHavoc: have to set up the baseline info for alpha and other stuff
82 for (i = 0;i < MAX_EDICTS;i++)
84 ClearStateToDefault(&cl_entities[i].state_baseline);
85 ClearStateToDefault(&cl_entities[i].state_previous);
86 ClearStateToDefault(&cl_entities[i].state_current);
94 Sends a disconnect message to the server
95 This is also called on Host_Error, so it shouldn't cause any errors
98 void CL_Disconnect (void)
100 // stop sounds (especially looping!)
101 S_StopAllSounds (true);
103 // clear contents blends
104 cl.cshifts[0].percent = 0;
105 cl.cshifts[1].percent = 0;
106 cl.cshifts[2].percent = 0;
107 cl.cshifts[3].percent = 0;
109 // if running a local server, shut it down
110 if (cls.demoplayback)
112 else if (cls.state == ca_connected)
114 if (cls.demorecording)
117 Con_DPrintf ("Sending clc_disconnect\n");
118 SZ_Clear (&cls.message);
119 MSG_WriteByte (&cls.message, clc_disconnect);
120 NET_SendUnreliableMessage (cls.netcon, &cls.message);
121 SZ_Clear (&cls.message);
122 NET_Close (cls.netcon);
124 cls.state = ca_disconnected;
126 Host_ShutdownServer(false);
129 cls.demoplayback = cls.timedemo = false;
133 void CL_Disconnect_f (void)
137 Host_ShutdownServer (false);
144 =====================
145 CL_EstablishConnection
147 Host should be either "local" or a net address to be passed on
148 =====================
150 void CL_EstablishConnection (char *host)
152 if (cls.state == ca_dedicated)
155 if (cls.demoplayback)
160 cls.netcon = NET_Connect (host);
162 Host_Error ("CL_Connect: connect failed\n");
163 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
165 cls.demonum = -1; // not in the demo loop now
166 cls.state = ca_connected;
167 cls.signon = 0; // need all the signon messages before playing
171 =====================
174 An svc_signonnum has been received, perform a client side setup
175 =====================
177 void CL_SignonReply (void)
181 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
186 MSG_WriteByte (&cls.message, clc_stringcmd);
187 MSG_WriteString (&cls.message, "prespawn");
191 MSG_WriteByte (&cls.message, clc_stringcmd);
192 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
194 MSG_WriteByte (&cls.message, clc_stringcmd);
195 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
199 MSG_WriteByte (&cls.message, clc_stringcmd);
200 MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
203 MSG_WriteByte (&cls.message, clc_stringcmd);
204 sprintf (str, "spawn %s", cls.spawnparms);
205 MSG_WriteString (&cls.message, str);
209 MSG_WriteByte (&cls.message, clc_stringcmd);
210 MSG_WriteString (&cls.message, "begin");
211 Cache_Report (); // print remaining memory
215 // SCR_EndLoadingPlaque (); // allow normal screen updates
222 =====================
225 Called to play the next demo in the demo loop
226 =====================
228 void CL_NextDemo (void)
232 if (cls.demonum == -1)
233 return; // don't play demos
235 // SCR_BeginLoadingPlaque ();
237 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
240 if (!cls.demos[cls.demonum][0])
242 Con_Printf ("No demos listed with startdemos\n");
248 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
249 Cbuf_InsertText (str);
258 void CL_PrintEntities_f (void)
263 for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
265 Con_Printf ("%3i:", i);
266 if (!ent->render.model)
268 Con_Printf ("EMPTY\n");
271 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]);
282 void CL_AllocDlight (entity_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
287 // first look for an exact key match
291 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
296 // then look for anything else
298 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
302 // unable to find one
306 memset (dl, 0, sizeof(*dl));
308 VectorCopy(org, dl->origin);
311 dl->color[1] = green;
314 dl->die = cl.time + lifetime;
324 void CL_DecayLights (void)
330 time = cl.time - cl.oldtime;
334 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
338 if (dl->die < cl.time)
344 c_dlights++; // count every dlight in use
346 dl->radius -= time*dl->decay;
357 Determines the fraction between the last two messages that the objects
361 float CL_LerpPoint (void)
365 f = cl.mtime[0] - cl.mtime[1];
367 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
368 if (!f || cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
370 cl.time = cl.mtime[0];
375 { // dropped packet, or start of demo
376 cl.mtime[1] = cl.mtime[0] - 0.1;
379 frac = (cl.time - cl.mtime[1]) / f;
380 // Con_Printf ("frac: %f\n",frac);
385 cl.time = cl.mtime[1];
386 // Con_Printf ("low frac\n");
394 cl.time = cl.mtime[0];
395 // Con_Printf ("high frac\n");
403 float CL_EntityLerpPoint (entity_t *ent)
407 if (cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
410 f = ent->state_current.time - ent->state_previous.time;
411 // Con_Printf(" %g-%g=%g", ent->state_current.time, ent->state_previous.time, f);
418 // Con_Printf(" %g-%g/%g=%f", cl.time, ent->state_previous.time, f, (cl.time - ent->state_previous.time) / f);
419 f = (cl.time - ent->state_previous.time) / f;
420 return bound(0, f, 1);
423 void CL_RelinkStaticEntities(void)
425 entity_t *ent, *endent;
426 if (cl.num_statics > MAX_VISEDICTS)
427 Host_Error("CL_RelinkStaticEntities: cl.num_statics > MAX_VISEDICTS??\n");
429 ent = cl_static_entities;
430 endent = ent + cl.num_statics;
431 for (;ent < endent;ent++)
432 cl_visedicts[cl_numvisedicts++] = ent;
440 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
441 void CL_RelinkEntities (void)
444 int i, j, glowcolor, effects;
445 float frac, f, d, bobjrotate/*, bobjoffset*/, dlightradius, glowsize;
446 vec3_t oldorg, delta, dlightcolor;
448 // determine partial update time
449 frac = CL_LerpPoint ();
453 CL_RelinkStaticEntities();
456 // interpolate player info
458 for (i = 0;i < 3;i++)
459 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
461 if (cls.demoplayback)
463 // interpolate the angles
464 for (j = 0;j < 3;j++)
466 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
471 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
475 bobjrotate = ANGLEMOD(100*cl.time);
476 // bobjoffset = cos(180 * cl.time * M_PI / 180) * 4.0f + 4.0f;
478 // start on the entity after the world
479 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
481 // if the object wasn't included in the latest packet, remove it
482 if (!ent->state_current.active)
485 VectorCopy (ent->render.origin, oldorg);
487 if (!ent->state_previous.active)
489 // only one state available
490 VectorCopy (ent->state_current.origin, ent->render.origin);
491 VectorCopy (ent->state_current.angles, ent->render.angles);
492 // Con_Printf(" %i", i);
496 // if the delta is large, assume a teleport and don't lerp
497 f = CL_EntityLerpPoint(ent);
500 for (j = 0;j < 3;j++)
502 delta[j] = ent->state_current.origin[j] - ent->state_previous.origin[j];
503 // LordHavoc: increased lerp tolerance from 100 to 200
504 if (delta[j] > 200 || delta[j] < -200)
511 VectorCopy (ent->state_current.origin, ent->render.origin);
512 VectorCopy (ent->state_current.angles, ent->render.angles);
516 // interpolate the origin and angles
517 for (j = 0;j < 3;j++)
519 ent->render.origin[j] = ent->state_previous.origin[j] + f*delta[j];
521 d = ent->state_current.angles[j] - ent->state_previous.angles[j];
526 ent->render.angles[j] = ent->state_previous.angles[j] + f*d;
531 ent->render.flags = ent->state_current.flags;
532 ent->render.effects = effects = ent->state_current.effects;
533 ent->render.model = cl.model_precache[ent->state_current.modelindex];
534 ent->render.frame = ent->state_current.frame;
535 if (cl.scores == NULL || !ent->state_current.colormap)
536 ent->render.colormap = -1; // no special coloring
538 ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
539 ent->render.skinnum = ent->state_current.skin;
540 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
541 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
542 glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
543 glowcolor = ent->state_current.glowcolor;
544 ent->render.colormod[0] = (float) ((ent->state_current.colormod >> 5) & 7) * (1.0f / 7.0f);
545 ent->render.colormod[1] = (float) ((ent->state_current.colormod >> 2) & 7) * (1.0f / 7.0f);
546 ent->render.colormod[2] = (float) (ent->state_current.colormod & 3) * (1.0f / 3.0f);
553 // LordHavoc: if the entity has no effects, don't check each
556 if (effects & EF_BRIGHTFIELD)
557 R_EntityParticles (ent);
558 if (effects & EF_MUZZLEFLASH)
562 AngleVectors (ent->render.angles, v, NULL, NULL);
564 v[0] = v[0] * 18 + ent->render.origin[0];
565 v[1] = v[1] * 18 + ent->render.origin[1];
566 v[2] = v[2] * 18 + ent->render.origin[2] + 16;
568 CL_AllocDlight (NULL, v, 100, 1, 1, 1, 0, 0.1);
570 if (effects & EF_DIMLIGHT)
572 dlightcolor[0] += 200.0f;
573 dlightcolor[1] += 200.0f;
574 dlightcolor[2] += 200.0f;
576 if (effects & EF_BRIGHTLIGHT)
578 dlightcolor[0] += 400.0f;
579 dlightcolor[1] += 400.0f;
580 dlightcolor[2] += 400.0f;
582 // LordHavoc: added EF_RED and EF_BLUE
583 if (effects & EF_RED) // red
585 dlightcolor[0] += 200.0f;
586 dlightcolor[1] += 20.0f;
587 dlightcolor[2] += 20.0f;
589 if (effects & EF_BLUE) // blue
591 dlightcolor[0] += 20.0f;
592 dlightcolor[1] += 20.0f;
593 dlightcolor[2] += 200.0f;
595 else if (effects & EF_FLAME)
597 if (ent->render.model)
601 VectorAdd(ent->render.origin, ent->render.model->mins, mins);
602 VectorAdd(ent->render.origin, ent->render.model->maxs, maxs);
603 // how many flames to make
604 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
605 R_FlameCube(mins, maxs, temp);
607 d = lhrandom(200, 250);
608 dlightcolor[0] += d * 1.0f;
609 dlightcolor[1] += d * 0.7f;
610 dlightcolor[2] += d * 0.3f;
614 // LordHavoc: if the model has no flags, don't check each
615 if (ent->render.model && ent->render.model->flags)
617 if (ent->render.model->flags & EF_ROTATE)
619 ent->render.angles[1] = bobjrotate;
620 // ent->render.origin[2] += bobjoffset;
622 // only do trails if present in the previous frame as well
623 if (ent->state_previous.active)
625 if (ent->render.model->flags & EF_GIB)
626 R_RocketTrail (oldorg, ent->render.origin, 2, ent);
627 else if (ent->render.model->flags & EF_ZOMGIB)
628 R_RocketTrail (oldorg, ent->render.origin, 4, ent);
629 else if (ent->render.model->flags & EF_TRACER)
630 R_RocketTrail (oldorg, ent->render.origin, 3, ent);
631 else if (ent->render.model->flags & EF_TRACER2)
632 R_RocketTrail (oldorg, ent->render.origin, 5, ent);
633 else if (ent->render.model->flags & EF_ROCKET)
635 R_RocketTrail (oldorg, ent->render.origin, 0, ent);
636 dlightcolor[0] += 200.0f;
637 dlightcolor[1] += 160.0f;
638 dlightcolor[2] += 80.0f;
640 else if (ent->render.model->flags & EF_GRENADE)
642 if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
643 R_RocketTrail (oldorg, ent->render.origin, 7, ent);
645 R_RocketTrail (oldorg, ent->render.origin, 1, ent);
647 else if (ent->render.model->flags & EF_TRACER3)
648 R_RocketTrail (oldorg, ent->render.origin, 6, ent);
651 // LordHavoc: customizable glow
654 byte *tempcolor = (byte *)&d_8to24table[glowcolor];
655 dlightcolor[0] += glowsize * tempcolor[0] * (1.0f / 255.0f);
656 dlightcolor[1] += glowsize * tempcolor[1] * (1.0f / 255.0f);
657 dlightcolor[2] += glowsize * tempcolor[2] * (1.0f / 255.0f);
659 // LordHavoc: customizable trail
660 if (ent->render.flags & RENDER_GLOWTRAIL)
661 R_RocketTrail2 (oldorg, ent->render.origin, glowcolor, ent);
663 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
666 dlightradius = VectorLength(dlightcolor);
667 d = 1.0f / dlightradius;
668 VectorCopy(ent->render.origin, vec);
669 if (i == cl.viewentity && !chase_active.value)
671 CL_AllocDlight (ent, vec, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
674 if (chase_active.value)
676 if (ent->render.flags & RENDER_VIEWMODEL)
681 if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
685 if (ent->render.model == NULL)
687 if (effects & EF_NODRAW)
689 if (cl_numvisedicts < MAX_VISEDICTS)
690 cl_visedicts[cl_numvisedicts++] = ent;
700 Read all incoming data from the server
703 int CL_ReadFromServer (void)
707 cl.oldtime = cl.time;
708 cl.time += cl.frametime;
713 ret = CL_GetMessage ();
715 Host_Error ("CL_ReadFromServer: lost server connection");
719 cl.last_received_message = realtime;
721 if (cl_shownet.value)
724 CL_ParseServerMessage ();
726 while (ret && cls.state == ca_connected);
731 CL_RelinkEntities ();
736 // bring the links up to date
746 void CL_SendCmd (void)
750 if (cls.state != ca_connected)
753 if (cls.signon == SIGNONS)
755 // get basic movement from keyboard
758 // allow mice or other external controllers to add to the move
761 // send the unreliable message
765 if (cls.demoplayback)
767 SZ_Clear (&cls.message);
771 // send the reliable message
772 if (!cls.message.cursize)
773 return; // no message at all
775 if (!NET_CanSendMessage (cls.netcon))
777 Con_DPrintf ("CL_WriteToServer: can't send\n");
781 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
782 Host_Error ("CL_WriteToServer: lost server connection");
784 SZ_Clear (&cls.message);
787 // LordHavoc: pausedemo command
788 void CL_PauseDemo_f (void)
790 cls.demopaused = !cls.demopaused;
792 Con_Printf("Demo paused\n");
794 Con_Printf("Demo unpaused\n");
798 ======================
800 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
801 ======================
803 void CL_PModel_f (void)
808 if (Cmd_Argc () == 1)
810 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
813 i = atoi(Cmd_Argv(1));
815 if (cmd_source == src_command)
817 if (cl_pmodel.value == i)
819 Cvar_SetValue ("_cl_pmodel", i);
820 if (cls.state == ca_connected)
821 Cmd_ForwardToServer ();
825 host_client->pmodel = i;
826 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
831 ======================
833 ======================
837 if (Cmd_Argc () == 1)
839 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
842 fog_density = atof(Cmd_Argv(1));
843 fog_red = atof(Cmd_Argv(2));
844 fog_green = atof(Cmd_Argv(3));
845 fog_blue = atof(Cmd_Argv(4));
855 SZ_Alloc (&cls.message, 1024);
861 // register our commands
863 Cvar_RegisterVariable (&cl_name);
864 Cvar_RegisterVariable (&cl_color);
865 Cvar_RegisterVariable (&cl_pmodel);
866 Cvar_RegisterVariable (&cl_upspeed);
867 Cvar_RegisterVariable (&cl_forwardspeed);
868 Cvar_RegisterVariable (&cl_backspeed);
869 Cvar_RegisterVariable (&cl_sidespeed);
870 Cvar_RegisterVariable (&cl_movespeedkey);
871 Cvar_RegisterVariable (&cl_yawspeed);
872 Cvar_RegisterVariable (&cl_pitchspeed);
873 Cvar_RegisterVariable (&cl_anglespeedkey);
874 Cvar_RegisterVariable (&cl_shownet);
875 Cvar_RegisterVariable (&cl_nolerp);
876 Cvar_RegisterVariable (&lookspring);
877 Cvar_RegisterVariable (&lookstrafe);
878 Cvar_RegisterVariable (&sensitivity);
879 Cvar_RegisterVariable (&freelook);
881 Cvar_RegisterVariable (&m_pitch);
882 Cvar_RegisterVariable (&m_yaw);
883 Cvar_RegisterVariable (&m_forward);
884 Cvar_RegisterVariable (&m_side);
886 // Cvar_RegisterVariable (&cl_autofire);
888 Cmd_AddCommand ("entities", CL_PrintEntities_f);
889 Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
890 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
891 Cmd_AddCommand ("record", CL_Record_f);
892 Cmd_AddCommand ("stop", CL_Stop_f);
893 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
894 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
896 Cmd_AddCommand ("fog", CL_Fog_f);
898 // LordHavoc: added pausedemo
899 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
900 // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
901 Cmd_AddCommand ("pmodel", CL_PModel_f);