- if(svs.perf_lost > 0 && developer_extra.integer)
- Con_DPrintf("Server can't keep up: %s\n", Host_TimingReport());
- 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 = 0;
- }
-
- if (slowmo.value < 0.00001 && slowmo.value != 0)
- Cvar_SetValue("slowmo", 0);
- if (host_framerate.value < 0.00001 && host_framerate.value != 0)
- Cvar_SetValue("host_framerate", 0);
-
- // keep the random time dependent, but not when playing demos/benchmarking
- if(!*sv_random_seed.string && !cls.demoplayback)
- rand();
-
- cl.islocalgame = NetConn_IsLocalGame();
-
- // get new key events
- Key_EventQueue_Unblock();
- SndSys_SendKeyEvents();
- Sys_SendKeyEvents();
-
- NetConn_UpdateSockets();
-
- 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)
- NetConn_ServerFrame();
-
- Curl_Run();
-
- // check for commands typed to the host
- Host_GetConsoleCommands();
-
- // when a server is running we only execute console commands on server frames
- // (this mainly allows frikbot .way config files to work properly by staying in sync with the server qc)
- // otherwise we execute them on client frames
- if (sv.active ? sv_timer > 0 : cl_timer > 0)
- {
- // process console commands
-// R_TimeReport("preconsole");
- CL_VM_PreventInformationLeaks();
- Cbuf_Execute();
-// R_TimeReport("console");
- }
-
- //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)
- wait = cl_timer * -1000000.0;
- else
- wait = max(cl_timer, sv_timer) * -1000000.0;
-
- if (!cls.timedemo && wait >= 1)
- {
- double time0;
-
- 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_DoubleTime();
- if (sv_checkforpacketsduringsleep.integer && !sys_usenoclockbutbenchmark.integer)
- NetConn_SleepMicroseconds((int)wait);
- else
- Sys_Sleep((int)wait);
- svs.perf_acc_sleeptime += Sys_DoubleTime() - time0;
-// R_TimeReport("sleep");
- continue;
- }
-
- R_TimeReport("---");
-
- //-------------------
- //
- // server operations
- //
- //-------------------
-
- // limit the frametime steps to no more than 100ms each
- if (cl_timer > 0.1)
- cl_timer = 0.1;
- if (sv_timer > 0.1)
- {
- svs.perf_acc_lost += (sv_timer - 0.1);
- sv_timer = 0.1;
- }
-
- if (sv.active && sv_timer > 0)
- {
- // 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;
-
- // 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 sys_ticrate
- advancetime = bound(0.01, cl_timer, sys_ticrate.value);
- framelimit = 10;
- aborttime = realtime + 0.1;
- }
- else
- {
- advancetime = sys_ticrate.value;
- // listen servers can run multiple server frames per client frame
- if (cls.state == ca_connected)
- {
- framelimit = 10;
- aborttime = realtime + 0.1;
- }
- }
- if(slowmo.value > 0 && slowmo.value < 1)
- advancetime = min(advancetime, 0.1 / slowmo.value);
- else
- advancetime = min(advancetime, 0.1);
-
- if(advancetime > 0)
- {
- offset = sv_timer + (Sys_DoubleTime() - realtime);
- ++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;
- }
-
- // only advance time if not paused
- // the game also pauses in singleplayer when menu or console is used
- 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 || cl.csqc_paused)))
- sv.frametime = 0;
-
- // setup the VM frame
- SV_VM_Begin();
-
- for (framecount = 0;framecount < framelimit && sv_timer > 0;framecount++)
- {
- sv_timer -= advancetime;
-
- // move things around and think unless paused
- if (sv.frametime)
- SV_Physics();
-
- // if this server frame took too long, break out of the loop
- if (framelimit > 1 && Sys_DoubleTime() >= aborttime)
- break;
- }
- R_TimeReport("serverphysics");
-
- // send all messages to the clients
- SV_SendClientMessages();
-
- if (sv.paused == 1 && realtime > sv.pausedstart && sv.pausedstart > 0) {
- prog->globals.generic[OFS_PARM0] = realtime - sv.pausedstart;
- PRVM_ExecuteProgram(prog->funcoffsets.SV_PausedTic, "QC function SV_PausedTic is missing");
- }
-
- // end the server VM frame
- SV_VM_End();
-
- // 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");
- }
-
- //-------------------
- //
- // client operations
- //
- //-------------------
-
- if (cls.state != ca_dedicated && (cl_timer > 0 || cls.timedemo || ((vid_activewindow ? cl_maxfps : cl_maxidlefps).value < 1)))
- {
- R_TimeReport("---");
- // decide the simulation time
- if (cls.capturevideo.active)
- {
- //***
- if (cls.capturevideo.realtime)
- clframetime = cl.realframetime = max(cl_timer, 1.0 / cls.capturevideo.framerate);
- else
- {
- clframetime = 1.0 / cls.capturevideo.framerate;
- cl.realframetime = max(cl_timer, clframetime);
- }
- }
- else if (vid_activewindow && cl_maxfps.value >= 1 && !cls.timedemo)
- {
- clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxfps.value);
- // when running slow, we need to sleep to keep input responsive
- wait = bound(0, cl_maxfps_alwayssleep.value * 1000, 100000);
- if (wait > 0)
- Sys_Sleep((int)wait);
- }
- else if (!vid_activewindow && cl_maxidlefps.value >= 1 && !cls.timedemo)
- clframetime = cl.realframetime = max(cl_timer, 1.0 / cl_maxidlefps.value);