From 59bb430e6223b73bf352a8bb61dab9a07bd98e88 Mon Sep 17 00:00:00 2001 From: Hector Franqui Date: Mon, 28 Aug 2017 15:43:57 -0400 Subject: [PATCH] Spawner: Nearby plotmarkers are now scanned for all mg_villages:plotmarkers. Places: Usable nodes now include workplaces Small bug fixes. Relationships: Small bugfix with gift item response. Occupations: Add work node descriptions. Experimental "priest" occupation. --- actions/places.lua | 72 +++++++++++++++++++++++++++++++++++-- data/occupations_data.lua | 23 ++++++++++++ occupations/occupations.lua | 4 +++ relationships.lua | 4 +-- spawner.lua | 27 +++++++++----- 5 files changed, 117 insertions(+), 13 deletions(-) diff --git a/actions/places.lua b/actions/places.lua index 3ca35f2..331b6d8 100644 --- a/actions/places.lua +++ b/actions/places.lua @@ -268,7 +268,57 @@ if minetest.get_modpath("mg_villages") ~= nil then result.building_type = result.building_data.typ end return result - end + end + + -- Pre-requisite: only run this function on mg_villages:plotmarker that has been adapted + -- by using spawner.adapt_mg_villages_plotmarker + function npc.places.get_all_workplaces_from_plotmarker(pos) + local result = {} + local meta = minetest.get_meta(pos) + local pos_data = minetest.deserialize(meta:get_string("building_pos_data")) + if pos_data then + local workplaces = pos_data.workplaces + if workplaces then + -- Insert all workplaces in this plotmarker + for i = 1, #workplaces do + table.insert(result, + { + workplace=workplaces[i], + building_type=meta:get_string("building_type"), + surrounding_workplace = false, + node_pos= { + x=workplaces[i].x, + y=workplaces[i].y, + z=workplaces[i].z + } + }) + end + end + end + -- Check the other plotmarkers as well + local nearby_plotmarkers = minetest.deserialize(meta:get_string("nearby_plotmarkers")) + if nearby_plotmarkers then + for i = 1, #nearby_plotmarkers do + 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])) + table.insert(result, { + workplace=nearby_plotmarkers[i].workplaces[j], + building_type = nearby_plotmarkers[i].building_type, + surrounding_workplace = true, + node_pos = { + x=nearby_plotmarkers[i].workplaces[j].x, + y=nearby_plotmarkers[i].workplaces[j].y, + z=nearby_plotmarkers[i].workplaces[j].z + } + }) + end + end + end + end + return result + end end -- This function will search for nodes of type plotmarker and, @@ -303,8 +353,11 @@ function npc.places.find_plotmarkers(pos, radius, exclude_current_pos) def["building_type"] = data.building_type if data.building_pos_data then def["building_pos_data"] = data.building_pos_data + def["workplaces"] = data.building_pos_data.workplaces end - end + end + -- Add building + minetest.log("Adding building: "..dump(def)) table.insert(result, def) end end @@ -320,7 +373,8 @@ function npc.places.scan_area_for_usable_nodes(pos1, pos2) sittable_type = {}, furnace_type = {}, storage_type = {}, - openable_type = {} + openable_type = {}, + workplace_type = {} } local start_pos, end_pos = vector.sort(pos1, pos2) @@ -330,6 +384,18 @@ function npc.places.scan_area_for_usable_nodes(pos1, pos2) result.storage_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.STORAGE_TYPE) result.openable_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.OPENABLE_TYPE) + -- Find workplace nodes: if mg_villages:plotmarker is given a start pos, take it from there. + -- If not, search for them. + local node = minetest.get_node(pos1) + if node.name == "mg_villages:plotmarker" then + if npc.places.get_all_workplaces_from_plotmarker then + result.workplace_type = npc.places.get_all_workplaces_from_plotmarker(pos1) + end + else + -- Just search for workplace nodes + result.workplace_type = npc.places.get_nodes_by_type(start_pos, end_pos, npc.places.nodes.WORKPLACE_TYPE) + end + return result end diff --git a/data/occupations_data.lua b/data/occupations_data.lua index b694d87..778200d 100644 --- a/data/occupations_data.lua +++ b/data/occupations_data.lua @@ -3,6 +3,29 @@ -- Register default occupation npc.occupations.register_occupation("default_basic", npc.occupations.basic_def) +-- Test priest +npc.occupations.register_occupation("test_priest", { + dialogues = { + { + text = "How are you today my child?", + tags = "male" + } + }, + textures = { + "npc_male_priest.png" + }, + initial_inventory = { + "farming:bread 1" + }, + building_types = { + "home" + }, + surrounding_building_types = { + "church" + }, + schedule_entries = {} +}) + -- Test farmer npc.occupations.register_occupation("test_farmer", { dialogues = {}, diff --git a/occupations/occupations.lua b/occupations/occupations.lua index 2a27b7f..fa344fc 100644 --- a/occupations/occupations.lua +++ b/occupations/occupations.lua @@ -58,6 +58,10 @@ -- -- spawns on house, then it has to be because there is a field -- -- nearby. If left empty or nil, surrounding buildings doesn't -- -- matter +-- workplace_nodes = {}, +-- -- An array of string where each string is a node the NPC +-- -- works with. These are useful for assigning workplaces and work +-- -- work nodes. -- initial_inventory = {}, -- -- An array of entries like the following: -- -- {name="", count=1} -- or diff --git a/relationships.lua b/relationships.lua index 08a24e3..91cfdbe 100644 --- a/relationships.lua +++ b/relationships.lua @@ -482,7 +482,7 @@ local function show_receive_gift_reaction(self, item_name, modifier, clicker_nam npc.relationships.GIFT_ITEM_LIKED, self.sex, phase) - npc.chat(self.npc_name, clicker_name, message_to_send) + npc.chat(self.npc_name, clicker_name, message_to_send.responses[1]) -- Disliked items reactions elseif modifier < 0 then effect({x = pos.x, y = pos.y + 1, z = pos.z}, 8, "default_item_smoke.png") @@ -492,7 +492,7 @@ local function show_receive_gift_reaction(self, item_name, modifier, clicker_nam npc.relationships.GIFT_ITEM_RESPONSE, npc.relationships.GIFT_ITEM_DISLIKED, self.sex) - npc.chat(self.npc_name, clicker_name, message_to_send) + npc.chat(self.npc_name, clicker_name, message_to_send.responses[1]) end end diff --git a/spawner.lua b/spawner.lua index cc5f05c..2405d31 100644 --- a/spawner.lua +++ b/spawner.lua @@ -204,18 +204,21 @@ 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") - -- TODO: Get occupation name + area_info["building_type"] = minetest.deserialize(meta:get_string("building_type")) + local nearby_plotmarkers = minetest.deserialize(meta:get_string("nearby_plotmarkers")) + + local metadata = npc.spawner.spawn_npc(pos, area_info) -- Set all metadata back into the node -- Increase NPC spawned count - area_info.spawned_npc_count = area_info.spawned_npc_count + 1 + area_info.spawned_npc_count = metadata.spawned_npc_count + 1 -- Store count into node meta:set_int("spawned_npc_count", area_info.spawned_npc_count) -- Store spawned NPC info - meta:set_string("npcs", minetest.serialize(area_info.npcs)) + meta:set_string("npcs", minetest.serialize(metadata.npcs)) -- Store NPC stats - meta:set_string("npc_stats", minetest.serialize(area_info.npc_stats)) + meta:set_string("npc_stats", minetest.serialize(metadata.npc_stats)) -- Check if there are more NPCs to spawn if area_info.spawned_npc_count >= area_info.npc_count then @@ -304,7 +307,7 @@ function npc.spawner.spawn_npc(pos, area_info, occupation_name) area_info.npc_stats = npc_stats -- Return npc.log("INFO", "Spawning successful!") - return true + return area_info else npc.log("ERROR", "Spawning failed!") ent:remove() @@ -400,11 +403,18 @@ function npc.spawner.assign_places(self, entrance, node_data, pos) --meta:set_string("node_data", minetest.serialize(node_data)) end -- Add all storage-types to places as shared since NPC should be able - -- to use other storage nodes as well. + -- to use other storaage nodes as well. npc.places.add_shared_accessible_place(self, node_data.storage_type, npc.places.PLACE_TYPE.STORAGE.SHARED) end + -- Assign workplace nodes + if #node_data.workplace_type > 0 then + local occupation = npc.occupations.registered_occupations[self.occupation_name] + + + end + npc.log("DEBUG", "Places for NPC "..self.npc_name..": "..dump(self.places_map)) -- Make NPC go into their house @@ -564,7 +574,8 @@ if minetest.get_modpath("mg_villages") ~= nil then local all_data = npc.places.get_mg_villages_building_data(pos) local building_data = all_data.building_data local building_type = all_data.building_type - local building_pos_data = all_data.building_pos_data + local building_pos_data = all_data.building_pos_dataS + --minetest.log("Found building data: "..dump(building_data)) -- Check if the building is of the support types @@ -601,7 +612,7 @@ 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, 20, true) + local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 40, 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)