From a470c3a0ea90e644f127440a9134fe3d52bf7c49 Mon Sep 17 00:00:00 2001 From: Kamay Xutax Date: Fri, 12 Jul 2024 15:15:40 +0200 Subject: [PATCH] Fixed lag compensation with low fps --- game/client/in_main.cpp | 2 +- game/server/player_lagcompensation.cpp | 67 +++++--------------------- game/shared/usercmd.cpp | 6 +-- game/shared/usercmd.h | 5 +- 4 files changed, 20 insertions(+), 60 deletions(-) diff --git a/game/client/in_main.cpp b/game/client/in_main.cpp index 858208301b..766107d9e5 100644 --- a/game/client/in_main.cpp +++ b/game/client/in_main.cpp @@ -1328,7 +1328,7 @@ void CInput::CreateMove ( int sequence_number, float input_sample_frametime, boo } cmd->has_animation[pBasePlayer->index] = true; - cmd->animationdata[pBasePlayer->index].m_flAnimTime = pBasePlayer->m_flAnimTime; + cmd->animationdata[pBasePlayer->index].m_flUninterpolatedSimulationTime = pBasePlayer->m_flSimulationTime; } pVerified->m_cmd = *cmd; diff --git a/game/server/player_lagcompensation.cpp b/game/server/player_lagcompensation.cpp index 35ad65d88e..f8b3cc75b5 100644 --- a/game/server/player_lagcompensation.cpp +++ b/game/server/player_lagcompensation.cpp @@ -388,33 +388,6 @@ void CLagCompensationManager::StartLagCompensation( CBasePlayer *player, CUserCm } } -template -inline T LoopingLerp( float flPercent, T flFrom, T flTo ) -{ - T s = flTo * flPercent + flFrom * (1.0f - flPercent); - return s; -} - -template <> -inline float LoopingLerp( float flPercent, float flFrom, float flTo ) -{ - if ( fabs( flTo - flFrom ) >= 0.5f ) - { - if (flFrom < flTo) - flFrom += 1.0f; - else - flTo += 1.0f; - } - - float s = flTo * flPercent + flFrom * (1.0f - flPercent); - - s = s - (int)(s); - if (s < 0.0f) - s = s + 1.0f; - - return s; -} - void CLagCompensationManager::BacktrackPlayer( CBasePlayer *pPlayer, CUserCmd *cmd ) { Vector org; @@ -440,8 +413,10 @@ void CLagCompensationManager::BacktrackPlayer( CBasePlayer *pPlayer, CUserCmd *c LagRecord *prevRecordSim = NULL; LagRecord *recordSim = NULL; + LagRecord *recordAnim = NULL; - Vector prevOrg = pPlayer->GetLocalOrigin(); + Vector prevOrg = pPlayer->GetLocalOrigin(); + bool foundAnimationData = false; // Walk context looking for any invalidating event while( trackSim->IsValidIndex(currSim) ) @@ -450,7 +425,14 @@ void CLagCompensationManager::BacktrackPlayer( CBasePlayer *pPlayer, CUserCmd *c prevRecordSim = recordSim; // get next record - recordSim = &trackSim->Element( currSim ); + recordSim = &trackSim->Element(currSim); + + if (recordSim->m_flSimulationTime + <= animationData->m_flUninterpolatedSimulationTime and !foundAnimationData) + { + recordAnim = recordSim; + foundAnimationData = true; + } if ( !(recordSim->m_fFlags & LC_ALIVE) ) { @@ -470,31 +452,6 @@ void CLagCompensationManager::BacktrackPlayer( CBasePlayer *pPlayer, CUserCmd *c currSim = trackSim->Next( currSim ); } - intp currAnim = trackAnim->Head(); - LagRecord *recordAnim = NULL; - - // Walk context looking for any invalidating event - while( trackAnim->IsValidIndex(currAnim) ) - { - // get next record - recordAnim = &trackAnim->Element( currAnim ); - - if ( !(recordAnim->m_fFlags & LC_ALIVE) ) - { - // player most be alive, lost track - return; - } - - // TODO: do proper teleportation checks. - - // did we find a context smaller than target time ? - if ( recordAnim->m_flAnimTime <= animationData->m_flAnimTime ) - break; // hurra, stop - - // go one step back - currAnim = trackAnim->Next( currAnim ); - } - Assert( recordAnim ); Assert( recordSim ); @@ -706,7 +663,7 @@ void CLagCompensationManager::BacktrackPlayer( CBasePlayer *pPlayer, CUserCmd *c // Set lag compensated player's times pPlayer->SetSimulationTime(flTargetSimulationTime); - pPlayer->SetAnimTime(animationData->m_flAnimTime); + // pPlayer->SetAnimTime(animationData->m_flAnimTime); if ( sv_lagflushbonecache.GetBool() ) pPlayer->InvalidateBoneCache(); diff --git a/game/shared/usercmd.cpp b/game/shared/usercmd.cpp index 66725faa12..d9ad6b731d 100644 --- a/game/shared/usercmd.cpp +++ b/game/shared/usercmd.cpp @@ -204,10 +204,10 @@ void WriteUsercmd( bf_write *buf, const CUserCmd *to, const CUserCmd *from ) continue; } - if (to->animationdata[i].m_flAnimTime != from->animationdata[i].m_flAnimTime) + if (to->animationdata[i].m_flUninterpolatedSimulationTime != from->animationdata[i].m_flUninterpolatedSimulationTime) { buf->WriteOneBit( 1 ); - buf->WriteFloat( to->animationdata[i].m_flAnimTime ); + buf->WriteFloat( to->animationdata[i].m_flUninterpolatedSimulationTime ); } else { @@ -360,7 +360,7 @@ void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from ) if (buf->ReadOneBit()) { - move->animationdata[i].m_flAnimTime = buf->ReadFloat(); + move->animationdata[i].m_flUninterpolatedSimulationTime = buf->ReadFloat(); } } diff --git a/game/shared/usercmd.h b/game/shared/usercmd.h index d370318d60..28ed6b92d5 100644 --- a/game/shared/usercmd.h +++ b/game/shared/usercmd.h @@ -53,7 +53,10 @@ struct LayerRecord struct ClientSideAnimationData { - float m_flAnimTime; + // TODO_ENHANCED: + // For now we send the last received update for animations. + // anim time is unreliable on low fps. + float m_flUninterpolatedSimulationTime; }; class CEntityGroundContact