From 37f31116b32b9ea08e2cb08fafdcfa5c37abddea Mon Sep 17 00:00:00 2001 From: Adam Bird Date: Sun, 22 Jan 2023 03:51:23 -0500 Subject: [PATCH] Add reset/randomize all SFX and Cosmetic commands to console (#2378) * add reset/randomize all commands to debug console for sfx and cosmetic editor * fix cvar func --- .../cosmetics/CosmeticsEditor.cpp | 22 +++++ .../Enhancements/cosmetics/CosmeticsEditor.h | 2 + soh/soh/Enhancements/debugconsole.cpp | 46 ++++++++++ soh/soh/Enhancements/sfx-editor/SfxEditor.cpp | 90 ++++++++++++------- soh/soh/Enhancements/sfx-editor/SfxEditor.h | 3 + 5 files changed, 132 insertions(+), 31 deletions(-) diff --git a/soh/soh/Enhancements/cosmetics/CosmeticsEditor.cpp b/soh/soh/Enhancements/cosmetics/CosmeticsEditor.cpp index ebfca5df8..e89442d8f 100644 --- a/soh/soh/Enhancements/cosmetics/CosmeticsEditor.cpp +++ b/soh/soh/Enhancements/cosmetics/CosmeticsEditor.cpp @@ -1798,3 +1798,25 @@ void InitCosmeticsEditor() { SohImGui::RequestCvarSaveOnNextTick(); ApplyOrResetCustomGfxPatches(); } + +void CosmeticsEditor_RandomizeAll() { + for (auto& [id, cosmeticOption] : cosmeticOptions) { + if (!CVarGetInteger(cosmeticOption.lockedCvar, 0)) { + RandomizeColor(cosmeticOption); + } + } + + SohImGui::RequestCvarSaveOnNextTick(); + ApplyOrResetCustomGfxPatches(); +} + +void CosmeticsEditor_ResetAll() { + for (auto& [id, cosmeticOption] : cosmeticOptions) { + if (!CVarGetInteger(cosmeticOption.lockedCvar, 0)) { + ResetColor(cosmeticOption); + } + } + + SohImGui::RequestCvarSaveOnNextTick(); + ApplyOrResetCustomGfxPatches(); +} diff --git a/soh/soh/Enhancements/cosmetics/CosmeticsEditor.h b/soh/soh/Enhancements/cosmetics/CosmeticsEditor.h index e0e17929a..12a190c17 100644 --- a/soh/soh/Enhancements/cosmetics/CosmeticsEditor.h +++ b/soh/soh/Enhancements/cosmetics/CosmeticsEditor.h @@ -25,3 +25,5 @@ static ImGuiTableColumnFlags FlagsCell = ImGuiTableColumnFlags_WidthStretch | Im void InitCosmeticsEditor();//Init the menu itself ImVec4 GetRandomValue(int MaximumPossible); +void CosmeticsEditor_RandomizeAll(); +void CosmeticsEditor_ResetAll(); diff --git a/soh/soh/Enhancements/debugconsole.cpp b/soh/soh/Enhancements/debugconsole.cpp index cbb629d01..6b27fba1d 100644 --- a/soh/soh/Enhancements/debugconsole.cpp +++ b/soh/soh/Enhancements/debugconsole.cpp @@ -8,6 +8,8 @@ #include #include "soh/OTRGlobals.h" #include +#include "soh/Enhancements/cosmetics/CosmeticsEditor.h" +#include "soh/Enhancements/sfx-editor/SfxEditor.h" #define Path _Path @@ -906,6 +908,42 @@ static bool CuccoStormHandler(std::shared_ptr Console, const std: return CMD_SUCCESS; } +static bool CosmeticsHandler(std::shared_ptr Console, const std::vector& args) { + if (args.size() != 2) { + SohImGui::GetConsole()->SendErrorMessage("[SOH] Unexpected arguments passed"); + return CMD_FAILED; + } + + if (args[1].compare("reset") == 0) { + CosmeticsEditor_ResetAll(); + } else if (args[1].compare("randomize") == 0) { + CosmeticsEditor_RandomizeAll(); + } else { + SohImGui::GetConsole()->SendErrorMessage("[SOH] Invalid argument passed, must be 'reset' or 'randomize'"); + return CMD_FAILED; + } + + return CMD_SUCCESS; +} + +static bool SfxHandler(std::shared_ptr Console, const std::vector& args) { + if (args.size() != 2) { + SohImGui::GetConsole()->SendErrorMessage("[SOH] Unexpected arguments passed"); + return CMD_FAILED; + } + + if (args[1].compare("reset") == 0) { + SfxEditor_ResetAll(); + } else if (args[1].compare("randomize") == 0) { + SfxEditor_RandomizeAll(); + } else { + SohImGui::GetConsole()->SendErrorMessage("[SOH] Invalid argument passed, must be 'reset' or 'randomize'"); + return CMD_FAILED; + } + + return CMD_SUCCESS; +} + #define VARTYPE_INTEGER 0 #define VARTYPE_FLOAT 1 #define VARTYPE_STRING 2 @@ -1159,5 +1197,13 @@ void DebugConsole_Init(void) { CMD_REGISTER("cucco_storm", { CuccoStormHandler, "Cucco Storm" }); + CMD_REGISTER("cosmetics", { CosmeticsHandler, "Change cosmetics.", { + { "reset|randomize", Ship::ArgumentType::TEXT }, + }}); + + CMD_REGISTER("sfx", { SfxHandler, "Change SFX.", { + { "reset|randomize", Ship::ArgumentType::TEXT }, + }}); + CVarLoad(); } diff --git a/soh/soh/Enhancements/sfx-editor/SfxEditor.cpp b/soh/soh/Enhancements/sfx-editor/SfxEditor.cpp index 80b9c9baa..fe2020b0f 100644 --- a/soh/soh/Enhancements/sfx-editor/SfxEditor.cpp +++ b/soh/soh/Enhancements/sfx-editor/SfxEditor.cpp @@ -203,22 +203,49 @@ void UpdateCurrentBGM(u16 seqKey, SeqType seqType) { } } +void RandomizeGroup(const std::map>& map, SeqType type) { + std::vector values; + for (const auto& [value, seqData] : map) { + if (std::get<2>(seqData) & type) { + values.push_back(value); + } + } + Shuffle(values); + for (const auto& [defaultValue, seqData] : map) { + const auto& [name, sfxKey, seqType] = seqData; + const std::string cvarKey = "gSfxEditor_" + sfxKey; + if (seqType & type) { + // Only save authentic sequence CVars + if (((seqType & SEQ_BGM_CUSTOM) || seqType == SEQ_FANFARE) && defaultValue >= MAX_AUTHENTIC_SEQID) { + continue; + } + const int randomValue = values.back(); + CVarSetInteger(cvarKey.c_str(), randomValue); + values.pop_back(); + } + } +} + +void ResetGroup(const std::map>& map, SeqType type) { + for (const auto& [defaultValue, seqData] : map) { + const auto& [name, sfxKey, seqType] = seqData; + if (seqType == type) { + // Only save authentic sequence CVars + if (seqType == SEQ_FANFARE && defaultValue >= MAX_AUTHENTIC_SEQID) { + continue; + } + const std::string cvarKey = "gSfxEditor_" + sfxKey; + CVarSetInteger(cvarKey.c_str(), defaultValue); + } + } +} + void Draw_SfxTab(const std::string& tabId, const std::map>& map, SeqType type) { const std::string hiddenTabId = "##" + tabId; const std::string resetAllButton = "Reset All" + hiddenTabId; const std::string randomizeAllButton = "Randomize All" + hiddenTabId; if (ImGui::Button(resetAllButton.c_str())) { - for (const auto& [defaultValue, seqData] : map) { - const auto& [name, sfxKey, seqType] = seqData; - if (seqType == type) { - // Only save authentic sequence CVars - if (seqType == SEQ_FANFARE && defaultValue >= MAX_AUTHENTIC_SEQID) { - continue; - } - const std::string cvarKey = "gSfxEditor_" + sfxKey; - CVarSetInteger(cvarKey.c_str(), defaultValue); - } - } + ResetGroup(map, type); SohImGui::RequestCvarSaveOnNextTick(); if (type == SEQ_BGM_WORLD) { ReplayCurrentBGM(); @@ -226,26 +253,7 @@ void Draw_SfxTab(const std::string& tabId, const std::map values; - for (const auto& [value, seqData] : map) { - if (std::get<2>(seqData) & type) { - values.push_back(value); - } - } - Shuffle(values); - for (const auto& [defaultValue, seqData] : map) { - const auto& [name, sfxKey, seqType] = seqData; - const std::string cvarKey = "gSfxEditor_" + sfxKey; - if (seqType & type) { - // Only save authentic sequence CVars - if (((seqType & SEQ_BGM_CUSTOM) || seqType == SEQ_FANFARE) && defaultValue >= MAX_AUTHENTIC_SEQID) { - continue; - } - const int randomValue = values.back(); - CVarSetInteger(cvarKey.c_str(), randomValue); - values.pop_back(); - } - } + RandomizeGroup(map, type); SohImGui::RequestCvarSaveOnNextTick(); if (type == SEQ_BGM_WORLD) { ReplayCurrentBGM(); @@ -476,6 +484,26 @@ void InitSfxEditor() { SohImGui::AddWindow("Enhancements", "SFX Editor", DrawSfxEditor); } +std::vector allTypes = { SEQ_BGM_WORLD, SEQ_BGM_EVENT, SEQ_BGM_BATTLE, SEQ_OCARINA, SEQ_FANFARE, SEQ_INSTRUMENT, SEQ_SFX }; + +void SfxEditor_RandomizeAll() { + for (auto type : allTypes) { + RandomizeGroup(sfxEditorSequenceMap, type); + } + + SohImGui::RequestCvarSaveOnNextTick(); + ReplayCurrentBGM(); +} + +void SfxEditor_ResetAll() { + for (auto type : allTypes) { + ResetGroup(sfxEditorSequenceMap, type); + } + + SohImGui::RequestCvarSaveOnNextTick(); + ReplayCurrentBGM(); +} + extern "C" void SfxEditor_AddSequence(char *otrPath, uint16_t seqNum) { std::vector splitName = StringHelper::Split(std::string(otrPath), "/"); std::string fileName = splitName[splitName.size() - 1]; diff --git a/soh/soh/Enhancements/sfx-editor/SfxEditor.h b/soh/soh/Enhancements/sfx-editor/SfxEditor.h index 5b1cd39fd..1bbbe17e0 100644 --- a/soh/soh/Enhancements/sfx-editor/SfxEditor.h +++ b/soh/soh/Enhancements/sfx-editor/SfxEditor.h @@ -2,6 +2,9 @@ #include "stdint.h" void InitSfxEditor(); +void SfxEditor_RandomizeAll(); +void SfxEditor_ResetAll(); + #ifndef __cplusplus const char* SfxEditor_GetSequenceName(u16 seqId); void SfxEditor_AddSequence(char *otrPath, uint16_t seqNum);