From 2cd6629ae16b2024502aeda9063821eb72127700 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 16 Jan 2023 16:27:11 +0000 Subject: [PATCH 1/7] Add some mob object checks to avoid crashing --- mods/ENTITIES/mcl_mobs/physics.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 0617fd1e8..8610feca8 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -186,10 +186,12 @@ function mob_class:slow_mob() local d = 0.85 if self:check_dying() then d = 0.92 end - local v = self.object:get_velocity() - if v then - --diffuse object velocity - self.object:set_velocity({x = v.x*d, y = v.y, z = v.z*d}) + if self.object then + local v = self.object:get_velocity() + if v then + --diffuse object velocity + self.object:set_velocity({x = v.x*d, y = v.y, z = v.z*d}) + end end end @@ -518,9 +520,11 @@ function mob_class:check_for_death(cause, cmi_cause) }) self:set_velocity(0) - local acc = self.object:get_acceleration() - acc.x, acc.y, acc.z = 0, DEFAULT_FALL_SPEED, 0 - self.object:set_acceleration(acc) + if self.object then + local acc = self.object:get_acceleration() + acc.x, acc.y, acc.z = 0, DEFAULT_FALL_SPEED, 0 + self.object:set_acceleration(acc) + end local length -- default death function and die animation (if defined) @@ -980,9 +984,11 @@ end function mob_class:check_dying() if ((self.state and self.state=="die") or self:check_for_death()) and not self.animation.die_end then - local rot = self.object:get_rotation() - rot.z = ((math.pi/2-rot.z)*.2)+rot.z - self.object:set_rotation(rot) + if self.object then + local rot = self.object:get_rotation() + rot.z = ((math.pi/2-rot.z)*.2)+rot.z + self.object:set_rotation(rot) + end return true end end From 84317afc938b72bd82d03a2f6f901c210290f646 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 16 Jan 2023 23:02:12 +0000 Subject: [PATCH 2/7] Review feedback implemented and planned changes --- mods/ENTITIES/mcl_mobs/api.lua | 9 ++++++++- mods/ENTITIES/mcl_mobs/movement.lua | 1 + mods/ENTITIES/mcl_mobs/physics.lua | 31 +++++++++++++---------------- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 05c6ed9c6..fc334a413 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -335,13 +335,19 @@ function mob_class:on_step(dtime) if self:check_despawn(pos, dtime) then return true end - self:slow_mob() + if self:check_death_and_slow_mob() then + minetest.log("action", "Mob is dying: ".. tostring(self.name)) + -- Do we abandon out of here now? + end + + -- Start: This code logically should be moved to after the die check if self:falling(pos) then return end self:check_suspend() self:check_water_flow() self:env_danger_movement_checks (dtime) + -- End: This code logically should be moved to after the die check if not self.fire_resistant then mcl_burning.tick(self.object, dtime, self) @@ -349,6 +355,7 @@ function mob_class:on_step(dtime) if not self.object:get_pos() then return end end + -- Move to after die check? if mobs_debug then self:update_tag() end if self.state == "die" then return end diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index df00a42a7..758e74467 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -279,6 +279,7 @@ function mob_class:env_danger_movement_checks(dtime) yaw = self:set_yaw( yaw, 8) end else + -- This code should probably be moved to movement code if self.move_in_group ~= false then self:check_herd(dtime) end diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 8610feca8..8fb3dea04 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -182,17 +182,17 @@ function mob_class:collision() return({x,z}) end -function mob_class:slow_mob() +function mob_class:check_death_and_slow_mob() local d = 0.85 - if self:check_dying() then d = 0.92 end + local dying = self:check_dying() + if dying then d = 0.92 end - if self.object then - local v = self.object:get_velocity() - if v then - --diffuse object velocity - self.object:set_velocity({x = v.x*d, y = v.y, z = v.z*d}) - end + local v = self.object:get_velocity() + if v then + --diffuse object velocity + self.object:set_velocity({x = v.x*d, y = v.y, z = v.z*d}) end + return dying end -- move mob in facing direction @@ -520,8 +520,8 @@ function mob_class:check_for_death(cause, cmi_cause) }) self:set_velocity(0) - if self.object then - local acc = self.object:get_acceleration() + local acc = self.object:get_acceleration() + if acc then acc.x, acc.y, acc.z = 0, DEFAULT_FALL_SPEED, 0 self.object:set_acceleration(acc) end @@ -530,10 +530,7 @@ function mob_class:check_for_death(cause, cmi_cause) -- default death function and die animation (if defined) if self.instant_death then length = 0 - elseif self.animation - and self.animation.die_start - and self.animation.die_end then - + elseif self.animation and self.animation.die_start and self.animation.die_end then local frames = self.animation.die_end - self.animation.die_start local speed = self.animation.die_speed or 15 length = math.max(frames / speed, 0) + DEATH_DELAY @@ -549,7 +546,6 @@ function mob_class:check_for_death(cause, cmi_cause) if not self.object:get_luaentity() then return end - death_handle(self) local dpos = self.object:get_pos() local cbox = self.collisionbox @@ -558,6 +554,7 @@ function mob_class:check_for_death(cause, cmi_cause) self.object:remove() mcl_mobs.death_effect(dpos, yaw, cbox, not self.instant_death) end + if length <= 0 then kill(self) else @@ -984,8 +981,8 @@ end function mob_class:check_dying() if ((self.state and self.state=="die") or self:check_for_death()) and not self.animation.die_end then - if self.object then - local rot = self.object:get_rotation() + local rot = self.object:get_rotation() + if rot then rot.z = ((math.pi/2-rot.z)*.2)+rot.z self.object:set_rotation(rot) end From 6bbf3af97bbda60795a6d648f8fe47943185b187 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 16 Jan 2023 23:09:38 +0000 Subject: [PATCH 3/7] Remove log line --- mods/ENTITIES/mcl_mobs/api.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index fc334a413..9597f572b 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -336,7 +336,7 @@ function mob_class:on_step(dtime) if self:check_despawn(pos, dtime) then return true end if self:check_death_and_slow_mob() then - minetest.log("action", "Mob is dying: ".. tostring(self.name)) + --minetest.log("action", "Mob is dying: ".. tostring(self.name)) -- Do we abandon out of here now? end From 15560d969c9f456da3be13e7a06c6d69f7c21501 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 23 Jan 2023 01:03:13 +0000 Subject: [PATCH 4/7] Change order of mob step --- mods/ENTITIES/mcl_mobs/api.lua | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 9597f572b..f5e50ba2a 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -340,26 +340,22 @@ function mob_class:on_step(dtime) -- Do we abandon out of here now? end - -- Start: This code logically should be moved to after the die check if self:falling(pos) then return end self:check_suspend() - self:check_water_flow() - - self:env_danger_movement_checks (dtime) - -- End: This code logically should be moved to after the die check - if not self.fire_resistant then mcl_burning.tick(self.object, dtime, self) -- mcl_burning.tick may remove object immediately if not self.object:get_pos() then return end end - -- Move to after die check? - if mobs_debug then self:update_tag() end - if self.state == "die" then return end + self:check_water_flow() + self:env_danger_movement_checks (dtime) + + if mobs_debug then self:update_tag() end + self:follow_flop() -- Mob following code. self:set_animation_speed() -- set animation speed relitive to velocity From 32be8f960276852985bb792e3707c0405d61ab3f Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 23 Jan 2023 01:28:46 +0000 Subject: [PATCH 5/7] Fix more self object references in falling --- mods/ENTITIES/mcl_mobs/physics.lua | 53 ++++++++++++++---------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 8fb3dea04..6e461c0c7 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -871,33 +871,32 @@ function mob_class:falling(pos) -- floating in water (or falling) local v = self.object:get_velocity() + if v then + if v.y > 0 then + -- apply gravity when moving up + self.object:set_acceleration({ + x = 0, + y = DEFAULT_FALL_SPEED, + z = 0 + }) - if v.y > 0 then - - -- apply gravity when moving up - self.object:set_acceleration({ - x = 0, - y = DEFAULT_FALL_SPEED, - z = 0 - }) - - elseif v.y <= 0 and v.y > self.fall_speed then - - -- fall downwards at set speed - self.object:set_acceleration({ - x = 0, - y = self.fall_speed, - z = 0 - }) - else - -- stop accelerating once max fall speed hit - self.object:set_acceleration({x = 0, y = 0, z = 0}) + elseif v.y <= 0 and v.y > self.fall_speed then + -- fall downwards at set speed + self.object:set_acceleration({ + x = 0, + y = self.fall_speed, + z = 0 + }) + else + -- stop accelerating once max fall speed hit + self.object:set_acceleration({x = 0, y = 0, z = 0}) + end end + local acc = self.object:get_acceleration() + if minetest.registered_nodes[node_ok(pos).name].groups.lava then - - if self.floats_on_lava == 1 then - + if acc and self.floats_on_lava == 1 then self.object:set_acceleration({ x = 0, y = -self.fall_speed / (math.max(1, v.y) ^ 2), @@ -908,9 +907,7 @@ function mob_class:falling(pos) -- in water then float up if minetest.registered_nodes[node_ok(pos).name].groups.water then - - if self.floats == 1 then - + if acc and self.floats == 1 then self.object:set_acceleration({ x = 0, y = -self.fall_speed / (math.max(1, v.y) ^ 2), @@ -918,10 +915,8 @@ function mob_class:falling(pos) }) end else - -- fall damage onto solid ground - if self.fall_damage == 1 - and self.object:get_velocity().y == 0 then + if self.fall_damage == 1 and self.object:get_velocity().y == 0 then local n = node_ok(vector.offset(pos,0,-1,0)).name local d = (self.old_y or 0) - self.object:get_pos().y From af86e732807cd4c3c4aedf3ae8268b69805fc5e2 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 23 Jan 2023 01:33:28 +0000 Subject: [PATCH 6/7] Fix self object reference in player_in_active_range --- mods/ENTITIES/mcl_mobs/physics.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 6e461c0c7..e5364c88a 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -47,7 +47,8 @@ end function mob_class:player_in_active_range() for _,p in pairs(minetest.get_connected_players()) do - if vector.distance(self.object:get_pos(),p:get_pos()) <= mob_active_range then return true end + local pos = self.object:get_pos() + if pos and vector.distance(pos, p:get_pos()) <= mob_active_range then return true end -- slightly larger than the mc 32 since mobs spawn on that circle and easily stand still immediately right after spawning. end end From e4f26a4688e8182013d90ddfd5ad48db64ec1b52 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 23 Jan 2023 01:44:52 +0000 Subject: [PATCH 7/7] Fix self object checks for check suspend --- mods/ENTITIES/mcl_mobs/physics.lua | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index e5364c88a..c7ea98c57 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -987,17 +987,22 @@ function mob_class:check_dying() end function mob_class:check_suspend() - if not self:player_in_active_range() then - local pos = self.object:get_pos() + local pos = self.object:get_pos() + + if pos and not self:player_in_active_range() then local node_under = node_ok(vector.offset(pos,0,-1,0)).name - local acc = self.object:get_acceleration() + self:set_animation( "stand", true) - if acc.y > 0 or node_under ~= "air" then - self.object:set_acceleration(vector.new(0,0,0)) - self.object:set_velocity(vector.new(0,0,0)) - end - if acc.y == 0 and node_under == "air" then - self:falling(pos) + + local acc = self.object:get_acceleration() + if acc then + if acc.y > 0 or node_under ~= "air" then + self.object:set_acceleration(vector.new(0,0,0)) + self.object:set_velocity(vector.new(0,0,0)) + end + if acc.y == 0 and node_under == "air" then + self:falling(pos) + end end return true end