Skip to content

Commit

Permalink
Move a bunch of things out of the main rando.cpp file
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettjoecox committed Sep 28, 2024
1 parent 0c4c6f2 commit 0a70999
Show file tree
Hide file tree
Showing 21 changed files with 437 additions and 332 deletions.
3 changes: 2 additions & 1 deletion mm/2s2h/BenPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ CrowdControl* CrowdControl::Instance;
#include "2s2h/DeveloperTools/DebugConsole.h"
#include "2s2h/DeveloperTools/DeveloperTools.h"
#include "2s2h/Rando/Rando.h"
#include "2s2h/Rando/Spoiler/Spoiler.h"
#include "2s2h/SaveManager/SaveManager.h"

// Resource Types/Factories
Expand Down Expand Up @@ -422,7 +423,7 @@ void Ben_ProcessDroppedFiles(std::string filePath) {
}

if (!handled) {
handled = Rando::HandleFileDropped(filePath);
handled = Rando::Spoiler::HandleFileDropped(filePath);
}

// if (!handled) {
Expand Down
46 changes: 38 additions & 8 deletions mm/2s2h/Rando/ActorBehavior/ActorBehavior.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,41 @@
#include "ActorBehavior.h"

void Rando::InitActorBehavior(bool isRando) {
ActorBehavior::InitDmStkBehavior(isRando);
ActorBehavior::InitEnBoxBehavior(isRando);
ActorBehavior::InitEnElfgrpBehavior(isRando);
ActorBehavior::InitEnElforgBehavior(isRando);
ActorBehavior::InitEnItem00Behavior(isRando);
ActorBehavior::InitEnSiBehavior(isRando);
ActorBehavior::InitItemBHeartBehavior(isRando);
extern "C" {
#include "variables.h"
}

void MiscVanillaBehaviorHandler(GIVanillaBehavior id, bool* should, void* optionalArg) {
switch (id) {
case GI_VB_GIVE_ITEM_FROM_GURUGURU:
case GI_VB_GIVE_ITEM_FROM_SCRIPT:
case GI_VB_GIVE_ITEM_FROM_SWORDSMAN:
*should = false;
break;
}
}

void Rando::ActorBehavior::Init() {
}

void Rando::ActorBehavior::OnFileLoad() {
Rando::ActorBehavior::InitDmStkBehavior();
Rando::ActorBehavior::InitEnBoxBehavior();
Rando::ActorBehavior::InitEnElfgrpBehavior();
Rando::ActorBehavior::InitEnElforgBehavior();
Rando::ActorBehavior::InitEnItem00Behavior();
Rando::ActorBehavior::InitEnSiBehavior();
Rando::ActorBehavior::InitItemBHeartBehavior();

static uint32_t onVanillaBehaviorHook = 0;

GameInteractor::Instance->UnregisterGameHook<GameInteractor::ShouldVanillaBehavior>(onVanillaBehaviorHook);

onVanillaBehaviorHook = 0;

if (!IS_RANDO) {
return;
}

onVanillaBehaviorHook =
GameInteractor::Instance->RegisterGameHook<GameInteractor::ShouldVanillaBehavior>(MiscVanillaBehaviorHandler);
}
19 changes: 10 additions & 9 deletions mm/2s2h/Rando/ActorBehavior/ActorBehavior.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

namespace Rando {

void InitActorBehavior(bool isRando);

namespace ActorBehavior {

void InitDmStkBehavior(bool isRando);
void InitEnBoxBehavior(bool isRando);
void InitEnElfgrpBehavior(bool isRando);
void InitEnElforgBehavior(bool isRando);
void InitEnItem00Behavior(bool isRando);
void InitEnSiBehavior(bool isRando);
void InitItemBHeartBehavior(bool isRando);
void Init();
void OnFileLoad();

void InitDmStkBehavior();
void InitEnBoxBehavior();
void InitEnElfgrpBehavior();
void InitEnElforgBehavior();
void InitEnItem00Behavior();
void InitEnSiBehavior();
void InitItemBHeartBehavior();

} // namespace ActorBehavior

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/DmStk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void DmChar02_UpdateCustom(Actor* actor, PlayState* play) {
DmChar02_PlaySfxForCutscenes(dmChar02, play);
}

void Rando::ActorBehavior::InitDmStkBehavior(bool isRando) {
void Rando::ActorBehavior::InitDmStkBehavior() {
static uint32_t onActorInit = 0;
static uint32_t shouldHookId1 = 0;
static uint32_t shouldHookId2 = 0;
Expand All @@ -45,7 +45,7 @@ void Rando::ActorBehavior::InitDmStkBehavior(bool isRando) {
shouldHookId3 = 0;
shouldHookId4 = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/EnBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void func_80837C78_override(PlayState* play, Player* player) {

// This simply prevents the player from getting an item from the chest, but still
// plays the chest opening animation and ensure the treasure chest flag is set
void Rando::ActorBehavior::InitEnBoxBehavior(bool isRando) {
void Rando::ActorBehavior::InitEnBoxBehavior() {
static uint32_t shouldHook1Id = 0;
static uint32_t shouldHook2Id = 0;
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::ShouldVanillaBehavior>(shouldHook1Id);
Expand All @@ -33,7 +33,7 @@ void Rando::ActorBehavior::InitEnBoxBehavior(bool isRando) {
shouldHook1Id = 0;
shouldHook2Id = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/EnElfgrp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extern "C" {
#include "overlays/actors/ovl_En_Elfgrp/z_en_elfgrp.h"
}

void Rando::ActorBehavior::InitEnElfgrpBehavior(bool isRando) {
void Rando::ActorBehavior::InitEnElfgrpBehavior() {
static uint32_t shouldHook1Id = 0;
static uint32_t shouldHook2Id = 0;
static uint32_t shouldHook3Id = 0;
Expand All @@ -20,7 +20,7 @@ void Rando::ActorBehavior::InitEnElfgrpBehavior(bool isRando) {
shouldHook2Id = 0;
shouldHook3Id = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/EnElforg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void EnElforg_DrawCustom(Actor* thisx, PlayState* play) {
Rando::DrawItem(randoSaveCheck.randoItemId);
}

void Rando::ActorBehavior::InitEnElforgBehavior(bool isRando) {
void Rando::ActorBehavior::InitEnElforgBehavior() {
static uint32_t onActorInitHookId = 0;
static uint32_t shouldHook1Id = 0;
static uint32_t shouldHook2Id = 0;
Expand All @@ -55,7 +55,7 @@ void Rando::ActorBehavior::InitEnElforgBehavior(bool isRando) {
shouldHook2Id = 0;
shouldHook3Id = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/EnItem00.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void EnItem00_DrawCustomForFreestanding(Actor* thisx, PlayState* play) {
Rando::DrawItem(randoSaveCheck.randoItemId);
}

void Rando::ActorBehavior::InitEnItem00Behavior(bool isRando) {
void Rando::ActorBehavior::InitEnItem00Behavior() {
static uint32_t onActorInitHookId = 0;
static uint32_t shouldHookId = 0;
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnActorInit>(onActorInitHookId);
Expand All @@ -30,7 +30,7 @@ void Rando::ActorBehavior::InitEnItem00Behavior(bool isRando) {
onActorInitHookId = 0;
shouldHookId = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/EnSi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void EnSi_DrawCustom(Actor* thisx, PlayState* play) {
Rando::DrawItem(randoSaveCheck.randoItemId);
}

void Rando::ActorBehavior::InitEnSiBehavior(bool isRando) {
void Rando::ActorBehavior::InitEnSiBehavior() {
static uint32_t onActorInitHookId = 0;
static uint32_t shouldHookId = 0;
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnActorInit>(onActorInitHookId);
Expand All @@ -31,7 +31,7 @@ void Rando::ActorBehavior::InitEnSiBehavior(bool isRando) {
onActorInitHookId = 0;
shouldHookId = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
4 changes: 2 additions & 2 deletions mm/2s2h/Rando/ActorBehavior/ItemBHeart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ void ItemBHeart_UpdateCustom(Actor* thisx, PlayState* play) {
}
}

void Rando::ActorBehavior::InitItemBHeartBehavior(bool isRando) {
void Rando::ActorBehavior::InitItemBHeartBehavior() {
static uint32_t onActorInitHookId = 0;
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnActorInit>(onActorInitHookId);

onActorInitHookId = 0;

if (!isRando) {
if (!IS_RANDO) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions mm/2s2h/Rando/Menu.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Rando/Rando.h"
#include "Rando/Spoiler/Spoiler.h"
#include <libultraship/libultraship.h>
#include "2s2h/BenGui/UIWidgets.hpp"
#include "BenPort.h"
Expand All @@ -7,12 +8,12 @@ void Rando::DrawMenu() {
if (UIWidgets::BeginMenu("Rando", UIWidgets::Colors::Green)) {
UIWidgets::CVarCheckbox("Enable Rando (Randomizes new files upon creation)", "gRando.Enabled");

if (UIWidgets::CVarCombobox("Seed", "gRando.SpoilerFileIndex", Rando::spoilerFileOptions)) {
if (UIWidgets::CVarCombobox("Seed", "gRando.SpoilerFileIndex", Rando::Spoiler::spoilerOptions)) {
if (CVarGetInteger("gRando.SpoilerFileIndex", 0) == 0) {
CVarSetString("gRando.SpoilerFile", "");
} else {
CVarSetString("gRando.SpoilerFile",
Rando::spoilerFileOptions[CVarGetInteger("gRando.SpoilerFileIndex", 0)].c_str());
Rando::Spoiler::spoilerOptions[CVarGetInteger("gRando.SpoilerFileIndex", 0)].c_str());
}
}

Expand Down
38 changes: 38 additions & 0 deletions mm/2s2h/Rando/MiscBehavior/CheckQueue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "MiscBehavior.h"
#include <libultraship/libultraship.h>

extern "C" {
#include "variables.h"
}

static bool queued = false;
void Rando::MiscBehavior::CheckQueue() {
if (queued) {
return;
}

for (auto& randoSaveCheck : RANDO_SAVE_CHECKS) {
if (randoSaveCheck.eligible && !randoSaveCheck.obtained) {
queued = true;

RandoItemId randoItemId = Rando::ConvertItem(randoSaveCheck.randoItemId);
GameInteractor::Instance->events.emplace_back(GIEventGiveItem{
.showGetItemCutscene = !CVarGetInteger("gEnhancements.Cutscenes.SkipGetItemCutscenes", 0),
.getItemText = Rando::StaticData::Items[randoItemId].name,
.drawItem = [randoItemId]() { Rando::DrawItem(randoItemId); },
.giveItem =
[&randoSaveCheck, randoItemId]() {
Rando::GiveItem(randoItemId);
randoSaveCheck.obtained = true;
queued = false;
} });
return;
}
}
}

void Rando::MiscBehavior::CheckQueueReset() {
queued = false;
GameInteractor::Instance->currentEvent = GIEventNone{};
GameInteractor::Instance->events.clear();
}
37 changes: 37 additions & 0 deletions mm/2s2h/Rando/MiscBehavior/MiscBehavior.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "MiscBehavior.h"

extern "C" {
#include "variables.h"
}

// Entry point for the module, run once on game boot
void Rando::MiscBehavior::Init() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnSaveInit>(Rando::MiscBehavior::OnFileCreate);
}

void Rando::MiscBehavior::OnFileLoad() {
static uint32_t onFlagSetHook = 0;
static uint32_t onSceneFlagSetHook = 0;
static uint32_t onPlayerUpdateHook = 0;

GameInteractor::Instance->UnregisterGameHook<GameInteractor::OnFlagSet>(onFlagSetHook);
GameInteractor::Instance->UnregisterGameHook<GameInteractor::OnSceneFlagSet>(onSceneFlagSetHook);
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::OnActorUpdate>(onPlayerUpdateHook);

onFlagSetHook = 0;
onSceneFlagSetHook = 0;
onPlayerUpdateHook = 0;

Rando::MiscBehavior::CheckQueueReset();

if (!IS_RANDO) {
return;
}

onFlagSetHook =
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnFlagSet>(Rando::MiscBehavior::OnFlagSet);
onSceneFlagSetHook =
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnSceneFlagSet>(Rando::MiscBehavior::OnSceneFlagSet);
onPlayerUpdateHook = GameInteractor::Instance->RegisterGameHookForID<GameInteractor::OnActorUpdate>(
ACTOR_PLAYER, [](Actor* actor) { Rando::MiscBehavior::CheckQueue(); });
}
23 changes: 23 additions & 0 deletions mm/2s2h/Rando/MiscBehavior/MiscBehavior.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef RANDO_MISC_BEHAVIOR_H
#define RANDO_MISC_BEHAVIOR_H

#include "Rando/Rando.h"

namespace Rando {

namespace MiscBehavior {

void Init();
void OnFileLoad();

void CheckQueue();
void CheckQueueReset();
void OnFileCreate(s16 fileNum);
void OnFlagSet(FlagType flagType, u32 flag);
void OnSceneFlagSet(s16 sceneId, FlagType flagType, u32 flag);

} // namespace MiscBehavior

} // namespace Rando

#endif
Loading

0 comments on commit 0a70999

Please sign in to comment.