Finish implementation to find nearby plotmarkers.

This commit is contained in:
Hector Franqui 2017-08-27 14:46:48 -04:00
parent 19c5ca1d0c
commit 0ddb30c0f9
2 changed files with 102 additions and 65 deletions

View File

@ -242,10 +242,39 @@ function npc.places.get_nodes_by_type(start_pos, end_pos, type)
return result
end
-- Function to get mg_villages building data
if minetest.get_modpath("mg_villages") ~= nil then
function npc.places.get_mg_villages_building_data(pos)
local result = {
village_id = "",
plot_nr = -1,
building_data = {},
building_type = "",
}
local meta = minetest.get_meta(pos)
result.plot_nr = meta:get_int("plot_nr")
result.village_id = meta:get_string("village_id")
-- Get building data
if mg_villages.get_plot_and_building_data then
local all_data = mg_villages.get_plot_and_building_data(result.village_id, result.plot_nr)
result.building_data = all_data.building_data
result.building_type = result.building_data.typ
result["building_pos_data"] = all_data.bpos
else
-- Following line from mg_villages mod, protection.lua
local btype = mg_villages.all_villages[result.village_id].to_add_data.bpos[result.plot_nr].btype
result.building_data = mg_villages.BUILDINGS[btype]
result.building_type = result.building_data.typ
end
return result
end
end
-- This function will search for nodes of type plotmarker and,
-- in case of being an mg_villages plotmarker, it will fetch building
-- information and include in result.
function npc.places.find_plotmarkers(pos, radius)
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}
@ -253,21 +282,32 @@ function npc.places.find_plotmarkers(pos, radius)
npc.places.nodes.PLOTMARKER_TYPE)
-- Scan nodes
for i = 1, #nodes do
-- Check if current plotmarker is to be excluded from the list
local exclude = false
if exclude_current_pos then
if pos.x == nodes[i].x and pos.y == nodes[i].y and pos.z == nodes[i].z then
exclude = true
end
end
-- Analyze and include node if not excluded
if not exclude then
local node = minetest.get_node(nodes[i])
local def = {}
def["pos"] = nodes[i]
def["name"] = node.name
if node.name == "mg_villages:plotmarker" then
local meta = minetest.get_meta(nodes[i])
def["plot_nr"] = meta:get_int("plot_nr")
def["village_id"] = meta:get_string("village_id")
if def.plot_nr and def.village_id and mg_villages.get_plot_and_building_data then
def["building_data"] = mg_villages.get_plot_and_building_data( def.village_id, def.plot_nr )
if node.name == "mg_villages:plotmarker" and npc.places.get_mg_villages_building_data then
local data = npc.places.get_mg_villages_building_data(nodes[i])
def["plot_nr"] = data.plot_nr
def["village_id"] = data.village_id
def["building_data"] = data.building_data
def["building_type"] = data.building_type
if data.building_pos_data then
def["building_pos_data"] = data.building_pos_data
end
end
table.insert(result, def)
end
end
return result
end

View File

@ -560,20 +560,12 @@ if minetest.get_modpath("mg_villages") ~= nil then
or (not plot_nr or (plot_nr and plot_nr == 0)) then
return
end
-- Get building data
local building_data = {}
local building_type = ""
if mg_villages.get_plot_and_building_data then
building_data = mg_villages.get_plot_and_building_data(village_id, plot_nr)
building_type = building_data.building_data.typ
else
-- Following line from mg_villages mod, protection.lua
local btype = mg_villages.all_villages[village_id].to_add_data.bpos[plot_nr].btype
building_data = mg_villages.BUILDINGS[btype]
building_type = building_data.typ
end
minetest.log("Found building data: "..dump(building_data))
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
--minetest.log("Found building data: "..dump(building_data))
-- Check if the building is of the support types
for _,value in pairs(npc.spawner.mg_villages_supported_building_types) do
@ -608,9 +600,14 @@ if minetest.get_modpath("mg_villages") ~= nil then
meta:set_string("entrance", minetest.serialize(entrance))
-- Store nodedata into the spawner's metadata
meta:set_string("node_data", minetest.serialize(nodedata))
-- Find nearby plotmarkers
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 20)
-- Find nearby plotmarkers, excluding current plotmarker
local nearby_plotmarkers = npc.places.find_plotmarkers(pos, 20, 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
meta:set_string("building_pos_data", minetest.serialize(building_pos_data))
end
-- Initialize NPCs
local npcs = {}
meta:set_string("npcs", minetest.serialize(npcs))