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 lookspring = {"lookspring","0", true};
35 cvar_t lookstrafe = {"lookstrafe","0", true};
36 cvar_t sensitivity = {"sensitivity","3", true};
38 cvar_t m_pitch = {"m_pitch","0.022", true};
39 cvar_t m_yaw = {"m_yaw","0.022", true};
40 cvar_t m_forward = {"m_forward","1", true};
41 cvar_t m_side = {"m_side","0.8", true};
46 // FIXME: put these on hunk?
47 efrag_t cl_efrags[MAX_EFRAGS];
48 entity_t cl_entities[MAX_EDICTS];
49 entity_t cl_static_entities[MAX_STATIC_ENTITIES];
50 lightstyle_t cl_lightstyle[MAX_LIGHTSTYLES];
51 dlight_t cl_dlights[MAX_DLIGHTS];
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_efrags, 0, sizeof(cl_efrags));
76 memset (cl_entities, 0, sizeof(cl_entities));
77 memset (cl_dlights, 0, sizeof(cl_dlights));
78 memset (cl_lightstyle, 0, sizeof(cl_lightstyle));
79 memset (cl_temp_entities, 0, sizeof(cl_temp_entities));
80 memset (cl_beams, 0, sizeof(cl_beams));
81 // LordHavoc: have to set up the baseline info for alpha and other stuff
82 for (i = 0;i < MAX_EDICTS;i++)
84 cl_entities[i].baseline.alpha = 255;
85 cl_entities[i].baseline.scale = 16;
86 cl_entities[i].baseline.glowsize = 0;
87 cl_entities[i].baseline.glowcolor = 254;
88 cl_entities[i].baseline.colormod = 255;
92 // allocate the efrags and chain together into a free list
94 cl.free_efrags = cl_efrags;
95 for (i=0 ; i<MAX_EFRAGS-1 ; i++)
96 cl.free_efrags[i].entnext = &cl.free_efrags[i+1];
97 cl.free_efrags[i].entnext = NULL;
101 =====================
104 Sends a disconnect message to the server
105 This is also called on Host_Error, so it shouldn't cause any errors
106 =====================
108 void CL_Disconnect (void)
110 // stop sounds (especially looping!)
111 S_StopAllSounds (true);
113 // bring the console down and fade the colors back to normal
114 // SCR_BringDownConsole ();
116 // if running a local server, shut it down
117 if (cls.demoplayback)
119 else if (cls.state == ca_connected)
121 if (cls.demorecording)
124 Con_DPrintf ("Sending clc_disconnect\n");
125 SZ_Clear (&cls.message);
126 MSG_WriteByte (&cls.message, clc_disconnect);
127 NET_SendUnreliableMessage (cls.netcon, &cls.message);
128 SZ_Clear (&cls.message);
129 NET_Close (cls.netcon);
131 cls.state = ca_disconnected;
133 Host_ShutdownServer(false);
136 cls.demoplayback = cls.timedemo = false;
140 void CL_Disconnect_f (void)
144 Host_ShutdownServer (false);
151 =====================
152 CL_EstablishConnection
154 Host should be either "local" or a net address to be passed on
155 =====================
157 void CL_EstablishConnection (char *host)
159 if (cls.state == ca_dedicated)
162 if (cls.demoplayback)
167 cls.netcon = NET_Connect (host);
169 Host_Error ("CL_Connect: connect failed\n");
170 Con_DPrintf ("CL_EstablishConnection: connected to %s\n", host);
172 cls.demonum = -1; // not in the demo loop now
173 cls.state = ca_connected;
174 cls.signon = 0; // need all the signon messages before playing
177 extern int numgltextures;
181 =====================
184 An svc_signonnum has been received, perform a client side setup
185 =====================
187 void CL_SignonReply (void)
191 Con_DPrintf ("CL_SignonReply: %i\n", cls.signon);
196 MSG_WriteByte (&cls.message, clc_stringcmd);
197 MSG_WriteString (&cls.message, "prespawn");
201 MSG_WriteByte (&cls.message, clc_stringcmd);
202 MSG_WriteString (&cls.message, va("name \"%s\"\n", cl_name.string));
204 MSG_WriteByte (&cls.message, clc_stringcmd);
205 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 sprintf (str, "spawn %s", cls.spawnparms);
209 MSG_WriteString (&cls.message, str);
213 MSG_WriteByte (&cls.message, clc_stringcmd);
214 MSG_WriteString (&cls.message, "begin");
215 Cache_Report (); // print remaining memory
219 SCR_EndLoadingPlaque (); // allow normal screen updates
220 // LordHavoc: debugging purposes
221 Con_DPrintf("GLQuake texture slots in use: %i : %i : %i texels\n", texture_extension_number, numgltextures, texels);
227 =====================
230 Called to play the next demo in the demo loop
231 =====================
233 void CL_NextDemo (void)
237 if (cls.demonum == -1)
238 return; // don't play demos
240 SCR_BeginLoadingPlaque ();
242 if (!cls.demos[cls.demonum][0] || cls.demonum == MAX_DEMOS)
245 if (!cls.demos[cls.demonum][0])
247 Con_Printf ("No demos listed with startdemos\n");
253 sprintf (str,"playdemo %s\n", cls.demos[cls.demonum]);
254 Cbuf_InsertText (str);
263 void CL_PrintEntities_f (void)
268 for (i=0,ent=cl_entities ; i<cl.num_entities ; i++,ent++)
270 Con_Printf ("%3i:",i);
273 Con_Printf ("EMPTY\n");
276 Con_Printf ("%s:%2i (%5.1f,%5.1f,%5.1f) [%5.1f %5.1f %5.1f]\n"
277 ,ent->model->name,ent->frame, ent->origin[0], ent->origin[1], ent->origin[2], ent->angles[0], ent->angles[1], ent->angles[2]);
286 Debugging tool, just flashes the screen
301 VID_SetPalette (host_basepal);
304 for (c=0 ; c<768 ; c+=3)
310 VID_SetPalette (pal);
314 for (c=0 ; c<768 ; c+=3)
320 VID_SetPalette (pal);
331 dlight_t *CL_AllocDlight (int key)
336 // first look for an exact key match
340 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
344 memset (dl, 0, sizeof(*dl));
351 // then look for anything else
353 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
355 if (dl->die < cl.time)
357 memset (dl, 0, sizeof(*dl));
364 memset (dl, 0, sizeof(*dl));
376 void CL_DecayLights (void)
382 time = cl.time - cl.oldtime;
385 for (i=0 ; i<MAX_DLIGHTS ; i++, dl++)
387 if (dl->die < cl.time || !dl->radius)
390 dl->radius -= time*dl->decay;
401 Determines the fraction between the last two messages that the objects
405 float CL_LerpPoint (void)
409 f = cl.mtime[0] - cl.mtime[1];
411 if (!f || cl_nolerp.value || cls.timedemo || sv.active)
413 cl.time = cl.mtime[0];
418 { // dropped packet, or start of demo
419 cl.mtime[1] = cl.mtime[0] - 0.1;
422 frac = (cl.time - cl.mtime[1]) / f;
423 //Con_Printf ("frac: %f\n",frac);
429 cl.time = cl.mtime[1];
430 // Con_Printf ("low frac\n");
439 cl.time = cl.mtime[0];
440 // Con_Printf ("high frac\n");
456 void R_RocketTrail2 (vec3_t start, vec3_t end, int color, entity_t *ent);
457 void CL_RelinkEntities (void)
468 // determine partial update time
469 frac = CL_LerpPoint ();
474 // interpolate player info
476 for (i=0 ; i<3 ; i++)
477 cl.velocity[i] = cl.mvelocity[1][i] + frac * (cl.mvelocity[0][i] - cl.mvelocity[1][i]);
479 if (cls.demoplayback)
481 // interpolate the angles
482 for (j=0 ; j<3 ; j++)
484 d = cl.mviewangles[0][j] - cl.mviewangles[1][j];
489 cl.viewangles[j] = cl.mviewangles[1][j] + frac*d;
493 bobjrotate = anglemod(100*cl.time);
495 // start on the entity after the world
496 for (i=1,ent=cl_entities+1 ; i<cl.num_entities ; i++,ent++)
501 R_RemoveEfrags (ent); // just became empty
505 // if the object wasn't included in the last packet, remove it
506 if (ent->msgtime != cl.mtime[0])
512 VectorCopy (ent->origin, oldorg);
515 { // the entity was not updated in the last message
516 // so move to the final spot
517 VectorCopy (ent->msg_origins[0], ent->origin);
518 VectorCopy (ent->msg_angles[0], ent->angles);
521 { // if the delta is large, assume a teleport and don't lerp
523 for (j=0 ; j<3 ; j++)
525 delta[j] = ent->msg_origins[0][j] - ent->msg_origins[1][j];
526 // LordHavoc: increased lerp tolerance from 100 to 200
527 if (delta[j] > 200 || delta[j] < -200)
528 f = 1; // assume a teleportation, not a motion
531 // interpolate the origin and angles
532 for (j=0 ; j<3 ; j++)
534 ent->origin[j] = ent->msg_origins[1][j] + f*delta[j];
536 d = ent->msg_angles[0][j] - ent->msg_angles[1][j];
541 ent->angles[j] = ent->msg_angles[1][j] + f*d;
546 if (ent->effects & EF_BRIGHTFIELD)
547 R_EntityParticles (ent);
548 if (ent->effects & EF_MUZZLEFLASH)
552 dl = CL_AllocDlight (i);
553 VectorCopy (ent->origin, dl->origin);
555 AngleVectors (ent->angles, fv, rv, uv);
557 VectorMA (dl->origin, 18, fv, dl->origin);
558 dl->radius = 200 + (rand()&31);
560 dl->die = cl.time + 0.1;
561 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
563 if (ent->effects & EF_BRIGHTLIGHT)
565 dl = CL_AllocDlight (i);
566 VectorCopy (ent->origin, dl->origin);
568 dl->radius = 400 + (rand()&31);
569 dl->die = cl.time + 0.001;
570 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
572 if (ent->effects & EF_DIMLIGHT)
574 dl = CL_AllocDlight (i);
575 VectorCopy (ent->origin, dl->origin);
576 dl->radius = 200 + (rand()&31);
577 dl->die = cl.time + 0.001;
578 dl->color[0] = 1.0;dl->color[1] = 1.0;dl->color[2] = 1.0;
580 // LordHavoc: added EF_RED and EF_BLUE
581 if (ent->effects & EF_RED) // red
583 if (ent->effects & EF_BLUE) // magenta
585 dl = CL_AllocDlight (i);
586 VectorCopy (ent->origin, dl->origin);
587 dl->radius = 200 + (rand()&31);
588 dl->die = cl.time + 0.001;
589 dl->color[0] = 0.7;dl->color[1] = 0.07;dl->color[2] = 0.7;
593 dl = CL_AllocDlight (i);
594 VectorCopy (ent->origin, dl->origin);
595 dl->radius = 200 + (rand()&31);
596 dl->die = cl.time + 0.001;
597 dl->color[0] = 0.8;dl->color[1] = 0.05;dl->color[2] = 0.05;
600 else if (ent->effects & EF_BLUE) // blue
602 dl = CL_AllocDlight (i);
603 VectorCopy (ent->origin, dl->origin);
604 dl->radius = 200 + (rand()&31);
605 dl->die = cl.time + 0.001;
606 dl->color[0] = 0.05;dl->color[1] = 0.05;dl->color[2] = 0.8;
609 if (ent->model->flags) // LordHavoc: if the model has no flags, don't check each
611 // rotate binary objects locally
612 if (ent->model->flags & EF_ROTATE)
613 ent->angles[1] = bobjrotate;
614 if (ent->model->flags & EF_GIB)
615 R_RocketTrail (oldorg, ent->origin, 2, ent);
616 else if (ent->model->flags & EF_ZOMGIB)
617 R_RocketTrail (oldorg, ent->origin, 4, ent);
618 else if (ent->model->flags & EF_TRACER)
619 R_RocketTrail (oldorg, ent->origin, 3, ent);
620 else if (ent->model->flags & EF_TRACER2)
621 R_RocketTrail (oldorg, ent->origin, 5, ent);
622 else if (ent->model->flags & EF_ROCKET)
624 R_RocketTrail (oldorg, ent->origin, 0, ent);
625 dl = CL_AllocDlight (i);
626 VectorCopy (ent->origin, dl->origin);
628 dl->die = cl.time + 0.001;
629 dl->color[0] = 1.0;dl->color[1] = 0.8;dl->color[2] = 0.4;
631 else if (ent->model->flags & EF_GRENADE)
633 if (ent->alpha == -1) // LordHavoc: Nehahra dem compatibility
634 R_RocketTrail (oldorg, ent->origin, 7, ent);
636 R_RocketTrail (oldorg, ent->origin, 1, ent);
638 else if (ent->model->flags & EF_TRACER3)
639 R_RocketTrail (oldorg, ent->origin, 6, ent);
641 if (ent->glowsize) // LordHavoc: customizable glow
643 dl = CL_AllocDlight (i);
644 VectorCopy (ent->origin, dl->origin);
645 dl->dark = ent->glowsize < 0; // darklight
646 dl->radius = ent->glowsize;
649 if (ent->glowtrail) // LordHavoc: all darklights leave black trails
650 R_RocketTrail2 (oldorg, ent->origin, 0, ent);
651 dl->radius = -ent->glowsize;
653 else if (ent->glowtrail) // LordHavoc: customizable glow and trail
654 R_RocketTrail2 (oldorg, ent->origin, ent->glowcolor, ent);
655 dl->die = cl.time + 0.001;
656 tempcolor = (byte *)&d_8to24table[ent->glowcolor];
657 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);
659 else if (ent->glowtrail) // LordHavoc: customizable glow and trail
660 R_RocketTrail2 (oldorg, ent->origin, ent->glowcolor, ent);
662 ent->forcelink = false;
664 if (i == cl.viewentity && !chase_active.value)
667 // LordHavoc: enabled EF_NODRAW
668 if (!ent->model || ent->effects & EF_NODRAW)
670 if (cl_numvisedicts < MAX_VISEDICTS)
672 cl_visedicts[cl_numvisedicts] = ent;
684 Read all incoming data from the server
687 int CL_ReadFromServer (void)
691 cl.oldtime = cl.time;
692 cl.time += host_frametime;
696 ret = CL_GetMessage ();
698 Host_Error ("CL_ReadFromServer: lost server connection");
702 cl.last_received_message = realtime;
703 CL_ParseServerMessage ();
704 } while (ret && cls.state == ca_connected);
706 if (cl_shownet.value)
709 CL_RelinkEntities ();
713 // bring the links up to date
723 void CL_SendCmd (void)
727 if (cls.state != ca_connected)
730 if (cls.signon == SIGNONS)
732 // get basic movement from keyboard
735 // allow mice or other external controllers to add to the move
738 // send the unreliable message
743 if (cls.demoplayback)
745 SZ_Clear (&cls.message);
749 // send the reliable message
750 if (!cls.message.cursize)
751 return; // no message at all
753 if (!NET_CanSendMessage (cls.netcon))
755 Con_DPrintf ("CL_WriteToServer: can't send\n");
759 if (NET_SendMessage (cls.netcon, &cls.message) == -1)
760 Host_Error ("CL_WriteToServer: lost server connection");
762 SZ_Clear (&cls.message);
765 // LordHavoc: pausedemo command
766 void CL_PauseDemo_f (void)
768 cls.demopaused = !cls.demopaused;
770 Con_Printf("Demo paused\n");
772 Con_Printf("Demo unpaused\n");
775 cvar_t demo_nehahra = {"demo_nehahra", "0"};
784 SZ_Alloc (&cls.message, 1024);
790 // register our commands
792 Cvar_RegisterVariable (&cl_name);
793 Cvar_RegisterVariable (&cl_color);
794 Cvar_RegisterVariable (&cl_upspeed);
795 Cvar_RegisterVariable (&cl_forwardspeed);
796 Cvar_RegisterVariable (&cl_backspeed);
797 Cvar_RegisterVariable (&cl_sidespeed);
798 Cvar_RegisterVariable (&cl_movespeedkey);
799 Cvar_RegisterVariable (&cl_yawspeed);
800 Cvar_RegisterVariable (&cl_pitchspeed);
801 Cvar_RegisterVariable (&cl_anglespeedkey);
802 Cvar_RegisterVariable (&cl_shownet);
803 Cvar_RegisterVariable (&cl_nolerp);
804 Cvar_RegisterVariable (&lookspring);
805 Cvar_RegisterVariable (&lookstrafe);
806 Cvar_RegisterVariable (&sensitivity);
808 Cvar_RegisterVariable (&m_pitch);
809 Cvar_RegisterVariable (&m_yaw);
810 Cvar_RegisterVariable (&m_forward);
811 Cvar_RegisterVariable (&m_side);
813 // Cvar_RegisterVariable (&cl_autofire);
815 Cmd_AddCommand ("entities", CL_PrintEntities_f);
816 Cmd_AddCommand ("disconnect", CL_Disconnect_f);
817 Cmd_AddCommand ("record", CL_Record_f);
818 Cmd_AddCommand ("stop", CL_Stop_f);
819 Cmd_AddCommand ("playdemo", CL_PlayDemo_f);
820 Cmd_AddCommand ("timedemo", CL_TimeDemo_f);
822 // LordHavoc: added pausedemo
823 Cmd_AddCommand ("pausedemo", CL_PauseDemo_f);
824 // LordHavoc: added demo_nehahra cvar
825 Cvar_RegisterVariable (&demo_nehahra);
827 Cvar_SetValue("demo_nehahra", 1);