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};
46 cvar_t demo_nehahra = {"demo_nehahra", "0"};
50 // FIXME: put these on hunk?
51 entity_t cl_entities[MAX_EDICTS];
52 entity_t cl_static_entities[MAX_STATIC_ENTITIES];
53 lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
54 dlight_t cl_dlights[MAX_DLIGHTS];
57 entity_t *cl_visedicts[MAX_VISEDICTS];
65 void CL_ClearState (void)
72 // wipe the entire cl structure
73 memset (&cl, 0, sizeof(cl));
75 SZ_Clear (&cls.message);
78 memset (cl_entities, 0, sizeof(cl_entities));
79 memset (cl_dlights, 0, sizeof(cl_dlights));
80 memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
81 memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
82 memset (cl_beams, 0, sizeof(cl_beams));
83 // LordHavoc: have to set up the baseline info for alpha and other stuff
84 for (i = 0;i < MAX_EDICTS;i++)
86 ClearStateToDefault(&cl_entities[i].state_baseline);
87 ClearStateToDefault(&cl_entities[i].state_previous);
88 ClearStateToDefault(&cl_entities[i].state_current);
96 Sends a disconnect message to the server
97 This is also called on Host_Error, so it shouldn't cause any errors
100 void CL_Disconnect (void)
102 // stop sounds (especially looping!)
103 S_StopAllSounds (true);
105 // clear contents blends
106 cl.cshifts[0].percent = 0;
107 cl.cshifts[1].percent = 0;
108 cl.cshifts[2].percent = 0;
109 cl.cshifts[3].percent = 0;
111 // if running a local server, shut it down
112 if (cls.demoplayback)
114 else if (cls.state == ca_connected)
116 if (cls.demorecording)
119 Con_DPrintf ("Sending clc_disconnect\n");
120 SZ_Clear (&cls.message);
121 MSG_WriteByte (&cls.message, clc_disconnect);
122 NET_SendUnreliableMessage (cls.netcon, &cls.message);
123 SZ_Clear (&cls.message);
124 NET_Close (cls.netcon);
126 cls.state = ca_disconnected;
128 Host_ShutdownServer(false);
131 cls.demoplayback = cls.timedemo = false;
135 void CL_Disconnect_f (void)
139 Host_ShutdownServer (false);
146 =====================
147 CL_EstablishConnection
149 Host should be either "local" or a net address to be passed on
150 =====================
152 void CL_EstablishConnection (char *host)
154 if (cls.state == ca_dedicated)
157 if (cls.demoplayback)
162 cls.netcon = NET_Connect (host);
164 Host_Error ("CL_Connect: connect failed\n");
165 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
167 cls.demonum = -1; // not in the demo loop now
168 cls.state = ca_connected;
169 cls.signon = 0; // need all the signon messages before playing
173 =====================
176 An svc_signonnum has been received, perform a client side setup
177 =====================
179 void CL_SignonReply (void)
183 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
188 MSG_WriteByte (&cls.message, clc_stringcmd);
189 MSG_WriteString (&cls.message, "prespawn");
193 MSG_WriteByte (&cls.message, clc_stringcmd);
194 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
196 MSG_WriteByte (&cls.message, clc_stringcmd);
197 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
201 MSG_WriteByte (&cls.message, clc_stringcmd);
202 MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
205 MSG_WriteByte (&cls.message, clc_stringcmd);
206 sprintf (str, "spawn %s", cls.spawnparms);
207 MSG_WriteString (&cls.message, str);
211 MSG_WriteByte (&cls.message, clc_stringcmd);
212 MSG_WriteString (&cls.message, "begin");
213 Cache_Report (); // print remaining memory
217 // SCR_EndLoadingPlaque (); // allow normal screen updates
224 =====================
227 Called to play the next demo in the demo loop
228 =====================
230 void CL_NextDemo (void)
234 if (cls.demonum == -1)
235 return; // don't play demos
237 // SCR_BeginLoadingPlaque ();
239 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
242 if (!cls.demos[cls.demonum][0])
244 Con_Printf ("No demos listed with startdemos\n");
250 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
251 Cbuf_InsertText (str);
260 void CL_PrintEntities_f (void)
265 for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
267 Con_Printf ("%3i:", i);
268 if (!ent->render.model)
270 Con_Printf ("EMPTY\n");
273 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]);
284 void CL_AllocDlight (entity_t *ent, vec3_t org, float radius, float red, float green, float blue, float decay, float lifetime)
289 // first look for an exact key match
293 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
298 // then look for anything else
300 for (i = 0;i < MAX_DLIGHTS;i++, dl++)
304 // unable to find one
308 memset (dl, 0, sizeof(*dl));
310 VectorCopy(org, dl->origin);
313 dl->color[1] = green;
316 dl->die = cl.time + lifetime;
326 void CL_DecayLights (void)
332 time = cl.time - cl.oldtime;
336 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
340 if (dl->die < cl.time)
346 c_dlights++; // count every dlight in use
348 dl->radius -= time*dl->decay;
359 Determines the fraction between the last two messages that the objects
363 float CL_LerpPoint (void)
367 f = cl.mtime[0] - cl.mtime[1];
369 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
370 if (!f || cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
372 cl.time = cl.mtime[0];
377 { // dropped packet, or start of demo
378 cl.mtime[1] = cl.mtime[0] - 0.1;
381 frac = (cl.time - cl.mtime[1]) / f;
382 // Con_Printf ("frac: %f\n",frac);
387 cl.time = cl.mtime[1];
388 // Con_Printf ("low frac\n");
396 cl.time = cl.mtime[0];
397 // Con_Printf ("high frac\n");
405 float CL_EntityLerpPoint (entity_t *ent)
409 if (cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
412 f = ent->state_current.time - ent->state_previous.time;
413 // Con_Printf(" %g-%g=%g", ent->state_current.time, ent->state_previous.time, f);
420 // Con_Printf(" %g-%g/%g=%f", cl.time, ent->state_previous.time, f, (cl.time - ent->state_previous.time) / f);
421 f = (cl.time - ent->state_previous.time) / f;
422 return bound(0, f, 1);
425 void CL_RelinkStaticEntities()
427 entity_t *ent, *endent;
428 if (cl.num_statics > MAX_VISEDICTS)
429 Host_Error("CL_RelinkStaticEntities: cl.num_statics > MAX_VISEDICTS??\n");
431 ent = cl_static_entities;
432 endent = ent + cl.num_statics;
433 for (;ent < endent;ent++)
434 cl_visedicts[cl_numvisedicts++] = ent;
442 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
443 void CL_RelinkEntities (void)
447 float frac, f, d, bobjrotate/*, bobjoffset*/, dlightradius;
448 vec3_t oldorg, delta, dlightcolor;
450 // determine partial update time
451 frac = CL_LerpPoint ();
455 CL_RelinkStaticEntities();
458 // interpolate player info
460 for (i = 0;i < 3;i++)
461 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
463 if (cls.demoplayback)
465 // interpolate the angles
466 for (j = 0;j < 3;j++)
468 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
473 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
477 bobjrotate = ANGLEMOD(100*cl.time);
478 // bobjoffset = cos(180 * cl.time * M_PI / 180) * 4.0f + 4.0f;
480 // start on the entity after the world
481 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
483 // if the object wasn't included in the latest packet, remove it
484 if (!ent->state_current.active)
487 VectorCopy (ent->render.origin, oldorg);
489 if (!ent->state_previous.active)
491 // only one state available
492 VectorCopy (ent->state_current.origin, ent->render.origin);
493 VectorCopy (ent->state_current.angles, ent->render.angles);
494 // Con_Printf(" %i", i);
498 // if the delta is large, assume a teleport and don't lerp
499 f = CL_EntityLerpPoint(ent);
502 for (j = 0;j < 3;j++)
504 delta[j] = ent->state_current.origin[j] - ent->state_previous.origin[j];
505 // LordHavoc: increased lerp tolerance from 100 to 200
506 if (delta[j] > 200 || delta[j] < -200)
513 VectorCopy (ent->state_current.origin, ent->render.origin);
514 VectorCopy (ent->state_current.angles, ent->render.angles);
518 // interpolate the origin and angles
519 for (j = 0;j < 3;j++)
521 ent->render.origin[j] = ent->state_previous.origin[j] + f*delta[j];
523 d = ent->state_current.angles[j] - ent->state_previous.angles[j];
528 ent->render.angles[j] = ent->state_previous.angles[j] + f*d;
533 ent->render.flags = ent->state_current.flags;
534 ent->render.effects = ent->state_current.effects;
535 ent->render.model = cl.model_precache[ent->state_current.modelindex];
536 ent->render.frame = ent->state_current.frame;
537 if (cl.scores == NULL || !ent->state_current.colormap)
538 ent->render.colormap = -1; // no special coloring
540 ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
541 ent->render.skinnum = ent->state_current.skin;
542 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
543 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
544 ent->render.glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
545 ent->render.glowcolor = ent->state_current.glowcolor;
546 ent->render.colormod[0] = (float) ((ent->state_current.colormod >> 5) & 7) * (1.0f / 7.0f);
547 ent->render.colormod[1] = (float) ((ent->state_current.colormod >> 2) & 7) * (1.0f / 7.0f);
548 ent->render.colormod[2] = (float) (ent->state_current.colormod & 3) * (1.0f / 3.0f);
555 // LordHavoc: if the entity has no effects, don't check each
556 if (ent->render.effects)
558 if (ent->render.effects & EF_BRIGHTFIELD)
559 R_EntityParticles (ent);
560 if (ent->render.effects & EF_MUZZLEFLASH)
564 AngleVectors (ent->render.angles, v, NULL, NULL);
566 v[0] = v[0] * 18 + ent->render.origin[0];
567 v[1] = v[1] * 18 + ent->render.origin[1];
568 v[2] = v[2] * 18 + ent->render.origin[2] + 16;
570 CL_AllocDlight (NULL, v, 100, 1, 1, 1, 0, 0.1);
572 if (ent->render.effects & EF_DIMLIGHT)
574 dlightcolor[0] += 200.0f;
575 dlightcolor[1] += 200.0f;
576 dlightcolor[2] += 200.0f;
578 if (ent->render.effects & EF_BRIGHTLIGHT)
580 dlightcolor[0] += 400.0f;
581 dlightcolor[1] += 400.0f;
582 dlightcolor[2] += 400.0f;
584 // LordHavoc: added EF_RED and EF_BLUE
585 if (ent->render.effects & EF_RED) // red
587 dlightcolor[0] += 200.0f;
588 dlightcolor[1] += 20.0f;
589 dlightcolor[2] += 20.0f;
591 if (ent->render.effects & EF_BLUE) // blue
593 dlightcolor[0] += 20.0f;
594 dlightcolor[1] += 20.0f;
595 dlightcolor[2] += 200.0f;
597 else if (ent->render.effects & EF_FLAME)
599 if (ent->render.model)
603 VectorAdd(ent->render.origin, ent->render.model->mins, mins);
604 VectorAdd(ent->render.origin, ent->render.model->maxs, maxs);
605 // how many flames to make
606 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
607 R_FlameCube(mins, maxs, temp);
609 d = lhrandom(200, 250);
610 dlightcolor[0] += d * 1.0f;
611 dlightcolor[1] += d * 0.7f;
612 dlightcolor[2] += d * 0.3f;
616 // LordHavoc: if the model has no flags, don't check each
617 if (ent->render.model && ent->render.model->flags)
619 if (ent->render.model->flags & EF_ROTATE)
621 ent->render.angles[1] = bobjrotate;
622 // ent->render.origin[2] += bobjoffset;
624 // only do trails if present in the previous frame as well
625 if (ent->state_previous.active)
627 if (ent->render.model->flags & EF_GIB)
628 R_RocketTrail (oldorg, ent->render.origin, 2, ent);
629 else if (ent->render.model->flags & EF_ZOMGIB)
630 R_RocketTrail (oldorg, ent->render.origin, 4, ent);
631 else if (ent->render.model->flags & EF_TRACER)
632 R_RocketTrail (oldorg, ent->render.origin, 3, ent);
633 else if (ent->render.model->flags & EF_TRACER2)
634 R_RocketTrail (oldorg, ent->render.origin, 5, ent);
635 else if (ent->render.model->flags & EF_ROCKET)
637 R_RocketTrail (oldorg, ent->render.origin, 0, ent);
638 dlightcolor[0] += 200.0f;
639 dlightcolor[1] += 160.0f;
640 dlightcolor[2] += 80.0f;
642 else if (ent->render.model->flags & EF_GRENADE)
644 if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
645 R_RocketTrail (oldorg, ent->render.origin, 7, ent);
647 R_RocketTrail (oldorg, ent->render.origin, 1, ent);
649 else if (ent->render.model->flags & EF_TRACER3)
650 R_RocketTrail (oldorg, ent->render.origin, 6, ent);
653 // LordHavoc: customizable glow
654 if (ent->render.glowsize)
656 byte *tempcolor = (byte *)&d_8to24table[ent->render.glowcolor];
657 dlightcolor[0] += ent->render.glowsize * tempcolor[0] * (1.0f / 255.0f);
658 dlightcolor[1] += ent->render.glowsize * tempcolor[1] * (1.0f / 255.0f);
659 dlightcolor[2] += ent->render.glowsize * tempcolor[2] * (1.0f / 255.0f);
661 // LordHavoc: customizable trail
662 if (ent->render.flags & RENDER_GLOWTRAIL)
663 R_RocketTrail2 (oldorg, ent->render.origin, ent->render.glowcolor, ent);
665 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
667 dlightradius = VectorLength(dlightcolor);
668 d = 1.0f / dlightradius;
669 CL_AllocDlight (ent, ent->render.origin, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
672 if (!chase_active.value && ((i == cl.viewentity) || (ent->render.flags & RENDER_EXTERIORMODEL)))
675 if (ent->render.model == NULL)
677 if (ent->render.effects & EF_NODRAW)
679 if (cl_numvisedicts < MAX_VISEDICTS)
680 cl_visedicts[cl_numvisedicts++] = ent;
686 // used by cl_shownet
693 Read all incoming data from the server
696 int CL_ReadFromServer (void)
700 cl.oldtime = cl.time;
701 cl.time += cl.frametime;
706 ret = CL_GetMessage ();
708 Host_Error ("CL_ReadFromServer: lost server connection");
712 cl.last_received_message = realtime;
713 CL_ParseServerMessage ();
714 } while (ret && cls.state == ca_connected);
719 CL_RelinkEntities ();
724 // bring the links up to date
734 void CL_SendCmd (void)
738 if (cls.state != ca_connected)
741 if (cls.signon == SIGNONS)
743 // get basic movement from keyboard
746 // allow mice or other external controllers to add to the move
749 // send the unreliable message
753 if (cls.demoplayback)
755 SZ_Clear (&cls.message);
759 // send the reliable message
760 if (!cls.message.cursize)
761 return; // no message at all
763 if (!NET_CanSendMessage (cls.netcon))
765 Con_DPrintf ("CL_WriteToServer: can't send\n");
769 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
770 Host_Error ("CL_WriteToServer: lost server connection");
772 SZ_Clear (&cls.message);
775 // LordHavoc: pausedemo command
776 void CL_PauseDemo_f (void)
778 cls.demopaused = !cls.demopaused;
780 Con_Printf("Demo paused\n");
782 Con_Printf("Demo unpaused\n");
786 ======================
788 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
789 ======================
791 void CL_PModel_f (void)
796 if (Cmd_Argc () == 1)
798 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
801 i = atoi(Cmd_Argv(1));
803 if (cmd_source == src_command)
805 if (cl_pmodel.value == i)
807 Cvar_SetValue ("_cl_pmodel", i);
808 if (cls.state == ca_connected)
809 Cmd_ForwardToServer ();
813 host_client->pmodel = i;
814 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
819 ======================
821 ======================
825 if (Cmd_Argc () == 1)
827 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
830 fog_density = atof(Cmd_Argv(1));
831 fog_red = atof(Cmd_Argv(2));
832 fog_green = atof(Cmd_Argv(3));
833 fog_blue = atof(Cmd_Argv(4));
843 SZ_Alloc (&cls.message, 1024);
849 // register our commands
851 Cvar_RegisterVariable (&cl_name);
852 Cvar_RegisterVariable (&cl_color);
853 Cvar_RegisterVariable (&cl_pmodel);
854 Cvar_RegisterVariable (&cl_upspeed);
855 Cvar_RegisterVariable (&cl_forwardspeed);
856 Cvar_RegisterVariable (&cl_backspeed);
857 Cvar_RegisterVariable (&cl_sidespeed);
858 Cvar_RegisterVariable (&cl_movespeedkey);
859 Cvar_RegisterVariable (&cl_yawspeed);
860 Cvar_RegisterVariable (&cl_pitchspeed);
861 Cvar_RegisterVariable (&cl_anglespeedkey);
862 Cvar_RegisterVariable (&cl_shownet);
863 Cvar_RegisterVariable (&cl_nolerp);
864 Cvar_RegisterVariable (&lookspring);
865 Cvar_RegisterVariable (&lookstrafe);
866 Cvar_RegisterVariable (&sensitivity);
867 Cvar_RegisterVariable (&freelook);
869 Cvar_RegisterVariable (&m_pitch);
870 Cvar_RegisterVariable (&m_yaw);
871 Cvar_RegisterVariable (&m_forward);
872 Cvar_RegisterVariable (&m_side);
874 // Cvar_RegisterVariable (&cl_autofire);
876 Cmd_AddCommand ("entities", CL_PrintEntities_f);
877 Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
878 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
879 Cmd_AddCommand ("record", CL_Record_f);
880 Cmd_AddCommand ("stop", CL_Stop_f);
881 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
882 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
884 Cmd_AddCommand ("fog", CL_Fog_f);
886 // LordHavoc: added pausedemo
887 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
888 // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
889 Cmd_AddCommand ("pmodel", CL_PModel_f);
890 // LordHavoc: added demo_nehahra cvar
891 Cvar_RegisterVariable (&demo_nehahra);
893 Cvar_SetValue("demo_nehahra", 1);