From e536d140bb3fd819673c27c62d4aac4439e8a56c Mon Sep 17 00:00:00 2001 From: MysterD Date: Tue, 15 Feb 2022 22:21:31 -0800 Subject: [PATCH] Added HOOK_ON_INTERACT to Lua API --- autogen/convert_constants.py | 1 + docs/lua/constants.md | 83 +++++++++++++++++++++++++++- docs/lua/examples/hud.lua | 6 -- docs/lua/hooks.md | 1 + src/game/characters.c | 2 +- src/game/interaction.c | 3 + src/game/interaction.h | 67 +++++++++++----------- src/pc/lua/smlua_constants_autogen.c | 73 +++++++++++++++++++++++- src/pc/lua/smlua_hooks.c | 32 +++++++++++ src/pc/lua/smlua_hooks.h | 5 +- 10 files changed, 230 insertions(+), 43 deletions(-) diff --git a/autogen/convert_constants.py b/autogen/convert_constants.py index c1de199c8..39bbeed4e 100644 --- a/autogen/convert_constants.py +++ b/autogen/convert_constants.py @@ -17,6 +17,7 @@ in_files = [ "src/pc/network/network_player.h", "include/PR/os_cont.h", "src/game/interaction.c", + "src/game/interaction.h", "src/pc/djui/djui_hud_utils.h", ] diff --git a/docs/lua/constants.md b/docs/lua/constants.md index fdaea6a22..0a10b2ecf 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -11,6 +11,8 @@ - [HudUtilsResolution](#HudUtilsResolution) - [interaction.c](#interaction.c) - [InteractionFlag](#InteractionFlag) +- [interaction.h](#interaction.h) + - [InteractionType](#InteractionType) - [mario_animation_ids.h](#mario_animation_ids.h) - [MarioAnimID](#MarioAnimID) - [network_player.h](#network_player.h) @@ -784,6 +786,84 @@
+## [interaction.h](#interaction.h) +- ATTACK_FAST_ATTACK +- ATTACK_FROM_ABOVE +- ATTACK_FROM_BELOW +- ATTACK_GROUND_POUND_OR_TWIRL +- ATTACK_KICK_OR_TRIP +- ATTACK_PUNCH +- INT_STATUS_ATTACKED_MARIO +- INT_STATUS_ATTACK_MASK +- INT_STATUS_GRABBED_MARIO +- INT_STATUS_HIT_MINE +- INT_STATUS_HOOT_GRABBED_BY_MARIO +- INT_STATUS_INTERACTED +- INT_STATUS_MARIO_DROP_OBJECT +- INT_STATUS_MARIO_UNK1 +- INT_STATUS_MARIO_UNK2 +- INT_STATUS_MARIO_UNK4 +- INT_STATUS_MARIO_UNK5 +- INT_STATUS_MARIO_UNK6 +- INT_STATUS_MARIO_UNK7 +- INT_STATUS_STOP_RIDING +- INT_STATUS_TOUCHED_BOB_OMB +- INT_STATUS_TRAP_TURN +- INT_STATUS_WAS_ATTACKED +- INT_SUBTYPE_BIG_KNOCKBACK +- INT_SUBTYPE_DELAY_INVINCIBILITY +- INT_SUBTYPE_DROP_IMMEDIATELY +- INT_SUBTYPE_EATS_MARIO +- INT_SUBTYPE_FADING_WARP +- INT_SUBTYPE_GRABS_MARIO +- INT_SUBTYPE_GRAND_STAR +- INT_SUBTYPE_HOLDABLE_NPC +- INT_SUBTYPE_KICKABLE +- INT_SUBTYPE_NOT_GRABBABLE +- INT_SUBTYPE_NO_EXIT +- INT_SUBTYPE_NPC +- INT_SUBTYPE_SIGN +- INT_SUBTYPE_STAR_DOOR +- INT_SUBTYPE_TWIRL_BOUNCE + +### [enum InteractionType](#InteractionType) +| Identifier | Value | +| :--------- | :---- | +| INTERACT_HOOT | (1 << 0) | +| INTERACT_GRABBABLE | (1 << 1) | +| INTERACT_DOOR | (1 << 2) | +| INTERACT_DAMAGE | (1 << 3) | +| INTERACT_COIN | (1 << 4) | +| INTERACT_CAP | (1 << 5) | +| INTERACT_POLE | (1 << 6) | +| INTERACT_KOOPA | (1 << 7) | +| INTERACT_UNKNOWN_08 | (1 << 8) | +| INTERACT_BREAKABLE | (1 << 9) | +| INTERACT_STRONG_WIND | (1 << 10) | +| INTERACT_WARP_DOOR | (1 << 11) | +| INTERACT_STAR_OR_KEY | (1 << 12) | +| INTERACT_WARP | (1 << 13) | +| INTERACT_CANNON_BASE | (1 << 14) | +| INTERACT_BOUNCE_TOP | (1 << 15) | +| INTERACT_WATER_RING | (1 << 16) | +| INTERACT_BULLY | (1 << 17) | +| INTERACT_FLAME | (1 << 18) | +| INTERACT_KOOPA_SHELL | (1 << 19) | +| INTERACT_BOUNCE_TOP2 | (1 << 20) | +| INTERACT_MR_BLIZZARD | (1 << 21) | +| INTERACT_HIT_FROM_BELOW | (1 << 22) | +| INTERACT_TEXT | (1 << 23) | +| INTERACT_TORNADO | (1 << 24) | +| INTERACT_WHIRLPOOL | (1 << 25) | +| INTERACT_CLAM_OR_BUBBA | (1 << 26) | +| INTERACT_BBH_ENTRANCE | (1 << 27) | +| INTERACT_SNUFIT_BULLET | (1 << 28) | +| INTERACT_SHOCK | (1 << 29) | +| INTERACT_IGLOO_BARRIER | (1 << 30) | +| INTERACT_PLAYER | (1 << 31) | + +
+ ## [mario_animation_ids.h](#mario_animation_ids.h) ### [enum MarioAnimID](#MarioAnimID) @@ -1436,7 +1516,8 @@ | HOOK_ON_PLAYER_CONNECTED | 6 | | HOOK_ON_PLAYER_DISCONNECTED | 7 | | HOOK_ON_HUD_RENDER | 8 | -| HOOK_MAX | 9 | +| HOOK_ON_INTERACT | 9 | +| HOOK_MAX | 10 |
diff --git a/docs/lua/examples/hud.lua b/docs/lua/examples/hud.lua index 78ee2c208..1377ba801 100644 --- a/docs/lua/examples/hud.lua +++ b/docs/lua/examples/hud.lua @@ -120,10 +120,4 @@ function on_hud_render() test_rainbow_text() end -function on_mario_update(m) - if m.action == ACT_JUMP then - displaying = true - end -end - hook_event(HOOK_ON_HUD_RENDER, on_hud_render) diff --git a/docs/lua/hooks.md b/docs/lua/hooks.md index cf597c311..6c2299be8 100644 --- a/docs/lua/hooks.md +++ b/docs/lua/hooks.md @@ -52,6 +52,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | HOOK_ON_PVP_ATTACK | Called when one player attacks another | [MarioState](structs.md#MarioState) attacker, [MarioState](structs.md#MarioState) victim | | HOOK_ON_PLAYER_CONNECTED | Called when a player connects | [MarioState](structs.md#MarioState) connector | | HOOK_ON_PLAYER_DISCONNECTED | Called when a player disconnects | [MarioState](structs.md#MarioState) disconnector | +| HOOK_ON_INTERACT | Called when mario interacts with an object | [MarioState](structs.md#MarioState) interactor, [Object](structs.md#Object) interactee, [InteractType](constants.md#InteractType) interactType, bool interactValue | ### Parameters diff --git a/src/game/characters.c b/src/game/characters.c index 58f36b319..937d5e5e8 100644 --- a/src/game/characters.c +++ b/src/game/characters.c @@ -285,7 +285,7 @@ struct Character gCharacters[CT_MAX] = { .type = CT_WARIO, .name = "Wario", .hudHead = 'x', - .hudHeadTexture = texture_hud_char_wario_head, + .hudHeadTexture = { .texture = texture_hud_char_wario_head, .bitSize = 8, .width = 16, .height = 16 }, .cameraHudHead = GLYPH_CAM_WARIO_HEAD, .modelId = MODEL_WARIO, .capModelId = MODEL_WARIOS_CAP, diff --git a/src/game/interaction.c b/src/game/interaction.c index e4994cd23..4c272da7b 100644 --- a/src/game/interaction.c +++ b/src/game/interaction.c @@ -2124,7 +2124,10 @@ void mario_process_interactions(struct MarioState *m) { if (!(object->oInteractStatus & INT_STATUS_INTERACTED)) { if (sInteractionHandlers[i].handler(m, interactType, object)) { + smlua_call_event_hooks_interact_params(HOOK_ON_INTERACT, m, object, interactType, true); break; + } else { + smlua_call_event_hooks_interact_params(HOOK_ON_INTERACT, m, object, interactType, false); } } } diff --git a/src/game/interaction.h b/src/game/interaction.h index 2cca79250..baa9e2b7c 100644 --- a/src/game/interaction.h +++ b/src/game/interaction.h @@ -5,39 +5,40 @@ #include "types.h" -#define INTERACT_HOOT /* 0x00000001 */ (1 << 0) -#define INTERACT_GRABBABLE /* 0x00000002 */ (1 << 1) -#define INTERACT_DOOR /* 0x00000004 */ (1 << 2) -#define INTERACT_DAMAGE /* 0x00000008 */ (1 << 3) -#define INTERACT_COIN /* 0x00000010 */ (1 << 4) -#define INTERACT_CAP /* 0x00000020 */ (1 << 5) -#define INTERACT_POLE /* 0x00000040 */ (1 << 6) -#define INTERACT_KOOPA /* 0x00000080 */ (1 << 7) -#define INTERACT_UNKNOWN_08 /* 0x00000100 */ (1 << 8) -#define INTERACT_BREAKABLE /* 0x00000200 */ (1 << 9) -#define INTERACT_STRONG_WIND /* 0x00000400 */ (1 << 10) -#define INTERACT_WARP_DOOR /* 0x00000800 */ (1 << 11) -#define INTERACT_STAR_OR_KEY /* 0x00001000 */ (1 << 12) -#define INTERACT_WARP /* 0x00002000 */ (1 << 13) -#define INTERACT_CANNON_BASE /* 0x00004000 */ (1 << 14) -#define INTERACT_BOUNCE_TOP /* 0x00008000 */ (1 << 15) -#define INTERACT_WATER_RING /* 0x00010000 */ (1 << 16) -#define INTERACT_BULLY /* 0x00020000 */ (1 << 17) -#define INTERACT_FLAME /* 0x00040000 */ (1 << 18) -#define INTERACT_KOOPA_SHELL /* 0x00080000 */ (1 << 19) -#define INTERACT_BOUNCE_TOP2 /* 0x00100000 */ (1 << 20) -#define INTERACT_MR_BLIZZARD /* 0x00200000 */ (1 << 21) -#define INTERACT_HIT_FROM_BELOW /* 0x00400000 */ (1 << 22) -#define INTERACT_TEXT /* 0x00800000 */ (1 << 23) -#define INTERACT_TORNADO /* 0x01000000 */ (1 << 24) -#define INTERACT_WHIRLPOOL /* 0x02000000 */ (1 << 25) -#define INTERACT_CLAM_OR_BUBBA /* 0x04000000 */ (1 << 26) -#define INTERACT_BBH_ENTRANCE /* 0x08000000 */ (1 << 27) -#define INTERACT_SNUFIT_BULLET /* 0x10000000 */ (1 << 28) -#define INTERACT_SHOCK /* 0x20000000 */ (1 << 29) -#define INTERACT_IGLOO_BARRIER /* 0x40000000 */ (1 << 30) -#define INTERACT_PLAYER /* 0x80000000 */ (1 << 31) - +enum InteractionType { + INTERACT_HOOT = /* 0x00000001 */ (1 << 0), + INTERACT_GRABBABLE = /* 0x00000002 */ (1 << 1), + INTERACT_DOOR = /* 0x00000004 */ (1 << 2), + INTERACT_DAMAGE = /* 0x00000008 */ (1 << 3), + INTERACT_COIN = /* 0x00000010 */ (1 << 4), + INTERACT_CAP = /* 0x00000020 */ (1 << 5), + INTERACT_POLE = /* 0x00000040 */ (1 << 6), + INTERACT_KOOPA = /* 0x00000080 */ (1 << 7), + INTERACT_UNKNOWN_08 = /* 0x00000100 */ (1 << 8), + INTERACT_BREAKABLE = /* 0x00000200 */ (1 << 9), + INTERACT_STRONG_WIND = /* 0x00000400 */ (1 << 10), + INTERACT_WARP_DOOR = /* 0x00000800 */ (1 << 11), + INTERACT_STAR_OR_KEY = /* 0x00001000 */ (1 << 12), + INTERACT_WARP = /* 0x00002000 */ (1 << 13), + INTERACT_CANNON_BASE = /* 0x00004000 */ (1 << 14), + INTERACT_BOUNCE_TOP = /* 0x00008000 */ (1 << 15), + INTERACT_WATER_RING = /* 0x00010000 */ (1 << 16), + INTERACT_BULLY = /* 0x00020000 */ (1 << 17), + INTERACT_FLAME = /* 0x00040000 */ (1 << 18), + INTERACT_KOOPA_SHELL = /* 0x00080000 */ (1 << 19), + INTERACT_BOUNCE_TOP2 = /* 0x00100000 */ (1 << 20), + INTERACT_MR_BLIZZARD = /* 0x00200000 */ (1 << 21), + INTERACT_HIT_FROM_BELOW = /* 0x00400000 */ (1 << 22), + INTERACT_TEXT = /* 0x00800000 */ (1 << 23), + INTERACT_TORNADO = /* 0x01000000 */ (1 << 24), + INTERACT_WHIRLPOOL = /* 0x02000000 */ (1 << 25), + INTERACT_CLAM_OR_BUBBA = /* 0x04000000 */ (1 << 26), + INTERACT_BBH_ENTRANCE = /* 0x08000000 */ (1 << 27), + INTERACT_SNUFIT_BULLET = /* 0x10000000 */ (1 << 28), + INTERACT_SHOCK = /* 0x20000000 */ (1 << 29), + INTERACT_IGLOO_BARRIER = /* 0x40000000 */ (1 << 30), + INTERACT_PLAYER = /* 0x80000000 */ (1 << 31), +}; // INTERACT_WARP #define INT_SUBTYPE_FADING_WARP 0x00000001 diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index dded5437e..889f4dbdc 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -864,6 +864,76 @@ char gSmluaConstants[] = "" "INT_ANY_ATTACK = (INT_GROUND_POUND_OR_TWIRL | INT_PUNCH | INT_KICK | INT_TRIP | INT_SLIDE_KICK | INT_FAST_ATTACK_OR_SHELL | INT_HIT_FROM_ABOVE | INT_HIT_FROM_BELOW)\n" "INT_ATTACK_NOT_WEAK_FROM_ABOVE = (INT_GROUND_POUND_OR_TWIRL | INT_PUNCH | INT_KICK | INT_TRIP | INT_HIT_FROM_BELOW)\n" "INT_ATTACK_SLIDE = (INT_SLIDE_KICK | INT_FAST_ATTACK_OR_SHELL)\n" +"INTERACT_HOOT = (1 << 0)\n" +"INTERACT_GRABBABLE = (1 << 1)\n" +"INTERACT_DOOR = (1 << 2)\n" +"INTERACT_DAMAGE = (1 << 3)\n" +"INTERACT_COIN = (1 << 4)\n" +"INTERACT_CAP = (1 << 5)\n" +"INTERACT_POLE = (1 << 6)\n" +"INTERACT_KOOPA = (1 << 7)\n" +"INTERACT_UNKNOWN_08 = (1 << 8)\n" +"INTERACT_BREAKABLE = (1 << 9)\n" +"INTERACT_STRONG_WIND = (1 << 10)\n" +"INTERACT_WARP_DOOR = (1 << 11)\n" +"INTERACT_STAR_OR_KEY = (1 << 12)\n" +"INTERACT_WARP = (1 << 13)\n" +"INTERACT_CANNON_BASE = (1 << 14)\n" +"INTERACT_BOUNCE_TOP = (1 << 15)\n" +"INTERACT_WATER_RING = (1 << 16)\n" +"INTERACT_BULLY = (1 << 17)\n" +"INTERACT_FLAME = (1 << 18)\n" +"INTERACT_KOOPA_SHELL = (1 << 19)\n" +"INTERACT_BOUNCE_TOP2 = (1 << 20)\n" +"INTERACT_MR_BLIZZARD = (1 << 21)\n" +"INTERACT_HIT_FROM_BELOW = (1 << 22)\n" +"INTERACT_TEXT = (1 << 23)\n" +"INTERACT_TORNADO = (1 << 24)\n" +"INTERACT_WHIRLPOOL = (1 << 25)\n" +"INTERACT_CLAM_OR_BUBBA = (1 << 26)\n" +"INTERACT_BBH_ENTRANCE = (1 << 27)\n" +"INTERACT_SNUFIT_BULLET = (1 << 28)\n" +"INTERACT_SHOCK = (1 << 29)\n" +"INTERACT_IGLOO_BARRIER = (1 << 30)\n" +"INTERACT_PLAYER = (1 << 31)\n" +"INT_SUBTYPE_FADING_WARP = 0x00000001\n" +"INT_SUBTYPE_DELAY_INVINCIBILITY = 0x00000002\n" +"INT_SUBTYPE_BIG_KNOCKBACK = 0x00000008\n" +"INT_SUBTYPE_GRABS_MARIO = 0x00000004\n" +"INT_SUBTYPE_HOLDABLE_NPC = 0x00000010\n" +"INT_SUBTYPE_DROP_IMMEDIATELY = 0x00000040\n" +"INT_SUBTYPE_KICKABLE = 0x00000100\n" +"INT_SUBTYPE_NOT_GRABBABLE = 0x00000200\n" +"INT_SUBTYPE_STAR_DOOR = 0x00000020\n" +"INT_SUBTYPE_TWIRL_BOUNCE = 0x00000080\n" +"INT_SUBTYPE_NO_EXIT = 0x00000400\n" +"INT_SUBTYPE_GRAND_STAR = 0x00000800\n" +"INT_SUBTYPE_SIGN = 0x00001000\n" +"INT_SUBTYPE_NPC = 0x00004000\n" +"INT_SUBTYPE_EATS_MARIO = 0x00002000\n" +"ATTACK_PUNCH = 1\n" +"ATTACK_KICK_OR_TRIP = 2\n" +"ATTACK_FROM_ABOVE = 3\n" +"ATTACK_GROUND_POUND_OR_TWIRL = 4\n" +"ATTACK_FAST_ATTACK = 5\n" +"ATTACK_FROM_BELOW = 6\n" +"INT_STATUS_ATTACK_MASK = 0x000000FF\n" +"INT_STATUS_HOOT_GRABBED_BY_MARIO = (1 << 0)\n" +"INT_STATUS_MARIO_UNK1 = (1 << 1)\n" +"INT_STATUS_MARIO_UNK2 = (1 << 2)\n" +"INT_STATUS_MARIO_DROP_OBJECT = (1 << 3)\n" +"INT_STATUS_MARIO_UNK4 = (1 << 4)\n" +"INT_STATUS_MARIO_UNK5 = (1 << 5)\n" +"INT_STATUS_MARIO_UNK6 = (1 << 6)\n" +"INT_STATUS_MARIO_UNK7 = (1 << 7)\n" +"INT_STATUS_GRABBED_MARIO = (1 << 11)\n" +"INT_STATUS_ATTACKED_MARIO = (1 << 13)\n" +"INT_STATUS_WAS_ATTACKED = (1 << 14)\n" +"INT_STATUS_INTERACTED = (1 << 15)\n" +"INT_STATUS_TRAP_TURN = (1 << 20)\n" +"INT_STATUS_HIT_MINE = (1 << 21)\n" +"INT_STATUS_STOP_RIDING = (1 << 22)\n" +"INT_STATUS_TOUCHED_BOB_OMB = (1 << 23)\n" "MARIO_ANIM_SLOW_LEDGE_GRAB = 0\n" "MARIO_ANIM_FALL_OVER_BACKWARDS = 1\n" "MARIO_ANIM_BACKWARD_AIR_KB = 2\n" @@ -1487,7 +1557,8 @@ char gSmluaConstants[] = "" "HOOK_ON_PLAYER_CONNECTED = 6\n" "HOOK_ON_PLAYER_DISCONNECTED = 7\n" "HOOK_ON_HUD_RENDER = 8\n" -"HOOK_MAX = 9\n" +"HOOK_ON_INTERACT = 9\n" +"HOOK_MAX = 10\n" "SPTASK_STATE_NOT_STARTED = 0\n" "SPTASK_STATE_RUNNING = 1\n" "SPTASK_STATE_INTERRUPTED = 2\n" diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index 652a4263b..d2ef4df04 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -112,6 +112,38 @@ void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struc } } +void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool interactValue) { + lua_State* L = gLuaState; + if (L == NULL) { return; } + struct LuaHookedEvent* hook = &sHookedEvents[hookType]; + for (int i = 0; i < hook->count; i++) { + // push the callback onto the stack + lua_rawgeti(L, LUA_REGISTRYINDEX, hook->reference[i]); + + // push mario state + lua_getglobal(L, "gMarioStates"); + lua_pushinteger(L, m->playerIndex); + lua_gettable(L, -2); + lua_remove(L, -2); + + // push object + smlua_push_object(L, LOT_OBJECT, obj); + + // push interact type + lua_pushinteger(L, interactType); + + // push interact value + lua_pushboolean(L, interactValue); + + // call the callback + if (0 != lua_pcall(L, 4, 0, 0)) { + LOG_LUA("Failed to call the callback: %u, %s", hookType, lua_tostring(L, -1)); + smlua_logline(); + continue; + } + } +} + 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 33f01543c..d3f94ab00 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -13,6 +13,7 @@ enum LuaHookedEventType { HOOK_ON_PLAYER_CONNECTED, HOOK_ON_PLAYER_DISCONNECTED, HOOK_ON_HUD_RENDER, + HOOK_ON_INTERACT, HOOK_MAX, }; @@ -26,12 +27,14 @@ static char* LuaHookedEventTypeName[] = { "HOOK_ON_PLAYER_CONNECTED", "HOOK_ON_PLAYER_DISCONNECTED", "HOOK_ON_HUD_RENDER", + "HOOK_ON_INTERACT", "HOOK_MAX" }; void smlua_call_event_hooks(enum LuaHookedEventType hookType); void smlua_call_event_hooks_mario_param(enum LuaHookedEventType hookType, struct MarioState* m); void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2); +void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool interactValue); bool smlua_call_action_hook(struct MarioState* m, s32* returnValue); u32 smlua_get_action_interaction_type(struct MarioState* m); @@ -41,4 +44,4 @@ void smlua_display_chat_commands(void); void smlua_bind_hooks(void); -#endif \ No newline at end of file +#endif