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].baseline.alpha = 255;
86 cl_entities[i].baseline.scale = 16;
87 cl_entities[i].baseline.glowsize = 0;
88 cl_entities[i].baseline.glowcolor = 254;
89 cl_entities[i].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 // bring the console down and fade the colors back to normal
115 // SCR_BringDownConsole ();
117 // if running a local server, shut it down
118 if (cls.demoplayback)
120 else if (cls.state == ca_connected)
122 if (cls.demorecording)
125 Con_DPrintf ("Sending clc_disconnect\n");
126 SZ_Clear (&cls.message);
127 MSG_WriteByte (&cls.message, clc_disconnect);
128 NET_SendUnreliableMessage (cls.netcon, &cls.message);
129 SZ_Clear (&cls.message);
130 NET_Close (cls.netcon);
132 cls.state = ca_disconnected;
134 Host_ShutdownServer(false);
137 cls.demoplayback = cls.timedemo = false;
141 void CL_Disconnect_f (void)
145 Host_ShutdownServer (false);
152 =====================
153 CL_EstablishConnection
155 Host should be either "local" or a net address to be passed on
156 =====================
158 void CL_EstablishConnection (char *host)
160 if (cls.state == ca_dedicated)
163 if (cls.demoplayback)
168 cls.netcon = NET_Connect (host);
170 Host_Error ("CL_Connect: connect failed\n");
171 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
173 cls.demonum = -1; // not in the demo loop now
174 cls.state = ca_connected;
175 cls.signon = 0; // need all the signon messages before playing
179 =====================
182 An svc_signonnum has been received, perform a client side setup
183 =====================
185 void CL_SignonReply (void)
189 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
194 MSG_WriteByte (&cls.message, clc_stringcmd);
195 MSG_WriteString (&cls.message, "prespawn");
199 MSG_WriteByte (&cls.message, clc_stringcmd);
200 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
202 MSG_WriteByte (&cls.message, clc_stringcmd);
203 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
207 MSG_WriteByte (&cls.message, clc_stringcmd);
208 MSG_WriteString (&cls.message, va("pmodel %f\n", cl_pmodel.value));
211 MSG_WriteByte (&cls.message, clc_stringcmd);
212 sprintf (str, "spawn %s", cls.spawnparms);
213 MSG_WriteString (&cls.message, str);
217 MSG_WriteByte (&cls.message, clc_stringcmd);
218 MSG_WriteString (&cls.message, "begin");
219 Cache_Report (); // print remaining memory
223 SCR_EndLoadingPlaque (); // allow normal screen updates
229 =====================
232 Called to play the next demo in the demo loop
233 =====================
235 void CL_NextDemo (void)
239 if (cls.demonum == -1)
240 return; // don't play demos
242 SCR_BeginLoadingPlaque ();
244 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
247 if (!cls.demos[cls.demonum][0])
249 Con_Printf ("No demos listed with startdemos\n");
255 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
256 Cbuf_InsertText (str);
265 void CL_PrintEntities_f (void)
270 for (i=0,ent=cl_entities ; i<cl.num_entities ; i++,ent++)
272 Con_Printf ("%3i:",i);
275 Con_Printf ("EMPTY\n");
278 Con_Printf ("%s:%2i (%5.1f,%5.1f,%5.1f) [%5.1f %5.1f %5.1f]\n", ent->model->name, ent->frame, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2]);
289 dlight_t *CL_AllocDlight (int key)
294 // first look for an exact key match
298 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
302 memset (dl, 0, sizeof(*dl));
309 // then look for anything else
311 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
313 if (dl->die < cl.time)
315 memset (dl, 0, sizeof(*dl));
322 memset (dl, 0, sizeof(*dl));
334 void CL_DecayLights (void)
340 time = cl.time - cl.oldtime;
344 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
346 if (dl->die < cl.time || !dl->radius)
349 c_dlights++; // count every dlight in use
351 dl->radius -= time*dl->decay;
362 Determines the fraction between the last two messages that the objects
366 float CL_LerpPoint (void)
370 f = cl.mtime[0] - cl.mtime[1];
372 // LordHavoc: lerp in listen games as the server is being capped below the client (usually)
373 if (!f || cl_nolerp.value || cls.timedemo || (sv.active && svs.maxclients == 1))
375 cl.time = cl.mtime[0];
380 { // dropped packet, or start of demo
381 cl.mtime[1] = cl.mtime[0] - 0.1;
384 frac = (cl.time - cl.mtime[1]) / f;
385 //Con_Printf ("frac: %f\n",frac);
390 cl.time = cl.mtime[1];
391 // Con_Printf ("low frac\n");
399 cl.time = cl.mtime[0];
400 // Con_Printf ("high frac\n");
414 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
415 void CL_RelinkEntities (void)
426 // determine partial update time
427 frac = CL_LerpPoint ();
432 // interpolate player info
434 for (i=0 ; i<3 ; i++)
435 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
437 if (cls.demoplayback)
439 // interpolate the angles
440 for (j=0 ; j<3 ; j++)
442 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
447 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
451 bobjrotate = anglemod(100*cl.time);
453 // start on the entity after the world
454 for (i=1,ent=cl_entities+1 ; i<cl.num_entities ; i++,ent++)
458 // if (ent->forcelink)
459 // R_RemoveEfrags (ent); // just became empty
463 // if the object wasn't included in the last packet, remove it
464 if (ent->msgtime != cl.mtime[0])
467 // LordHavoc: free on the same frame, not the next
468 // if (ent->forcelink)
469 // R_RemoveEfrags (ent); // just became empty
473 VectorCopy (ent->origin, oldorg);
476 { // the entity was not updated in the last message
477 // so move to the final spot
478 VectorCopy (ent->msg_origins[0], ent->origin);
479 VectorCopy (ent->msg_angles[0], ent->angles);
482 { // if the delta is large, assume a teleport and don't lerp
484 for (j=0 ; j<3 ; j++)
486 delta[j] = ent->msg_origins[0][j] - ent->msg_origins[1][j];
487 // LordHavoc: increased lerp tolerance from 100 to 200
488 if (delta[j] > 200 || delta[j] < -200)
489 f = 1; // assume a teleportation, not a motion
492 // interpolate the origin and angles
493 for (j=0 ; j<3 ; j++)
495 ent->origin[j] = ent->msg_origins[1][j] + f*delta[j];
497 d = ent->msg_angles[0][j] - ent->msg_angles[1][j];
502 ent->angles[j] = ent->msg_angles[1][j] + f*d;
507 if (ent->effects & EF_BRIGHTFIELD)
508 R_EntityParticles (ent);
509 if (ent->effects & EF_MUZZLEFLASH)
513 dl = CL_AllocDlight (i);
514 VectorCopy (ent->origin, dl->origin);
516 AngleVectors (ent->angles, fv, NULL, NULL);
518 VectorMA (dl->origin, 18, fv, dl->origin);
519 dl->radius = 100 + (rand()&31);
520 dl->die = cl.time + 0.1;
521 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
523 if (ent->effects & EF_BRIGHTLIGHT)
525 dl = CL_AllocDlight (i);
526 VectorCopy (ent->origin, dl->origin);
528 dl->radius = 400 + (rand()&31);
529 dl->die = cl.time + 0.001;
530 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
532 if (ent->effects & EF_DIMLIGHT)
534 dl = CL_AllocDlight (i);
535 VectorCopy (ent->origin, dl->origin);
536 dl->radius = 200 + (rand()&31);
537 dl->die = cl.time + 0.001;
538 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
540 // LordHavoc: added EF_RED and EF_BLUE
541 if (ent->effects & EF_RED) // red
543 if (ent->effects & EF_BLUE) // magenta
545 dl = CL_AllocDlight (i);
546 VectorCopy (ent->origin, dl->origin);
547 dl->radius = 200 + (rand()&31);
548 dl->die = cl.time + 0.001;
549 dl->color[0] = 0.7;dl->color[1] = 0.07;dl->color[2] = 0.7;
553 dl = CL_AllocDlight (i);
554 VectorCopy (ent->origin, dl->origin);
555 dl->radius = 200 + (rand()&31);
556 dl->die = cl.time + 0.001;
557 dl->color[0] = 0.8;dl->color[1] = 0.05;dl->color[2] = 0.05;
560 else if (ent->effects & EF_BLUE) // blue
562 dl = CL_AllocDlight (i);
563 VectorCopy (ent->origin, dl->origin);
564 dl->radius = 200 + (rand()&31);
565 dl->die = cl.time + 0.001;
566 dl->color[0] = 0.05;dl->color[1] = 0.05;dl->color[2] = 0.8;
568 else if (ent->effects & EF_FLAME)
574 VectorAdd(ent->origin, ent->model->mins, mins);
575 VectorAdd(ent->origin, ent->model->maxs, maxs);
576 // how many flames to make
577 temp = (int) (cl.time * 30) - (int) (cl.oldtime * 30);
578 R_FlameCube(mins, maxs, temp);
580 dl = CL_AllocDlight (i);
581 VectorCopy (ent->origin, dl->origin);
582 dl->radius = 200 + (rand()&31);
583 dl->die = cl.time + 0.25;
584 dl->decay = dl->radius * 4;
585 dl->color[0] = 1.0;dl->color[1] = 0.7;dl->color[2] = 0.3;
588 if (ent->model->flags) // LordHavoc: if the model has no flags, don't check each
590 // rotate binary objects locally
591 if (ent->model->flags & EF_ROTATE)
592 ent->angles[1] = bobjrotate;
593 if (ent->model->flags & EF_GIB)
594 R_RocketTrail (oldorg, ent->origin, 2, ent);
595 else if (ent->model->flags & EF_ZOMGIB)
596 R_RocketTrail (oldorg, ent->origin, 4, ent);
597 else if (ent->model->flags & EF_TRACER)
598 R_RocketTrail (oldorg, ent->origin, 3, ent);
599 else if (ent->model->flags & EF_TRACER2)
600 R_RocketTrail (oldorg, ent->origin, 5, ent);
601 else if (ent->model->flags & EF_ROCKET)
603 R_RocketTrail (oldorg, ent->origin, 0, ent);
604 dl = CL_AllocDlight (i);
605 VectorCopy (ent->origin, dl->origin);
607 dl->die = cl.time + 0.001;
608 dl->color[0] = 1.0;dl->color[1] = 0.8;dl->color[2] = 0.4;
610 else if (ent->model->flags & EF_GRENADE)
612 if (ent->alpha == -1) // LordHavoc: Nehahra dem compatibility
613 R_RocketTrail (oldorg, ent->origin, 7, ent);
615 R_RocketTrail (oldorg, ent->origin, 1, ent);
617 else if (ent->model->flags & EF_TRACER3)
618 R_RocketTrail (oldorg, ent->origin, 6, ent);
620 if (ent->glowsize) // LordHavoc: customizable glow
622 dl = CL_AllocDlight (i);
623 VectorCopy (ent->origin, dl->origin);
624 dl->radius = ent->glowsize;
625 dl->die = cl.time + 0.001;
626 tempcolor = (byte *)&d_8to24table[ent->glowcolor];
627 dl->color[0] = tempcolor[0]*(1.0/255.0);dl->color[1] = tempcolor[1]*(1.0/255.0);dl->color[2] = tempcolor[2]*(1.0/255.0);
629 if (ent->glowtrail) // LordHavoc: customizable glow and trail
630 R_RocketTrail2 (oldorg, ent->origin, ent->glowcolor, ent);
632 ent->forcelink = false;
634 if (i == cl.viewentity && !chase_active.value)
637 // LordHavoc: enabled EF_NODRAW
638 if (!ent->model || ent->effects & EF_NODRAW)
640 if (cl_numvisedicts < MAX_VISEDICTS)
641 cl_visedicts[cl_numvisedicts++] = ent;
651 Read all incoming data from the server
654 int CL_ReadFromServer (void)
658 cl.oldtime = cl.time;
659 cl.time += cl.frametime;
663 ret = CL_GetMessage ();
665 Host_Error ("CL_ReadFromServer: lost server connection");
669 cl.last_received_message = realtime;
670 CL_ParseServerMessage ();
671 } while (ret && cls.state == ca_connected);
673 if (cl_shownet.value)
676 CL_RelinkEntities ();
681 // bring the links up to date
691 void CL_SendCmd (void)
695 if (cls.state != ca_connected)
698 if (cls.signon == SIGNONS)
700 // get basic movement from keyboard
703 // allow mice or other external controllers to add to the move
706 // send the unreliable message
710 if (cls.demoplayback)
712 SZ_Clear (&cls.message);
716 // send the reliable message
717 if (!cls.message.cursize)
718 return; // no message at all
720 if (!NET_CanSendMessage (cls.netcon))
722 Con_DPrintf ("CL_WriteToServer: can't send\n");
726 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
727 Host_Error ("CL_WriteToServer: lost server connection");
729 SZ_Clear (&cls.message);
732 // LordHavoc: pausedemo command
733 void CL_PauseDemo_f (void)
735 cls.demopaused = !cls.demopaused;
737 Con_Printf("Demo paused\n");
739 Con_Printf("Demo unpaused\n");
743 ======================
745 LordHavoc: Intended for Nehahra, I personally think this is dumb, but Mindcrime won't listen.
746 ======================
748 void CL_PModel_f (void)
753 if (Cmd_Argc () == 1)
755 Con_Printf ("\"pmodel\" is \"%s\"\n", cl_pmodel.string);
758 i = atoi(Cmd_Argv(1));
760 if (cmd_source == src_command)
762 if (cl_pmodel.value == i)
764 Cvar_SetValue ("_cl_pmodel", i);
765 if (cls.state == ca_connected)
766 Cmd_ForwardToServer ();
770 host_client->pmodel = i;
771 if ((val = GETEDICTFIELDVALUE(host_client->edict, eval_pmodel)))
776 ======================
778 ======================
782 if (Cmd_Argc () == 1)
784 Con_Printf ("\"fog\" is \"%f %f %f %f\"\n", fog_density, fog_red, fog_green, fog_blue);
787 fog_density = atof(Cmd_Argv(1));
788 fog_red = atof(Cmd_Argv(2));
789 fog_green = atof(Cmd_Argv(3));
790 fog_blue = atof(Cmd_Argv(4));
793 cvar_t demo_nehahra = {"demo_nehahra", "0"};
802 SZ_Alloc (&cls.message, 1024);
808 // register our commands
810 Cvar_RegisterVariable (&cl_name);
811 Cvar_RegisterVariable (&cl_color);
812 Cvar_RegisterVariable (&cl_pmodel);
813 Cvar_RegisterVariable (&cl_upspeed);
814 Cvar_RegisterVariable (&cl_forwardspeed);
815 Cvar_RegisterVariable (&cl_backspeed);
816 Cvar_RegisterVariable (&cl_sidespeed);
817 Cvar_RegisterVariable (&cl_movespeedkey);
818 Cvar_RegisterVariable (&cl_yawspeed);
819 Cvar_RegisterVariable (&cl_pitchspeed);
820 Cvar_RegisterVariable (&cl_anglespeedkey);
821 Cvar_RegisterVariable (&cl_shownet);
822 Cvar_RegisterVariable (&cl_nolerp);
823 Cvar_RegisterVariable (&lookspring);
824 Cvar_RegisterVariable (&lookstrafe);
825 Cvar_RegisterVariable (&sensitivity);
827 Cvar_RegisterVariable (&m_pitch);
828 Cvar_RegisterVariable (&m_yaw);
829 Cvar_RegisterVariable (&m_forward);
830 Cvar_RegisterVariable (&m_side);
832 // Cvar_RegisterVariable (&cl_autofire);
834 Cmd_AddCommand ("entities", CL_PrintEntities_f);
835 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
836 Cmd_AddCommand ("record", CL_Record_f);
837 Cmd_AddCommand ("stop", CL_Stop_f);
838 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
839 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
841 Cmd_AddCommand ("fog", CL_Fog_f);
843 // LordHavoc: added pausedemo
844 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
845 // LordHavoc: added pmodel command (like name, etc, only intended for Nehahra)
846 Cmd_AddCommand ("pmodel", CL_PModel_f);
847 // LordHavoc: added demo_nehahra cvar
848 Cvar_RegisterVariable (&demo_nehahra);
850 Cvar_SetValue("demo_nehahra", 1);