From 78b592d631e8c9fbab8972c5b95a4b5568dfe3bd Mon Sep 17 00:00:00 2001 From: past-due <30942300+past-due@users.noreply.github.com> Date: Mon, 18 Mar 2024 17:53:09 -0400 Subject: [PATCH] Add support for "Autosaves-Only" tweak option Co-Authored-By: KJeff01 <22485442+KJeff01@users.noreply.github.com> --- src/campaigninfo.cpp | 10 ++++++++++ src/campaigninfo.h | 1 + src/ingameop.cpp | 10 +++++++--- src/keybind.cpp | 11 +++++++++++ src/loadsave.cpp | 8 +++++++- src/loadsave.h | 2 +- src/main.cpp | 14 ++++++++++++++ 7 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/campaigninfo.cpp b/src/campaigninfo.cpp index 60b69ee3b2b..e189f6aa7fe 100644 --- a/src/campaigninfo.cpp +++ b/src/campaigninfo.cpp @@ -171,3 +171,13 @@ nlohmann::json getCamTweakOptionsValue(const std::string& identifier, nlohmann:: } return it->second; } + +bool getCamTweakOption_AutosavesOnly() +{ + auto val = getCamTweakOptionsValue("autosavesOnly", false); + if (!val.is_boolean()) + { + return false; + } + return val.get(); +} diff --git a/src/campaigninfo.h b/src/campaigninfo.h index df14c5e3c80..2ba55382105 100644 --- a/src/campaigninfo.h +++ b/src/campaigninfo.h @@ -56,5 +56,6 @@ void setCamTweakOptions(std::unordered_map options) void clearCamTweakOptions(); const std::unordered_map& getCamTweakOptions(); nlohmann::json getCamTweakOptionsValue(const std::string& identifier, nlohmann::json defaultValue); +bool getCamTweakOption_AutosavesOnly(); #endif // __INCLUDED_SRC_CAMPAIGNINFO_H__ diff --git a/src/ingameop.cpp b/src/ingameop.cpp index fd44ce4cc76..21f57d131ec 100644 --- a/src/ingameop.cpp +++ b/src/ingameop.cpp @@ -49,6 +49,7 @@ #include "qtscript.h" // for bInTutorial #include "radar.h" #include "seqdisp.h" +#include "campaigninfo.h" #include "hci/groups.h" #include "screens/netpregamescreen.h" @@ -256,9 +257,12 @@ static bool _intAddInGameOptions() // add 'load' addIGTextButton(INTINGAMEOP_LOAD_MISSION, INTINGAMEOP_1_X, INTINGAMEOPAUTO_Y_LINE(row), INTINGAMEOP_OP_W, _("Load Game"), OPALIGN); row++; - // add 'save' - addIGTextButton(INTINGAMEOP_SAVE_MISSION, INTINGAMEOP_1_X, INTINGAMEOPAUTO_Y_LINE(row), INTINGAMEOP_OP_W, _("Save Game"), OPALIGN); - row++; + if (!getCamTweakOption_AutosavesOnly()) + { + // add 'save' + addIGTextButton(INTINGAMEOP_SAVE_MISSION, INTINGAMEOP_1_X, INTINGAMEOPAUTO_Y_LINE(row), INTINGAMEOP_OP_W, _("Save Game"), OPALIGN); + row++; + } } } diff --git a/src/keybind.cpp b/src/keybind.cpp index 591e557526f..c4b6e5b6a61 100644 --- a/src/keybind.cpp +++ b/src/keybind.cpp @@ -77,6 +77,7 @@ #include "game.h" #include "droid.h" #include "spectatorwidgets.h" +#include "campaigninfo.h" #include "activity.h" @@ -2543,6 +2544,11 @@ void kf_QuickSave() { return; } + if (getCamTweakOption_AutosavesOnly()) + { + console(_("QuickSave not allowed in Autosaves-Only mode")); + return; + } const char *filename = bMultiPlayer ? QUICKSAVE_SKI_FILENAME : QUICKSAVE_CAM_FILENAME; const char *quickSaveFolder = bMultiPlayer ? QUICKSAVE_SKI_FOLDER : QUICKSAVE_CAM_FOLDER; @@ -2572,6 +2578,11 @@ void kf_QuickLoad() { return; } + if (getCamTweakOption_AutosavesOnly()) + { + console(_("QuickLoad not allowed in Autosaves-Only mode")); + return; + } const char *filename = bMultiPlayer ? QUICKSAVE_SKI_FILENAME : QUICKSAVE_CAM_FILENAME; // check for .json version, because that's what going to be loaded anyway diff --git a/src/loadsave.cpp b/src/loadsave.cpp index 24d4feec647..708fefe5841 100644 --- a/src/loadsave.cpp +++ b/src/loadsave.cpp @@ -59,6 +59,7 @@ #include "clparse.h" #include "ingameop.h" #include "game.h" +#include "campaigninfo.h" #include "version.h" #define totalslots 36 // saves slots #define slotsInColumn 12 // # of slots in a column @@ -1128,7 +1129,7 @@ static void freeAutoSaveSlot(SAVEGAME_LOC loc) deleteSaveGame_classic(savefile); } -bool autoSave() +bool autoSave(bool force) { // Bail out if we're running a _true_ multiplayer game or are playing a tutorial/debug/cheating/autogames const DebugInputManager& dbgInputManager = gInputManager.debugManager(); @@ -1141,6 +1142,11 @@ bool autoSave() { return false; } + // Bail out if autosaves only mode and not forced + if (getCamTweakOption_AutosavesOnly() && !force) + { + return false; + } const char *dir = bMultiPlayer ? SAVEGAME_SKI_AUTO : SAVEGAME_CAM_AUTO; // Backward compatibility: remove later if (!freeAutoSaveSlot_old(dir)) diff --git a/src/loadsave.h b/src/loadsave.h index b86618d377e..4f6d8cbb1e0 100644 --- a/src/loadsave.h +++ b/src/loadsave.h @@ -92,6 +92,6 @@ void loadSaveScreenSizeDidChange(unsigned int oldWidth, unsigned int oldHeight, bool findLastSave(); -bool autoSave(); +bool autoSave(bool force = false); #endif // __INCLUDED_SRC_LOADSAVE_H__ diff --git a/src/main.cpp b/src/main.cpp index 295eac1640e..b3d683fb45e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -109,6 +109,7 @@ #include "activity.h" #include "stdinreader.h" #include "gamehistorylogger.h" +#include "campaigninfo.h" #if defined(ENABLE_DISCORD) #include "integrations/wzdiscordrpc.h" #endif @@ -185,6 +186,7 @@ char MultiCustomMapsPath[PATH_MAX]; char MultiPlayersPath[PATH_MAX]; char KeyMapPath[PATH_MAX]; char FavoriteStructuresPath[PATH_MAX]; +static uint32_t forcedAutosaveTime = 0; // Start game in title mode: static GS_GAMEMODE gameStatus = GS_TITLE_SCREEN; // Status of the gameloop @@ -988,6 +990,11 @@ static void startGameLoop() triggerEvent(TRIGGER_START_LEVEL); screen_disableMapPreview(); + if (!bMultiPlayer && getCamTweakOption_AutosavesOnly()) + { + forcedAutosaveTime = gameTime + 1000; //Really just to prevent Intel videos messages from not getting saved if run immediately. + } + GameStoryLogger::instance().logStartGame(); auto currentGameMode = ActivityManager::instance().getCurrentGameMode(); @@ -1084,6 +1091,7 @@ static void stopGameLoop() GameStoryLogger::instance().reset(); gameInitialised = false; + forcedAutosaveTime = 0; } @@ -1316,6 +1324,12 @@ void mainLoop() realTimeUpdate(); // Update realTime. } + if ((forcedAutosaveTime != 0) && (gameTime > forcedAutosaveTime)) + { + forcedAutosaveTime = 0; + autoSave(true); + } + wzApplyCursor(); runNotifications(); #if defined(ENABLE_DISCORD)