Fixed possible buffer overflow & optimized network

This commit is contained in:
Kamay Xutax 2024-08-31 00:20:04 +02:00
parent 8db1b5ec1a
commit 380f352adf
9 changed files with 233 additions and 352 deletions

View file

@ -62,105 +62,100 @@ static inline bool EncodeSpecialFloat( const SendProp *pProp, float fVal, bf_wri
// TODO_ENHANCED: // TODO_ENHANCED:
// Encoding works by sending a normalized float (from range 0.0 to 1.0) // Encoding works by sending a normalized float (from range 0.0 to 1.0)
// This works because we know the range we encode the float to // This works because we know the range we encode the float to
static inline void EncodeFloat(const SendProp* pProp, static inline void EncodeFloat( const SendProp* pProp, float fVal, bf_write* pOut, int objectID )
float fVal,
bf_write* pOut,
int objectID)
{ {
const auto WriteNormalizedFloat = [&](double dblValue) const auto WriteNormalizedFloat = [&]( double dblValue )
{ {
bool isPerfectOne = dblValue >= 1.0; bool isPerfectOne = dblValue >= 1.0;
if (isPerfectOne) if ( isPerfectOne )
{ {
pOut->WriteOneBit(true); pOut->WriteOneBit( true );
} }
else else
{ {
pOut->WriteOneBit(false); pOut->WriteOneBit( false );
char strNumber[16]; char strNumber[16];
V_memset(strNumber, 0, sizeof(strNumber)); V_memset( strNumber, 0, sizeof( strNumber ) );
V_sprintf_safe(strNumber, "%f", static_cast<float>(dblValue)); V_sprintf_safe( strNumber, "%f", static_cast< float >( dblValue ) );
auto sixDigitsValue = V_atoi(&strNumber[2]); auto sixDigitsValue = V_atoi( &strNumber[2] );
if (sixDigitsValue > 999999) if ( sixDigitsValue > 999999 )
{ {
Sys_Error("EncodeFloat: tell to xutaxkamay that he's " Sys_Error( "EncodeFloat: tell to xutaxkamay that he's "
"dumb\n"); "dumb\n" );
} }
pOut->WriteUBitLong(sixDigitsValue, 20); pOut->WriteUBitLong( sixDigitsValue, 20 );
} }
}; };
// Check for special flags like SPROP_COORD, SPROP_NOSCALE, and // Check for special flags like SPROP_COORD, SPROP_NOSCALE, and
// SPROP_NORMAL. // SPROP_NORMAL.
if (EncodeSpecialFloat(pProp, fVal, pOut)) if ( EncodeSpecialFloat( pProp, fVal, pOut ) )
{ {
return; return;
} }
if ((pProp->m_fHighValue == 0.0f && pProp->m_fLowValue == 0.0f) if ( ( pProp->m_fHighValue == 0.0f && pProp->m_fLowValue == 0.0f ) || pProp->m_fHighValue == pProp->m_fLowValue )
|| pProp->m_fHighValue == pProp->m_fLowValue) {
{ pOut->WriteBitFloat( fVal );
pOut->WriteBitFloat(fVal); return;
return; }
}
if (fVal > pProp->m_fHighValue) if ( fVal > pProp->m_fHighValue )
{ {
Sys_Error("EncodeFloat: %s has value %f which is too big " Sys_Error( "EncodeFloat: %s has value %f which is too big "
"(%f)", "(%f)",
pProp->GetName(), pProp->GetName(),
fVal, fVal,
pProp->m_fHighValue); pProp->m_fHighValue );
} }
if (fVal < pProp->m_fLowValue) if ( fVal < pProp->m_fLowValue )
{ {
Sys_Error("EncodeFloat: %s has value %f which is too low " Sys_Error( "EncodeFloat: %s has value %f which is too low "
"(%f)", "(%f)",
pProp->GetName(), pProp->GetName(),
fVal, fVal,
pProp->m_fLowValue); pProp->m_fLowValue );
} }
if (pProp->m_fLowValue > pProp->m_fHighValue) if ( pProp->m_fLowValue > pProp->m_fHighValue )
{ {
Sys_Error("EncodeFloat: %s(%f) low value %f is higher than " Sys_Error( "EncodeFloat: %s(%f) low value %f is higher than "
"%f", "%f",
pProp->GetName(), pProp->GetName(),
fVal, fVal,
pProp->m_fLowValue, pProp->m_fLowValue,
pProp->m_fHighValue); pProp->m_fHighValue );
} }
double dblVal = static_cast<double>(fVal); double dblVal = static_cast< double >( fVal );
// If low value bigger than zero, we need to substract to the
// lowest value possible, so we can recover it later.
if ((pProp->m_fLowValue > 0.0f && pProp->m_fHighValue > 0.0f)
|| (pProp->m_fHighValue < 0.0f && pProp->m_fLowValue < 0.0f))
{
dblVal -= pProp->m_fLowValue;
}
double dblRange = pProp->m_fHighValue - pProp->m_fLowValue; // If low value bigger than zero, we need to substract to the
// lowest value possible, so we can recover it later.
if ( ( pProp->m_fLowValue > 0.0f && pProp->m_fHighValue > 0.0f )
|| ( pProp->m_fHighValue < 0.0f && pProp->m_fLowValue < 0.0f ) )
{
dblVal -= pProp->m_fLowValue;
}
bool sign = false; double dblRange = pProp->m_fHighValue - pProp->m_fLowValue;
if (dblVal < 0.0) bool sign = false;
{
sign = true;
dblVal = -dblVal;
}
pOut->WriteOneBit(sign); if ( dblVal < 0.0 )
{
sign = true;
dblVal = -dblVal;
}
WriteNormalizedFloat(dblRange > 1.0 ? (dblVal / dblRange) : pOut->WriteOneBit( sign );
dblVal);
WriteNormalizedFloat( dblRange > 1.0 ? ( dblVal / dblRange ) : dblVal );
} }
// Look for special flags like SPROP_COORD, SPROP_NOSCALE, and SPROP_NORMAL and // Look for special flags like SPROP_COORD, SPROP_NOSCALE, and SPROP_NORMAL and
@ -203,73 +198,73 @@ static inline bool DecodeSpecialFloat( SendProp const *pProp, bf_read *pIn, floa
return false; return false;
} }
static inline float DecodeNormalizedFloat(const SendProp* pProp, bf_read* pIn) static inline float DecodeNormalizedFloat( const SendProp* pProp, bf_read* pIn )
{ {
bool sign = pIn->ReadOneBit(); bool sign = pIn->ReadOneBit();
double dblVal; double dblVal;
if (!pIn->ReadOneBit()) if ( !pIn->ReadOneBit() )
{ {
const auto fractionPart = pIn->ReadUBitLong(20); const auto fractionPart = pIn->ReadUBitLong( 20 );
char strNumber[8]; char strNumber[8];
V_memset(strNumber, 0, sizeof(strNumber)); V_memset( strNumber, 0, sizeof( strNumber ) );
V_sprintf_safe(strNumber, "%i", fractionPart); V_sprintf_safe( strNumber, "%i", fractionPart );
int countDigits = 0; int countDigits = 0;
while (strNumber[countDigits]) while ( strNumber[countDigits] )
{ {
countDigits++; countDigits++;
}
int digitLeft = 6 - countDigits;
if (digitLeft < 0)
{
Sys_Error("DecodeNormalizedFloat: digitLeft < 0\n");
} }
char strFraction[16]; int digitLeft = 6 - countDigits;
strFraction[0] = '0';
strFraction[1] = '.';
for (int i = 0; i < digitLeft; i++) if ( digitLeft < 0 )
{ {
strFraction[2 + i] = '0'; Sys_Error( "DecodeNormalizedFloat: digitLeft < 0\n" );
} }
for (int i = 0; i < countDigits; i++) char strFraction[16];
{ strFraction[0] = '0';
strFraction[2 + digitLeft + i] = strNumber[i]; strFraction[1] = '.';
}
dblVal = V_atof(strFraction); for ( int i = 0; i < digitLeft; i++ )
} {
else strFraction[2 + i] = '0';
{ }
dblVal = 1.0;
}
if (dblVal <= 0.0) for ( int i = 0; i < countDigits; i++ )
{ {
return 0.0f; strFraction[2 + digitLeft + i] = strNumber[i];
} }
double dblRange = pProp->m_fHighValue - pProp->m_fLowValue; dblVal = V_atof( strFraction );
}
else
{
dblVal = 1.0;
}
if (dblRange > 1.0) if ( dblVal <= 0.0 )
{ {
dblVal *= dblRange; return 0.0f;
} }
if ((pProp->m_fLowValue > 0.0f && pProp->m_fHighValue > 0.0f) double dblRange = pProp->m_fHighValue - pProp->m_fLowValue;
|| (pProp->m_fHighValue < 0.0f && pProp->m_fLowValue < 0.0f))
{
dblVal += pProp->m_fLowValue;
}
dblVal = sign ? -dblVal : dblVal; if ( dblRange > 1.0 )
return static_cast<float>(dblVal); {
dblVal *= dblRange;
}
if ( ( pProp->m_fLowValue > 0.0f && pProp->m_fHighValue > 0.0f )
|| ( pProp->m_fHighValue < 0.0f && pProp->m_fLowValue < 0.0f ) )
{
dblVal += pProp->m_fLowValue;
}
dblVal = sign ? -dblVal : dblVal;
return static_cast< float >( dblVal );
} }
static inline float DecodeFloat(const SendProp* pProp, bf_read* pIn) static inline float DecodeFloat(const SendProp* pProp, bf_read* pIn)

View file

@ -1018,6 +1018,8 @@ if active == 1 then we are 1) not playing back demos ( where our commands are ig
void CInput::ExtraMouseSample( float frametime, bool active ) void CInput::ExtraMouseSample( float frametime, bool active )
{ {
VPROF( "CInput::ExtraMouseSample" );
CUserCmd dummy; CUserCmd dummy;
CUserCmd *cmd = &dummy; CUserCmd *cmd = &dummy;
@ -1124,14 +1126,15 @@ void CInput::ExtraMouseSample( float frametime, bool active )
} }
void CInput::CreateMove ( int sequence_number, float input_sample_frametime, bool active ) void CInput::CreateMove ( int sequence_number, float input_sample_frametime, bool active )
{ {
VPROF( "CInput::CreateMove" );
CUserCmd *cmd = &m_pCommands[ sequence_number % MULTIPLAYER_BACKUP ]; CUserCmd *cmd = &m_pCommands[ sequence_number % MULTIPLAYER_BACKUP ];
CVerifiedUserCmd *pVerified = &m_pVerifiedCommands[ sequence_number % MULTIPLAYER_BACKUP ]; CVerifiedUserCmd *pVerified = &m_pVerifiedCommands[ sequence_number % MULTIPLAYER_BACKUP ];
cmd->Reset(); cmd->Reset();
cmd->command_number = sequence_number; cmd->command_number = sequence_number;
cmd->tick_count = gpGlobals->tickcount;
QAngle viewangles; QAngle viewangles;
engine->GetViewAngles( viewangles ); engine->GetViewAngles( viewangles );
@ -1301,45 +1304,45 @@ void CInput::CreateMove ( int sequence_number, float input_sample_frametime, boo
m_EntityGroundContact.RemoveAll(); m_EntityGroundContact.RemoveAll();
#endif #endif
for (int i = 0; i < MAX_EDICTS; i++) for ( int i = 0; i < MAX_EDICTS; i++ )
{ {
cmd->simulationdata[i].m_bEntityExists = false; cmd->simulationdata[i].m_bEntityExists = false;
} }
// Send interpolated simulation time for lag compensation // Send interpolated simulation time for lag compensation, let it also auto-vectorize this.
for (int i = 0; i <= ClientEntityList().GetHighestEntityIndex(); i++) for ( int i = 0; i < MAX_EDICTS; i++ )
{ {
auto pEntity = ClientEntityList().GetEnt(i); auto pEntity = ClientEntityList().GetEnt( i );
if (!pEntity) if ( !pEntity )
{ {
continue; continue;
} }
cmd->simulationdata[pEntity->index].m_flSimulationTime = pEntity->m_flInterpolatedSimulationTime; cmd->simulationdata[pEntity->index].m_flSimulationTime = pEntity->m_flInterpolatedSimulationTime;
cmd->simulationdata[pEntity->index].m_flAnimTime = pEntity->m_flSimulationTime; cmd->simulationdata[pEntity->index].m_flAnimTime = pEntity->m_flSimulationTime;
cmd->simulationdata[pEntity->index].m_bEntityExists = true; cmd->simulationdata[pEntity->index].m_bEntityExists = true;
} }
#ifdef CSTRIKE_DLL #ifdef CSTRIKE_DLL
static ConVarRef debug_screenshot_bullet_position("debug_screenshot_bullet_position"); static ConVarRef debug_screenshot_bullet_position( "debug_screenshot_bullet_position" );
static ConVarRef cl_showfirebullethitboxes("cl_showfirebullethitboxes"); static ConVarRef cl_showfirebullethitboxes( "cl_showfirebullethitboxes" );
static ConVarRef cl_showimpacts("cl_showimpacts"); static ConVarRef cl_showimpacts( "cl_showimpacts" );
cmd->debug_hitboxes = CUserCmd::DEBUG_HITBOXES_OFF; cmd->debug_hitboxes = CUserCmd::DEBUG_HITBOXES_OFF;
if (cl_showfirebullethitboxes.GetBool()) if ( cl_showfirebullethitboxes.GetBool() )
{ {
cmd->debug_hitboxes |= CUserCmd::DEBUG_HITBOXES_ON_FIRE; cmd->debug_hitboxes |= CUserCmd::DEBUG_HITBOXES_ON_FIRE;
} }
if (cl_showimpacts.GetBool() || debug_screenshot_bullet_position.GetBool()) if ( cl_showimpacts.GetBool() || debug_screenshot_bullet_position.GetBool() )
{ {
cmd->debug_hitboxes |= CUserCmd::DEBUG_HITBOXES_ON_HIT; cmd->debug_hitboxes |= CUserCmd::DEBUG_HITBOXES_ON_HIT;
} }
#endif #endif
cmd->interpolated_amount = gpGlobals->next_interpolation_amount; cmd->interpolated_amount = gpGlobals->next_interpolation_amount;
pVerified->m_cmd = *cmd; pVerified->m_cmd = *cmd;
pVerified->m_crc = cmd->GetChecksum(); pVerified->m_crc = cmd->GetChecksum();

View file

@ -863,10 +863,6 @@ bool UserCmdChanged( const CUserCmd& lhs, const CUserCmd& rhs )
return true; return true;
if ( lhs.weaponsubtype != rhs.weaponsubtype ) if ( lhs.weaponsubtype != rhs.weaponsubtype )
return true; return true;
if ( lhs.mousedx != rhs.mousedx )
return true;
if ( lhs.mousedy != rhs.mousedy )
return true;
return false; return false;
} }

View file

@ -606,14 +606,6 @@ void CCSPlayer::PlayerRunCommand( CUserCmd *ucmd, IMoveHelper *moveHelper )
if ( !sv_runcmds.GetInt() ) if ( !sv_runcmds.GetInt() )
return; return;
// don't run commands in the future
if (!IsEngineThreaded()
&& (ucmd->tick_count > (gpGlobals->tickcount + sv_max_usercmd_future_ticks.GetInt())) && !IsBot())
{
DevMsg( "Client cmd out of sync (delta: %i, client: %i != server: %i).\n", ucmd->tick_count - gpGlobals->tickcount, ucmd->tick_count, gpGlobals->tickcount);
return;
}
// If they use a negative bot_mimic value, then don't process their usercmds, but have // If they use a negative bot_mimic value, then don't process their usercmds, but have
// bots process them instead (so they can stay still and have the bot move around). // bots process them instead (so they can stay still and have the bot move around).
CUserCmd tempCmd; CUserCmd tempCmd;
@ -696,7 +688,6 @@ void CCSPlayer::RunPlayerMove( const QAngle& viewangles, float forwardmove, floa
{ {
CUserCmd lastCmd = *GetLastUserCommand(); CUserCmd lastCmd = *GetLastUserCommand();
lastCmd.command_number = cmd.command_number; lastCmd.command_number = cmd.command_number;
lastCmd.tick_count = cmd.tick_count;
SetLastUserCommand( lastCmd ); SetLastUserCommand( lastCmd );
} }
else else

View file

@ -3274,7 +3274,6 @@ void CBasePlayer::PhysicsSimulate( void )
// run the last known cmd for each dropped cmd we don't have a backup for // run the last known cmd for each dropped cmd we don't have a backup for
while ( droppedcmds > numbackup ) while ( droppedcmds > numbackup )
{ {
m_LastCmd.tick_count++;
vecAvailCommands.AddToTail( m_LastCmd ); vecAvailCommands.AddToTail( m_LastCmd );
droppedcmds--; droppedcmds--;
} }
@ -3527,13 +3526,7 @@ bool CBasePlayer::IsUserCmdDataValid( CUserCmd *pCmd )
if ( IsBot() || IsFakeClient() ) if ( IsBot() || IsFakeClient() )
return true; return true;
// Maximum difference between client's and server's tick_count bool bValid = // Prevent clients from sending invalid view angles to try to get leaf server code to crash
const int nCmdMaxTickDelta = ( 1.f / gpGlobals->interval_per_tick ) * 2.5f;
const int nMinDelta = Max( 0, gpGlobals->tickcount - nCmdMaxTickDelta );
const int nMaxDelta = gpGlobals->tickcount + nCmdMaxTickDelta;
bool bValid = ( pCmd->tick_count >= nMinDelta && pCmd->tick_count < nMaxDelta ) &&
// Prevent clients from sending invalid view angles to try to get leaf server code to crash
( pCmd->viewangles.IsValid() && IsEntityQAngleReasonable( pCmd->viewangles ) ) && ( pCmd->viewangles.IsValid() && IsEntityQAngleReasonable( pCmd->viewangles ) ) &&
// Movement ranges // Movement ranges
( IsFinite( pCmd->forwardmove ) && IsEntityCoordinateReasonable( pCmd->forwardmove ) ) && ( IsFinite( pCmd->forwardmove ) && IsEntityCoordinateReasonable( pCmd->forwardmove ) ) &&
@ -3547,8 +3540,7 @@ bool CBasePlayer::IsUserCmdDataValid( CUserCmd *pCmd )
if ( nWarningLevel == 2 ) if ( nWarningLevel == 2 )
{ {
DevMsg( " tick_count: %i\n viewangles: %5.2f %5.2f %5.2f \n forward: %5.2f \n side: \t%5.2f \n up: \t%5.2f\n", DevMsg( "viewangles: %5.2f %5.2f %5.2f \n forward: %5.2f \n side: \t%5.2f \n up: \t%5.2f\n",
pCmd->tick_count,
pCmd->viewangles.x, pCmd->viewangles.x,
pCmd->viewangles.y, pCmd->viewangles.y,
pCmd->viewangles.x, pCmd->viewangles.x,
@ -9230,13 +9222,8 @@ void CPlayerInfo::RunPlayerMove( CBotCmd *ucmd )
cmd.buttons = ucmd->buttons; cmd.buttons = ucmd->buttons;
cmd.command_number = ucmd->command_number; cmd.command_number = ucmd->command_number;
cmd.forwardmove = ucmd->forwardmove; cmd.forwardmove = ucmd->forwardmove;
cmd.hasbeenpredicted = ucmd->hasbeenpredicted;
cmd.impulse = ucmd->impulse; cmd.impulse = ucmd->impulse;
cmd.mousedx = ucmd->mousedx;
cmd.mousedy = ucmd->mousedy;
cmd.random_seed = ucmd->random_seed;
cmd.sidemove = ucmd->sidemove; cmd.sidemove = ucmd->sidemove;
cmd.tick_count = ucmd->tick_count;
cmd.upmove = ucmd->upmove; cmd.upmove = ucmd->upmove;
cmd.viewangles = ucmd->viewangles; cmd.viewangles = ucmd->viewangles;
cmd.weaponselect = ucmd->weaponselect; cmd.weaponselect = ucmd->weaponselect;
@ -9273,13 +9260,8 @@ void CPlayerInfo::SetLastUserCommand( const CBotCmd &ucmd )
cmd.buttons = ucmd.buttons; cmd.buttons = ucmd.buttons;
cmd.command_number = ucmd.command_number; cmd.command_number = ucmd.command_number;
cmd.forwardmove = ucmd.forwardmove; cmd.forwardmove = ucmd.forwardmove;
cmd.hasbeenpredicted = ucmd.hasbeenpredicted;
cmd.impulse = ucmd.impulse; cmd.impulse = ucmd.impulse;
cmd.mousedx = ucmd.mousedx;
cmd.mousedy = ucmd.mousedy;
cmd.random_seed = ucmd.random_seed;
cmd.sidemove = ucmd.sidemove; cmd.sidemove = ucmd.sidemove;
cmd.tick_count = ucmd.tick_count;
cmd.upmove = ucmd.upmove; cmd.upmove = ucmd.upmove;
cmd.viewangles = ucmd.viewangles; cmd.viewangles = ucmd.viewangles;
cmd.weaponselect = ucmd.weaponselect; cmd.weaponselect = ucmd.weaponselect;
@ -9299,13 +9281,9 @@ CBotCmd CPlayerInfo::GetLastUserCommand()
cmd.buttons = ucmd->buttons; cmd.buttons = ucmd->buttons;
cmd.command_number = ucmd->command_number; cmd.command_number = ucmd->command_number;
cmd.forwardmove = ucmd->forwardmove; cmd.forwardmove = ucmd->forwardmove;
cmd.hasbeenpredicted = ucmd->hasbeenpredicted;
cmd.impulse = ucmd->impulse; cmd.impulse = ucmd->impulse;
cmd.mousedx = ucmd->mousedx;
cmd.mousedy = ucmd->mousedy;
cmd.random_seed = ucmd->random_seed; cmd.random_seed = ucmd->random_seed;
cmd.sidemove = ucmd->sidemove; cmd.sidemove = ucmd->sidemove;
cmd.tick_count = ucmd->tick_count;
cmd.upmove = ucmd->upmove; cmd.upmove = ucmd->upmove;
cmd.viewangles = ucmd->viewangles; cmd.viewangles = ucmd->viewangles;
cmd.weaponselect = ucmd->weaponselect; cmd.weaponselect = ucmd->weaponselect;

View file

@ -450,6 +450,8 @@ void CCSPlayer::FireBullet(
float xSpread, float ySpread float xSpread, float ySpread
) )
{ {
VPROF( "CCSPlayer::FireBullet" );
float fCurrentDamage = iDamage; // damage of the bullet at it's current trajectory float fCurrentDamage = iDamage; // damage of the bullet at it's current trajectory
float flCurrentDistance = 0.0; //distance that the bullet has traveled so far float flCurrentDistance = 0.0; //distance that the bullet has traveled so far

View file

@ -45,17 +45,6 @@ void WriteUsercmd( bf_write *buf, const CUserCmd *to, const CUserCmd *from )
buf->WriteOneBit( 0 ); buf->WriteOneBit( 0 );
} }
if ( to->tick_count != ( from->tick_count + 1 ) )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->tick_count, 32 );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->viewangles[ 0 ] != from->viewangles[ 0 ] ) if ( to->viewangles[ 0 ] != from->viewangles[ 0 ] )
{ {
buf->WriteOneBit( 1 ); buf->WriteOneBit( 1 );
@ -157,82 +146,61 @@ void WriteUsercmd( bf_write *buf, const CUserCmd *to, const CUserCmd *from )
buf->WriteOneBit( 0 ); buf->WriteOneBit( 0 );
} }
// TODO: Can probably get away with fewer bits.
if ( to->mousedx != from->mousedx )
{
buf->WriteOneBit( 1 );
buf->WriteShort( to->mousedx );
}
else
{
buf->WriteOneBit( 0 );
}
if ( to->mousedy != from->mousedy )
{
buf->WriteOneBit( 1 );
buf->WriteShort( to->mousedy );
}
else
{
buf->WriteOneBit( 0 );
}
#ifdef CLIENT_DLL #ifdef CLIENT_DLL
int highestEntityIndex = 0; int highestEntityIndex = 0;
if (cl_entitylist) if ( cl_entitylist )
highestEntityIndex = cl_entitylist->GetHighestEntityIndex(); {
highestEntityIndex = cl_entitylist->GetHighestEntityIndex();
}
#else #else
static constexpr auto highestEntityIndex = MAX_EDICTS-1; static constexpr auto highestEntityIndex = MAX_EDICTS - 1;
#endif #endif
// Write entity count // Write entity count
buf->WriteUBitLong(highestEntityIndex, 11); buf->WriteUBitLong( highestEntityIndex, 11 );
// Write finally simulation data with entity index // Write finally simulation data with entity index
for (unsigned int i = 0; i <= highestEntityIndex; i++) for ( unsigned int i = 0; i <= highestEntityIndex; i++ )
{
if (from->simulationdata[i].m_flSimulationTime != to->simulationdata[i].m_flSimulationTime)
{
buf->WriteOneBit(1);
buf->WriteBitFloat(to->simulationdata[i].m_flSimulationTime);
}
else
{
buf->WriteOneBit(0);
}
if (from->simulationdata[i].m_flAnimTime != to->simulationdata[i].m_flAnimTime)
{
buf->WriteOneBit(1);
buf->WriteBitFloat(to->simulationdata[i].m_flAnimTime);
}
else
{
buf->WriteOneBit(0);
}
}
if (to->debug_hitboxes != from->debug_hitboxes)
{
buf->WriteOneBit(1);
buf->WriteUBitLong(to->debug_hitboxes, 2);
}
else
{
buf->WriteOneBit(0);
}
if (from->interpolated_amount != to->interpolated_amount)
{ {
buf->WriteOneBit(1); if ( from->simulationdata[i].m_flSimulationTime != to->simulationdata[i].m_flSimulationTime )
buf->WriteBitFloat(to->interpolated_amount); {
buf->WriteOneBit( 1 );
buf->WriteBitFloat( to->simulationdata[i].m_flSimulationTime );
}
else
{
buf->WriteOneBit( 0 );
}
if ( from->simulationdata[i].m_flAnimTime != to->simulationdata[i].m_flAnimTime )
{
buf->WriteOneBit( 1 );
buf->WriteBitFloat( to->simulationdata[i].m_flAnimTime );
}
else
{
buf->WriteOneBit( 0 );
}
}
if ( to->debug_hitboxes != from->debug_hitboxes )
{
buf->WriteOneBit( 1 );
buf->WriteUBitLong( to->debug_hitboxes, 2 );
} }
else else
{ {
buf->WriteOneBit(0); buf->WriteOneBit( 0 );
}
if ( from->interpolated_amount != to->interpolated_amount )
{
buf->WriteOneBit( 1 );
buf->WriteBitFloat( to->interpolated_amount );
}
else
{
buf->WriteOneBit( 0 );
} }
#if defined( HL2_CLIENT_DLL ) #if defined( HL2_CLIENT_DLL )
@ -277,16 +245,6 @@ void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from )
move->command_number = from->command_number + 1; move->command_number = from->command_number + 1;
} }
if ( buf->ReadOneBit() )
{
move->tick_count = buf->ReadUBitLong( 32 );
}
else
{
// Assume steady increment
move->tick_count = from->tick_count + 1;
}
// Read direction // Read direction
if ( buf->ReadOneBit() ) if ( buf->ReadOneBit() )
{ {
@ -332,7 +290,6 @@ void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from )
move->impulse = buf->ReadUBitLong( 8 ); move->impulse = buf->ReadUBitLong( 8 );
} }
if ( buf->ReadOneBit() ) if ( buf->ReadOneBit() )
{ {
move->weaponselect = buf->ReadUBitLong( MAX_EDICT_BITS ); move->weaponselect = buf->ReadUBitLong( MAX_EDICT_BITS );
@ -344,17 +301,9 @@ void ReadUsercmd( bf_read *buf, CUserCmd *move, CUserCmd *from )
move->random_seed = MD5_PseudoRandom( move->command_number ) & 0x7fffffff; move->random_seed = MD5_PseudoRandom( move->command_number ) & 0x7fffffff;
if ( buf->ReadOneBit() ) auto highestEntityIndex = buf->ReadUBitLong( 11 );
{
move->mousedx = buf->ReadShort();
}
if ( buf->ReadOneBit() ) highestEntityIndex = MIN(MAX_EDICTS - 1, highestEntityIndex);
{
move->mousedy = buf->ReadShort();
}
const auto highestEntityIndex = buf->ReadUBitLong(11);
for (unsigned int i = 0; i <= highestEntityIndex; i++) for (unsigned int i = 0; i <= highestEntityIndex; i++)
{ {

View file

@ -46,17 +46,17 @@ struct LayerRecord
LayerRecord() LayerRecord()
{ {
m_sequence = 0; m_sequence = 0;
m_cycle = 0; m_cycle = 0;
m_weight = 0; m_weight = 0;
m_order = 0; m_order = 0;
} }
}; };
struct SimulationData struct SimulationData
{ {
// TODO_ENHANCED: // TODO_ENHANCED:
// For now we send the last received update for animations. // For now we send the last received update for animations.
// anim time is unreliable on low fps. // anim time is unreliable on low fps.
float m_flSimulationTime; float m_flSimulationTime;
float m_flAnimTime; float m_flAnimTime;
bool m_bEntityExists; bool m_bEntityExists;
@ -83,7 +83,6 @@ public:
void Reset() void Reset()
{ {
command_number = 0; command_number = 0;
tick_count = 0;
viewangles.Init(); viewangles.Init();
forwardmove = 0.0f; forwardmove = 0.0f;
sidemove = 0.0f; sidemove = 0.0f;
@ -116,7 +115,6 @@ public:
return *this; return *this;
command_number = src.command_number; command_number = src.command_number;
tick_count = src.tick_count;
viewangles = src.viewangles; viewangles = src.viewangles;
forwardmove = src.forwardmove; forwardmove = src.forwardmove;
sidemove = src.sidemove; sidemove = src.sidemove;
@ -155,7 +153,6 @@ public:
CRC32_Init( &crc ); CRC32_Init( &crc );
CRC32_ProcessBuffer( &crc, &command_number, sizeof( command_number ) ); CRC32_ProcessBuffer( &crc, &command_number, sizeof( command_number ) );
CRC32_ProcessBuffer( &crc, &tick_count, sizeof( tick_count ) );
CRC32_ProcessBuffer( &crc, &viewangles, sizeof( viewangles ) ); CRC32_ProcessBuffer( &crc, &viewangles, sizeof( viewangles ) );
CRC32_ProcessBuffer( &crc, &forwardmove, sizeof( forwardmove ) ); CRC32_ProcessBuffer( &crc, &forwardmove, sizeof( forwardmove ) );
CRC32_ProcessBuffer( &crc, &sidemove, sizeof( sidemove ) ); CRC32_ProcessBuffer( &crc, &sidemove, sizeof( sidemove ) );
@ -165,11 +162,9 @@ public:
CRC32_ProcessBuffer( &crc, &weaponselect, sizeof( weaponselect ) ); CRC32_ProcessBuffer( &crc, &weaponselect, sizeof( weaponselect ) );
CRC32_ProcessBuffer( &crc, &weaponsubtype, sizeof( weaponsubtype ) ); CRC32_ProcessBuffer( &crc, &weaponsubtype, sizeof( weaponsubtype ) );
CRC32_ProcessBuffer( &crc, &random_seed, sizeof( random_seed ) ); CRC32_ProcessBuffer( &crc, &random_seed, sizeof( random_seed ) );
CRC32_ProcessBuffer( &crc, &mousedx, sizeof( mousedx ) ); CRC32_ProcessBuffer( &crc, simulationdata, sizeof( simulationdata ) );
CRC32_ProcessBuffer(&crc, &mousedy, sizeof(mousedy)); CRC32_ProcessBuffer( &crc, &debug_hitboxes, sizeof( debug_hitboxes ) );
CRC32_ProcessBuffer(&crc, simulationdata, sizeof(simulationdata)); CRC32_ProcessBuffer( &crc, &interpolated_amount, sizeof( interpolated_amount ) );
CRC32_ProcessBuffer(&crc, &debug_hitboxes, sizeof(debug_hitboxes));
CRC32_ProcessBuffer(&crc, &interpolated_amount, sizeof(interpolated_amount));
CRC32_Final( &crc ); CRC32_Final( &crc );
return crc; return crc;
@ -178,22 +173,12 @@ public:
// Allow command, but negate gameplay-affecting values // Allow command, but negate gameplay-affecting values
void MakeInert( void ) void MakeInert( void )
{ {
viewangles = vec3_angle; Reset();
forwardmove = 0.f;
sidemove = 0.f;
upmove = 0.f;
buttons = 0;
impulse = 0;
debug_hitboxes = DEBUG_HITBOXES_OFF;
interpolated_amount = 0.0f;
} }
// For matching server and client commands for debugging // For matching server and client commands for debugging
int command_number; int command_number;
// the tick the client created this command
int tick_count;
// Player instantaneous view angles. // Player instantaneous view angles.
QAngle viewangles; QAngle viewangles;
// Intended velocities // Intended velocities
@ -219,21 +204,21 @@ public:
// Client only, tracks whether we've predicted this command at least once // Client only, tracks whether we've predicted this command at least once
bool hasbeenpredicted; bool hasbeenpredicted;
// TODO_ENHANCED: Lag compensate also other entities when needed. // TODO_ENHANCED: Lag compensate also other entities when needed.
// Send simulation times for each players for lag compensation. // Send simulation times for each players for lag compensation.
SimulationData simulationdata[MAX_EDICTS]; SimulationData simulationdata[MAX_EDICTS];
enum debug_hitboxes_t : uint8 enum debug_hitboxes_t : uint8
{ {
DEBUG_HITBOXES_OFF, DEBUG_HITBOXES_OFF,
DEBUG_HITBOXES_ON_FIRE = 1 << 0, DEBUG_HITBOXES_ON_FIRE = 1 << 0,
DEBUG_HITBOXES_ON_HIT = 1 << 1 DEBUG_HITBOXES_ON_HIT = 1 << 1
}; };
uint8 debug_hitboxes; uint8 debug_hitboxes;
// TODO_ENHANCED: check README_ENHANCED in host.cpp! // TODO_ENHANCED: check README_ENHANCED in host.cpp!
float interpolated_amount; float interpolated_amount;
// Back channel to communicate IK state // Back channel to communicate IK state
#if defined( HL2_DLL ) || defined( HL2_CLIENT_DLL ) #if defined( HL2_DLL ) || defined( HL2_CLIENT_DLL )

View file

@ -25,7 +25,6 @@ public:
void Reset() void Reset()
{ {
command_number = 0; command_number = 0;
tick_count = 0;
viewangles.Init(); viewangles.Init();
forwardmove = 0.0f; forwardmove = 0.0f;
sidemove = 0.0f; sidemove = 0.0f;
@ -35,10 +34,6 @@ public:
weaponselect = 0; weaponselect = 0;
weaponsubtype = 0; weaponsubtype = 0;
random_seed = 0; random_seed = 0;
mousedx = 0;
mousedy = 0;
hasbeenpredicted = false;
} }
CBotCmd& operator =( const CBotCmd& src ) CBotCmd& operator =( const CBotCmd& src )
@ -47,7 +42,6 @@ public:
return *this; return *this;
command_number = src.command_number; command_number = src.command_number;
tick_count = src.tick_count;
viewangles = src.viewangles; viewangles = src.viewangles;
forwardmove = src.forwardmove; forwardmove = src.forwardmove;
sidemove = src.sidemove; sidemove = src.sidemove;
@ -57,18 +51,12 @@ public:
weaponselect = src.weaponselect; weaponselect = src.weaponselect;
weaponsubtype = src.weaponsubtype; weaponsubtype = src.weaponsubtype;
random_seed = src.random_seed; random_seed = src.random_seed;
mousedx = src.mousedx;
mousedy = src.mousedy;
hasbeenpredicted = src.hasbeenpredicted;
return *this; return *this;
} }
// For matching server and client commands for debugging // For matching server and client commands for debugging
int command_number; int command_number;
// the tick the client created this command
int tick_count;
// Player instantaneous view angles. // Player instantaneous view angles.
QAngle viewangles; QAngle viewangles;
// Intended velocities // Intended velocities
@ -87,12 +75,6 @@ public:
int weaponsubtype; int weaponsubtype;
int random_seed; // For shared random functions int random_seed; // For shared random functions
short mousedx; // mouse accum in x from create move
short mousedy; // mouse accum in y from create move
// Client only, tracks whether we've predicted this command at least once
bool hasbeenpredicted;
}; };