From 5eda59dc135972d8b48ec8802f230c0b4a119dee Mon Sep 17 00:00:00 2001 From: kno10 Date: Sat, 13 Jul 2024 16:04:04 +0200 Subject: [PATCH] reduce code duplication, add mob:stand() --- mods/ENTITIES/mcl_mobs/movement.lua | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index 801b2b010..65c70a2a3 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -26,6 +26,13 @@ local PIQUARTER = 0.25 * math.pi local registered_fallback_node = minetest.registered_nodes[mcl_mobs.fallback_node] +-- Stop movement and stand +function mob_class:stand() + self:set_velocity(0) + self.state = "stand" + self:set_animation("stand") +end + -- get node but use fallback for nil or unknown local node_ok = function(pos, fallback) local node = minetest.get_node_or_nil(pos) @@ -351,9 +358,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range) end if random() <= 0.8 then if self.state ~= "stand" then - self:set_velocity(0) - self.state = "stand" - self:set_animation("stand") + self:stand() end local yaw = self.object:get_yaw() or 0 self:set_yaw(yaw + PIHALF * (random() - 0.5), 10) @@ -366,9 +371,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range) end if random() <= 0.99 then if self.state ~= "stand" then - self:set_velocity(0) - self.state = "stand" - self:set_animation("stand") + self:stand() end local yaw = self.object:get_yaw() or 0 yaw = self:set_yaw(yaw + PI * (random() - 0.5), 10) @@ -871,9 +874,7 @@ function mob_class:do_states_walk() yaw = yaw + random() - 0.5 self:set_yaw(yaw, 8) end - self:set_velocity(0) - self.state = "stand" - self:set_animation("stand") + self:stand() yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 6) return elseif logging then @@ -905,9 +906,7 @@ function mob_class:do_states_walk() end -- stop at fences or randomly if self.facing_fence == true or random() <= 0.3 then - self:set_velocity(0) - self.state = "stand" - self:set_animation("stand") + self:stand() return end -- facing wall? then turn @@ -998,9 +997,7 @@ function mob_class:do_states_runaway() if self.runaway_timer > 5 or self:is_at_cliff_or_danger() then self.runaway_timer = 0 - self:set_velocity(0) - self.state = "stand" - self:set_animation("stand") + self:stand() yaw = self:set_yaw(yaw + PI * (random() + 0.5), 8) else self:set_velocity( self.run_velocity)