Optimisation - Only check for town bell if ready to path

This commit is contained in:
ancientmarinerdev 2022-12-09 19:07:45 +00:00 committed by Gitea
parent 9b1ceebf0d
commit 5c0a763b83
2 changed files with 32 additions and 9 deletions

View File

@ -120,22 +120,27 @@ local function calculate_path_through_door (p, t, target)
local pos_closest_to_door = nil local pos_closest_to_door = nil
local other_side_of_door = nil local other_side_of_door = nil
--Path to door first --Check direct route
local wp = minetest.find_path(p,t,150,1,4) local wp = minetest.find_path(p,t,150,1,4)
--Path to door first
if not wp then if not wp then
mcl_log("No direct path. Path through door") mcl_log("No direct path. Path through door")
-- This could improve. There could be multiple doors. Check you can path from door to target first. -- This could improve. There could be multiple doors. Check you can path from door to target first.
-- target could be pos
local cur_door_pos = minetest.find_node_near(target,16,{"group:door"}) local cur_door_pos = minetest.find_node_near(target,16,{"group:door"})
if cur_door_pos then if cur_door_pos then
mcl_log("Found a door near: " .. minetest.pos_to_string(cur_door_pos)) mcl_log("Found a door near: " .. minetest.pos_to_string(cur_door_pos))
for _,v in pairs(plane_adjacents) do for _,v in pairs(plane_adjacents) do
pos_closest_to_door = vector.add(cur_door_pos,v) pos_closest_to_door = vector.add(cur_door_pos,v)
local n = minetest.get_node(pos_closest_to_door) local n = minetest.get_node(pos_closest_to_door)
if n.name == "air" then if n.name == "air" then
wp = minetest.find_path(p,pos_closest_to_door,150,1,4) wp = minetest.find_path(p,pos_closest_to_door,150,1,4)
if wp then if wp then
mcl_log("Found a path to next to door".. minetest.pos_to_string(pos_closest_to_door)) mcl_log("Found a path to next to door".. minetest.pos_to_string(pos_closest_to_door))
other_side_of_door = vector.add(cur_door_pos,-v) other_side_of_door = vector.add(cur_door_pos,-v)
mcl_log("Opposite is: ".. minetest.pos_to_string(other_side_of_door)) mcl_log("Opposite is: ".. minetest.pos_to_string(other_side_of_door))
@ -154,7 +159,7 @@ local function calculate_path_through_door (p, t, target)
mcl_log("This block next to door doesn't work.") mcl_log("This block next to door doesn't work.")
end end
else else
mcl_log("Block is not air, it is: ".. n.name) --mcl_log("Block is not air, it is: ".. n.name)
end end
end end
@ -165,21 +170,33 @@ local function calculate_path_through_door (p, t, target)
mcl_log("We have a direct route") mcl_log("We have a direct route")
end end
-- If not, get door near pos
--path from pos to door, path from otherside to target
if wp and not enriched_path then if wp and not enriched_path then
enriched_path = generate_enriched_path(wp) enriched_path = generate_enriched_path(wp)
end end
return enriched_path return enriched_path
end end
local gopath_last = os.time() --local gopath_last = os.time()
function mob_class:ready_to_path()
mcl_log("Check ready to path")
if self._pf_last_failed and (os.time() - self._pf_last_failed) < 30 then
return false
else
mcl_log("We are ready to pathfind, no previous fail or we are past threshold")
return true
end
end
function mob_class:gopath(target,callback_arrived) function mob_class:gopath(target,callback_arrived)
if self.state == PATHFINDING then mcl_log("Already pathfinding, don't set another until done.") return end if self.state == PATHFINDING then mcl_log("Already pathfinding, don't set another until done.") return end
if self._pf_last_failed and (os.time() - self._pf_last_failed) < 30 then if not self:ready_to_path() then
mcl_log("We are not ready to path as last fail is less than threshold: " .. (os.time() - self._pf_last_failed)) mcl_log("We are not ready to path as last fail is less than threshold: " .. (os.time() - self._pf_last_failed))
return return
else
mcl_log("We are ready to pathfind, no previous fail or we are past threshold")
end end
--if os.time() - gopath_last < 5 then --if os.time() - gopath_last < 5 then

View File

@ -1205,11 +1205,17 @@ end
local function go_to_town_bell(self) local function go_to_town_bell(self)
if self.order == GATHERING then if self.order == GATHERING then
mcl_log("Already gathering") --mcl_log("Already gathering")
return return
else else
mcl_log("Current order" .. self.order) mcl_log("Current order" .. self.order)
end end
if not self:ready_to_path() then
mcl_log("Negative response to go_path. Do not bother")
return
end
mcl_log("Go to town bell") mcl_log("Go to town bell")
local looking_for_type={} local looking_for_type={}
@ -1222,7 +1228,7 @@ local function go_to_town_bell(self)
for _,n in pairs(nn) do for _,n in pairs(nn) do
mcl_log("Found bell") mcl_log("Found bell")
local target_point = get_ground_below_floating_object(n) local target_point = get_ground_below_floating_object(n)
local gp = self:gopath(target_point,function(self) local gp = self:gopath(target_point,function(self)
if self then if self then
self.order = GATHERING self.order = GATHERING