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 // host.c -- coordinates spawning and killing of local servers
24 #include "psp/module.h"
28 A server can allways be started, even if the system started out as a client
31 A client can NOT be started if the system started as a dedicated server.
33 Memory is cleared / released when a server or client begins, not when they end.
37 quakeparms_t host_parms
;
39 qboolean host_initialized
; // true if into command execution
41 double host_frametime
;
43 double realtime
; // without any filtering or bounding
44 double oldrealtime
; // last frame run
52 client_t
*host_client
; // current client
54 jmp_buf host_abortserver
;
59 cvar_t show_fps
= {"show_fps","0", true}; // set for running times - muff
60 cvar_t max_fps
= {"max_fps", "65", true}; // MrG - max_fps
61 cvar_t host_framerate
= {"host_framerate","0"}; // set for slow motion
62 cvar_t host_speeds
= {"host_speeds","0"}; // set for running times
64 cvar_t sys_ticrate
= {"sys_ticrate","0.05"};
65 cvar_t serverprofile
= {"serverprofile","0"};
67 cvar_t fraglimit
= {"fraglimit","15",false,true};
68 cvar_t timelimit
= {"timelimit","15",false,true};
69 cvar_t teamplay
= {"teamplay","0",false,true};
71 cvar_t samelevel
= {"samelevel","0"};
72 cvar_t noexit
= {"noexit","0",false,true};
75 cvar_t developer
= {"developer","1"}; // should be 0 for release!
77 cvar_t developer
= {"developer","0"};
80 cvar_t skill
= {"skill","1"}; // 0 - 3
81 cvar_t deathmatch
= {"deathmatch","0"}; // 0, 1, or 2
82 cvar_t coop
= {"coop","0"}; // 0 or 1
84 cvar_t pausable
= {"pausable","1"};
86 cvar_t temp1
= {"temp1","0"};
88 qboolean bmg_type_changed
= false;
96 void Host_EndGame (char *message
, ...)
101 va_start (argptr
,message
);
102 vsprintf (string
,message
,argptr
);
104 Con_DPrintf ("Host_EndGame: %s\n",string
);
107 Host_ShutdownServer (false);
109 if (cls
.state
== ca_dedicated
)
110 Sys_Error ("Host_EndGame: %s\n",string
); // dedicated servers exit
112 if (cls
.demonum
!= -1)
117 longjmp (host_abortserver
, 1);
124 This shuts down both the client and server
127 void Host_Error (char *error
, ...)
131 static qboolean inerror
= false;
134 Sys_Error ("Host_Error: recursively entered");
137 SCR_EndLoadingPlaque (); // reenable screen updates
139 va_start (argptr
,error
);
140 vsprintf (string
,error
,argptr
);
142 Con_Printf ("Host_Error: %s\n",string
);
145 Host_ShutdownServer (false);
147 if (cls
.state
== ca_dedicated
)
148 Sys_Error ("Host_Error: %s\n",string
); // dedicated servers exit
155 longjmp (host_abortserver
, 1);
163 void Host_FindMaxClients (void)
169 i
= COM_CheckParm ("-dedicated");
172 cls
.state
= ca_dedicated
;
173 if (i
!= (com_argc
- 1))
175 svs
.maxclients
= Q_atoi (com_argv
[i
+1]);
181 cls
.state
= ca_disconnected
;
183 i
= COM_CheckParm ("-listen");
186 if (cls
.state
== ca_dedicated
)
187 Sys_Error ("Only one of -dedicated or -listen can be specified");
188 if (i
!= (com_argc
- 1))
189 svs
.maxclients
= Q_atoi (com_argv
[i
+1]);
193 if (svs
.maxclients
< 1)
195 else if (svs
.maxclients
> MAX_SCOREBOARD
)
196 svs
.maxclients
= MAX_SCOREBOARD
;
198 svs
.maxclientslimit
= svs
.maxclients
;
199 if (svs
.maxclientslimit
< 4)
200 svs
.maxclientslimit
= 4;
201 svs
.clients
= Hunk_AllocName (svs
.maxclientslimit
*sizeof(client_t
), "clients");
203 if (svs
.maxclients
> 1)
205 if (deathmatch
.value
> 1)
206 Cvar_SetValue ("deathmatch", deathmatch
.value
);
208 Cvar_SetValue ("deathmatch", 1.0);
211 Cvar_SetValue ("deathmatch", 0.0);
216 =======================
218 ======================
220 void Host_InitLocal (void)
222 Host_InitCommands ();
224 Cvar_RegisterVariable (&show_fps
); // muff
225 Cvar_RegisterVariable (&max_fps
); // MrG - max_fps
226 Cvar_RegisterVariable (&host_framerate
);
227 Cvar_RegisterVariable (&host_speeds
);
229 Cvar_RegisterVariable (&sys_ticrate
);
230 Cvar_RegisterVariable (&serverprofile
);
232 Cvar_RegisterVariable (&fraglimit
);
233 Cvar_RegisterVariable (&timelimit
);
234 Cvar_RegisterVariable (&teamplay
);
235 Cvar_RegisterVariable (&samelevel
);
236 Cvar_RegisterVariable (&noexit
);
237 Cvar_RegisterVariable (&skill
);
238 Cvar_RegisterVariable (&developer
);
239 Cvar_RegisterVariable (&deathmatch
);
240 Cvar_RegisterVariable (&coop
);
242 Cvar_RegisterVariable (&pausable
);
244 Cvar_RegisterVariable (&temp1
);
246 Host_FindMaxClients ();
248 host_time
= 1.0; // so a think at time 0 won't get called
254 Host_WriteConfiguration
256 Writes key bindings and archived cvars to config.cfg
259 void Host_WriteConfiguration (void)
263 // dedicated servers initialize the host but don't parse and set the
265 if (host_initialized
& !isDedicated
)
267 f
= fopen (va("%s/config.cfg",com_gamedir
), "w");
270 Con_Printf ("Couldn't write config.cfg.\n");
274 Key_WriteBindings (f
);
275 Cvar_WriteVariables (f
);
286 Sends text across to be displayed
287 FIXME: make this just a stuffed echo?
290 void SV_ClientPrintf (char *fmt
, ...)
295 va_start (argptr
,fmt
);
296 vsprintf (string
, fmt
,argptr
);
299 MSG_WriteByte (&host_client
->message
, svc_print
);
300 MSG_WriteString (&host_client
->message
, string
);
307 Sends text to all active clients
310 void SV_BroadcastPrintf (char *fmt
, ...)
316 va_start (argptr
,fmt
);
317 vsprintf (string
, fmt
,argptr
);
320 for (i
=0 ; i
<svs
.maxclients
; i
++)
321 if (svs
.clients
[i
].active
&& svs
.clients
[i
].spawned
)
323 MSG_WriteByte (&svs
.clients
[i
].message
, svc_print
);
324 MSG_WriteString (&svs
.clients
[i
].message
, string
);
332 Send text over to the client to be executed
335 void Host_ClientCommands (char *fmt
, ...)
340 va_start (argptr
,fmt
);
341 vsprintf (string
, fmt
,argptr
);
344 MSG_WriteByte (&host_client
->message
, svc_stufftext
);
345 MSG_WriteString (&host_client
->message
, string
);
349 =====================
352 Called when the player is getting totally kicked off the host
353 if (crash = true), don't bother sending signofs
354 =====================
356 void SV_DropClient (qboolean crash
)
364 // send any final messages (don't check for errors)
365 if (NET_CanSendMessage (host_client
->netconnection
))
367 MSG_WriteByte (&host_client
->message
, svc_disconnect
);
368 NET_SendMessage (host_client
->netconnection
, &host_client
->message
);
371 if (host_client
->edict
&& host_client
->spawned
)
373 // call the prog function for removing a client
374 // this will set the body to a dead frame, among other things
375 saveSelf
= pr_global_struct
->self
;
376 pr_global_struct
->self
= EDICT_TO_PROG(host_client
->edict
);
377 PR_ExecuteProgram (pr_global_struct
->ClientDisconnect
);
378 pr_global_struct
->self
= saveSelf
;
381 Sys_Printf ("Client %s removed\n",host_client
->name
);
384 // break the net connection
385 NET_Close (host_client
->netconnection
);
386 host_client
->netconnection
= NULL
;
388 // free the client (the body stays around)
389 host_client
->active
= false;
390 host_client
->name
[0] = 0;
391 host_client
->old_frags
= -999999;
392 net_activeconnections
--;
394 // send notification to all clients
395 for (i
=0, client
= svs
.clients
; i
<svs
.maxclients
; i
++, client
++)
399 MSG_WriteByte (&client
->message
, svc_updatename
);
400 MSG_WriteByte (&client
->message
, host_client
- svs
.clients
);
401 MSG_WriteString (&client
->message
, "");
402 MSG_WriteByte (&client
->message
, svc_updatefrags
);
403 MSG_WriteByte (&client
->message
, host_client
- svs
.clients
);
404 MSG_WriteShort (&client
->message
, 0);
405 MSG_WriteByte (&client
->message
, svc_updatecolors
);
406 MSG_WriteByte (&client
->message
, host_client
- svs
.clients
);
407 MSG_WriteByte (&client
->message
, 0);
415 This only happens at the end of a game, not between levels
418 void Host_ShutdownServer(qboolean crash
)
431 // stop all client sounds immediately
432 if (cls
.state
== ca_connected
)
435 // flush any pending messages - like the score!!!
436 start
= Sys_FloatTime();
440 for (i
=0, host_client
= svs
.clients
; i
<svs
.maxclients
; i
++, host_client
++)
442 if (host_client
->active
&& host_client
->message
.cursize
)
444 if (NET_CanSendMessage (host_client
->netconnection
))
446 NET_SendMessage(host_client
->netconnection
, &host_client
->message
);
447 SZ_Clear (&host_client
->message
);
451 NET_GetMessage(host_client
->netconnection
);
456 if ((Sys_FloatTime() - start
) > 3.0)
461 // make sure all the clients know we're disconnecting
465 MSG_WriteByte(&buf
, svc_disconnect
);
466 count
= NET_SendToAll(&buf
, 5);
468 Con_Printf("Host_ShutdownServer: NET_SendToAll failed for %u clients\n", count
);
470 for (i
=0, host_client
= svs
.clients
; i
<svs
.maxclients
; i
++, host_client
++)
471 if (host_client
->active
)
472 SV_DropClient(crash
);
477 memset (&sv
, 0, sizeof(sv
));
478 memset (svs
.clients
, 0, svs
.maxclientslimit
*sizeof(client_t
));
486 This clears all the memory used by both the client and server, but does
487 not reinitialize anything.
490 void Host_ClearMemory (void)
492 Con_DPrintf ("Clearing memory\n");
496 Hunk_FreeToLowMark (host_hunklevel
);
499 memset (&sv
, 0, sizeof(sv
));
500 memset (&cl
, 0, sizeof(cl
));
504 //============================================================================
511 Returns false if the time is too short to run a frame
514 qboolean
Host_FilterTime (float time
)
520 if (max_fps
.value
< 1) Cvar_SetValue("max_fps", 65);
521 if (!cls
.timedemo
&& realtime
- oldrealtime
< 1.0/max_fps
.value
)
522 return false; // framerate is too high
524 host_frametime
= realtime
- oldrealtime
;
525 oldrealtime
= realtime
;
527 if (host_framerate
.value
> 0)
528 host_frametime
= host_framerate
.value
;
530 { // don't allow really long or short frames
531 if (host_frametime
> 0.1)
532 host_frametime
= 0.1;
533 if (host_frametime
< 0.001)
534 host_frametime
= 0.001;
543 Host_GetConsoleCommands
545 Add them exactly as if they had been typed at the console
548 void Host_GetConsoleCommands (void)
554 cmd
= Sys_ConsoleInput ();
570 void _Host_ServerFrame (void)
572 // run the world state
573 pr_global_struct
->frametime
= host_frametime
;
575 // read client messages
578 // move things around and think
579 // always pause in single player if in console or menus
580 if (!sv
.paused
&& (svs
.maxclients
> 1 || key_dest
== key_game
) )
586 Cvar_Set("bgmtype","cd");
587 bmg_type_changed
= true;
594 Cvar_Set("bgmtype","none");
595 bmg_type_changed
= true;
600 void Host_ServerFrame (void)
602 float save_host_frametime
;
603 float temp_host_frametime
;
605 // run the world state
606 pr_global_struct
->frametime
= host_frametime
;
608 // set the time and clear the general datagram
611 // check for new clients
612 SV_CheckForNewClients ();
614 temp_host_frametime
= save_host_frametime
= host_frametime
;
615 while(temp_host_frametime
> (1.0/72.0))
617 if (temp_host_frametime
> 0.05)
618 host_frametime
= 0.05;
620 host_frametime
= temp_host_frametime
;
621 temp_host_frametime
-= host_frametime
;
622 _Host_ServerFrame ();
624 host_frametime
= save_host_frametime
;
626 // send all messages to the clients
627 SV_SendClientMessages ();
632 void Host_ServerFrame (void)
634 // run the world state
635 pr_global_struct
->frametime
= host_frametime
;
637 // set the time and clear the general datagram
640 // check for new clients
641 SV_CheckForNewClients ();
643 // read client messages
646 // move things around and think
647 // always pause in single player if in console or menus
648 if (!sv
.paused
&& (svs
.maxclients
> 1 || key_dest
== key_game
) )
651 // send all messages to the clients
652 SV_SendClientMessages ();
662 Runs all active servers
665 void _Host_Frame (float time
)
667 static double time1
= 0;
668 static double time2
= 0;
669 static double time3
= 0;
670 int pass1
, pass2
, pass3
;
672 if (setjmp (host_abortserver
) )
673 return; // something bad happened, or the server disconnected
675 // keep the random time dependent
678 // decide the simulation time
679 if (!Host_FilterTime (time
))
680 return; // don't run too fast, or packets will flood out
682 // get new key events
683 Sys_SendKeyEvents ();
685 // allow mice or other external controllers to add commands
688 // process console commands
693 // if running the server locally, make intentions now
697 //-------------------
701 //-------------------
703 // check for commands typed to the host
704 Host_GetConsoleCommands ();
709 //-------------------
713 //-------------------
715 // if running the server remotely, send intentions now after
716 // the incoming messages have been read
720 host_time
+= host_frametime
;
722 // fetch results from server
723 if (cls
.state
== ca_connected
)
725 CL_ReadFromServer ();
729 if (host_speeds
.value
)
730 time1
= Sys_FloatTime ();
734 if (host_speeds
.value
)
735 time2
= Sys_FloatTime ();
738 if (cls
.signon
== SIGNONS
)
740 S_Update (r_origin
, vpn
, vright
, vup
);
744 S_Update (vec3_origin
, vec3_origin
, vec3_origin
, vec3_origin
);
746 if (bmg_type_changed
== true) {
748 bmg_type_changed
= false;
751 if (host_speeds
.value
)
753 pass1
= (time1
- time3
)*1000;
754 time3
= Sys_FloatTime ();
755 pass2
= (time2
- time1
)*1000;
756 pass3
= (time3
- time2
)*1000;
757 Con_Printf ("%3i tot %3i server %3i gfx %3i snd\n",
758 pass1
+pass2
+pass3
, pass1
, pass2
, pass3
);
762 //frame speed counter
766 void Host_Frame (float time
)
769 static double timetotal
;
770 static int timecount
;
773 if (!serverprofile
.value
)
779 time1
= Sys_FloatTime ();
781 time2
= Sys_FloatTime ();
783 timetotal
+= time2
- time1
;
786 if (timecount
< 1000)
789 m
= timetotal
*1000/timecount
;
793 for (i
=0 ; i
<svs
.maxclients
; i
++)
795 if (svs
.clients
[i
].active
)
799 Con_Printf ("serverprofile: %2i clients %2i msec\n", c
, m
);
802 //============================================================================
806 #define VCR_SIGNATURE 0x56435231
809 void Host_InitVCR (quakeparms_t
*parms
)
814 if (COM_CheckParm("-playback"))
817 Sys_Error("No other parameters allowed with -playback\n");
819 Sys_FileOpenRead("quake.vcr", &vcrFile
);
821 Sys_Error("playback file not found\n");
823 Sys_FileRead (vcrFile
, &i
, sizeof(int));
824 if (i
!= VCR_SIGNATURE
)
825 Sys_Error("Invalid signature in vcr file\n");
827 Sys_FileRead (vcrFile
, &com_argc
, sizeof(int));
828 com_argv
= malloc(com_argc
* sizeof(char *));
829 com_argv
[0] = parms
->argv
[0];
830 for (i
= 0; i
< com_argc
; i
++)
832 Sys_FileRead (vcrFile
, &len
, sizeof(int));
834 Sys_FileRead (vcrFile
, p
, len
);
837 com_argc
++; /* add one for arg[0] */
838 parms
->argc
= com_argc
;
839 parms
->argv
= com_argv
;
842 if ( (n
= COM_CheckParm("-record")) != 0)
844 vcrFile
= Sys_FileOpenWrite("quake.vcr");
847 Sys_FileWrite(vcrFile
, &i
, sizeof(int));
849 Sys_FileWrite(vcrFile
, &i
, sizeof(int));
850 for (i
= 1; i
< com_argc
; i
++)
855 Sys_FileWrite(vcrFile
, &len
, sizeof(int));
856 Sys_FileWrite(vcrFile
, "-playback", len
);
859 len
= Q_strlen(com_argv
[i
]) + 1;
860 Sys_FileWrite(vcrFile
, &len
, sizeof(int));
861 Sys_FileWrite(vcrFile
, com_argv
[i
], len
);
872 void Host_Init (quakeparms_t
*parms
)
874 scr_disabled_for_loading
= true;
877 minimum_memory
= MINIMUM_MEMORY
;
879 minimum_memory
= MINIMUM_MEMORY_LEVELPAK
;
881 if (COM_CheckParm ("-minmemory"))
882 parms
->memsize
= minimum_memory
;
886 if (parms
->memsize
< minimum_memory
)
887 Sys_Error ("Only %4.1f megs of memory available, can't execute game", parms
->memsize
/ (float)0x100000);
889 com_argc
= parms
->argc
;
890 com_argv
= parms
->argv
;
892 Memory_Init (parms
->membase
, parms
->memsize
);
897 Host_InitVCR (parms
);
898 COM_Init (parms
->basedir
);
900 W_LoadWadFile ("gfx.wad");
909 Con_Printf ("PSP Kurok Engine v 0.4\n"); //(EBOOT: "__TIME__" "__DATE__")\n");
911 int currentCPU
= scePowerGetCpuClockFrequency();
912 int currentVRAM
= sceGeEdramGetSize();
913 int currentVRAMADD
= sceGeEdramGetAddr();
916 Con_Printf ("PSP Normal 32MB RAM Mode \n");
919 Con_Printf ("PSP Slim 64MB RAM Mode \n");
922 Con_Printf ("%4.1f megabyte heap \n",parms
->memsize
/ (1024*1024.0));
923 Con_Printf ("%4.1f PSP application heap \n",1.0f
*PSP_HEAP_SIZE_MB
);
924 Con_Printf ("%d VRAM \n",currentVRAM
);
925 Con_Printf ("%d VRAM Address \n",currentVRAMADD
);
927 Con_Printf ("CPU Speed %d MHz\n", currentCPU
);
929 R_InitTextures (); // needed even for dedicated servers
931 if (cls
.state
!= ca_dedicated
)
933 host_basepal
= (byte
*)COM_LoadHunkFile ("gfx/palette.lmp");
935 Sys_Error ("Couldn't load gfx/palette.lmp");
936 host_colormap
= (byte
*)COM_LoadHunkFile ("gfx/colormap.lmp");
938 Sys_Error ("Couldn't load gfx/colormap.lmp");
940 #ifndef WIN32 // on non win32, mouse comes before video for security reasons
943 VID_Init (host_basepal
);
950 // on Win32, sound initialization has to come before video initialization, so we
951 // can put up a popup if the sound hardware is in use
956 // FIXME: doesn't use the new one-window approach yet
964 #ifdef WIN32 // on non win32, mouse comes before video for security reasons
969 Cbuf_InsertText ("exec quake.rc\n");
971 Hunk_AllocName (0, "-HOST_HUNKLEVEL-");
972 host_hunklevel
= Hunk_LowMark ();
974 host_initialized
= true;
976 scr_disabled_for_loading
= false;
978 Con_Printf ("Loading ... Please wait ...\n");
980 Sys_Printf ("================ Kurok Initialized ===============\n");
988 FIXME: this is a callback from Sys_Quit and Sys_Error. It would be better
989 to run quit through here before the final handoff to the sys code.
992 void Host_Shutdown(void)
994 static qboolean isdown
= false;
998 printf ("recursive shutdown\n");
1003 // keep Con_Printf from trying to update the screen
1004 scr_disabled_for_loading
= true;
1006 Host_WriteConfiguration ();
1008 CDAudio_Shutdown ();
1013 if (cls
.state
!= ca_dedicated
)