Occupations: Add schedule check function, allow enqueuing of schedule check
This commit is contained in:
parent
0f931d273c
commit
5a93800e77
@ -13,6 +13,8 @@
|
||||
|
||||
npc.actions = {}
|
||||
|
||||
npc.actions.default_interval = 1
|
||||
|
||||
-- Describes actions with doors or openable nodes
|
||||
npc.actions.const = {
|
||||
doors = {
|
||||
@ -50,7 +52,9 @@ npc.actions.cmd = {
|
||||
USE_FURNACE = 11,
|
||||
USE_BED = 12,
|
||||
USE_SITTABLE = 13,
|
||||
WALK_TO_POS = 14
|
||||
WALK_TO_POS = 14,
|
||||
DIG = 15,
|
||||
PLACE = 16
|
||||
}
|
||||
|
||||
--npc.actions.one_nps_speed = 0.98
|
||||
@ -60,6 +64,10 @@ npc.actions.one_nps_speed = 1
|
||||
npc.actions.one_half_nps_speed = 1.5
|
||||
npc.actions.two_nps_speed = 2
|
||||
|
||||
npc.actions.take_from_inventory = "take_from_inventory"
|
||||
npc.actions.take_from_inventory_forced = "take_from_inventory_forced"
|
||||
npc.actions.force_place = "force_place"
|
||||
|
||||
-- Executor --
|
||||
--------------
|
||||
-- Function references aren't reliable in Minetest entities. Objects get serialized
|
||||
@ -115,6 +123,12 @@ function npc.actions.execute(self, command, args)
|
||||
-- Call walk to position task
|
||||
--minetest.log("Self: "..dump(self)..", Command: "..dump(command)..", args: "..dump(args))
|
||||
return npc.actions.walk_to_pos(self, args)
|
||||
elseif command == npc.actions.cmd.DIG then
|
||||
-- Call dig node action
|
||||
return npc.actions.dig(self, args)
|
||||
elseif command == npc.actions.cmd.PLACE then
|
||||
-- Call place node action
|
||||
return npc.actions.place(self, args)
|
||||
end
|
||||
end
|
||||
|
||||
@ -146,6 +160,85 @@ function npc.actions.freeze(self, args)
|
||||
return not(freeze_mobs_api)
|
||||
end
|
||||
|
||||
-- This action digs the node at the given position
|
||||
-- If 'add_to_inventory' is true, it will put the digged node in the NPC
|
||||
-- inventory.
|
||||
-- Returns true if dig is successful, otherwise false
|
||||
function npc.actions.dig(self, args)
|
||||
local pos = args.pos
|
||||
local add_to_inventory = args.add_to_inventory
|
||||
local bypass_protection = args.bypass_protection
|
||||
local node = minetest.get_node_or_nil(pos)
|
||||
if node then
|
||||
-- Check if protection not enforced
|
||||
if not force_dig then
|
||||
-- Try to dig node
|
||||
if minetest.dig_node(pos) then
|
||||
-- Add to inventory the node drops
|
||||
if add_to_inventory then
|
||||
-- Get node drop
|
||||
local drop = minetest.registered_nodes[node.name].drop
|
||||
-- Add to NPC inventory
|
||||
npc.npc.add_item_to_inventory(self, drop, 1)
|
||||
end
|
||||
return true
|
||||
end
|
||||
else
|
||||
-- Add to inventory
|
||||
if add_to_inventory then
|
||||
-- Get node drop
|
||||
local drop = minetest.registered_nodes[node.name].drop
|
||||
-- Add to NPC inventory
|
||||
npc.npc.add_item_to_inventory(self, drop, 1)
|
||||
end
|
||||
-- Dig node
|
||||
minetest.set_node(pos, {name="air"})
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
-- This action places a given node at the given position
|
||||
-- There are three ways to source the node:
|
||||
-- 1. take_from_inventory: takes node from inventory. If not in inventory,
|
||||
-- node isn't placed.
|
||||
-- 2. take_from_inventory_forced: takes node from inventory. If not in
|
||||
-- inventory, node will be placed anyways.
|
||||
-- 3. force_place: places node regardless of inventory - will not touch
|
||||
-- the NPCs inventory
|
||||
function npc.actions.place(self, args)
|
||||
local pos = args.pos
|
||||
local node = args.node
|
||||
local source = args.source
|
||||
local bypass_protection = args.bypass_protection
|
||||
local node_at_pos = minetest.get_node_or_nil(pos)
|
||||
-- Check if position is empty or has a node that can be built to
|
||||
if node_at_pos and
|
||||
(node_at_pos.name == "air" or minetest.registered_nodes(node_at_pos.name).buildable_to == true) then
|
||||
-- Check protection
|
||||
if (not bypass_protection and not minetest.is_protected(pos, self.npc_name))
|
||||
or bypass_protection == true then
|
||||
-- Take from inventory if necessary
|
||||
local place_item = false
|
||||
if source == npc.actions.take_from_inventory then
|
||||
if npc.take_item_from_inventory(self, node.name, 1) then
|
||||
place_item = true
|
||||
end
|
||||
elseif source == npc.actions.take_from_inventory_forced then
|
||||
npc.take_item_from_inventory(self, node.name, 1)
|
||||
place_item = true
|
||||
elseif source == npc.actions.force_place then
|
||||
place_item = true
|
||||
end
|
||||
-- Place node
|
||||
if place_item then
|
||||
minetest.set_node(pos, node)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- This action is to rotate to mob to a specifc direction. Currently, the code
|
||||
-- contains also for diagonals, but remaining in the orthogonal domain is preferrable.
|
||||
function npc.actions.rotate(self, args)
|
||||
|
@ -13,70 +13,73 @@
|
||||
npc.places = {}
|
||||
|
||||
npc.places.nodes = {
|
||||
BED_TYPE = {
|
||||
"beds:bed_bottom",
|
||||
"beds:fancy_bed_bottom",
|
||||
"cottages:bed_foot",
|
||||
"cottages:straw_mat",
|
||||
"cottages:sleeping_mat"
|
||||
},
|
||||
SITTABLE_TYPE = {
|
||||
"cottages:bench",
|
||||
-- Currently commented out since some NPCs
|
||||
-- were sitting at stairs that are actually staircases
|
||||
-- TODO: Register other stair types
|
||||
--"stairs:stair_wood"
|
||||
},
|
||||
STORAGE_TYPE = {
|
||||
"default:chest",
|
||||
"default:chest_locked",
|
||||
"cottages:shelf"
|
||||
},
|
||||
FURNACE_TYPE = {
|
||||
"default:furnace",
|
||||
"default:furnace_active"
|
||||
},
|
||||
OPENABLE_TYPE = {
|
||||
-- TODO: Register fences
|
||||
"doors:door_glass_a",
|
||||
"doors:door_glass_b",
|
||||
"doors:door_obsidian_a",
|
||||
"doors:door_obsidian_b",
|
||||
"doors:door_steel_a",
|
||||
"doors:door_steel_b",
|
||||
"doors:door_wood_a",
|
||||
"doors:door_wood_b",
|
||||
"cottages:gate_open",
|
||||
"cottages:gate_closed",
|
||||
"cottages:half_door"
|
||||
}
|
||||
BED_TYPE = {
|
||||
"beds:bed_bottom",
|
||||
"beds:fancy_bed_bottom",
|
||||
"cottages:bed_foot",
|
||||
"cottages:straw_mat",
|
||||
"cottages:sleeping_mat"
|
||||
},
|
||||
SITTABLE_TYPE = {
|
||||
"cottages:bench",
|
||||
-- Currently commented out since some NPCs
|
||||
-- were sitting at stairs that are actually staircases
|
||||
-- TODO: Register other stair types
|
||||
--"stairs:stair_wood"
|
||||
},
|
||||
STORAGE_TYPE = {
|
||||
"default:chest",
|
||||
"default:chest_locked",
|
||||
"cottages:shelf"
|
||||
},
|
||||
FURNACE_TYPE = {
|
||||
"default:furnace",
|
||||
"default:furnace_active"
|
||||
},
|
||||
OPENABLE_TYPE = {
|
||||
-- TODO: Register fences
|
||||
"doors:door_glass_a",
|
||||
"doors:door_glass_b",
|
||||
"doors:door_obsidian_a",
|
||||
"doors:door_obsidian_b",
|
||||
"doors:door_steel_a",
|
||||
"doors:door_steel_b",
|
||||
"doors:door_wood_a",
|
||||
"doors:door_wood_b",
|
||||
"cottages:gate_open",
|
||||
"cottages:gate_closed",
|
||||
"cottages:half_door"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
npc.places.PLACE_TYPE = {
|
||||
BED = {
|
||||
PRIMARY = "bed_primary"
|
||||
},
|
||||
SITTABLE = {
|
||||
PRIMARY = "sit_primary",
|
||||
SHARED = "sit_shared"
|
||||
},
|
||||
FURNACE = {
|
||||
PRIMARY = "furnace_primary",
|
||||
SHARED = "furnace_shared"
|
||||
},
|
||||
STORAGE = {
|
||||
PRIMARY = "storage_primary",
|
||||
SHARED = "storage_shared"
|
||||
},
|
||||
OPENABLE = {
|
||||
HOME_ENTRANCE_DOOR = "home_entrance_door"
|
||||
},
|
||||
OTHER = {
|
||||
HOME_PLOTMARKER = "home_plotmarker",
|
||||
HOME_INSIDE = "home_inside",
|
||||
HOME_OUTSIDE = "home_outside"
|
||||
}
|
||||
BED = {
|
||||
PRIMARY = "bed_primary"
|
||||
},
|
||||
SITTABLE = {
|
||||
PRIMARY = "sit_primary",
|
||||
SHARED = "sit_shared"
|
||||
},
|
||||
FURNACE = {
|
||||
PRIMARY = "furnace_primary",
|
||||
SHARED = "furnace_shared"
|
||||
},
|
||||
STORAGE = {
|
||||
PRIMARY = "storage_primary",
|
||||
SHARED = "storage_shared"
|
||||
},
|
||||
OPENABLE = {
|
||||
HOME_ENTRANCE_DOOR = "home_entrance_door"
|
||||
},
|
||||
SCHEDULE = {
|
||||
TARGET = "schedule_target_pos"
|
||||
},
|
||||
OTHER = {
|
||||
HOME_PLOTMARKER = "home_plotmarker",
|
||||
HOME_INSIDE = "home_inside",
|
||||
HOME_OUTSIDE = "home_outside"
|
||||
}
|
||||
}
|
||||
|
||||
function npc.places.add_shared(self, place_name, place_type, pos, access_node)
|
||||
@ -84,94 +87,110 @@ function npc.places.add_shared(self, place_name, place_type, pos, access_node)
|
||||
end
|
||||
|
||||
function npc.places.add_owned(self, place_name, place_type, pos, access_node)
|
||||
self.places_map[place_name] = {type=place_type, pos=pos, access_node=access_node or pos, status="owned"}
|
||||
self.places_map[place_name] = {type=place_type, pos=pos, access_node=access_node or pos, status="owned"}
|
||||
end
|
||||
|
||||
function npc.places.add_unowned_accessible_place(self, nodes, place_type)
|
||||
for i = 1, #nodes do
|
||||
-- Check if node has owner
|
||||
if nodes[i].owner == "" then
|
||||
-- If node has no owner, check if it is accessible
|
||||
local empty_nodes = npc.places.find_node_orthogonally(
|
||||
nodes[i].node_pos, {"air"}, 0)
|
||||
-- Check if node is accessible
|
||||
if #empty_nodes > 0 then
|
||||
-- Set owner to this NPC
|
||||
nodes[i].owner = self.npc_id
|
||||
-- Assign node to NPC
|
||||
npc.places.add_owned(self, place_type, place_type,
|
||||
nodes[i].node_pos, empty_nodes[1].pos)
|
||||
npc.log("DEBUG", "Added node at "..minetest.pos_to_string(nodes[i].node_pos)
|
||||
.." to NPC "..dump(self.npc_name))
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
function npc.places.add_owned_accessible_place(self, nodes, place_type)
|
||||
for i = 1, #nodes do
|
||||
-- Check if node has owner
|
||||
if nodes[i].owner == "" then
|
||||
-- If node has no owner, check if it is accessible
|
||||
local empty_nodes = npc.places.find_node_orthogonally(
|
||||
nodes[i].node_pos, {"air"}, 0)
|
||||
-- Check if node is accessible
|
||||
if #empty_nodes > 0 then
|
||||
-- Set owner to this NPC
|
||||
nodes[i].owner = self.npc_id
|
||||
-- Assign node to NPC
|
||||
npc.places.add_owned(self, place_type, place_type,
|
||||
nodes[i].node_pos, empty_nodes[1].pos)
|
||||
npc.log("DEBUG", "Added node at "..minetest.pos_to_string(nodes[i].node_pos)
|
||||
.." to NPC "..dump(self.npc_name))
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function npc.places.add_shared_accessible_place(self, nodes, place_type)
|
||||
for i = 1, #nodes do
|
||||
-- Check of not adding same owned sit
|
||||
if nodes[i].owner ~= self.npc_id then
|
||||
-- Check if it is accessible
|
||||
local empty_nodes = npc.places.find_node_orthogonally(
|
||||
nodes[i].node_pos, {"air"}, 0)
|
||||
-- Check if bed is accessible
|
||||
if #empty_nodes > 0 then
|
||||
-- Assign node to NPC
|
||||
npc.places.add_shared(self, place_type..dump(i),
|
||||
place_type, nodes[i].node_pos, empty_nodes[1].pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Override flag allows to overwrite a place in the places_map.
|
||||
-- The only valid use right now is for schedules - don't use this
|
||||
-- anywhere else unless you have a position that changes over time.
|
||||
function npc.places.add_shared_accessible_place(self, nodes, place_type, override)
|
||||
if not override then
|
||||
for i = 1, #nodes do
|
||||
-- Check if not adding same owned place
|
||||
if nodes[i].owner ~= self.npc_id then
|
||||
-- Check if it is accessible
|
||||
local empty_nodes = npc.places.find_node_orthogonally(
|
||||
nodes[i].node_pos, {"air"}, 0)
|
||||
-- Check if node is accessible
|
||||
if #empty_nodes > 0 then
|
||||
-- Assign node to NPC
|
||||
npc.places.add_shared(self, place_type..dump(i),
|
||||
place_type, nodes[i].node_pos, empty_nodes[1].pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif override then
|
||||
-- Note: Nodes is only *one* node in case override = true
|
||||
-- Check if it is accessible
|
||||
local empty_nodes = npc.places.find_node_orthogonally(
|
||||
nodes.node_pos, {"air"}, 0)
|
||||
-- Check if node is accessible
|
||||
if #empty_nodes > 0 then
|
||||
-- Nodes is only one node
|
||||
npc.places.add_shared(self, place_type, place_type,
|
||||
nodes.node_pos, empty_nodes[1].pos)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function npc.places.get_by_type(self, place_type)
|
||||
local result = {}
|
||||
for place_name, place_entry in pairs(self.places_map) do
|
||||
if place_entry.type == place_type then
|
||||
table.insert(result, place_entry)
|
||||
end
|
||||
table.insert(result, place_entry)
|
||||
end
|
||||
end
|
||||
return result
|
||||
return result
|
||||
end
|
||||
|
||||
-- This function searches on a squared are of the given radius
|
||||
-- for nodes of the given type. The type should be npc.places.nodes
|
||||
function npc.places.find_node_nearby(pos, type, radius)
|
||||
-- Determine area points
|
||||
local start_pos = {x=pos.x - radius, y=pos.y - 1, z=pos.z - radius}
|
||||
local end_pos = {x=pos.x + radius, y=pos.y + 1, z=pos.z + radius}
|
||||
-- Get nodes
|
||||
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
|
||||
-- Determine area points
|
||||
local start_pos = {x=pos.x - radius, y=pos.y - 1, z=pos.z - radius}
|
||||
local end_pos = {x=pos.x + radius, y=pos.y + 1, z=pos.z + radius}
|
||||
-- Get nodes
|
||||
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
|
||||
|
||||
return nodes
|
||||
return nodes
|
||||
end
|
||||
|
||||
-- TODO: This function can be improved to support a radius greater than 1.
|
||||
function npc.places.find_node_orthogonally(pos, nodes, y_adjustment)
|
||||
-- Calculate orthogonal points
|
||||
local points = {}
|
||||
table.insert(points, {x=pos.x+1,y=pos.y+y_adjustment,z=pos.z})
|
||||
table.insert(points, {x=pos.x-1,y=pos.y+y_adjustment,z=pos.z})
|
||||
table.insert(points, {x=pos.x,y=pos.y+y_adjustment,z=pos.z+1})
|
||||
table.insert(points, {x=pos.x,y=pos.y+y_adjustment,z=pos.z-1})
|
||||
local result = {}
|
||||
for _,point in pairs(points) do
|
||||
local node = minetest.get_node(point)
|
||||
--minetest.log("Found node: "..dump(node)..", at pos: "..dump(point))
|
||||
for _,node_name in pairs(nodes) do
|
||||
if node.name == node_name then
|
||||
table.insert(result, {name=node.name, pos=point, param2=node.param2})
|
||||
end
|
||||
end
|
||||
end
|
||||
return result
|
||||
-- Calculate orthogonal points
|
||||
local points = {}
|
||||
table.insert(points, {x=pos.x+1,y=pos.y+y_adjustment,z=pos.z})
|
||||
table.insert(points, {x=pos.x-1,y=pos.y+y_adjustment,z=pos.z})
|
||||
table.insert(points, {x=pos.x,y=pos.y+y_adjustment,z=pos.z+1})
|
||||
table.insert(points, {x=pos.x,y=pos.y+y_adjustment,z=pos.z-1})
|
||||
local result = {}
|
||||
for _,point in pairs(points) do
|
||||
local node = minetest.get_node(point)
|
||||
--minetest.log("Found node: "..dump(node)..", at pos: "..dump(point))
|
||||
for _,node_name in pairs(nodes) do
|
||||
if node.name == node_name then
|
||||
table.insert(result, {name=node.name, pos=point, param2=node.param2})
|
||||
end
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
function npc.places.find_node_in_area(start_pos, end_pos, type)
|
||||
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
|
||||
return nodes
|
||||
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
|
||||
return nodes
|
||||
end
|
||||
|
||||
-- Specialized function to find doors that are an entrance to a building.
|
||||
@ -180,90 +199,90 @@ end
|
||||
-- Based on this definition, other entrances aren't going to be used
|
||||
-- by the NPC to get into the building
|
||||
function npc.places.find_entrance_from_openable_nodes(all_openable_nodes, marker_pos)
|
||||
local result = nil
|
||||
local openable_nodes = {}
|
||||
local min = 100
|
||||
local result = nil
|
||||
local openable_nodes = {}
|
||||
local min = 100
|
||||
|
||||
-- Filter out all other openable nodes except MTG doors.
|
||||
-- Why? For supported village types (which are: medieval, nore
|
||||
-- and logcabin) all buildings use, as the main entrance,
|
||||
-- a MTG door. Some medieval building have "half_doors" (like farms)
|
||||
-- which NPCs love to confuse with the right building entrance.
|
||||
for i = 1, #all_openable_nodes do
|
||||
local name = minetest.get_node(all_openable_nodes[i].node_pos).name
|
||||
local doors_st, doors_en = string.find(name, "doors:")
|
||||
if doors_st ~= nil then
|
||||
table.insert(openable_nodes, all_openable_nodes[i])
|
||||
end
|
||||
end
|
||||
-- Filter out all other openable nodes except MTG doors.
|
||||
-- Why? For supported village types (which are: medieval, nore
|
||||
-- and logcabin) all buildings use, as the main entrance,
|
||||
-- a MTG door. Some medieval building have "half_doors" (like farms)
|
||||
-- which NPCs love to confuse with the right building entrance.
|
||||
for i = 1, #all_openable_nodes do
|
||||
local name = minetest.get_node(all_openable_nodes[i].node_pos).name
|
||||
local doors_st, doors_en = string.find(name, "doors:")
|
||||
if doors_st ~= nil then
|
||||
table.insert(openable_nodes, all_openable_nodes[i])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
for i = 1, #openable_nodes do
|
||||
for i = 1, #openable_nodes do
|
||||
|
||||
local open_pos = openable_nodes[i].node_pos
|
||||
local open_pos = openable_nodes[i].node_pos
|
||||
|
||||
-- Get node name - check if this node is a 'door'. The way to check
|
||||
-- is by explicitly checking for 'door' string
|
||||
local name = minetest.get_node(open_pos).name
|
||||
local start_i, end_i = string.find(name, "door")
|
||||
-- Get node name - check if this node is a 'door'. The way to check
|
||||
-- is by explicitly checking for 'door' string
|
||||
local name = minetest.get_node(open_pos).name
|
||||
local start_i, end_i = string.find(name, "door")
|
||||
|
||||
if start_i ~= nil then
|
||||
-- Define start and end pos
|
||||
local start_pos = {x=open_pos.x, y=open_pos.y, z=open_pos.z}
|
||||
local end_pos = {x=marker_pos.x, y=marker_pos.y, z=marker_pos.z}
|
||||
if start_i ~= nil then
|
||||
-- Define start and end pos
|
||||
local start_pos = {x=open_pos.x, y=open_pos.y, z=open_pos.z}
|
||||
local end_pos = {x=marker_pos.x, y=marker_pos.y, z=marker_pos.z}
|
||||
|
||||
-- Check if there's any difference in vertical position
|
||||
-- minetest.log("Openable node pos: "..minetest.pos_to_string(open_pos))
|
||||
-- minetest.log("Plotmarker node pos: "..minetest.pos_to_string(marker_pos))
|
||||
-- NOTE: Commented out while testing MarkBu's pathfinder
|
||||
--if start_pos.y ~= end_pos.y then
|
||||
-- Adjust to make pathfinder find nodes one node above
|
||||
-- end_pos.y = start_pos.y
|
||||
--end
|
||||
-- Check if there's any difference in vertical position
|
||||
-- minetest.log("Openable node pos: "..minetest.pos_to_string(open_pos))
|
||||
-- minetest.log("Plotmarker node pos: "..minetest.pos_to_string(marker_pos))
|
||||
-- NOTE: Commented out while testing MarkBu's pathfinder
|
||||
--if start_pos.y ~= end_pos.y then
|
||||
-- Adjust to make pathfinder find nodes one node above
|
||||
-- end_pos.y = start_pos.y
|
||||
--end
|
||||
|
||||
-- This adjustment allows the map to be created correctly
|
||||
--start_pos.y = start_pos.y + 1
|
||||
--end_pos.y = end_pos.y + 1
|
||||
-- This adjustment allows the map to be created correctly
|
||||
--start_pos.y = start_pos.y + 1
|
||||
--end_pos.y = end_pos.y + 1
|
||||
|
||||
-- Find path from the openable node to the plotmarker
|
||||
--local path = pathfinder.find_path(start_pos, end_pos, 20, {})
|
||||
local entity = {}
|
||||
entity.collisionbox = {-0.20,-1.0,-0.20, 0.20,0.8,0.20}
|
||||
--minetest.log("Start pos: "..minetest.pos_to_string(start_pos))
|
||||
--minetest.log("End pos: "..minetest.pos_to_string(end_pos))
|
||||
local path = npc.pathfinder.find_path(start_pos, end_pos, entity, false)
|
||||
--minetest.log("Found path: "..dump(path))
|
||||
if path ~= nil then
|
||||
--minetest.log("Path distance: "..dump(#path))
|
||||
-- Check if path length is less than the minimum found so far
|
||||
if #path < min then
|
||||
-- Set min to path length and the result to the currently found node
|
||||
min = #path
|
||||
result = openable_nodes[i]
|
||||
else
|
||||
-- Specific check to prefer mtg's doors to cottages' doors.
|
||||
-- The reason? Sometimes a cottages' door could be closer to the
|
||||
-- plotmarker, but not being the building entrance. MTG doors
|
||||
-- are usually the entrance... so yes, hackity hack.
|
||||
-- Get the name of the currently mininum-distance door
|
||||
min_node_name = minetest.get_node(result.node_pos).name
|
||||
-- Check if this is a door from MTG's doors.
|
||||
local doors_st, doors_en = string.find(name, "doors:")
|
||||
-- Check if min-distance door is a cottages door
|
||||
-- while we have a MTG door
|
||||
if min_node_name == "cottages:half_door" and doors_st ~= nil then
|
||||
--minetest.log("Assigned new door...")
|
||||
min = #path
|
||||
result = openable_nodes[i]
|
||||
end
|
||||
end
|
||||
else
|
||||
npc.log("ERROR", "Path not found to marker from "..minetest.pos_to_string(start_pos))
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Return result
|
||||
return result
|
||||
-- Find path from the openable node to the plotmarker
|
||||
--local path = pathfinder.find_path(start_pos, end_pos, 20, {})
|
||||
local entity = {}
|
||||
entity.collisionbox = {-0.20,-1.0,-0.20, 0.20,0.8,0.20}
|
||||
--minetest.log("Start pos: "..minetest.pos_to_string(start_pos))
|
||||
--minetest.log("End pos: "..minetest.pos_to_string(end_pos))
|
||||
local path = npc.pathfinder.find_path(start_pos, end_pos, entity, false)
|
||||
--minetest.log("Found path: "..dump(path))
|
||||
if path ~= nil then
|
||||
--minetest.log("Path distance: "..dump(#path))
|
||||
-- Check if path length is less than the minimum found so far
|
||||
if #path < min then
|
||||
-- Set min to path length and the result to the currently found node
|
||||
min = #path
|
||||
result = openable_nodes[i]
|
||||
else
|
||||
-- Specific check to prefer mtg's doors to cottages' doors.
|
||||
-- The reason? Sometimes a cottages' door could be closer to the
|
||||
-- plotmarker, but not being the building entrance. MTG doors
|
||||
-- are usually the entrance... so yes, hackity hack.
|
||||
-- Get the name of the currently mininum-distance door
|
||||
min_node_name = minetest.get_node(result.node_pos).name
|
||||
-- Check if this is a door from MTG's doors.
|
||||
local doors_st, doors_en = string.find(name, "doors:")
|
||||
-- Check if min-distance door is a cottages door
|
||||
-- while we have a MTG door
|
||||
if min_node_name == "cottages:half_door" and doors_st ~= nil then
|
||||
--minetest.log("Assigned new door...")
|
||||
min = #path
|
||||
result = openable_nodes[i]
|
||||
end
|
||||
end
|
||||
else
|
||||
npc.log("ERROR", "Path not found to marker from "..minetest.pos_to_string(start_pos))
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Return result
|
||||
return result
|
||||
end
|
||||
|
||||
-- Specialized function to find all sittable nodes supported by the
|
||||
@ -271,27 +290,27 @@ end
|
||||
-- stairs nodes placed aren't meant to simulate benches, this function
|
||||
-- is necessary in order to find stairs that are meant to be benches.
|
||||
function npc.places.find_sittable_nodes_nearby(pos, radius)
|
||||
local result = {}
|
||||
-- Try to find sittable nodes
|
||||
local nodes = npc.places.find_node_nearby(pos, npc.places.nodes.SITTABLE, radius)
|
||||
-- Highly unorthodox check for emptinnes
|
||||
if nodes[1] ~= nil then
|
||||
for i = 1, #nodes do
|
||||
-- Get node name, try to avoid using the staircase check if not a stair node
|
||||
local node = minetest.get_node(nodes[i])
|
||||
local i1, i2 = string.find(node.name, "stairs:")
|
||||
if i1 ~= nil then
|
||||
if npc.places.is_in_staircase(nodes[i]) < 1 then
|
||||
table.insert(result, nodes[i])
|
||||
end
|
||||
else
|
||||
-- Add node as it is sittable
|
||||
table.insert(result, nodes[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Return sittable nodes
|
||||
return result
|
||||
local result = {}
|
||||
-- Try to find sittable nodes
|
||||
local nodes = npc.places.find_node_nearby(pos, npc.places.nodes.SITTABLE, radius)
|
||||
-- Highly unorthodox check for emptinnes
|
||||
if nodes[1] ~= nil then
|
||||
for i = 1, #nodes do
|
||||
-- Get node name, try to avoid using the staircase check if not a stair node
|
||||
local node = minetest.get_node(nodes[i])
|
||||
local i1, i2 = string.find(node.name, "stairs:")
|
||||
if i1 ~= nil then
|
||||
if npc.places.is_in_staircase(nodes[i]) < 1 then
|
||||
table.insert(result, nodes[i])
|
||||
end
|
||||
else
|
||||
-- Add node as it is sittable
|
||||
table.insert(result, nodes[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
-- Return sittable nodes
|
||||
return result
|
||||
end
|
||||
|
||||
-- Specialized function to find sittable stairs: stairs that don't
|
||||
@ -300,104 +319,104 @@ end
|
||||
-- Receives a position of a stair node.
|
||||
|
||||
npc.places.staircase = {
|
||||
none = 0,
|
||||
bottom = 1,
|
||||
middle = 2,
|
||||
top = 3
|
||||
none = 0,
|
||||
bottom = 1,
|
||||
middle = 2,
|
||||
top = 3
|
||||
}
|
||||
|
||||
function npc.places.is_in_staircase(pos)
|
||||
local node = minetest.get_node(pos)
|
||||
-- Verify node is actually from default stairs mod
|
||||
local p1, p2 = string.find(node.name, "stairs:")
|
||||
if p1 ~= nil then
|
||||
-- Calculate the logical position to the lower and upper stairs node location
|
||||
local up_x_adj, up_z_adj = 0, 0
|
||||
local lo_x_adj, lo_z_adj = 0, 0
|
||||
if node.param2 == 1 then
|
||||
up_z_adj = -1
|
||||
lo_z_adj = 1
|
||||
elseif node.param2 == 2 then
|
||||
up_z_adj = 1
|
||||
lo_z_adj = -1
|
||||
elseif node.param2 == 3 then
|
||||
up_x_adj = -1
|
||||
lo_x_adj = 1
|
||||
elseif node.param2 == 4 then
|
||||
up_x_adj = 1
|
||||
lo_x_adj = -1
|
||||
else
|
||||
-- This is not a staircase
|
||||
return false
|
||||
end
|
||||
local node = minetest.get_node(pos)
|
||||
-- Verify node is actually from default stairs mod
|
||||
local p1, p2 = string.find(node.name, "stairs:")
|
||||
if p1 ~= nil then
|
||||
-- Calculate the logical position to the lower and upper stairs node location
|
||||
local up_x_adj, up_z_adj = 0, 0
|
||||
local lo_x_adj, lo_z_adj = 0, 0
|
||||
if node.param2 == 1 then
|
||||
up_z_adj = -1
|
||||
lo_z_adj = 1
|
||||
elseif node.param2 == 2 then
|
||||
up_z_adj = 1
|
||||
lo_z_adj = -1
|
||||
elseif node.param2 == 3 then
|
||||
up_x_adj = -1
|
||||
lo_x_adj = 1
|
||||
elseif node.param2 == 4 then
|
||||
up_x_adj = 1
|
||||
lo_x_adj = -1
|
||||
else
|
||||
-- This is not a staircase
|
||||
return false
|
||||
end
|
||||
|
||||
-- Calculate upper and lower position
|
||||
local upper_pos = {x=pos.x + up_x_adj, y=pos.y + 1, z=pos.z + up_z_adj}
|
||||
local lower_pos = {x=pos.x + lo_x_adj, y=pos.y - 1, z=pos.z + lo_z_adj}
|
||||
-- Get next node
|
||||
local upper_node = minetest.get_node(upper_pos)
|
||||
local lower_node = minetest.get_node(lower_pos)
|
||||
--minetest.log("Next node: "..dump(upper_pos))
|
||||
-- Check if next node is also a stairs node
|
||||
local up_p1, up_p2 = string.find(upper_node.name, "stairs:")
|
||||
local lo_p1, lo_p2 = string.find(lower_node.name, "stairs:")
|
||||
-- Calculate upper and lower position
|
||||
local upper_pos = {x=pos.x + up_x_adj, y=pos.y + 1, z=pos.z + up_z_adj}
|
||||
local lower_pos = {x=pos.x + lo_x_adj, y=pos.y - 1, z=pos.z + lo_z_adj}
|
||||
-- Get next node
|
||||
local upper_node = minetest.get_node(upper_pos)
|
||||
local lower_node = minetest.get_node(lower_pos)
|
||||
--minetest.log("Next node: "..dump(upper_pos))
|
||||
-- Check if next node is also a stairs node
|
||||
local up_p1, up_p2 = string.find(upper_node.name, "stairs:")
|
||||
local lo_p1, lo_p2 = string.find(lower_node.name, "stairs:")
|
||||
|
||||
if up_p1 ~= nil then
|
||||
-- By default, think this is bottom of staircase.
|
||||
local result = npc.places.staircase.bottom
|
||||
-- Try downwards now
|
||||
if lo_p1 ~= nil then
|
||||
result = npc.places.staircase.middle
|
||||
end
|
||||
return result
|
||||
else
|
||||
-- Check if there is a staircase downwards
|
||||
if lo_p1 ~= nil then
|
||||
return npc.places.staircase.top
|
||||
else
|
||||
return npc.places.staircase.none
|
||||
end
|
||||
end
|
||||
end
|
||||
-- This is not a stairs node
|
||||
return nil
|
||||
if up_p1 ~= nil then
|
||||
-- By default, think this is bottom of staircase.
|
||||
local result = npc.places.staircase.bottom
|
||||
-- Try downwards now
|
||||
if lo_p1 ~= nil then
|
||||
result = npc.places.staircase.middle
|
||||
end
|
||||
return result
|
||||
else
|
||||
-- Check if there is a staircase downwards
|
||||
if lo_p1 ~= nil then
|
||||
return npc.places.staircase.top
|
||||
else
|
||||
return npc.places.staircase.none
|
||||
end
|
||||
end
|
||||
end
|
||||
-- This is not a stairs node
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Specialized function to find the node position right behind
|
||||
-- a door. Used to make NPCs enter buildings.
|
||||
function npc.places.find_node_behind_door(door_pos)
|
||||
local door = minetest.get_node(door_pos)
|
||||
if door.param2 == 0 then
|
||||
-- Looking south
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
|
||||
elseif door.param2 == 1 then
|
||||
-- Looking east
|
||||
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
|
||||
elseif door.param2 == 2 then
|
||||
-- Looking north
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
|
||||
-- Looking west
|
||||
elseif door.param2 == 3 then
|
||||
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
|
||||
end
|
||||
local door = minetest.get_node(door_pos)
|
||||
if door.param2 == 0 then
|
||||
-- Looking south
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
|
||||
elseif door.param2 == 1 then
|
||||
-- Looking east
|
||||
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
|
||||
elseif door.param2 == 2 then
|
||||
-- Looking north
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
|
||||
-- Looking west
|
||||
elseif door.param2 == 3 then
|
||||
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
|
||||
end
|
||||
end
|
||||
|
||||
-- Specialized function to find the node position right in
|
||||
-- front of a door. Used to make NPCs exit buildings.
|
||||
function npc.places.find_node_in_front_of_door(door_pos)
|
||||
local door = minetest.get_node(door_pos)
|
||||
--minetest.log("Param2 of door: "..dump(door.param2))
|
||||
if door.param2 == 0 then
|
||||
-- Looking south
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
|
||||
elseif door.param2 == 1 then
|
||||
-- Looking east
|
||||
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
|
||||
elseif door.param2 == 2 then
|
||||
-- Looking north
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
|
||||
-- Looking west
|
||||
elseif door.param2 == 3 then
|
||||
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
|
||||
end
|
||||
local door = minetest.get_node(door_pos)
|
||||
--minetest.log("Param2 of door: "..dump(door.param2))
|
||||
if door.param2 == 0 then
|
||||
-- Looking south
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
|
||||
elseif door.param2 == 1 then
|
||||
-- Looking east
|
||||
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
|
||||
elseif door.param2 == 2 then
|
||||
-- Looking north
|
||||
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
|
||||
elseif door.param2 == 3 then
|
||||
-- Looking west
|
||||
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
|
||||
end
|
||||
end
|
2
init.lua
2
init.lua
@ -34,10 +34,12 @@ dofile(path .. "/actions/actions.lua")
|
||||
dofile(path .. "/actions/places.lua")
|
||||
dofile(path .. "/actions/pathfinder.lua")
|
||||
dofile(path .. "/actions/node_registry.lua")
|
||||
dofile(path .. "/occupations/occupations.lua")
|
||||
-- Load random data definitions
|
||||
dofile(path .. "/random_data.lua")
|
||||
dofile(path .. "/random_data/dialogues_data.lua")
|
||||
dofile(path .. "/random_data/gift_items_data.lua")
|
||||
dofile(path .. "/random_data/names_data.lua")
|
||||
dofile(path .. "/random_data/occupations_data.lua")
|
||||
|
||||
print (S("[Mod] Advanced NPC loaded"))
|
||||
|
22
spawner.lua
22
spawner.lua
@ -264,7 +264,7 @@ function spawner.assign_places(self, pos)
|
||||
-- Assign beds
|
||||
if #node_data.bed_type > 0 then
|
||||
-- Assign a specific sittable node to a NPC.
|
||||
npc.places.add_unowned_accessible_place(self, node_data.bed_type,
|
||||
npc.places.add_owned_accessible_place(self, node_data.bed_type,
|
||||
npc.places.PLACE_TYPE.BED.PRIMARY)
|
||||
-- Store changes to node_data
|
||||
meta:set_string("node_data", minetest.serialize(node_data))
|
||||
@ -275,7 +275,7 @@ function spawner.assign_places(self, pos)
|
||||
-- Check if there are same or more amount of sits as beds
|
||||
if #node_data.sittable_type >= #node_data.bed_type then
|
||||
-- Assign a specific sittable node to a NPC.
|
||||
npc.places.add_unowned_accessible_place(self, node_data.sittable_type,
|
||||
npc.places.add_owned_accessible_place(self, node_data.sittable_type,
|
||||
npc.places.PLACE_TYPE.SITTABLE.PRIMARY)
|
||||
-- Store changes to node_data
|
||||
meta:set_string("node_data", minetest.serialize(node_data))
|
||||
@ -291,7 +291,7 @@ function spawner.assign_places(self, pos)
|
||||
-- Check if there are same or more amount of furnace as beds
|
||||
if #node_data.furnace_type >= #node_data.bed_type then
|
||||
-- Assign a specific furnace node to a NPC.
|
||||
npc.places.add_unowned_accessible_place(self, node_data.furnace_type,
|
||||
npc.places.add_owned_accessible_place(self, node_data.furnace_type,
|
||||
npc.places.PLACE_TYPE.FURNACE.PRIMARY)
|
||||
-- Store changes to node_data
|
||||
meta:set_string("node_data", minetest.serialize(node_data))
|
||||
@ -307,7 +307,7 @@ function spawner.assign_places(self, pos)
|
||||
-- Check if there are same or more amount of storage as beds
|
||||
if #node_data.storage_type >= #node_data.bed_type then
|
||||
-- Assign a specific storage node to a NPC.
|
||||
npc.places.add_unowned_accessible_place(self, node_data.storage_type,
|
||||
npc.places.add_owned_accessible_place(self, node_data.storage_type,
|
||||
npc.places.PLACE_TYPE.STORAGE.PRIMARY)
|
||||
-- Store changes to node_data
|
||||
meta:set_string("node_data", minetest.serialize(node_data))
|
||||
@ -372,17 +372,19 @@ function npc.spawner.spawn_npc(pos)
|
||||
local ent = minetest.add_entity({x=pos.x, y=pos.y+1, z=pos.z}, "advanced_npc:npc")
|
||||
if ent and ent:get_luaentity() then
|
||||
ent:get_luaentity().initialized = false
|
||||
-- Determine NPC occupation
|
||||
local occupation_name = "default_basic"
|
||||
-- Initialize NPC
|
||||
-- Call with stats if there are NPCs
|
||||
if #npc_table > 0 then
|
||||
npc.initialize(ent, pos, false, npc_stats)
|
||||
if npc_table and #npc_table > 0 then
|
||||
npc.initialize(ent, pos, false, npc_stats, occupation_name)
|
||||
else
|
||||
npc.initialize(ent, pos)
|
||||
npc.initialize(ent, pos, nil, nil, occupation_name)
|
||||
end
|
||||
-- Assign nodes
|
||||
spawner.assign_places(ent:get_luaentity(), pos)
|
||||
-- Assign schedules
|
||||
spawner.assign_schedules(ent:get_luaentity(), pos)
|
||||
--spawner.assign_schedules(ent:get_luaentity(), pos)
|
||||
-- Increase NPC spawned count
|
||||
spawned_npc_count = spawned_npc_count + 1
|
||||
-- Store count into node
|
||||
@ -750,8 +752,8 @@ minetest.register_chatcommand("restore_plotmarkers", {
|
||||
end
|
||||
-- Search for nodes
|
||||
local radius = tonumber(param)
|
||||
local start_pos = {x=pos.x - radius, y=pos.y - radius, z=pos.z - radius}
|
||||
local end_pos = {x=pos.x + radius, y=pos.y + radius, z=pos.z + radius}
|
||||
local start_pos = {x=pos.x - radius, y=pos.y - 5, z=pos.z - radius}
|
||||
local end_pos = {x=pos.x + radius, y=pos.y + 5, z=pos.z + radius}
|
||||
local nodes = minetest.find_nodes_in_area_under_air(start_pos, end_pos,
|
||||
{"mg_villages:plotmarker"})
|
||||
-- Check if we have nodes to replace
|
||||
|
41
utils.lua
41
utils.lua
@ -1,4 +1,4 @@
|
||||
-- Basic utilities to work with array operations in Lua
|
||||
-- Basic utilities to work with table operations in Lua, and specific querying
|
||||
-- By Zorman2000
|
||||
|
||||
npc.utils = {}
|
||||
@ -34,4 +34,43 @@ function npc.utils.get_map_keys(map)
|
||||
table.insert(result, key)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
-- This function searches for a node given the conditions specified in the
|
||||
-- query object, starting from the given start_pos and up to a certain, specified
|
||||
-- range.
|
||||
-- Query object:
|
||||
-- search_type: determines the direction to search nodes.
|
||||
-- Valid values are: orthogonal, cross, cube
|
||||
-- - orthogonal search means only nodes which are parallel to the search node's faces
|
||||
-- will be considered. This limits the search to only 6 nodes.
|
||||
-- - cross search will look at the same nodes as orthogonal, plus will also
|
||||
-- check nodes diagonal to the node four horizontal nodes. This search looks at 14 nodes
|
||||
-- - cube search means to look every node surrounding the node, including all diagonals.
|
||||
-- This search looks at 26 nodes.
|
||||
-- search_nodes: array of nodes to search for
|
||||
-- surrounding_nodes: object specifying which neighbor nodes are to be expected and
|
||||
-- at which locations. Valid keys are:
|
||||
-- - North (+Z dir)
|
||||
-- - East (+x dir)
|
||||
-- - South (-Z dir)
|
||||
-- - West (-X dir)
|
||||
-- - Top (+Y dir)
|
||||
-- - Bottom (-Y dir)
|
||||
-- Example: ["bottom"] = {nodes={"default:dirt"}, criteria="all"}
|
||||
-- Each object will contain nodes, and criteria for acceptance.
|
||||
-- Criteria values are:
|
||||
-- - any: true as long as one of the nodes on this side is one of the specified
|
||||
-- in "nodes"
|
||||
-- - all: true when the set of neighboring nodes on this side contain one or many of
|
||||
-- the specified "nodes"
|
||||
-- - all-exact: true when the set of neighboring nodes on this side contain all nodes
|
||||
-- specified in "nodes"
|
||||
-- - shape: true when the set of neighboring nodes on this side contains nodes in
|
||||
-- the exact given shape. If so, nodes will not be an array, but a 2d array
|
||||
-- of three rows and three columns, with the specific shape. Notice that
|
||||
-- the nodes on the side can vary depending on the search type (orthogonal,
|
||||
-- cross, cube)
|
||||
function npc.utils.search_node(query, start_pos, range)
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user