Skip to content

Commit

Permalink
Convert Delayed Controls to std::chrono
Browse files Browse the repository at this point in the history
This will ensure the delay will be framerate agnostic, meaning no matter if you have high or low FPS, the delay will always be the same.

Additionally the delay is now between 150ms and 250ms for each input, making it feel a bit more random
  • Loading branch information
Lordmau5 committed Sep 8, 2024
1 parent 1dec7f4 commit 67f5b1d
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions src/gtasa/effects/custom/unsorted/DelayedControlsEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class DelayedControls : public EffectBase
{
struct InputData
{
unsigned int timeInMs;
CControllerState state;
CMouseControllerState mouseState;
CKeyboardState keyboardState;
std::chrono::steady_clock::time_point chronoTime;
CControllerState state;
CMouseControllerState mouseState;
CKeyboardState keyboardState;
};

static inline std::deque<InputData> bufferedInputData = {};

// For whatever reason we need to do half of the buffer we want in code
int bufferInMs = 500 / 2;
// 150ms is the minimum for delay
std::chrono::milliseconds chronoBuffer = std::chrono::milliseconds{100};

public:
void
Expand All @@ -40,16 +40,22 @@ class DelayedControls : public EffectBase
CPad *pad = player->GetPadFromPlayer ();
if (!pad) return;

unsigned int timeInMs = CTimer::m_snTimeInMilliseconds;
unsigned int adjustedBuffer = bufferInMs * CTimer::ms_fTimeScale;
std::chrono::steady_clock::time_point chronoTime
= std::chrono::steady_clock::now ();

InputData frameData = {.timeInMs = timeInMs + adjustedBuffer,
.state = pad->NewState,
.mouseState = pad->NewMouseControllerState,
.keyboardState = pad->NewKeyState};
// Add extra random delay between 0 and 100ms, resulting in a random
// input delay between 150 and 250ms
auto addedRandomDelay
= std::chrono::milliseconds{inst->Random (0, 100)};

InputData frameData
= {.chronoTime = chronoTime + chronoBuffer + addedRandomDelay,
.state = pad->NewState,
.mouseState = pad->NewMouseControllerState,
.keyboardState = pad->NewKeyState};

bufferedInputData.push_back (frameData);
if (timeInMs > bufferedInputData[0].timeInMs)
if (chronoTime > bufferedInputData[0].chronoTime)
{
InputData data = bufferedInputData.front ();
pad->NewState = data.state;
Expand Down Expand Up @@ -79,9 +85,10 @@ class DelayedControls : public EffectBase
{
InputData data = bufferedInputData.front ();

unsigned int timeInMs = CTimer::m_snTimeInMilliseconds;
std::chrono::steady_clock::time_point chronoTime
= std::chrono::steady_clock::now ();

if (timeInMs > data.timeInMs)
if (chronoTime > data.chronoTime)
{
CTheScripts::ScriptParams[0].iParam = data.state.LeftStickX;
CTheScripts::ScriptParams[1].iParam = data.state.LeftStickY;
Expand Down

0 comments on commit 67f5b1d

Please sign in to comment.