Compare commits

...

4 Commits

Author SHA1 Message Date
kno10 250fc73e92 More tweaks to pathfinding 2024-10-29 00:57:42 +01:00
kno10 9ea8945ff4 Fixup fences 2024-10-28 23:59:28 +01:00
kno10 488f61ba1b Make villagers hurry for long paths and night 2024-10-28 23:54:41 +01:00
kno10 16e17ba1ed avoid trivial fences, open some fence gates 2024-10-28 23:52:44 +01:00
2 changed files with 126 additions and 95 deletions

View File

@ -368,6 +368,10 @@ function mob_class:do_jump()
-- what is in front of mob?
local nod = node_ok(vector_offset(pos, dir_x, 0.5, dir_z))
local ndef = minetest.registered_nodes[nod.name]
-- thin blocks that do not need to be jumped
if nod.name == node_snow or (ndef and ndef.groups.carpet or 0) > 0 then return false end
-- this is used to detect if there's a block on top of the block in front of the mob.
-- If there is, there is no point in jumping as we won't manage.
@ -378,15 +382,9 @@ function mob_class:do_jump()
local ntdef = minetest.registered_nodes[node_top.name]
if ntdef and ntdef.walkable == true and not (self.attack and self.state == "attack") then return false end
-- thin blocks that do not need to be jumped
if nod.name == node_snow then return false end
local ndef = minetest.registered_nodes[nod.name]
if self.walk_chance ~= 0 and not (ndef and ndef.walkable) and not self._can_jump_cliff then return false end
if minetest.get_item_group(nod.name, "fence") ~= 0
or minetest.get_item_group(nod.name, "fence_gate") ~= 0
or minetest.get_item_group(nod.name, "wall") ~= 0 then
if (ndef.groups.fence or 0) ~= 0 or (ndef.groups.fence_gate or 0) ~= 0 or (ndef.groups.wall or 0) ~= 0 then
self.facing_fence = true
return false
end
@ -657,15 +655,16 @@ function mob_class:flop()
end
end
function mob_class:go_to_pos(b)
function mob_class:go_to_pos(b, speed)
if not self then return end
if not b then return end
local s = self.object:get_pos()
if vector_distance(b,s) < .4 then return true end
if b.y > s.y + 0.2 then self:do_jump() end
self:turn_in_direction(b.x - s.x, b.z - s.z, 2)
self:set_velocity(self.walk_velocity)
self:set_animation("walk")
speed = speed or self.walk_velocity
self:set_velocity(speed)
self:set_animation(speed <= self.walk_velocity and "walk" or "run")
end
local check_herd_timer = 0

View File

@ -9,9 +9,6 @@ local PATHFINDING_SEARCH_DISTANCE = 25 -- How big the square is that pathfinding
local PATHFINDING = "gowp"
local one_down = vector.new(0,-1,0)
local one_up = vector.new(0,1,0)
local plane_adjacents = {
vector.new(1,0,0),
vector.new(-1,0,0),
@ -69,38 +66,22 @@ end
-- an action, such as to open or close a door where we know that pos requires that action
local function generate_enriched_path(wp_in, door_open_pos, door_close_pos, cur_door_pos)
local wp_out = {}
-- TODO Just pass in door position and the index before is open, the index after is close
local current_door_index = -1
for i, cur_pos in pairs(wp_in) do
local action = nil
local cur_pos_to_add -- = vector.add(cur_pos, one_down)
if door_open_pos and vector.equals (cur_pos, door_open_pos) then
if door_open_pos and vector.equals(cur_pos, door_open_pos) then
mcl_log ("Door open match")
action = {type = "door", action = "open", target = cur_door_pos}
cur_pos_to_add = vector.add(cur_pos, one_down)
elseif door_close_pos and vector.equals(cur_pos, door_close_pos) then
mcl_log ("Door close match")
action = {type = "door", action = "close", target = cur_door_pos}
cur_pos_to_add = vector.add(cur_pos, one_down)
elseif cur_door_pos and vector.equals(cur_pos, cur_door_pos) then
mcl_log("Current door pos")
action = {type = "door", action = "open", target = cur_door_pos}
cur_pos_to_add = vector.add(cur_pos, one_down)
else
cur_pos_to_add = cur_pos
--mcl_log ("Pos doesn't match")
end
if visualize then
core.add_particle({pos = cur_pos_to_add, expirationtime=5+i/4, size=3+2/i, velocity=vector.new(0,-0.01,0),
texture="mcl_copper_anti_oxidation_particle.png"}) -- white stars
end
wp_out[i] = {}
wp_out[i]["pos"] = cur_pos_to_add
wp_out[i]["pos"] = cur_pos
wp_out[i]["failed_attempts"] = 0
wp_out[i]["action"] = action
@ -130,33 +111,23 @@ end
-- This function is used to see if we can path. We could use to check a route, rather than making people move.
local function calculate_path_through_door (p, cur_door_pos, t)
if not cur_door_pos then return end
if t then
mcl_log("Plot route through door from pos: " .. minetest.pos_to_string(p) .. ", to target: " .. minetest.pos_to_string(t))
mcl_log("Plot route through door from pos: " .. minetest.pos_to_string(p) .. " through " .. minetest.pos_to_string(cur_door_pos) .. ", to target: " .. minetest.pos_to_string(t))
else
mcl_log("Plot route through door from pos: " .. minetest.pos_to_string(p))
mcl_log("Plot route through door from pos: " .. minetest.pos_to_string(p) .. " through " .. minetest.pos_to_string(cur_door_pos))
end
local enriched_path = nil
local wp, prospective_wp
local pos_closest_to_door = nil
local other_side_of_door = nil
if cur_door_pos then
mcl_log("Found a door near: " .. minetest.pos_to_string(cur_door_pos))
for _,v in pairs(plane_adjacents) do
pos_closest_to_door = vector.add(cur_door_pos,v)
other_side_of_door = vector.add(cur_door_pos,-v)
local pos_closest_to_door = vector.add(cur_door_pos,v)
local n = minetest.get_node(pos_closest_to_door)
if not n.walkable then
mcl_log("We have open space next to door at: " .. minetest.pos_to_string(pos_closest_to_door))
if n.name == "air" then
mcl_log("We have air space next to door at: " .. minetest.pos_to_string(pos_closest_to_door))
prospective_wp = minetest.find_path(p, pos_closest_to_door, PATHFINDING_SEARCH_DISTANCE, 1, 4)
local prospective_wp = minetest.find_path(p, pos_closest_to_door, PATHFINDING_SEARCH_DISTANCE, 1, 4)
if prospective_wp then
local other_side_of_door = vector.add(cur_door_pos,-v)
mcl_log("Found a path to next to door".. minetest.pos_to_string(pos_closest_to_door))
mcl_log("Opposite is: ".. minetest.pos_to_string(other_side_of_door))
@ -168,36 +139,21 @@ local function calculate_path_through_door (p, cur_door_pos, t)
if wp_otherside_door_to_target and #wp_otherside_door_to_target > 0 then
append_paths (prospective_wp, wp_otherside_door_to_target)
wp = prospective_wp
mcl_log("We have a path from outside door to target")
return generate_enriched_path(prospective_wp, pos_closest_to_door, other_side_of_door, cur_door_pos)
else
mcl_log("We cannot path from outside door to target")
end
else
mcl_log("No t, just add other side of door")
table.insert(prospective_wp, other_side_of_door)
wp = prospective_wp
end
if wp then
enriched_path = generate_enriched_path(wp, pos_closest_to_door, other_side_of_door, cur_door_pos)
break
return generate_enriched_path(prospective_wp, pos_closest_to_door, other_side_of_door, cur_door_pos)
end
else
mcl_log("Cannot path to this air block next to door.")
end
end
end
else
mcl_log("No door found")
end
if wp and not enriched_path then
mcl_log("Wp but not enriched")
enriched_path = generate_enriched_path(wp)
end
return enriched_path
end
-- we treat ignore as solid, as we cannot path there
@ -207,8 +163,9 @@ local function is_solid(pos)
end
local function find_open_node(pos, radius)
if not is_solid(pos) then return pos end
local above = vector.offset(pos, 0, 1, 0)
local r = vector.round(pos)
if not is_solid(r) then return r end
local above = vector.offset(r, 0, 1, 0)
if not is_solid(above) then return above, true end -- additional return: drop last
local n = minetest.find_node_near(pos, radius or 1, {"air"})
if n then return n end
@ -225,7 +182,7 @@ function mob_class:gopath(target, callback_arrived, prioritised)
-- maybe feet are buried in solid?
local start = self.object:get_pos()
local p = find_open_node(vector.round(start), 1)
local p = find_open_node(start, 1)
if not p then -- buried?
minetest.log("action", "Cannot path from "..minetest.pos_to_string(start).." because it is solid. Nodetype: "..minetest.get_node(start).name)
return
@ -243,15 +200,15 @@ function mob_class:gopath(target, callback_arrived, prioritised)
if not wp then
mcl_log("### No direct path. Path through door closest to target.")
local door_near_target = minetest.find_node_near(target, 16, {"group:door"})
local below = vector.offset(door_near_target, 0, -1, 0)
if minetest.get_item_group(minetest.get_node(below), "door") > 0 then door_near_target = below end
local below = door_near_target and vector.offset(door_near_target, 0, -1, 0)
if below and minetest.get_item_group(minetest.get_node(below), "door") > 0 then door_near_target = below end
wp = calculate_path_through_door(p, door_near_target, t)
if not wp then
mcl_log("### No path though door closest to target. Try door closest to origin.")
local door_closest = minetest.find_node_near(p, 16, {"group:door"})
local below = vector.offset(door_closest, 0, -1, 0)
if minetest.get_item_group(minetest.get_node(below), "door") > 0 then door_closest = below end
local below = door_closest and vector.offset(door_closest, 0, -1, 0)
if below and minetest.get_item_group(minetest.get_node(below), "door") > 0 then door_closest = below end
wp = calculate_path_through_door(p, door_closest, t)
-- Path through 2 doors
@ -297,9 +254,73 @@ function mob_class:gopath(target, callback_arrived, prioritised)
-- If cannot path, don't immediately try again
end
-- todo: we would also need to avoid overhangs, but minetest.find_path cannot help us there
-- we really need a better pathfinder overall.
-- try to find a way around fences and walls. This is very barebones, but at least it should
-- help path around very simple fences *IF* there is a detour that does not require jumping or gates.
if wp and #wp > 0 then
local i = 1
while i < #wp do
-- fence or wall underneath?
local bdef = minetest.registered_nodes[minetest.get_node(vector.offset(wp[i].pos, 0, -1, 0)).name]
if not bdef then minetest.log("warning", "There must not be unknown nodes on path") end
-- carpets are fine
if bdef and (bdef.groups.carpet or 0) > 0 then
wp[i].pos = vector.offset(wp[i].pos, 0, -1, 0)
-- target bottom of door
elseif bdef and (bdef.groups.door or 0) > 0 then
wp[i].pos = vector.offset(wp[i].pos, 0, -1, 0)
-- not walkable?
elseif bdef and not bdef.walkable then
wp[i].pos = vector.offset(wp[i].pos, 0, -1, 0)
i = i - 1
-- plan opening fence gates
elseif bdef and (bdef.groups.fence_gate or 0) > 0 then
wp[i].pos = vector.offset(wp[i].pos, 0, -1, 0)
wp[math.max(1,i-1)].action = {type = "door", action = "open", target = wp[i].pos}
if i+1 < #wp then
wp[i+1].action = {type = "door", action = "close", target = wp[i].pos}
end
-- do not jump on fences and walls, but try to walk around
elseif bdef and i > 1 and ((bdef.groups.fence or 0) > 0 or (bdef.groups.wall or 0) > 0) and wp[i].pos.y > wp[i-1].pos.y then
-- find end of wall(s)
local j = i + 1
while j <= #wp do
local below = vector.offset(wp[j].pos, 0, -1, 0)
local bdef = minetest.registered_nodes[minetest.get_node(below).name]
if not bdef or ((bdef.groups.fence or 0) == 0 and (bdef.groups.wall or 0) == 0) then
break
end
j = j + 1
end
minetest.log("warning", bdef.name .. " at "..tostring(i).." end at "..(j <= #wp and tostring(j) or "nil"))
if j <= #wp and wp[i-1].pos.y == wp[j].pos.y then
local swp = minetest.find_path(wp[i-1].pos, wp[j].pos, PATHFINDING_SEARCH_DISTANCE, 0, 0)
-- TODO: if we do not find a path here, consider pathing through a fence gate!
if swp and #swp > 0 then
for k = j-1,i,-1 do table.remove(wp, k) end
for k = 2, #swp-1 do table.insert(wp, i-2+k, {pos = swp[k], failed_attempts = 0}) end
minetest.log("warning", "Monkey patch pathfinding around "..bdef.name.." successful.")
i = i + #swp - 4
else
minetest.log("warning", "Monkey patch pathfinding around "..bdef.name.." failed.")
end
end
end
i = i + 1
end
end
if wp and drop_last_wp and vector.equals(wp[#wp], t) then table.remove(wp, #wp) end
if wp and #wp > 0 then
if visualize then
for i = 1,#wp do
core.add_particle({pos = wp[i].pos, expirationtime=3+i/3, size=3+2/i, velocity=vector.new(0,-0.02,0),
texture="mcl_copper_anti_oxidation_particle.png"}) -- white stars
end
end
--output_table(wp)
if drop_last_wp and #wp > 1 then table.remove(wp, #wp) end
self._target = t
self.callback_arrived = callback_arrived
self.current_target = table.remove(wp,1)
@ -337,11 +358,21 @@ function mob_class:interact_with_door(action, target)
if closed and action == "open" and def.on_rightclick then
mcl_log("Open door")
def.on_rightclick(target,n,self)
end
if not closed and action == "close" and def.on_rightclick then
elseif not closed and action == "close" and def.on_rightclick then
mcl_log("Close door")
def.on_rightclick(target,n,self)
end
elseif n.name:find("_gate") then
local def = minetest.registered_nodes[n.name]
local meta = minetest.get_meta(target)
local closed = meta:get_int("state") == 0
if closed and action == "open" and def.on_rightclick then
mcl_log("Open gate")
def.on_rightclick(target,n,self)
elseif not closed and action == "close" and def.on_rightclick then
mcl_log("Close gate")
def.on_rightclick(target,n,self)
end
else
mcl_log("Not door")
end
@ -429,14 +460,15 @@ function mob_class:check_gowp(dtime)
local failed_attempts = self.current_target["failed_attempts"]
mcl_log("There after " .. failed_attempts .. " failed attempts. current target:".. minetest.pos_to_string(self.current_target.pos) .. ". Distance: " .. distance_to_current_target)
local hurry = (self.order == "sleep" or #self.waypoints > 15) and self.run_velocity or self.walk_velocity
self.current_target = table.remove(self.waypoints, 1)
-- use smoothing -- TODO: check for blockers before cutting corners?
if #self.waypoints > 0 and not self.current_target["action"] then
local curwp, nextwp = self.current_target.pos, self.waypoints[1].pos
self:go_to_pos(vector.new(curwp.x*0.7+nextwp.x*0.3,curwp.y,curwp.z*0.7+nextwp.z*0.3))
self:go_to_pos(vector.new(curwp.x*0.7+nextwp.x*0.3,curwp.y,curwp.z*0.7+nextwp.z*0.3), hurry)
return
end
self:go_to_pos(self.current_target.pos)
self:go_to_pos(self.current_target.pos, hurry)
--if self.current_target["action"] then self:set_velocity(self.walk_velocity * 0.5) end
return
elseif self.current_target and self.current_target.pos then