Skip to content

Commit

Permalink
[fix] remove redundant check in usercmd
Browse files Browse the repository at this point in the history
- bitwise or will not change the value if the bit is already setted so the check is unnecessary
  • Loading branch information
maecry committed Jul 1, 2024
1 parent 732a079 commit 75d66f7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
20 changes: 10 additions & 10 deletions cstrike/features/misc/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void F::MISC::MOVEMENT::BunnyHop(CUserCmd* pCmd, CBaseUserCmdPB* pUserCmd, C_CSP
// im lazy so yea :D
if (pLocalPawn->GetFlags() & FL_ONGROUND)
{
pUserCmd->pInButtonState->CheckAndSetBits(EButtonStatePBBits::BUTTON_STATE_PB_BITS_BUTTONSTATE1);
pUserCmd->pInButtonState->SetBits(EButtonStatePBBits::BUTTON_STATE_PB_BITS_BUTTONSTATE1);
pUserCmd->pInButtonState->nValue &= ~IN_JUMP;
}
}
Expand All @@ -81,7 +81,7 @@ void F::MISC::MOVEMENT::AutoStrafe(CBaseUserCmdPB* pUserCmd, C_CSPlayerPawn* pLo
if (!C_GET(bool, Vars.bAutoStrafe) || pLocalPawn->GetFlags() & FL_ONGROUND)
return;

pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_LEFTMOVE);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_LEFTMOVE);
pUserCmd->flSideMove = pUserCmd->nMousedX > 0 ? -1.0f : 1.0f; // a bit yanky, but works
}

Expand All @@ -93,7 +93,7 @@ void F::MISC::MOVEMENT::ValidateUserCommand(CUserCmd* pCmd, CBaseUserCmdPB* pUse
// clamp angle to avoid untrusted angle
if (C_GET(bool, Vars.bAntiUntrusted))
{
pInputEntry->CheckAndSetBits(EInputHistoryBits::INPUT_HISTORY_BITS_VIEWANGLES);
pInputEntry->SetBits(EInputHistoryBits::INPUT_HISTORY_BITS_VIEWANGLES);
if (pInputEntry->pViewAngles->angValue.IsValid())
{
pInputEntry->pViewAngles->angValue.Clamp();
Expand All @@ -110,7 +110,7 @@ void F::MISC::MOVEMENT::ValidateUserCommand(CUserCmd* pCmd, CBaseUserCmdPB* pUse

// correct movement buttons while player move have different to buttons values
// clear all of the move buttons states
pUserCmd->pInButtonState->CheckAndSetBits(EButtonStatePBBits::BUTTON_STATE_PB_BITS_BUTTONSTATE1);
pUserCmd->pInButtonState->SetBits(EButtonStatePBBits::BUTTON_STATE_PB_BITS_BUTTONSTATE1);
pUserCmd->pInButtonState->nValue &= (~IN_FORWARD | ~IN_BACK | ~IN_LEFT | ~IN_RIGHT);

// re-store buttons by active forward/side moves
Expand All @@ -123,7 +123,7 @@ void F::MISC::MOVEMENT::ValidateUserCommand(CUserCmd* pCmd, CBaseUserCmdPB* pUse
pUserCmd->pInButtonState->nValue |= IN_RIGHT;
else if (pUserCmd->flSideMove < 0.0f)
pUserCmd->pInButtonState->nValue |= IN_LEFT;

if (!pInputEntry->pViewAngles->angValue.IsZero())
{
const float flDeltaX = std::remainderf(pInputEntry->pViewAngles->angValue.x - angCorrectionView.x, 360.f);
Expand All @@ -136,10 +136,10 @@ void F::MISC::MOVEMENT::ValidateUserCommand(CUserCmd* pCmd, CBaseUserCmdPB* pUse
if (flSensitivity == 0.0f)
flSensitivity = 1.0f;

pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_MOUSEDX);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_MOUSEDX);
pUserCmd->nMousedX = static_cast<short>(flDeltaX / (flSensitivity * flPitch));

pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_MOUSEDY);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_MOUSEDY);
pUserCmd->nMousedY = static_cast<short>(-flDeltaY / (flSensitivity * flYaw));
}
}
Expand Down Expand Up @@ -176,12 +176,12 @@ void F::MISC::MOVEMENT::MovementCorrection(CBaseUserCmdPB* pUserCmd, CCSGOInputH
const float flRollUp = vecUp.z * pUserCmd->flUpMove;

// solve corrected movement speed
pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_FORWARDMOVE);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_FORWARDMOVE);
pUserCmd->flForwardMove = vecOldForward.x * flPitchSide + vecOldForward.y * flYawSide + vecOldForward.x * flPitchForward + vecOldForward.y * flYawForward + vecOldForward.z * flRollUp;

pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_LEFTMOVE);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_LEFTMOVE);
pUserCmd->flSideMove = vecOldRight.x * flPitchSide + vecOldRight.y * flYawSide + vecOldRight.x * flPitchForward + vecOldRight.y * flYawForward + vecOldRight.z * flRollUp;

pUserCmd->CheckAndSetBits(EBaseCmdBits::BASE_BITS_UPMOVE);
pUserCmd->SetBits(EBaseCmdBits::BASE_BITS_UPMOVE);
pUserCmd->flUpMove = vecOldUp.x * flYawSide + vecOldUp.y * flPitchSide + vecOldUp.x * flYawForward + vecOldUp.y * flPitchForward + vecOldUp.z * flRollUp;
}
19 changes: 14 additions & 5 deletions cstrike/sdk/datatypes/usercmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ class CBasePB
std::uint32_t nHasBits; // 0x8
std::uint64_t nCachedBits; // 0xC

// @note: this function is used to check if the bits are set and set them if they are not
void CheckAndSetBits(std::uint64_t nBits)
void SetBits(std::uint64_t nBits)
{
if (!(nCachedBits & nBits))
nCachedBits |= nBits;
// @note: you don't need to check if the bits are already set as bitwise OR will not change the value if the bit is already set
nCachedBits |= nBits;
}
};

Expand All @@ -142,13 +141,15 @@ class CMsgQAngle : public CBasePB
public:
QAngle_t angValue; // 0x18
};

static_assert(sizeof(CMsgQAngle) == 0x28);

class CMsgVector : public CBasePB
{
public:
Vector4D_t vecValue; // 0x18
};

static_assert(sizeof(CMsgVector) == 0x28);

class CCSGOInterpolationInfoPB : public CBasePB
Expand All @@ -158,6 +159,7 @@ class CCSGOInterpolationInfoPB : public CBasePB
int nSrcTick; // 0x1C
int nDstTick; // 0x20
};

static_assert(sizeof(CCSGOInterpolationInfoPB) == 0x28);

class CCSGOInputHistoryEntryPB : public CBasePB
Expand All @@ -179,6 +181,7 @@ class CCSGOInputHistoryEntryPB : public CBasePB
int nFrameNumber; // 0x70
int nTargetEntIndex; // 0x74
};

static_assert(sizeof(CCSGOInputHistoryEntryPB) == 0x78);

struct CInButtonStatePB : CBasePB
Expand All @@ -187,6 +190,7 @@ struct CInButtonStatePB : CBasePB
std::uint64_t nValueChanged;
std::uint64_t nValueScroll;
};

static_assert(sizeof(CInButtonStatePB) == 0x30);

struct CSubtickMoveStep : CBasePB
Expand All @@ -198,6 +202,7 @@ struct CSubtickMoveStep : CBasePB
float flAnalogForwardDelta;
float flAnalogLeftDelta;
};

static_assert(sizeof(CSubtickMoveStep) == 0x30);

class CBaseUserCmdPB : public CBasePB
Expand Down Expand Up @@ -226,6 +231,7 @@ class CBaseUserCmdPB : public CBasePB
return MEM::CallVFunc<int, 7U>(this);
}
};

static_assert(sizeof(CBaseUserCmdPB) == 0x80);

class CCSGOUserCmdPB
Expand All @@ -247,6 +253,7 @@ class CCSGOUserCmdPB
nHasBits |= nBits;
}
};

static_assert(sizeof(CCSGOUserCmdPB) == 0x40);

struct CInButtonState
Expand All @@ -257,6 +264,7 @@ struct CInButtonState
std::uint64_t nValueChanged; // 0x10
std::uint64_t nValueScroll; // 0x18
};

static_assert(sizeof(CInButtonState) == 0x20);

class CUserCmd
Expand Down Expand Up @@ -284,8 +292,9 @@ class CUserCmd
continue;

pInputEntry->pViewAngles->angValue = angView;
pInputEntry->CheckAndSetBits(EInputHistoryBits::INPUT_HISTORY_BITS_VIEWANGLES);
pInputEntry->SetBits(EInputHistoryBits::INPUT_HISTORY_BITS_VIEWANGLES);
}
}
};

static_assert(sizeof(CUserCmd) == 0x88);

0 comments on commit 75d66f7

Please sign in to comment.