]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - host.c
reduced client memory usage by 29MB by making cl_max_entities dynamic (starts out...
[xonotic/darkplaces.git] / host.c
diff --git a/host.c b/host.c
index 94d097f83f14582dc2e8af26198f9d15ecbdb4ef..58b423e172aed05db9862c6a9f58fae6fc7e1eba 100644 (file)
--- a/host.c
+++ b/host.c
@@ -112,7 +112,7 @@ void Host_Error (const char *error, ...)
        va_list argptr;
 
        va_start (argptr,error);
-       vsprintf (hosterrorstring1,error,argptr);
+       dpvsnprintf (hosterrorstring1,sizeof(hosterrorstring1),error,argptr);
        va_end (argptr);
 
        Con_Printf("Host_Error: %s\n", hosterrorstring1);
@@ -229,7 +229,7 @@ void Host_SaveConfig_f(void);
 void Host_InitLocal (void)
 {
        Host_InitCommands ();
-       
+
        Cmd_AddCommand("saveconfig", Host_SaveConfig_f);
 
        Cvar_RegisterVariable (&host_framerate);
@@ -278,9 +278,10 @@ void Host_SaveConfig_f(void)
 
 // dedicated servers initialize the host but don't parse and set the
 // config.cfg cvars
-       if (host_initialized && cls.state != ca_dedicated)
+       // LordHavoc: save a config only after Host_Frame finished the first frame
+       if (host_initialized && host_loopactive && cls.state != ca_dedicated)
        {
-               f = FS_Open ("config.cfg", "w", false);
+               f = FS_Open ("config.cfg", "wb", false, false);
                if (!f)
                {
                        Con_Print("Couldn't write config.cfg.\n");
@@ -323,7 +324,7 @@ void SV_ClientPrintf(const char *fmt, ...)
        char msg[4096];
 
        va_start(argptr,fmt);
-       vsnprintf(msg,sizeof(msg),fmt,argptr);
+       dpvsnprintf(msg,sizeof(msg),fmt,argptr);
        va_end(argptr);
 
        SV_ClientPrint(msg);
@@ -351,7 +352,7 @@ void SV_BroadcastPrint(const char *msg)
        }
 
        if (sv_echobprint.integer && cls.state == ca_dedicated)
-               Sys_Print(msg);
+               Con_Print(msg);
 }
 
 /*
@@ -367,7 +368,7 @@ void SV_BroadcastPrintf(const char *fmt, ...)
        char msg[4096];
 
        va_start(argptr,fmt);
-       vsnprintf(msg,sizeof(msg),fmt,argptr);
+       dpvsnprintf(msg,sizeof(msg),fmt,argptr);
        va_end(argptr);
 
        SV_BroadcastPrint(msg);
@@ -386,7 +387,7 @@ void Host_ClientCommands(const char *fmt, ...)
        char string[1024];
 
        va_start(argptr,fmt);
-       vsprintf(string, fmt,argptr);
+       dpvsnprintf(string, sizeof(string), fmt, argptr);
        va_end(argptr);
 
        MSG_WriteByte(&host_client->message, svc_stufftext);
@@ -464,13 +465,13 @@ void SV_DropClient(qboolean crash)
                EntityFrame4_FreeDatabase(host_client->entitydatabase4);
        if (host_client->entitydatabase5)
                EntityFrame5_FreeDatabase(host_client->entitydatabase5);
-       
+
        if (sv.active)
        {
                // clear a fields that matter to DP_SV_CLIENTNAME and DP_SV_CLIENTCOLORS, and also frags
                ED_ClearEdict(host_client->edict);
        }
-       
+
        // clear the client struct (this sets active to false)
        memset(host_client, 0, sizeof(*host_client));
 
@@ -563,7 +564,7 @@ qboolean Host_FilterTime (double time)
        double timecap, timeleft;
        realtime += time;
 
-       if (sys_ticrate.value < 0.01 || sys_ticrate.value > 0.1)
+       if (sys_ticrate.value < 0.00999 || sys_ticrate.value > 0.10001)
                Cvar_SetValue("sys_ticrate", bound(0.01, sys_ticrate.value, 0.1));
        if (slowmo.value < 0)
                Cvar_SetValue("slowmo", 0);
@@ -597,12 +598,21 @@ qboolean Host_FilterTime (double time)
        timeleft = (oldrealtime - realtime) + timecap;
        if (timeleft > 0)
        {
+               int msleft;
                // don't totally hog the CPU
-               int msleft = (int)(timeleft * 1000);
-               // if not dedicated, try to hit exactly a steady framerate by by not
-               // sleeping the full amount
-               if (cls.state != ca_dedicated)
-                       msleft -= 1;
+               if (cls.state == ca_dedicated)
+               {
+                       // if dedicated, try to use as little cpu as possible by waiting
+                       // just a little longer than necessary
+                       // (yes this means it doesn't quite keep up with the framerate)
+                       msleft = (int)ceil(timeleft * 1000);
+               }
+               else
+               {
+                       // if not dedicated, try to hit exactly a steady framerate by not
+                       // sleeping the full amount
+                       msleft = (int)floor(timeleft * 1000);
+               }
                if (msleft > 0)
                        Sys_Sleep(msleft);
                return false;
@@ -658,34 +668,38 @@ Host_ServerFrame
 */
 void Host_ServerFrame (void)
 {
-       // never run more than 20 frames at a time as a sanity limit
-       int framecount, framelimit = 20;
+       // never run more than 5 frames at a time as a sanity limit
+       int framecount, framelimit = 5;
        double advancetime;
-       static double frametimetotal = 0, lastservertime = 0;
-       frametimetotal += host_frametime;
-       // LordHavoc: cap server at sys_ticrate in networked games
-       if (frametimetotal < 0.001 || (!cl.islocalgame && cls.state == ca_connected && sv.active && ((realtime - lastservertime) < sys_ticrate.value)))
+       if (!sv.active)
+       {
+               sv.timer = 0;
                return;
-       lastservertime = realtime;
-
-       // set the time and clear the general datagram
-       SV_ClearDatagram();
-
+       }
+       sv.timer += host_realframetime;
        // run the world state
        // don't allow simulation to run too fast or too slow or logic glitches can occur
-       for (framecount = 0;framecount < framelimit && frametimetotal > 0;framecount++, frametimetotal -= advancetime)
+       for (framecount = 0;framecount < framelimit && sv.timer > 0;framecount++)
        {
-               advancetime = min(frametimetotal, sys_ticrate.value);
+               if (cl.islocalgame)
+                       advancetime = min(sv.timer, sys_ticrate.value);
+               else
+                       advancetime = sys_ticrate.value;
+               sv.timer -= advancetime;
 
                // only advance time if not paused
                // the game also pauses in singleplayer when menu or console is used
-               if (!sv.paused && (!cl.islocalgame || (key_dest == key_game && !key_consoleactive)))
-                       sv.frametime = advancetime;
-               else
+               sv.frametime = advancetime * slowmo.value;
+               if (host_framerate.value)
+                       sv.frametime = host_framerate.value;
+               if (sv.paused || (cl.islocalgame && (key_dest != key_game || key_consoleactive)))
                        sv.frametime = 0;
 
                pr_global_struct->frametime = sv.frametime;
 
+               // set the time and clear the general datagram
+               SV_ClearDatagram();
+
                // check for network packets to the server each world step incase they
                // come in midframe (particularly if host is running really slow)
                NetConn_ServerFrame();
@@ -696,13 +710,16 @@ void Host_ServerFrame (void)
                // move things around and think unless paused
                if (sv.frametime)
                        SV_Physics();
-       }
 
-       // send all messages to the clients
-       SV_SendClientMessages();
+               // send all messages to the clients
+               SV_SendClientMessages();
 
-       // send an heartbeat if enough time has passed since the last one
-       NetConn_Heartbeat(0);
+               // send an heartbeat if enough time has passed since the last one
+               NetConn_Heartbeat(0);
+       }
+       // if we fell behind too many frames just don't worry about it
+       if (sv.timer > 0)
+               sv.timer = 0;
 }
 
 
@@ -719,7 +736,6 @@ void _Host_Frame (float time)
        static double time2 = 0;
        static double time3 = 0;
        int pass1, pass2, pass3;
-       usercmd_t cmd; // Used for receiving input
 
        if (setjmp(host_abortserver))
                return;                 // something bad happened, or the server disconnected
@@ -740,14 +756,14 @@ void _Host_Frame (float time)
        IN_Commands();
 
        // Collect input into cmd
-       IN_ProcessMove(&cmd);
+       IN_ProcessMove();
 
        // process console commands
        Cbuf_Execute();
 
        // if running the server locally, make intentions now
        if (cls.state == ca_connected && sv.active)
-               CL_SendCmd(&cmd);
+               CL_SendCmd();
 
 //-------------------
 //
@@ -777,7 +793,7 @@ void _Host_Frame (float time)
                // if running the server remotely, send intentions now after
                // the incoming messages have been read
                if (!sv.active)
-                       CL_SendCmd(&cmd);
+                       CL_SendCmd();
                CL_ReadFromServer();
        }
 
@@ -795,7 +811,7 @@ void _Host_Frame (float time)
                time2 = Sys_DoubleTime();
 
        // update audio
-       if (cls.signon == SIGNONS && cl.viewentity >= 0 && cl.viewentity < MAX_EDICTS && cl_entities[cl.viewentity].state_current.active)
+       if (cls.signon == SIGNONS && cl_entities[cl.viewentity].state_current.active)
        {
                // LordHavoc: this used to use renderer variables (eww)
                S_Update(&cl_entities[cl.viewentity].render.matrix);
@@ -925,10 +941,35 @@ void Host_Init (void)
        if (cls.state != ca_dedicated)
        {
                VID_Open();
+               CDAudio_Startup();
+               CL_InitTEnts ();  // We must wait after sound startup to load tent sounds
                SCR_BeginLoadingPlaque();
                MR_Init();
        }
 
+       // set up the default startmap_sp and startmap_dm aliases, mods can
+       // override these
+       if (gamemode == GAME_NEHAHRA)
+       {
+               Cbuf_InsertText ("alias startmap_sp \"map nehstart\"\n");
+               Cbuf_InsertText ("alias startmap_dm \"map nehstart\"\n");
+       }
+       else if (gamemode == GAME_TRANSFUSION)
+       {
+               Cbuf_InsertText ("alias startmap_sp \"map e1m1\"\n");
+               Cbuf_InsertText ("alias startmap_dm \"map bb1\"\n");
+       }
+       else if (gamemode == GAME_NEXUIZ)
+       {
+               Cbuf_InsertText ("alias startmap_sp \"map nexdm01\"\n");
+               Cbuf_InsertText ("alias startmap_dm \"map nexdm01\"\n");
+       }
+       else
+       {
+               Cbuf_InsertText ("alias startmap_sp \"map start\"\n");
+               Cbuf_InsertText ("alias startmap_dm \"map start\"\n");
+       }
+
        // stuff it again so the first host frame will execute it again, this time
        // in its entirety
        if (gamemode == GAME_TEU)
@@ -936,22 +977,22 @@ void Host_Init (void)
        else
                Cbuf_InsertText("exec quake.rc\n");
 
+       Cbuf_Execute();
+       Cbuf_Execute();
+       Cbuf_Execute();
+
        if (!sv.active && (cls.state == ca_dedicated || COM_CheckParm("-listen")))
-       {
-               if (gamemode == GAME_TRANSFUSION)
-                       Cbuf_InsertText ("map bb1\n");
-               else if (gamemode == GAME_NEXUIZ)
-                       Cbuf_InsertText ("map nexdm01\n");
-               else
-                       Cbuf_InsertText ("map start\n");
-       }
+               Cbuf_InsertText ("startmap_dm\n");
 
        // check for special benchmark mode
 // COMMANDLINEOPTION: Client: -benchmark <demoname> runs a timedemo and quits, results of any timedemo can be found in gamedir/benchmark.log (for example id1/benchmark.log)
        i = COM_CheckParm("-benchmark");
-       if (i && i + 1 < com_argc)
+       if (i && i + 1 < com_argc && !sv.active)
                Cbuf_InsertText(va("timedemo %s\n", com_argv[i + 1]));
 
+       if (!sv.active && !cls.demoplayback && !cls.connect_trying)
+               Cbuf_InsertText("togglemenu\n");
+
        Cbuf_Execute();
 
        // We must wait for the log_file cvar to be initialized to start the log
@@ -991,11 +1032,15 @@ void Host_Shutdown(void)
        // AK shutdown PRVM
        // AK hmm, no PRVM_Shutdown(); yet
 
+       CL_Video_Shutdown();
 
        Host_SaveConfig_f();
 
        CDAudio_Shutdown ();
+       S_Terminate ();
        NetConn_Shutdown ();
+       PR_Shutdown ();
+       Cbuf_Shutdown ();
 
        if (cls.state != ca_dedicated)
        {
@@ -1003,7 +1048,11 @@ void Host_Shutdown(void)
                VID_Shutdown();
        }
 
+       Cmd_Shutdown();
+       CL_Shutdown();
        Sys_Shutdown();
        Log_Close ();
+       COM_Shutdown ();
+       Memory_Shutdown();
 }