Skip to content
This repository was archived by the owner on Jan 10, 2025. It is now read-only.

Commit 197f01a

Browse files
committed
Improve acceleration timer accuracy
1 parent dc15bdb commit 197f01a

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

Gears/Util/Timer.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#include "Timer.h"
2+
3+
#ifndef NO_NATIVES
4+
#include <inc/natives.h>
5+
#endif
6+
27
#include <chrono>
38

49
inline auto now() {
@@ -32,3 +37,35 @@ int64_t Timer::Elapsed() const {
3237
int64_t Timer::Period() const {
3338
return mPeriod;
3439
}
40+
41+
#ifndef NO_NATIVES
42+
inline auto gameNow() {
43+
return MISC::GET_GAME_TIMER();
44+
}
45+
46+
GameTimer::GameTimer(int64_t timeout) :
47+
mPeriod(timeout),
48+
mPreviousTime(now()) {
49+
}
50+
51+
void GameTimer::Reset() {
52+
mPreviousTime = now();
53+
}
54+
55+
void GameTimer::Reset(int64_t newTimeout) {
56+
mPeriod = newTimeout;
57+
mPreviousTime = now();
58+
}
59+
60+
bool GameTimer::Expired() const {
61+
return now() > mPreviousTime + mPeriod;
62+
}
63+
64+
int64_t GameTimer::Elapsed() const {
65+
return now() - mPreviousTime;
66+
}
67+
68+
int64_t GameTimer::Period() const {
69+
return mPeriod;
70+
}
71+
#endif

Gears/Util/Timer.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,18 @@ class Timer
1515
int64_t mPeriod;
1616
int64_t mPreviousTime;
1717
};
18+
19+
class GameTimer
20+
{
21+
public:
22+
explicit GameTimer(int64_t timeout);
23+
~GameTimer() = default;
24+
void Reset();
25+
void Reset(int64_t newTimeout);
26+
bool Expired() const;
27+
int64_t Elapsed() const;
28+
int64_t Period() const;
29+
private:
30+
int64_t mPeriod;
31+
int64_t mPreviousTime;
32+
};

Gears/Util/ValueTimer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class ValueTimer {
2121

2222
bool triggeredNow = false;
2323
if (mLimA < mLimB) {
24-
if (newVal > mLimB && !mTriggered) {
24+
if (newVal >= mLimB && !mTriggered) {
2525
triggeredNow = true;
2626
}
2727
}
2828
else {
29-
if (newVal < mLimB && !mTriggered) {
29+
if (newVal <= mLimB && !mTriggered) {
3030
triggeredNow = true;
3131
}
3232
}
@@ -45,6 +45,6 @@ class ValueTimer {
4545
bool mTriggered;
4646
std::string mUnit;
4747
protected:
48-
Timer mTimer;
48+
GameTimer mTimer;
4949
std::function<void(const std::string&)> mFunc;
5050
};

Gears/script.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,13 @@ void update_vehicle() {
321321
float speed;
322322
switch(joaat(valueTimer.mUnit.c_str())) {
323323
case (joaat("kph")):
324-
speed = g_vehData.mVelocity.y * 3.6f;
324+
speed = Length(g_vehData.mVelocity) * 3.6f;
325325
break;
326326
case (joaat("mph")):
327-
speed = g_vehData.mVelocity.y / 0.44704f;
327+
speed = Length(g_vehData.mVelocity) / 0.44704f;
328328
break;
329329
default:
330-
speed = g_vehData.mVelocity.y;
330+
speed = Length(g_vehData.mVelocity);
331331
}
332332
valueTimer.Update(speed);
333333
}

0 commit comments

Comments
 (0)