From ce6d9d561fde63557173e4eb43d80a63cdddc6a7 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Tue, 7 Mar 2023 18:39:29 +0000 Subject: [PATCH] When mob out of range, avoid processing expensive or unneccessary things --- mods/ENTITIES/mcl_mobs/api.lua | 56 ++++++++++++++++------------- mods/ENTITIES/mcl_mobs/movement.lua | 55 +++++++--------------------- mods/ENTITIES/mcl_mobs/physics.lua | 4 +-- 3 files changed, 46 insertions(+), 69 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 52d765349..f3c5fb132 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -392,7 +392,10 @@ local function on_step_work (self, dtime) end if self:falling(pos) then return end - self:check_suspend() + + local player_in_active_range = self:player_in_active_range() + + self:check_suspend(player_in_active_range) if not self.fire_resistant then mcl_burning.tick(self.object, dtime, self) @@ -411,53 +414,56 @@ local function on_step_work (self, dtime) self:check_water_flow() self:env_danger_movement_checks (dtime) - self:follow_flop() -- Mob following code. - - self:set_animation_speed() -- set animation speed relative to velocity + -- Follow code is heavy and probably shouldn't run when not in range, but we need to extract the cancel follow stuff + self:follow() + self:flop() self:check_smooth_rotation(dtime) - self:check_head_swivel(dtime) - if self.jump_sound_cooloff > 0 then self.jump_sound_cooloff = self.jump_sound_cooloff - dtime end - self:do_jump() + if not player_in_active_range then + self:set_animation_speed() -- set animation speed relative to velocity + + self:check_head_swivel(dtime) + + if self.jump_sound_cooloff > 0 then self.jump_sound_cooloff = self.jump_sound_cooloff - dtime end + self:do_jump() + + self:check_runaway_from() + self:monster_attack() + self:npc_attack() + end - self:check_runaway_from() - self:monster_attack() - self:npc_attack() self:check_aggro(dtime) if self.do_custom and self.do_custom(self, dtime) == false then return end - -- In certain circumstances, we abandon processing of certain functionality local skip_processing = false if update_timers(self, dtime) then skip_processing = true end - - if not skip_processing then self:check_breeding() - self:check_item_pickup() - self:set_armor_texture() + if not player_in_active_range then + self:check_item_pickup() + self:set_armor_texture() + + if self.opinion_sound_cooloff > 0 then + self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime + end + -- mob plays random sound at times. Should be 120. Zombie and mob farms are ridiculous + if math.random(1, 70) == 1 then + self:mob_sound("random", true) + end + end self:check_particlespawners(dtime) - if self.opinion_sound_cooloff > 0 then - self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime - end - -- mob plays random sound at times. Should be 120. Zombie and mob farms are ridiculous - if math.random(1, 70) == 1 then - self:mob_sound("random", true) - end - if self:do_states(dtime) then return end end - - if mobs_debug then self:update_tag() end if not self.object:get_luaentity() then diff --git a/mods/ENTITIES/mcl_mobs/movement.lua b/mods/ENTITIES/mcl_mobs/movement.lua index 9542a6417..39a732010 100644 --- a/mods/ENTITIES/mcl_mobs/movement.lua +++ b/mods/ENTITIES/mcl_mobs/movement.lua @@ -614,75 +614,51 @@ end -- follow player if owner or holding item, if fish outta water then flop -function mob_class:follow_flop() - +function mob_class:follow() -- find player to follow - if (self.follow ~= "" - or self.order == "follow") - and not self.following + if (self.follow ~= "" or self.order == "follow") and not self.following and self.state ~= "attack" and self.order ~= "sit" and self.state ~= "runaway" then - local s = self.object:get_pos() local players = minetest.get_connected_players() - for n = 1, #players do - - if (self:object_in_range(players[n])) - and not mcl_mobs.invis[ players[n]:get_player_name() ] then - + if (self:object_in_range(players[n])) and not mcl_mobs.invis[ players[n]:get_player_name() ] then self.following = players[n] - break end end end - if self.type == "npc" - and self.order == "follow" - and self.state ~= "attack" - and self.order ~= "sit" - and self.owner ~= "" then + if self.type == "npc" and self.order == "follow" + and self.state ~= "attack" and self.order ~= "sit" and self.owner ~= "" then - -- npc stop following player if not owner - if self.following - and self.owner - and self.owner ~= self.following:get_player_name() then + if self.following and self.owner and self.owner ~= self.following:get_player_name() then self.following = nil end else -- stop following player if not holding specific item, -- mob is horny, fleeing or attacking - if self.following - and self.following:is_player() - and (self:follow_holding(self.following) == false or - self.horny or self.state == "runaway") then + if self.following and self.following:is_player() + and (self:follow_holding(self.following) == false or self.horny or self.state == "runaway") then self.following = nil end - end -- follow that thing if self.following then - local s = self.object:get_pos() + local p - if self.following:is_player() then - p = self.following:get_pos() - elseif self.following.object then - p = self.following.object:get_pos() end if p then - local dist = vector.distance(p, s) - -- dont follow if out of range if (not self:object_in_range(self.following)) then self.following = nil else @@ -692,17 +668,12 @@ function mob_class:follow_flop() } local yaw = (atan(vec.z / vec.x) +math.pi/ 2) - self.rotate - if p.x > s.x then yaw = yaw +math.pi end - self:set_yaw( yaw, 2.35) -- anyone but standing npc's can move along - if dist > 3 - and self.order ~= "stand" then - + if dist > 3 and self.order ~= "stand" then self:set_velocity(self.follow_velocity) - if self.walk_chance ~= 0 then self:set_animation( "run") end @@ -710,17 +681,18 @@ function mob_class:follow_flop() self:set_velocity(0) self:set_animation( "stand") end - return end end end +end +function mob_class:flop() -- swimmers flop when out of their element, and swim again when back in if self.fly then local s = self.object:get_pos() - if self:flight_check( s) == false then + if self:flight_check(s) == false then self.state = "flop" self.object:set_acceleration({x = 0, y = DEFAULT_FALL_SPEED, z = 0}) @@ -739,7 +711,6 @@ function mob_class:follow_flop() end self:set_animation( "stand", true) - return elseif self.state == "flop" then self.state = "stand" diff --git a/mods/ENTITIES/mcl_mobs/physics.lua b/mods/ENTITIES/mcl_mobs/physics.lua index 05a7780d8..7d5eb6c68 100644 --- a/mods/ENTITIES/mcl_mobs/physics.lua +++ b/mods/ENTITIES/mcl_mobs/physics.lua @@ -995,10 +995,10 @@ function mob_class:check_dying() end end -function mob_class:check_suspend() +function mob_class:check_suspend(player_in_active_range) local pos = self.object:get_pos() - if pos and not self:player_in_active_range() then + if pos and not player_in_active_range then local node_under = node_ok(vector.offset(pos,0,-1,0)).name self:set_animation( "stand", true)