Add files via upload
parent
03f5ac8467
commit
b66625fc95
34
api.lua
34
api.lua
|
@ -110,6 +110,34 @@ function working_villages.villager.get_front_node(self)
|
|||
return minetest.get_node(front)
|
||||
end
|
||||
|
||||
-- working_villages.villager.get_back returns a position behind the villager.
|
||||
function working_villages.villager.get_back(self)
|
||||
local direction = self:get_look_direction()
|
||||
if math.abs(direction.x) >= 0.5 then
|
||||
if direction.x > 0 then direction.x = -1
|
||||
else direction.x = 1 end
|
||||
else
|
||||
direction.x = 0
|
||||
end
|
||||
|
||||
if math.abs(direction.z) >= 0.5 then
|
||||
if direction.z > 0 then direction.z = -1
|
||||
else direction.z = 1 end
|
||||
else
|
||||
direction.z = 0
|
||||
end
|
||||
|
||||
direction.y = direction.y - 1
|
||||
|
||||
return vector.add(vector.round(self.object:getpos()), direction)
|
||||
end
|
||||
|
||||
-- working_villages.villager.get_back_node returns a node that exists behind the villager.
|
||||
function working_villages.villager.get_back_node(self)
|
||||
local back = self:get_back()
|
||||
return minetest.get_node(back)
|
||||
end
|
||||
|
||||
-- working_villages.villager.get_look_direction returns a normalized vector that is
|
||||
-- the villagers's looking direction.
|
||||
function working_villages.villager.get_look_direction(self)
|
||||
|
@ -662,11 +690,13 @@ function working_villages.register_villager(product_name, def)
|
|||
|
||||
-- extra methods.
|
||||
get_inventory = working_villages.villager.get_inventory,
|
||||
get_job = working_villages.villager.get_job,
|
||||
get_job_name = working_villages.villager.get_job_name,
|
||||
get_job = working_villages.villager.get_job,
|
||||
get_job_name = working_villages.villager.get_job_name,
|
||||
get_nearest_player = working_villages.villager.get_nearest_player,
|
||||
get_front = working_villages.villager.get_front,
|
||||
get_front_node = working_villages.villager.get_front_node,
|
||||
get_back = working_villages.villager.get_back,
|
||||
get_back_node = working_villages.villager.get_back_node,
|
||||
get_look_direction = working_villages.villager.get_look_direction,
|
||||
set_animation = working_villages.villager.set_animation,
|
||||
set_yaw_by_direction = working_villages.villager.set_yaw_by_direction,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
default
|
||||
doors
|
||||
doors?
|
||||
beds?
|
||||
pathfinder?
|
|
@ -3,9 +3,9 @@ working_villages.home = {}
|
|||
minetest.register_node("working_villages:home_marker", {
|
||||
description = "home marker for working_villages",
|
||||
drawtype = "nodebox",
|
||||
tiles = {"default_sign.png"},
|
||||
inventory_image = "default_sign_wall.png",
|
||||
wield_image = "default_sign_wall.png",
|
||||
tiles = {"default_sign_wall_wood.png"},
|
||||
inventory_image = "default_sign_wood.png",
|
||||
wield_image = "default_sign_wood.png",
|
||||
paramtype = "light",
|
||||
paramtype2 = "wallmounted",
|
||||
sunlight_propagates = true,
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
if pathfinder==nil then
|
||||
pathfinder = {}
|
||||
end
|
||||
working_villages.pathfinder = {}
|
||||
|
||||
--[[
|
||||
minetest.get_content_id(name)
|
||||
|
@ -47,14 +45,18 @@ local function get_distance_to_neighbor(start_pos, end_pos)
|
|||
end
|
||||
end
|
||||
local function walkable(node)
|
||||
return minetest.registered_nodes[node.name].walkable
|
||||
if string.find(node.name,"doors:door") then
|
||||
return false
|
||||
else
|
||||
return minetest.registered_nodes[node.name].walkable
|
||||
end
|
||||
end
|
||||
|
||||
local function check_clearance(cpos, x, z, height)
|
||||
for i = 1, height do
|
||||
local n_name = minetest.get_node({x = cpos.x + x, y = cpos.y + i, z = cpos.z + z}).name
|
||||
local c_name = minetest.get_node({x = cpos.x, y = cpos.y + i, z = cpos.z}).name
|
||||
--~ print(i, n_name, c_name)
|
||||
--print(i, n_name, c_name)
|
||||
if walkable(n_name) or walkable(c_name) then
|
||||
return false
|
||||
end
|
||||
|
@ -73,7 +75,7 @@ local function get_neighbor_ground_level(pos, jump_height, fall_height)
|
|||
end
|
||||
pos.y = pos.y + 1
|
||||
node = minetest.get_node(pos)
|
||||
until not walkable(node)
|
||||
until not(walkable(node))
|
||||
return pos
|
||||
else
|
||||
repeat
|
||||
|
@ -88,8 +90,8 @@ local function get_neighbor_ground_level(pos, jump_height, fall_height)
|
|||
end
|
||||
end
|
||||
|
||||
if pathfinder.find_path==nil then
|
||||
function pathfinder.find_path(pos, endpos, entity)
|
||||
function working_villages.pathfinder.find_path(pos, endpos, entity)
|
||||
--print("searching for a path to:" .. endpos.x .. ",".. endpos.y .. ",".. endpos.z)
|
||||
local start_index = minetest.hash_node_position(pos)
|
||||
local target_index = minetest.hash_node_position(endpos)
|
||||
local count = 1
|
||||
|
@ -129,24 +131,24 @@ function pathfinder.find_path(pos, endpos, entity)
|
|||
count = count - 1
|
||||
|
||||
if current_index == target_index then
|
||||
-- print("Success")
|
||||
--print("Found path")
|
||||
local path = {}
|
||||
local reverse_path = {}
|
||||
repeat
|
||||
if not closedSet[current_index] then
|
||||
return
|
||||
if not(closedSet[current_index]) then
|
||||
return {endpos} --was empty return
|
||||
end
|
||||
table.insert(path, closedSet[current_index].pos)
|
||||
current_index = closedSet[current_index].parent
|
||||
if #path > 100 then
|
||||
-- print("path to long")
|
||||
--print("path to long")
|
||||
return
|
||||
end
|
||||
until start_index == current_index
|
||||
repeat
|
||||
table.insert(reverse_path, table.remove(path))
|
||||
until #path == 0
|
||||
-- print("path lenght: "..#reverse_path)
|
||||
--print("path length: "..#reverse_path)
|
||||
return reverse_path
|
||||
end
|
||||
|
||||
|
@ -165,7 +167,7 @@ function pathfinder.find_path(pos, endpos, entity)
|
|||
-- minetest.set_node(neighbor_ground_level, {name = "default:dry_shrub"})
|
||||
local node_above_head = minetest.get_node(
|
||||
{x = current_pos.x, y = current_pos.y + entity_height, z = current_pos.z})
|
||||
if neighbor_ground_level.y - current_pos.y > 0 and not walkable(node_above_head) then
|
||||
if neighbor_ground_level.y - current_pos.y > 0 and not(walkable(node_above_head)) then
|
||||
local height = -1
|
||||
repeat
|
||||
height = height + 1
|
||||
|
@ -220,7 +222,7 @@ function pathfinder.find_path(pos, endpos, entity)
|
|||
-- don't cut corners
|
||||
local cut_corner = false
|
||||
if id == 1 then
|
||||
if not neighbors[id + 1].clear or not neighbors[id + 3].clear
|
||||
if not(neighbors[id + 1].clear) or not(neighbors[id + 3].clear)
|
||||
or neighbors[id + 1].walkable or neighbors[id + 3].walkable then
|
||||
cut_corner = true
|
||||
end
|
||||
|
@ -263,14 +265,15 @@ function pathfinder.find_path(pos, endpos, entity)
|
|||
end
|
||||
end
|
||||
if count > 100 then
|
||||
-- print("fail")
|
||||
print("failed finding a path to:" .. endpos.x .. ",".. endpos.y .. ",".. endpos.z)
|
||||
return
|
||||
end
|
||||
until count < 1
|
||||
-- print("count < 1")
|
||||
return {pos}
|
||||
end
|
||||
--print("count < 1")
|
||||
return {endpos}
|
||||
end
|
||||
|
||||
working_villages.func.walkable = walkable
|
||||
working_villages.func.get_ground_level = get_neighbor_ground_level
|
||||
working_villages.pathfinder.walkable = walkable
|
||||
function working_villages.pathfinder.get_ground_level(pos)
|
||||
return get_neighbor_ground_level(pos, 30927, 30927)
|
||||
end
|
Loading…
Reference in New Issue