Optimise do states and env danger check

This commit is contained in:
ancientmarinerdev 2023-05-12 21:00:01 +01:00
parent 3d1fb8cf4a
commit f326fa620f
2 changed files with 27 additions and 14 deletions

@ -316,7 +316,7 @@ end
-- execute current state (stand, walk, run, attacks) -- execute current state (stand, walk, run, attacks)
-- returns true if mob has died -- returns true if mob has died
function mob_class:do_states(dtime) function mob_class:do_states(dtime, player_in_active_range)
--if self.can_open_doors then check_doors(self) end --if self.can_open_doors then check_doors(self) end
-- knockback timer. set in on_punch -- knockback timer. set in on_punch
@ -325,7 +325,7 @@ function mob_class:do_states(dtime)
return return
end end
self:env_danger_movement_checks (dtime) self:env_danger_movement_checks(player_in_active_range)
if self.state == PATHFINDING then if self.state == PATHFINDING then
self:check_gowp(dtime) self:check_gowp(dtime)
@ -336,7 +336,7 @@ function mob_class:do_states(dtime)
else else
if mcl_util.check_dtime_timer(self, dtime, "onstep_dostates", 1) then if mcl_util.check_dtime_timer(self, dtime, "onstep_dostates", 1) then
if self.state == "stand" then if self.state == "stand" then
self:do_states_stand() self:do_states_stand(player_in_active_range)
elseif self.state == "walk" then elseif self.state == "walk" then
self:do_states_walk() self:do_states_walk()
elseif self.state == "runaway" then elseif self.state == "runaway" then
@ -452,7 +452,7 @@ local function on_step_work (self, dtime)
end end
end end
if self:do_states(dtime) then return end if self:do_states(dtime, player_in_active_range) then return end
if mobs_debug then self:update_tag() end if mobs_debug then self:update_tag() end

@ -6,6 +6,8 @@ local FLOP_HOR_SPEED = 1.5
local CHECK_HERD_FREQUENCY = 4 local CHECK_HERD_FREQUENCY = 4
local PATHFINDING = "gowp"
local node_snow = "mcl_core:snow" local node_snow = "mcl_core:snow"
local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false local mobs_griefing = minetest.settings:get_bool("mobs_griefing") ~= false
@ -279,10 +281,19 @@ function mob_class:is_at_water_danger()
return false return false
end end
function mob_class:env_danger_movement_checks(dtime) function mob_class:env_danger_movement_checks(player_in_active_range)
local yaw = 0 local yaw = 0
if self.state ~= "attack" and self:is_at_water_danger() then if not player_in_active_range then return end
if self.state == PATHFINDING
or self.state == "attack"
or self.state == "stand"
or self.state == "runaway" then
return
end
if self:is_at_water_danger() then
--minetest.log("At water danger for mob, stop?: " .. self.name) --minetest.log("At water danger for mob, stop?: " .. self.name)
if math.random(1, 10) <= 7 then if math.random(1, 10) <= 7 then
if self.state ~= "stand" then if self.state ~= "stand" then
@ -884,7 +895,7 @@ function mob_class:do_states_walk()
end end
end end
function mob_class:do_states_stand() function mob_class:do_states_stand(player_in_active_range)
local yaw = self.object:get_yaw() or 0 local yaw = self.object:get_yaw() or 0
if math.random(1, 4) == 1 then if math.random(1, 4) == 1 then
@ -928,14 +939,16 @@ function mob_class:do_states_stand()
if self.order == "stand" or self.order == "sleep" or self.order == "work" then if self.order == "stand" or self.order == "sleep" or self.order == "work" then
else else
if self.walk_chance ~= 0 if player_in_active_range then
and self.facing_fence ~= true if self.walk_chance ~= 0
and math.random(1, 100) <= self.walk_chance and self.facing_fence ~= true
and self:is_at_cliff_or_danger() == false then and math.random(1, 100) <= self.walk_chance
and self:is_at_cliff_or_danger() == false then
self:set_velocity(self.walk_velocity) self:set_velocity(self.walk_velocity)
self.state = "walk" self.state = "walk"
self:set_animation( "walk") self:set_animation( "walk")
end
end end
end end
end end