Occupations: Add schedule check function, allow enqueuing of schedule check

This commit is contained in:
Hector Franqui 2017-08-11 12:03:45 -04:00
parent 0f931d273c
commit 5a93800e77
6 changed files with 1116 additions and 772 deletions

View File

@ -13,6 +13,8 @@
npc.actions = {} npc.actions = {}
npc.actions.default_interval = 1
-- Describes actions with doors or openable nodes -- Describes actions with doors or openable nodes
npc.actions.const = { npc.actions.const = {
doors = { doors = {
@ -50,7 +52,9 @@ npc.actions.cmd = {
USE_FURNACE = 11, USE_FURNACE = 11,
USE_BED = 12, USE_BED = 12,
USE_SITTABLE = 13, USE_SITTABLE = 13,
WALK_TO_POS = 14 WALK_TO_POS = 14,
DIG = 15,
PLACE = 16
} }
--npc.actions.one_nps_speed = 0.98 --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.one_half_nps_speed = 1.5
npc.actions.two_nps_speed = 2 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 -- -- Executor --
-------------- --------------
-- Function references aren't reliable in Minetest entities. Objects get serialized -- 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 -- Call walk to position task
--minetest.log("Self: "..dump(self)..", Command: "..dump(command)..", args: "..dump(args)) --minetest.log("Self: "..dump(self)..", Command: "..dump(command)..", args: "..dump(args))
return npc.actions.walk_to_pos(self, 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
end end
@ -146,6 +160,85 @@ function npc.actions.freeze(self, args)
return not(freeze_mobs_api) return not(freeze_mobs_api)
end 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 -- 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. -- contains also for diagonals, but remaining in the orthogonal domain is preferrable.
function npc.actions.rotate(self, args) function npc.actions.rotate(self, args)

View File

@ -13,70 +13,73 @@
npc.places = {} npc.places = {}
npc.places.nodes = { npc.places.nodes = {
BED_TYPE = { BED_TYPE = {
"beds:bed_bottom", "beds:bed_bottom",
"beds:fancy_bed_bottom", "beds:fancy_bed_bottom",
"cottages:bed_foot", "cottages:bed_foot",
"cottages:straw_mat", "cottages:straw_mat",
"cottages:sleeping_mat" "cottages:sleeping_mat"
}, },
SITTABLE_TYPE = { SITTABLE_TYPE = {
"cottages:bench", "cottages:bench",
-- Currently commented out since some NPCs -- Currently commented out since some NPCs
-- were sitting at stairs that are actually staircases -- were sitting at stairs that are actually staircases
-- TODO: Register other stair types -- TODO: Register other stair types
--"stairs:stair_wood" --"stairs:stair_wood"
}, },
STORAGE_TYPE = { STORAGE_TYPE = {
"default:chest", "default:chest",
"default:chest_locked", "default:chest_locked",
"cottages:shelf" "cottages:shelf"
}, },
FURNACE_TYPE = { FURNACE_TYPE = {
"default:furnace", "default:furnace",
"default:furnace_active" "default:furnace_active"
}, },
OPENABLE_TYPE = { OPENABLE_TYPE = {
-- TODO: Register fences -- TODO: Register fences
"doors:door_glass_a", "doors:door_glass_a",
"doors:door_glass_b", "doors:door_glass_b",
"doors:door_obsidian_a", "doors:door_obsidian_a",
"doors:door_obsidian_b", "doors:door_obsidian_b",
"doors:door_steel_a", "doors:door_steel_a",
"doors:door_steel_b", "doors:door_steel_b",
"doors:door_wood_a", "doors:door_wood_a",
"doors:door_wood_b", "doors:door_wood_b",
"cottages:gate_open", "cottages:gate_open",
"cottages:gate_closed", "cottages:gate_closed",
"cottages:half_door" "cottages:half_door"
} }
} }
npc.places.PLACE_TYPE = { npc.places.PLACE_TYPE = {
BED = { BED = {
PRIMARY = "bed_primary" PRIMARY = "bed_primary"
}, },
SITTABLE = { SITTABLE = {
PRIMARY = "sit_primary", PRIMARY = "sit_primary",
SHARED = "sit_shared" SHARED = "sit_shared"
}, },
FURNACE = { FURNACE = {
PRIMARY = "furnace_primary", PRIMARY = "furnace_primary",
SHARED = "furnace_shared" SHARED = "furnace_shared"
}, },
STORAGE = { STORAGE = {
PRIMARY = "storage_primary", PRIMARY = "storage_primary",
SHARED = "storage_shared" SHARED = "storage_shared"
}, },
OPENABLE = { OPENABLE = {
HOME_ENTRANCE_DOOR = "home_entrance_door" HOME_ENTRANCE_DOOR = "home_entrance_door"
}, },
OTHER = { SCHEDULE = {
HOME_PLOTMARKER = "home_plotmarker", TARGET = "schedule_target_pos"
HOME_INSIDE = "home_inside", },
HOME_OUTSIDE = "home_outside" 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) 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 end
function npc.places.add_owned(self, place_name, place_type, pos, access_node) 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 end
function npc.places.add_unowned_accessible_place(self, nodes, place_type) function npc.places.add_owned_accessible_place(self, nodes, place_type)
for i = 1, #nodes do for i = 1, #nodes do
-- Check if node has owner -- Check if node has owner
if nodes[i].owner == "" then if nodes[i].owner == "" then
-- If node has no owner, check if it is accessible -- If node has no owner, check if it is accessible
local empty_nodes = npc.places.find_node_orthogonally( local empty_nodes = npc.places.find_node_orthogonally(
nodes[i].node_pos, {"air"}, 0) nodes[i].node_pos, {"air"}, 0)
-- Check if node is accessible -- Check if node is accessible
if #empty_nodes > 0 then if #empty_nodes > 0 then
-- Set owner to this NPC -- Set owner to this NPC
nodes[i].owner = self.npc_id nodes[i].owner = self.npc_id
-- Assign node to NPC -- Assign node to NPC
npc.places.add_owned(self, place_type, place_type, npc.places.add_owned(self, place_type, place_type,
nodes[i].node_pos, empty_nodes[1].pos) nodes[i].node_pos, empty_nodes[1].pos)
npc.log("DEBUG", "Added node at "..minetest.pos_to_string(nodes[i].node_pos) npc.log("DEBUG", "Added node at "..minetest.pos_to_string(nodes[i].node_pos)
.." to NPC "..dump(self.npc_name)) .." to NPC "..dump(self.npc_name))
break break
end end
end end
end end
end end
function npc.places.add_shared_accessible_place(self, nodes, place_type) -- Override flag allows to overwrite a place in the places_map.
for i = 1, #nodes do -- The only valid use right now is for schedules - don't use this
-- Check of not adding same owned sit -- anywhere else unless you have a position that changes over time.
if nodes[i].owner ~= self.npc_id then function npc.places.add_shared_accessible_place(self, nodes, place_type, override)
-- Check if it is accessible if not override then
local empty_nodes = npc.places.find_node_orthogonally( for i = 1, #nodes do
nodes[i].node_pos, {"air"}, 0) -- Check if not adding same owned place
-- Check if bed is accessible if nodes[i].owner ~= self.npc_id then
if #empty_nodes > 0 then -- Check if it is accessible
-- Assign node to NPC local empty_nodes = npc.places.find_node_orthogonally(
npc.places.add_shared(self, place_type..dump(i), nodes[i].node_pos, {"air"}, 0)
place_type, nodes[i].node_pos, empty_nodes[1].pos) -- Check if node is accessible
end if #empty_nodes > 0 then
end -- Assign node to NPC
end 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 end
function npc.places.get_by_type(self, place_type) function npc.places.get_by_type(self, place_type)
local result = {} local result = {}
for place_name, place_entry in pairs(self.places_map) do for place_name, place_entry in pairs(self.places_map) do
if place_entry.type == place_type then if place_entry.type == place_type then
table.insert(result, place_entry) table.insert(result, place_entry)
end end
end end
return result return result
end end
-- This function searches on a squared are of the given radius -- This function searches on a squared are of the given radius
-- for nodes of the given type. The type should be npc.places.nodes -- for nodes of the given type. The type should be npc.places.nodes
function npc.places.find_node_nearby(pos, type, radius) function npc.places.find_node_nearby(pos, type, radius)
-- Determine area points -- Determine area points
local start_pos = {x=pos.x - radius, y=pos.y - 1, z=pos.z - radius} 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} local end_pos = {x=pos.x + radius, y=pos.y + 1, z=pos.z + radius}
-- Get nodes -- Get nodes
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type) local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
return nodes return nodes
end end
-- TODO: This function can be improved to support a radius greater than 1. -- TODO: This function can be improved to support a radius greater than 1.
function npc.places.find_node_orthogonally(pos, nodes, y_adjustment) function npc.places.find_node_orthogonally(pos, nodes, y_adjustment)
-- Calculate orthogonal points -- Calculate orthogonal points
local 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-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})
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 = {} local result = {}
for _,point in pairs(points) do for _,point in pairs(points) do
local node = minetest.get_node(point) local node = minetest.get_node(point)
--minetest.log("Found node: "..dump(node)..", at pos: "..dump(point)) --minetest.log("Found node: "..dump(node)..", at pos: "..dump(point))
for _,node_name in pairs(nodes) do for _,node_name in pairs(nodes) do
if node.name == node_name then if node.name == node_name then
table.insert(result, {name=node.name, pos=point, param2=node.param2}) table.insert(result, {name=node.name, pos=point, param2=node.param2})
end end
end end
end end
return result return result
end end
function npc.places.find_node_in_area(start_pos, end_pos, type) function npc.places.find_node_in_area(start_pos, end_pos, type)
local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type) local nodes = minetest.find_nodes_in_area(start_pos, end_pos, type)
return nodes return nodes
end end
-- Specialized function to find doors that are an entrance to a building. -- 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 -- Based on this definition, other entrances aren't going to be used
-- by the NPC to get into the building -- by the NPC to get into the building
function npc.places.find_entrance_from_openable_nodes(all_openable_nodes, marker_pos) function npc.places.find_entrance_from_openable_nodes(all_openable_nodes, marker_pos)
local result = nil local result = nil
local openable_nodes = {} local openable_nodes = {}
local min = 100 local min = 100
-- Filter out all other openable nodes except MTG doors. -- Filter out all other openable nodes except MTG doors.
-- Why? For supported village types (which are: medieval, nore -- Why? For supported village types (which are: medieval, nore
-- and logcabin) all buildings use, as the main entrance, -- and logcabin) all buildings use, as the main entrance,
-- a MTG door. Some medieval building have "half_doors" (like farms) -- a MTG door. Some medieval building have "half_doors" (like farms)
-- which NPCs love to confuse with the right building entrance. -- which NPCs love to confuse with the right building entrance.
for i = 1, #all_openable_nodes do for i = 1, #all_openable_nodes do
local name = minetest.get_node(all_openable_nodes[i].node_pos).name local name = minetest.get_node(all_openable_nodes[i].node_pos).name
local doors_st, doors_en = string.find(name, "doors:") local doors_st, doors_en = string.find(name, "doors:")
if doors_st ~= nil then if doors_st ~= nil then
table.insert(openable_nodes, all_openable_nodes[i]) table.insert(openable_nodes, all_openable_nodes[i])
end end
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 -- Get node name - check if this node is a 'door'. The way to check
-- is by explicitly checking for 'door' string -- is by explicitly checking for 'door' string
local name = minetest.get_node(open_pos).name local name = minetest.get_node(open_pos).name
local start_i, end_i = string.find(name, "door") local start_i, end_i = string.find(name, "door")
if start_i ~= nil then if start_i ~= nil then
-- Define start and end pos -- Define start and end pos
local start_pos = {x=open_pos.x, y=open_pos.y, z=open_pos.z} 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} local end_pos = {x=marker_pos.x, y=marker_pos.y, z=marker_pos.z}
-- Check if there's any difference in vertical position -- Check if there's any difference in vertical position
-- minetest.log("Openable node pos: "..minetest.pos_to_string(open_pos)) -- minetest.log("Openable node pos: "..minetest.pos_to_string(open_pos))
-- minetest.log("Plotmarker node pos: "..minetest.pos_to_string(marker_pos)) -- minetest.log("Plotmarker node pos: "..minetest.pos_to_string(marker_pos))
-- NOTE: Commented out while testing MarkBu's pathfinder -- NOTE: Commented out while testing MarkBu's pathfinder
--if start_pos.y ~= end_pos.y then --if start_pos.y ~= end_pos.y then
-- Adjust to make pathfinder find nodes one node above -- Adjust to make pathfinder find nodes one node above
-- end_pos.y = start_pos.y -- end_pos.y = start_pos.y
--end --end
-- This adjustment allows the map to be created correctly -- This adjustment allows the map to be created correctly
--start_pos.y = start_pos.y + 1 --start_pos.y = start_pos.y + 1
--end_pos.y = end_pos.y + 1 --end_pos.y = end_pos.y + 1
-- Find path from the openable node to the plotmarker -- Find path from the openable node to the plotmarker
--local path = pathfinder.find_path(start_pos, end_pos, 20, {}) --local path = pathfinder.find_path(start_pos, end_pos, 20, {})
local entity = {} local entity = {}
entity.collisionbox = {-0.20,-1.0,-0.20, 0.20,0.8,0.20} 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("Start pos: "..minetest.pos_to_string(start_pos))
--minetest.log("End pos: "..minetest.pos_to_string(end_pos)) --minetest.log("End pos: "..minetest.pos_to_string(end_pos))
local path = npc.pathfinder.find_path(start_pos, end_pos, entity, false) local path = npc.pathfinder.find_path(start_pos, end_pos, entity, false)
--minetest.log("Found path: "..dump(path)) --minetest.log("Found path: "..dump(path))
if path ~= nil then if path ~= nil then
--minetest.log("Path distance: "..dump(#path)) --minetest.log("Path distance: "..dump(#path))
-- Check if path length is less than the minimum found so far -- Check if path length is less than the minimum found so far
if #path < min then if #path < min then
-- Set min to path length and the result to the currently found node -- Set min to path length and the result to the currently found node
min = #path min = #path
result = openable_nodes[i] result = openable_nodes[i]
else else
-- Specific check to prefer mtg's doors to cottages' doors. -- Specific check to prefer mtg's doors to cottages' doors.
-- The reason? Sometimes a cottages' door could be closer to the -- The reason? Sometimes a cottages' door could be closer to the
-- plotmarker, but not being the building entrance. MTG doors -- plotmarker, but not being the building entrance. MTG doors
-- are usually the entrance... so yes, hackity hack. -- are usually the entrance... so yes, hackity hack.
-- Get the name of the currently mininum-distance door -- Get the name of the currently mininum-distance door
min_node_name = minetest.get_node(result.node_pos).name min_node_name = minetest.get_node(result.node_pos).name
-- Check if this is a door from MTG's doors. -- Check if this is a door from MTG's doors.
local doors_st, doors_en = string.find(name, "doors:") local doors_st, doors_en = string.find(name, "doors:")
-- Check if min-distance door is a cottages door -- Check if min-distance door is a cottages door
-- while we have a MTG door -- while we have a MTG door
if min_node_name == "cottages:half_door" and doors_st ~= nil then if min_node_name == "cottages:half_door" and doors_st ~= nil then
--minetest.log("Assigned new door...") --minetest.log("Assigned new door...")
min = #path min = #path
result = openable_nodes[i] result = openable_nodes[i]
end end
end end
else else
npc.log("ERROR", "Path not found to marker from "..minetest.pos_to_string(start_pos)) npc.log("ERROR", "Path not found to marker from "..minetest.pos_to_string(start_pos))
end end
end end
end end
-- Return result -- Return result
return result return result
end end
-- Specialized function to find all sittable nodes supported by the -- 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 -- stairs nodes placed aren't meant to simulate benches, this function
-- is necessary in order to find stairs that are meant to be benches. -- is necessary in order to find stairs that are meant to be benches.
function npc.places.find_sittable_nodes_nearby(pos, radius) function npc.places.find_sittable_nodes_nearby(pos, radius)
local result = {} local result = {}
-- Try to find sittable nodes -- Try to find sittable nodes
local nodes = npc.places.find_node_nearby(pos, npc.places.nodes.SITTABLE, radius) local nodes = npc.places.find_node_nearby(pos, npc.places.nodes.SITTABLE, radius)
-- Highly unorthodox check for emptinnes -- Highly unorthodox check for emptinnes
if nodes[1] ~= nil then if nodes[1] ~= nil then
for i = 1, #nodes do for i = 1, #nodes do
-- Get node name, try to avoid using the staircase check if not a stair node -- Get node name, try to avoid using the staircase check if not a stair node
local node = minetest.get_node(nodes[i]) local node = minetest.get_node(nodes[i])
local i1, i2 = string.find(node.name, "stairs:") local i1, i2 = string.find(node.name, "stairs:")
if i1 ~= nil then if i1 ~= nil then
if npc.places.is_in_staircase(nodes[i]) < 1 then if npc.places.is_in_staircase(nodes[i]) < 1 then
table.insert(result, nodes[i]) table.insert(result, nodes[i])
end end
else else
-- Add node as it is sittable -- Add node as it is sittable
table.insert(result, nodes[i]) table.insert(result, nodes[i])
end end
end end
end end
-- Return sittable nodes -- Return sittable nodes
return result return result
end end
-- Specialized function to find sittable stairs: stairs that don't -- Specialized function to find sittable stairs: stairs that don't
@ -300,104 +319,104 @@ end
-- Receives a position of a stair node. -- Receives a position of a stair node.
npc.places.staircase = { npc.places.staircase = {
none = 0, none = 0,
bottom = 1, bottom = 1,
middle = 2, middle = 2,
top = 3 top = 3
} }
function npc.places.is_in_staircase(pos) function npc.places.is_in_staircase(pos)
local node = minetest.get_node(pos) local node = minetest.get_node(pos)
-- Verify node is actually from default stairs mod -- Verify node is actually from default stairs mod
local p1, p2 = string.find(node.name, "stairs:") local p1, p2 = string.find(node.name, "stairs:")
if p1 ~= nil then if p1 ~= nil then
-- Calculate the logical position to the lower and upper stairs node location -- Calculate the logical position to the lower and upper stairs node location
local up_x_adj, up_z_adj = 0, 0 local up_x_adj, up_z_adj = 0, 0
local lo_x_adj, lo_z_adj = 0, 0 local lo_x_adj, lo_z_adj = 0, 0
if node.param2 == 1 then if node.param2 == 1 then
up_z_adj = -1 up_z_adj = -1
lo_z_adj = 1 lo_z_adj = 1
elseif node.param2 == 2 then elseif node.param2 == 2 then
up_z_adj = 1 up_z_adj = 1
lo_z_adj = -1 lo_z_adj = -1
elseif node.param2 == 3 then elseif node.param2 == 3 then
up_x_adj = -1 up_x_adj = -1
lo_x_adj = 1 lo_x_adj = 1
elseif node.param2 == 4 then elseif node.param2 == 4 then
up_x_adj = 1 up_x_adj = 1
lo_x_adj = -1 lo_x_adj = -1
else else
-- This is not a staircase -- This is not a staircase
return false return false
end end
-- Calculate upper and lower position -- 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 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} local lower_pos = {x=pos.x + lo_x_adj, y=pos.y - 1, z=pos.z + lo_z_adj}
-- Get next node -- Get next node
local upper_node = minetest.get_node(upper_pos) local upper_node = minetest.get_node(upper_pos)
local lower_node = minetest.get_node(lower_pos) local lower_node = minetest.get_node(lower_pos)
--minetest.log("Next node: "..dump(upper_pos)) --minetest.log("Next node: "..dump(upper_pos))
-- Check if next node is also a stairs node -- Check if next node is also a stairs node
local up_p1, up_p2 = string.find(upper_node.name, "stairs:") local up_p1, up_p2 = string.find(upper_node.name, "stairs:")
local lo_p1, lo_p2 = string.find(lower_node.name, "stairs:") local lo_p1, lo_p2 = string.find(lower_node.name, "stairs:")
if up_p1 ~= nil then if up_p1 ~= nil then
-- By default, think this is bottom of staircase. -- By default, think this is bottom of staircase.
local result = npc.places.staircase.bottom local result = npc.places.staircase.bottom
-- Try downwards now -- Try downwards now
if lo_p1 ~= nil then if lo_p1 ~= nil then
result = npc.places.staircase.middle result = npc.places.staircase.middle
end end
return result return result
else else
-- Check if there is a staircase downwards -- Check if there is a staircase downwards
if lo_p1 ~= nil then if lo_p1 ~= nil then
return npc.places.staircase.top return npc.places.staircase.top
else else
return npc.places.staircase.none return npc.places.staircase.none
end end
end end
end end
-- This is not a stairs node -- This is not a stairs node
return nil return nil
end end
-- Specialized function to find the node position right behind -- Specialized function to find the node position right behind
-- a door. Used to make NPCs enter buildings. -- a door. Used to make NPCs enter buildings.
function npc.places.find_node_behind_door(door_pos) function npc.places.find_node_behind_door(door_pos)
local door = minetest.get_node(door_pos) local door = minetest.get_node(door_pos)
if door.param2 == 0 then if door.param2 == 0 then
-- Looking south -- Looking south
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1} return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
elseif door.param2 == 1 then elseif door.param2 == 1 then
-- Looking east -- Looking east
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z} return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
elseif door.param2 == 2 then elseif door.param2 == 2 then
-- Looking north -- Looking north
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1} return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
-- Looking west -- Looking west
elseif door.param2 == 3 then elseif door.param2 == 3 then
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z} return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
end end
end end
-- Specialized function to find the node position right in -- Specialized function to find the node position right in
-- front of a door. Used to make NPCs exit buildings. -- front of a door. Used to make NPCs exit buildings.
function npc.places.find_node_in_front_of_door(door_pos) function npc.places.find_node_in_front_of_door(door_pos)
local door = minetest.get_node(door_pos) local door = minetest.get_node(door_pos)
--minetest.log("Param2 of door: "..dump(door.param2)) --minetest.log("Param2 of door: "..dump(door.param2))
if door.param2 == 0 then if door.param2 == 0 then
-- Looking south -- Looking south
return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1} return {x=door_pos.x, y=door_pos.y, z=door_pos.z - 1}
elseif door.param2 == 1 then elseif door.param2 == 1 then
-- Looking east -- Looking east
return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z} return {x=door_pos.x - 1, y=door_pos.y, z=door_pos.z}
elseif door.param2 == 2 then elseif door.param2 == 2 then
-- Looking north -- Looking north
return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1} return {x=door_pos.x, y=door_pos.y, z=door_pos.z + 1}
-- Looking west elseif door.param2 == 3 then
elseif door.param2 == 3 then -- Looking west
return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z} return {x=door_pos.x + 1, y=door_pos.y, z=door_pos.z}
end end
end end

View File

@ -34,10 +34,12 @@ dofile(path .. "/actions/actions.lua")
dofile(path .. "/actions/places.lua") dofile(path .. "/actions/places.lua")
dofile(path .. "/actions/pathfinder.lua") dofile(path .. "/actions/pathfinder.lua")
dofile(path .. "/actions/node_registry.lua") dofile(path .. "/actions/node_registry.lua")
dofile(path .. "/occupations/occupations.lua")
-- Load random data definitions -- Load random data definitions
dofile(path .. "/random_data.lua") dofile(path .. "/random_data.lua")
dofile(path .. "/random_data/dialogues_data.lua") dofile(path .. "/random_data/dialogues_data.lua")
dofile(path .. "/random_data/gift_items_data.lua") dofile(path .. "/random_data/gift_items_data.lua")
dofile(path .. "/random_data/names_data.lua") dofile(path .. "/random_data/names_data.lua")
dofile(path .. "/random_data/occupations_data.lua")
print (S("[Mod] Advanced NPC loaded")) print (S("[Mod] Advanced NPC loaded"))

1099
npc.lua

File diff suppressed because it is too large Load Diff

View File

@ -264,7 +264,7 @@ function spawner.assign_places(self, pos)
-- Assign beds -- Assign beds
if #node_data.bed_type > 0 then if #node_data.bed_type > 0 then
-- Assign a specific sittable node to a NPC. -- 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) npc.places.PLACE_TYPE.BED.PRIMARY)
-- Store changes to node_data -- Store changes to node_data
meta:set_string("node_data", minetest.serialize(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 -- Check if there are same or more amount of sits as beds
if #node_data.sittable_type >= #node_data.bed_type then if #node_data.sittable_type >= #node_data.bed_type then
-- Assign a specific sittable node to a NPC. -- 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) npc.places.PLACE_TYPE.SITTABLE.PRIMARY)
-- Store changes to node_data -- Store changes to node_data
meta:set_string("node_data", minetest.serialize(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 -- Check if there are same or more amount of furnace as beds
if #node_data.furnace_type >= #node_data.bed_type then if #node_data.furnace_type >= #node_data.bed_type then
-- Assign a specific furnace node to a NPC. -- 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) npc.places.PLACE_TYPE.FURNACE.PRIMARY)
-- Store changes to node_data -- Store changes to node_data
meta:set_string("node_data", minetest.serialize(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 -- Check if there are same or more amount of storage as beds
if #node_data.storage_type >= #node_data.bed_type then if #node_data.storage_type >= #node_data.bed_type then
-- Assign a specific storage node to a NPC. -- 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) npc.places.PLACE_TYPE.STORAGE.PRIMARY)
-- Store changes to node_data -- Store changes to node_data
meta:set_string("node_data", minetest.serialize(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") 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 if ent and ent:get_luaentity() then
ent:get_luaentity().initialized = false ent:get_luaentity().initialized = false
-- Determine NPC occupation
local occupation_name = "default_basic"
-- Initialize NPC -- Initialize NPC
-- Call with stats if there are NPCs -- Call with stats if there are NPCs
if #npc_table > 0 then if npc_table and #npc_table > 0 then
npc.initialize(ent, pos, false, npc_stats) npc.initialize(ent, pos, false, npc_stats, occupation_name)
else else
npc.initialize(ent, pos) npc.initialize(ent, pos, nil, nil, occupation_name)
end end
-- Assign nodes -- Assign nodes
spawner.assign_places(ent:get_luaentity(), pos) spawner.assign_places(ent:get_luaentity(), pos)
-- Assign schedules -- Assign schedules
spawner.assign_schedules(ent:get_luaentity(), pos) --spawner.assign_schedules(ent:get_luaentity(), pos)
-- Increase NPC spawned count -- Increase NPC spawned count
spawned_npc_count = spawned_npc_count + 1 spawned_npc_count = spawned_npc_count + 1
-- Store count into node -- Store count into node
@ -750,8 +752,8 @@ minetest.register_chatcommand("restore_plotmarkers", {
end end
-- Search for nodes -- Search for nodes
local radius = tonumber(param) local radius = tonumber(param)
local start_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 + radius, 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, local nodes = minetest.find_nodes_in_area_under_air(start_pos, end_pos,
{"mg_villages:plotmarker"}) {"mg_villages:plotmarker"})
-- Check if we have nodes to replace -- Check if we have nodes to replace

View File

@ -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 -- By Zorman2000
npc.utils = {} npc.utils = {}
@ -34,4 +34,43 @@ function npc.utils.get_map_keys(map)
table.insert(result, key) table.insert(result, key)
end end
return result 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 end