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:
parent
59bb430e62
commit
df56e44bbd
@ -302,7 +302,7 @@ if minetest.get_modpath("mg_villages") ~= nil then
|
|||||||
if nearby_plotmarkers[i].workplaces then
|
if nearby_plotmarkers[i].workplaces then
|
||||||
-- Insert all workplaces in this plotmarker
|
-- Insert all workplaces in this plotmarker
|
||||||
for j = 1, #nearby_plotmarkers[i].workplaces do
|
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, {
|
table.insert(result, {
|
||||||
workplace=nearby_plotmarkers[i].workplaces[j],
|
workplace=nearby_plotmarkers[i].workplaces[j],
|
||||||
building_type = nearby_plotmarkers[i].building_type,
|
building_type = nearby_plotmarkers[i].building_type,
|
||||||
@ -328,7 +328,7 @@ function npc.places.find_plotmarkers(pos, radius, exclude_current_pos)
|
|||||||
local result = {}
|
local result = {}
|
||||||
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}
|
||||||
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)
|
npc.places.nodes.PLOTMARKER_TYPE)
|
||||||
-- Scan nodes
|
-- Scan nodes
|
||||||
for i = 1, #nodes do
|
for i = 1, #nodes do
|
||||||
@ -357,7 +357,7 @@ function npc.places.find_plotmarkers(pos, radius, exclude_current_pos)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- Add building
|
-- Add building
|
||||||
minetest.log("Adding building: "..dump(def))
|
--minetest.log("Adding building: "..dump(def))
|
||||||
table.insert(result, def)
|
table.insert(result, def)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,24 +1,28 @@
|
|||||||
-- Occupations definitions
|
-- Occupations definitions
|
||||||
|
|
||||||
-- Register default occupation
|
-- 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
|
-- Test priest
|
||||||
npc.occupations.register_occupation("test_priest", {
|
npc.occupations.register_occupation("test_priest", {
|
||||||
dialogues = {
|
dialogues = {
|
||||||
{
|
type = "given",
|
||||||
text = "How are you today my child?",
|
data = {
|
||||||
tags = "male"
|
{
|
||||||
|
text = "How are you today my child?",
|
||||||
|
tags = {"male"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
textures = {
|
textures = {
|
||||||
"npc_male_priest.png"
|
"npc_male_priest.png"
|
||||||
},
|
},
|
||||||
initial_inventory = {
|
initial_inventory = {
|
||||||
"farming:bread 1"
|
{name="farming:bread", count=1}
|
||||||
},
|
},
|
||||||
|
initial_trader_status = npc.trade.NONE,
|
||||||
building_types = {
|
building_types = {
|
||||||
"home"
|
"hut", "house", "farm_tiny", "lumberjack"
|
||||||
},
|
},
|
||||||
surrounding_building_types = {
|
surrounding_building_types = {
|
||||||
"church"
|
"church"
|
||||||
@ -28,63 +32,63 @@ npc.occupations.register_occupation("test_priest", {
|
|||||||
|
|
||||||
-- Test farmer
|
-- Test farmer
|
||||||
npc.occupations.register_occupation("test_farmer", {
|
npc.occupations.register_occupation("test_farmer", {
|
||||||
dialogues = {},
|
dialogues = {},
|
||||||
textures = {},
|
textures = {},
|
||||||
initial_inventory = {},
|
initial_inventory = {},
|
||||||
schedule_entries = {
|
schedule_entries = {
|
||||||
[7] = {
|
[7] = {
|
||||||
[1] =
|
[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,
|
-- Actions for grown cotton - harvest and replant
|
||||||
args = {
|
["farming:cotton_3"] =
|
||||||
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
|
[1] =
|
||||||
["farming:cotton_3"] =
|
|
||||||
{
|
{
|
||||||
[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,
|
node = "farming:cotton_1"
|
||||||
},
|
|
||||||
[2] =
|
|
||||||
{
|
|
||||||
action = npc.actions.cmd.DIG,
|
|
||||||
},
|
|
||||||
[3] =
|
|
||||||
{
|
|
||||||
action = npc.actions.cmd.PLACE,
|
|
||||||
args =
|
|
||||||
{
|
|
||||||
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"
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
})
|
@ -70,6 +70,9 @@
|
|||||||
-- -- items and the specified count, or, a count between min and max
|
-- -- items and the specified count, or, a count between min and max
|
||||||
-- -- when the entry contains random=true
|
-- -- when the entry contains random=true
|
||||||
-- -- If left empty, it will initialize with random items.
|
-- -- 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 = {},
|
-- schedules_entries = {},
|
||||||
-- -- This is a table of tables in the following format:
|
-- -- This is a table of tables in the following format:
|
||||||
-- -- {
|
-- -- {
|
||||||
@ -95,6 +98,9 @@ local occupations = {}
|
|||||||
-- The key is the name of the occupation.
|
-- The key is the name of the occupation.
|
||||||
npc.occupations.registered_occupations = {}
|
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
|
-- This is the basic occupation definition, this is for all NPCs that
|
||||||
-- don't have a specific occupation. It serves as an example.
|
-- don't have a specific occupation. It serves as an example.
|
||||||
npc.occupations.basic_def = {
|
npc.occupations.basic_def = {
|
||||||
@ -110,15 +116,15 @@ npc.occupations.basic_def = {
|
|||||||
[7] = {
|
[7] = {
|
||||||
-- Get out of bed
|
-- Get out of bed
|
||||||
[1] = {task = npc.actions.cmd.USE_BED, args = {
|
[1] = {task = npc.actions.cmd.USE_BED, args = {
|
||||||
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
|
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
|
||||||
action = npc.actions.const.beds.GET_UP
|
action = npc.actions.const.beds.GET_UP
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Walk to home inside
|
-- Walk to home inside
|
||||||
[2] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
[2] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
||||||
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
||||||
walkable = {}
|
walkable = {}
|
||||||
},
|
},
|
||||||
chance = 75
|
chance = 75
|
||||||
},
|
},
|
||||||
-- Allow mobs_redo wandering
|
-- Allow mobs_redo wandering
|
||||||
@ -128,9 +134,9 @@ npc.occupations.basic_def = {
|
|||||||
[8] = {
|
[8] = {
|
||||||
-- Walk to outside of home
|
-- Walk to outside of home
|
||||||
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
||||||
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
|
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_OUTSIDE,
|
||||||
walkable = {}
|
walkable = {}
|
||||||
},
|
},
|
||||||
chance = 75
|
chance = 75
|
||||||
},
|
},
|
||||||
-- Allow mobs_redo wandering
|
-- Allow mobs_redo wandering
|
||||||
@ -140,36 +146,36 @@ npc.occupations.basic_def = {
|
|||||||
[12] = {
|
[12] = {
|
||||||
-- Walk to a sittable node
|
-- Walk to a sittable node
|
||||||
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
||||||
end_pos = {place_type=npc.places.PLACE_TYPE.SITTABLE.PRIMARY, use_access_node=true},
|
end_pos = {place_type=npc.places.PLACE_TYPE.SITTABLE.PRIMARY, use_access_node=true},
|
||||||
walkable = {"cottages:bench"}
|
walkable = {"cottages:bench"}
|
||||||
},
|
},
|
||||||
chance = 75
|
chance = 75
|
||||||
},
|
},
|
||||||
-- Sit on the node
|
-- Sit on the node
|
||||||
[2] = {task = npc.actions.cmd.USE_SITTABLE, args = {
|
[2] = {task = npc.actions.cmd.USE_SITTABLE, args = {
|
||||||
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
|
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
|
||||||
action = npc.actions.const.sittable.SIT
|
action = npc.actions.const.sittable.SIT
|
||||||
},
|
},
|
||||||
depends = {1}
|
depends = {1}
|
||||||
},
|
},
|
||||||
-- Stay put into place
|
-- Stay put into place
|
||||||
[3] = {action = npc.actions.cmd.SET_INTERVAL, args = {
|
[3] = {action = npc.actions.cmd.SET_INTERVAL, args = {
|
||||||
freeze = true,
|
freeze = true,
|
||||||
interval = 35
|
interval = 35
|
||||||
},
|
},
|
||||||
depends = {2}
|
depends = {2}
|
||||||
},
|
},
|
||||||
[4] = {action = npc.actions.cmd.SET_INTERVAL, args = {
|
[4] = {action = npc.actions.cmd.SET_INTERVAL, args = {
|
||||||
freeze = true,
|
freeze = true,
|
||||||
interval = npc.actions.default_interval
|
interval = npc.actions.default_interval
|
||||||
},
|
},
|
||||||
depends = {3}
|
depends = {3}
|
||||||
},
|
},
|
||||||
-- Get up from sit
|
-- Get up from sit
|
||||||
[5] = {action = npc.actions.cmd.USE_SITTABLE, args = {
|
[5] = {action = npc.actions.cmd.USE_SITTABLE, args = {
|
||||||
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
|
pos = npc.places.PLACE_TYPE.SITTABLE.PRIMARY,
|
||||||
action = npc.actions.const.sittable.GET_UP
|
action = npc.actions.const.sittable.GET_UP
|
||||||
},
|
},
|
||||||
depends = {4}
|
depends = {4}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -177,21 +183,21 @@ npc.occupations.basic_def = {
|
|||||||
[13] = {
|
[13] = {
|
||||||
-- Give NPC money to buy from player
|
-- Give NPC money to buy from player
|
||||||
[1] = {property = npc.schedule_properties.put_multiple_items, args = {
|
[1] = {property = npc.schedule_properties.put_multiple_items, args = {
|
||||||
itemlist = {
|
itemlist = {
|
||||||
{name="default:iron_lump", random=true, min=2, max=4}
|
{name="default:iron_lump", random=true, min=2, max=4}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
chance = 75
|
chance = 75
|
||||||
},
|
},
|
||||||
-- Change trader status to "trader"
|
-- Change trader status to "trader"
|
||||||
[2] = {property = npc.schedule_properties.trader_status, args = {
|
[2] = {property = npc.schedule_properties.trader_status, args = {
|
||||||
status = npc.trade.TRADER
|
status = npc.trade.TRADER
|
||||||
},
|
},
|
||||||
chance = 75
|
chance = 75
|
||||||
},
|
},
|
||||||
[3] = {property = npc.schedule_properties.can_receive_gifts, args = {
|
[3] = {property = npc.schedule_properties.can_receive_gifts, args = {
|
||||||
can_receive_gifts = false
|
can_receive_gifts = false
|
||||||
},
|
},
|
||||||
depends = {1}
|
depends = {1}
|
||||||
},
|
},
|
||||||
-- Allow mobs_redo wandering
|
-- Allow mobs_redo wandering
|
||||||
@ -201,19 +207,19 @@ npc.occupations.basic_def = {
|
|||||||
[18] = {
|
[18] = {
|
||||||
-- Change trader status to "none"
|
-- Change trader status to "none"
|
||||||
[1] = {property = npc.schedule_properties.trader_status, args = {
|
[1] = {property = npc.schedule_properties.trader_status, args = {
|
||||||
status = npc.trade.NONE
|
status = npc.trade.NONE
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Enable gift receiving again
|
-- Enable gift receiving again
|
||||||
[2] = {property = npc.schedule_properties.can_receive_gifts, args = {
|
[2] = {property = npc.schedule_properties.can_receive_gifts, args = {
|
||||||
can_receive_gifts = true
|
can_receive_gifts = true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Get inside home
|
-- Get inside home
|
||||||
[3] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
[3] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
||||||
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
end_pos = npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
||||||
walkable = {}
|
walkable = {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Allow mobs_redo wandering
|
-- Allow mobs_redo wandering
|
||||||
[4] = {action = npc.actions.cmd.FREEZE, args = {freeze = false}}
|
[4] = {action = npc.actions.cmd.FREEZE, args = {freeze = false}}
|
||||||
@ -221,15 +227,15 @@ npc.occupations.basic_def = {
|
|||||||
-- Schedule entry for 10 in the evening
|
-- Schedule entry for 10 in the evening
|
||||||
[22] = {
|
[22] = {
|
||||||
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
[1] = {task = npc.actions.cmd.WALK_TO_POS, args = {
|
||||||
end_pos = {place_type=npc.places.PLACE_TYPE.BED.PRIMARY, use_access_node=true},
|
end_pos = {place_type=npc.places.PLACE_TYPE.BED.PRIMARY, use_access_node=true},
|
||||||
walkable = {}
|
walkable = {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Use bed
|
-- Use bed
|
||||||
[2] = {task = npc.actions.cmd.USE_BED, args = {
|
[2] = {task = npc.actions.cmd.USE_BED, args = {
|
||||||
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
|
pos = npc.places.PLACE_TYPE.BED.PRIMARY,
|
||||||
action = npc.actions.const.beds.LAY
|
action = npc.actions.const.beds.LAY
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
-- Stay put on bed
|
-- Stay put on bed
|
||||||
[3] = {action = npc.actions.cmd.FREEZE, args = {freeze = true}}
|
[3] = {action = npc.actions.cmd.FREEZE, args = {freeze = true}}
|
||||||
@ -249,7 +255,7 @@ end
|
|||||||
-- of occupation names (strings)
|
-- of occupation names (strings)
|
||||||
function npc.occupations.get_for_building(building_type, surrounding_building_types)
|
function npc.occupations.get_for_building(building_type, surrounding_building_types)
|
||||||
local result = {}
|
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
|
-- Check for empty or nil building types, in that case, any building
|
||||||
if def.building_types == nil or def.building_types == {} then
|
if def.building_types == nil or def.building_types == {} then
|
||||||
-- Empty building types, add to result
|
-- 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
|
if npc.utils.array_contains(def.building_types, building_type) then
|
||||||
-- Check for empty or nil surrounding building types
|
-- Check for empty or nil surrounding building types
|
||||||
if def.surrounding_building_types == nil
|
if def.surrounding_building_types == nil
|
||||||
or def.surrounding_building_types == {} then
|
or def.surrounding_building_types == {} then
|
||||||
-- Add this occupation
|
-- Add this occupation
|
||||||
table.insert(result, name)
|
table.insert(result, name)
|
||||||
else
|
else
|
||||||
-- Check if surround type is included in the def
|
-- Check if surround type is included in the def
|
||||||
if npc.utils.array_is_subset_of_array(def.surrounding_building_types,
|
if npc.utils.array_is_subset_of_array(
|
||||||
surrounding_building_types) then
|
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
|
-- Add this occupation
|
||||||
table.insert(result, name)
|
table.insert(result, name)
|
||||||
end
|
end
|
||||||
@ -294,16 +305,20 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
|
|||||||
minetest.log("Texture entries: "..dump(table.getn(def.textures)))
|
minetest.log("Texture entries: "..dump(table.getn(def.textures)))
|
||||||
if def.textures and table.getn(def.textures) > 0 then
|
if def.textures and table.getn(def.textures) > 0 then
|
||||||
self.selected_texture =
|
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
|
-- 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 an array was returned, select a random texture from it
|
||||||
if self.selected_texture then
|
if type(self.selected_texture) == "table" then
|
||||||
-- Set texture and base texture
|
local selected_texture = self.selected_texture[math.random(1, #self.selected_texture)]
|
||||||
self.textures = self.selected_texture
|
self.selected_texture = selected_texture
|
||||||
self.base_texture = self.selected_texture
|
|
||||||
-- Refresh entity
|
|
||||||
self.object:set_properties(self)
|
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- Initialize inventory
|
-- Initialize inventory
|
||||||
@ -321,16 +336,17 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Initialize dialogues
|
-- Initialize dialogues
|
||||||
if def.dialogues and table.getn(def.dialogues) > 0 then
|
if def.dialogues then
|
||||||
local dialogue_keys = {}
|
local dialogue_keys = {}
|
||||||
-- Check which type of dialogues we have
|
-- Check which type of dialogues we have
|
||||||
if def.dialogues.type == "given" then
|
if def.dialogues.type == "given" then
|
||||||
-- We have been given the dialogues, so def.dialogues.data contains
|
-- We have been given the dialogues, so def.dialogues.data contains
|
||||||
-- an array of dialogues
|
-- 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"
|
-- Add to the dialogue tags the "occupation name"
|
||||||
table.insert(dialogue.tags, occupation_name)
|
table.insert(dialogue.tags, occupation_name)
|
||||||
-- Register dialogue
|
-- Register dialogue
|
||||||
|
npc.log("INFO", "Registering dialogue for occupation "..dump(occupation_name)..": "..dump(dialogue))
|
||||||
local key = npc.dialogue.register_dialogue(dialogue)
|
local key = npc.dialogue.register_dialogue(dialogue)
|
||||||
-- Add key to set of dialogue keys
|
-- Add key to set of dialogue keys
|
||||||
table.insert(dialogue_keys, key)
|
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
|
-- an array of dialogues and def.dialogues.tags contains an array of
|
||||||
-- tags that we will use to search
|
-- tags that we will use to search
|
||||||
-- Register dialogues
|
-- 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"
|
-- Add to the dialogue tags the "occupation name"
|
||||||
table.insert(dialogue.tags, occupation_name)
|
table.insert(dialogue.tags, occupation_name)
|
||||||
-- Register dialogue
|
-- Register dialogue
|
||||||
@ -351,7 +367,7 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
|
|||||||
-- Find dialogues using tags
|
-- Find dialogues using tags
|
||||||
local dialogues = npc.search_dialogue_by_tags(def.dialogues.tags, true)
|
local dialogues = npc.search_dialogue_by_tags(def.dialogues.tags, true)
|
||||||
-- Add keys to set of dialogue keys
|
-- 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)
|
table.insert(dialogue_keys, key)
|
||||||
end
|
end
|
||||||
elseif def.dialogues.type == "tags" then
|
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
|
max_dialogue_count = def.dialogues.max_count
|
||||||
end
|
end
|
||||||
-- Add dialogues to the normal dialogues for NPC
|
-- Add dialogues to the normal dialogues for NPC
|
||||||
self.dialogues.normal = {}
|
self.dialogues.normal = dialogue_keys
|
||||||
for i = 1, max_dialogue_count do
|
-- for i = 1, max_dialogue_count do
|
||||||
self.dialogues.normal[i] = dialogue_keys[i]
|
-- self.dialogues.normal[i] = dialogue_keys[i]
|
||||||
end
|
-- end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Initialize trader status
|
||||||
|
if def.initial_trader_status then
|
||||||
|
self.trader_data.trader_status = def.initial_trader_status
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Initialize schedule entries
|
-- Initialize schedule entries
|
||||||
if def.schedules_entries and table.getn(npc.utils.get_map_keys(def.schedules_entries)) > 0 then
|
if def.schedules_entries and table.getn(npc.utils.get_map_keys(def.schedules_entries)) > 0 then
|
||||||
-- Create schedule in NPC
|
-- Create schedule in NPC
|
||||||
npc.create_schedule(self, npc.schedule_types.generic, 0)
|
npc.create_schedule(self, npc.schedule_types.generic, 0)
|
||||||
-- Traverse schedules
|
-- Traverse schedules
|
||||||
for time, entries in pairs(def.schedules_entries) do
|
for time, entries in pairs(def.schedules_entries) do
|
||||||
-- Add schedule entry for each time
|
-- Add schedule entry for each time
|
||||||
|
556
spawner.lua
556
spawner.lua
@ -61,14 +61,14 @@ npc.spawner.replacement_interval = 60
|
|||||||
npc.spawner.spawn_delay = 10
|
npc.spawner.spawn_delay = 10
|
||||||
|
|
||||||
npc.spawner.spawn_data = {
|
npc.spawner.spawn_data = {
|
||||||
status = {
|
status = {
|
||||||
dead = 0,
|
dead = 0,
|
||||||
alive = 1
|
alive = 1
|
||||||
},
|
},
|
||||||
age = {
|
age = {
|
||||||
adult = "adult",
|
adult = "adult",
|
||||||
child = "child"
|
child = "child"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
-- Array of nodes that serve as plotmarker of a plot, and therefore
|
-- 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
|
||||||
-- Initialize NPC stats
|
-- Initialize NPC stats
|
||||||
local npc_stats = {
|
local npc_stats = {
|
||||||
male = {
|
male = {
|
||||||
total = 0,
|
total = 0,
|
||||||
adult = 0,
|
adult = 0,
|
||||||
child = 0
|
child = 0
|
||||||
},
|
},
|
||||||
female = {
|
female = {
|
||||||
total = 0,
|
total = 0,
|
||||||
adult = 0,
|
adult = 0,
|
||||||
child = 0
|
child = 0
|
||||||
},
|
},
|
||||||
adult_total = 0,
|
adult_total = 0,
|
||||||
child_total = 0
|
child_total = 0
|
||||||
}
|
}
|
||||||
result.building_npc_stats = npc_stats
|
result.building_npc_stats = npc_stats
|
||||||
|
|
||||||
@ -180,6 +180,78 @@ end
|
|||||||
-- Spawning functions
|
-- 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
|
-- This function is called when the node timer for spawning NPC
|
||||||
-- is expired. Can be called manually by supplying either:
|
-- is expired. Can be called manually by supplying either:
|
||||||
-- - Position of mg_villages plotmarker, or,
|
-- - 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["npc_count"] = meta:get_int("npc_count")
|
||||||
area_info["spawned_npc_count"] = meta:get_int("spawned_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 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, occupation_data.name)
|
||||||
local metadata = npc.spawner.spawn_npc(pos, area_info)
|
|
||||||
|
|
||||||
-- Set all metadata back into the node
|
-- Set all metadata back into the node
|
||||||
-- Increase NPC spawned count
|
-- 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
|
if npc_table and #npc_table > 0 then
|
||||||
npc.initialize(ent, pos, false, npc_stats, occupation)
|
npc.initialize(ent, pos, false, npc_stats, occupation)
|
||||||
else
|
else
|
||||||
npc.initialize(ent, pos, nil, nil, occupation)
|
npc.initialize(ent, pos, nil, nil, occupation)
|
||||||
end
|
end
|
||||||
-- If entrance and node_data are present, assign nodes
|
-- If entrance and node_data are present, assign nodes
|
||||||
if entrance and node_data then
|
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
|
if ent:get_luaentity().child then
|
||||||
age = npc.age.child
|
age = npc.age.child
|
||||||
end
|
end
|
||||||
-- TODO: Add more information here at some time...
|
|
||||||
local entry = {
|
local entry = {
|
||||||
status = npc.spawner.spawn_data.status.alive,
|
status = npc.spawner.spawn_data.status.alive,
|
||||||
name = ent:get_luaentity().name,
|
name = ent:get_luaentity().name,
|
||||||
id = ent:get_luaentity().npc_id,
|
id = ent:get_luaentity().npc_id,
|
||||||
sex = ent:get_luaentity().sex,
|
sex = ent:get_luaentity().sex,
|
||||||
age = age,
|
age = age,
|
||||||
|
occupation = occupation,
|
||||||
born_day = minetest.get_day_count()
|
born_day = minetest.get_day_count()
|
||||||
}
|
}
|
||||||
minetest.log("Area info: "..dump(area_info))
|
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
|
-- Update and store stats
|
||||||
-- Increase total of NPCs for specific sex
|
-- Increase total of NPCs for specific sex
|
||||||
npc_stats[ent:get_luaentity().sex].total =
|
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
|
-- Increase total number of NPCs by age
|
||||||
npc_stats[age.."_total"] = npc_stats[age.."_total"] + 1
|
npc_stats[age.."_total"] = npc_stats[age.."_total"] + 1
|
||||||
-- Increase number of NPCs by age and sex
|
-- Increase number of NPCs by age and sex
|
||||||
npc_stats[ent:get_luaentity().sex][age] =
|
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
|
area_info.npc_stats = npc_stats
|
||||||
-- Return
|
-- Return
|
||||||
npc.log("INFO", "Spawning successful!")
|
npc.log("INFO", "Spawning successful!")
|
||||||
@ -337,7 +413,7 @@ function npc.spawner.assign_places(self, entrance, node_data, pos)
|
|||||||
-- Assign plotmarker if position given
|
-- Assign plotmarker if position given
|
||||||
if pos then
|
if pos then
|
||||||
npc.places.add_shared(self, npc.places.PLACE_TYPE.OTHER.HOME_PLOTMARKER,
|
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
|
end
|
||||||
|
|
||||||
-- Assign entrance door and related locations
|
-- Assign entrance door and related locations
|
||||||
@ -353,11 +429,11 @@ function npc.spawner.assign_places(self, entrance, node_data, 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_owned_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))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Assign sits
|
-- 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
|
-- Add all furnaces to places as shared since NPC should be able to use
|
||||||
-- any accessible furnace
|
-- any accessible furnace
|
||||||
npc.places.add_shared_accessible_place(self, node_data.furnace_type,
|
npc.places.add_shared_accessible_place(self, node_data.furnace_type,
|
||||||
npc.places.PLACE_TYPE.FURNACE.SHARED)
|
npc.places.PLACE_TYPE.FURNACE.SHARED)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Assign storage nodes
|
-- 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))
|
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 is available let NPC
|
||||||
if entrance then
|
if entrance then
|
||||||
npc.add_task(self,
|
npc.add_task(self,
|
||||||
npc.actions.cmd.WALK_TO_POS,
|
npc.actions.cmd.WALK_TO_POS,
|
||||||
{end_pos=npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
{end_pos=npc.places.PLACE_TYPE.OTHER.HOME_INSIDE,
|
||||||
walkable={}})
|
walkable={}})
|
||||||
npc.add_action(self, npc.actions.cmd.FREEZE, {freeze = false})
|
npc.add_action(self, npc.actions.cmd.FREEZE, {freeze = false})
|
||||||
end
|
end
|
||||||
|
|
||||||
return node_data
|
return node_data
|
||||||
@ -438,7 +514,7 @@ function npc.spawner.calculate_npc_spawning_on_plotmarker(pos)
|
|||||||
-- Check node metadata
|
-- Check node metadata
|
||||||
local meta = minetest.get_meta(pos)
|
local meta = minetest.get_meta(pos)
|
||||||
if meta:get_string("replaced") ~= "true" then
|
if meta:get_string("replaced") ~= "true" then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- Get nodes for this building
|
-- Get nodes for this building
|
||||||
local node_data = minetest.deserialize(meta:get_string("node_data"))
|
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("Building data: "..dump(building_data))
|
||||||
--minetest.log("--------------------------------------------")
|
--minetest.log("--------------------------------------------")
|
||||||
-- Get area of the building
|
-- Get area of the building
|
||||||
local x_size = building_data.bsizex
|
local x_size = building_data.bsizex
|
||||||
local y_size = building_data.ysize
|
local y_size = building_data.ysize
|
||||||
local z_size = building_data.bsizez
|
local z_size = building_data.bsizez
|
||||||
local brotate = building_data.brotate
|
local brotate = building_data.brotate
|
||||||
local start_pos = {x=pos.x, y=pos.y, z=pos.z}
|
local start_pos = {x=pos.x, y=pos.y, z=pos.z}
|
||||||
local x_sign, z_sign = 1, 1
|
local x_sign, z_sign = 1, 1
|
||||||
|
|
||||||
-- Check plot direction
|
-- Check plot direction
|
||||||
-- NOTE: Below values may be wrong, very wrong!
|
-- NOTE: Below values may be wrong, very wrong!
|
||||||
-- 0 - facing West, -X
|
-- 0 - facing West, -X
|
||||||
-- 1 - facing North, +Z
|
-- 1 - facing North, +Z
|
||||||
-- 2 - facing East, +X
|
-- 2 - facing East, +X
|
||||||
-- 3 - facing South -Z
|
-- 3 - facing South -Z
|
||||||
if brotate == 0 then
|
if brotate == 0 then
|
||||||
x_sign, z_sign = 1, -1
|
x_sign, z_sign = 1, -1
|
||||||
elseif brotate == 1 then
|
elseif brotate == 1 then
|
||||||
x_sign, z_sign = -1, -1
|
x_sign, z_sign = -1, -1
|
||||||
local temp = z_size
|
local temp = z_size
|
||||||
z_size = x_size
|
z_size = x_size
|
||||||
x_size = temp
|
x_size = temp
|
||||||
elseif brotate == 2 then
|
elseif brotate == 2 then
|
||||||
x_sign, z_sign = -1, 1
|
x_sign, z_sign = -1, 1
|
||||||
elseif brotate == 3 then
|
elseif brotate == 3 then
|
||||||
x_sign, z_sign = 1, 1
|
x_sign, z_sign = 1, 1
|
||||||
end
|
end
|
||||||
|
|
||||||
------------------------
|
------------------------
|
||||||
-- For debug:
|
-- For debug:
|
||||||
------------------------
|
------------------------
|
||||||
-- Red is x marker
|
-- Red is x marker
|
||||||
--minetest.set_node({x=pos.x + (x_sign * x_size),y=pos.y,z=pos.z}, {name = "wool:red"})
|
--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))
|
--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
|
-- Blue is z marker
|
||||||
--minetest.set_node({x=pos.x,y=pos.y,z=pos.z + (z_sign * z_size)}, {name = "wool:blue"})
|
--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))
|
--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", "Start pos: "..minetest.pos_to_string(start_pos))
|
||||||
npc.log("DEBUG", "Plot: "..dump(minetest.get_meta(start_pos):get_string("infotext")))
|
npc.log("DEBUG", "Plot: "..dump(minetest.get_meta(start_pos):get_string("infotext")))
|
||||||
|
|
||||||
npc.log("DEBUG", "Brotate: "..dump(brotate))
|
npc.log("DEBUG", "Brotate: "..dump(brotate))
|
||||||
npc.log("DEBUG", "X_sign: "..dump(x_sign))
|
npc.log("DEBUG", "X_sign: "..dump(x_sign))
|
||||||
npc.log("DEBUG", "X_adj: "..dump(x_sign*x_size))
|
npc.log("DEBUG", "X_adj: "..dump(x_sign*x_size))
|
||||||
npc.log("DEBUG", "Z_sign: "..dump(z_sign))
|
npc.log("DEBUG", "Z_sign: "..dump(z_sign))
|
||||||
npc.log("DEBUG", "Z_adj: "..dump(z_sign*z_size))
|
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:
|
-- For debug:
|
||||||
--minetest.set_node(start_pos, {name="default:mese_block"})
|
--minetest.set_node(start_pos, {name="default:mese_block"})
|
||||||
--minetest.set_node(end_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"))
|
--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
|
end
|
||||||
|
|
||||||
-- This function "adapts" an existent mg_villages:plotmarker for NPC spawning.
|
-- 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")
|
local infotext = meta:get_string("infotext")
|
||||||
-- Check for nil values above
|
-- Check for nil values above
|
||||||
if (not village_id or (village_id and village_id == ""))
|
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
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -612,8 +688,8 @@ if minetest.get_modpath("mg_villages") ~= nil then
|
|||||||
-- Store nodedata into the spawner's metadata
|
-- Store nodedata into the spawner's metadata
|
||||||
meta:set_string("node_data", minetest.serialize(nodedata))
|
meta:set_string("node_data", minetest.serialize(nodedata))
|
||||||
-- Find nearby plotmarkers, excluding current plotmarker
|
-- Find nearby plotmarkers, excluding current plotmarker
|
||||||
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 40, true)
|
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 35, true)
|
||||||
minetest.log("Found nearby plotmarkers: "..dump(nearby_plotmarkers))
|
--minetest.log("Found nearby plotmarkers: "..dump(nearby_plotmarkers))
|
||||||
meta:set_string("nearby_plotmarkers", minetest.serialize(nearby_plotmarkers))
|
meta:set_string("nearby_plotmarkers", minetest.serialize(nearby_plotmarkers))
|
||||||
-- Check if building position data is also available (recent mg_villages)
|
-- Check if building position data is also available (recent mg_villages)
|
||||||
if building_pos_data then
|
if building_pos_data then
|
||||||
@ -624,22 +700,22 @@ if minetest.get_modpath("mg_villages") ~= nil then
|
|||||||
meta:set_string("npcs", minetest.serialize(npcs))
|
meta:set_string("npcs", minetest.serialize(npcs))
|
||||||
-- Initialize NPC stats
|
-- Initialize NPC stats
|
||||||
local npc_stats = {
|
local npc_stats = {
|
||||||
male = {
|
male = {
|
||||||
total = 0,
|
total = 0,
|
||||||
adult = 0,
|
adult = 0,
|
||||||
child = 0
|
child = 0
|
||||||
},
|
},
|
||||||
female = {
|
female = {
|
||||||
total = 0,
|
total = 0,
|
||||||
adult = 0,
|
adult = 0,
|
||||||
child = 0
|
child = 0
|
||||||
},
|
},
|
||||||
adult_total = 0,
|
adult_total = 0,
|
||||||
child_total = 0
|
child_total = 0
|
||||||
}
|
}
|
||||||
meta:set_string("npc_stats", minetest.serialize(npc_stats))
|
meta:set_string("npc_stats", minetest.serialize(npc_stats))
|
||||||
-- Set replaced
|
-- Set replaced
|
||||||
meta:set_string("replaced", "true")
|
meta:set_string("replaced", "true")
|
||||||
-- Calculate how many NPCs will spawn
|
-- Calculate how many NPCs will spawn
|
||||||
npc.spawner.calculate_npc_spawning_on_plotmarker(pos)
|
npc.spawner.calculate_npc_spawning_on_plotmarker(pos)
|
||||||
-- Stop searching for building type
|
-- Stop searching for building type
|
||||||
@ -648,107 +724,107 @@ if minetest.get_modpath("mg_villages") ~= nil then
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Node registration
|
-- Node registration
|
||||||
-- This node is currently a slightly modified mg_villages:plotmarker
|
-- This node is currently a slightly modified mg_villages:plotmarker
|
||||||
-- TODO: Change formspec to a more detailed one.
|
-- TODO: Change formspec to a more detailed one.
|
||||||
minetest.override_item("mg_villages:plotmarker", {
|
minetest.override_item("mg_villages:plotmarker", {
|
||||||
-- description = "Automatic NPC Spawner",
|
-- description = "Automatic NPC Spawner",
|
||||||
-- drawtype = "nodebox",
|
-- drawtype = "nodebox",
|
||||||
-- tiles = {"default_stone.png"},
|
-- tiles = {"default_stone.png"},
|
||||||
-- paramtype = "light",
|
-- paramtype = "light",
|
||||||
-- paramtype2 = "facedir",
|
-- paramtype2 = "facedir",
|
||||||
-- node_box = {
|
-- node_box = {
|
||||||
-- type = "fixed",
|
-- type = "fixed",
|
||||||
-- 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+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},
|
-- --{-0.5+0/16, -0.5, -0.5+0/16, 0.5-0/16, -0.5+0/16, 0.5-0/16},
|
||||||
-- }
|
-- }
|
||||||
-- },
|
-- },
|
||||||
walkable = false,
|
walkable = false,
|
||||||
groups = {cracky=3,stone=2},
|
groups = {cracky=3,stone=2},
|
||||||
|
|
||||||
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
|
||||||
-- NOTE: This is temporary code for testing...
|
-- NOTE: This is temporary code for testing...
|
||||||
local nodedata = minetest.deserialize(minetest.get_meta(pos):get_string("node_data"))
|
local nodedata = minetest.deserialize(minetest.get_meta(pos):get_string("node_data"))
|
||||||
--minetest.log("Node data: "..dump(nodedata))
|
--minetest.log("Node data: "..dump(nodedata))
|
||||||
--minetest.log("Entrance: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("entrance"))))
|
--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)))
|
--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)
|
--local entrance = npc.places.find_entrance_from_openable_nodes(nodedata.openable_type, pos)
|
||||||
--minetest.log("Found entrance: "..dump(entrance))
|
--minetest.log("Found entrance: "..dump(entrance))
|
||||||
minetest.log("Replaced: "..dump(minetest.get_meta(pos):get_string("replaced")))
|
minetest.log("Replaced: "..dump(minetest.get_meta(pos):get_string("replaced")))
|
||||||
-- for i = 1, #nodedata.bed_type do
|
-- for i = 1, #nodedata.bed_type do
|
||||||
-- nodedata.bed_type[i].owner = ""
|
-- nodedata.bed_type[i].owner = ""
|
||||||
-- end
|
-- end
|
||||||
-- minetest.get_meta(pos):set_string("node_data", minetest.serialize(nodedata))
|
-- minetest.get_meta(pos):set_string("node_data", minetest.serialize(nodedata))
|
||||||
-- minetest.log("Cleared bed owners")
|
-- minetest.log("Cleared bed owners")
|
||||||
--minetest.log("NPC stats: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("npc_stats"))))
|
--minetest.log("NPC stats: "..dump(minetest.deserialize(minetest.get_meta(pos):get_string("npc_stats"))))
|
||||||
|
|
||||||
return mg_villages.plotmarker_formspec( pos, nil, {}, clicker )
|
return mg_villages.plotmarker_formspec( pos, nil, {}, clicker )
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- on_receive_fields = function(pos, formname, fields, sender)
|
-- on_receive_fields = function(pos, formname, fields, sender)
|
||||||
-- return mg_villages.plotmarker_formspec( pos, formname, fields, sender );
|
-- return mg_villages.plotmarker_formspec( pos, formname, fields, sender );
|
||||||
-- end,
|
-- end,
|
||||||
|
|
||||||
on_timer = function(pos, elapsed)
|
on_timer = function(pos, elapsed)
|
||||||
npc.spawner.spawn_npc_on_plotmarker(pos)
|
npc.spawner.spawn_npc_on_plotmarker(pos)
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- protect against digging
|
-- protect against digging
|
||||||
-- can_dig = function(pos, player)
|
-- can_dig = function(pos, player)
|
||||||
-- local meta = minetest.get_meta(pos);
|
-- 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
|
-- if (meta and meta:get_string("village_id") ~= "" and meta:get_int("plot_nr") and meta:get_int("plot_nr") > 0 ) then
|
||||||
-- return false;
|
-- 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
|
-- end
|
||||||
-- return true;
|
-- })
|
||||||
-- end
|
|
||||||
})
|
|
||||||
|
|
||||||
-- LBM Registration
|
-- ABM Registration
|
||||||
-- Used to modify plotmarkers and replace them with advanced_npc:plotmarker_auto_spawner
|
minetest.register_abm({
|
||||||
-- minetest.register_lbm({
|
label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
|
||||||
-- label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
|
nodenames = {"mg_villages:plotmarker"},
|
||||||
-- name = "advanced_npc:mg_villages_plotmarker_replacer",
|
interval = 10,--npc.spawner.replacement_interval,
|
||||||
-- nodenames = {"mg_villages:plotmarker"},
|
chance = 1,--5,
|
||||||
-- run_at_every_load = false,
|
catch_up = true,
|
||||||
-- action = function(pos, node)
|
action = function(pos, node, active_object_count, active_object_count_wider)
|
||||||
-- -- Check if replacement is activated
|
-- Check if replacement is needed
|
||||||
-- if npc.spawner.replace_activated then
|
local meta = minetest.get_meta(pos)
|
||||||
-- -- Replace mg_villages:plotmarker
|
if meta then
|
||||||
-- spawner.replace_mg_villages_plotmarker(pos)
|
-- minetest.log("------ Plotmarker metadata -------")
|
||||||
-- -- Set NPCs to spawn
|
-- local plot_nr = meta:get_int("plot_nr")
|
||||||
-- spawner.calculate_npc_spawning(pos)
|
-- local village_id = meta:get_string("village_id")
|
||||||
-- end
|
-- minetest.log("Plot nr: "..dump(plot_nr)..", village ID: "..dump(village_id))
|
||||||
-- end
|
-- 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
|
||||||
-- ABM Registration
|
return
|
||||||
minetest.register_abm({
|
end
|
||||||
label = "Replace mg_villages:plotmarker with Advanced NPC auto spawners",
|
-- Check if replacement is activated
|
||||||
nodenames = {"mg_villages:plotmarker"},
|
if npc.spawner.replace_activated then
|
||||||
interval = 10,--npc.spawner.replacement_interval,
|
-- Replace mg_villages:plotmarker
|
||||||
chance = 1,--5,
|
spawner.adapt_mg_villages_plotmarker(pos)
|
||||||
catch_up = true,
|
end
|
||||||
action = function(pos, node, active_object_count, active_object_count_wider)
|
end
|
||||||
-- 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
|
end
|
||||||
|
|
||||||
@ -756,50 +832,50 @@ end
|
|||||||
|
|
||||||
-- Chat commands to manage spawners
|
-- Chat commands to manage spawners
|
||||||
minetest.register_chatcommand("restore_plotmarkers", {
|
minetest.register_chatcommand("restore_plotmarkers", {
|
||||||
description = "Replaces all advanced_npc:plotmarker_auto_spawner with mg_villages:plotmarker in the specified radius.",
|
description = "Replaces all advanced_npc:plotmarker_auto_spawner with mg_villages:plotmarker in the specified radius.",
|
||||||
privs = {server=true},
|
privs = {server=true},
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
-- Check if radius is null
|
-- Check if radius is null
|
||||||
if param == nil and type(param) ~= "number" then
|
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")
|
minetest.chat_send_player(name, "Need to enter a radius as an integer number. Ex. /restore_plotmarkers 10 for a radius of 10")
|
||||||
return
|
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
|
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
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user