Skip to content

Commit

Permalink
Improve acceleration timer accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
ikt32 committed Jun 20, 2021
1 parent dc15bdb commit 197f01a
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
37 changes: 37 additions & 0 deletions Gears/Util/Timer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#include "Timer.h"

#ifndef NO_NATIVES
#include <inc/natives.h>
#endif

#include <chrono>

inline auto now() {
Expand Down Expand Up @@ -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
15 changes: 15 additions & 0 deletions Gears/Util/Timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
6 changes: 3 additions & 3 deletions Gears/Util/ValueTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -45,6 +45,6 @@ class ValueTimer {
bool mTriggered;
std::string mUnit;
protected:
Timer mTimer;
GameTimer mTimer;
std::function<void(const std::string&)> mFunc;
};
6 changes: 3 additions & 3 deletions Gears/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 197f01a

Please sign in to comment.