From ac805bf94dec8dcec2cb9a3a765dceee913e10b1 Mon Sep 17 00:00:00 2001
From: Agent X <44549182+AgentXLP@users.noreply.github.com>
Date: Sun, 23 Jun 2024 19:30:35 -0400
Subject: [PATCH] Expose some behavior_script.h functions to Lua
---
autogen/convert_functions.py | 6 +-
autogen/lua_definitions/functions.lua | 35 +++++++
docs/lua/functions-2.md | 134 ++++++++++++++++++++++++++
docs/lua/functions.md | 11 +++
src/engine/behavior_script.c | 4 -
src/engine/behavior_script.h | 1 -
src/pc/lua/smlua_functions_autogen.c | 121 +++++++++++++++++++++++
7 files changed, 305 insertions(+), 7 deletions(-)
diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py
index c89acd14..e9f0a9a3 100644
--- a/autogen/convert_functions.py
+++ b/autogen/convert_functions.py
@@ -65,7 +65,8 @@ in_files = [
"src/game/area.h",
"src/engine/level_script.h",
"src/game/ingame_menu.h",
- "src/game/first_person_cam.h"
+ "src/game/first_person_cam.h",
+ "src/engine/behavior_script.h"
]
override_allowed_functions = {
@@ -118,7 +119,8 @@ override_disallowed_functions = {
"src/pc/lua/utils/smlua_anim_utils.h": [ "smlua_anim_util_reset", "smlua_anim_util_register_animation" ],
"src/pc/network/lag_compensation.h": [ "lag_compensation_clear", "lag_compensation_store" ],
"src/game/first_person_cam.h": [ "first_person_update" ],
- "src/pc/lua/utils/smlua_collision_utils.h": [ "collision_find_surface_on_ray" ]
+ "src/pc/lua/utils/smlua_collision_utils.h": [ "collision_find_surface_on_ray" ],
+ "src/engine/behavior_script.h": [ "stub_behavior_script_2", "cur_obj_update" ]
}
override_hide_functions = {
diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua
index 35bcbbe1..0c114a2d 100644
--- a/autogen/lua_definitions/functions.lua
+++ b/autogen/lua_definitions/functions.lua
@@ -2343,6 +2343,41 @@ function vec3f_copy_2(dest, src)
-- ...
end
+--- @return number
+function draw_distance_scalar()
+ -- ...
+end
+
+--- @param obj Object
+function obj_update_gfx_pos_and_angle(obj)
+ -- ...
+end
+
+--- @return number
+function position_based_random_float_position()
+ -- ...
+end
+
+--- @return integer
+function position_based_random_u16()
+ -- ...
+end
+
+--- @return number
+function random_float()
+ -- ...
+end
+
+--- @return integer
+function random_sign()
+ -- ...
+end
+
+--- @return integer
+function random_u16()
+ -- ...
+end
+
--- @param id BehaviorId
--- @return Pointer_BehaviorScript
function get_behavior_from_id(id)
diff --git a/docs/lua/functions-2.md b/docs/lua/functions-2.md
index ea78b8e4..a0b6a61e 100644
--- a/docs/lua/functions-2.md
+++ b/docs/lua/functions-2.md
@@ -10331,6 +10331,140 @@
+---
+# functions from behavior_script.h
+
+
+
+
+## [draw_distance_scalar](#draw_distance_scalar)
+
+### Lua Example
+`local numberValue = draw_distance_scalar()`
+
+### Parameters
+- None
+
+### Returns
+- `number`
+
+### C Prototype
+`f32 draw_distance_scalar(void);`
+
+[:arrow_up_small:](#)
+
+
+
+## [obj_update_gfx_pos_and_angle](#obj_update_gfx_pos_and_angle)
+
+### Lua Example
+`obj_update_gfx_pos_and_angle(obj)`
+
+### Parameters
+| Field | Type |
+| ----- | ---- |
+| obj | [Object](structs.md#Object) |
+
+### Returns
+- None
+
+### C Prototype
+`void obj_update_gfx_pos_and_angle(struct Object *obj);`
+
+[:arrow_up_small:](#)
+
+
+
+## [position_based_random_float_position](#position_based_random_float_position)
+
+### Lua Example
+`local numberValue = position_based_random_float_position()`
+
+### Parameters
+- None
+
+### Returns
+- `number`
+
+### C Prototype
+`f32 position_based_random_float_position(void);`
+
+[:arrow_up_small:](#)
+
+
+
+## [position_based_random_u16](#position_based_random_u16)
+
+### Lua Example
+`local integerValue = position_based_random_u16()`
+
+### Parameters
+- None
+
+### Returns
+- `integer`
+
+### C Prototype
+`u16 position_based_random_u16(void);`
+
+[:arrow_up_small:](#)
+
+
+
+## [random_float](#random_float)
+
+### Lua Example
+`local numberValue = random_float()`
+
+### Parameters
+- None
+
+### Returns
+- `number`
+
+### C Prototype
+`float random_float(void);`
+
+[:arrow_up_small:](#)
+
+
+
+## [random_sign](#random_sign)
+
+### Lua Example
+`local integerValue = random_sign()`
+
+### Parameters
+- None
+
+### Returns
+- `integer`
+
+### C Prototype
+`s32 random_sign(void);`
+
+[:arrow_up_small:](#)
+
+
+
+## [random_u16](#random_u16)
+
+### Lua Example
+`local integerValue = random_u16()`
+
+### Parameters
+- None
+
+### Returns
+- `integer`
+
+### C Prototype
+`u16 random_u16(void);`
+
+[:arrow_up_small:](#)
+
+
+
---
# functions from behavior_table.h
diff --git a/docs/lua/functions.md b/docs/lua/functions.md
index 54cdcd22..88fbc64c 100644
--- a/docs/lua/functions.md
+++ b/docs/lua/functions.md
@@ -609,6 +609,17 @@
+- behavior_script.h
+ - [draw_distance_scalar](functions-2.md#draw_distance_scalar)
+ - [obj_update_gfx_pos_and_angle](functions-2.md#obj_update_gfx_pos_and_angle)
+ - [position_based_random_float_position](functions-2.md#position_based_random_float_position)
+ - [position_based_random_u16](functions-2.md#position_based_random_u16)
+ - [random_float](functions-2.md#random_float)
+ - [random_sign](functions-2.md#random_sign)
+ - [random_u16](functions-2.md#random_u16)
+
+
+
- behavior_table.h
- [get_behavior_from_id](functions-2.md#get_behavior_from_id)
- [get_behavior_name_from_id](functions-2.md#get_behavior_name_from_id)
diff --git a/src/engine/behavior_script.c b/src/engine/behavior_script.c
index 528aa3e1..3b4eea8a 100644
--- a/src/engine/behavior_script.c
+++ b/src/engine/behavior_script.c
@@ -1464,10 +1464,6 @@ f32 position_based_random_float_position(void) {
return rnd / (double)0x10000;
}
-u8 cur_obj_is_last_nat_update_per_frame(void) {
- return (gCurrentObject->areaTimer == (gNetworkAreaTimer - 1));
-}
-
f32 draw_distance_scalar(void) {
if (!gBehaviorValues.InfiniteRenderDistance) { return 1.0f; }
diff --git a/src/engine/behavior_script.h b/src/engine/behavior_script.h
index 7bdd7e43..b92034ed 100644
--- a/src/engine/behavior_script.h
+++ b/src/engine/behavior_script.h
@@ -31,7 +31,6 @@ void obj_update_gfx_pos_and_angle(struct Object *obj);
u16 position_based_random_u16(void);
f32 position_based_random_float_position(void);
-u8 cur_obj_is_last_nat_update_per_frame(void);
f32 draw_distance_scalar(void);
#endif // BEHAVIOR_SCRIPT_H
diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c
index 07572245..919de0f5 100644
--- a/src/pc/lua/smlua_functions_autogen.c
+++ b/src/pc/lua/smlua_functions_autogen.c
@@ -45,6 +45,7 @@
#include "src/engine/level_script.h"
#include "src/game/ingame_menu.h"
#include "src/game/first_person_cam.h"
+#include "src/engine/behavior_script.h"
////////////
@@ -9620,6 +9621,117 @@ int smlua_func_vec3f_copy_2(lua_State* L) {
return 1;
}
+ ///////////////////////
+ // behavior_script.h //
+///////////////////////
+
+int smlua_func_draw_distance_scalar(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "draw_distance_scalar", 0, top);
+ return 0;
+ }
+
+
+ lua_pushnumber(L, draw_distance_scalar());
+
+ return 1;
+}
+
+int smlua_func_obj_update_gfx_pos_and_angle(lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 1) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "obj_update_gfx_pos_and_angle", 1, top);
+ return 0;
+ }
+
+ struct Object* obj = (struct Object*)smlua_to_cobject(L, 1, LOT_OBJECT);
+ if (!gSmLuaConvertSuccess) { LOG_LUA("Failed to convert parameter %u for function '%s'", 1, "obj_update_gfx_pos_and_angle"); return 0; }
+
+ obj_update_gfx_pos_and_angle(obj);
+
+ return 1;
+}
+
+int smlua_func_position_based_random_float_position(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "position_based_random_float_position", 0, top);
+ return 0;
+ }
+
+
+ lua_pushnumber(L, position_based_random_float_position());
+
+ return 1;
+}
+
+int smlua_func_position_based_random_u16(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "position_based_random_u16", 0, top);
+ return 0;
+ }
+
+
+ lua_pushinteger(L, position_based_random_u16());
+
+ return 1;
+}
+
+int smlua_func_random_float(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "random_float", 0, top);
+ return 0;
+ }
+
+
+ lua_pushnumber(L, random_float());
+
+ return 1;
+}
+
+int smlua_func_random_sign(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "random_sign", 0, top);
+ return 0;
+ }
+
+
+ lua_pushinteger(L, random_sign());
+
+ return 1;
+}
+
+int smlua_func_random_u16(UNUSED lua_State* L) {
+ if (L == NULL) { return 0; }
+
+ int top = lua_gettop(L);
+ if (top != 0) {
+ LOG_LUA_LINE("Improper param count for '%s': Expected %u, Received %u", "random_u16", 0, top);
+ return 0;
+ }
+
+
+ lua_pushinteger(L, random_u16());
+
+ return 1;
+}
+
//////////////////////
// behavior_table.h //
//////////////////////
@@ -33269,6 +33381,15 @@ void smlua_bind_functions_autogen(void) {
smlua_bind_function(L, "uv_update_scroll", smlua_func_uv_update_scroll);
smlua_bind_function(L, "vec3f_copy_2", smlua_func_vec3f_copy_2);
+ // behavior_script.h
+ smlua_bind_function(L, "draw_distance_scalar", smlua_func_draw_distance_scalar);
+ smlua_bind_function(L, "obj_update_gfx_pos_and_angle", smlua_func_obj_update_gfx_pos_and_angle);
+ smlua_bind_function(L, "position_based_random_float_position", smlua_func_position_based_random_float_position);
+ smlua_bind_function(L, "position_based_random_u16", smlua_func_position_based_random_u16);
+ smlua_bind_function(L, "random_float", smlua_func_random_float);
+ smlua_bind_function(L, "random_sign", smlua_func_random_sign);
+ smlua_bind_function(L, "random_u16", smlua_func_random_u16);
+
// behavior_table.h
smlua_bind_function(L, "get_behavior_from_id", smlua_func_get_behavior_from_id);
smlua_bind_function(L, "get_behavior_name_from_id", smlua_func_get_behavior_name_from_id);