Skip to content

Commit

Permalink
Refactor autosave and quicksave
Browse files Browse the repository at this point in the history
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"
  • Loading branch information
Lordmau5 committed Aug 27, 2024
1 parent 4fa33ba commit ec0e41e
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/gtasa/util/GameHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ class GameHandler

static inline bool didTryLoadAutoSave = false;

static inline int lastMissionsPassed = -1;
static inline int lastSaved = 0;
static inline int lastQuickSave = 0;
static inline int lastMissionsPassed = -1;
static inline std::chrono::steady_clock::time_point lastSaved
= std::chrono::steady_clock::now ();
static inline std::chrono::steady_clock::time_point lastQuickSave
= std::chrono::steady_clock::now ();

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

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

int missionsPassed = GameUtil::GetRealMissionsPassed ();
int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped,
(unsigned int) lastMissionsPassed);

if (lastMissionsPassed == -1)
{
Expand All @@ -142,6 +142,7 @@ class GameHandler
lastMissionsPassed = missionsPassed;
}

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

EffectHandler::HandleFunction (json);

lastSaved = currentTime + 1000;
lastSaved = currentTime + std::chrono::milliseconds{10000};
}
}

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

int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped,
(unsigned int) lastQuickSave);
auto currentTime = std::chrono::steady_clock::now ();

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

nlohmann::json json;

Expand Down Expand Up @@ -246,7 +246,6 @@ class GameHandler
Hooked_CTheScripts_Load (auto &&cb)
{
lastMissionsPassed = -1;
lastSaved = 0;

return cb ();
}
Expand Down

0 comments on commit ec0e41e

Please sign in to comment.