From ea353af5c97d6c495428b048f3ae5171b8bf8592 Mon Sep 17 00:00:00 2001 From: Blockyyy <88585273+Blockyyy@users.noreply.github.com> Date: Sun, 27 Apr 2025 23:21:17 +0200 Subject: [PATCH] HOOK_ON_INSTANT_WARP (#747) * make HOOK_ON_WARP trigger on instant warps * oops * HOOK_ON_INSTANT_WARP * make the displacement a Vec3s, hooks.md * fix hooks.md oversight * get rid of this --- autogen/lua_definitions/constants.lua | 4 ++- docs/lua/constants.md | 3 ++- docs/lua/guides/hooks.md | 3 ++- src/game/level_update.c | 12 ++------- src/game/level_update.h | 1 - src/pc/lua/smlua_constants_autogen.c | 3 ++- src/pc/lua/smlua_hooks.c | 39 +++++++++++++++++++++++++++ src/pc/lua/smlua_hooks.h | 3 +++ 8 files changed, 53 insertions(+), 15 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 5af5f7745..4d9ca4ebe 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -7815,7 +7815,8 @@ HOOK_MARIO_OVERRIDE_GEOMETRY_INPUTS = 51 --- @type LuaHookedEventType HOOK_ON_INTERACTIONS = 52 --- @type LuaHookedEventType HOOK_ALLOW_FORCE_WATER_ACTION = 53 --- @type LuaHookedEventType HOOK_BEFORE_WARP = 54 --- @type LuaHookedEventType -HOOK_MAX = 55 --- @type LuaHookedEventType +HOOK_ON_INSTANT_WARP = 55 --- @type LuaHookedEventType +HOOK_MAX = 56 --- @type LuaHookedEventType --- @alias LuaHookedEventType --- | `HOOK_UPDATE` @@ -7873,6 +7874,7 @@ HOOK_MAX = 55 --- @type LuaHookedEventType --- | `HOOK_ON_INTERACTIONS` --- | `HOOK_ALLOW_FORCE_WATER_ACTION` --- | `HOOK_BEFORE_WARP` +--- | `HOOK_ON_INSTANT_WARP` --- | `HOOK_MAX` ACTION_HOOK_EVERY_FRAME = 0 --- @type LuaActionHookType diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 99c25891d..9191550a7 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -3441,7 +3441,8 @@ | HOOK_ON_INTERACTIONS | 52 | | HOOK_ALLOW_FORCE_WATER_ACTION | 53 | | HOOK_BEFORE_WARP | 54 | -| HOOK_MAX | 55 | +| HOOK_ON_INSTANT_WARP | 55 | +| HOOK_MAX | 56 | ### [enum LuaActionHookType](#LuaActionHookType) | Identifier | Value | diff --git a/docs/lua/guides/hooks.md b/docs/lua/guides/hooks.md index 6bbef3b7c..9da9e9012 100644 --- a/docs/lua/guides/hooks.md +++ b/docs/lua/guides/hooks.md @@ -146,7 +146,8 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | HOOK_MARIO_OVERRIDE_GEOMETRY_INPUTS | Called before running Mario's geometry input logic, return `false` to not run it. | [MarioState](../structs.md) m | | HOOK_ON_INTERACTIONS | Called when the Mario interactions are processed | [MarioState](../structs.md#MarioState) mario | | HOOK_ALLOW_FORCE_WATER_ACTION | Called when executing a non-water action while under the water's surface, or vice versa. Return `false` to prevent the player from being forced out of the action at the water's surface | [MarioState](../structs.md#MarioState) mario, `boolean` isInWaterAction | -| HOOK_BEFORE_WARP | Called before the local player warps. Return a table with `destLevel`, `destArea`, `destWarpNode`, and `arg` to override the warp | `integer` destLevel, `integer` destArea, `integer` destWarpNode, `integer` arg | +| HOOK_BEFORE_WARP | Called before the local player warps. Return a table with `destLevel`, `destArea`, `destAreaWarpNode`, and `arg` to override the warp | `integer` destLevel, `integer` destArea, `integer` destAreaWarpNode, `integer` arg | +| HOOK_ON_INSTANT_WARP | Called when the local player goes through an instant warp.| `integer` area, `integer` id, `Vec3s` displacement| ### Parameters diff --git a/src/game/level_update.c b/src/game/level_update.c index e8ca64d7a..20262587f 100644 --- a/src/game/level_update.c +++ b/src/game/level_update.c @@ -56,7 +56,6 @@ struct SavedWarpValues gReceiveWarp = { 0 }; extern s8 sReceivedLoadedActNum; -u8 gRejectInstantWarp = 0; u16 gFanFareDebounce = 0; s16 gChangeLevel = -1; @@ -647,10 +646,6 @@ void check_instant_warp(void) { s16 cameraAngle; struct Surface *floor; - if (gRejectInstantWarp > 0) { - gRejectInstantWarp--; - } - if (gCurrLevelNum == LEVEL_CASTLE && save_file_get_total_star_count(gCurrSaveFileNum - 1, COURSE_MIN - 1, COURSE_MAX - 1) >= gLevelValues.infiniteStairsRequirement) { return; @@ -661,11 +656,6 @@ void check_instant_warp(void) { if (index >= INSTANT_WARP_INDEX_START && index < INSTANT_WARP_INDEX_STOP && gCurrentArea->instantWarps != NULL) { struct InstantWarp *warp = &gCurrentArea->instantWarps[index]; if (warp->id != 0) { - if (gRejectInstantWarp > 0) { - vec3f_copy(gMarioStates[0].pos, gMarioStates[0].nonInstantWarpPos); - //vec3f_mul(gMarioStates[0].vel, -0.8f); - return; - } mario_drop_held_object(&gMarioStates[0]); u8 changeOfArea = (gCurrAreaIndex != warp->area); @@ -695,6 +685,8 @@ void check_instant_warp(void) { skip_camera_interpolation(); gMarioStates[0].area->camera->yaw = cameraAngle; + smlua_call_event_hooks_instant_warp_params(HOOK_ON_INSTANT_WARP, warp->area, warp->id, warp->displacement); + return; } } diff --git a/src/game/level_update.h b/src/game/level_update.h index 6455a1db7..1e653fe1a 100644 --- a/src/game/level_update.h +++ b/src/game/level_update.h @@ -131,7 +131,6 @@ struct SavedWarpValues { extern struct WarpDest sWarpDest; extern s8 sWarpCheckpointActive; -extern u8 gRejectInstantWarp; extern u16 gFanFareDebounce; extern s16 D_80339EE0; diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 19d8ac6bb..eb51f92ca 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -3288,7 +3288,8 @@ char gSmluaConstants[] = "" "HOOK_ON_INTERACTIONS=52\n" "HOOK_ALLOW_FORCE_WATER_ACTION=53\n" "HOOK_BEFORE_WARP=54\n" -"HOOK_MAX=55\n" +"HOOK_ON_INSTANT_WARP=55\n" +"HOOK_MAX=56\n" "ACTION_HOOK_EVERY_FRAME=0\n" "ACTION_HOOK_GRAVITY=1\n" "ACTION_HOOK_MAX=2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index de496ac1e..2953ca061 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -656,6 +656,45 @@ void smlua_call_event_hooks_warp_params(enum LuaHookedEventType hookType, u8 typ } } +void smlua_call_event_hooks_instant_warp_params(enum LuaHookedEventType hookType, u8 area, u8 warpId, Vec3s displacement) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + 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]); + + // push params + lua_pushinteger(L, area); + lua_pushinteger(L, warpId); + + lua_newtable(L); + int tbl = lua_gettop(L); + + lua_pushstring(L, "x"); + lua_pushinteger(L, displacement[0]); + lua_settable(L, tbl); + + lua_pushstring(L, "y"); + lua_pushinteger(L, displacement[1]); + lua_settable(L, tbl); + + lua_pushstring(L, "z"); + lua_pushinteger(L, displacement[2]); + lua_settable(L, tbl); + + // call the callback + if (0 != smlua_call_hook(L, 3, 0, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u", hookType); + continue; + } + + lua_settop(L, prevTop); + } +} + void smlua_call_event_hooks_int_params_ret_string(enum LuaHookedEventType hookType, s32 param, char** returnValue) { 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 8314d7e4e..18bca97b9 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -67,6 +67,7 @@ enum LuaHookedEventType { HOOK_ON_INTERACTIONS, HOOK_ALLOW_FORCE_WATER_ACTION, HOOK_BEFORE_WARP, + HOOK_ON_INSTANT_WARP, HOOK_MAX, }; @@ -126,6 +127,7 @@ static const char* LuaHookedEventTypeName[] = { "HOOK_ON_INTERACTIONS", "HOOK_ALLOW_FORCE_WATER_ACTION", "HOOK_BEFORE_WARP", + "HOOK_ON_INSTANT_WARP" "HOOK_MAX" }; @@ -208,6 +210,7 @@ void smlua_call_event_hooks_graph_node_and_int_param(enum LuaHookedEventType hoo void smlua_call_event_hooks_on_seq_load(enum LuaHookedEventType hookType, u32 player, u32 seqId, s32 loadAsync, s16* returnValue); void smlua_call_event_hooks_before_warp(enum LuaHookedEventType hookType, s16 *destLevel, s16 *destArea, s16 *destWarpNode, s32 *arg); void smlua_call_event_hooks_warp_params(enum LuaHookedEventType hookType, u8 type, s16 levelNum, u8 areaIdx, u8 nodeId, u32 arg); +void smlua_call_event_hooks_instant_warp_params(enum LuaHookedEventType hookType, u8 area, u8 warpId, Vec3s displacement); const char *smlua_call_event_hooks_int_ret_bool_and_string(enum LuaHookedEventType hookType, s32 param, bool* returnValue); void smlua_call_event_hooks_string_param(enum LuaHookedEventType hookType, const char* string);