Skip to content

Commit

Permalink
Port over ShipInit from 2Ship (HarbourMasters#4756)
Browse files Browse the repository at this point in the history
* Port over ShipInit and transform Moon Jump as example

* Clean up moon jump

* Updated moon jump structure & cvar defines
  • Loading branch information
aMannus authored Jan 5, 2025
1 parent f0b02d6 commit f8fa441
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 29 deletions.
26 changes: 26 additions & 0 deletions soh/soh/Enhancements/Cheats/MoonJump.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <libultraship/bridge.h>
#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h"
#include "soh/ShipInit.hpp"

extern "C" {
#include "macros.h"
extern PlayState* gPlayState;
}

#define CVAR_MOON_JUMP_NAME "gCheats.MoonJumpOnL"
#define CVAR_MOON_JUMP_DEFAULT 0
#define CVAR_MOON_JUMP_VALUE CVarGetInteger(CVAR_MOON_JUMP_NAME, CVAR_MOON_JUMP_DEFAULT)

void OnPlayerUpdateMoonJump() {
Player* player = GET_PLAYER(gPlayState);

if (player != nullptr && CHECK_BTN_ANY(gPlayState->state.input[0].cur.button, BTN_L)) {
player->actor.velocity.y = 6.34375f;
}
}

void RegisterMoonJump() {
COND_HOOK(OnPlayerUpdate, CVAR_MOON_JUMP_VALUE, OnPlayerUpdateMoonJump);
}

static RegisterShipInitFunc initFunc(RegisterMoonJump, { CVAR_MOON_JUMP_NAME });
40 changes: 34 additions & 6 deletions soh/soh/Enhancements/game-interactor/GameInteractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,43 @@ struct HookInfo {
#define GET_CURRENT_REGISTERING_INFO(type) HookRegisteringInfo{}
#endif

#define REGISTER_VB_SHOULD(flag, body) \
#define REGISTER_VB_SHOULD(flag, body) \
GameInteractor::Instance->RegisterGameHookForID<GameInteractor::OnVanillaBehavior>( \
flag, [](GIVanillaBehavior _, bool* should, va_list _originalArgs) { \
va_list args; \
va_copy(args, _originalArgs); \
body; \
va_end(args); \
flag, [](GIVanillaBehavior _, bool* should, va_list _originalArgs) { \
va_list args; \
va_copy(args, _originalArgs); \
body; \
va_end(args); \
})

#define COND_HOOK(hookType, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHook<GameInteractor::hookType>(hookId); \
hookId = 0; \
if (condition) { \
hookId = GameInteractor::Instance->RegisterGameHook<GameInteractor::hookType>(body); \
} \
}
#define COND_ID_HOOK(hookType, id, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::hookType>(hookId); \
hookId = 0; \
if (condition) { \
hookId = GameInteractor::Instance->RegisterGameHookForID<GameInteractor::hookType>(id, body); \
} \
}
#define COND_VB_SHOULD(id, condition, body) \
{ \
static HOOK_ID hookId = 0; \
GameInteractor::Instance->UnregisterGameHookForID<GameInteractor::ShouldVanillaBehavior>(hookId); \
hookId = 0; \
if (condition) { \
hookId = REGISTER_VB_SHOULD(id, body); \
} \
}

class GameInteractor {
public:
static GameInteractor* Instance;
Expand Down
16 changes: 0 additions & 16 deletions soh/soh/Enhancements/mods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,6 @@ void RegisterInfiniteNayrusLove() {
});
}

void RegisterMoonJumpOnL() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>([]() {
if (!GameInteractor::IsSaveLoaded(true)) return;

if (CVarGetInteger(CVAR_CHEAT("MoonJumpOnL"), 0) != 0) {
Player* player = GET_PLAYER(gPlayState);

if (CHECK_BTN_ANY(gPlayState->state.input[0].cur.button, BTN_L)) {
player->actor.velocity.y = 6.34375f;
}
}
});
}


void RegisterInfiniteISG() {
GameInteractor::Instance->RegisterGameHook<GameInteractor::OnGameFrameUpdate>([]() {
if (!GameInteractor::IsSaveLoaded(true)) return;
Expand Down Expand Up @@ -1473,7 +1458,6 @@ void InitMods() {
RegisterInfiniteAmmo();
RegisterInfiniteMagic();
RegisterInfiniteNayrusLove();
RegisterMoonJumpOnL();
RegisterInfiniteISG();
RegisterEzQPA();
RegisterUnrestrictedItems();
Expand Down
2 changes: 2 additions & 0 deletions soh/soh/OTRGlobals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ Sail* Sail::Instance;
#include "soh/resource/importer/BackgroundFactory.h"

#include "soh/config/ConfigUpdaters.h"
#include "soh/ShipInit.hpp"

extern "C" {
#include "src/overlays/actors/ovl_En_Dns/z_en_dns.h"
Expand Down Expand Up @@ -1150,6 +1151,7 @@ extern "C" void InitOTR() {
conf->RunVersionUpdates();

SohGui::SetupGuiElements();
ShipInit::InitAll();
AudioCollection::Instance = new AudioCollection();
ActorDB::Instance = new ActorDB();
#ifdef __APPLE__
Expand Down
43 changes: 43 additions & 0 deletions soh/soh/ShipInit.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef SHIP_INIT_HPP
#define SHIP_INIT_HPP

#ifdef __cplusplus

#include <vector>
#include <set>
#include <unordered_map>
#include <functional>

struct ShipInit {
static std::unordered_map<std::string, std::vector<std::function<void()>>>& GetAll() {
static std::unordered_map<std::string, std::vector<std::function<void()>>> shipInitFuncs;
return shipInitFuncs;
}

static void InitAll() {
ShipInit::Init("*");
}

static void Init(const std::string& path) {
auto& shipInitFuncs = ShipInit::GetAll();
for (const auto& initFunc : shipInitFuncs[path]) {
initFunc();
}
}
};

struct RegisterShipInitFunc {
RegisterShipInitFunc(std::function<void()> initFunc, const std::set<std::string>& updatePaths = {}) {
auto& shipInitFuncs = ShipInit::GetAll();

shipInitFuncs["*"].push_back(initFunc);

for (const auto& path : updatePaths) {
shipInitFuncs[path].push_back(initFunc);
}
}
};

#endif // __cplusplus

#endif // SHIP_INIT_HPP
2 changes: 2 additions & 0 deletions soh/soh/UIWidgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ namespace UIWidgets {
if (CustomCheckbox(text, &val, disabled, disabledGraphic)) {
CVarSetInteger(cvarName, val);
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
ShipInit::Init(cvarName);
changed = true;
}

Expand Down Expand Up @@ -298,6 +299,7 @@ namespace UIWidgets {
selected = i;
changed = true;
Ship::Context::GetInstance()->GetWindow()->GetGui()->SaveConsoleVariablesNextFrame();
ShipInit::Init(cvarName);
}
}
}
Expand Down
8 changes: 1 addition & 7 deletions soh/soh/UIWidgets.hpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// UIWidgets.hpp
// soh
//
// Created by David Chavez on 25.08.22.
//

#ifndef UIWidgets_hpp
#define UIWidgets_hpp

Expand All @@ -17,6 +10,7 @@
#define IMGUI_DEFINE_MATH_OPERATORS
#endif
#include <imgui.h>
#include "soh/ShipInit.hpp"

namespace UIWidgets {

Expand Down

0 comments on commit f8fa441

Please sign in to comment.