Skip to content

Commit ec0e41e

Browse files
committed
Refactor autosave and quicksave
They now use a `std::chrono::steady_clock` instead of GTA's internal timers - this should help with time drift and not being able to quicksave when loading a save that's "in the future"
1 parent 4fa33ba commit ec0e41e

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/gtasa/util/GameHandler.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ class GameHandler
2525

2626
static inline bool didTryLoadAutoSave = false;
2727

28-
static inline int lastMissionsPassed = -1;
29-
static inline int lastSaved = 0;
30-
static inline int lastQuickSave = 0;
28+
static inline int lastMissionsPassed = -1;
29+
static inline std::chrono::steady_clock::time_point lastSaved
30+
= std::chrono::steady_clock::now ();
31+
static inline std::chrono::steady_clock::time_point lastQuickSave
32+
= std::chrono::steady_clock::now ();
3133

3234
static inline CStuntJump *&currentStuntJump = *(CStuntJump **) 0xA9A88C;
3335

@@ -130,8 +132,6 @@ class GameHandler
130132
if (!CONFIG ("Chaos.AutosaveAfterMissionPassed", true)) return;
131133

132134
int missionsPassed = GameUtil::GetRealMissionsPassed ();
133-
int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped,
134-
(unsigned int) lastMissionsPassed);
135135

136136
if (lastMissionsPassed == -1)
137137
{
@@ -142,6 +142,7 @@ class GameHandler
142142
lastMissionsPassed = missionsPassed;
143143
}
144144

145+
auto currentTime = std::chrono::steady_clock::now ();
145146
if (missionsPassed > lastMissionsPassed && lastSaved < currentTime
146147
&& !CTheScripts::IsPlayerOnAMission ())
147148
{
@@ -155,7 +156,7 @@ class GameHandler
155156

156157
EffectHandler::HandleFunction (json);
157158

158-
lastSaved = currentTime + 1000;
159+
lastSaved = currentTime + std::chrono::milliseconds{10000};
159160
}
160161
}
161162

@@ -164,13 +165,12 @@ class GameHandler
164165
{
165166
if (!CONFIG ("Chaos.QuickSave", false)) return;
166167

167-
int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped,
168-
(unsigned int) lastQuickSave);
168+
auto currentTime = std::chrono::steady_clock::now ();
169169

170170
if (!FrontEndMenuManager.m_bMenuActive && KeyPressed (VK_F7)
171171
&& lastQuickSave < currentTime)
172172
{
173-
lastQuickSave = currentTime + 1000;
173+
lastQuickSave = currentTime + std::chrono::milliseconds{10000};
174174

175175
nlohmann::json json;
176176

@@ -246,7 +246,6 @@ class GameHandler
246246
Hooked_CTheScripts_Load (auto &&cb)
247247
{
248248
lastMissionsPassed = -1;
249-
lastSaved = 0;
250249

251250
return cb ();
252251
}

0 commit comments

Comments
 (0)