AMFP Enhanced
C++ reference: the code this unofficial fork added
Loading...
Searching...
No Matches
LuxAutoSave.h
Go to the documentation of this file.
1/*
2 * AMFP-2026: autosave and manual saves (Enhanced option "Autosave"). Pure, unit tested in
3 * tests/cpp/test_autosave.cpp.
4 *
5 * - Two kinds of save file, side by side in the profile folder: autosaves ("AutoSave_...", the
6 * game's own name) rotate among themselves; manual saves ("Save_...", pause menu "Save game")
7 * are never deleted by the game.
8 * - Option levels: Off (as shipped: only the saves the map scripts make), On map change (plus a
9 * save after every map change), Every 5 / 10 / 15 minutes (plus a timed save).
10 * - How many autosaves are kept: Off keeps the shipped count (game.cfg MaxAutoSaves, 20); any
11 * other level keeps the newest kLuxAutoSavesKept (5), so frequent saves do not pile up.
12 * - Extra saves happen only at a safe moment: playing freely (no story scene, menu or journal),
13 * alive, and no enemy hunting or searching. A pending save waits for one.
14 */
15#ifndef LUX_AUTO_SAVE_H
16#define LUX_AUTO_SAVE_H
17
18#include <string>
19
21{
28};
29
31{
35};
36
37static const wchar_t* const kLuxAutoSavePrefix = L"AutoSave";
38static const wchar_t* const kLuxManualSavePrefix = L"Save";
39
40inline bool LuxAutoSaveOnMapChange(int alLevel) { return alLevel >= eLuxAutoSave_MapChange && alLevel < eLuxAutoSave_LastEnum; }
41
42static const int kLuxAutoSavesKept = 5;
43
44/** How many autosaves to keep, the new one included. Off = as shipped (alShippedMax). */
45inline int LuxAutoSavesToKeep(int alLevel, int alShippedMax)
46{
47 if(alLevel > eLuxAutoSave_Off && alLevel < eLuxAutoSave_LastEnum) return kLuxAutoSavesKept;
48 return alShippedMax;
49}
50
51/** Seconds between timed saves, 0 = none. */
52inline float LuxAutoSaveInterval(int alLevel)
53{
54 switch(alLevel)
55 {
56 case eLuxAutoSave_Every5: return 300.0f;
57 case eLuxAutoSave_Every10: return 600.0f;
58 case eLuxAutoSave_Every15: return 900.0f;
59 default: return 0.0f;
60 }
61}
62
63inline bool LuxStartsWithI(const std::wstring& s, const std::wstring& p)
64{
65 if(s.size() < p.size()) return false;
66 for(size_t i = 0; i < p.size(); ++i)
67 {
68 wchar_t a = s[i], b = p[i];
69 if(a >= L'A' && a <= L'Z') a = (wchar_t)(a - L'A' + L'a');
70 if(b >= L'A' && b <= L'Z') b = (wchar_t)(b - L'A' + L'a');
71 if(a != b) return false;
72 }
73 return true;
74}
75
76/** Which kind a save file name is ("AutoSave_<map>_...sav", "Save_<map>_...sav"). */
77inline int LuxSaveKind(const std::wstring& asFile)
78{
79 if(LuxStartsWithI(asFile, std::wstring(kLuxAutoSavePrefix) + L"_")) return eLuxSaveKind_Auto;
80 if(LuxStartsWithI(asFile, std::wstring(kLuxManualSavePrefix) + L"_")) return eLuxSaveKind_Manual;
81 return eLuxSaveKind_Other;
82}
83
84/** A moment the game can be saved without surprising the player on load. */
85inline bool LuxAutoSaveSafe(bool abPlayerActive, bool abDead, bool abStoryScene, bool abInDanger, bool abInMenu)
86{
87 return abPlayerActive && !abDead && !abStoryScene && !abInDanger && !abInMenu;
88}
89
90/** When the extra autosaves are due. Counts play time only; any save restarts it.
91 * Call OnMapChange before the new map's OnStart / OnEnter run: when the map's own script saves
92 * on arrival (most do), that save clears the pending one. */
94{
95public:
96 cLuxAutoSaveTimer() : mlLevel(eLuxAutoSave_Off), mfSinceSave(0), mbMapChangePending(false) {}
97
98 void SetLevel(int alLevel) { mlLevel = alLevel; }
99 int GetLevel() const { return mlLevel; }
100
101 /** A map change started: save at the next safe moment, unless something saves first. */
102 void OnMapChange() { if(LuxAutoSaveOnMapChange(mlLevel)) mbMapChangePending = true; }
103 /** Any save (script, quit, manual, ours) restarts the wait and covers a pending one. */
104 void OnSaved() { mfSinceSave = 0; mbMapChangePending = false; }
105
106 /** True when a save should happen now. */
107 bool Update(float afTimeStep, bool abPlaying, bool abSafe)
108 {
109 if(abPlaying && afTimeStep > 0) mfSinceSave += afTimeStep;
110 if(abSafe == false) return false;
111 if(mbMapChangePending) return true;
112 float fInterval = LuxAutoSaveInterval(mlLevel);
113 return fInterval > 0 && mfSinceSave >= fInterval;
114 }
115
116 bool IsMapChangePending() const { return mbMapChangePending; }
117 float GetSinceSave() const { return mfSinceSave; }
118
119private:
120 int mlLevel;
121 float mfSinceSave;
122 bool mbMapChangePending;
123};
124
125#endif
eLuxSaveKind
Definition LuxAutoSave.h:31
@ eLuxSaveKind_Auto
Definition LuxAutoSave.h:32
@ eLuxSaveKind_Other
Definition LuxAutoSave.h:34
@ eLuxSaveKind_Manual
Definition LuxAutoSave.h:33
static const wchar_t *const kLuxAutoSavePrefix
Definition LuxAutoSave.h:37
bool LuxStartsWithI(const std::wstring &s, const std::wstring &p)
Definition LuxAutoSave.h:63
static const int kLuxAutoSavesKept
Definition LuxAutoSave.h:42
bool LuxAutoSaveSafe(bool abPlayerActive, bool abDead, bool abStoryScene, bool abInDanger, bool abInMenu)
A moment the game can be saved without surprising the player on load.
Definition LuxAutoSave.h:85
bool LuxAutoSaveOnMapChange(int alLevel)
Definition LuxAutoSave.h:40
float LuxAutoSaveInterval(int alLevel)
Seconds between timed saves, 0 = none.
Definition LuxAutoSave.h:52
int LuxSaveKind(const std::wstring &asFile)
Which kind a save file name is ("AutoSave_<map>_...sav", "Save_<map>_...sav").
Definition LuxAutoSave.h:77
eLuxAutoSave
Definition LuxAutoSave.h:21
@ eLuxAutoSave_MapChange
Definition LuxAutoSave.h:23
@ eLuxAutoSave_Off
Definition LuxAutoSave.h:22
@ eLuxAutoSave_Every5
Definition LuxAutoSave.h:24
@ eLuxAutoSave_Every15
Definition LuxAutoSave.h:26
@ eLuxAutoSave_Every10
Definition LuxAutoSave.h:25
@ eLuxAutoSave_LastEnum
Definition LuxAutoSave.h:27
static const wchar_t *const kLuxManualSavePrefix
Definition LuxAutoSave.h:38
int LuxAutoSavesToKeep(int alLevel, int alShippedMax)
How many autosaves to keep, the new one included.
Definition LuxAutoSave.h:45
int GetLevel() const
Definition LuxAutoSave.h:99
float GetSinceSave() const
bool IsMapChangePending() const
void OnSaved()
Any save (script, quit, manual, ours) restarts the wait and covers a pending one.
bool Update(float afTimeStep, bool abPlaying, bool abSafe)
True when a save should happen now.
void SetLevel(int alLevel)
Definition LuxAutoSave.h:98
void OnMapChange()
A map change started: save at the next safe moment, unless something saves first.