]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - host.c
cl_main: Slight refactor
[xonotic/darkplaces.git] / host.c
diff --git a/host.c b/host.c
index 92537f39369e82e843a1542a24f1b6a5523ad916..5c50ba791499266d80394d0f7392da7faf990fa0 100644 (file)
--- a/host.c
+++ b/host.c
@@ -167,6 +167,21 @@ static void Host_Framerate_c(cvar_t *var)
                Cvar_SetValueQuick(var, 0);
 }
 
+// TODO: Find a better home for this.
+static void SendCvar_f(cmd_state_t *cmd)
+{
+       if(cmd->source == src_local && host.hook.SV_SendCvar)
+       {
+               host.hook.SV_SendCvar(cmd);
+               return;
+       }
+       if(cmd->source == src_client && host.hook.CL_SendCvar)
+       {
+               host.hook.CL_SendCvar(cmd);
+               return;
+       }
+}
+
 /*
 =======================
 Host_InitLocal
@@ -182,6 +197,7 @@ static void Host_InitLocal (void)
        Cmd_AddCommand(CF_SHARED, "version", Host_Version_f, "print engine version");
        Cmd_AddCommand(CF_SHARED, "saveconfig", Host_SaveConfig_f, "save settings to config.cfg (or a specified filename) immediately (also automatic when quitting)");
        Cmd_AddCommand(CF_SHARED, "loadconfig", Host_LoadConfig_f, "reset everything and reload configs");
+       Cmd_AddCommand(CF_SHARED, "sendcvar", SendCvar_f, "sends the value of a cvar to the server as a sentcvar command, for use by QuakeC");
        Cvar_RegisterVariable (&cl_maxphysicsframesperserverframe);
        Cvar_RegisterVariable (&host_framerate);
        Cvar_RegisterCallback (&host_framerate, Host_Framerate_c);
@@ -277,34 +293,14 @@ void Host_LoadConfig_f(cmd_state_t *cmd)
        Cmd_RestoreInitState();
 #ifdef CONFIG_MENU
        // prepend a menu restart command to execute after the config
-       Cbuf_InsertText(&cmd_client, "\nmenu_restart\n");
+       Cbuf_InsertText(cmd_client, "\nmenu_restart\n");
 #endif
        // reset cvars to their defaults, and then exec startup scripts again
-       Host_AddConfigText(&cmd_client);
+       Host_AddConfigText(cmd_client);
 }
 
 //============================================================================
 
-/*
-===================
-Host_GetConsoleCommands
-
-Add them exactly as if they had been typed at the console
-===================
-*/
-static void Host_GetConsoleCommands (void)
-{
-       char *line;
-
-       while ((line = Sys_ConsoleInput()))
-       {
-               if (cls.state == ca_dedicated)
-                       Cbuf_AddText(&cmd_server, line);
-               else
-                       Cbuf_AddText(&cmd_client, line);
-       }
-}
-
 /*
 ==================
 Host_TimeReport
@@ -327,9 +323,7 @@ Runs all active servers
 static void Host_Init(void);
 double Host_Frame(double time)
 {
-       double cl_timer = 0;
-       double sv_timer = 0;
-       static double wait;
+       double cl_wait, sv_wait;
 
        TaskQueue_Frame(false);
 
@@ -341,43 +335,36 @@ double Host_Frame(double time)
 
        Log_DestBuffer_Flush();
 
-       Curl_Run();
-
-       // check for commands typed to the host
-       Host_GetConsoleCommands();
+       // Run any downloads
+       Curl_Frame();
 
        // process console commands
-//             R_TimeReport("preconsole");
-
        Cbuf_Frame(host.cbuf);
 
-//             R_TimeReport("console");
-
-       //Con_Printf("%6.0f %6.0f\n", cl_timer * 1000000.0, sv_timer * 1000000.0);
-
        R_TimeReport("---");
 
-       sv_timer = SV_Frame(time);
-       cl_timer = CL_Frame(time);
+       sv_wait = SV_Frame(time);
+       cl_wait = CL_Frame(time);
+
+//     Con_Printf("%6.0f %6.0f\n", cl_wait * 1000000.0, sv_wait * 1000000.0);
 
        Mem_CheckSentinelsGlobal();
 
-       // if the accumulators haven't become positive yet, wait a while
-       if(!sv_timer || !cl_timer)
-               wait = min(cl_timer, sv_timer) * -1000000.0;
-       else
-               wait = max(cl_timer, sv_timer) * -1000000.0;
+       if(host.restless)
+               return 0;
 
-       if (!host.restless && wait >= 1)
-               return wait;
+       // if the accumulators haven't become positive yet, wait a while
+       if (cls.state == ca_dedicated)
+               return sv_wait * -1000000.0; // dedicated
+       else if (!sv.active || svs.threaded)
+               return cl_wait * -1000000.0; // connected to server, main menu, or server is on different thread
        else
-               return 0;
+               return max(cl_wait, sv_wait) * -1000000.0; // listen server or singleplayer
 }
 
 static inline void Host_Sleep(double time)
 {
-       static double delta;
-       double time0;
+       double delta, time0;
 
        if(host_maxwait.value <= 0)
                time = min(time, 1000000.0);
@@ -387,8 +374,7 @@ static inline void Host_Sleep(double time)
                time = 1; // because we cast to int
 
        time0 = Sys_DirtyTime();
-       if (sv_checkforpacketsduringsleep.integer && !sys_usenoclockbutbenchmark.integer && !svs.threaded)
-       {
+       if (sv_checkforpacketsduringsleep.integer && !sys_usenoclockbutbenchmark.integer && !svs.threaded) {
                NetConn_SleepMicroseconds((int)time);
                if (cls.state != ca_dedicated)
                        NetConn_ClientFrame(); // helps server browser get good ping values
@@ -450,7 +436,7 @@ void Host_Main(void)
                sleeptime = Host_Frame(time);
                oldtime = newtime;
 
-               if (sleeptime)
+               if (sleeptime >= 1)
                {
                        Host_Sleep(sleeptime);
                        continue;
@@ -541,7 +527,6 @@ static void Host_Init (void)
        int i;
        const char* os;
        char vabuf[1024];
-       cmd_state_t *cmd = &cmd_client;
 
        host.hook.ConnectLocal = NULL;
        host.hook.Disconnect = NULL;
@@ -655,13 +640,13 @@ static void Host_Init (void)
 
        // here comes the not so critical stuff
 
-       Host_AddConfigText(cmd);
+       Host_AddConfigText(cmd_client);
 
        // if quake.rc is missing, use default
        if (!FS_FileExists("quake.rc"))
        {
-               Cbuf_AddText(cmd, "exec default.cfg\nexec " CONFIGFILENAME "\nexec autoexec.cfg\n");
-               Cbuf_Execute(cmd->cbuf);
+               Cbuf_AddText(cmd_client, "exec default.cfg\nexec " CONFIGFILENAME "\nexec autoexec.cfg\n");
+               Cbuf_Execute(cmd_client->cbuf);
        }
 
        host.state = host_active;
@@ -682,8 +667,8 @@ static void Host_Init (void)
        if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "timedemo %s\n", sys.argv[i + 1]));
-               Cbuf_Execute((&cmd_client)->cbuf);
+               Cbuf_AddText(cmd_client, va(vabuf, sizeof(vabuf), "timedemo %s\n", sys.argv[i + 1]));
+               Cbuf_Execute(cmd_client->cbuf);
        }
 
        // check for special demo mode
@@ -692,8 +677,8 @@ static void Host_Init (void)
        if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\n", sys.argv[i + 1]));
-               Cbuf_Execute((&cmd_client)->cbuf);
+               Cbuf_AddText(cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\n", sys.argv[i + 1]));
+               Cbuf_Execute(cmd_client->cbuf);
        }
 
 #ifdef CONFIG_VIDEO_CAPTURE
@@ -702,24 +687,24 @@ static void Host_Init (void)
        if (i && i + 1 < sys.argc)
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\ncl_capturevideo 1\n", sys.argv[i + 1]));
-               Cbuf_Execute((&cmd_client)->cbuf);
+               Cbuf_AddText(cmd_client, va(vabuf, sizeof(vabuf), "playdemo %s\ncl_capturevideo 1\n", sys.argv[i + 1]));
+               Cbuf_Execute((cmd_client)->cbuf);
        }
 #endif
 
        if (cls.state == ca_dedicated || Sys_CheckParm("-listen"))
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
-               Cbuf_AddText(&cmd_client, "startmap_dm\n");
-               Cbuf_Execute((&cmd_client)->cbuf);
+               Cbuf_AddText(cmd_client, "startmap_dm\n");
+               Cbuf_Execute(cmd_client->cbuf);
        }
 
        if (!sv.active && !cls.demoplayback && !cls.connect_trying)
        {
 #ifdef CONFIG_MENU
-               Cbuf_AddText(&cmd_client, "togglemenu 1\n");
+               Cbuf_AddText(cmd_client, "togglemenu 1\n");
 #endif
-               Cbuf_Execute((&cmd_client)->cbuf);
+               Cbuf_Execute(cmd_client->cbuf);
        }
 
        Con_DPrint("========Initialized=========\n");