From 5e47e45d837132eeeecad606ec3d573c999146c7 Mon Sep 17 00:00:00 2001 From: Amos Date: Thu, 17 Aug 2023 01:59:54 +0200 Subject: [PATCH] Take clock drift into account during lag compensation clamp Client's clock could be behind or ahead of the server's, take this into account. Note that this code has been tested over night and only had a few hits when new baseline snapshots were applied on the server, or when there was a clock drift. It still requires an actual playtest with several players on low and high latency to determine if this is even needed. --- r5dev/game/server/player.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/r5dev/game/server/player.cpp b/r5dev/game/server/player.cpp index 1c0b3c0a..91f3fa83 100644 --- a/r5dev/game/server/player.cpp +++ b/r5dev/game/server/player.cpp @@ -125,6 +125,7 @@ void CPlayer::ProcessUserCmds(CUserCmd* cmds, int numCmds, int totalCmds, CUserCmd* lastCmd = &m_Commands[MAX_QUEUED_COMMANDS_PROCESS]; + const float clockDriftMsecs = sv_clockcorrection_msecs->GetFloat(); const float maxUnlag = sv_maxunlag->GetFloat(); const float latencyAmount = Clamp(chan->GetLatency(FLOW_OUTGOING), 0.0f, maxUnlag); const float serverTime = (*g_pGlobals)->m_flCurTime; @@ -165,26 +166,26 @@ void CPlayer::ProcessUserCmds(CUserCmd* cmds, int numCmds, int totalCmds, __FUNCTION__, commandDelta, maxUnlag); } } - else if (commandTime < lastCommandTime) + else if (commandTime < (lastCommandTime - clockDriftMsecs)) { // Can never be lower than last !!! recomputeUnlag = true; if (IsDebug()) { - Warning(eDLL_T::SERVER, "%s: cmd->command_time( %f ) < m_LastCmd.command_time( %f ) !!!\n", - __FUNCTION__, commandTime, lastCommandTime); + Warning(eDLL_T::SERVER, "%s: cmd->command_time( %f ) < (m_LastCmd.command_time( %f ) - sv_clockcorrection_msecs->GetFloat( %f )) !!!\n", + __FUNCTION__, commandTime, lastCommandTime, clockDriftMsecs); } } - else if (commandTime > serverTime) + else if (commandTime > (serverTime + clockDriftMsecs)) { // Too far in the future, clamp to max !!! recomputeUnlag = true; if (IsDebug()) { - Warning(eDLL_T::SERVER, "%s: cmd->command_time( %f ) > g_pGlobals->m_flCurTime( %f ) !!!\n", - __FUNCTION__, commandTime, serverTime); + Warning(eDLL_T::SERVER, "%s: cmd->command_time( %f ) > (g_pGlobals->m_flCurTime( %f ) + sv_clockcorrection_msecs->GetFloat( %f )) !!!\n", + __FUNCTION__, commandTime, serverTime, clockDriftMsecs); } }