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 = {CVAR_SAVE, "_cl_name", "player"};
29 cvar_t cl_color = {CVAR_SAVE, "_cl_color", "0"};
30 cvar_t cl_pmodel = {CVAR_SAVE, "_cl_pmodel", "0"};
32 cvar_t cl_shownet = {0, "cl_shownet","0"};
33 cvar_t cl_nolerp = {0, "cl_nolerp", "0"};
35 cvar_t lookspring = {CVAR_SAVE, "lookspring","0"};
36 cvar_t lookstrafe = {CVAR_SAVE, "lookstrafe","0"};
37 cvar_t sensitivity = {CVAR_SAVE, "sensitivity","3", 1, 30};
39 cvar_t m_pitch = {CVAR_SAVE, "m_pitch","0.022"};
40 cvar_t m_yaw = {CVAR_SAVE, "m_yaw","0.022"};
41 cvar_t m_forward = {CVAR_SAVE, "m_forward","1"};
42 cvar_t m_side = {CVAR_SAVE, "m_side","0.8"};
44 cvar_t freelook = {CVAR_SAVE, "freelook", "1"};
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];
54 entity_t *cl_visedicts[MAX_VISEDICTS];
62 void CL_ClearState (void)
69 // wipe the entire cl structure
70 memset (&cl, 0, sizeof(cl));
72 SZ_Clear (&cls.message);
75 memset (cl_entities, 0, sizeof(cl_entities));
76 memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
77 memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
78 memset (cl_beams, 0, sizeof(cl_beams));
79 // LordHavoc: have to set up the baseline info for alpha and other stuff
80 for (i = 0;i < MAX_EDICTS;i++)
82 ClearStateToDefault(&cl_entities[i].state_baseline);
83 ClearStateToDefault(&cl_entities[i].state_previous);
84 ClearStateToDefault(&cl_entities[i].state_current);
88 void CL_LerpUpdate(entity_t *e, int frame, int modelindex)
90 entity_persistent_t *p;
95 if (p->modelindex != modelindex)
97 // reset all interpolation information
98 p->modelindex = modelindex;
99 p->frame1 = p->frame2 = frame;
100 p->frame1time = p->frame2time = cl.time;
103 else if (p->frame2 != frame)
105 // transition to new frame
106 p->frame1 = p->frame2;
107 p->frame1time = p->frame2time;
109 p->frame2time = cl.time;
115 p->framelerp = (cl.time - p->frame2time) * 10;
116 p->framelerp = bound(0, p->framelerp, 1);
119 r->frame1 = p->frame1;
120 r->frame2 = p->frame2;
121 r->framelerp = p->framelerp;
122 r->frame1time = p->frame1time;
123 r->frame2time = p->frame2time;
127 =====================
130 Sends a disconnect message to the server
131 This is also called on Host_Error, so it shouldn't cause any errors
132 =====================
134 void CL_Disconnect (void)
136 // stop sounds (especially looping!)
137 S_StopAllSounds (true);
139 // clear contents blends
140 cl.cshifts[0].percent = 0;
141 cl.cshifts[1].percent = 0;
142 cl.cshifts[2].percent = 0;
143 cl.cshifts[3].percent = 0;
145 // if running a local server, shut it down
146 if (cls.demoplayback)
148 else if (cls.state == ca_connected)
150 if (cls.demorecording)
153 Con_DPrintf ("Sending clc_disconnect\n");
154 SZ_Clear (&cls.message);
155 MSG_WriteByte (&cls.message, clc_disconnect);
156 NET_SendUnreliableMessage (cls.netcon, &cls.message);
157 SZ_Clear (&cls.message);
158 NET_Close (cls.netcon);
160 cls.state = ca_disconnected;
162 Host_ShutdownServer(false);
165 cls.demoplayback = cls.timedemo = false;
169 void CL_Disconnect_f (void)
173 Host_ShutdownServer (false);
180 =====================
181 CL_EstablishConnection
183 Host should be either "local" or a net address to be passed on
184 =====================
186 void CL_EstablishConnection (char *host)
188 if (cls.state == ca_dedicated)
191 if (cls.demoplayback)
196 cls.netcon = NET_Connect (host);
198 Host_Error ("CL_Connect: connect failed\n");
199 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
201 cls.demonum = -1; // not in the demo loop now
202 cls.state = ca_connected;
203 cls.signon = 0; // need all the signon messages before playing
207 =====================
210 An svc_signonnum has been received, perform a client side setup
211 =====================
213 void CL_SignonReply (void)
217 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
222 MSG_WriteByte (&cls.message, clc_stringcmd);
223 MSG_WriteString (&cls.message, "prespawn");
227 MSG_WriteByte (&cls.message, clc_stringcmd);
228 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
230 MSG_WriteByte (&cls.message, clc_stringcmd);
231 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
235 MSG_WriteByte (&cls.message, clc_stringcmd);
236 MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
239 MSG_WriteByte (&cls.message, clc_stringcmd);
240 sprintf (str, "spawn %s", cls.spawnparms);
241 MSG_WriteString (&cls.message, str);
245 MSG_WriteByte (&cls.message, clc_stringcmd);
246 MSG_WriteString (&cls.message, "begin");
247 Cache_Report (); // print remaining memory
251 // SCR_EndLoadingPlaque (); // allow normal screen updates
258 =====================
261 Called to play the next demo in the demo loop
262 =====================
264 void CL_NextDemo (void)
268 if (cls.demonum == -1)
269 return; // don't play demos
271 // SCR_BeginLoadingPlaque ();
273 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
276 if (!cls.demos[cls.demonum][0])
278 Con_Printf ("No demos listed with startdemos\n");
284 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
285 Cbuf_InsertText (str);
294 void CL_PrintEntities_f (void)
300 for (i = 0, ent = cl_entities;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
302 if (!ent->state_current.active)
304 if (!ent->render.model)
307 Con_Printf ("%3i:", i);
308 if (!ent->render.model)
310 Con_Printf ("EMPTY\n");
313 strncpy(name, ent->render.model->name, 30);
315 for (j = strlen(name);j < 30;j++)
317 Con_Printf ("%s:%04i (%5i %5i %5i) [%3i %3i %3i]\n", 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);
326 Determines the fraction between the last two messages that the objects
330 float CL_LerpPoint (void)
334 f = cl.mtime[0] - cl.mtime[1];
336 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
337 if (!f || cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
339 cl.time = cl.mtime[0];
344 { // dropped packet, or start of demo
345 cl.mtime[1] = cl.mtime[0] - 0.1;
348 frac = (cl.time - cl.mtime[1]) / f;
349 // Con_Printf ("frac: %f\n",frac);
354 cl.time = cl.mtime[1];
355 // Con_Printf ("low frac\n");
363 cl.time = cl.mtime[0];
364 // Con_Printf ("high frac\n");
372 float CL_EntityLerpPoint (entity_t *ent)
376 if (cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
379 f = ent->state_current.time - ent->state_previous.time;
380 // Con_Printf(" %g-%g=%g", ent->state_current.time, ent->state_previous.time, f);
387 // Con_Printf(" %g-%g/%g=%f", cl.time, ent->state_previous.time, f, (cl.time - ent->state_previous.time) / f);
388 f = (cl.time - ent->state_previous.time) / f;
389 return bound(0, f, 1);
392 void CL_RelinkStaticEntities(void)
395 for (i = 0;i < cl.num_statics && cl_numvisedicts < MAX_VISEDICTS;i++)
396 cl_visedicts[cl_numvisedicts++] = &cl_static_entities[i];
404 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
405 void CL_RelinkNetworkEntities()
408 int i, j, glowcolor, effects;
409 float f, d, bobjrotate/*, bobjoffset*/, dlightradius, glowsize;
410 vec3_t oldorg, neworg, delta, dlightcolor;
412 bobjrotate = ANGLEMOD(100*cl.time);
413 // bobjoffset = cos(180 * cl.time * M_PI / 180) * 4.0f + 4.0f;
415 CL_RelinkStaticEntities();
417 // start on the entity after the world
418 for (i = 1, ent = cl_entities + 1;i < MAX_EDICTS /*cl.num_entities*/;i++, ent++)
420 // if the object wasn't included in the latest packet, remove it
421 if (!ent->state_current.active)
424 VectorCopy(ent->persistent.trail_origin, oldorg);
426 if (!ent->state_previous.active)
428 // only one state available
429 VectorCopy (ent->state_current.origin, neworg);
430 VectorCopy (ent->state_current.angles, ent->render.angles);
434 // if the delta is large, assume a teleport and don't lerp
435 VectorSubtract(ent->state_current.origin, ent->state_previous.origin, delta);
436 // LordHavoc: increased tolerance from 100 to 200
437 if (DotProduct(delta, delta) > 200*200)
440 f = CL_EntityLerpPoint(ent);
444 VectorCopy (ent->state_current.origin, neworg);
445 VectorCopy (ent->state_current.angles, ent->render.angles);
449 // interpolate the origin and angles
450 for (j = 0;j < 3;j++)
452 neworg[j] = ent->state_previous.origin[j] + f*delta[j];
454 d = ent->state_current.angles[j] - ent->state_previous.angles[j];
459 ent->render.angles[j] = ent->state_previous.angles[j] + f*d;
464 VectorCopy (neworg, ent->persistent.trail_origin);
465 // persistent.modelindex will be updated by CL_LerpUpdate
466 if (ent->state_current.modelindex != ent->persistent.modelindex)
467 VectorCopy(neworg, oldorg);
469 VectorCopy (neworg, ent->render.origin);
470 ent->render.flags = ent->state_current.flags;
471 ent->render.effects = effects = ent->state_current.effects;
472 ent->render.model = cl.model_precache[ent->state_current.modelindex];
473 ent->render.frame = ent->state_current.frame;
474 if (cl.scores == NULL || !ent->state_current.colormap)
475 ent->render.colormap = -1; // no special coloring
477 ent->render.colormap = cl.scores[ent->state_current.colormap - 1].colors; // color it
478 ent->render.skinnum = ent->state_current.skin;
479 ent->render.alpha = ent->state_current.alpha * (1.0f / 255.0f); // FIXME: interpolate?
480 ent->render.scale = ent->state_current.scale * (1.0f / 16.0f); // FIXME: interpolate?
481 glowsize = ent->state_current.glowsize * 4.0f; // FIXME: interpolate?
482 glowcolor = ent->state_current.glowcolor;
483 ent->render.colormod[0] = (float) ((ent->state_current.colormod >> 5) & 7) * (1.0f / 7.0f);
484 ent->render.colormod[1] = (float) ((ent->state_current.colormod >> 2) & 7) * (1.0f / 7.0f);
485 ent->render.colormod[2] = (float) (ent->state_current.colormod & 3) * (1.0f / 3.0f);
487 // update interpolation info
488 CL_LerpUpdate(ent, ent->state_current.frame, ent->state_current.modelindex);
490 // handle effects now...
496 // LordHavoc: if the entity has no effects, don't check each
499 if (effects & EF_BRIGHTFIELD)
500 R_EntityParticles (ent);
501 if (effects & EF_MUZZLEFLASH)
505 AngleVectors (ent->render.angles, v, NULL, NULL);
507 v2[0] = v[0] * 18 + neworg[0];
508 v2[1] = v[1] * 18 + neworg[1];
509 v2[2] = v[2] * 18 + neworg[2] + 16;
510 TraceLine(neworg, v2, v, NULL, 0);
512 CL_AllocDlight (NULL, v, 100, 1, 1, 1, 0, 0.1);
514 if (effects & EF_DIMLIGHT)
516 dlightcolor[0] += 200.0f;
517 dlightcolor[1] += 200.0f;
518 dlightcolor[2] += 200.0f;
520 if (effects & EF_BRIGHTLIGHT)
522 dlightcolor[0] += 400.0f;
523 dlightcolor[1] += 400.0f;
524 dlightcolor[2] += 400.0f;
526 // LordHavoc: added EF_RED and EF_BLUE
527 if (effects & EF_RED) // red
529 dlightcolor[0] += 200.0f;
530 dlightcolor[1] += 20.0f;
531 dlightcolor[2] += 20.0f;
533 if (effects & EF_BLUE) // blue
535 dlightcolor[0] += 20.0f;
536 dlightcolor[1] += 20.0f;
537 dlightcolor[2] += 200.0f;
539 if (effects & EF_FLAME)
541 if (ent->render.model)
545 if (ent->render.angles[0] || ent->render.angles[2])
547 VectorAdd(neworg, ent->render.model->rotatedmins, mins);
548 VectorAdd(neworg, ent->render.model->rotatedmaxs, maxs);
550 else if (ent->render.angles[1])
552 VectorAdd(neworg, ent->render.model->yawmins, mins);
553 VectorAdd(neworg, ent->render.model->yawmaxs, maxs);
557 VectorAdd(neworg, ent->render.model->normalmins, mins);
558 VectorAdd(neworg, ent->render.model->normalmaxs, maxs);
560 // how many flames to make
561 temp = (int) (cl.time * 300) - (int) (cl.oldtime * 300);
562 R_FlameCube(mins, maxs, temp);
564 d = lhrandom(200, 250);
565 dlightcolor[0] += d * 1.0f;
566 dlightcolor[1] += d * 0.7f;
567 dlightcolor[2] += d * 0.3f;
571 // LordHavoc: if the model has no flags, don't check each
572 if (ent->render.model && ent->render.model->flags)
574 if (ent->render.model->flags & EF_ROTATE)
576 ent->render.angles[1] = bobjrotate;
577 // ent->render.origin[2] += bobjoffset;
579 // only do trails if present in the previous frame as well
580 if (ent->state_previous.active)
582 if (ent->render.model->flags & EF_GIB)
583 R_RocketTrail (oldorg, neworg, 2, ent);
584 else if (ent->render.model->flags & EF_ZOMGIB)
585 R_RocketTrail (oldorg, neworg, 4, ent);
586 else if (ent->render.model->flags & EF_TRACER)
587 R_RocketTrail (oldorg, neworg, 3, ent);
588 else if (ent->render.model->flags & EF_TRACER2)
589 R_RocketTrail (oldorg, neworg, 5, ent);
590 else if (ent->render.model->flags & EF_ROCKET)
592 R_RocketTrail (oldorg, ent->render.origin, 0, ent);
593 dlightcolor[0] += 200.0f;
594 dlightcolor[1] += 160.0f;
595 dlightcolor[2] += 80.0f;
597 else if (ent->render.model->flags & EF_GRENADE)
599 if (ent->render.alpha == -1) // LordHavoc: Nehahra dem compatibility
600 R_RocketTrail (oldorg, neworg, 7, ent);
602 R_RocketTrail (oldorg, neworg, 1, ent);
604 else if (ent->render.model->flags & EF_TRACER3)
605 R_RocketTrail (oldorg, neworg, 6, ent);
608 // LordHavoc: customizable glow
611 byte *tempcolor = (byte *)&d_8to24table[glowcolor];
612 dlightcolor[0] += glowsize * tempcolor[0] * (1.0f / 255.0f);
613 dlightcolor[1] += glowsize * tempcolor[1] * (1.0f / 255.0f);
614 dlightcolor[2] += glowsize * tempcolor[2] * (1.0f / 255.0f);
616 // LordHavoc: customizable trail
617 if (ent->render.flags & RENDER_GLOWTRAIL)
618 R_RocketTrail2 (oldorg, neworg, glowcolor, ent);
620 if (dlightcolor[0] || dlightcolor[1] || dlightcolor[2])
623 dlightradius = VectorLength(dlightcolor);
624 d = 1.0f / dlightradius;
625 VectorCopy(neworg, vec);
626 // hack to make glowing player light shine on their gun
627 if (i == cl.viewentity && !chase_active.value)
629 CL_AllocDlight (&ent->render, vec, dlightradius, dlightcolor[0] * d, dlightcolor[1] * d, dlightcolor[2] * d, 0, 0);
632 if (chase_active.value)
634 if (ent->render.flags & RENDER_VIEWMODEL)
639 if (i == cl.viewentity || (ent->render.flags & RENDER_EXTERIORMODEL))
643 if (ent->render.model == NULL)
645 if (effects & EF_NODRAW)
647 if (cl_numvisedicts < MAX_VISEDICTS)
648 cl_visedicts[cl_numvisedicts++] = ent;
652 void CL_LerpPlayerVelocity (void)
657 // fraction from previous network update to current
658 frac = CL_LerpPoint ();
660 for (i = 0;i < 3;i++)
661 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
663 if (cls.demoplayback)
665 // interpolate the angles
666 for (i = 0;i < 3;i++)
668 d = cl.mviewangles[0][i] - cl.mviewangles[1][i];
673 cl.viewangles[i] = cl.mviewangles[1][i] + frac*d;
678 void CL_RelinkEntities (void)
682 CL_LerpPlayerVelocity();
683 CL_RelinkNetworkEntities();
691 Read all incoming data from the server
694 int CL_ReadFromServer (void)
698 cl.oldtime = cl.time;
699 cl.time += cl.frametime;
704 ret = CL_GetMessage ();
706 Host_Error ("CL_ReadFromServer: lost server connection");
710 cl.last_received_message = realtime;
712 if (cl_shownet.value)
715 CL_ParseServerMessage ();
717 while (ret && cls.state == ca_connected);
722 CL_RelinkEntities ();
727 // bring the links up to date
737 void CL_SendCmd (void)
741 if (cls.state != ca_connected)
744 if (cls.signon == SIGNONS)
746 // get basic movement from keyboard
749 // allow mice or other external controllers to add to the move
752 // send the unreliable message
756 if (cls.demoplayback)
758 SZ_Clear (&cls.message);
762 // send the reliable message
763 if (!cls.message.cursize)
764 return; // no message at all
766 if (!NET_CanSendMessage (cls.netcon))
768 Con_DPrintf ("CL_WriteToServer: can't send\n");
772 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
773 Host_Error ("CL_WriteToServer: lost server connection");
775 SZ_Clear (&cls.message);
778 // LordHavoc: pausedemo command
779 void CL_PauseDemo_f (void)
781 cls.demopaused = !cls.demopaused;
783 Con_Printf("Demo paused\n");
785 Con_Printf("Demo unpaused\n");
789 ======================
791 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
792 ======================
794 void CL_PModel_f (void)
799 if (Cmd_Argc () == 1)
801 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
804 i = atoi(Cmd_Argv(1));
806 if (cmd_source == src_command)
808 if (cl_pmodel.value == i)
810 Cvar_SetValue ("_cl_pmodel", i);
811 if (cls.state == ca_connected)
812 Cmd_ForwardToServer ();
816 host_client->pmodel = i;
817 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
822 ======================
824 ======================
828 if (Cmd_Argc () == 1)
830 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
833 fog_density = atof(Cmd_Argv(1));
834 fog_red = atof(Cmd_Argv(2));
835 fog_green = atof(Cmd_Argv(3));
836 fog_blue = atof(Cmd_Argv(4));
846 SZ_Alloc (&cls.message, 1024);
852 // register our commands
854 Cvar_RegisterVariable (&cl_name);
855 Cvar_RegisterVariable (&cl_color);
856 Cvar_RegisterVariable (&cl_pmodel);
857 Cvar_RegisterVariable (&cl_upspeed);
858 Cvar_RegisterVariable (&cl_forwardspeed);
859 Cvar_RegisterVariable (&cl_backspeed);
860 Cvar_RegisterVariable (&cl_sidespeed);
861 Cvar_RegisterVariable (&cl_movespeedkey);
862 Cvar_RegisterVariable (&cl_yawspeed);
863 Cvar_RegisterVariable (&cl_pitchspeed);
864 Cvar_RegisterVariable (&cl_anglespeedkey);
865 Cvar_RegisterVariable (&cl_shownet);
866 Cvar_RegisterVariable (&cl_nolerp);
867 Cvar_RegisterVariable (&lookspring);
868 Cvar_RegisterVariable (&lookstrafe);
869 Cvar_RegisterVariable (&sensitivity);
870 Cvar_RegisterVariable (&freelook);
872 Cvar_RegisterVariable (&m_pitch);
873 Cvar_RegisterVariable (&m_yaw);
874 Cvar_RegisterVariable (&m_forward);
875 Cvar_RegisterVariable (&m_side);
877 // Cvar_RegisterVariable (&cl_autofire);
879 Cmd_AddCommand ("entities", CL_PrintEntities_f);
880 Cmd_AddCommand ("bitprofile", CL_BitProfile_f);
881 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
882 Cmd_AddCommand ("record", CL_Record_f);
883 Cmd_AddCommand ("stop", CL_Stop_f);
884 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
885 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
887 Cmd_AddCommand ("fog", CL_Fog_f);
889 // LordHavoc: added pausedemo
890 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
891 // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
892 Cmd_AddCommand ("pmodel", CL_PModel_f);