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};
31 cvar_t cl_shownet = {"cl_shownet","0"}; // can be 0, 1, or 2
32 cvar_t cl_nolerp = {"cl_nolerp","0"};
34 cvar_t maxfps = {"maxfps", "100"};
36 cvar_t lookspring = {"lookspring","0", true};
37 cvar_t lookstrafe = {"lookstrafe","0", true};
38 cvar_t sensitivity = {"sensitivity","3", true};
40 cvar_t m_pitch = {"m_pitch","0.022", true};
41 cvar_t m_yaw = {"m_yaw","0.022", true};
42 cvar_t m_forward = {"m_forward","1", true};
43 cvar_t m_side = {"m_side","0.8", true};
48 // FIXME: put these on hunk?
49 efrag_t cl_efrags[MAX_EFRAGS];
50 entity_t cl_entities[MAX_EDICTS];
51 entity_t cl_static_entities[MAX_STATIC_ENTITIES];
52 lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
53 dlight_t cl_dlights[MAX_DLIGHTS];
56 entity_t *cl_visedicts[MAX_VISEDICTS];
64 void CL_ClearState (void)
71 // wipe the entire cl structure
72 memset (&cl, 0, sizeof(cl));
74 SZ_Clear (&cls.message);
77 memset (cl_efrags, 0, sizeof(cl_efrags));
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 cl_entities[i].baseline.alpha = 255;
87 cl_entities[i].baseline.scale = 16;
88 cl_entities[i].baseline.glowsize = 0;
89 cl_entities[i].baseline.glowcolor = 254;
90 cl_entities[i].baseline.colormod = 255;
94 // allocate the efrags and chain together into a free list
96 cl.free_efrags = cl_efrags;
97 for (i=0 ; i<MAX_EFRAGS-1 ; i++)
98 cl.free_efrags[i].entnext = &cl.free_efrags[i+1];
99 cl.free_efrags[i].entnext = NULL;
103 =====================
106 Sends a disconnect message to the server
107 This is also called on Host_Error, so it shouldn't cause any errors
108 =====================
110 void CL_Disconnect (void)
112 // stop sounds (especially looping!)
113 S_StopAllSounds (true);
115 // bring the console down and fade the colors back to normal
116 // SCR_BringDownConsole ();
118 // if running a local server, shut it down
119 if (cls.demoplayback)
121 else if (cls.state == ca_connected)
123 if (cls.demorecording)
126 Con_DPrintf ("Sending clc_disconnect\n");
127 SZ_Clear (&cls.message);
128 MSG_WriteByte (&cls.message, clc_disconnect);
129 NET_SendUnreliableMessage (cls.netcon, &cls.message);
130 SZ_Clear (&cls.message);
131 NET_Close (cls.netcon);
133 cls.state = ca_disconnected;
135 Host_ShutdownServer(false);
138 cls.demoplayback = cls.timedemo = false;
142 void CL_Disconnect_f (void)
146 Host_ShutdownServer (false);
153 =====================
154 CL_EstablishConnection
156 Host should be either "local" or a net address to be passed on
157 =====================
159 void CL_EstablishConnection (char *host)
161 if (cls.state == ca_dedicated)
164 if (cls.demoplayback)
169 cls.netcon = NET_Connect (host);
171 Host_Error ("CL_Connect: connect failed\n");
172 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
174 cls.demonum = -1; // not in the demo loop now
175 cls.state = ca_connected;
176 cls.signon = 0; // need all the signon messages before playing
179 extern int numgltextures;
183 =====================
186 An svc_signonnum has been received, perform a client side setup
187 =====================
189 void CL_SignonReply (void)
193 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
198 MSG_WriteByte (&cls.message, clc_stringcmd);
199 MSG_WriteString (&cls.message, "prespawn");
203 MSG_WriteByte (&cls.message, clc_stringcmd);
204 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
206 MSG_WriteByte (&cls.message, clc_stringcmd);
207 MSG_WriteString (&cls.message, va("color %i %i\n", ((int)cl_color.value)>>4, ((int)cl_color.value)&15));
209 MSG_WriteByte (&cls.message, clc_stringcmd);
210 sprintf (str, "spawn %s", cls.spawnparms);
211 MSG_WriteString (&cls.message, str);
215 MSG_WriteByte (&cls.message, clc_stringcmd);
216 MSG_WriteString (&cls.message, "begin");
217 Cache_Report (); // print remaining memory
221 SCR_EndLoadingPlaque (); // allow normal screen updates
222 // LordHavoc: debugging purposes
223 Con_DPrintf("GLQuake texture slots in use: %i : %i : %i texels\n", texture_extension_number, numgltextures, texels);
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"
279 ,ent->model->name,ent->frame, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2]);
288 Debugging tool, just flashes the screen
303 VID_SetPalette (host_basepal);
306 for (c=0 ; c<768 ; c+=3)
312 VID_SetPalette (pal);
316 for (c=0 ; c<768 ; c+=3)
322 VID_SetPalette (pal);
333 dlight_t *CL_AllocDlight (int key)
338 // first look for an exact key match
342 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
346 memset (dl, 0, sizeof(*dl));
353 // then look for anything else
355 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
357 if (dl->die < cl.time)
359 memset (dl, 0, sizeof(*dl));
366 memset (dl, 0, sizeof(*dl));
378 void CL_DecayLights (void)
384 time = cl.time - cl.oldtime;
387 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
389 if (dl->die < cl.time || !dl->radius)
392 dl->radius -= time*dl->decay;
403 Determines the fraction between the last two messages that the objects
407 float CL_LerpPoint (void)
411 f = cl.mtime[0] - cl.mtime[1];
413 if (!f || cl_nolerp.value || cls.timedemo || sv.active)
415 cl.time = cl.mtime[0];
420 { // dropped packet, or start of demo
421 cl.mtime[1] = cl.mtime[0] - 0.1;
424 frac = (cl.time - cl.mtime[1]) / f;
425 //Con_Printf ("frac: %f\n",frac);
431 cl.time = cl.mtime[1];
432 // Con_Printf ("low frac\n");
441 cl.time = cl.mtime[0];
442 // Con_Printf ("high frac\n");
458 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
459 void CL_RelinkEntities (void)
470 // determine partial update time
471 frac = CL_LerpPoint ();
476 // interpolate player info
478 for (i=0 ; i<3 ; i++)
479 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
481 if (cls.demoplayback)
483 // interpolate the angles
484 for (j=0 ; j<3 ; j++)
486 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
491 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
495 bobjrotate = anglemod(100*cl.time);
497 // start on the entity after the world
498 for (i=1,ent=cl_entities+1 ; i<cl.num_entities ; i++,ent++)
503 R_RemoveEfrags (ent); // just became empty
507 // if the object wasn't included in the last packet, remove it
508 if (ent->msgtime != cl.mtime[0])
514 VectorCopy (ent->origin, oldorg);
517 { // the entity was not updated in the last message
518 // so move to the final spot
519 VectorCopy (ent->msg_origins[0], ent->origin);
520 VectorCopy (ent->msg_angles[0], ent->angles);
523 { // if the delta is large, assume a teleport and don't lerp
525 for (j=0 ; j<3 ; j++)
527 delta[j] = ent->msg_origins[0][j] - ent->msg_origins[1][j];
528 // LordHavoc: increased lerp tolerance from 100 to 200
529 if (delta[j] > 200 || delta[j] < -200)
530 f = 1; // assume a teleportation, not a motion
533 // interpolate the origin and angles
534 for (j=0 ; j<3 ; j++)
536 ent->origin[j] = ent->msg_origins[1][j] + f*delta[j];
538 d = ent->msg_angles[0][j] - ent->msg_angles[1][j];
543 ent->angles[j] = ent->msg_angles[1][j] + f*d;
548 if (ent->effects & EF_BRIGHTFIELD)
549 R_EntityParticles (ent);
550 if (ent->effects & EF_MUZZLEFLASH)
554 dl = CL_AllocDlight (i);
555 VectorCopy (ent->origin, dl->origin);
557 AngleVectors (ent->angles, fv, rv, uv);
559 VectorMA (dl->origin, 18, fv, dl->origin);
560 dl->radius = 200 + (rand()&31);
562 dl->die = cl.time + 0.1;
563 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
565 if (ent->effects & EF_BRIGHTLIGHT)
567 dl = CL_AllocDlight (i);
568 VectorCopy (ent->origin, dl->origin);
570 dl->radius = 400 + (rand()&31);
571 dl->die = cl.time + 0.001;
572 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
574 if (ent->effects & EF_DIMLIGHT)
576 dl = CL_AllocDlight (i);
577 VectorCopy (ent->origin, dl->origin);
578 dl->radius = 200 + (rand()&31);
579 dl->die = cl.time + 0.001;
580 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
582 // LordHavoc: added EF_RED and EF_BLUE
583 if (ent->effects & EF_RED) // red
585 if (ent->effects & EF_BLUE) // magenta
587 dl = CL_AllocDlight (i);
588 VectorCopy (ent->origin, dl->origin);
589 dl->radius = 200 + (rand()&31);
590 dl->die = cl.time + 0.001;
591 dl->color[0] = 0.7;dl->color[1] = 0.07;dl->color[2] = 0.7;
595 dl = CL_AllocDlight (i);
596 VectorCopy (ent->origin, dl->origin);
597 dl->radius = 200 + (rand()&31);
598 dl->die = cl.time + 0.001;
599 dl->color[0] = 0.8;dl->color[1] = 0.05;dl->color[2] = 0.05;
602 else if (ent->effects & EF_BLUE) // blue
604 dl = CL_AllocDlight (i);
605 VectorCopy (ent->origin, dl->origin);
606 dl->radius = 200 + (rand()&31);
607 dl->die = cl.time + 0.001;
608 dl->color[0] = 0.05;dl->color[1] = 0.05;dl->color[2] = 0.8;
611 if (ent->model->flags) // LordHavoc: if the model has no flags, don't check each
613 // rotate binary objects locally
614 if (ent->model->flags & EF_ROTATE)
615 ent->angles[1] = bobjrotate;
616 if (ent->model->flags & EF_GIB)
617 R_RocketTrail (oldorg, ent->origin, 2, ent);
618 else if (ent->model->flags & EF_ZOMGIB)
619 R_RocketTrail (oldorg, ent->origin, 4, ent);
620 else if (ent->model->flags & EF_TRACER)
621 R_RocketTrail (oldorg, ent->origin, 3, ent);
622 else if (ent->model->flags & EF_TRACER2)
623 R_RocketTrail (oldorg, ent->origin, 5, ent);
624 else if (ent->model->flags & EF_ROCKET)
626 R_RocketTrail (oldorg, ent->origin, 0, ent);
627 dl = CL_AllocDlight (i);
628 VectorCopy (ent->origin, dl->origin);
630 dl->die = cl.time + 0.001;
631 dl->color[0] = 1.0;dl->color[1] = 0.8;dl->color[2] = 0.4;
633 else if (ent->model->flags & EF_GRENADE)
635 if (ent->alpha == -1) // LordHavoc: Nehahra dem compatibility
636 R_RocketTrail (oldorg, ent->origin, 7, ent);
638 R_RocketTrail (oldorg, ent->origin, 1, ent);
640 else if (ent->model->flags & EF_TRACER3)
641 R_RocketTrail (oldorg, ent->origin, 6, ent);
643 if (ent->glowsize) // LordHavoc: customizable glow
645 dl = CL_AllocDlight (i);
646 VectorCopy (ent->origin, dl->origin);
647 dl->dark = ent->glowsize < 0; // darklight
648 dl->radius = ent->glowsize;
651 if (ent->glowtrail) // LordHavoc: all darklights leave black trails
652 R_RocketTrail2 (oldorg, ent->origin, 0, ent);
653 dl->radius = -ent->glowsize;
655 else if (ent->glowtrail) // LordHavoc: customizable glow and trail
656 R_RocketTrail2 (oldorg, ent->origin, ent->glowcolor, ent);
657 dl->die = cl.time + 0.001;
658 tempcolor = (byte *)&d_8to24table[ent->glowcolor];
659 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);
661 else if (ent->glowtrail) // LordHavoc: customizable glow and trail
662 R_RocketTrail2 (oldorg, ent->origin, ent->glowcolor, ent);
664 ent->forcelink = false;
666 if (i == cl.viewentity && !chase_active.value)
669 // LordHavoc: enabled EF_NODRAW
670 if (!ent->model || ent->effects & EF_NODRAW)
672 if (cl_numvisedicts < MAX_VISEDICTS)
674 cl_visedicts[cl_numvisedicts] = ent;
686 Read all incoming data from the server
689 int CL_ReadFromServer (void)
693 cl.oldtime = cl.time;
694 cl.time += host_frametime;
698 ret = CL_GetMessage ();
700 Host_Error ("CL_ReadFromServer: lost server connection");
704 cl.last_received_message = realtime;
705 CL_ParseServerMessage ();
706 } while (ret && cls.state == ca_connected);
708 if (cl_shownet.value)
711 CL_RelinkEntities ();
715 // bring the links up to date
725 void CL_SendCmd (void)
729 if (cls.state != ca_connected)
732 if (cls.signon == SIGNONS)
734 // get basic movement from keyboard
737 // allow mice or other external controllers to add to the move
740 // send the unreliable message
745 if (cls.demoplayback)
747 SZ_Clear (&cls.message);
751 // send the reliable message
752 if (!cls.message.cursize)
753 return; // no message at all
755 if (!NET_CanSendMessage (cls.netcon))
757 Con_DPrintf ("CL_WriteToServer: can't send\n");
761 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
762 Host_Error ("CL_WriteToServer: lost server connection");
764 SZ_Clear (&cls.message);
767 // LordHavoc: pausedemo command
768 void CL_PauseDemo_f (void)
770 cls.demopaused = !cls.demopaused;
772 Con_Printf("Demo paused\n");
774 Con_Printf("Demo unpaused\n");
777 cvar_t demo_nehahra = {"demo_nehahra", "0"};
786 SZ_Alloc (&cls.message, 1024);
792 // register our commands
794 Cvar_RegisterVariable (&cl_name);
795 Cvar_RegisterVariable (&cl_color);
796 Cvar_RegisterVariable (&cl_upspeed);
797 Cvar_RegisterVariable (&cl_forwardspeed);
798 Cvar_RegisterVariable (&cl_backspeed);
799 Cvar_RegisterVariable (&cl_sidespeed);
800 Cvar_RegisterVariable (&cl_movespeedkey);
801 Cvar_RegisterVariable (&cl_yawspeed);
802 Cvar_RegisterVariable (&cl_pitchspeed);
803 Cvar_RegisterVariable (&cl_anglespeedkey);
804 Cvar_RegisterVariable (&cl_shownet);
805 Cvar_RegisterVariable (&cl_nolerp);
806 Cvar_RegisterVariable (&maxfps);
807 Cvar_RegisterVariable (&lookspring);
808 Cvar_RegisterVariable (&lookstrafe);
809 Cvar_RegisterVariable (&sensitivity);
811 Cvar_RegisterVariable (&m_pitch);
812 Cvar_RegisterVariable (&m_yaw);
813 Cvar_RegisterVariable (&m_forward);
814 Cvar_RegisterVariable (&m_side);
816 // Cvar_RegisterVariable (&cl_autofire);
818 Cmd_AddCommand ("entities", CL_PrintEntities_f);
819 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
820 Cmd_AddCommand ("record", CL_Record_f);
821 Cmd_AddCommand ("stop", CL_Stop_f);
822 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
823 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
825 // LordHavoc: added pausedemo
826 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
827 // LordHavoc: added demo_nehahra cvar
828 Cvar_RegisterVariable (&demo_nehahra);
830 Cvar_SetValue("demo_nehahra", 1);