From 957e03cd81234f9257942cf48f6b59c2214436a1 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sun, 10 Apr 2022 02:44:11 -0700 Subject: [PATCH] Added HOOK_GET_STAR_COLLECTION_DIALOG --- autogen/lua_definitions/constants.lua | 5 ++++- docs/lua/constants.md | 3 ++- docs/lua/hooks.md | 1 + src/game/mario_actions_cutscene.c | 4 ++++ src/pc/lua/smlua_constants_autogen.c | 3 ++- src/pc/lua/smlua_hooks.c | 27 +++++++++++++++++++++++++++ src/pc/lua/smlua_hooks.h | 3 +++ 7 files changed, 43 insertions(+), 3 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index f13105b55..e4e40d0cc 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -7721,7 +7721,10 @@ HOOK_ON_SYNC_OBJECT_UNLOAD = 15 HOOK_ON_PAUSE_EXIT = 16 --- @type LuaHookedEventType -HOOK_MAX = 17 +HOOK_GET_STAR_COLLECTION_DIALOG = 17 + +--- @type LuaHookedEventType +HOOK_MAX = 18 --- @class ModelExtendedId diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 4fd1d76b5..b3e7f786e 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -2700,7 +2700,8 @@ | HOOK_ON_OBJECT_UNLOAD | 14 | | HOOK_ON_SYNC_OBJECT_UNLOAD | 15 | | HOOK_ON_PAUSE_EXIT | 16 | -| HOOK_MAX | 17 | +| HOOK_GET_STAR_COLLECTION_DIALOG | 17 | +| HOOK_MAX | 18 | [:arrow_up_small:](#) diff --git a/docs/lua/hooks.md b/docs/lua/hooks.md index 329904bb4..559084614 100644 --- a/docs/lua/hooks.md +++ b/docs/lua/hooks.md @@ -103,6 +103,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | HOOK_ON_OBJECT_UNLOAD | Called when any object is unloaded | [Object](structs.md#Object) unloadedObject | | HOOK_ON_SYNC_OBJECT_UNLOAD | Called when any networked object is unloaded | [Object](structs.md#Object) unloadedObject | | HOOK_ON_PAUSE_EXIT | Called when the local player exits through the pause screen, return `false` to prevent the exit | `boolean` usedExitToCastle | +| HOOK_GET_STAR_COLLECTION_DIALOG | Called when the local player collects a star, return a [DialogId](constants.md#enum-DialogId) to show a message | None | ### Parameters diff --git a/src/game/mario_actions_cutscene.c b/src/game/mario_actions_cutscene.c index 306a34a17..0cbbd27a8 100644 --- a/src/game/mario_actions_cutscene.c +++ b/src/game/mario_actions_cutscene.c @@ -241,6 +241,10 @@ static void stub_is_textbox_active(u16 *a0) { s32 get_star_collection_dialog(struct MarioState *m) { s32 dialogID = 0; + if (smlua_call_event_hooks_ret_int(HOOK_GET_STAR_COLLECTION_DIALOG, &dialogID)) { + return dialogID; + } + for (s32 i = 0; i < ARRAY_COUNT(sStarsNeededForDialog); i++) { s32 numStarsRequired = sStarsNeededForDialog[i]; if (m->prevNumStarsForDialog < numStarsRequired && m->numStars >= numStarsRequired) { diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index fcd938b2f..056f13a90 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -2755,7 +2755,8 @@ char gSmluaConstants[] = "" "HOOK_ON_OBJECT_UNLOAD = 14\n" "HOOK_ON_SYNC_OBJECT_UNLOAD = 15\n" "HOOK_ON_PAUSE_EXIT = 16\n" -"HOOK_MAX = 17\n" +"HOOK_GET_STAR_COLLECTION_DIALOG = 17\n" +"HOOK_MAX = 18\n" "E_MODEL_NONE = 0\n" "E_MODEL_MARIO = 1\n" "E_MODEL_SMOKE = 2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index cf8e5076c..a31d47344 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -264,6 +264,33 @@ void smlua_call_event_hooks_object_param(enum LuaHookedEventType hookType, struc } } +bool smlua_call_event_hooks_ret_int(enum LuaHookedEventType hookType, s32* returnValue) { + lua_State* L = gLuaState; + if (L == NULL) { return false; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + s32 prevTop = lua_gettop(L); + + // push the callback onto the stack + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + + // call the callback + if (0 != smlua_call_hook(L, 0, 1, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); + continue; + } + + // output the return value + if (lua_type(L, -1) == LUA_TNUMBER) { + *returnValue = smlua_to_integer(L, -1); + } + lua_settop(L, prevTop); + return true; + } + return false; +} + void smlua_call_event_hooks_network_player_param(enum LuaHookedEventType hookType, struct NetworkPlayer* np) { lua_State* L = gLuaState; if (L == NULL) { return; } diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index ef1e623aa..778928556 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -22,6 +22,7 @@ enum LuaHookedEventType { HOOK_ON_OBJECT_UNLOAD, HOOK_ON_SYNC_OBJECT_UNLOAD, HOOK_ON_PAUSE_EXIT, + HOOK_GET_STAR_COLLECTION_DIALOG, HOOK_MAX, }; @@ -43,6 +44,7 @@ static char* LuaHookedEventTypeName[] = { "HOOK_ON_OBJECT_UNLOAD", "HOOK_ON_SYNC_OBJECT_UNLOAD", "HOOK_ON_PAUSE_EXIT", + "HOOK_GET_STAR_COLLECTION_DIALOG", "HOOK_MAX" }; @@ -56,6 +58,7 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc void smlua_call_event_hooks_mario_params_ret_bool(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2, bool* returnValue); void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool interactValue); void smlua_call_event_hooks_object_param(enum LuaHookedEventType hookType, struct Object* obj); +bool smlua_call_event_hooks_ret_int(enum LuaHookedEventType hookType, s32* returnValue); enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior); const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);