Add workplaces support.

Places:
Workplaces are now usable nodes.
Plotmarkers around a building can be scanned and their information stored into current plotmarker.
Slightly optimize plotmarker search.
Occupations:
Add test "priest" occupation definition.
Fix lots of bugs in initialization code.
Spawner:
Add ability to determine occupation name out of surrounding workplaces.
Assign occupation to spawned NPC using simple algorithm.
Others:
Reduce log noise a bit.
Fix some warnings.
This commit is contained in:
Hector Franqui 2017-08-29 14:54:57 -04:00
parent 59bb430e62
commit df56e44bbd
5 changed files with 1093 additions and 988 deletions

View File

@ -302,7 +302,7 @@ if minetest.get_modpath("mg_villages") ~= nil then
if nearby_plotmarkers[i].workplaces then
-- Insert all workplaces in this plotmarker
for j = 1, #nearby_plotmarkers[i].workplaces do
minetest.log("Nearby plotmarker workplace #"..dump(j)..": "..dump(nearby_plotmarkers[i].workplaces[j]))
--minetest.log("Nearby plotmarker workplace #"..dump(j)..": "..dump(nearby_plotmarkers[i].workplaces[j]))
table.insert(result, {
workplace=nearby_plotmarkers[i].workplaces[j],
building_type = nearby_plotmarkers[i].building_type,
@ -328,7 +328,7 @@ function npc.places.find_plotmarkers(pos, radius, exclude_current_pos)
local result = {}
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 nodes = minetest.find_nodes_in_area_under_air(start_pos, end_pos,
local nodes = minetest.find_nodes_in_area(start_pos, end_pos,
npc.places.nodes.PLOTMARKER_TYPE)
-- Scan nodes
for i = 1, #nodes do
@ -357,7 +357,7 @@ function npc.places.find_plotmarkers(pos, radius, exclude_current_pos)
end
end
-- Add building
minetest.log("Adding building: "..dump(def))
--minetest.log("Adding building: "..dump(def))
table.insert(result, def)
end
end

View File

@ -1,24 +1,28 @@
-- Occupations definitions
-- Register default occupation
npc.occupations.register_occupation("default_basic", npc.occupations.basic_def)
npc.occupations.register_occupation(npc.occupations.basic_name, npc.occupations.basic_def)
-- Test priest
npc.occupations.register_occupation("test_priest", {
dialogues = {
{
text = "How are you today my child?",
tags = "male"
type = "given",
data = {
{
text = "How are you today my child?",
tags = {"male"}
}
}
},
textures = {
"npc_male_priest.png"
},
initial_inventory = {
"farming:bread 1"
{name="farming:bread", count=1}
},
initial_trader_status = npc.trade.NONE,
building_types = {
"home"
"hut", "house", "farm_tiny", "lumberjack"
},
surrounding_building_types = {
"church"
@ -28,63 +32,63 @@ npc.occupations.register_occupation("test_priest", {
-- Test farmer
npc.occupations.register_occupation("test_farmer", {
dialogues = {},
textures = {},
initial_inventory = {},
schedule_entries = {
[7] = {
[1] =
dialogues = {},
textures = {},
initial_inventory = {},
schedule_entries = {
[7] = {
[1] =
{
task = npc.actions.cmd.WALK_TO_POS,
args = {
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
walkable = {}
}
},
[2] =
{
check = true,
range = 2,
random_execution_times = true,
min_count = 10,
max_count = 12,
nodes = {"farming:cotton_3"},
actions =
{
task = npc.actions.cmd.WALK_TO_POS,
args = {
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
walkable = {}
}
},
[2] =
{
check = true,
range = 2,
random_execution_times = true,
min_count = 10,
max_count = 12,
nodes = {"farming:cotton_3"},
actions =
-- Actions for grown cotton - harvest and replant
["farming:cotton_3"] =
{
-- Actions for grown cotton - harvest and replant
["farming:cotton_3"] =
[1] =
{
[1] =
action = npc.actions.cmd.WALK_STEP,
},
[2] =
{
action = npc.actions.cmd.DIG,
},
[3] =
{
action = npc.actions.cmd.PLACE,
args =
{
action = npc.actions.cmd.WALK_STEP,
},
[2] =
{
action = npc.actions.cmd.DIG,
},
[3] =
{
action = npc.actions.cmd.PLACE,
args =
{
node = "farming:cotton_1"
}
node = "farming:cotton_1"
}
}
},
none_actions =
{
-- Walk a single step in a random direction
[1] = {
action = npc.actions.cmd.WALK_STEP,
args =
{
dir = "random"
}
},
}
},
none_actions =
{
-- Walk a single step in a random direction
[1] = {
action = npc.actions.cmd.WALK_STEP,
args =
{
dir = "random"
}
},
}
}
}
})
}
})

1250
npc.lua

File diff suppressed because it is too large Load Diff

View File

@ -70,6 +70,9 @@
-- -- items and the specified count, or, a count between min and max
-- -- when the entry contains random=true
-- -- If left empty, it will initialize with random items.
-- initial_trader_status = "",
-- -- String that specifies initial trader value. Valid values are:
-- -- "casual", "trader", "none"
-- schedules_entries = {},
-- -- This is a table of tables in the following format:
-- -- {
@ -95,6 +98,9 @@ local occupations = {}
-- The key is the name of the occupation.
npc.occupations.registered_occupations = {}
-- Basic occupation name
npc.occupations.basic_name = "default_basic"
-- This is the basic occupation definition, this is for all NPCs that
-- don't have a specific occupation. It serves as an example.
npc.occupations.basic_def = {
@ -110,15 +116,15 @@ npc.occupations.basic_def = {
[7] = {
-- Get out of bed
[1] = {task = npc.actions.cmd.USE_BED, args = {
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
action = npc.actions.const.beds.GET_UP
}
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
action = npc.actions.const.beds.GET_UP
}
},
-- Walk to home inside
[2] = {task = npc.actions.cmd.WALK_TO_POS, args = {
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable = {}
},
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable = {}
},
chance = 75
},
-- Allow mobs_redo wandering
@ -128,9 +134,9 @@ npc.occupations.basic_def = {
[8] = {
-- Walk to outside of home
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
walkable = {}
},
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
walkable = {}
},
chance = 75
},
-- Allow mobs_redo wandering
@ -140,36 +146,36 @@ npc.occupations.basic_def = {
[12] = {
-- Walk to a sittable node
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
end_pos = {place_type=npc.places.PLACE_TYPE.SITTABLE.PRIMARY, use_access_node=true},
walkable = {"cottages:bench"}
},
end_pos = {place_type=npc.places.PLACE_TYPE.SITTABLE.PRIMARY, use_access_node=true},
walkable = {"cottages:bench"}
},
chance = 75
},
-- Sit on the node
[2] = {task = npc.actions.cmd.USE_SITTABLE, args = {
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
action = npc.actions.const.sittable.SIT
},
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
action = npc.actions.const.sittable.SIT
},
depends = {1}
},
-- Stay put into place
[3] = {action = npc.actions.cmd.SET_INTERVAL, args = {
freeze = true,
interval = 35
},
freeze = true,
interval = 35
},
depends = {2}
},
[4] = {action = npc.actions.cmd.SET_INTERVAL, args = {
freeze = true,
interval = npc.actions.default_interval
},
freeze = true,
interval = npc.actions.default_interval
},
depends = {3}
},
-- Get up from sit
[5] = {action = npc.actions.cmd.USE_SITTABLE, args = {
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
action = npc.actions.const.sittable.GET_UP
},
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
action = npc.actions.const.sittable.GET_UP
},
depends = {4}
}
},
@ -177,21 +183,21 @@ npc.occupations.basic_def = {
[13] = {
-- Give NPC money to buy from player
[1] = {property = npc.schedule_properties.put_multiple_items, args = {
itemlist = {
{name="default:iron_lump", random=true, min=2, max=4}
}
},
itemlist = {
{name="default:iron_lump", random=true, min=2, max=4}
}
},
chance = 75
},
-- Change trader status to "trader"
[2] = {property = npc.schedule_properties.trader_status, args = {
status = npc.trade.TRADER
},
status = npc.trade.TRADER
},
chance = 75
},
[3] = {property = npc.schedule_properties.can_receive_gifts, args = {
can_receive_gifts = false
},
can_receive_gifts = false
},
depends = {1}
},
-- Allow mobs_redo wandering
@ -201,19 +207,19 @@ npc.occupations.basic_def = {
[18] = {
-- Change trader status to "none"
[1] = {property = npc.schedule_properties.trader_status, args = {
status = npc.trade.NONE
}
status = npc.trade.NONE
}
},
-- Enable gift receiving again
[2] = {property = npc.schedule_properties.can_receive_gifts, args = {
can_receive_gifts = true
}
can_receive_gifts = true
}
},
-- Get inside home
[3] = {task = npc.actions.cmd.WALK_TO_POS, args = {
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable = {}
}
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable = {}
}
},
-- Allow mobs_redo wandering
[4] = {action = npc.actions.cmd.FREEZE, args = {freeze = false}}
@ -221,15 +227,15 @@ npc.occupations.basic_def = {
-- Schedule entry for 10 in the evening
[22] = {
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
end_pos = {place_type=npc.places.PLACE_TYPE.BED.PRIMARY, use_access_node=true},
walkable = {}
}
end_pos = {place_type=npc.places.PLACE_TYPE.BED.PRIMARY, use_access_node=true},
walkable = {}
}
},
-- Use bed
[2] = {task = npc.actions.cmd.USE_BED, args = {
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
action = npc.actions.const.beds.LAY
}
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
action = npc.actions.const.beds.LAY
}
},
-- Stay put on bed
[3] = {action = npc.actions.cmd.FREEZE, args = {freeze = true}}
@ -249,7 +255,7 @@ end
-- of occupation names (strings)
function npc.occupations.get_for_building(building_type, surrounding_building_types)
local result = {}
for name,def in pairs(npc.registered_occupations) do
for name,def in pairs(npc.occupations.registered_occupations) do
-- Check for empty or nil building types, in that case, any building
if def.building_types == nil or def.building_types == {} then
-- Empty building types, add to result
@ -259,13 +265,18 @@ function npc.occupations.get_for_building(building_type, surrounding_building_ty
if npc.utils.array_contains(def.building_types, building_type) then
-- Check for empty or nil surrounding building types
if def.surrounding_building_types == nil
or def.surrounding_building_types == {} then
or def.surrounding_building_types == {} then
-- Add this occupation
table.insert(result, name)
else
-- Check if surround type is included in the def
if npc.utils.array_is_subset_of_array(def.surrounding_building_types,
surrounding_building_types) then
if npc.utils.array_is_subset_of_array(
def.surrounding_building_types,
surrounding_building_types)
or npc.utils.array_is_subset_of_array(
surrounding_building_types,
def.surrounding_building_types)
then
-- Add this occupation
table.insert(result, name)
end
@ -294,16 +305,20 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
minetest.log("Texture entries: "..dump(table.getn(def.textures)))
if def.textures and table.getn(def.textures) > 0 then
self.selected_texture =
npc.get_random_texture_from_array(self.sex, self.age, def.textures)
npc.get_random_texture_from_array(self.sex, self.age, def.textures)
-- Set texture if it found for sex and age
minetest.log("No textures found for sex "..dump(self.sex).." and age "..dump(self.age))
if self.selected_texture then
-- Set texture and base texture
self.textures = self.selected_texture
self.base_texture = self.selected_texture
-- Refresh entity
self.object:set_properties(self)
-- If an array was returned, select a random texture from it
if type(self.selected_texture) == "table" then
local selected_texture = self.selected_texture[math.random(1, #self.selected_texture)]
self.selected_texture = selected_texture
end
-- Set texture and base texture
self.textures = {self.selected_texture}
self.base_texture = {self.selected_texture }
-- Assign sex based on texture
self.sex = npc.assign_sex_from_texture(self)
-- Refresh entity
self.object:set_properties(self)
end
-- Initialize inventory
@ -321,16 +336,17 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
end
-- Initialize dialogues
if def.dialogues and table.getn(def.dialogues) > 0 then
if def.dialogues then
local dialogue_keys = {}
-- Check which type of dialogues we have
if def.dialogues.type == "given" then
-- We have been given the dialogues, so def.dialogues.data contains
-- an array of dialogues
for _, dialogue in def.dialogues.data do
for _, dialogue in pairs(def.dialogues.data) do
-- Add to the dialogue tags the "occupation name"
table.insert(dialogue.tags, occupation_name)
-- Register dialogue
npc.log("INFO", "Registering dialogue for occupation "..dump(occupation_name)..": "..dump(dialogue))
local key = npc.dialogue.register_dialogue(dialogue)
-- Add key to set of dialogue keys
table.insert(dialogue_keys, key)
@ -340,7 +356,7 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
-- an array of dialogues and def.dialogues.tags contains an array of
-- tags that we will use to search
-- Register dialogues
for _, dialogue in def.dialogues.data do
for _, dialogue in pairs(def.dialogues.data) do
-- Add to the dialogue tags the "occupation name"
table.insert(dialogue.tags, occupation_name)
-- Register dialogue
@ -351,7 +367,7 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
-- Find dialogues using tags
local dialogues = npc.search_dialogue_by_tags(def.dialogues.tags, true)
-- Add keys to set of dialogue keys
for _, key in npc.utils.get_map_keys(dialogues) do
for _, key in pairs(npc.utils.get_map_keys(dialogues)) do
table.insert(dialogue_keys, key)
end
elseif def.dialogues.type == "tags" then
@ -368,16 +384,21 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
max_dialogue_count = def.dialogues.max_count
end
-- Add dialogues to the normal dialogues for NPC
self.dialogues.normal = {}
for i = 1, max_dialogue_count do
self.dialogues.normal[i] = dialogue_keys[i]
end
self.dialogues.normal = dialogue_keys
-- for i = 1, max_dialogue_count do
-- self.dialogues.normal[i] = dialogue_keys[i]
-- end
end
-- Initialize trader status
if def.initial_trader_status then
self.trader_data.trader_status = def.initial_trader_status
end
-- Initialize schedule entries
if def.schedules_entries and table.getn(npc.utils.get_map_keys(def.schedules_entries)) > 0 then
-- Create schedule in NPC
npc.create_schedule(self, npc.schedule_types.generic, 0)
npc.create_schedule(self, npc.schedule_types.generic, 0)
-- Traverse schedules
for time, entries in pairs(def.schedules_entries) do
-- Add schedule entry for each time

View File

@ -61,14 +61,14 @@ npc.spawner.replacement_interval = 60
npc.spawner.spawn_delay = 10
npc.spawner.spawn_data = {
status = {
dead = 0,
alive = 1
},
age = {
adult = "adult",
child = "child"
}
status = {
dead = 0,
alive = 1
},
age = {
adult = "adult",
child = "child"
}
}
-- Array of nodes that serve as plotmarker of a plot, and therefore
@ -158,18 +158,18 @@ function npc.spawner.scan_area_for_spawn(start_pos, end_pos, player_name)
-- Initialize NPC stats
-- Initialize NPC stats
local npc_stats = {
male = {
total = 0,
adult = 0,
child = 0
},
female = {
total = 0,
adult = 0,
child = 0
},
adult_total = 0,
child_total = 0
male = {
total = 0,
adult = 0,
child = 0
},
female = {
total = 0,
adult = 0,
child = 0
},
adult_total = 0,
child_total = 0
}
result.building_npc_stats = npc_stats
@ -180,6 +180,78 @@ end
-- Spawning functions
---------------------------------------------------------------------------------------
function npc.spawner.determine_npc_occupation(building_type, workplace_nodes, npcs)
local surrounding_buildings_map = {}
local current_building_map = {}
local current_building_npc_occupations = {}
-- Get all occupation names in the current building
for i = 1, #npcs do
if not npc.utils.array_contains(current_building_npc_occupations, npcs[i].occupation) then
table.insert(current_building_npc_occupations, npcs[i].occupations)
end
end
minetest.log("Occupation names in current building: "..dump(current_building_npc_occupations))
-- Classify workplaces
for i = 1, #workplace_nodes do
local workplace = workplace_nodes[i]
if workplace.surrounding_workplace == true then
surrounding_buildings_map[workplace.building_type] = workplace
else
current_building_map[workplace.building_type] = workplace
end
end
minetest.log("Surrounding workplaces map: "..dump(surrounding_buildings_map))
minetest.log("Current building type: "..dump(building_type))
-- Get occupation names for the buildings
local occupation_names = npc.occupations.get_for_building(
building_type,
npc.utils.get_map_keys(surrounding_buildings_map)
)
-----------------------
-- Determine occupation
-----------------------
-- First of all, iterate through all names, discard the default basic occupation.
-- Next, check if no-one in this builiding has this occupation name.
-- Next, check if the workplace node has no data assigned to it.
-- Finally, if not, return an table with the occupation name, and the selected
-- workplace node.
-- Note: Much more can be done here. This is a simplistic implementation,
-- given this is already complicated enough. For example, existing NPCs' occupation
-- can play a much more important role, not only taken in consideration for discarding.
for i = 1, #occupation_names do
-- Check if this occupation name is the default occupation, and if it is, continue
if occupation_names[i] ~= npc.occupations.basic_name then
-- Check if someone already works on this
if not npc.utils.array_contains(current_building_npc_occupations, occupation_names[i]) then
-- Check if someone else already has this occupation at the same workplace
for j = 1, #workplace_nodes do
-- Get building types from occupation
local local_building_types =
npc.occupations.registered_occupations[occupation_names[i]].building_type or {}
local surrounding_building_types =
npc.occupations.registered_occupations[occupation_names[i]].surrounding_building_types or {}
minetest.log("Occupation btype: "..dump(local_building_types))
minetest.log("Surrounding btypes: "..dump(surrounding_building_types))
-- Check the workplace_node is of any of those building_types
if npc.utils.array_contains(local_building_types, workplace_nodes[j].building_type) or
npc.utils.array_contains(surrounding_building_types, workplace_nodes[j].building_type) then
minetest.log("Found corresponding node: "..dump(workplace_nodes[j]))
local meta = minetest.get_meta(workplace_nodes[j].node_pos)
local worker_data = minetest.deserialize(meta:get_string("worker_data") or "")
-- If no worker data is found, then create it
if not worker_data then
return {name=occupation_names[i], node=workplace_nodes[j]}
end
end
end
end
end
end
return nil
end
-- This function is called when the node timer for spawning NPC
-- is expired. Can be called manually by supplying either:
-- - Position of mg_villages plotmarker, or,
@ -204,11 +276,15 @@ function npc.spawner.spawn_npc_on_plotmarker(pos)
area_info["npc_count"] = meta:get_int("npc_count")
area_info["spawned_npc_count"] = meta:get_int("spawned_npc_count")
area_info["building_type"] = minetest.deserialize(meta:get_string("building_type"))
-- Determine occupation
area_info["building_type"] = meta:get_string("building_type")
local nearby_plotmarkers = minetest.deserialize(meta:get_string("nearby_plotmarkers"))
local occupation_data = npc.spawner.determine_npc_occupation(
area_info.building_type,
area_info.node_data.workplace_type,
area_info.npcs)
local metadata = npc.spawner.spawn_npc(pos, area_info)
local metadata = npc.spawner.spawn_npc(pos, area_info, occupation_data.name)
-- Set all metadata back into the node
-- Increase NPC spawned count
@ -273,7 +349,7 @@ function npc.spawner.spawn_npc(pos, area_info, occupation_name)
if npc_table and #npc_table > 0 then
npc.initialize(ent, pos, false, npc_stats, occupation)
else
npc.initialize(ent, pos, nil, nil, occupation)
npc.initialize(ent, pos, nil, nil, occupation)
end
-- If entrance and node_data are present, assign nodes
if entrance and node_data then
@ -284,13 +360,13 @@ function npc.spawner.spawn_npc(pos, area_info, occupation_name)
if ent:get_luaentity().child then
age = npc.age.child
end
-- TODO: Add more information here at some time...
local entry = {
status = npc.spawner.spawn_data.status.alive,
name = ent:get_luaentity().name,
id = ent:get_luaentity().npc_id,
sex = ent:get_luaentity().sex,
age = age,
occupation = occupation,
born_day = minetest.get_day_count()
}
minetest.log("Area info: "..dump(area_info))
@ -298,12 +374,12 @@ function npc.spawner.spawn_npc(pos, area_info, occupation_name)
-- Update and store stats
-- Increase total of NPCs for specific sex
npc_stats[ent:get_luaentity().sex].total =
npc_stats[ent:get_luaentity().sex].total + 1
npc_stats[ent:get_luaentity().sex].total + 1
-- Increase total number of NPCs by age
npc_stats[age.."_total"] = npc_stats[age.."_total"] + 1
-- Increase number of NPCs by age and sex
npc_stats[ent:get_luaentity().sex][age] =
npc_stats[ent:get_luaentity().sex][age] + 1
npc_stats[ent:get_luaentity().sex][age] + 1
area_info.npc_stats = npc_stats
-- Return
npc.log("INFO", "Spawning successful!")
@ -337,7 +413,7 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
-- Assign plotmarker if position given
if pos then
npc.places.add_shared(self, npc.places.PLACE_TYPE.OTHER.HOME_PLOTMARKER,
npc.places.PLACE_TYPE.OTHER.HOME_PLOTMARKER, pos)
npc.places.PLACE_TYPE.OTHER.HOME_PLOTMARKER, pos)
end
-- Assign entrance door and related locations
@ -353,11 +429,11 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
-- Assign beds
if #node_data.bed_type > 0 then
-- Assign a specific sittable node to a NPC.
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))
-- Assign a specific sittable node to a NPC.
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))
end
-- Assign sits
@ -389,7 +465,7 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
-- Add all furnaces to places as shared since NPC should be able to use
-- any accessible furnace
npc.places.add_shared_accessible_place(self, node_data.furnace_type,
npc.places.PLACE_TYPE.FURNACE.SHARED)
npc.places.PLACE_TYPE.FURNACE.SHARED)
end
-- Assign storage nodes
@ -417,14 +493,14 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
npc.log("DEBUG", "Places for NPC "..self.npc_name..": "..dump(self.places_map))
-- Make NPC go into their house
-- Make NPC go into their house
-- If entrance is available let NPC
if entrance then
npc.add_task(self,
npc.actions.cmd.WALK_TO_POS,
{end_pos=npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable={}})
npc.add_action(self, npc.actions.cmd.FREEZE, {freeze = false})
npc.add_task(self,
npc.actions.cmd.WALK_TO_POS,
{end_pos=npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
walkable={}})
npc.add_action(self, npc.actions.cmd.FREEZE, {freeze = false})
end
return node_data
@ -438,7 +514,7 @@ function npc.spawner.calculate_npc_spawning_on_plotmarker(pos)
-- Check node metadata
local meta = minetest.get_meta(pos)
if meta:get_string("replaced") ~= "true" then
return
return
end
-- Get nodes for this building
local node_data = minetest.deserialize(meta:get_string("node_data"))
@ -497,61 +573,61 @@ if minetest.get_modpath("mg_villages") ~= nil then
--minetest.log("Building data: "..dump(building_data))
--minetest.log("--------------------------------------------")
-- Get area of the building
local x_size = building_data.bsizex
local y_size = building_data.ysize
local z_size = building_data.bsizez
local brotate = building_data.brotate
local start_pos = {x=pos.x, y=pos.y, z=pos.z}
local x_sign, z_sign = 1, 1
local x_size = building_data.bsizex
local y_size = building_data.ysize
local z_size = building_data.bsizez
local brotate = building_data.brotate
local start_pos = {x=pos.x, y=pos.y, z=pos.z}
local x_sign, z_sign = 1, 1
-- Check plot direction
-- NOTE: Below values may be wrong, very wrong!
-- 0 - facing West, -X
-- 1 - facing North, +Z
-- 2 - facing East, +X
-- 3 - facing South -Z
if brotate == 0 then
x_sign, z_sign = 1, -1
elseif brotate == 1 then
x_sign, z_sign = -1, -1
local temp = z_size
z_size = x_size
x_size = temp
elseif brotate == 2 then
x_sign, z_sign = -1, 1
elseif brotate == 3 then
x_sign, z_sign = 1, 1
end
-- Check plot direction
-- NOTE: Below values may be wrong, very wrong!
-- 0 - facing West, -X
-- 1 - facing North, +Z
-- 2 - facing East, +X
-- 3 - facing South -Z
if brotate == 0 then
x_sign, z_sign = 1, -1
elseif brotate == 1 then
x_sign, z_sign = -1, -1
local temp = z_size
z_size = x_size
x_size = temp
elseif brotate == 2 then
x_sign, z_sign = -1, 1
elseif brotate == 3 then
x_sign, z_sign = 1, 1
end
------------------------
-- For debug:
------------------------
-- Red is x marker
--minetest.set_node({x=pos.x + (x_sign * x_size),y=pos.y,z=pos.z}, {name = "wool:red"})
--minetest.get_meta({x=pos.x + (x_sign * x_size),y=pos.y,z=pos.z}):set_string("infotext", minetest.get_meta(pos):get_string("infotext")..", Axis: x, Sign: "..dump(x_sign))
-- Blue is z marker
--minetest.set_node({x=pos.x,y=pos.y,z=pos.z + (z_sign * z_size)}, {name = "wool:blue"})
--minetest.get_meta({x=pos.x,y=pos.y,z=pos.z + (z_sign * z_size)}):set_string("infotext", minetest.get_meta(pos):get_string("infotext")..", Axis: z, Sign: "..dump(z_sign))
------------------------
-- For debug:
------------------------
-- Red is x marker
--minetest.set_node({x=pos.x + (x_sign * x_size),y=pos.y,z=pos.z}, {name = "wool:red"})
--minetest.get_meta({x=pos.x + (x_sign * x_size),y=pos.y,z=pos.z}):set_string("infotext", minetest.get_meta(pos):get_string("infotext")..", Axis: x, Sign: "..dump(x_sign))
-- Blue is z marker
--minetest.set_node({x=pos.x,y=pos.y,z=pos.z + (z_sign * z_size)}, {name = "wool:blue"})
--minetest.get_meta({x=pos.x,y=pos.y,z=pos.z + (z_sign * z_size)}):set_string("infotext", minetest.get_meta(pos):get_string("infotext")..", Axis: z, Sign: "..dump(z_sign))
npc.log("DEBUG", "Start pos: "..minetest.pos_to_string(start_pos))
npc.log("DEBUG", "Plot: "..dump(minetest.get_meta(start_pos):get_string("infotext")))
npc.log("DEBUG", "Start pos: "..minetest.pos_to_string(start_pos))
npc.log("DEBUG", "Plot: "..dump(minetest.get_meta(start_pos):get_string("infotext")))
npc.log("DEBUG", "Brotate: "..dump(brotate))
npc.log("DEBUG", "X_sign: "..dump(x_sign))
npc.log("DEBUG", "X_adj: "..dump(x_sign*x_size))
npc.log("DEBUG", "Z_sign: "..dump(z_sign))
npc.log("DEBUG", "Z_adj: "..dump(z_sign*z_size))
npc.log("DEBUG", "Brotate: "..dump(brotate))
npc.log("DEBUG", "X_sign: "..dump(x_sign))
npc.log("DEBUG", "X_adj: "..dump(x_sign*x_size))
npc.log("DEBUG", "Z_sign: "..dump(z_sign))
npc.log("DEBUG", "Z_adj: "..dump(z_sign*z_size))
local end_pos = {x=pos.x + (x_sign * x_size), y=pos.y + y_size, z=pos.z + (z_sign * z_size)}
local end_pos = {x=pos.x + (x_sign * x_size), y=pos.y + y_size, z=pos.z + (z_sign * z_size)}
-- For debug:
--minetest.set_node(start_pos, {name="default:mese_block"})
--minetest.set_node(end_pos, {name="default:mese_block"})
--minetest.get_meta(end_pos):set_string("infotext", minetest.get_meta(start_pos):get_string("infotext"))
-- For debug:
--minetest.set_node(start_pos, {name="default:mese_block"})
--minetest.set_node(end_pos, {name="default:mese_block"})
--minetest.get_meta(end_pos):set_string("infotext", minetest.get_meta(start_pos):get_string("infotext"))
npc.log("DEBUG", "Calculated end pos: "..minetest.pos_to_string(end_pos))
npc.log("DEBUG", "Calculated end pos: "..minetest.pos_to_string(end_pos))
return npc.places.scan_area_for_usable_nodes(start_pos, end_pos)
return npc.places.scan_area_for_usable_nodes(start_pos, end_pos)
end
-- This function "adapts" an existent mg_villages:plotmarker for NPC spawning.
@ -567,7 +643,7 @@ if minetest.get_modpath("mg_villages") ~= nil then
local infotext = meta:get_string("infotext")
-- Check for nil values above
if (not village_id or (village_id and village_id == ""))
or (not plot_nr or (plot_nr and plot_nr == 0)) then
or (not plot_nr or (plot_nr and plot_nr == 0)) then
return
end
@ -612,8 +688,8 @@ if minetest.get_modpath("mg_villages") ~= nil then
-- Store nodedata into the spawner's metadata
meta:set_string("node_data", minetest.serialize(nodedata))
-- Find nearby plotmarkers, excluding current plotmarker
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 40, true)
minetest.log("Found nearby plotmarkers: "..dump(nearby_plotmarkers))
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 35, true)
--minetest.log("Found nearby plotmarkers: "..dump(nearby_plotmarkers))
meta:set_string("nearby_plotmarkers", minetest.serialize(nearby_plotmarkers))
-- Check if building position data is also available (recent mg_villages)
if building_pos_data then
@ -624,22 +700,22 @@ if minetest.get_modpath("mg_villages") ~= nil then
meta:set_string("npcs", minetest.serialize(npcs))
-- Initialize NPC stats
local npc_stats = {
male = {
total = 0,
male = {
total = 0,
adult = 0,
child = 0
},
female = {
total = 0,
child = 0
},
female = {
total = 0,
adult = 0,
child = 0
},
adult_total = 0,
child_total = 0
}
meta:set_string("npc_stats", minetest.serialize(npc_stats))
-- Set replaced
meta:set_string("replaced", "true")
}
meta:set_string("npc_stats", minetest.serialize(npc_stats))
-- Set replaced
meta:set_string("replaced", "true")
-- Calculate how many NPCs will spawn
npc.spawner.calculate_npc_spawning_on_plotmarker(pos)
-- Stop searching for building type
@ -648,107 +724,107 @@ if minetest.get_modpath("mg_villages") ~= nil then
end
end
-- Node registration
-- This node is currently a slightly modified mg_villages:plotmarker
-- TODO: Change formspec to a more detailed one.
minetest.override_item("mg_villages:plotmarker", {
-- description = "Automatic NPC Spawner",
-- drawtype = "nodebox",
-- tiles = {"default_stone.png"},
-- paramtype = "light",
-- paramtype2 = "facedir",
-- node_box = {
-- type = "fixed",
-- fixed = {
-- {-0.5+2/16, -0.5, -0.5+2/16, 0.5-2/16, -0.5+2/16, 0.5-2/16},
-- --{-0.5+0/16, -0.5, -0.5+0/16, 0.5-0/16, -0.5+0/16, 0.5-0/16},
-- }
-- },
walkable = false,
groups = {cracky=3,stone=2},
-- Node registration
-- This node is currently a slightly modified mg_villages:plotmarker
-- TODO: Change formspec to a more detailed one.
minetest.override_item("mg_villages:plotmarker", {
-- description = "Automatic NPC Spawner",
-- drawtype = "nodebox",
-- tiles = {"default_stone.png"},
-- paramtype = "light",
-- paramtype2 = "facedir",
-- node_box = {
-- type = "fixed",
-- fixed = {
-- {-0.5+2/16, -0.5, -0.5+2/16, 0.5-2/16, -0.5+2/16, 0.5-2/16},
-- --{-0.5+0/16, -0.5, -0.5+0/16, 0.5-0/16, -0.5+0/16, 0.5-0/16},
-- }
-- },
walkable = false,
groups = {cracky=3,stone=2},
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- NOTE: This is temporary code for testing...
local nodedata = minetest.deserialize(minetest.get_meta(pos):get_string("node_data"))
--minetest.log("Node data: "..dump(nodedata))
--minetest.log("Entrance: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("entrance"))))
--minetest.log("First-floor beds: "..dump(spawner.filter_first_floor_nodes(nodedata.bed_type, pos)))
--local entrance = npc.places.find_entrance_from_openable_nodes(nodedata.openable_type, pos)
--minetest.log("Found entrance: "..dump(entrance))
minetest.log("Replaced: "..dump(minetest.get_meta(pos):get_string("replaced")))
-- for i = 1, #nodedata.bed_type do
-- nodedata.bed_type[i].owner = ""
-- end
-- minetest.get_meta(pos):set_string("node_data", minetest.serialize(nodedata))
-- minetest.log("Cleared bed owners")
--minetest.log("NPC stats: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("npc_stats"))))
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
-- NOTE: This is temporary code for testing...
local nodedata = minetest.deserialize(minetest.get_meta(pos):get_string("node_data"))
--minetest.log("Node data: "..dump(nodedata))
--minetest.log("Entrance: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("entrance"))))
--minetest.log("First-floor beds: "..dump(spawner.filter_first_floor_nodes(nodedata.bed_type, pos)))
--local entrance = npc.places.find_entrance_from_openable_nodes(nodedata.openable_type, pos)
--minetest.log("Found entrance: "..dump(entrance))
minetest.log("Replaced: "..dump(minetest.get_meta(pos):get_string("replaced")))
-- for i = 1, #nodedata.bed_type do
-- nodedata.bed_type[i].owner = ""
-- end
-- minetest.get_meta(pos):set_string("node_data", minetest.serialize(nodedata))
-- minetest.log("Cleared bed owners")
--minetest.log("NPC stats: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("npc_stats"))))
return mg_villages.plotmarker_formspec( pos, nil, {}, clicker )
end,
return mg_villages.plotmarker_formspec( pos, nil, {}, clicker )
end,
-- on_receive_fields = function(pos, formname, fields, sender)
-- return mg_villages.plotmarker_formspec( pos, formname, fields, sender );
-- end,
-- on_receive_fields = function(pos, formname, fields, sender)
-- return mg_villages.plotmarker_formspec( pos, formname, fields, sender );
-- end,
on_timer = function(pos, elapsed)
npc.spawner.spawn_npc_on_plotmarker(pos)
end,
on_timer = function(pos, elapsed)
npc.spawner.spawn_npc_on_plotmarker(pos)
end,
-- protect against digging
-- can_dig = function(pos, player)
-- local meta = minetest.get_meta(pos);
-- if (meta and meta:get_string("village_id") ~= "" and meta:get_int("plot_nr") and meta:get_int("plot_nr") > 0 ) then
-- return false;
-- protect against digging
-- can_dig = function(pos, player)
-- local meta = minetest.get_meta(pos);
-- if (meta and meta:get_string("village_id") ~= "" and meta:get_int("plot_nr") and meta:get_int("plot_nr") > 0 ) then
-- return false;
-- end
-- return true;
-- end
})
-- LBM Registration
-- Used to modify plotmarkers and replace them with advanced_npc:plotmarker_auto_spawner
-- minetest.register_lbm({
-- label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
-- name = "advanced_npc:mg_villages_plotmarker_replacer",
-- nodenames = {"mg_villages:plotmarker"},
-- run_at_every_load = false,
-- action = function(pos, node)
-- -- Check if replacement is activated
-- if npc.spawner.replace_activated then
-- -- Replace mg_villages:plotmarker
-- spawner.replace_mg_villages_plotmarker(pos)
-- -- Set NPCs to spawn
-- spawner.calculate_npc_spawning(pos)
-- end
-- end
-- return true;
-- end
})
-- })
-- LBM Registration
-- Used to modify plotmarkers and replace them with advanced_npc:plotmarker_auto_spawner
-- minetest.register_lbm({
-- label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
-- name = "advanced_npc:mg_villages_plotmarker_replacer",
-- nodenames = {"mg_villages:plotmarker"},
-- run_at_every_load = false,
-- action = function(pos, node)
-- -- Check if replacement is activated
-- if npc.spawner.replace_activated then
-- -- Replace mg_villages:plotmarker
-- spawner.replace_mg_villages_plotmarker(pos)
-- -- Set NPCs to spawn
-- spawner.calculate_npc_spawning(pos)
-- end
-- end
-- })
-- ABM Registration
minetest.register_abm({
label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
nodenames = {"mg_villages:plotmarker"},
interval = 10,--npc.spawner.replacement_interval,
chance = 1,--5,
catch_up = true,
action = function(pos, node, active_object_count, active_object_count_wider)
-- Check if replacement is needed
local meta = minetest.get_meta(pos)
if meta then
-- minetest.log("------ Plotmarker metadata -------")
-- local plot_nr = meta:get_int("plot_nr")
-- local village_id = meta:get_string("village_id")
-- minetest.log("Plot nr: "..dump(plot_nr)..", village ID: "..dump(village_id))
-- minetest.log(dump(mg_villages.get_plot_and_building_data( village_id, plot_nr )))
end
if minetest.get_meta(pos):get_string("replaced") == "true" then
return
end
-- Check if replacement is activated
if npc.spawner.replace_activated then
-- Replace mg_villages:plotmarker
spawner.adapt_mg_villages_plotmarker(pos)
end
end
})
-- ABM Registration
minetest.register_abm({
label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
nodenames = {"mg_villages:plotmarker"},
interval = 10,--npc.spawner.replacement_interval,
chance = 1,--5,
catch_up = true,
action = function(pos, node, active_object_count, active_object_count_wider)
-- Check if replacement is needed
local meta = minetest.get_meta(pos)
if meta then
-- minetest.log("------ Plotmarker metadata -------")
-- local plot_nr = meta:get_int("plot_nr")
-- local village_id = meta:get_string("village_id")
-- minetest.log("Plot nr: "..dump(plot_nr)..", village ID: "..dump(village_id))
-- minetest.log(dump(mg_villages.get_plot_and_building_data( village_id, plot_nr )))
end
if minetest.get_meta(pos):get_string("replaced") == "true" then
return
end
-- Check if replacement is activated
if npc.spawner.replace_activated then
-- Replace mg_villages:plotmarker
spawner.adapt_mg_villages_plotmarker(pos)
end
end
})
end
@ -756,50 +832,50 @@ end
-- Chat commands to manage spawners
minetest.register_chatcommand("restore_plotmarkers", {
description = "Replaces all advanced_npc:plotmarker_auto_spawner with mg_villages:plotmarker in the specified radius.",
privs = {server=true},
func = function(name, param)
-- Check if radius is null
if param == nil and type(param) ~= "number" then
minetest.chat_send_player(name, "Need to enter a radius as an integer number. Ex. /restore_plotmarkers 10 for a radius of 10")
return
description = "Replaces all advanced_npc:plotmarker_auto_spawner with mg_villages:plotmarker in the specified radius.",
privs = {server=true},
func = function(name, param)
-- Check if radius is null
if param == nil and type(param) ~= "number" then
minetest.chat_send_player(name, "Need to enter a radius as an integer number. Ex. /restore_plotmarkers 10 for a radius of 10")
return
end
-- Get player position
local pos = {}
for _,player in pairs(minetest.get_connected_players()) do
if player:get_player_name() == name then
pos = player:get_pos()
break
end
end
-- Search for nodes
local radius = tonumber(param)
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
minetest.chat_send_player(name, "Found "..dump(#nodes).." nodes to replace...")
if #nodes == 0 then
return
end
-- Replace all nodes
for i = 1, #nodes do
local meta = minetest.get_meta(nodes[i])
local village_id = meta:get_string("village_id")
local plot_nr = meta:get_int("plot_nr")
local infotext = meta:get_string("infotext")
-- Set metadata
meta = minetest.get_meta(nodes[i])
meta:set_string("village_id", village_id)
meta:set_int("plot_nr", plot_nr)
meta:set_string("infotext", infotext)
-- Clear NPC stats, NPC data and node data
meta:set_string("node_data", nil)
meta:set_string("npcs", nil)
meta:set_string("npc_stats", nil)
meta:set_string("replaced", "false")
end
minetest.chat_send_player(name, "Finished replacement of "..dump(#nodes).." auto-spawners successfully")
end
-- Get player position
local pos = {}
for _,player in pairs(minetest.get_connected_players()) do
if player:get_player_name() == name then
pos = player:get_pos()
break
end
end
-- Search for nodes
local radius = tonumber(param)
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
minetest.chat_send_player(name, "Found "..dump(#nodes).." nodes to replace...")
if #nodes == 0 then
return
end
-- Replace all nodes
for i = 1, #nodes do
local meta = minetest.get_meta(nodes[i])
local village_id = meta:get_string("village_id")
local plot_nr = meta:get_int("plot_nr")
local infotext = meta:get_string("infotext")
-- Set metadata
meta = minetest.get_meta(nodes[i])
meta:set_string("village_id", village_id)
meta:set_int("plot_nr", plot_nr)
meta:set_string("infotext", infotext)
-- Clear NPC stats, NPC data and node data
meta:set_string("node_data", nil)
meta:set_string("npcs", nil)
meta:set_string("npc_stats", nil)
meta:set_string("replaced", "false")
end
minetest.chat_send_player(name, "Finished replacement of "..dump(#nodes).." auto-spawners successfully")
end
})