add HOOK_OBJECT_SET_MODEL (#236)

* add HOOK_OBJECT_SET_MODEL
runs whenever a behaviour sets a model.
This commit is contained in:
Isaac0-dev 2022-11-30 18:37:43 +10:00 committed by GitHub
parent 523e7842d9
commit 07ad5a55c5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 37 additions and 30 deletions

View file

@ -8131,7 +8131,10 @@ HOOK_ALLOW_HAZARD_SURFACE = 26
HOOK_ON_CHAT_MESSAGE = 27
--- @type LuaHookedEventType
HOOK_MAX = 28
HOOK_OBJECT_SET_MODEL = 28
--- @type LuaHookedEventType
HOOK_MAX = 29
--- @class HudDisplayFlags

View file

@ -43,6 +43,9 @@ gBehaviorValues = {}
--- @type PlayerPalette[]
gPalettePresets = {}
--- @type LakituState
gLakituState = {}
-----------
-- hooks --
-----------

View file

@ -2879,7 +2879,8 @@
| HOOK_ON_SCREEN_TRANSITION | 25 |
| HOOK_ALLOW_HAZARD_SURFACE | 26 |
| HOOK_ON_CHAT_MESSAGE | 27 |
| HOOK_MAX | 28 |
| HOOK_OBJECT_SET_MODEL | 28 |
| HOOK_MAX | 29 |
[:arrow_up_small:](#)

View file

@ -114,6 +114,7 @@ The lua functions sent to `hook_event()` will be automatically called by SM64 wh
| HOOK_ON_SCREEN_TRANSITION | Called when the game is about to play a transition, return `false` to prevent the transition from playing. | `integer` type |
| HOOK_ALLOW_HAZARD_SURFACE | Called once per frame. Return `false` to prevent Mario from being affected by lava or quicksand. | [MarioState](structs.md#MarioState) localMario |
| HOOK_ON_CHAT_MESSAGE | Called when a chat message gets sent. Return `false` to prevent the message from being sent. | [MarioState](structs.md#MarioState) messageSender |
| HOOK_OBJECT_SET_MODEL | Called when a behavior changes models. Also runs when a behavior spawns. | [Object](structs.md#Object) obj, `integer` modelID |
### Parameters

View file

@ -677,6 +677,7 @@ struct Object *spawn_object_at_origin(struct Object *parent, UNUSED s32 unusedAr
geo_obj_init((struct GraphNodeObject *) &obj->header.gfx, gLoadedGraphNodes[model], gVec3fZero,
gVec3sZero);
smlua_call_event_hooks_object_model_param(HOOK_OBJECT_SET_MODEL, obj, model);
return obj;
}
@ -1363,6 +1364,7 @@ void cur_obj_set_model(s32 modelID) {
void obj_set_model(struct Object* obj, s32 modelID) {
obj->header.gfx.sharedChild = gLoadedGraphNodes[modelID];
dynos_actor_override((void*)&obj->header.gfx.sharedChild);
smlua_call_event_hooks_object_model_param(HOOK_OBJECT_SET_MODEL, obj, modelID);
}
void mario_set_flag(s32 flag) {

View file

@ -10,7 +10,6 @@ char gSmluaConstants[] = ""
" return a['_pointer'] == b['_pointer'] and a['_lot'] == b['_lot'] and a['_pointer'] ~= nil and a['_lot'] ~= nil\n"
" end\n"
"}\n"
"\n"
"_CPointer = {\n"
" __index = function (t,k)\n"
" return nil\n"
@ -24,7 +23,6 @@ char gSmluaConstants[] = ""
" return a['_pointer'] == b['_pointer'] and a['_pointer'] ~= nil and a['_lvt'] ~= nil\n"
" end\n"
"}\n"
"\n"
"_SyncTable = {\n"
" __index = function (t,k)\n"
" local _table = rawget(t, '_table')\n"
@ -36,7 +34,6 @@ char gSmluaConstants[] = ""
" _set_sync_table_field(t, k, v)\n"
" end\n"
"}\n"
"\n"
"_ReadOnlyTable = {\n"
" __index = function (t,k)\n"
" local _table = rawget(t, '_table')\n"
@ -45,7 +42,6 @@ char gSmluaConstants[] = ""
" __newindex = function (t,k,v)\n"
" end\n"
"}\n"
"\n"
"--- @param dest Vec3f\n"
"--- @param src Vec3f\n"
"--- @return Vec3f\n"
@ -55,7 +51,6 @@ char gSmluaConstants[] = ""
" dest.z = src.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3f\n"
"--- @param x number\n"
"--- @param y number\n"
@ -67,7 +62,6 @@ char gSmluaConstants[] = ""
" dest.z = z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3f\n"
"--- @param a Vec3f\n"
"--- @return Vec3f\n"
@ -77,7 +71,6 @@ char gSmluaConstants[] = ""
" dest.z = dest.z + a.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3f\n"
"--- @param a Vec3f\n"
"--- @param b Vec3f\n"
@ -88,7 +81,6 @@ char gSmluaConstants[] = ""
" dest.z = a.z + b.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3f\n"
"--- @param a number\n"
"--- @return Vec3f\n"
@ -98,7 +90,6 @@ char gSmluaConstants[] = ""
" dest.z = dest.z * a\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3f\n"
"--- @return Vec3f\n"
"function vec3f_normalize(dest)\n"
@ -106,28 +97,23 @@ char gSmluaConstants[] = ""
" if divisor == 0 then\n"
" return dest\n"
" end\n"
"\n"
" local invsqrt = 1.0 / divisor\n"
" dest.x = dest.x * invsqrt\n"
" dest.y = dest.y * invsqrt\n"
" dest.z = dest.z * invsqrt\n"
"\n"
" return dest\n"
"end\n"
"\n"
"--- @param a Vec3f\n"
"--- @return number\n"
"function vec3f_length(a)\n"
" return math.sqrt(a.x * a.x + a.y * a.y + a.z * a.z)\n"
"end\n"
"\n"
"--- @param a Vec3f\n"
"--- @param b Vec3f\n"
"--- @return number\n"
"function vec3f_dot(a, b)\n"
" return a.x * b.x + a.y * b.y + a.z * b.z\n"
"end\n"
"\n"
"--- @param vec Vec3f\n"
"--- @param onto Vec3f\n"
"--- @return Vec3f\n"
@ -139,7 +125,6 @@ char gSmluaConstants[] = ""
" vec3f_mul(out, numerator / denominator)\n"
" return out\n"
"end\n"
"\n"
"--- @param v1 Vec3f\n"
"--- @param v2 Vec3f\n"
"--- @return number\n"
@ -149,7 +134,6 @@ char gSmluaConstants[] = ""
" dz = v1.z - v2.z\n"
" return math.sqrt(dx * dx + dy * dy + dz * dz)\n"
"end\n"
"\n"
"--- @param dest Vec3s\n"
"--- @param src Vec3s\n"
"--- @return Vec3s\n"
@ -159,7 +143,6 @@ char gSmluaConstants[] = ""
" dest.z = src.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3s\n"
"--- @param x number\n"
"--- @param y number\n"
@ -171,7 +154,6 @@ char gSmluaConstants[] = ""
" dest.z = z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3s\n"
"--- @param a Vec3s\n"
"--- @return Vec3s\n"
@ -181,7 +163,6 @@ char gSmluaConstants[] = ""
" dest.z = dest.z + a.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3s\n"
"--- @param a Vec3s\n"
"--- @param b Vec3s\n"
@ -192,7 +173,6 @@ char gSmluaConstants[] = ""
" dest.z = a.z + b.z\n"
" return dest\n"
"end\n"
"\n"
"--- @param dest Vec3s\n"
"--- @param a number\n"
"--- @return Vec3s\n"
@ -202,7 +182,6 @@ char gSmluaConstants[] = ""
" dest.z = dest.z * a\n"
" return dest\n"
"end\n"
"\n"
"--- @param v1 Vec3s\n"
"--- @param v2 Vec3s\n"
"--- @return number\n"
@ -212,7 +191,6 @@ char gSmluaConstants[] = ""
" dz = v1.z - v2.z\n"
" return math.sqrt(dx * dx + dy * dy + dz * dz)\n"
"end\n"
"\n"
"--- @param current number\n"
"--- @param target number\n"
"--- @param inc number\n"
@ -232,7 +210,6 @@ char gSmluaConstants[] = ""
" end\n"
" return current;\n"
"end\n"
"\n"
"--- @param current number\n"
"--- @param target number\n"
"--- @param inc number\n"
@ -250,7 +227,6 @@ char gSmluaConstants[] = ""
" current = target\n"
" end\n"
" end\n"
"\n"
" -- keep within 32 bits\n"
" if current > 2147483647 then\n"
" current = -2147483648 + (current - 2147483647)\n"
@ -259,7 +235,6 @@ char gSmluaConstants[] = ""
" end\n"
" return current;\n"
"end\n"
"\n"
"--- @param bank number\n"
"--- @param soundID number\n"
"--- @param priority number\n"
@ -269,11 +244,9 @@ char gSmluaConstants[] = ""
" if flags == nil then flags = 0 end\n"
" return (bank << 28) | (soundID << 16) | (priority << 8) | flags | SOUND_STATUS_WAITING\n"
"end\n"
"\n"
"-------------\n"
"-- courses --\n"
"-------------\n"
"\n"
"--- @type integer\n"
"COURSE_NONE = 0\n"
"--- @type integer\n"
@ -2913,7 +2886,8 @@ char gSmluaConstants[] = ""
"HOOK_ON_SCREEN_TRANSITION = 25\n"
"HOOK_ALLOW_HAZARD_SURFACE = 26\n"
"HOOK_ON_CHAT_MESSAGE = 27\n"
"HOOK_MAX = 28\n"
"HOOK_OBJECT_SET_MODEL = 28\n"
"HOOK_MAX = 29\n"
"ACTION_HOOK_EVERY_FRAME = 0\n"
"ACTION_HOOK_GRAVITY = 1\n"
"ACTION_HOOK_MAX = 2\n"

View file

@ -398,6 +398,26 @@ void smlua_call_event_hooks_object_param(enum LuaHookedEventType hookType, struc
}
}
void smlua_call_event_hooks_object_model_param(enum LuaHookedEventType hookType, struct Object* obj, s32 modelID) {
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 params
smlua_push_object(L, LOT_OBJECT, obj);
lua_pushinteger(L, modelID);
// call the callback
if (0 != smlua_call_hook(L, 2, 0, 0, hook->mod[i])) {
LOG_LUA("Failed to call the callback: %u", hookType);
continue;
}
}
}
bool smlua_call_event_hooks_ret_int(enum LuaHookedEventType hookType, s32* returnValue) {
lua_State* L = gLuaState;
if (L == NULL) { return false; }

View file

@ -39,6 +39,7 @@ enum LuaHookedEventType {
HOOK_ON_SCREEN_TRANSITION,
HOOK_ALLOW_HAZARD_SURFACE,
HOOK_ON_CHAT_MESSAGE,
HOOK_OBJECT_SET_MODEL,
HOOK_MAX,
};
@ -71,6 +72,7 @@ static const char* LuaHookedEventTypeName[] = {
"HOOK_ON_SCREEN_TRANSITION",
"HOOK_ALLOW_HAZARD_SURFACE",
"HOOK_ON_CHAT_MESSAGE",
"HOOK_OBJECT_SET_MODEL",
"HOOK_MAX"
};
@ -107,6 +109,7 @@ void smlua_call_event_hooks_value_param(enum LuaHookedEventType hookType, int mo
void smlua_call_event_hooks_use_act_select(enum LuaHookedEventType hookType, int value, bool* foundHook, bool* returnValue);
void smlua_call_event_hooks_ret_bool(enum LuaHookedEventType hookType, bool* returnValue);
void smlua_call_event_hooks_on_chat_message(enum LuaHookedEventType hookType, struct MarioState* m, const char* message, bool* returnValue);
void smlua_call_event_hooks_object_model_param(enum LuaHookedEventType hookType, struct Object* obj, s32 modelID);
enum BehaviorId smlua_get_original_behavior_id(const BehaviorScript* behavior);
const BehaviorScript* smlua_override_behavior(const BehaviorScript* behavior);