From f42e4ad55b1bc1c4fe1fa9487e1a1be0cee4d9e2 Mon Sep 17 00:00:00 2001 From: djoslin0 Date: Sun, 15 Jun 2025 00:26:53 -0700 Subject: [PATCH] Add get_game_tick_counter() (#852) Coop now maintains a counter that increments at the start of each game tick, And another counter that increments at the start of each frame render. This is to be able to identify specific frames regardless of mod load, hook, and execution order. --------- Co-authored-by: MysterD --- autogen/lua_definitions/functions.lua | 6 ++++++ docs/lua/functions-6.md | 21 +++++++++++++++++++++ docs/lua/functions.md | 1 + src/game/game_init.c | 3 +++ src/game/game_init.h | 1 + src/pc/lua/smlua_functions_autogen.c | 16 ++++++++++++++++ src/pc/lua/utils/smlua_misc_utils.c | 4 ++++ src/pc/lua/utils/smlua_misc_utils.h | 3 +++ 8 files changed, 55 insertions(+) diff --git a/autogen/lua_definitions/functions.lua b/autogen/lua_definitions/functions.lua index ce8adeb06..062072618 100644 --- a/autogen/lua_definitions/functions.lua +++ b/autogen/lua_definitions/functions.lua @@ -11104,6 +11104,12 @@ function texture_to_lua_table(tex) -- ... end +--- @return integer +--- Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX. +function get_game_tick_counter() + -- ... +end + --- @param name string --- @return ModelExtendedId --- Gets the extended model ID for the `name` of a `GeoLayout` diff --git a/docs/lua/functions-6.md b/docs/lua/functions-6.md index c87745a38..6e6331fc1 100644 --- a/docs/lua/functions-6.md +++ b/docs/lua/functions-6.md @@ -5128,6 +5128,27 @@ Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, re
+## [get_game_tick_counter](#get_game_tick_counter) + +### Description +Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX. + +### Lua Example +`local integerValue = get_game_tick_counter()` + +### Parameters +- None + +### Returns +- `integer` + +### C Prototype +`u64 get_game_tick_counter(void);` + +[:arrow_up_small:](#) + +
+ --- # functions from smlua_model_utils.h diff --git a/docs/lua/functions.md b/docs/lua/functions.md index 10d832f15..b13b718ee 100644 --- a/docs/lua/functions.md +++ b/docs/lua/functions.md @@ -1991,6 +1991,7 @@ - [geo_get_current_camera](functions-6.md#geo_get_current_camera) - [geo_get_current_held_object](functions-6.md#geo_get_current_held_object) - [texture_to_lua_table](functions-6.md#texture_to_lua_table) + - [get_game_tick_counter](functions-6.md#get_game_tick_counter)
diff --git a/src/game/game_init.c b/src/game/game_init.c index 2e13c7e57..63ea79102 100644 --- a/src/game/game_init.c +++ b/src/game/game_init.c @@ -63,6 +63,7 @@ struct Controller *gPlayer3Controller = &gControllers[2]; struct DemoInput *gCurrDemoInput = NULL; // demo input sequence u16 gDemoInputListID = 0; struct DemoInput gRecordedDemoInput = { 0 }; // possibly removed in EU. TODO: Check +u64 gGameTickCounter = 0; /** * Initializes the Reality Display Processor (RDP). @@ -602,6 +603,8 @@ void thread5_game_loop(UNUSED void *arg) { void game_loop_one_iteration(void) { profiler_log_thread5_time(THREAD5_START); + gGameTickCounter++; + // if any controllers are plugged in, start read the data for when // read_controller_inputs is called later. if (gControllerBits) { diff --git a/src/game/game_init.h b/src/game/game_init.h index 86710b1e0..ee6edd254 100644 --- a/src/game/game_init.h +++ b/src/game/game_init.h @@ -58,6 +58,7 @@ extern u8 gDemoInputs[]; extern u16 frameBufferIndex; extern u32 gGlobalTimer; +extern u64 gGameTickCounter; void setup_game_memory(void); void thread5_game_loop(UNUSED void *arg); diff --git a/src/pc/lua/smlua_functions_autogen.c b/src/pc/lua/smlua_functions_autogen.c index 2bf3ba87c..db72048f1 100644 --- a/src/pc/lua/smlua_functions_autogen.c +++ b/src/pc/lua/smlua_functions_autogen.c @@ -33296,6 +33296,21 @@ int smlua_func_texture_to_lua_table(lua_State* L) { return 1; } +int smlua_func_get_game_tick_counter(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", "get_game_tick_counter", 0, top); + return 0; + } + + + lua_pushinteger(L, get_game_tick_counter()); + + return 1; +} + ///////////////////////// // smlua_model_utils.h // ///////////////////////// @@ -37179,6 +37194,7 @@ void smlua_bind_functions_autogen(void) { smlua_bind_function(L, "geo_get_current_camera", smlua_func_geo_get_current_camera); smlua_bind_function(L, "geo_get_current_held_object", smlua_func_geo_get_current_held_object); smlua_bind_function(L, "texture_to_lua_table", smlua_func_texture_to_lua_table); + smlua_bind_function(L, "get_game_tick_counter", smlua_func_get_game_tick_counter); // smlua_model_utils.h smlua_bind_function(L, "smlua_model_util_get_id", smlua_func_smlua_model_util_get_id); diff --git a/src/pc/lua/utils/smlua_misc_utils.c b/src/pc/lua/utils/smlua_misc_utils.c index 5fe69642c..d567335ea 100644 --- a/src/pc/lua/utils/smlua_misc_utils.c +++ b/src/pc/lua/utils/smlua_misc_utils.c @@ -625,3 +625,7 @@ void texture_to_lua_table(const u8 *tex) { lua_rawseti(L, -2, i / bytesPerPixel + 1); } } + +u64 get_game_tick_counter(void) { + return gGameTickCounter; +} diff --git a/src/pc/lua/utils/smlua_misc_utils.h b/src/pc/lua/utils/smlua_misc_utils.h index 401467a6e..8d2bc1b18 100644 --- a/src/pc/lua/utils/smlua_misc_utils.h +++ b/src/pc/lua/utils/smlua_misc_utils.h @@ -234,4 +234,7 @@ struct GraphNodeHeldObject* geo_get_current_held_object(void); /* |description|Converts a texture's pixels to a Lua table. Returns nil if failed. Otherwise, returns a table as a pure memory buffer. Supports rgba16 and rgba32 textures.|descriptionEnd|*/ void texture_to_lua_table(const u8 *tex); +/* |description|Gets the total number of game ticks since the game was launched. Wraps around at U64_MAX.|descriptionEnd|*/ +u64 get_game_tick_counter(void); + #endif