From 3564f6ebde5203fae583cecffd4a58e27a7a6087 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sun, 15 Oct 2023 22:03:48 +0100 Subject: [PATCH] Fixed a crash in minetest vector code that isn't propogated to lua. Create util for ease of use. --- mods/CORE/mcl_util/init.lua | 12 ++++++++++++ mods/ENTITIES/mcl_mobs/effects.lua | 12 ++---------- mods/ENTITIES/mcl_mobs/physics.lua | 3 ++- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/mods/CORE/mcl_util/init.lua b/mods/CORE/mcl_util/init.lua index d37bb8785..e894eebd7 100644 --- a/mods/CORE/mcl_util/init.lua +++ b/mods/CORE/mcl_util/init.lua @@ -74,6 +74,18 @@ function mcl_util.check_dtime_timer(self, dtime, timer_name, threshold) return false end +-- While we should always favour the new minetest vector functions such as vector.new or vector.offset which validate on +-- creation. There may be cases where state gets corrupted and we may have to check the vector is valid if created the +-- old way. This allows us to do this as a tactical solution until old style vectors are completely removed. +function mcl_util.validate_vector (vect) + if vect then + if tonumber(vect.x) and tonumber(vect.y) and tonumber(vect.z) then + return true + end + end + return false +end + -- Minetest 5.3.0 or less can only measure the light level. This came in at 5.4 -- This function has been known to fail in multiple places so the error handling is added increase safety and improve -- debugging. See: diff --git a/mods/ENTITIES/mcl_mobs/effects.lua b/mods/ENTITIES/mcl_mobs/effects.lua index aa44a67f7..f889f8e99 100644 --- a/mods/ENTITIES/mcl_mobs/effects.lua +++ b/mods/ENTITIES/mcl_mobs/effects.lua @@ -1,5 +1,7 @@ local math, tonumber, vector, minetest, mcl_mobs = math, tonumber, vector, minetest, mcl_mobs local mob_class = mcl_mobs.mob_class +local validate_vector = mcl_util.validate_vector + local active_particlespawners = {} local disable_blood = minetest.settings:get_bool("mobs_disable_blood") local DEFAULT_FALL_SPEED = -9.81*1.5 @@ -9,16 +11,6 @@ local PATHFINDING = "gowp" local player_transfer_distance = tonumber(minetest.settings:get("player_transfer_distance")) or 128 if player_transfer_distance == 0 then player_transfer_distance = math.huge end - -local function validate_vector (vect) - if vect then - if tonumber(vect.x) and tonumber(vect.y) and tonumber(vect.z) then - return true - end - end - return false -end - -- custom particle effects function mcl_mobs.effect(pos, amount, texture, min_size, max_size, radius, gravity, glow, go_down) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 7ffa62ffe..64bcfa822 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -1,5 +1,6 @@ local math, vector, minetest, mcl_mobs = math, vector, minetest, mcl_mobs local mob_class = mcl_mobs.mob_class +local validate_vector = mcl_util.validate_vector local ENTITY_CRAMMING_MAX = 24 local CRAMMING_DAMAGE = 3 @@ -355,7 +356,7 @@ function mob_class:set_yaw(yaw, delay, dtime) if math.abs(target_shortest_path_nums) > 10 then self.object:set_yaw(self.object:get_yaw()+(target_shortest_path*(3.6*ddtime))) - if self.acc then + if validate_vector(self.acc) then self.acc=vector.rotate_around_axis(self.acc,vector.new(0,1,0), target_shortest_path*(3.6*ddtime)) end end