Persistent Owl Saves & Autosaves (#458)

* Persistent Owl Saves & Autosaves

* Better entrance handling

* clang format happy now pls?
This commit is contained in:
aMannus 2024-05-24 20:01:48 +02:00 committed by GitHub
parent f270156409
commit cb3237c645
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 246 additions and 18 deletions

View File

@ -413,12 +413,30 @@ void DrawEnhancementsMenu() {
ImGui::EndMenu();
}
if (UIWidgets::BeginMenu("Cycle / Saving")) {
if (UIWidgets::BeginMenu("Saving / Time Cycle")) {
ImGui::SeparatorText("Saving");
UIWidgets::CVarCheckbox("Persistent Owl Saves", "gEnhancements.Saving.PersistentOwlSaves",
{ .tooltip = "Continuing a save will not remove the owl save. Playing Song of "
"Time, allowing the moon to crash or finishing the "
"game will remove the owl save and become the new last save." });
UIWidgets::CVarCheckbox(
"Pause Menu Save", "gEnhancements.Kaleido.PauseSave",
"Pause Menu Save", "gEnhancements.Saving.PauseSave",
{ .tooltip = "Re-introduce the pause menu save system. Pressing B in the pause menu will give you the "
"option to create an Owl Save from your current location. When loading back into the "
"game, you will be placed at your last entrance." });
"option to create an Owl Save from your current location.\n\nWhen loading back into the "
"game, you will be placed either at the entrance of the dungeon you saved in, or in South "
"Clock Town." });
if (UIWidgets::CVarCheckbox(
"Autosave", "gEnhancements.Saving.Autosave",
{ .tooltip = "Automatically create owl saves on the chosen interval.\n\nWhen loading back into the "
"game, you will be placed either at the entrance of the dungeon you saved in, or in "
"South Clock Town." })) {
RegisterAutosave();
}
UIWidgets::CVarSliderInt("Autosave Interval (minutes): %d", "gEnhancements.Saving.AutosaveInterval", 1, 60,
5, { .disabled = !CVarGetInteger("gEnhancements.Saving.Autosave", 0) });
ImGui::SeparatorText("Time Cycle");
UIWidgets::CVarCheckbox("Do not reset Bottle content", "gEnhancements.Cycle.DoNotResetBottleContent",
{ .tooltip = "Playing the Song Of Time will not reset the bottles' content." });
UIWidgets::CVarCheckbox("Do not reset Consumables", "gEnhancements.Cycle.DoNotResetConsumables",
@ -428,11 +446,12 @@ void DrawEnhancementsMenu() {
{ .tooltip = "Playing the Song Of Time will not reset the Sword back to Kokiri Sword." });
UIWidgets::CVarCheckbox("Do not reset Rupees", "gEnhancements.Cycle.DoNotResetRupees",
{ .tooltip = "Playing the Song Of Time will not reset the your rupees." });
ImGui::PushStyleColor(ImGuiCol_Text, ImVec4(255, 255, 0, 255));
ImGui::SeparatorText("Unstable");
ImGui::PopStyleColor();
UIWidgets::CVarCheckbox(
"Disable Save Delay", "gEnhancements.Save.DisableSaveDelay",
"Disable Save Delay", "gEnhancements.Saving.DisableSaveDelay",
{ .tooltip = "Removes the arbitrary 2 second timer for saving from the original game. This is known to "
"cause issues when attempting the 0th Day Glitch" });

View File

@ -14,8 +14,10 @@ void InitEnhancements() {
// Clock
RegisterTextBasedClock();
// Cycle
// Cycle & Saving
RegisterEndOfCycleSaveHooks();
RegisterSavingEnhancements();
RegisterAutosave();
// Masks
RegisterFastTransformation();

View File

@ -21,6 +21,7 @@
#include "Graphics/PlayAsKafei.h"
#include "PlayerMovement/ClimbSpeed.h"
#include "Songs/EnableSunsSong.h"
#include "Saving/SavingEnhancements.h"
enum AlwaysWinDoggyRaceOptions {
ALWAYS_WIN_DOGGY_RACE_OFF,

View File

@ -31,6 +31,10 @@ void GameInteractor_ExecuteAfterEndOfCycleSave() {
GameInteractor::Instance->ExecuteHooks<GameInteractor::AfterEndOfCycleSave>();
}
void GameInteractor_ExecuteBeforeMoonCrashSaveReset() {
GameInteractor::Instance->ExecuteHooks<GameInteractor::BeforeMoonCrashSaveReset>();
}
void GameInteractor_ExecuteOnSceneInit(s16 sceneId, s8 spawnNum) {
SPDLOG_DEBUG("OnSceneInit: sceneId: {}, spawnNum: {}", sceneId, spawnNum);
GameInteractor::Instance->ExecuteHooks<GameInteractor::OnSceneInit>(sceneId, spawnNum);

View File

@ -45,6 +45,7 @@ typedef enum {
GI_VB_PREVENT_CLOCK_DISPLAY,
GI_VB_SONG_AVAILABLE_TO_PLAY,
GI_VB_USE_CUSTOM_CAMERA,
GI_VB_DELETE_OWL_SAVE,
GI_VB_PLAY_TRANSITION_CS,
GI_VB_TATL_INTERUPT_MSG3,
GI_VB_TATL_INTERUPT_MSG6,
@ -250,6 +251,7 @@ class GameInteractor {
DEFINE_HOOK(OnSaveInit, (s16 fileNum));
DEFINE_HOOK(BeforeEndOfCycleSave, ());
DEFINE_HOOK(AfterEndOfCycleSave, ());
DEFINE_HOOK(BeforeMoonCrashSaveReset, ());
DEFINE_HOOK(OnSceneInit, (s8 sceneId, s8 spawnNum));
DEFINE_HOOK(OnRoomInit, (s8 sceneId, s8 roomNum));
@ -288,6 +290,7 @@ void GameInteractor_ExecuteOnGameStateUpdate();
void GameInteractor_ExecuteOnSaveInit(s16 fileNum);
void GameInteractor_ExecuteBeforeEndOfCycleSave();
void GameInteractor_ExecuteAfterEndOfCycleSave();
void GameInteractor_ExecuteBeforeMoonCrashSaveReset();
void GameInteractor_ExecuteOnSceneInit(s16 sceneId, s8 spawnNum);
void GameInteractor_ExecuteOnRoomInit(s16 sceneId, s8 roomNum);

View File

@ -0,0 +1,150 @@
#include <libultraship/libultraship.h>
#include "BenPort.h"
#include "Enhancements/GameInteractor/GameInteractor.h"
extern "C" {
#include <variables.h>
#include <functions.h>
}
static uint32_t autosaveInterval = 0;
static uint32_t iconTimer = 0;
static uint64_t currentTimestamp = 0;
static uint64_t lastSaveTimestamp = GetUnixTimestamp();
static uint32_t autosaveGameStateUpdateHookId = 0;
static uint32_t autosaveGameStateDrawFinishHookId = 0;
// Used for saving through Autosaves and Pause Menu saves.
extern "C" int32_t GetSaveEntrance(PlayState* play) {
switch (play->sceneId) {
// Woodfall Temple + Odolwa
case SCENE_MITURIN:
case SCENE_MITURIN_BS:
return ENTRANCE(WOODFALL_TEMPLE, 0);
// Snowhead Temple + Goht
case SCENE_HAKUGIN:
case SCENE_HAKUGIN_BS:
return ENTRANCE(SNOWHEAD_TEMPLE, 0);
// Great Bay Temple + Gyorg
case SCENE_SEA:
case SCENE_SEA_BS:
return ENTRANCE(GREAT_BAY_TEMPLE, 0);
// Stone Tower Temple (+ inverted) + Twinmold
case SCENE_INISIE_N:
case SCENE_INISIE_R:
case SCENE_INISIE_BS:
return ENTRANCE(STONE_TOWER_TEMPLE, 0);
default:
return ENTRANCE(SOUTH_CLOCK_TOWN, 0);
}
}
void DeleteOwlSave() {
// Remove Owl Save on time cycle reset, needed when persisting owl saves and/or when
// creating owl saves without the player being send back to the file select screen.
// Delete Owl Save
func_80147314(&gPlayState->sramCtx, gSaveContext.fileNum);
// Set it to not be an owl save so after reloading the save file it doesn't try to load at the owl's position in
// clock town
gSaveContext.save.isOwlSave = false;
}
void DrawAutosaveIcon() {
// 5 seconds (100 frames) of showing the owl save icon to signify autosave has happened.
if (iconTimer != 0) {
float opacity = 255.0;
// Fade in icon
if (iconTimer > 80) {
opacity = 255.0 - (((iconTimer - 80.0) / 20.0) * 255);
// Fade out icon
} else if (iconTimer < 20) {
opacity = (iconTimer / 20.0) * 255.0;
}
Interface_DrawAutosaveIcon(gPlayState, uint16_t(opacity));
iconTimer--;
}
}
void HandleAutoSave() {
// Check if the interval has passed in minutes.
autosaveInterval = CVarGetInteger("gEnhancements.Saving.AutosaveInterval", 5) * 60000;
currentTimestamp = GetUnixTimestamp();
if ((currentTimestamp - lastSaveTimestamp) < autosaveInterval) {
return;
}
Player* player = GET_PLAYER(gPlayState);
if (player == NULL) {
return;
}
// If owl save available to create, do it and reset the interval.
if (gSaveContext.flashSaveAvailable && gSaveContext.fileNum != 255 &&
!Player_InBlockingCsMode(gPlayState, player) && gPlayState->pauseCtx.state == 0 &&
gPlayState->msgCtx.msgMode == 0) {
// Reset timestamp, set icon timer to show autosave icon for 5 seconds (100 frames)
lastSaveTimestamp = GetUnixTimestamp();
iconTimer = 100;
// Create owl save
gSaveContext.save.isOwlSave = true;
gSaveContext.save.shipSaveInfo.pauseSaveEntrance = GetSaveEntrance(gPlayState);
Play_SaveCycleSceneFlags(&gPlayState->state);
gSaveContext.save.saveInfo.playerData.savedSceneId = gPlayState->sceneId;
func_8014546C(&gPlayState->sramCtx);
Sram_SetFlashPagesOwlSave(&gPlayState->sramCtx, gFlashOwlSaveStartPages[gSaveContext.fileNum * 2],
gFlashOwlSaveNumPages[gSaveContext.fileNum * 2]);
Sram_StartWriteToFlashOwlSave(&gPlayState->sramCtx);
gSaveContext.save.isOwlSave = false;
gSaveContext.save.shipSaveInfo.pauseSaveEntrance = -1;
}
}
void RegisterSavingEnhancements() {
REGISTER_VB_SHOULD(GI_VB_DELETE_OWL_SAVE, {
if (CVarGetInteger("gEnhancements.Saving.PersistentOwlSaves", 0)) {
*should = false;
}
});
GameInteractor::Instance->RegisterGameHook<GameInteractor::BeforeEndOfCycleSave>([]() { DeleteOwlSave(); });
GameInteractor::Instance->RegisterGameHook<GameInteractor::BeforeMoonCrashSaveReset>([]() { DeleteOwlSave(); });
}
void RegisterAutosave() {
if (autosaveGameStateUpdateHookId) {
GameInteractor::Instance->UnregisterGameHook<GameInteractor::OnGameStateUpdate>(autosaveGameStateUpdateHookId);
autosaveGameStateUpdateHookId = 0;
}
if (autosaveGameStateDrawFinishHookId) {
GameInteractor::Instance->UnregisterGameHook<GameInteractor::OnGameStateDrawFinish>(
autosaveGameStateDrawFinishHookId);
autosaveGameStateDrawFinishHookId = 0;
}
if (CVarGetInteger("gEnhancements.Saving.Autosave", 0)) {
autosaveGameStateUpdateHookId =
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameStateUpdate>([]() {
if (gPlayState == nullptr) {
return;
}
HandleAutoSave();
});
autosaveGameStateDrawFinishHookId =
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameStateDrawFinish>([]() {
if (gPlayState == nullptr) {
return;
}
DrawAutosaveIcon();
});
}
}

View File

@ -0,0 +1,7 @@
#ifndef SAVING_ENHANCEMENTS_H
#define SAVING_ENHANCEMENTS_H
void RegisterSavingEnhancements();
void RegisterAutosave();
#endif // SAVING_ENHANCEMENTS_H

View File

@ -1365,6 +1365,10 @@ void osSpTaskYield(void);
void osViSetXScale(f32 value);
void osViSetYScale(f32 value);
// #endregion
// #region 2S2H [Enhancements]
// [Autosaves & Pause Menu Saves]
int32_t GetSaveEntrance(PlayState* play);
// #endregion
void Regs_InitData(PlayState* play);

View File

@ -293,8 +293,11 @@ s32 Inventory_ReplaceItem(struct PlayState* play, u8 oldItem, u8 newItem);
void Inventory_UpdateDeitySwordEquip(struct PlayState* play) ;
s32 Inventory_HasEmptyBottle(void);
s32 Inventory_HasItemInBottle(u8 item);
// #region 2S2H [Dpad]
// #region 2S2H - Enhancements
// [Dpad]
void Inventory_Dpad_UpdateBottleItem(struct PlayState* play, u8 item, u8 btn);
// [Autosave]
void Interface_DrawAutosaveIcon(struct PlayState* play, uint16_t opacity);
// #endregion
void Inventory_UpdateBottleItem(struct PlayState* play, u8 item, u8 btn);
s32 Inventory_ConsumeFairy(struct PlayState* play);

View File

@ -1753,6 +1753,9 @@ void Sram_UpdateWriteToFlashDefault(SramContext* sramCtx);
void Sram_SetFlashPagesOwlSave(SramContext* sramCtx, s32 curPage, s32 numPages);
void Sram_StartWriteToFlashOwlSave(SramContext* sramCtx);
void Sram_UpdateWriteToFlashOwlSave(SramContext* sramCtx);
// #region 2S2H [Port] Moved from z_sram_NES.c to use function in enhancement - Persistent Owl Saves
void func_80147314(SramContext* sramCtx, s32 fileNum); // Removes Owl Saves
// #endregion
extern u32 gSramSlotOffsets[];
extern u8 gAmmoItems[];

View File

@ -15,6 +15,7 @@
#include "overlays/kaleido_scope/ovl_kaleido_scope/z_kaleido_scope.h"
#include "overlays/actors/ovl_En_Mm3/z_en_mm3.h"
#include "interface/week_static/week_static.h"
#include "misc/title_static/title_static.h"
#include "BenPort.h"
#include <string.h>
#include "BenGui/HudEditor.h"
@ -3435,6 +3436,33 @@ void Interface_Dpad_LoadItemIconImpl(PlayState* play, u8 btn) {
}
}
void Interface_DrawAutosaveIcon(PlayState* play, uint16_t opacity) {
s16 rectLeft = 290;
s16 rectTop = 220;
s16 rectWidth = 24;
s16 rectHeight = 12;
s16 dsdx = 512;
s16 dtdy = 512;
OPEN_DISPS(gPlayState->state.gfxCtx);
gDPSetPrimColor(OVERLAY_DISP++, 0, 0, 255, 255, 255, opacity);
rectLeft = OTRGetRectDimensionFromRightEdge(rectLeft);
gDPPipeSync(OVERLAY_DISP++);
gDPSetCombineMode(OVERLAY_DISP++, G_CC_MODULATEIA_PRIM, G_CC_MODULATEIA_PRIM);
gDPLoadTextureBlock(OVERLAY_DISP++, gFileSelOwlSaveIconTex, G_IM_FMT_RGBA, G_IM_SIZ_32b, 24, 12, 0,
G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMIRROR | G_TX_WRAP, G_TX_NOMASK, G_TX_NOMASK, G_TX_NOLOD,
G_TX_NOLOD);
gSPWideTextureRectangle(OVERLAY_DISP++, rectLeft << 2, rectTop << 2, (rectLeft + rectWidth) << 2,
(rectTop + rectHeight) << 2, G_TX_RENDERTILE, 0, 0, dsdx << 1, dtdy << 1);
gDPPipeSync(OVERLAY_DISP++);
CLOSE_DISPS(gPlayState->state.gfxCtx);
}
void Interface_LoadItemIconImpl(PlayState* play, u8 btn) {
InterfaceContext* interfaceCtx = &play->interfaceCtx;

View File

@ -8,7 +8,6 @@
#include "Enhancements/GameInteractor/GameInteractor.h"
void Sram_SyncWriteToFlash(SramContext* sramCtx, s32 curPage, s32 numPages);
void func_80147314(SramContext* sramCtx, s32 fileNum);
void func_80147414(SramContext* sramCtx, s32 fileNum, s32 arg2);
#define CHECK_NEWF(newf) \
@ -1218,6 +1217,7 @@ void Sram_InitDebugSave(void) {
}
void Sram_ResetSaveFromMoonCrash(SramContext* sramCtx) {
GameInteractor_ExecuteBeforeMoonCrashSaveReset();
s32 i;
s32 cutsceneIndex = gSaveContext.save.cutsceneIndex;
@ -1375,7 +1375,11 @@ void Sram_OpenSave(FileSelectState* fileSelect, SramContext* sramCtx) {
}
fileNum = gSaveContext.fileNum;
func_80147314(sramCtx, fileNum);
// Remove Owl saves on save continue
if (GameInteractor_Should(GI_VB_DELETE_OWL_SAVE, true, 0)) {
func_80147314(sramCtx, fileNum);
}
}
}
@ -1996,7 +2000,7 @@ void Sram_UpdateWriteToFlashDefault(SramContext* sramCtx) {
}
}
} else if (OSTIME_TO_TIMER(osGetTime() - sramCtx->startWriteOsTime) >=
SECONDS_TO_TIMER(CVarGetInteger("gEnhancements.Save.DisableSaveDelay", 0) ? 0 : 2)) {
SECONDS_TO_TIMER(CVarGetInteger("gEnhancements.Saving.DisableSaveDelay", 0) ? 0 : 2)) {
// 2S2H [Port] Some tricks require a save delay so we can't just force it to zero
// Finished status is hardcoded to 2 seconds instead of when the task finishes
sramCtx->status = 0;
@ -2036,7 +2040,7 @@ void Sram_UpdateWriteToFlashOwlSave(SramContext* sramCtx) {
}
}
} else if (OSTIME_TO_TIMER(osGetTime() - sramCtx->startWriteOsTime) >=
SECONDS_TO_TIMER(CVarGetInteger("gEnhancements.Save.DisableSaveDelay", 0) ? 0 : 2)) {
SECONDS_TO_TIMER(CVarGetInteger("gEnhancements.Saving.DisableSaveDelay", 0) ? 0 : 2)) {
// 2S2H [Port] Some tricks require a save delay so we can't just force it to zero
// Finished status is hardcoded to 2 seconds instead of when the task finishes
sramCtx->status = 0;

View File

@ -912,7 +912,7 @@ void KaleidoScope_DrawPages(PlayState* play, GraphicsContext* gfxCtx) {
}
}
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0) || CVarGetInteger("gEnhancements.Kaleido.GameOver", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0) || CVarGetInteger("gEnhancements.Kaleido.GameOver", 0)) {
Gfx_SetupDL42_Opa(gfxCtx);
if ((pauseCtx->state == PAUSE_STATE_SAVEPROMPT) || IS_PAUSE_STATE_GAMEOVER) {
KaleidoScope_UpdatePrompt(play);
@ -3377,7 +3377,7 @@ void KaleidoScope_Update(PlayState* play) {
if (!pauseCtx->itemDescriptionOn &&
(CHECK_BTN_ALL(input->press.button, BTN_START) || CHECK_BTN_ALL(input->press.button, BTN_B))) {
Interface_SetAButtonDoAction(play, DO_ACTION_NONE);
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0)) {
if (CHECK_BTN_ALL(input->press.button, BTN_B)) {
pauseCtx->state = PAUSE_STATE_SAVEPROMPT;
Audio_PlaySfx_MessageDecide();
@ -3418,7 +3418,7 @@ void KaleidoScope_Update(PlayState* play) {
// Abort having the player play the song and close the pause menu
AudioOcarina_SetInstrument(OCARINA_INSTRUMENT_OFF);
Interface_SetAButtonDoAction(play, DO_ACTION_NONE);
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0)) {
if (CHECK_BTN_ALL(input->press.button, BTN_B)) {
pauseCtx->state = PAUSE_STATE_SAVEPROMPT;
Audio_PlaySfx_MessageDecide();
@ -3461,7 +3461,7 @@ void KaleidoScope_Update(PlayState* play) {
if (CHECK_BTN_ALL(input->press.button, BTN_START) || CHECK_BTN_ALL(input->press.button, BTN_B)) {
AudioOcarina_SetInstrument(OCARINA_INSTRUMENT_OFF);
Interface_SetAButtonDoAction(play, DO_ACTION_NONE);
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0)) {
if (CHECK_BTN_ALL(input->press.button, BTN_B)) {
pauseCtx->state = PAUSE_STATE_SAVEPROMPT;
Audio_PlaySfx_MessageDecide();
@ -3513,12 +3513,12 @@ void KaleidoScope_Update(PlayState* play) {
pauseCtx->savePromptState = PAUSE_SAVEPROMPT_STATE_RETURN_TO_MENU;
} else {
Audio_PlaySfx(NA_SE_SY_PIECE_OF_HEART);
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0)) {
gSaveContext.save.isOwlSave = true;
// 2S2H [Enhancement] Eventually we might allow them to load from their last entrance,
// but we need to first identify and fix edge cases where that doesn't work properly
// like grottos and cutscenes
gSaveContext.save.shipSaveInfo.pauseSaveEntrance = ENTRANCE(SOUTH_CLOCK_TOWN, 0);
gSaveContext.save.shipSaveInfo.pauseSaveEntrance = GetSaveEntrance(play);
}
Play_SaveCycleSceneFlags(&play->state);
gSaveContext.save.saveInfo.playerData.savedSceneId = play->sceneId;
@ -3528,7 +3528,7 @@ void KaleidoScope_Update(PlayState* play) {
255) { // 2S2H [Enhancement] Don't let them save if they are in debug save
pauseCtx->savePromptState = PAUSE_SAVEPROMPT_STATE_5;
} else {
if (CVarGetInteger("gEnhancements.Kaleido.PauseSave", 0)) {
if (CVarGetInteger("gEnhancements.Saving.PauseSave", 0)) {
Sram_SetFlashPagesOwlSave(sramCtx,
gFlashOwlSaveStartPages[gSaveContext.fileNum * 2],
gFlashOwlSaveNumPages[gSaveContext.fileNum * 2]);