From 2a405b3233ea6ddc1f69a35b8ea5091c3a9829a8 Mon Sep 17 00:00:00 2001 From: Prince Frizzy Date: Thu, 28 Apr 2022 23:42:29 -0400 Subject: [PATCH] Add two new LUA util functions. (#72) --- autogen/lua_definitions/functions.lua | 13 +++++++++ docs/lua/functions-4.md | 41 +++++++++++++++++++++++++++ docs/lua/functions.md | 2 ++ src/pc/lua/smlua_functions_autogen.c | 26 +++++++++++++++++ src/pc/lua/utils/smlua_obj_utils.c | 34 ++++++++++++++++++++++ src/pc/lua/utils/smlua_obj_utils.h | 4 +++ 6 files changed, 120 insertions(+) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index 59c8d8948..cf6bc6a3f 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -7262,6 +7262,12 @@ function obj_check_hitbox_overlap(o1, o2) -- ... end +--- @param behaviorId BehaviorId +--- @return integer +function obj_count_objects_with_behavior_id(behaviorId) + -- ... +end + --- @param objList ObjectList --- @return Object function obj_get_first(objList) @@ -7290,6 +7296,13 @@ function obj_get_first_with_behavior_id_and_field_s32(behaviorId, fieldIndex, va -- ... end +--- @param o Object +--- @param behaviorId BehaviorId +--- @return Object +function obj_get_nearest_object_with_behavior_id(o, behaviorId) + -- ... +end + --- @param o Object --- @return Object function obj_get_next(o) diff --git a/docs/lua/functions-4.md b/docs/lua/functions-4.md index d18a515d0..f3e997f1a 100644 --- a/docs/lua/functions-4.md +++ b/docs/lua/functions-4.md @@ -5155,6 +5155,26 @@
+## [obj_count_objects_with_behavior_id](#obj_count_objects_with_behavior_id) + +### Lua Example +`local integerValue = obj_count_objects_with_behavior_id(behaviorId)` + +### Parameters +| Field | Type | +| ----- | ---- | +| behaviorId | [enum BehaviorId](constants.md#enum-BehaviorId) | + +### Returns +- `integer` + +### C Prototype +`s32 obj_count_objects_with_behavior_id(enum BehaviorId behaviorId);` + +[:arrow_up_small:](#) + +
+ ## [obj_get_first](#obj_get_first) ### Lua Example @@ -5239,6 +5259,27 @@
+## [obj_get_nearest_object_with_behavior_id](#obj_get_nearest_object_with_behavior_id) + +### Lua Example +`local ObjectValue = obj_get_nearest_object_with_behavior_id(o, behaviorId)` + +### Parameters +| Field | Type | +| ----- | ---- | +| o | [Object](structs.md#Object) | +| behaviorId | [enum BehaviorId](constants.md#enum-BehaviorId) | + +### Returns +[Object](structs.md#Object) + +### C Prototype +`struct Object *obj_get_nearest_object_with_behavior_id(struct Object *o, enum BehaviorId behaviorId);` + +[:arrow_up_small:](#) + +
+ ## [obj_get_next](#obj_get_next) ### Lua Example diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 8857f6eaa..b14e8774f 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1363,10 +1363,12 @@ - [get_temp_object_hitbox](functions-4.md#get_temp_object_hitbox) - [get_trajectory](functions-4.md#get_trajectory) - [obj_check_hitbox_overlap](functions-4.md#obj_check_hitbox_overlap) + - [obj_count_objects_with_behavior_id](functions-4.md#obj_count_objects_with_behavior_id) - [obj_get_first](functions-4.md#obj_get_first) - [obj_get_first_with_behavior_id](functions-4.md#obj_get_first_with_behavior_id) - [obj_get_first_with_behavior_id_and_field_f32](functions-4.md#obj_get_first_with_behavior_id_and_field_f32) - [obj_get_first_with_behavior_id_and_field_s32](functions-4.md#obj_get_first_with_behavior_id_and_field_s32) + - [obj_get_nearest_object_with_behavior_id](functions-4.md#obj_get_nearest_object_with_behavior_id) - [obj_get_next](functions-4.md#obj_get_next) - [obj_get_next_with_same_behavior_id](functions-4.md#obj_get_next_with_same_behavior_id) - [obj_get_next_with_same_behavior_id_and_field_f32](functions-4.md#obj_get_next_with_same_behavior_id_and_field_f32) diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index b985c4f66..c6de9478d 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -14945,6 +14945,17 @@ int smlua_func_obj_check_hitbox_overlap(lua_State* L) { return 1; } +int smlua_func_obj_count_objects_with_behavior_id(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 1)) { return 0; } + + int behaviorId = smlua_to_integer(L, 1); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + + lua_pushinteger(L, obj_count_objects_with_behavior_id(behaviorId)); + + return 1; +} + int smlua_func_obj_get_first(lua_State* L) { if(!smlua_functions_valid_param_count(L, 1)) { return 0; } @@ -14997,6 +15008,19 @@ int smlua_func_obj_get_first_with_behavior_id_and_field_s32(lua_State* L) { return 1; } +int smlua_func_obj_get_nearest_object_with_behavior_id(lua_State* L) { + if(!smlua_functions_valid_param_count(L, 2)) { return 0; } + + struct Object* o = (struct Object*)smlua_to_cobject(L, 1, LOT_OBJECT); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 1"); return 0; } + int behaviorId = smlua_to_integer(L, 2); + if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter 2"); return 0; } + + smlua_push_object(L, LOT_OBJECT, obj_get_nearest_object_with_behavior_id(o, behaviorId)); + + return 1; +} + int smlua_func_obj_get_next(lua_State* L) { if(!smlua_functions_valid_param_count(L, 1)) { return 0; } @@ -17069,10 +17093,12 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "get_temp_object_hitbox", smlua_func_get_temp_object_hitbox); smlua_bind_function(L, "get_trajectory", smlua_func_get_trajectory); smlua_bind_function(L, "obj_check_hitbox_overlap", smlua_func_obj_check_hitbox_overlap); + smlua_bind_function(L, "obj_count_objects_with_behavior_id", smlua_func_obj_count_objects_with_behavior_id); smlua_bind_function(L, "obj_get_first", smlua_func_obj_get_first); smlua_bind_function(L, "obj_get_first_with_behavior_id", smlua_func_obj_get_first_with_behavior_id); smlua_bind_function(L, "obj_get_first_with_behavior_id_and_field_f32", smlua_func_obj_get_first_with_behavior_id_and_field_f32); smlua_bind_function(L, "obj_get_first_with_behavior_id_and_field_s32", smlua_func_obj_get_first_with_behavior_id_and_field_s32); + smlua_bind_function(L, "obj_get_nearest_object_with_behavior_id", smlua_func_obj_get_nearest_object_with_behavior_id); smlua_bind_function(L, "obj_get_next", smlua_func_obj_get_next); smlua_bind_function(L, "obj_get_next_with_same_behavior_id", smlua_func_obj_get_next_with_same_behavior_id); smlua_bind_function(L, "obj_get_next_with_same_behavior_id_and_field_f32", smlua_func_obj_get_next_with_same_behavior_id_and_field_f32); diff --git a/src/pc/lua/utils/smlua_obj_utils.c b/src/pc/lua/utils/smlua_obj_utils.c index 77579dc2b..5c2840de6 100644 --- a/src/pc/lua/utils/smlua_obj_utils.c +++ b/src/pc/lua/utils/smlua_obj_utils.c @@ -156,6 +156,40 @@ struct Object *obj_get_first_with_behavior_id_and_field_f32(enum BehaviorId beha return NULL; } +struct Object *obj_get_nearest_object_with_behavior_id(struct Object *o, enum BehaviorId behaviorId) { + f32 minDist = 0x20000; + const BehaviorScript *behavior = get_behavior_from_id(behaviorId); + struct Object *closestObj = NULL; + + if (behavior) { + enum ObjectList objList = get_object_list_from_behavior(behavior); + for (struct Object *obj = obj_get_first(objList); obj != NULL; obj = obj_get_next(obj)) { + if (obj->behavior == behavior && obj->activeFlags != ACTIVE_FLAG_DEACTIVATED) { + f32 objDist = dist_between_objects(o, obj); + if (objDist < minDist) { + closestObj = obj; + minDist = objDist; + } + } + } + } + return closestObj; +} + +s32 obj_count_objects_with_behavior_id(enum BehaviorId behaviorId) { + const BehaviorScript *behavior = get_behavior_from_id(behaviorId); + s32 count = 0; + + if (behavior) { + enum ObjectList objList = get_object_list_from_behavior(behavior); + for (struct Object *obj = obj_get_first(objList); obj != NULL; obj = obj_get_next(obj)) { + if (obj->behavior == behavior) { count++; } + } + } + + return count; +} + struct Object *obj_get_next(struct Object *o) { if (gObjectLists && o) { enum ObjectList objList = get_object_list_from_behavior(o->behavior); diff --git a/src/pc/lua/utils/smlua_obj_utils.h b/src/pc/lua/utils/smlua_obj_utils.h index 769966338..e432c0859 100644 --- a/src/pc/lua/utils/smlua_obj_utils.h +++ b/src/pc/lua/utils/smlua_obj_utils.h @@ -28,6 +28,10 @@ struct Object *obj_get_next_with_same_behavior_id(struct Object *o); struct Object *obj_get_next_with_same_behavior_id_and_field_s32(struct Object *o, s32 fieldIndex, s32 value); struct Object *obj_get_next_with_same_behavior_id_and_field_f32(struct Object *o, s32 fieldIndex, f32 value); +struct Object *obj_get_nearest_object_with_behavior_id(struct Object *o, enum BehaviorId behaviorId); + +s32 obj_count_objects_with_behavior_id(enum BehaviorId behaviorId); + // misc obj helpers struct SpawnParticlesInfo* obj_get_temp_spawn_particles_info(enum ModelExtendedId modelId);