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 = {
type = "given",
data = {
{
text = "How are you today my child?",
tags = "male"
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"
@ -87,4 +91,4 @@ npc.occupations.register_occupation("test_farmer", {
}
}
}
})
})

40
npc.lua
View File

@ -120,6 +120,14 @@ local function is_female_texture(textures)
return false
end
function npc.assign_sex_from_texture(self)
if is_female_texture(self.base_texture) then
return npc.FEMALE
else
return npc.MALE
end
end
local function get_random_texture(sex, age)
local textures = {}
local filtered_textures = {}
@ -179,7 +187,9 @@ function npc.get_random_texture_from_array(age, sex, textures)
-- Check if there are no textures
if #filtered_textures == 0 then
return nil
-- Return whole array for re-evaluation
npc.log("DEBUG", "No textures found, returning original array")
return textures
end
return filtered_textures[math.random(1, #filtered_textures)]
@ -324,23 +334,10 @@ function npc.initialize(entity, pos, is_lua_entity, npc_stats, occupation_name)
-- Get sex based on texture. This is a 50% chance for
-- each sex as there's same amount of textures for male and female.
-- Do not spawn child as first NPC
if (is_female_texture(ent.base_texture)) then
ent.sex = npc.FEMALE
else
ent.sex = npc.MALE
end
ent.sex = npc.assign_sex_from_texture(ent)
ent.age = npc.age.adult
end
-- Nametag is initialized to blank
ent.nametag = ""
-- Set name
ent.npc_name = get_random_name(ent.sex)
-- Set ID
ent.npc_id = tostring(math.random(1000, 9999))..":"..ent.npc_name
-- Initialize all gift data
ent.gift_data = {
-- Choose favorite items. Choose phase1 per default
@ -474,12 +471,19 @@ function npc.initialize(entity, pos, is_lua_entity, npc_stats, occupation_name)
-- If occupation name given, override properties with
-- occupation values and initialize schedules
--minetest.log("Entity age: "..dump(ent.age)..", afult? "..dump(ent.age==npc.age.adult))
npc.log("INFO", "Overriding NPC values with occupation: "..dump(occupation_name))
if occupation_name and occupation_name ~= "" and ent.age == npc.age.adult then
npc.occupations.initialize_occupation_values(ent, occupation_name)
end
-- Nametag is initialized to blank
ent.nametag = ""
-- Set name
ent.npc_name = get_random_name(ent.sex)
-- Set ID
ent.npc_id = tostring(math.random(1000, 9999))..":"..ent.npc_name
-- TODO: Remove this - do inside occupation
-- Dedicated trade test
ent.trader_data.trade_list.both = {
@ -1029,7 +1033,7 @@ function npc.schedule_check(self)
-- Get actions related to node and enqueue them
for i = 1, #actions[node.name] do
local args = {}
local action = nil
local action
-- Calculate arguments for the following supported actions:
-- - Dig
-- - Place

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 = {
@ -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
@ -264,8 +270,13 @@ function npc.occupations.get_for_building(building_type, surrounding_building_ty
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
@ -296,15 +307,19 @@ function npc.occupations.initialize_occupation_values(self, occupation_name)
self.selected_texture =
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
-- 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
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
if def.initial_inventory and table.getn(def.initial_inventory) > 0 then
@ -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,10 +384,15 @@ 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]
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

View File

@ -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
@ -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))
@ -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