From 197f01a70b41a87d2e6957f3386150d0c737bed2 Mon Sep 17 00:00:00 2001 From: E66666666 Date: Sun, 20 Jun 2021 14:36:09 +0200 Subject: [PATCH] Improve acceleration timer accuracy --- Gears/Util/Timer.cpp | 37 +++++++++++++++++++++++++++++++++++++ Gears/Util/Timer.h | 15 +++++++++++++++ Gears/Util/ValueTimer.h | 6 +++--- Gears/script.cpp | 6 +++--- 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Gears/Util/Timer.cpp b/Gears/Util/Timer.cpp index 0c2ec6ad..810b6378 100644 --- a/Gears/Util/Timer.cpp +++ b/Gears/Util/Timer.cpp @@ -1,4 +1,9 @@ #include "Timer.h" + +#ifndef NO_NATIVES +#include +#endif + #include inline auto now() { @@ -32,3 +37,35 @@ int64_t Timer::Elapsed() const { int64_t Timer::Period() const { return mPeriod; } + +#ifndef NO_NATIVES +inline auto gameNow() { + return MISC::GET_GAME_TIMER(); +} + +GameTimer::GameTimer(int64_t timeout) : + mPeriod(timeout), + mPreviousTime(now()) { +} + +void GameTimer::Reset() { + mPreviousTime = now(); +} + +void GameTimer::Reset(int64_t newTimeout) { + mPeriod = newTimeout; + mPreviousTime = now(); +} + +bool GameTimer::Expired() const { + return now() > mPreviousTime + mPeriod; +} + +int64_t GameTimer::Elapsed() const { + return now() - mPreviousTime; +} + +int64_t GameTimer::Period() const { + return mPeriod; +} +#endif diff --git a/Gears/Util/Timer.h b/Gears/Util/Timer.h index ba0b026b..0003041f 100644 --- a/Gears/Util/Timer.h +++ b/Gears/Util/Timer.h @@ -15,3 +15,18 @@ class Timer int64_t mPeriod; int64_t mPreviousTime; }; + +class GameTimer +{ +public: + explicit GameTimer(int64_t timeout); + ~GameTimer() = default; + void Reset(); + void Reset(int64_t newTimeout); + bool Expired() const; + int64_t Elapsed() const; + int64_t Period() const; +private: + int64_t mPeriod; + int64_t mPreviousTime; +}; diff --git a/Gears/Util/ValueTimer.h b/Gears/Util/ValueTimer.h index a3af0ac2..02d33396 100644 --- a/Gears/Util/ValueTimer.h +++ b/Gears/Util/ValueTimer.h @@ -21,12 +21,12 @@ class ValueTimer { bool triggeredNow = false; if (mLimA < mLimB) { - if (newVal > mLimB && !mTriggered) { + if (newVal >= mLimB && !mTriggered) { triggeredNow = true; } } else { - if (newVal < mLimB && !mTriggered) { + if (newVal <= mLimB && !mTriggered) { triggeredNow = true; } } @@ -45,6 +45,6 @@ class ValueTimer { bool mTriggered; std::string mUnit; protected: - Timer mTimer; + GameTimer mTimer; std::function mFunc; }; diff --git a/Gears/script.cpp b/Gears/script.cpp index 549edcdc..7f9827be 100644 --- a/Gears/script.cpp +++ b/Gears/script.cpp @@ -321,13 +321,13 @@ void update_vehicle() { float speed; switch(joaat(valueTimer.mUnit.c_str())) { case (joaat("kph")): - speed = g_vehData.mVelocity.y * 3.6f; + speed = Length(g_vehData.mVelocity) * 3.6f; break; case (joaat("mph")): - speed = g_vehData.mVelocity.y / 0.44704f; + speed = Length(g_vehData.mVelocity) / 0.44704f; break; default: - speed = g_vehData.mVelocity.y; + speed = Length(g_vehData.mVelocity); } valueTimer.Update(speed); }