From 8812fbac8b0a6118ebfd58380573237093861dc9 Mon Sep 17 00:00:00 2001 From: PeachyPeach <72323920+PeachyPeachSM64@users.noreply.github.com> Date: Sun, 1 May 2022 02:33:38 +0200 Subject: [PATCH] HOOK_ALLOW_INTERACT; new HUD functions (#73) Added new hook: HOOK_ALLOW_INTERACT: Called before Mario interacts with an object. Return true to allow the interaction. The hook signature is bool function(MarioState, Object, InteractionType) Added new HUD constants: enum HudDisplayValue: HUD_DISPLAY_LIVES HUD_DISPLAY_COINS HUD_DISPLAY_STARS HUD_DISPLAY_WEDGES HUD_DISPLAY_KEYS HUD_DISPLAY_FLAGS HUD_DISPLAY_TIMER enum HudDisplayFlags: HUD_DISPLAY_FLAGS_NONE HUD_DISPLAY_FLAGS_LIVES HUD_DISPLAY_FLAGS_COIN_COUNT HUD_DISPLAY_FLAGS_STAR_COUNT HUD_DISPLAY_FLAGS_CAMERA_AND_POWER HUD_DISPLAY_FLAGS_KEYS HUD_DISPLAY_FLAGS_UNKNOWN_0020 HUD_DISPLAY_FLAGS_TIMER HUD_DISPLAY_FLAGS_EMPHASIZE_POWER Added new HUD functions: s32 hud_get_value(enum HudDisplayValue type) void hud_set_value(enum HudDisplayValue type, s32 value) void hud_render_power_meter(s32 health, f32 x, f32 y, f32 width, f32 height) --- autogen/convert_constants.py | 1 + autogen/lua_definitions/constants.lua | 81 ++++++++++++++++++++++----- autogen/lua_definitions/functions.lua | 23 ++++++++ docs/lua/constants.md | 60 +++++++++++++++----- docs/lua/functions-4.md | 65 +++++++++++++++++++++ docs/lua/functions.md | 3 + docs/lua/hooks.md | 1 + src/game/interaction.c | 14 +++-- src/pc/djui/djui_hud_utils.c | 2 +- src/pc/lua/smlua_constants_autogen.c | 43 +++++++++----- src/pc/lua/smlua_functions_autogen.c | 46 +++++++++++++++ src/pc/lua/smlua_hooks.c | 36 ++++++++++++ src/pc/lua/smlua_hooks.h | 3 + src/pc/lua/utils/smlua_misc_utils.c | 58 +++++++++++++++++++ src/pc/lua/utils/smlua_misc_utils.h | 25 +++++++++ 15 files changed, 416 insertions(+), 45 deletions(-) diff --git a/autogen/convert_constants.py b/autogen/convert_constants.py index 0cd9044f1..86e9803c9 100644 --- a/autogen/convert_constants.py +++ b/autogen/convert_constants.py @@ -24,6 +24,7 @@ in_files = [ "src/pc/djui/djui_hud_utils.h", "include/behavior_table.h", "src/pc/lua/utils/smlua_model_utils.h", + "src/pc/lua/utils/smlua_misc_utils.h", "include/object_constants.h", "include/mario_geo_switch_case_ids.h", "src/game/object_list_processor.h", diff --git a/autogen/lua_definitions/constants.lua b/autogen/lua_definitions/constants.lua index 099e5b2f0..2afc75f43 100644 --- a/autogen/lua_definitions/constants.lua +++ b/autogen/lua_definitions/constants.lua @@ -7822,43 +7822,98 @@ HOOK_ON_PLAYER_DISCONNECTED = 8 HOOK_ON_HUD_RENDER = 9 --- @type LuaHookedEventType -HOOK_ON_INTERACT = 10 +HOOK_ALLOW_INTERACT = 10 --- @type LuaHookedEventType -HOOK_ON_LEVEL_INIT = 11 +HOOK_ON_INTERACT = 11 --- @type LuaHookedEventType -HOOK_ON_WARP = 12 +HOOK_ON_LEVEL_INIT = 12 --- @type LuaHookedEventType -HOOK_ON_SYNC_VALID = 13 +HOOK_ON_WARP = 13 --- @type LuaHookedEventType -HOOK_ON_OBJECT_UNLOAD = 14 +HOOK_ON_SYNC_VALID = 14 --- @type LuaHookedEventType -HOOK_ON_SYNC_OBJECT_UNLOAD = 15 +HOOK_ON_OBJECT_UNLOAD = 15 --- @type LuaHookedEventType -HOOK_ON_PAUSE_EXIT = 16 +HOOK_ON_SYNC_OBJECT_UNLOAD = 16 --- @type LuaHookedEventType -HOOK_GET_STAR_COLLECTION_DIALOG = 17 +HOOK_ON_PAUSE_EXIT = 17 --- @type LuaHookedEventType -HOOK_ON_SET_CAMERA_MODE = 18 +HOOK_GET_STAR_COLLECTION_DIALOG = 18 --- @type LuaHookedEventType -HOOK_ON_OBJECT_RENDER = 19 +HOOK_ON_SET_CAMERA_MODE = 19 --- @type LuaHookedEventType -HOOK_ON_DEATH = 20 +HOOK_ON_OBJECT_RENDER = 20 --- @type LuaHookedEventType -HOOK_ON_PACKET_RECEIVE = 21 +HOOK_ON_DEATH = 21 --- @type LuaHookedEventType -HOOK_MAX = 22 +HOOK_ON_PACKET_RECEIVE = 22 + +--- @type LuaHookedEventType +HOOK_MAX = 23 + +--- @class HudDisplayFlags + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_NONE = 0x0000 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_LIVES = 0x0001 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_COIN_COUNT = 0x0002 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_STAR_COUNT = 0x0004 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_CAMERA_AND_POWER = 0x0008 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_KEYS = 0x0010 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_UNKNOWN_0020 = 0x0020 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_TIMER = 0x0040 + +--- @type HudDisplayFlags +HUD_DISPLAY_FLAGS_EMPHASIZE_POWER = 0x8000 + +--- @class HudDisplayValue + +--- @type HudDisplayValue +HUD_DISPLAY_LIVES = 0 + +--- @type HudDisplayValue +HUD_DISPLAY_COINS = 1 + +--- @type HudDisplayValue +HUD_DISPLAY_STARS = 2 + +--- @type HudDisplayValue +HUD_DISPLAY_WEDGES = 3 + +--- @type HudDisplayValue +HUD_DISPLAY_KEYS = 4 + +--- @type HudDisplayValue +HUD_DISPLAY_FLAGS = 5 + +--- @type HudDisplayValue +HUD_DISPLAY_TIMER = 6 --- @class ModelExtendedId diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index cf6bc6a3f..6be355817 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -7171,11 +7171,34 @@ function get_temp_s32_pointer(initialValue) -- ... end +--- @param type HudDisplayValue +--- @return integer +function hud_get_value(type) + -- ... +end + --- @return nil function hud_hide() -- ... end +--- @param health integer +--- @param x number +--- @param y number +--- @param width number +--- @param height number +--- @return nil +function hud_render_power_meter(health, x, y, width, height) + -- ... +end + +--- @param type HudDisplayValue +--- @param value integer +--- @return nil +function hud_set_value(type, value) + -- ... +end + --- @return nil function hud_show() -- ... diff --git a/docs/lua/constants.md b/docs/lua/constants.md index 84e861287..3dc475cd5 100644 --- a/docs/lua/constants.md +++ b/docs/lua/constants.md @@ -44,6 +44,9 @@ - [sm64.h](#sm64h) - [smlua_hooks.h](#smlua_hooksh) - [enum LuaHookedEventType](#enum-LuaHookedEventType) +- [smlua_misc_utils.h](#smlua_misc_utilsh) + - [enum HudDisplayFlags](#enum-HudDisplayFlags) + - [enum HudDisplayValue](#enum-HudDisplayValue) - [smlua_model_utils.h](#smlua_model_utilsh) - [enum ModelExtendedId](#enum-ModelExtendedId) - [sounds.h](#soundsh) @@ -2745,19 +2748,50 @@ | HOOK_ON_PLAYER_CONNECTED | 7 | | HOOK_ON_PLAYER_DISCONNECTED | 8 | | HOOK_ON_HUD_RENDER | 9 | -| HOOK_ON_INTERACT | 10 | -| HOOK_ON_LEVEL_INIT | 11 | -| HOOK_ON_WARP | 12 | -| HOOK_ON_SYNC_VALID | 13 | -| HOOK_ON_OBJECT_UNLOAD | 14 | -| HOOK_ON_SYNC_OBJECT_UNLOAD | 15 | -| HOOK_ON_PAUSE_EXIT | 16 | -| HOOK_GET_STAR_COLLECTION_DIALOG | 17 | -| HOOK_ON_SET_CAMERA_MODE | 18 | -| HOOK_ON_OBJECT_RENDER | 19 | -| HOOK_ON_DEATH | 20 | -| HOOK_ON_PACKET_RECEIVE | 21 | -| HOOK_MAX | 22 | +| HOOK_ALLOW_INTERACT | 10 | +| HOOK_ON_INTERACT | 11 | +| HOOK_ON_LEVEL_INIT | 12 | +| HOOK_ON_WARP | 13 | +| HOOK_ON_SYNC_VALID | 14 | +| HOOK_ON_OBJECT_UNLOAD | 15 | +| HOOK_ON_SYNC_OBJECT_UNLOAD | 16 | +| HOOK_ON_PAUSE_EXIT | 17 | +| HOOK_GET_STAR_COLLECTION_DIALOG | 18 | +| HOOK_ON_SET_CAMERA_MODE | 19 | +| HOOK_ON_OBJECT_RENDER | 20 | +| HOOK_ON_DEATH | 21 | +| HOOK_ON_PACKET_RECEIVE | 22 | +| HOOK_MAX | 23 | + +[:arrow_up_small:](#) + +
+ +## [smlua_misc_utils.h](#smlua_misc_utils.h) + +### [enum HudDisplayFlags](#HudDisplayFlags) +| Identifier | Value | +| :--------- | :---- | +| HUD_DISPLAY_FLAGS_NONE | 0x0000 | +| HUD_DISPLAY_FLAGS_LIVES | 0x0001 | +| HUD_DISPLAY_FLAGS_COIN_COUNT | 0x0002 | +| HUD_DISPLAY_FLAGS_STAR_COUNT | 0x0004 | +| HUD_DISPLAY_FLAGS_CAMERA_AND_POWER | 0x0008 | +| HUD_DISPLAY_FLAGS_KEYS | 0x0010 | +| HUD_DISPLAY_FLAGS_UNKNOWN_0020 | 0x0020 | +| HUD_DISPLAY_FLAGS_TIMER | 0x0040 | +| HUD_DISPLAY_FLAGS_EMPHASIZE_POWER | 0x8000 | + +### [enum HudDisplayValue](#HudDisplayValue) +| Identifier | Value | +| :--------- | :---- | +| HUD_DISPLAY_LIVES | 0 | +| HUD_DISPLAY_COINS | 1 | +| HUD_DISPLAY_STARS | 2 | +| HUD_DISPLAY_WEDGES | 3 | +| HUD_DISPLAY_KEYS | 4 | +| HUD_DISPLAY_FLAGS | 5 | +| HUD_DISPLAY_TIMER | 6 | [:arrow_up_small:](#) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index f3e997f1a..099502506 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -4860,6 +4860,26 @@
+## [hud_get_value](#hud_get_value) + +### Lua Example +`local integerValue = hud_get_value(type)` + +### Parameters +| Field | Type | +| ----- | ---- | +| type | [enum HudDisplayValue](constants.md#enum-HudDisplayValue) | + +### Returns +- `integer` + +### C Prototype +`s32 hud_get_value(enum HudDisplayValue type);` + +[:arrow_up_small:](#) + +
+ ## [hud_hide](#hud_hide) ### Lua Example @@ -4878,6 +4898,51 @@
+## [hud_render_power_meter](#hud_render_power_meter) + +### Lua Example +`hud_render_power_meter(health, x, y, width, height)` + +### Parameters +| Field | Type | +| ----- | ---- | +| health | `integer` | +| x | `number` | +| y | `number` | +| width | `number` | +| height | `number` | + +### Returns +- None + +### C Prototype +`void hud_render_power_meter(s32 health, f32 x, f32 y, f32 width, f32 height);` + +[:arrow_up_small:](#) + +
+ +## [hud_set_value](#hud_set_value) + +### Lua Example +`hud_set_value(type, value)` + +### Parameters +| Field | Type | +| ----- | ---- | +| type | [enum HudDisplayValue](constants.md#enum-HudDisplayValue) | +| value | `integer` | + +### Returns +- None + +### C Prototype +`void hud_set_value(enum HudDisplayValue type, s32 value);` + +[:arrow_up_small:](#) + +
+ ## [hud_show](#hud_show) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index b14e8774f..083e34251 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1341,7 +1341,10 @@ - [get_hand_foot_pos_z](functions-4.md#get_hand_foot_pos_z) - [get_network_area_timer](functions-4.md#get_network_area_timer) - [get_temp_s32_pointer](functions-4.md#get_temp_s32_pointer) + - [hud_get_value](functions-4.md#hud_get_value) - [hud_hide](functions-4.md#hud_hide) + - [hud_render_power_meter](functions-4.md#hud_render_power_meter) + - [hud_set_value](functions-4.md#hud_set_value) - [hud_show](functions-4.md#hud_show) - [movtexqc_register](functions-4.md#movtexqc_register) - [play_transition](functions-4.md#play_transition) diff --git a/docs/lua/hooks.md b/docs/lua/hooks.md index c3d47d9f8..1d16d71fd 100644 --- a/docs/lua/hooks.md +++ b/docs/lua/hooks.md @@ -96,6 +96,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh | 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_HUD_RENDER | Called when the HUD is being rendered | None | +| HOOK_ALLOW_INTERACT | Called before mario interacts with an object, return `true` to allow the interaction | [MarioState](structs.md#MarioState) interactor, [Object](structs.md#Object) interactee, [enum InteractionType](constants.md#enum-InteractionType) interactType | | HOOK_ON_INTERACT | Called when mario interacts with an object | [MarioState](structs.md#MarioState) interactor, [Object](structs.md#Object) interactee, [enum InteractionType](constants.md#enum-InteractionType) interactType, bool interactValue | | HOOK_ON_LEVEL_INIT | Called when the level is initialized | None | | HOOK_ON_WARP | Called when the local player warps | None | diff --git a/src/game/interaction.c b/src/game/interaction.c index a60cd9ee2..b82e2c1ed 100644 --- a/src/game/interaction.c +++ b/src/game/interaction.c @@ -2174,11 +2174,15 @@ void mario_process_interactions(struct MarioState *m) { m->collidedObjInteractTypes &= ~interactType; 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); + bool allow = true; + smlua_call_event_hooks_interact_params_ret_bool(HOOK_ALLOW_INTERACT, m, object, interactType, &allow); + if (allow) { + 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/pc/djui/djui_hud_utils.c b/src/pc/djui/djui_hud_utils.c index 012cbc27c..4206df784 100644 --- a/src/pc/djui/djui_hud_utils.c +++ b/src/pc/djui/djui_hud_utils.c @@ -164,7 +164,7 @@ static void djui_hud_render_texture_raw(const u8* texture, u32 bitSize, u32 widt } void djui_hud_render_texture(struct TextureInfo* texInfo, f32 x, f32 y, f32 scaleW, f32 scaleH) { - djui_hud_render_texture_raw(texInfo->texture, texInfo->bitSize, texInfo->width, texInfo->width, x, y, scaleW, scaleH); + djui_hud_render_texture_raw(texInfo->texture, texInfo->bitSize, texInfo->width, texInfo->height, x, y, scaleW, scaleH); } void djui_hud_render_rect(f32 x, f32 y, f32 width, f32 height) { diff --git a/src/pc/lua/smlua_constants_autogen.c b/src/pc/lua/smlua_constants_autogen.c index 6783deeaf..85f7f337c 100644 --- a/src/pc/lua/smlua_constants_autogen.c +++ b/src/pc/lua/smlua_constants_autogen.c @@ -2788,19 +2788,36 @@ char gSmluaConstants[] = "" "HOOK_ON_PLAYER_CONNECTED = 7\n" "HOOK_ON_PLAYER_DISCONNECTED = 8\n" "HOOK_ON_HUD_RENDER = 9\n" -"HOOK_ON_INTERACT = 10\n" -"HOOK_ON_LEVEL_INIT = 11\n" -"HOOK_ON_WARP = 12\n" -"HOOK_ON_SYNC_VALID = 13\n" -"HOOK_ON_OBJECT_UNLOAD = 14\n" -"HOOK_ON_SYNC_OBJECT_UNLOAD = 15\n" -"HOOK_ON_PAUSE_EXIT = 16\n" -"HOOK_GET_STAR_COLLECTION_DIALOG = 17\n" -"HOOK_ON_SET_CAMERA_MODE = 18\n" -"HOOK_ON_OBJECT_RENDER = 19\n" -"HOOK_ON_DEATH = 20\n" -"HOOK_ON_PACKET_RECEIVE = 21\n" -"HOOK_MAX = 22\n" +"HOOK_ALLOW_INTERACT = 10\n" +"HOOK_ON_INTERACT = 11\n" +"HOOK_ON_LEVEL_INIT = 12\n" +"HOOK_ON_WARP = 13\n" +"HOOK_ON_SYNC_VALID = 14\n" +"HOOK_ON_OBJECT_UNLOAD = 15\n" +"HOOK_ON_SYNC_OBJECT_UNLOAD = 16\n" +"HOOK_ON_PAUSE_EXIT = 17\n" +"HOOK_GET_STAR_COLLECTION_DIALOG = 18\n" +"HOOK_ON_SET_CAMERA_MODE = 19\n" +"HOOK_ON_OBJECT_RENDER = 20\n" +"HOOK_ON_DEATH = 21\n" +"HOOK_ON_PACKET_RECEIVE = 22\n" +"HOOK_MAX = 23\n" +"HUD_DISPLAY_LIVES = 0\n" +"HUD_DISPLAY_COINS = 1\n" +"HUD_DISPLAY_STARS = 2\n" +"HUD_DISPLAY_WEDGES = 3\n" +"HUD_DISPLAY_KEYS = 4\n" +"HUD_DISPLAY_FLAGS = 5\n" +"HUD_DISPLAY_TIMER = 6\n" +"HUD_DISPLAY_FLAGS_NONE = 0x0000\n" +"HUD_DISPLAY_FLAGS_LIVES = 0x0001\n" +"HUD_DISPLAY_FLAGS_COIN_COUNT = 0x0002\n" +"HUD_DISPLAY_FLAGS_STAR_COUNT = 0x0004\n" +"HUD_DISPLAY_FLAGS_CAMERA_AND_POWER = 0x0008\n" +"HUD_DISPLAY_FLAGS_KEYS = 0x0010\n" +"HUD_DISPLAY_FLAGS_UNKNOWN_0020 = 0x0020\n" +"HUD_DISPLAY_FLAGS_TIMER = 0x0040\n" +"HUD_DISPLAY_FLAGS_EMPHASIZE_POWER = 0x8000\n" "E_MODEL_NONE = 0\n" "E_MODEL_MARIO = 1\n" "E_MODEL_SMOKE = 2\n" diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index c6de9478d..cc63f37a2 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -14769,6 +14769,17 @@ int smlua_func_get_temp_s32_pointer(lua_State* L) { return 1; } +int smlua_func_hud_get_value(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 1)) { return 0; } + + int type = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + + lua_pushinteger(L, hud_get_value(type)); + + return 1; +} + int smlua_func_hud_hide(UNUSED lua_State* L) { if(!smlua_functions_valid_param_count(L, 0)) { return 0; } @@ -14778,6 +14789,38 @@ int smlua_func_hud_hide(UNUSED lua_State* L) { return 1; } +int smlua_func_hud_render_power_meter(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 5)) { return 0; } + + s32 health = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + f32 x = smlua_to_number(L, 2); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 2"); return 0; } + f32 y = smlua_to_number(L, 3); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 3"); return 0; } + f32 width = smlua_to_number(L, 4); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 4"); return 0; } + f32 height = smlua_to_number(L, 5); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 5"); return 0; } + + hud_render_power_meter(health, x, y, width, height); + + return 1; +} + +int smlua_func_hud_set_value(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 2)) { return 0; } + + int type = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + s32 value = smlua_to_integer(L, 2); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 2"); return 0; } + + hud_set_value(type, value); + + return 1; +} + int smlua_func_hud_show(UNUSED lua_State* L) { if(!smlua_functions_valid_param_count(L, 0)) { return 0; } @@ -17075,7 +17118,10 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "get_hand_foot_pos_z", smlua_func_get_hand_foot_pos_z); smlua_bind_function(L, "get_network_area_timer", smlua_func_get_network_area_timer); smlua_bind_function(L, "get_temp_s32_pointer", smlua_func_get_temp_s32_pointer); + smlua_bind_function(L, "hud_get_value", smlua_func_hud_get_value); smlua_bind_function(L, "hud_hide", smlua_func_hud_hide); + smlua_bind_function(L, "hud_render_power_meter", smlua_func_hud_render_power_meter); + smlua_bind_function(L, "hud_set_value", smlua_func_hud_set_value); smlua_bind_function(L, "hud_show", smlua_func_hud_show); smlua_bind_function(L, "movtexqc_register", smlua_func_movtexqc_register); smlua_bind_function(L, "play_transition", smlua_func_play_transition); diff --git a/src/pc/lua/smlua_hooks.c b/src/pc/lua/smlua_hooks.c index c829111d6..c5d588a04 100644 --- a/src/pc/lua/smlua_hooks.c +++ b/src/pc/lua/smlua_hooks.c @@ -342,6 +342,42 @@ void smlua_call_event_hooks_interact_params(enum LuaHookedEventType hookType, st } } +void smlua_call_event_hooks_interact_params_ret_bool(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool* returnValue) { + 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 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); + + // call the callback + if (0 != smlua_call_hook(L, 3, 1, 0, hook->mod[i])) { + LOG_LUA("Failed to call the callback: %u", hookType); + continue; + } + + // output the return value + if (lua_type(L, -1) == LUA_TBOOLEAN) { + *returnValue = smlua_to_boolean(L, -1); + } + lua_settop(L, prevTop); + } +} + void smlua_call_event_hooks_object_param(enum LuaHookedEventType hookType, struct Object* obj) { 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 e7f25c40f..0796f51cf 100644 --- a/src/pc/lua/smlua_hooks.h +++ b/src/pc/lua/smlua_hooks.h @@ -18,6 +18,7 @@ enum LuaHookedEventType { HOOK_ON_PLAYER_CONNECTED, HOOK_ON_PLAYER_DISCONNECTED, HOOK_ON_HUD_RENDER, + HOOK_ALLOW_INTERACT, HOOK_ON_INTERACT, HOOK_ON_LEVEL_INIT, HOOK_ON_WARP, @@ -44,6 +45,7 @@ static char* LuaHookedEventTypeName[] = { "HOOK_ON_PLAYER_CONNECTED", "HOOK_ON_PLAYER_DISCONNECTED", "HOOK_ON_HUD_RENDER", + "HOOK_ALLOW_INTERACT", "HOOK_ON_INTERACT", "HOOK_ON_LEVEL_INIT", "HOOK_ON_WARP", @@ -69,6 +71,7 @@ void smlua_call_event_hooks_mario_param_ret_bool(enum LuaHookedEventType hookTyp void smlua_call_event_hooks_mario_params(enum LuaHookedEventType hookType, struct MarioState* m1, struct MarioState* m2); 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_interact_params_ret_bool(enum LuaHookedEventType hookType, struct MarioState* m, struct Object* obj, u32 interactType, bool* returnValue); 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); void smlua_call_event_hooks_set_camera_mode_params(enum LuaHookedEventType hookType, struct Camera *c, s16 mode, s16 frames, bool* returnValue); diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index e37659aa1..c65521732 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -7,6 +7,8 @@ #include "smlua_misc_utils.h" #include "pc/debuglog.h" #include "game/object_list_processor.h" +#include "game/level_update.h" +#include "pc/djui/djui_hud_utils.h" u32 get_network_area_timer(void) { return gNetworkAreaTimer; @@ -38,6 +40,62 @@ void hud_show(void) { gOverrideHideHud = 0; } +s32 hud_get_value(enum HudDisplayValue type) { + switch (type) { + case HUD_DISPLAY_LIVES: return gHudDisplay.lives; + case HUD_DISPLAY_COINS: return gHudDisplay.coins; + case HUD_DISPLAY_STARS: return gHudDisplay.stars; + case HUD_DISPLAY_WEDGES: return gHudDisplay.wedges; + case HUD_DISPLAY_KEYS: return gHudDisplay.keys; + case HUD_DISPLAY_FLAGS: return gHudDisplay.flags; + case HUD_DISPLAY_TIMER: return gHudDisplay.timer; + } + return 0; +} + +void hud_set_value(enum HudDisplayValue type, s32 value) { + switch (type) { + case HUD_DISPLAY_LIVES: gHudDisplay.lives = value; break; + case HUD_DISPLAY_COINS: gHudDisplay.coins = value; break; + case HUD_DISPLAY_STARS: gHudDisplay.stars = value; break; + case HUD_DISPLAY_WEDGES: gHudDisplay.wedges = value; break; + case HUD_DISPLAY_KEYS: gHudDisplay.keys = value; break; + case HUD_DISPLAY_FLAGS: gHudDisplay.flags = value; break; + case HUD_DISPLAY_TIMER: gHudDisplay.timer = value; break; + } +} + +void hud_render_power_meter(s32 health, f32 x, f32 y, f32 width, f32 height) { + extern const u8 texture_power_meter_left_side[]; + extern const u8 texture_power_meter_right_side[]; + extern const u8 texture_power_meter_full[]; + extern const u8 texture_power_meter_seven_segments[]; + extern const u8 texture_power_meter_six_segments[]; + extern const u8 texture_power_meter_five_segments[]; + extern const u8 texture_power_meter_four_segments[]; + extern const u8 texture_power_meter_three_segments[]; + extern const u8 texture_power_meter_two_segments[]; + extern const u8 texture_power_meter_one_segments[]; + static struct TextureInfo sPowerMeterTexturesInfo[] = { + { texture_power_meter_left_side, 8, 32, 64 }, + { texture_power_meter_right_side, 8, 32, 64 }, + { texture_power_meter_one_segments, 8, 32, 32 }, + { texture_power_meter_two_segments, 8, 32, 32 }, + { texture_power_meter_three_segments, 8, 32, 32 }, + { texture_power_meter_four_segments, 8, 32, 32 }, + { texture_power_meter_five_segments, 8, 32, 32 }, + { texture_power_meter_six_segments, 8, 32, 32 }, + { texture_power_meter_seven_segments, 8, 32, 32 }, + { texture_power_meter_full, 8, 32, 32 }, + }; + djui_hud_render_texture(&sPowerMeterTexturesInfo[0], x, y, width / 64, height / 64); + djui_hud_render_texture(&sPowerMeterTexturesInfo[1], x + width / 2, y, width / 64, height / 64); + s32 numWedges = MIN(MAX(health >> 8, 0), 8); + if (numWedges != 0) { + djui_hud_render_texture(&sPowerMeterTexturesInfo[numWedges + 1], x + width / 4, y + height / 4, width / 64, height / 64); + } +} + /// bool warp_to_level(s32 aLevel, s32 aArea, s32 aAct) { diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h index ae209995c..57b8dbf17 100644 --- a/src/pc/lua/utils/smlua_misc_utils.h +++ b/src/pc/lua/utils/smlua_misc_utils.h @@ -8,8 +8,33 @@ u32 get_network_area_timer(void); s32* get_temp_s32_pointer(s32 initialValue); s32 deref_s32_pointer(s32* pointer); +enum HudDisplayValue { + HUD_DISPLAY_LIVES, + HUD_DISPLAY_COINS, + HUD_DISPLAY_STARS, + HUD_DISPLAY_WEDGES, + HUD_DISPLAY_KEYS, + HUD_DISPLAY_FLAGS, + HUD_DISPLAY_TIMER, +}; + +enum HudDisplayFlags { + HUD_DISPLAY_FLAGS_NONE = 0x0000, + HUD_DISPLAY_FLAGS_LIVES = 0x0001, + HUD_DISPLAY_FLAGS_COIN_COUNT = 0x0002, + HUD_DISPLAY_FLAGS_STAR_COUNT = 0x0004, + HUD_DISPLAY_FLAGS_CAMERA_AND_POWER = 0x0008, + HUD_DISPLAY_FLAGS_KEYS = 0x0010, + HUD_DISPLAY_FLAGS_UNKNOWN_0020 = 0x0020, + HUD_DISPLAY_FLAGS_TIMER = 0x0040, + HUD_DISPLAY_FLAGS_EMPHASIZE_POWER = 0x8000, +}; + void hud_hide(void); void hud_show(void); +s32 hud_get_value(enum HudDisplayValue type); +void hud_set_value(enum HudDisplayValue type, s32 value); +void hud_render_power_meter(s32 health, f32 x, f32 y, f32 width, f32 height); bool warp_to_level(s32 aLevel, s32 aArea, s32 aAct); bool warp_restart_level(void);