]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - host.c
gl_rsurf: Don't cull geometry that leaves the view frustum with r_lockvisibility
[xonotic/darkplaces.git] / host.c
diff --git a/host.c b/host.c
index 592949c4de81dd98ce65283a02f90225d7ba91d5..3aa2b8db3a27ee0bbd14baae27a1f90ae4246a45 100644 (file)
--- a/host.c
+++ b/host.c
@@ -61,7 +61,7 @@ cvar_t cl_minfps_qualityhysteresis = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualit
 cvar_t cl_minfps_qualitystepmax = {CVAR_CLIENT | CVAR_SAVE, "cl_minfps_qualitystepmax", "0.1", "maximum quality change in a single frame"};
 cvar_t cl_minfps_force = {CVAR_CLIENT, "cl_minfps_force", "0", "also apply quality reductions in timedemo/capturevideo"};
 cvar_t cl_maxfps = {CVAR_CLIENT | CVAR_SAVE, "cl_maxfps", "0", "maximum fps cap, 0 = unlimited, if game is running faster than this it will wait before running another frame (useful to make cpu time available to other programs)"};
-cvar_t cl_maxfps_alwayssleep = {CVAR_CLIENT, "cl_maxfps_alwayssleep","1", "gives up some processing time to other applications each frame, value in milliseconds, disabled if cl_maxfps is 0"};
+cvar_t cl_maxfps_alwayssleep = {CVAR_CLIENT | CVAR_SAVE, "cl_maxfps_alwayssleep","1", "gives up some processing time to other applications each frame, value in milliseconds, disabled if cl_maxfps is 0"};
 cvar_t cl_maxidlefps = {CVAR_CLIENT | CVAR_SAVE, "cl_maxidlefps", "20", "maximum fps cap when the game is not the active window (makes cpu time available to other programs"};
 
 cvar_t developer = {CVAR_CLIENT | CVAR_SERVER | CVAR_SAVE, "developer","0", "shows debugging messages and information (recommended for all developers and level designers); the value -1 also suppresses buffering and logging these messages"};
@@ -413,7 +413,7 @@ void Host_Main(void)
 
        host.realtime = 0;
        host.sleeptime = 0;
-       host.dirtytime = Sys_DirtyTime();
+       host.dirtytime = oldtime = Sys_DirtyTime();
 
        while(host.state != host_shutdown)
        {
@@ -423,8 +423,7 @@ void Host_Main(void)
                        continue;                       // something bad happened, or the server disconnected
                }
 
-               oldtime = host.dirtytime;
-               newtime = Sys_DirtyTime();
+               newtime = host.dirtytime = Sys_DirtyTime();
                time = newtime - oldtime;
                if (time < 0)
                {
@@ -439,44 +438,6 @@ void Host_Main(void)
                        time = 0;
                }
                host.realtime += time;
-               host.dirtytime = newtime;
-
-               cl_timer += time;
-               sv_timer += time;
-
-               if (!svs.threaded)
-               {
-                       svs.perf_acc_sleeptime = host.sleeptime;
-                       svs.perf_acc_realtime += time;
-
-                       // Look for clients who have spawned
-                       playing = false;
-                       for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
-                               if(host_client->begun)
-                                       if(host_client->netconnection)
-                                               playing = true;
-                       if(sv.time < 10)
-                       {
-                               // don't accumulate time for the first 10 seconds of a match
-                               // so things can settle
-                               svs.perf_acc_realtime = svs.perf_acc_sleeptime = svs.perf_acc_lost = svs.perf_acc_offset = svs.perf_acc_offset_squared = svs.perf_acc_offset_max = svs.perf_acc_offset_samples = host.sleeptime = 0;
-                       }
-                       else if(svs.perf_acc_realtime > 5)
-                       {
-                               svs.perf_cpuload = 1 - svs.perf_acc_sleeptime / svs.perf_acc_realtime;
-                               svs.perf_lost = svs.perf_acc_lost / svs.perf_acc_realtime;
-                               if(svs.perf_acc_offset_samples > 0)
-                               {
-                                       svs.perf_offset_max = svs.perf_acc_offset_max;
-                                       svs.perf_offset_avg = svs.perf_acc_offset / svs.perf_acc_offset_samples;
-                                       svs.perf_offset_sdev = sqrt(svs.perf_acc_offset_squared / svs.perf_acc_offset_samples - svs.perf_offset_avg * svs.perf_offset_avg);
-                               }
-                               if(svs.perf_lost > 0 && developer_extra.integer)
-                                       if(playing) // only complain if anyone is looking
-                                               Con_DPrintf("Server can't keep up: %s\n", Host_TimingReport(vabuf, sizeof(vabuf)));
-                               svs.perf_acc_realtime = svs.perf_acc_sleeptime = svs.perf_acc_lost = svs.perf_acc_offset = svs.perf_acc_offset_squared = svs.perf_acc_offset_max = svs.perf_acc_offset_samples = host.sleeptime = 0;
-                       }
-               }
 
                if (host_framerate.value < 0.00001 && host_framerate.value != 0)
                        Cvar_SetValueQuick(&host_framerate, 0);
@@ -491,11 +452,6 @@ void Host_Main(void)
 
                Log_DestBuffer_Flush();
 
-               // receive packets on each main loop iteration, as the main loop may
-               // be undersleeping due to select() detecting a new packet
-               if (sv.active && !svs.threaded)
-                       NetConn_ServerFrame();
-
                Curl_Run();
 
                // check for commands typed to the host
@@ -514,42 +470,6 @@ void Host_Main(void)
 
                //Con_Printf("%6.0f %6.0f\n", cl_timer * 1000000.0, sv_timer * 1000000.0);
 
-               // if the accumulators haven't become positive yet, wait a while
-               if (cls.state == ca_dedicated)
-                       wait = sv_timer * -1000000.0;
-               else if (!sv.active || svs.threaded)
-                       wait = cl_timer * -1000000.0;
-               else
-                       wait = max(cl_timer, sv_timer) * -1000000.0;
-
-               if (!host.restless && wait >= 1)
-               {
-                       double time0, delta;
-
-                       if(host_maxwait.value <= 0)
-                               wait = min(wait, 1000000.0);
-                       else
-                               wait = min(wait, host_maxwait.value * 1000.0);
-                       if(wait < 1)
-                               wait = 1; // because we cast to int
-
-                       time0 = Sys_DirtyTime();
-                       if (sv_checkforpacketsduringsleep.integer && !sys_usenoclockbutbenchmark.integer && !svs.threaded) {
-                               NetConn_SleepMicroseconds((int)wait);
-                               if (cls.state != ca_dedicated)
-                                       NetConn_ClientFrame(); // helps server browser get good ping values
-                               // TODO can we do the same for ServerFrame? Probably not.
-                       }
-                       else
-                               Sys_Sleep((int)wait);
-                       delta = Sys_DirtyTime() - time0;
-                       if (delta < 0 || delta >= 1800) 
-                               delta = 0;
-                       host.sleeptime += delta;
-//                     R_TimeReport("sleep");
-                       continue;
-               }
-
                R_TimeReport("---");
 
        //-------------------
@@ -566,93 +486,129 @@ void Host_Main(void)
                        sv_timer = 0.1;
                }
 
-               if (sv.active && sv_timer > 0 && !svs.threaded)
+               if (!svs.threaded)
                {
-                       // execute one or more server frames, with an upper limit on how much
-                       // execution time to spend on server frames to avoid freezing the game if
-                       // the server is overloaded, this execution time limit means the game will
-                       // slow down if the server is taking too long.
-                       int framecount, framelimit = 1;
-                       double advancetime, aborttime = 0;
-                       float offset;
-                       prvm_prog_t *prog = SVVM_prog;
-
-                       // run the world state
-                       // don't allow simulation to run too fast or too slow or logic glitches can occur
-
-                       // stop running server frames if the wall time reaches this value
-                       if (sys_ticrate.value <= 0)
-                               advancetime = sv_timer;
-                       else if (cl.islocalgame && !sv_fixedframeratesingleplayer.integer)
+                       svs.perf_acc_sleeptime = host.sleeptime;
+                       svs.perf_acc_realtime += time;
+
+                       // Look for clients who have spawned
+                       playing = false;
+                       for (i = 0, host_client = svs.clients;i < svs.maxclients;i++, host_client++)
+                               if(host_client->begun)
+                                       if(host_client->netconnection)
+                                               playing = true;
+                       if(sv.time < 10)
                        {
-                               // synchronize to the client frametime, but no less than 10ms and no more than 100ms
-                               advancetime = bound(0.01, cl_timer, 0.1);
+                               // don't accumulate time for the first 10 seconds of a match
+                               // so things can settle
+                               svs.perf_acc_realtime = svs.perf_acc_sleeptime = svs.perf_acc_lost = svs.perf_acc_offset = svs.perf_acc_offset_squared = svs.perf_acc_offset_max = svs.perf_acc_offset_samples = host.sleeptime = 0;
                        }
-                       else
+                       else if(svs.perf_acc_realtime > 5)
                        {
-                               advancetime = sys_ticrate.value;
-                               // listen servers can run multiple server frames per client frame
-                               framelimit = cl_maxphysicsframesperserverframe.integer;
-                               aborttime = Sys_DirtyTime() + 0.1;
+                               svs.perf_cpuload = 1 - svs.perf_acc_sleeptime / svs.perf_acc_realtime;
+                               svs.perf_lost = svs.perf_acc_lost / svs.perf_acc_realtime;
+                               if(svs.perf_acc_offset_samples > 0)
+                               {
+                                       svs.perf_offset_max = svs.perf_acc_offset_max;
+                                       svs.perf_offset_avg = svs.perf_acc_offset / svs.perf_acc_offset_samples;
+                                       svs.perf_offset_sdev = sqrt(svs.perf_acc_offset_squared / svs.perf_acc_offset_samples - svs.perf_offset_avg * svs.perf_offset_avg);
+                               }
+                               if(svs.perf_lost > 0 && developer_extra.integer)
+                                       if(playing) // only complain if anyone is looking
+                                               Con_DPrintf("Server can't keep up: %s\n", Host_TimingReport(vabuf, sizeof(vabuf)));
+                               svs.perf_acc_realtime = svs.perf_acc_sleeptime = svs.perf_acc_lost = svs.perf_acc_offset = svs.perf_acc_offset_squared = svs.perf_acc_offset_max = svs.perf_acc_offset_samples = host.sleeptime = 0;
                        }
-                       if(host_timescale.value > 0 && host_timescale.value < 1)
-                               advancetime = min(advancetime, 0.1 / host_timescale.value);
-                       else
-                               advancetime = min(advancetime, 0.1);
 
-                       if(advancetime > 0)
+                       if (sv.active && sv_timer > 0)
                        {
-                               offset = Sys_DirtyTime() - newtime;if (offset < 0 || offset >= 1800) offset = 0;
-                               offset += sv_timer;
-                               ++svs.perf_acc_offset_samples;
-                               svs.perf_acc_offset += offset;
-                               svs.perf_acc_offset_squared += offset * offset;
-                               if(svs.perf_acc_offset_max < offset)
-                                       svs.perf_acc_offset_max = offset;
-                       }
+                               // execute one or more server frames, with an upper limit on how much
+                               // execution time to spend on server frames to avoid freezing the game if
+                               // the server is overloaded, this execution time limit means the game will
+                               // slow down if the server is taking too long.
+                               int framecount, framelimit = 1;
+                               double advancetime, aborttime = 0;
+                               float offset;
+                               prvm_prog_t *prog = SVVM_prog;
+                               // receive packets on each main loop iteration, as the main loop may
+                               // be undersleeping due to select() detecting a new packet
+                               if (sv.active && !svs.threaded)
+                                       NetConn_ServerFrame();
+                               // run the world state
+                               // don't allow simulation to run too fast or too slow or logic glitches can occur
+
+                               // stop running server frames if the wall time reaches this value
+                               if (sys_ticrate.value <= 0)
+                                       advancetime = sv_timer;
+                               else if (cl.islocalgame && !sv_fixedframeratesingleplayer.integer)
+                               {
+                                       // synchronize to the client frametime, but no less than 10ms and no more than 100ms
+                                       advancetime = bound(0.01, cl_timer, 0.1);
+                               }
+                               else
+                               {
+                                       advancetime = sys_ticrate.value;
+                                       // listen servers can run multiple server frames per client frame
+                                       framelimit = cl_maxphysicsframesperserverframe.integer;
+                                       aborttime = Sys_DirtyTime() + 0.1;
+                               }
+                               if(host_timescale.value > 0 && host_timescale.value < 1)
+                                       advancetime = min(advancetime, 0.1 / host_timescale.value);
+                               else
+                                       advancetime = min(advancetime, 0.1);
 
-                       // only advance time if not paused
-                       // the game also pauses in singleplayer when menu or console is used
-                       sv.frametime = advancetime * host_timescale.value;
-                       if (host_framerate.value)
-                               sv.frametime = host_framerate.value;
-                       if (sv.paused || host.paused)
-                               sv.frametime = 0;
+                               if(advancetime > 0)
+                               {
+                                       offset = Sys_DirtyTime() - newtime;if (offset < 0 || offset >= 1800) offset = 0;
+                                       offset += sv_timer;
+                                       ++svs.perf_acc_offset_samples;
+                                       svs.perf_acc_offset += offset;
+                                       svs.perf_acc_offset_squared += offset * offset;
+                                       if(svs.perf_acc_offset_max < offset)
+                                               svs.perf_acc_offset_max = offset;
+                               }
 
-                       for (framecount = 0;framecount < framelimit && sv_timer > 0;framecount++)
-                       {
-                               sv_timer -= advancetime;
+                               // only advance time if not paused
+                               // the game also pauses in singleplayer when menu or console is used
+                               sv.frametime = advancetime * host_timescale.value;
+                               if (host_framerate.value)
+                                       sv.frametime = host_framerate.value;
+                               if (sv.paused || host.paused)
+                                       sv.frametime = 0;
 
-                               // move things around and think unless paused
-                               if (sv.frametime)
-                                       SV_Physics();
+                               for (framecount = 0;framecount < framelimit && sv_timer > 0;framecount++)
+                               {
+                                       sv_timer -= advancetime;
 
-                               // if this server frame took too long, break out of the loop
-                               if (framelimit > 1 && Sys_DirtyTime() >= aborttime)
-                                       break;
-                       }
-                       R_TimeReport("serverphysics");
+                                       // move things around and think unless paused
+                                       if (sv.frametime)
+                                               SV_Physics();
 
-                       // send all messages to the clients
-                       SV_SendClientMessages();
+                                       // if this server frame took too long, break out of the loop
+                                       if (framelimit > 1 && Sys_DirtyTime() >= aborttime)
+                                               break;
+                               }
+                               R_TimeReport("serverphysics");
 
-                       if (sv.paused == 1 && host.realtime > sv.pausedstart && sv.pausedstart > 0) {
-                               prog->globals.fp[OFS_PARM0] = host.realtime - sv.pausedstart;
-                               PRVM_serverglobalfloat(time) = sv.time;
-                               prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PausedTic), "QC function SV_PausedTic is missing");
-                       }
+                               // send all messages to the clients
+                               SV_SendClientMessages();
 
-                       // send an heartbeat if enough time has passed since the last one
-                       NetConn_Heartbeat(0);
-                       R_TimeReport("servernetwork");
-               }
-               else if (!svs.threaded)
-               {
-                       // don't let r_speeds display jump around
-                       R_TimeReport("serverphysics");
-                       R_TimeReport("servernetwork");
-               }
+                               if (sv.paused == 1 && host.realtime > sv.pausedstart && sv.pausedstart > 0) {
+                                       prog->globals.fp[OFS_PARM0] = host.realtime - sv.pausedstart;
+                                       PRVM_serverglobalfloat(time) = sv.time;
+                                       prog->ExecuteProgram(prog, PRVM_serverfunction(SV_PausedTic), "QC function SV_PausedTic is missing");
+                               }
 
+                               // send an heartbeat if enough time has passed since the last one
+                               NetConn_Heartbeat(0);
+                               R_TimeReport("servernetwork");
+                       }
+                       else
+                       {
+                               // don't let r_speeds display jump around
+                               R_TimeReport("serverphysics");
+                               R_TimeReport("servernetwork");
+                       }
+               }
                // if there is some time remaining from this frame, reset the timer
                if (sv_timer >= 0)
                {
@@ -661,6 +617,8 @@ void Host_Main(void)
                        sv_timer = 0;
                }
 
+               sv_timer += time;
+
        //-------------------
        //
        // client operations
@@ -805,6 +763,8 @@ void Host_Main(void)
                if (cl_timer >= 0)
                        cl_timer = 0;
 
+               cl_timer += time;
+
 #if MEMPARANOIA
                Mem_CheckSentinelsGlobal();
 #else
@@ -812,7 +772,38 @@ void Host_Main(void)
                        Mem_CheckSentinelsGlobal();
 #endif
 
+               // if the accumulators haven't become positive yet, wait a while
+               wait = max(cl_timer, sv_timer) * -1000000.0;
+
+               if (!host.restless && wait >= 1)
+               {
+                       double time0, delta;
+
+                       if(host_maxwait.value <= 0)
+                               wait = min(wait, 1000000.0);
+                       else
+                               wait = min(wait, host_maxwait.value * 1000.0);
+                       if(wait < 1)
+                               wait = 1; // because we cast to int
+
+                       time0 = Sys_DirtyTime();
+                       if (sv_checkforpacketsduringsleep.integer && !sys_usenoclockbutbenchmark.integer && !svs.threaded) {
+                               NetConn_SleepMicroseconds((int)wait);
+                               if (cls.state != ca_dedicated)
+                                       NetConn_ClientFrame(); // helps server browser get good ping values
+                               // TODO can we do the same for ServerFrame? Probably not.
+                       }
+                       else
+                               Sys_Sleep((int)wait);
+                       delta = Sys_DirtyTime() - time0;
+                       if (delta < 0 || delta >= 1800) 
+                               delta = 0;
+                       host.sleeptime += delta;
+//                     R_TimeReport("sleep");
+               }
+
                host.framecount++;
+               oldtime = newtime;
        }
 
        Sys_Quit(0);
@@ -1122,58 +1113,36 @@ void Host_Shutdown(void)
        }
        isdown = true;
 
-       // be quiet while shutting down
-       S_StopAllSounds();
+       if(cls.state != ca_dedicated)
+               CL_Shutdown();
 
        // end the server thread
        if (svs.threaded)
                SV_StopThread();
 
-       // disconnect client from server if active
-       CL_Disconnect();
-
        // shut down local server if active
        SV_LockThreadMutex();
        SV_Shutdown ();
        SV_UnlockThreadMutex();
 
-#ifdef CONFIG_MENU
-       // Shutdown menu
-       if(MR_Shutdown)
-               MR_Shutdown();
-#endif
-
        // AK shutdown PRVM
        // AK hmm, no PRVM_Shutdown(); yet
 
-       CL_Video_Shutdown();
-
        Host_SaveConfig();
 
-       CDAudio_Shutdown ();
-       S_Terminate ();
        Curl_Shutdown ();
        NetConn_Shutdown ();
 
-       if (cls.state != ca_dedicated)
-       {
-               R_Modules_Shutdown();
-               VID_Shutdown();
-       }
-
        SV_StopThread();
        TaskQueue_Shutdown();
        Thread_Shutdown();
        Cmd_Shutdown();
-       Key_Shutdown();
-       CL_Shutdown();
        Sys_Shutdown();
        Log_Close();
        Crypto_Shutdown();
 
        Host_UnlockSession();
 
-       S_Shutdown();
        Con_Shutdown();
        Memory_Shutdown();
 }