When mob out of range, avoid processing expensive or unneccessary things

This commit is contained in:
ancientmarinerdev 2023-03-07 18:39:29 +00:00 committed by Gitea
parent 85fe29e5d3
commit ce6d9d561f
3 changed files with 46 additions and 69 deletions

View File

@ -392,7 +392,10 @@ local function on_step_work (self, dtime)
end end
if self:falling(pos) then return 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 if not self.fire_resistant then
mcl_burning.tick(self.object, dtime, self) mcl_burning.tick(self.object, dtime, self)
@ -411,11 +414,15 @@ local function on_step_work (self, dtime)
self:check_water_flow() self:check_water_flow()
self:env_danger_movement_checks (dtime) self:env_danger_movement_checks (dtime)
self:follow_flop() -- Mob following code. -- 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:set_animation_speed() -- set animation speed relative to velocity self:flop()
self:check_smooth_rotation(dtime) self:check_smooth_rotation(dtime)
if not player_in_active_range then
self:set_animation_speed() -- set animation speed relative to velocity
self:check_head_swivel(dtime) self:check_head_swivel(dtime)
if self.jump_sound_cooloff > 0 then self.jump_sound_cooloff = self.jump_sound_cooloff - dtime end if self.jump_sound_cooloff > 0 then self.jump_sound_cooloff = self.jump_sound_cooloff - dtime end
@ -424,27 +431,25 @@ local function on_step_work (self, dtime)
self:check_runaway_from() self:check_runaway_from()
self:monster_attack() self:monster_attack()
self:npc_attack() self:npc_attack()
end
self:check_aggro(dtime) self:check_aggro(dtime)
if self.do_custom and self.do_custom(self, dtime) == false then return end if self.do_custom and self.do_custom(self, dtime) == false then return end
-- In certain circumstances, we abandon processing of certain functionality -- In certain circumstances, we abandon processing of certain functionality
local skip_processing = false local skip_processing = false
if update_timers(self, dtime) then if update_timers(self, dtime) then
skip_processing = true skip_processing = true
end end
if not skip_processing then if not skip_processing then
self:check_breeding() self:check_breeding()
if not player_in_active_range then
self:check_item_pickup() self:check_item_pickup()
self:set_armor_texture() self:set_armor_texture()
self:check_particlespawners(dtime)
if self.opinion_sound_cooloff > 0 then if self.opinion_sound_cooloff > 0 then
self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime self.opinion_sound_cooloff = self.opinion_sound_cooloff - dtime
end end
@ -452,12 +457,13 @@ local function on_step_work (self, dtime)
if math.random(1, 70) == 1 then if math.random(1, 70) == 1 then
self:mob_sound("random", true) self:mob_sound("random", true)
end end
end
self:check_particlespawners(dtime)
if self:do_states(dtime) then return end if self:do_states(dtime) then return end
end end
if mobs_debug then self:update_tag() end if mobs_debug then self:update_tag() end
if not self.object:get_luaentity() then if not self.object:get_luaentity() then

View File

@ -614,75 +614,51 @@ end
-- follow player if owner or holding item, if fish outta water then flop -- 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 -- find player to follow
if (self.follow ~= "" if (self.follow ~= "" or self.order == "follow") and not self.following
or self.order == "follow")
and not self.following
and self.state ~= "attack" and self.state ~= "attack"
and self.order ~= "sit" and self.order ~= "sit"
and self.state ~= "runaway" then and self.state ~= "runaway" then
local s = self.object:get_pos() local s = self.object:get_pos()
local players = minetest.get_connected_players() local players = minetest.get_connected_players()
for n = 1, #players do 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] self.following = players[n]
break break
end end
end end
end end
if self.type == "npc" if self.type == "npc" and self.order == "follow"
and self.order == "follow" and self.state ~= "attack" and self.order ~= "sit" and self.owner ~= "" then
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 self.following = nil
end end
else else
-- stop following player if not holding specific item, -- stop following player if not holding specific item,
-- mob is horny, fleeing or attacking -- mob is horny, fleeing or attacking
if self.following if self.following and self.following:is_player()
and self.following:is_player() and (self:follow_holding(self.following) == false or self.horny or self.state == "runaway") then
and (self:follow_holding(self.following) == false or
self.horny or self.state == "runaway") then
self.following = nil self.following = nil
end end
end end
-- follow that thing -- follow that thing
if self.following then if self.following then
local s = self.object:get_pos() local s = self.object:get_pos()
local p local p
if self.following:is_player() then if self.following:is_player() then
p = self.following:get_pos() p = self.following:get_pos()
elseif self.following.object then elseif self.following.object then
p = self.following.object:get_pos() p = self.following.object:get_pos()
end end
if p then if p then
local dist = vector.distance(p, s) local dist = vector.distance(p, s)
-- dont follow if out of range
if (not self:object_in_range(self.following)) then if (not self:object_in_range(self.following)) then
self.following = nil self.following = nil
else else
@ -692,17 +668,12 @@ function mob_class:follow_flop()
} }
local yaw = (atan(vec.z / vec.x) +math.pi/ 2) - self.rotate local yaw = (atan(vec.z / vec.x) +math.pi/ 2) - self.rotate
if p.x > s.x then yaw = yaw +math.pi end if p.x > s.x then yaw = yaw +math.pi end
self:set_yaw( yaw, 2.35) self:set_yaw( yaw, 2.35)
-- anyone but standing npc's can move along -- anyone but standing npc's can move along
if dist > 3 if dist > 3 and self.order ~= "stand" then
and self.order ~= "stand" then
self:set_velocity(self.follow_velocity) self:set_velocity(self.follow_velocity)
if self.walk_chance ~= 0 then if self.walk_chance ~= 0 then
self:set_animation( "run") self:set_animation( "run")
end end
@ -710,17 +681,18 @@ function mob_class:follow_flop()
self:set_velocity(0) self:set_velocity(0)
self:set_animation( "stand") self:set_animation( "stand")
end end
return return
end end
end end
end end
end
function mob_class:flop()
-- swimmers flop when out of their element, and swim again when back in -- swimmers flop when out of their element, and swim again when back in
if self.fly then if self.fly then
local s = self.object:get_pos() local s = self.object:get_pos()
if self:flight_check( s) == false then
if self:flight_check(s) == false then
self.state = "flop" self.state = "flop"
self.object:set_acceleration({x = 0, y = DEFAULT_FALL_SPEED, z = 0}) self.object:set_acceleration({x = 0, y = DEFAULT_FALL_SPEED, z = 0})
@ -739,7 +711,6 @@ function mob_class:follow_flop()
end end
self:set_animation( "stand", true) self:set_animation( "stand", true)
return return
elseif self.state == "flop" then elseif self.state == "flop" then
self.state = "stand" self.state = "stand"

View File

@ -995,10 +995,10 @@ function mob_class:check_dying()
end end
end end
function mob_class:check_suspend() function mob_class:check_suspend(player_in_active_range)
local pos = self.object:get_pos() 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 local node_under = node_ok(vector.offset(pos,0,-1,0)).name
self:set_animation( "stand", true) self:set_animation( "stand", true)