From 31c2e23c48f26b78cb9b08df0196ca03274f3a1c Mon Sep 17 00:00:00 2001 From: PeachyPeach <72323920+PeachyPeachSM64@users.noreply.github.com> Date: Fri, 18 Aug 2023 18:55:39 +0200 Subject: [PATCH] allocate_mario_action fix and improvement (#453) --- autogen/lua_definitions/constants.lua | 9 +++++++++ docs/lua/constants.md | 3 +++ include/sm64.h | 4 ++++ src/pc/lua/smlua_constants_autogen.c | 3 +++ src/pc/lua/smlua_hooks.c | 7 ++++--- src/pc/lua/smlua_hooks.h | 2 +- src/pc/lua/utils/smlua_misc_utils.c | 9 +++++++-- 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index a20b1634..887e8a12 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -7597,6 +7597,9 @@ ACT_IDLE = 0x0C400201 --- @type integer ACT_ID_MASK = 0x000001FF +--- @type integer +ACT_INDEX_MASK = (ACT_ID_MASK & ~ACT_GROUP_MASK) + --- @type integer ACT_INTRO_CUTSCENE = 0x04001301 @@ -7672,6 +7675,12 @@ ACT_METAL_WATER_WALKING = 0x000044F2 --- @type integer ACT_MOVE_PUNCHING = 0x00800457 +--- @type integer +ACT_NUM_ACTIONS_PER_GROUP = (ACT_INDEX_MASK + 1) + +--- @type integer +ACT_NUM_GROUPS = ((ACT_GROUP_MASK >> 6) + 1) + --- @type integer ACT_PANTING = 0x0C400205 diff --git a/docs/lua/constants.md b/docs/lua/constants.md index f1a0a4d4..85cdd591 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -2714,6 +2714,7 @@ - ACT_HOLD_WATER_JUMP - ACT_IDLE - ACT_ID_MASK +- ACT_INDEX_MASK - ACT_INTRO_CUTSCENE - ACT_IN_CANNON - ACT_IN_QUICKSAND @@ -2739,6 +2740,8 @@ - ACT_METAL_WATER_STANDING - ACT_METAL_WATER_WALKING - ACT_MOVE_PUNCHING +- ACT_NUM_ACTIONS_PER_GROUP +- ACT_NUM_GROUPS - ACT_PANTING - ACT_PICKING_UP - ACT_PICKING_UP_BOWSER diff --git a/include/sm64.h b/include/sm64.h index 02e1490f..80bdd25b 100644 --- a/include/sm64.h +++ b/include/sm64.h @@ -151,6 +151,10 @@ #define ACT_GROUP_AUTOMATIC /* 0x00000140 */ (5 << 6) #define ACT_GROUP_OBJECT /* 0x00000180 */ (6 << 6) +#define ACT_INDEX_MASK (ACT_ID_MASK & ~ACT_GROUP_MASK) +#define ACT_NUM_GROUPS ((ACT_GROUP_MASK >> 6) + 1) +#define ACT_NUM_ACTIONS_PER_GROUP (ACT_INDEX_MASK + 1) + #define ACT_FLAG_STATIONARY /* 0x00000200 */ (1 << 9) #define ACT_FLAG_MOVING /* 0x00000400 */ (1 << 10) #define ACT_FLAG_AIR /* 0x00000800 */ (1 << 11) diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 70fe6058..6f747c71 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -2678,6 +2678,9 @@ char gSmluaConstants[] = "" "ACT_GROUP_CUTSCENE = (4 << 6)\n" "ACT_GROUP_AUTOMATIC = (5 << 6)\n" "ACT_GROUP_OBJECT = (6 << 6)\n" +"ACT_INDEX_MASK = (ACT_ID_MASK & ~ACT_GROUP_MASK)\n" +"ACT_NUM_GROUPS = ((ACT_GROUP_MASK >> 6) + 1)\n" +"ACT_NUM_ACTIONS_PER_GROUP = (ACT_INDEX_MASK + 1)\n" "ACT_FLAG_STATIONARY = (1 << 9)\n" "ACT_FLAG_MOVING = (1 << 10)\n" "ACT_FLAG_AIR = (1 << 11)\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index a253a079..3ff71793 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -1,4 +1,5 @@ #include "smlua.h" +#include "sm64.h" #include "behavior_commands.h" #include "pc/mods/mod.h" #include "src/game/object_list_processor.h" @@ -851,11 +852,11 @@ struct LuaHookedMarioAction { struct Mod* mod; }; -#define MAX_HOOKED_ACTIONS 128 +#define MAX_HOOKED_ACTIONS (ACT_NUM_GROUPS * ACT_NUM_ACTIONS_PER_GROUP) static struct LuaHookedMarioAction sHookedMarioActions[MAX_HOOKED_ACTIONS] = { 0 }; static int sHookedMarioActionsCount = 0; -u32 gLuaMarioActionIndex = 0; +u32 gLuaMarioActionIndex[ACT_NUM_GROUPS] = { 0 }; int smlua_hook_mario_action(lua_State* L) { if (L == NULL) { return 0; } @@ -1564,7 +1565,7 @@ void smlua_clear_hooks(void) { hooked->mod = NULL; } sHookedBehaviorsCount = 0; - gLuaMarioActionIndex = 0; + memset(gLuaMarioActionIndex, 0, sizeof(gLuaMarioActionIndex)); } void smlua_bind_hooks(void) { diff --git a/src/pc/lua/smlua_hooks.h b/src/pc/lua/smlua_hooks.h index 2980b4d3..38f1d4e1 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -98,7 +98,7 @@ static const char* LuaActionHookTypeArgName[] = { "max (dummy)", }; -extern u32 gLuaMarioActionIndex; +extern u32 gLuaMarioActionIndex[]; int smlua_hook_custom_bhv(BehaviorScript *bhvScript, const char *bhvName); diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index 770d2a96..fbdda7ea 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -441,8 +441,13 @@ bool is_transition_playing(void) { /// u32 allocate_mario_action(u32 actFlags) { - actFlags = actFlags & (~((u32)0x3F)); - return actFlags | ACT_FLAG_CUSTOM_ACTION | gLuaMarioActionIndex++; + u32 actGroup = ((actFlags & ACT_GROUP_MASK) >> 6); + u32 actIndex = gLuaMarioActionIndex[actGroup]++; + if (actIndex >= ACT_NUM_ACTIONS_PER_GROUP) { + LOG_LUA("Cannot allocate more actions for group %u", actGroup); + return 0; + } + return (actFlags & ~ACT_INDEX_MASK) | ACT_FLAG_CUSTOM_ACTION | actIndex; } ///