From 8092fd573c91cbdd1f7b6ca6f42a3ad929a4291a Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Mon, 8 May 2023 23:16:04 +0100 Subject: [PATCH] Cleanup self.acc code and convert to new style vectors --- mods/ENTITIES/mcl_mobs/combat.lua | 20 ++++++++++++++------ mods/ENTITIES/mcl_mobs/physics.lua | 15 +++++++-------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/combat.lua b/mods/ENTITIES/mcl_mobs/combat.lua index 0ec1044f5..3b53e0d1a 100644 --- a/mods/ENTITIES/mcl_mobs/combat.lua +++ b/mods/ENTITIES/mcl_mobs/combat.lua @@ -830,16 +830,18 @@ local function clear_aggro(self) end function mob_class:do_states_attack (dtime) - local yaw = self.object:get_yaw() or 0 - - local s = self.object:get_pos() - local p = self.attack:get_pos() or s - self.timer = self.timer + dtime if self.timer > 100 then self.timer = 1 end + local s = self.object:get_pos() + if not s then return end + + local p = self.attack:get_pos() or s + + local yaw = self.object:get_yaw() or 0 + -- stop attacking if player invisible or out of range if not self.attack or not self.attack:get_pos() @@ -1141,7 +1143,13 @@ function mob_class:do_states_attack (dtime) if math.random(40) == 1 then self.strafe_direction = self.strafe_direction*-1 end - self.acc = vector.add(vector.multiply(vector.rotate_around_axis(vector.direction(s, p), vector.new(0,1,0), self.strafe_direction), 0.3*self.walk_velocity), stay_away_from_player) + + local dir = vector.rotate_around_axis(vector.direction(s, p), vector.new(0,1,0), self.strafe_direction) + local dir2 = vector.multiply(dir, 0.3 * self.walk_velocity) + + if dir2 and stay_away_from_player then + self.acc = vector.add(dir2, stay_away_from_player) + end else self:set_velocity( 0) end diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 4ef72eed7..2859cfe98 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -198,6 +198,8 @@ end -- move mob in facing direction function mob_class:set_velocity(v) + if not v then return end + local c_x, c_y = 0, 0 -- can mob be pushed, if so calculate direction @@ -207,18 +209,15 @@ function mob_class:set_velocity(v) -- halt mob if it has been ordered to stay if self.order == "stand" or self.order == "sit" then - self.acc=vector.new(0,0,0) - return + self.acc = vector.zero() + return end local yaw = (self.object:get_yaw() or 0) + self.rotate local vv = self.object:get_velocity() - if vv then - self.acc={ - x = ((math.sin(yaw) * -v) + c_x)*.27, - y = 0, - z = ((math.cos(yaw) * v) + c_y)*.27, - } + + if vv and yaw then + self.acc = vector.new(((math.sin(yaw) * -v) + c_x) * .27, 0, ((math.cos(yaw) * v) + c_y) * .27) end end