From ee3b14d4db84120c5be53888dcd88dd8d4094bb6 Mon Sep 17 00:00:00 2001 From: kno10 Date: Tue, 9 Jul 2024 13:51:45 +0200 Subject: [PATCH] fix delay=0 in combat code, tune turning parameters --- mods/ENTITIES/mcl_mobs/combat.lua | 45 +++++++---------------------- mods/ENTITIES/mcl_mobs/movement.lua | 14 ++++----- mods/ENTITIES/mcl_mobs/physics.lua | 36 +++++++++++------------ 3 files changed, 35 insertions(+), 60 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/combat.lua b/mods/ENTITIES/mcl_mobs/combat.lua index 608209ee2..2011e70e8 100644 --- a/mods/ENTITIES/mcl_mobs/combat.lua +++ b/mods/ENTITIES/mcl_mobs/combat.lua @@ -12,15 +12,7 @@ local enable_pathfinding = true local TIME_TO_FORGET_TARGET = 15 -local atann = math.atan -local function atan(x) - if not x or x ~= x then - return 0 - else - return atann(x) - end -end - +local atan2 = math.atan2 -- check if daytime and also if mob is docile during daylight hours function mob_class:day_docile() @@ -925,10 +917,8 @@ function mob_class:do_states_attack (dtime) if self.attack_type == "explode" then if target_line_of_sight then - local vec = { x = p.x - s.x, z = p.z - s.z } - yaw = (atan(vec.z / vec.x) +math.pi/ 2) - self.rotate - if p.x > s.x then yaw = yaw +math.pi end - yaw = self:set_yaw( yaw, 0, dtime) + yaw = -atan2(p.x - s.x, p.z - s.z) - self.rotate + yaw = self:set_yaw(yaw, 1, dtime) end local node_break_radius = self.explosion_radius or 1 @@ -1081,16 +1071,8 @@ function mob_class:do_states_attack (dtime) p = {x = p1.x, y = p1.y, z = p1.z} end - local vec = { - x = p.x - s.x, - z = p.z - s.z - } - - yaw = (atan(vec.z / vec.x) + math.pi / 2) - self.rotate - - if p.x > s.x then yaw = yaw + math.pi end - - yaw = self:set_yaw( yaw, 0, dtime) + yaw = -atan2(p.x - s.x, p.z - s.z) - self.rotate + yaw = self:set_yaw(yaw, 1, dtime) -- move towards enemy if beyond mob reach if dist > self.reach then @@ -1171,18 +1153,14 @@ function mob_class:do_states_attack (dtime) p.y = p.y - .5 s.y = s.y + .5 - local dist = vector.distance(p, s) local vec = { x = p.x - s.x, y = p.y - s.y, z = p.z - s.z } - - yaw = (atan(vec.z / vec.x) +math.pi/ 2) - self.rotate - - if p.x > s.x then yaw = yaw +math.pi end - - yaw = self:set_yaw( yaw, 0, dtime) + local dist = (vec.x^2 + vec.y^2 + vec.z^2)^0.5 + yaw = -atan2(vec.x, vec.z) - self.rotate + yaw = self:set_yaw(yaw, 1, dtime) local stay_away_from_player = vector.zero() @@ -1252,12 +1230,11 @@ function mob_class:do_states_attack (dtime) end end - local amount = (vec.x * vec.x + vec.y * vec.y + vec.z * vec.z) ^ 0.5 -- offset makes shoot aim accurate vec.y = vec.y + self.shoot_offset - vec.x = vec.x * (v / amount) - vec.y = vec.y * (v / amount) - vec.z = vec.z * (v / amount) + vec.x = vec.x * (v / dist) + vec.y = vec.y * (v / dist) + vec.z = vec.z * (v / dist) if self.shoot_arrow then vec = vector.normalize(vec) self:shoot_arrow(p, vec) diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index 1b2a83536..1751013da 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -354,7 +354,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range) self:set_animation("stand") end local yaw = self.object:get_yaw() or 0 - self:set_yaw(yaw + PIHALF * (random() - 0.5), 6) + self:set_yaw(yaw + PIHALF * (random() - 0.5), 10) return end end @@ -367,7 +367,7 @@ function mob_class:env_danger_movement_checks(player_in_active_range) self:set_animation("stand") end local yaw = self.object:get_yaw() or 0 - yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 6) + yaw = self:set_yaw(yaw + PI * (random() - 0.5), 10) end end end @@ -473,7 +473,7 @@ function mob_class:do_jump() self.jump_count = (self.jump_count or 0) + 1 if self.jump_count == 4 then local yaw = self.object:get_yaw() or 0 - yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 8) + yaw = self:set_yaw(yaw + PI * (random() - 0.5), 8) self.jump_count = 0 end end @@ -924,7 +924,7 @@ function mob_class:do_states_walk() if logging then minetest.log("action", "[mcl_mobs] "..self.name.." facing a wall, turning.") end - yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 6) + yaw = self:set_yaw(yaw + PI * (random() - 0.5), 6) -- otherwise randomly turn elseif random() <= 0.3 then yaw = self:set_yaw(yaw + PIHALF * (random() - 0.5), 10) @@ -973,8 +973,8 @@ function mob_class:do_states_stand(player_in_active_range) and self.facing_fence ~= true and random(1, 100) <= self.walk_chance then if self:is_at_cliff_or_danger() then - yaw = yaw + PIHALF * (random() - 0.5) - yaw = self:set_yaw(yaw, 8) + yaw = yaw + PI * (random() - 0.5) + yaw = self:set_yaw(yaw, 10) else self:set_velocity(self.walk_velocity) self.state = "walk" @@ -997,7 +997,7 @@ function mob_class:do_states_runaway() self:set_velocity(0) self.state = "stand" self:set_animation("stand") - yaw = self:set_yaw(yaw + PI * (random() - 0.5), 8) + yaw = self:set_yaw(yaw + PI * (random() + 0.5), 8) else self:set_velocity( self.run_velocity) self:set_animation( "run") diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index ddc1302dd..d97ed0e66 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -283,27 +283,25 @@ function mob_class:check_smooth_rotation(dtime) end local delay = self.delay - if delay and delay > 0 then - local yaw = self.object:get_yaw() or 0 - local target_yaw = self.target_yaw - if delay == 1 then - yaw = target_yaw - else - local dif = (target_yaw - yaw + PI) % TWOPI - PI - yaw = (yaw + dif / delay) % TWOPI - end - + local yaw = self.object:get_yaw() or 0 + local target_yaw = self.target_yaw + if delay and delay > 1 then + local dif = (target_yaw - yaw + PI) % TWOPI - PI + yaw = (yaw + dif / delay) % TWOPI self.delay = delay - 1 - if self.shaking then - yaw = yaw + (random() * 2 - 1) / 72 * dtime - end - self.object:set_yaw(yaw) - -- TODO: needed? - --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 - --self:update_roll() + else + yaw = target_yaw end + + if self.shaking then + yaw = yaw + (random() * 2 - 1) / 72 * dtime + end + self.object:set_yaw(yaw) + -- TODO: needed? + --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 + --self:update_roll() end -- global function to set mob yaw