Advertisement
Guest User

StartLagCompensation

a guest
Nov 20th, 2015
2,821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. void CLagCompensationManager::StartLagCompensation( CBasePlayer *player, CUserCmd *cmd )
  2. {
  3.     // Assume no players need to be restored
  4.     m_RestorePlayer.ClearAll();
  5.     m_bNeedToRestore = false;
  6.  
  7.     m_pCurrentPlayer = player;
  8.    
  9.     if ( !player->m_bLagCompensation        // Player not wanting lag compensation
  10.          || (gpGlobals->maxClients <= 1)    // no lag compensation in single player
  11.          || !sv_unlag.GetBool()             // disabled by server admin
  12.          || player->IsBot()                 // not for bots
  13.          || player->IsObserver()            // not for spectators
  14.         )
  15.         return;
  16.  
  17.     // NOTE: Put this here so that it won't show up in single player mode.
  18.     VPROF_BUDGET( "StartLagCompensation", VPROF_BUDGETGROUP_OTHER_NETWORKING );
  19.     Q_memset( m_RestoreData, 0, sizeof( m_RestoreData ) );
  20.     Q_memset( m_ChangeData, 0, sizeof( m_ChangeData ) );
  21.  
  22.     // Get true latency
  23.  
  24.     // correct is the amout of time we have to correct game time
  25.     float correct = 0.0f;
  26.  
  27.     INetChannelInfo *nci = engine->GetPlayerNetInfo( player->entindex() );
  28.  
  29.     if ( nci )
  30.     {
  31.         // add network latency
  32.         correct+= nci->GetLatency( FLOW_OUTGOING );
  33.     }
  34.  
  35.     // calc number of view interpolation ticks - 1
  36.     int lerpTicks = TIME_TO_TICKS( player->m_fLerpTime );
  37.  
  38.     // add view interpolation latency see C_BaseEntity::GetInterpolationAmount()
  39.     correct += TICKS_TO_TIME( lerpTicks );
  40.    
  41.     // check bouns [0,sv_maxunlag]
  42.     correct = clamp( correct, 0.0f, sv_maxunlag.GetFloat() );
  43.  
  44.     // correct tick send by player
  45.     int targettick = cmd->tick_count - lerpTicks;
  46.  
  47.     // calc difference between tick send by player and our latency based tick
  48.     float deltaTime =  correct - TICKS_TO_TIME(gpGlobals->tickcount - targettick);
  49.  
  50.     if ( fabs( deltaTime ) > 0.2f )
  51.     {
  52.         // difference between cmd time and latency is too big > 200ms, use time correction based on latency
  53.         // DevMsg("StartLagCompensation: delta too big (%.3f)\n", deltaTime );
  54.         targettick = gpGlobals->tickcount - TIME_TO_TICKS( correct );
  55.     }
  56.    
  57.     // Iterate all active players
  58.     const CBitVec<MAX_EDICTS> *pEntityTransmitBits = engine->GetEntityTransmitBitsForClient( player->entindex() - 1 );
  59.     for ( int i = 1; i <= gpGlobals->maxClients; i++ )
  60.     {
  61.         CBasePlayer *pPlayer = UTIL_PlayerByIndex( i );
  62.  
  63.         if ( !pPlayer )
  64.         {
  65.             continue;
  66.         }
  67.  
  68.         // Don't lag compensate yourself you loser...
  69.         if ( player == pPlayer )
  70.         {
  71.             continue;
  72.         }
  73.  
  74.         // Custom checks for if things should lag compensate (based on things like what team the player is on).
  75.         if ( !player->WantsLagCompensationOnEntity( pPlayer, cmd, pEntityTransmitBits ) )
  76.             continue;
  77.  
  78.         // Move other player back in time
  79.         BacktrackPlayer( pPlayer, TICKS_TO_TIME( targettick ) );
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement