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};
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];
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_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++)
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;
93 //// allocate the efrags and chain together into a free list
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;
102 =====================
105 Sends a disconnect message to the server
106 This is also called on Host_Error, so it shouldn't cause any errors
107 =====================
109 void CL_Disconnect (void)
111 // stop sounds (especially looping!)
112 S_StopAllSounds (true);
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;
120 // if running a local server, shut it down
121 if (cls.demoplayback)
123 else if (cls.state == ca_connected)
125 if (cls.demorecording)
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);
135 cls.state = ca_disconnected;
137 Host_ShutdownServer(false);
140 cls.demoplayback = cls.timedemo = false;
144 void CL_Disconnect_f (void)
148 Host_ShutdownServer (false);
155 =====================
156 CL_EstablishConnection
158 Host should be either "local" or a net address to be passed on
159 =====================
161 void CL_EstablishConnection (char *host)
163 if (cls.state == ca_dedicated)
166 if (cls.demoplayback)
171 cls.netcon = NET_Connect (host);
173 Host_Error ("CL_Connect: connect failed\n");
174 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
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
182 =====================
185 An svc_signonnum has been received, perform a client side setup
186 =====================
188 void CL_SignonReply (void)
192 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
197 MSG_WriteByte (&cls.message, clc_stringcmd);
198 MSG_WriteString (&cls.message, "prespawn");
202 MSG_WriteByte (&cls.message, clc_stringcmd);
203 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
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));
210 MSG_WriteByte (&cls.message, clc_stringcmd);
211 MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
214 MSG_WriteByte (&cls.message, clc_stringcmd);
215 sprintf (str, "spawn %s", cls.spawnparms);
216 MSG_WriteString (&cls.message, str);
220 MSG_WriteByte (&cls.message, clc_stringcmd);
221 MSG_WriteString (&cls.message, "begin");
222 Cache_Report (); // print remaining memory
226 // SCR_EndLoadingPlaque (); // allow normal screen updates
233 =====================
236 Called to play the next demo in the demo loop
237 =====================
239 void CL_NextDemo (void)
243 if (cls.demonum == -1)
244 return; // don't play demos
246 // SCR_BeginLoadingPlaque ();
248 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
251 if (!cls.demos[cls.demonum][0])
253 Con_Printf ("No demos listed with startdemos\n");
259 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
260 Cbuf_InsertText (str);
269 void CL_PrintEntities_f (void)
274 for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
276 Con_Printf ("%3i:", i);
277 if (!ent->render.model)
279 Con_Printf ("EMPTY\n");
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]);
293 void CL_AllocDlight (entity_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
298 // first look for an exact key match
302 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
307 // then look for anything else
309 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
313 // unable to find one
317 memset (dl, 0, sizeof(*dl));
319 VectorCopy(org, dl->origin);
322 dl->color[1] = green;
325 dl->die = cl.time + lifetime;
335 void CL_DecayLights (void)
341 time = cl.time - cl.oldtime;
345 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
349 if (dl->die < cl.time)
355 c_dlights++; // count every dlight in use
357 dl->radius -= time*dl->decay;
368 Determines the fraction between the last two messages that the objects
372 float CL_LerpPoint (void)
376 f = cl.mtime[0] - cl.mtime[1];
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))
381 cl.time = cl.mtime[0];
386 { // dropped packet, or start of demo
387 cl.mtime[1] = cl.mtime[0] - 0.1;
390 frac = (cl.time - cl.mtime[1]) / f;
391 //Con_Printf ("frac: %f\n",frac);
396 cl.time = cl.mtime[1];
397 // Con_Printf ("low frac\n");
405 cl.time = cl.mtime[0];
406 // Con_Printf ("high frac\n");
414 float CL_EntityLerpPoint (entity_t *ent)
418 if (cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
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);
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);
434 void CL_RelinkStaticEntities()
436 entity_t *ent, *endent;
437 if (cl.num_statics > MAX_VISEDICTS)
438 Host_Error("CL_RelinkStaticEntities: cl.num_statics > MAX_VISEDICTS??\n");
440 ent = cl_static_entities;
441 endent = ent + cl.num_statics;
442 for (;ent < endent;ent++)
443 cl_visedicts[cl_numvisedicts++] = ent;
451 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
452 void CL_RelinkEntities (void)
462 // determine partial update time
463 frac = CL_LerpPoint ();
467 CL_RelinkStaticEntities();
470 // interpolate player info
472 for (i = 0;i < 3;i++)
473 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
475 if (cls.demoplayback)
477 // interpolate the angles
478 for (j = 0;j < 3;j++)
480 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
485 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
489 bobjrotate = ANGLEMOD(100*cl.time);
490 // bobjoffset = cos(180 * cl.time * M_PI / 180) * 4.0f + 4.0f;
492 // start on the entity after the world
493 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
495 // if the object wasn't included in the latest packet, remove it
496 if (!ent->state_current.active)
499 VectorCopy (ent->render.origin, oldorg);
501 if (!ent->state_previous.active)
503 // only one state available
504 VectorCopy (ent->state_current.origin, ent->render.origin);
505 VectorCopy (ent->state_current.angles, ent->render.angles);
506 // Con_Printf(" %i", i);
510 // if the delta is large, assume a teleport and don't lerp
511 f = CL_EntityLerpPoint(ent);
514 for (j = 0;j < 3;j++)
516 delta[j] = ent->state_current.origin[j] - ent->state_previous.origin[j];
517 // LordHavoc: increased lerp tolerance from 100 to 200
518 if (delta[j] > 200 || delta[j] < -200)
525 VectorCopy (ent->state_current.origin, ent->render.origin);
526 VectorCopy (ent->state_current.angles, ent->render.angles);
530 // interpolate the origin and angles
531 for (j = 0;j < 3;j++)
533 ent->render.origin[j] = ent->state_previous.origin[j] + f*delta[j];
535 d = ent->state_current.angles[j] - ent->state_previous.angles[j];
540 ent->render.angles[j] = ent->state_previous.angles[j] + f*d;
545 ent->render.flags = ent->state_current.flags;
546 ent->render.effects = ent->state_current.effects;
547 ent->render.model = cl.model_precache[ent->state_current.modelindex];
548 ent->render.frame = ent->state_current.frame;
549 if (cl.scores == NULL || !ent->state_current.colormap)
550 ent->render.colormap = -1; // no special coloring
552 ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
553 ent->render.skinnum = ent->state_current.skin;
554 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
555 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
556 ent->render.glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
557 ent->render.glowcolor = ent->state_current.glowcolor;
558 ent->render.colormod[0] = (float) ((ent->state_current.colormod >> 5) & 7) * (1.0f / 7.0f);
559 ent->render.colormod[1] = (float) ((ent->state_current.colormod >> 2) & 7) * (1.0f / 7.0f);
560 ent->render.colormod[2] = (float) (ent->state_current.colormod & 3) * (1.0f / 3.0f);
562 // LordHavoc: if the entity has no effects, don't check each
563 if (ent->render.effects)
565 if (ent->render.effects & EF_BRIGHTFIELD)
566 R_EntityParticles (ent);
567 if (ent->render.effects & EF_MUZZLEFLASH)
571 AngleVectors (ent->render.angles, v, NULL, NULL);
573 v[0] = v[0] * 18 + ent->render.origin[0];
574 v[1] = v[1] * 18 + ent->render.origin[1];
575 v[2] = v[2] * 18 + ent->render.origin[2] + 16;
577 CL_AllocDlight (ent, v, 100, 1, 1, 1, 0, 0.1);
579 if (ent->render.effects & EF_BRIGHTLIGHT)
580 CL_AllocDlight (ent, ent->render.origin, 400, 1, 1, 1, 0, 0);
581 if (ent->render.effects & EF_DIMLIGHT)
582 CL_AllocDlight (ent, ent->render.origin, 200, 1, 1, 1, 0, 0);
583 // LordHavoc: added EF_RED and EF_BLUE
584 if (ent->render.effects & EF_RED) // red
586 if (ent->render.effects & EF_BLUE) // magenta
587 CL_AllocDlight (ent, ent->render.origin, 200, 1.0f, 0.2f, 1.0f, 0, 0);
589 CL_AllocDlight (ent, ent->render.origin, 200, 1.0f, 0.1f, 0.1f, 0, 0);
591 else if (ent->render.effects & EF_BLUE) // blue
592 CL_AllocDlight (ent, ent->render.origin, 200, 0.1f, 0.1f, 1.0f, 0, 0);
593 else if (ent->render.effects & EF_FLAME)
595 if (ent->render.model)
599 VectorAdd(ent->render.origin, ent->render.model->mins, mins);
600 VectorAdd(ent->render.origin, ent->render.model->maxs, maxs);
601 // how many flames to make
602 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
603 R_FlameCube(mins, maxs, temp);
605 CL_AllocDlight (ent, ent->render.origin, lhrandom(200, 250), 1.0f, 0.7f, 0.3f, 0, 0);
609 // LordHavoc: if the model has no flags, don't check each
610 if (ent->render.model && ent->render.model->flags)
612 if (ent->render.model->flags & EF_ROTATE)
614 ent->render.angles[1] = bobjrotate;
615 // ent->render.origin[2] += bobjoffset;
617 // only do trails if present in the previous frame as well
618 if (ent->state_previous.active)
620 if (ent->render.model->flags & EF_GIB)
621 R_RocketTrail (oldorg, ent->render.origin, 2, ent);
622 else if (ent->render.model->flags & EF_ZOMGIB)
623 R_RocketTrail (oldorg, ent->render.origin, 4, ent);
624 else if (ent->render.model->flags & EF_TRACER)
625 R_RocketTrail (oldorg, ent->render.origin, 3, ent);
626 else if (ent->render.model->flags & EF_TRACER2)
627 R_RocketTrail (oldorg, ent->render.origin, 5, ent);
628 else if (ent->render.model->flags & EF_ROCKET)
630 R_RocketTrail (oldorg, ent->render.origin, 0, ent);
631 CL_AllocDlight (ent, ent->render.origin, 200, 1.0f, 0.8f, 0.4f, 0, 0);
633 else if (ent->render.model->flags & EF_GRENADE)
635 if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
636 R_RocketTrail (oldorg, ent->render.origin, 7, ent);
638 R_RocketTrail (oldorg, ent->render.origin, 1, ent);
640 else if (ent->render.model->flags & EF_TRACER3)
641 R_RocketTrail (oldorg, ent->render.origin, 6, ent);
644 if (ent->render.glowsize) // LordHavoc: customizable glow
646 byte *tempcolor = (byte *)&d_8to24table[ent->render.glowcolor];
647 CL_AllocDlight (ent, ent->render.origin, ent->render.glowsize, tempcolor[0]*(1.0/255.0), tempcolor[1]*(1.0/255.0), tempcolor[2]*(1.0/255.0), 0, 0);
649 if (ent->render.flags & RENDER_GLOWTRAIL) // LordHavoc: customizable glow and trail
650 R_RocketTrail2 (oldorg, ent->render.origin, ent->render.glowcolor, ent);
652 if (i == cl.viewentity && !chase_active.value)
655 if (ent->render.model == NULL)
657 if (ent->render.effects & EF_NODRAW)
659 if (cl_numvisedicts < MAX_VISEDICTS)
660 cl_visedicts[cl_numvisedicts++] = ent;
666 // used by cl_shownet
673 Read all incoming data from the server
676 int CL_ReadFromServer (void)
680 cl.oldtime = cl.time;
681 cl.time += cl.frametime;
686 ret = CL_GetMessage ();
688 Host_Error ("CL_ReadFromServer: lost server connection");
692 cl.last_received_message = realtime;
693 CL_ParseServerMessage ();
694 } while (ret && cls.state == ca_connected);
699 CL_RelinkEntities ();
704 // bring the links up to date
714 void CL_SendCmd (void)
718 if (cls.state != ca_connected)
721 if (cls.signon == SIGNONS)
723 // get basic movement from keyboard
726 // allow mice or other external controllers to add to the move
729 // send the unreliable message
733 if (cls.demoplayback)
735 SZ_Clear (&cls.message);
739 // send the reliable message
740 if (!cls.message.cursize)
741 return; // no message at all
743 if (!NET_CanSendMessage (cls.netcon))
745 Con_DPrintf ("CL_WriteToServer: can't send\n");
749 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
750 Host_Error ("CL_WriteToServer: lost server connection");
752 SZ_Clear (&cls.message);
755 // LordHavoc: pausedemo command
756 void CL_PauseDemo_f (void)
758 cls.demopaused = !cls.demopaused;
760 Con_Printf("Demo paused\n");
762 Con_Printf("Demo unpaused\n");
766 ======================
768 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
769 ======================
771 void CL_PModel_f (void)
776 if (Cmd_Argc () == 1)
778 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
781 i = atoi(Cmd_Argv(1));
783 if (cmd_source == src_command)
785 if (cl_pmodel.value == i)
787 Cvar_SetValue ("_cl_pmodel", i);
788 if (cls.state == ca_connected)
789 Cmd_ForwardToServer ();
793 host_client->pmodel = i;
794 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
799 ======================
801 ======================
805 if (Cmd_Argc () == 1)
807 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
810 fog_density = atof(Cmd_Argv(1));
811 fog_red = atof(Cmd_Argv(2));
812 fog_green = atof(Cmd_Argv(3));
813 fog_blue = atof(Cmd_Argv(4));
816 cvar_t demo_nehahra = {"demo_nehahra", "0"};
825 SZ_Alloc (&cls.message, 1024);
831 // register our commands
833 Cvar_RegisterVariable (&cl_name);
834 Cvar_RegisterVariable (&cl_color);
835 Cvar_RegisterVariable (&cl_pmodel);
836 Cvar_RegisterVariable (&cl_upspeed);
837 Cvar_RegisterVariable (&cl_forwardspeed);
838 Cvar_RegisterVariable (&cl_backspeed);
839 Cvar_RegisterVariable (&cl_sidespeed);
840 Cvar_RegisterVariable (&cl_movespeedkey);
841 Cvar_RegisterVariable (&cl_yawspeed);
842 Cvar_RegisterVariable (&cl_pitchspeed);
843 Cvar_RegisterVariable (&cl_anglespeedkey);
844 Cvar_RegisterVariable (&cl_shownet);
845 Cvar_RegisterVariable (&cl_nolerp);
846 Cvar_RegisterVariable (&lookspring);
847 Cvar_RegisterVariable (&lookstrafe);
848 Cvar_RegisterVariable (&sensitivity);
850 Cvar_RegisterVariable (&m_pitch);
851 Cvar_RegisterVariable (&m_yaw);
852 Cvar_RegisterVariable (&m_forward);
853 Cvar_RegisterVariable (&m_side);
855 // Cvar_RegisterVariable (&cl_autofire);
857 Cmd_AddCommand ("entities", CL_PrintEntities_f);
858 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
859 Cmd_AddCommand ("record", CL_Record_f);
860 Cmd_AddCommand ("stop", CL_Stop_f);
861 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
862 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
864 Cmd_AddCommand ("fog", CL_Fog_f);
866 // LordHavoc: added pausedemo
867 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
868 // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
869 Cmd_AddCommand ("pmodel", CL_PModel_f);
870 // LordHavoc: added demo_nehahra cvar
871 Cvar_RegisterVariable (&demo_nehahra);
873 Cvar_SetValue("demo_nehahra", 1);