From ef980f2ea058d7645be85611f767f528639ae529 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 16:38:21 -0700 Subject: [PATCH 1/8] Make mobs have smooth turning --- mods/ENTITIES/mcl_mobs/api.lua | 67 +++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 093c56ace..55f9d54a2 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -16,6 +16,29 @@ local CRAMMING_DAMAGE = 3 -- Localize local S = minetest.get_translator("mcl_mobs") +local function shortest_term_of_yaw_rotatoin(self, rot_origin, rot_target, nums) + + if not rot_origin or not rot_target then + return + end + + rot_origin = math.deg(rot_origin) + rot_target = math.deg(rot_target) + + + + if math.abs(rot_target - rot_origin) < 180 then + return rot_target - rot_origin + else + if (rot_target - rot_origin) > 0 then + return 360-(rot_target - rot_origin) + else + return (rot_target - rot_origin)+360 + end + end +end + + -- Invisibility mod check mcl_mobs.invis = {} @@ -309,16 +332,47 @@ local function update_roll(self) end -- set and return valid yaw + + local set_yaw = function(self, yaw, delay, dtime) if self.noyaw then return end if true then - self.object:set_yaw(yaw) - return yaw - end + self._turn_to = yaw + --clamp our yaw to a 360 range + if math.deg(self.object:get_yaw()) > 360 then + self.object:set_yaw(math.rad(10)) + elseif math.deg(self.object:get_yaw()) < 0 then + self.object:set_yaw(math.rad(350)) + end - if not yaw or yaw ~= yaw then - yaw = 0 + --calculate the shortest way to turn to find our target + local target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), yaw, true) + + --turn in the shortest path possible toward our target. if we are attacking, don't dance. + if math.abs(target_shortest_path) > 100 and (self.attack and self.attack:get_pos() or self.following and self.following:get_pos()) then + if self.following then + target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.following:get_pos())), true) + else + target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.attack:get_pos())), true) + end + end + + local ddtime = 0.05 --set_tick_rate + + if dtime then + ddtime = dtime + end + + if math.abs(target_shortest_path) > 120*ddtime then + if target_shortest_path > 0 then + self.object:set_yaw(self.object:get_yaw()+3*ddtime) + else + self.object:set_yaw(self.object:get_yaw()-3*ddtime) + end + end + + return yaw end delay = delay or 0 @@ -3669,6 +3723,9 @@ local mob_step = function(self, dtime) end -- smooth rotation by ThomasMonroe314 + if self._turn_to and (not self.type == "monster" and self.state == "attack") then + set_yaw(self, self._turn_to, .1) + end if self.delay and self.delay > 0 then From 4a086db4c5a65209ca739b5e003d4648f61bcdd8 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 16:57:26 -0700 Subject: [PATCH 2/8] get rid of unecessary "if true" statement --- mods/ENTITIES/mcl_mobs/api.lua | 65 +++++++++++++++++----------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 55f9d54a2..f4c2f497a 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -336,52 +336,51 @@ end local set_yaw = function(self, yaw, delay, dtime) + if self.noyaw then return end - if true then - self._turn_to = yaw - --clamp our yaw to a 360 range - if math.deg(self.object:get_yaw()) > 360 then - self.object:set_yaw(math.rad(10)) - elseif math.deg(self.object:get_yaw()) < 0 then - self.object:set_yaw(math.rad(350)) + + self._turn_to = yaw + --clamp our yaw to a 360 range + if math.deg(self.object:get_yaw()) > 360 then + self.object:set_yaw(math.rad(10)) + elseif math.deg(self.object:get_yaw()) < 0 then + self.object:set_yaw(math.rad(350)) + end + + --calculate the shortest way to turn to find our target + local target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), yaw, true) + + --turn in the shortest path possible toward our target. if we are attacking, don't dance. + if math.abs(target_shortest_path) > 100 and (self.attack and self.attack:get_pos() or self.following and self.following:get_pos()) then + if self.following then + target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.following:get_pos())), true) + else + target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.attack:get_pos())), true) end + end - --calculate the shortest way to turn to find our target - local target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), yaw, true) + local ddtime = 0.05 --set_tick_rate - --turn in the shortest path possible toward our target. if we are attacking, don't dance. - if math.abs(target_shortest_path) > 100 and (self.attack and self.attack:get_pos() or self.following and self.following:get_pos()) then - if self.following then - target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.following:get_pos())), true) - else - target_shortest_path = shortest_term_of_yaw_rotatoin(self, self.object:get_yaw(), minetest.dir_to_yaw(vector.direction(self.object:get_pos(), self.attack:get_pos())), true) - end + if dtime then + ddtime = dtime + end + + if math.abs(target_shortest_path) > 120*ddtime then + if target_shortest_path > 0 then + self.object:set_yaw(self.object:get_yaw()+3*ddtime) + else + self.object:set_yaw(self.object:get_yaw()-3*ddtime) end - - local ddtime = 0.05 --set_tick_rate - - if dtime then - ddtime = dtime - end - - if math.abs(target_shortest_path) > 120*ddtime then - if target_shortest_path > 0 then - self.object:set_yaw(self.object:get_yaw()+3*ddtime) - else - self.object:set_yaw(self.object:get_yaw()-3*ddtime) - end - end - - return yaw end delay = delay or 0 + yaw = self.object:get_yaw() + if delay == 0 then if self.shaking and dtime then yaw = yaw + (random() * 2 - 1) * 5 * dtime end - self.object:set_yaw(yaw) update_roll(self) return yaw end From c500dc98f9dbf78f75f22f1e38223801099c1cc6 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 17:23:14 -0700 Subject: [PATCH 3/8] fix only hostile mobs using smooth turning --- 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 f4c2f497a..482519105 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -3722,7 +3722,7 @@ local mob_step = function(self, dtime) end -- smooth rotation by ThomasMonroe314 - if self._turn_to and (not self.type == "monster" and self.state == "attack") then + if self._turn_to then set_yaw(self, self._turn_to, .1) end From 090c5b086aae31c611ca01f2e2df84630a3be53e Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 17:29:28 -0700 Subject: [PATCH 4/8] fix mobs shaking a lot --- 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 482519105..6e21c05c8 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -365,7 +365,7 @@ local set_yaw = function(self, yaw, delay, dtime) ddtime = dtime end - if math.abs(target_shortest_path) > 120*ddtime then + if math.abs(target_shortest_path) > 200*ddtime then if target_shortest_path > 0 then self.object:set_yaw(self.object:get_yaw()+3*ddtime) else From a16e8f04038e3b13fb6ab6395f1a033836dfc2ed Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 17:41:50 -0700 Subject: [PATCH 5/8] remove all glitchy shaking --- mods/ENTITIES/mcl_mobs/api.lua | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 6e21c05c8..6eac022a3 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -365,11 +365,13 @@ local set_yaw = function(self, yaw, delay, dtime) ddtime = dtime end - if math.abs(target_shortest_path) > 200*ddtime then + minetest.chat_send_all(math.abs(target_shortest_path)) + + if math.abs(target_shortest_path) > 280*ddtime then if target_shortest_path > 0 then - self.object:set_yaw(self.object:get_yaw()+3*ddtime) + self.object:set_yaw(self.object:get_yaw()+1.5*ddtime) else - self.object:set_yaw(self.object:get_yaw()-3*ddtime) + self.object:set_yaw(self.object:get_yaw()-1.5*ddtime) end end From 8a63e90e4a541df5cec039f5789e722d24dea822 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:26:20 -0700 Subject: [PATCH 6/8] remove debug message --- mods/ENTITIES/mcl_mobs/api.lua | 2 -- 1 file changed, 2 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 6eac022a3..f67a09403 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -365,8 +365,6 @@ local set_yaw = function(self, yaw, delay, dtime) ddtime = dtime end - minetest.chat_send_all(math.abs(target_shortest_path)) - if math.abs(target_shortest_path) > 280*ddtime then if target_shortest_path > 0 then self.object:set_yaw(self.object:get_yaw()+1.5*ddtime) From 9cf5b2a9f638adb56ccb331f4dc3523e44f4f917 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:40:41 -0700 Subject: [PATCH 7/8] make mobs rotate when punched --- mods/ENTITIES/mcl_mobs/api.lua | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index f67a09403..d4f493c04 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -339,7 +339,9 @@ local set_yaw = function(self, yaw, delay, dtime) if self.noyaw then return end - self._turn_to = yaw + if self._kb_turn then + self._turn_to = yaw + end --clamp our yaw to a 360 range if math.deg(self.object:get_yaw()) > 360 then self.object:set_yaw(math.rad(10)) @@ -367,9 +369,9 @@ local set_yaw = function(self, yaw, delay, dtime) if math.abs(target_shortest_path) > 280*ddtime then if target_shortest_path > 0 then - self.object:set_yaw(self.object:get_yaw()+1.5*ddtime) + self.object:set_yaw(self.object:get_yaw()+3.6*ddtime) else - self.object:set_yaw(self.object:get_yaw()-1.5*ddtime) + self.object:set_yaw(self.object:get_yaw()-3.6*ddtime) end end @@ -3364,6 +3366,13 @@ local mob_punch = function(self, hitter, tflp, tool_capabilities, dir) elseif luaentity and luaentity._knockback then kb = kb + luaentity._knockback end + self._kb_turn = false + self._turn_to=self.object:get_yaw()+1.57 + minetest.after(0.5, function() + if self and self.object then + self._kb_turn = true + end + end) self.object:set_velocity({ x = dir.x * kb, From 8cd093afa96f5dc29d758ee20fc93f9543185719 Mon Sep 17 00:00:00 2001 From: epCode <64379263+epCode@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:43:41 -0700 Subject: [PATCH 8/8] adjust values --- mods/ENTITIES/mcl_mobs/api.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index d4f493c04..d0972d050 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -3366,9 +3366,9 @@ local mob_punch = function(self, hitter, tflp, tool_capabilities, dir) elseif luaentity and luaentity._knockback then kb = kb + luaentity._knockback end - self._kb_turn = false - self._turn_to=self.object:get_yaw()+1.57 - minetest.after(0.5, function() + --self._kb_turn = false + self._turn_to=self.object:get_yaw()+0.85 + minetest.after(0.2, function() if self and self.object then self._kb_turn = true end