From 1e4e1f8e759c91eeccff01fcb17ffbab6811b484 Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Sat, 11 May 2024 08:28:20 +1000 Subject: [PATCH] fix bugs caused by interpolating the frame an object spawns (#36) --- autogen/lua_definitions/structs.lua | 1 + docs/lua/structs.md | 1 + include/types.h | 2 ++ src/engine/behavior_script.h | 2 ++ src/game/rendering_graph_node.c | 11 +++++++++-- src/game/spawn_object.c | 1 + src/pc/lua/smlua_cobject_autogen.c | 3 ++- 7 files changed, 18 insertions(+), 3 deletions(-) diff --git a/autogen/lua_definitions/structs.lua b/autogen/lua_definitions/structs.lua index 9cf74bf4..3eb27ce4 100644 --- a/autogen/lua_definitions/structs.lua +++ b/autogen/lua_definitions/structs.lua @@ -794,6 +794,7 @@ --- @field public areaIndex integer --- @field public cameraToObject Vec3f --- @field public disableAutomaticShadowPos boolean +--- @field public inited boolean --- @field public node GraphNode --- @field public pos Vec3f --- @field public prevAngle Vec3s diff --git a/docs/lua/structs.md b/docs/lua/structs.md index 48f2a463..d6f62fe7 100644 --- a/docs/lua/structs.md +++ b/docs/lua/structs.md @@ -1085,6 +1085,7 @@ | areaIndex | `integer` | | | cameraToObject | [Vec3f](structs.md#Vec3f) | read-only | | disableAutomaticShadowPos | `boolean` | | +| inited | `boolean` | | | node | [GraphNode](structs.md#GraphNode) | read-only | | pos | [Vec3f](structs.md#Vec3f) | read-only | | prevAngle | [Vec3s](structs.md#Vec3s) | read-only | diff --git a/include/types.h b/include/types.h index 184b81ec..547021e7 100644 --- a/include/types.h +++ b/include/types.h @@ -176,6 +176,8 @@ struct GraphNodeObject /*0x54*/ Vec3f cameraToObject; u32 skipInterpolationTimestamp; bool skipInViewCheck; + + bool inited; }; struct ObjectNode diff --git a/src/engine/behavior_script.h b/src/engine/behavior_script.h index 6d2ff491..7bdd7e43 100644 --- a/src/engine/behavior_script.h +++ b/src/engine/behavior_script.h @@ -27,6 +27,8 @@ void stub_behavior_script_2(void); void cur_obj_update(void); +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); diff --git a/src/game/rendering_graph_node.c b/src/game/rendering_graph_node.c index bc36cb60..89f14d6b 100644 --- a/src/game/rendering_graph_node.c +++ b/src/game/rendering_graph_node.c @@ -1212,6 +1212,13 @@ static void geo_process_object(struct Object *node) { // Sanity check our stack index, If we above or equal to our stack size. Return to prevent OOB\. if ((gMatStackIndex + 1) >= MATRIX_STACK_SIZE) { LOG_ERROR("Preventing attempt to exceed the maximum size %i for our matrix stack with size of %i.", MATRIX_STACK_SIZE - 1, gMatStackIndex); return; } + if (!node->header.gfx.inited) { + node->header.gfx.inited = true; + obj_update_gfx_pos_and_angle(node); + vec3f_copy(node->header.gfx.prevPos, node->header.gfx.pos); + vec3s_copy(node->header.gfx.prevAngle, node->header.gfx.angle); + } + if (node->hookRender) { smlua_call_event_hooks_object_param(HOOK_ON_OBJECT_RENDER, node); } @@ -1335,7 +1342,7 @@ static void geo_process_object(struct Object *node) { if (node->header.gfx.animInfo.curAnim != NULL) { dynos_gfx_swap_animations(node); geo_set_animation_globals(&node->header.gfx.animInfo, hasAnimation); - smlua_call_event_hooks_object_param(HOOK_ON_OBJECT_ANIM_UPDATE, node); + if (node->hookRender) smlua_call_event_hooks_object_param(HOOK_ON_OBJECT_ANIM_UPDATE, node); dynos_gfx_swap_animations(node); } if (obj_is_in_view(&node->header.gfx, gMatStack[gMatStackIndex])) { @@ -1458,7 +1465,7 @@ void geo_process_held_object(struct GraphNodeHeldObject *node) { if (node->objNode->header.gfx.animInfo.curAnim != NULL) { dynos_gfx_swap_animations(node->objNode); geo_set_animation_globals(&node->objNode->header.gfx.animInfo, hasAnimation); - smlua_call_event_hooks_object_param(HOOK_ON_OBJECT_ANIM_UPDATE, node->objNode); + if (node->objNode->hookRender) smlua_call_event_hooks_object_param(HOOK_ON_OBJECT_ANIM_UPDATE, node->objNode); dynos_gfx_swap_animations(node->objNode); } diff --git a/src/game/spawn_object.c b/src/game/spawn_object.c index 1370fe45..c903b1b3 100644 --- a/src/game/spawn_object.c +++ b/src/game/spawn_object.c @@ -324,6 +324,7 @@ struct Object *allocate_object(struct ObjectNode *objList) { obj->header.gfx.angle[0] = 0; obj->header.gfx.angle[1] = 0; obj->header.gfx.angle[2] = 0; + obj->header.gfx.inited = false; obj->coopFlags = 0; obj->hookRender = 0; diff --git a/src/pc/lua/smlua_cobject_autogen.c b/src/pc/lua/smlua_cobject_autogen.c index f87f6014..ac929ca0 100644 --- a/src/pc/lua/smlua_cobject_autogen.c +++ b/src/pc/lua/smlua_cobject_autogen.c @@ -883,7 +883,7 @@ static struct LuaObjectField sGraphNodeFields[LUA_GRAPH_NODE_FIELD_COUNT] = { { "type", LVT_S16, offsetof(struct GraphNode, type), true, LOT_NONE }, }; -#define LUA_GRAPH_NODE_OBJECT_FIELD_COUNT 26 +#define LUA_GRAPH_NODE_OBJECT_FIELD_COUNT 27 static struct LuaObjectField sGraphNodeObjectFields[LUA_GRAPH_NODE_OBJECT_FIELD_COUNT] = { { "activeAreaIndex", LVT_S8, offsetof(struct GraphNodeObject, activeAreaIndex), false, LOT_NONE }, { "angle", LVT_COBJECT, offsetof(struct GraphNodeObject, angle), true, LOT_VEC3S }, @@ -891,6 +891,7 @@ static struct LuaObjectField sGraphNodeObjectFields[LUA_GRAPH_NODE_OBJECT_FIELD_ { "areaIndex", LVT_S8, offsetof(struct GraphNodeObject, areaIndex), false, LOT_NONE }, { "cameraToObject", LVT_COBJECT, offsetof(struct GraphNodeObject, cameraToObject), true, LOT_VEC3F }, { "disableAutomaticShadowPos", LVT_BOOL, offsetof(struct GraphNodeObject, disableAutomaticShadowPos), false, LOT_NONE }, + { "inited", LVT_BOOL, offsetof(struct GraphNodeObject, inited), false, LOT_NONE }, { "node", LVT_COBJECT, offsetof(struct GraphNodeObject, node), true, LOT_GRAPHNODE }, { "pos", LVT_COBJECT, offsetof(struct GraphNodeObject, pos), true, LOT_VEC3F }, { "prevAngle", LVT_COBJECT, offsetof(struct GraphNodeObject, prevAngle), true, LOT_VEC3S },