2021-04-27 23:03:47 +00:00
|
|
|
local get_node = mcl_mapgen.get_far_node
|
2021-03-28 18:56:51 +00:00
|
|
|
|
2021-01-27 08:56:53 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- function to copy tables
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.shallowCopy(original)
|
|
|
|
local copy = {}
|
|
|
|
for key, value in pairs(original) do
|
|
|
|
copy[key] = value
|
|
|
|
end
|
|
|
|
return copy
|
|
|
|
end
|
|
|
|
--
|
|
|
|
--
|
|
|
|
--
|
|
|
|
function settlements.round(num, numDecimalPlaces)
|
|
|
|
local mult = 10^(numDecimalPlaces or 0)
|
|
|
|
return math.floor(num * mult + 0.5) / mult
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- function to find surface block y coordinate
|
|
|
|
-- returns surface postion
|
|
|
|
-------------------------------------------------------------------------------
|
2021-02-21 23:15:32 +00:00
|
|
|
function settlements.find_surface(pos, wait)
|
2021-01-27 08:56:53 +00:00
|
|
|
local p6 = vector.new(pos)
|
|
|
|
local cnt = 0
|
2021-02-21 23:15:32 +00:00
|
|
|
local itter = 1 -- count up or down
|
2021-01-27 08:56:53 +00:00
|
|
|
local cnt_max = 200
|
|
|
|
-- check, in which direction to look for surface
|
2021-02-21 23:15:32 +00:00
|
|
|
local surface_node
|
|
|
|
if wait then
|
2021-03-28 18:56:51 +00:00
|
|
|
surface_node = get_node(p6, true, 10000000)
|
2021-01-27 08:56:53 +00:00
|
|
|
else
|
2021-03-28 18:56:51 +00:00
|
|
|
surface_node = get_node(p6)
|
2021-02-21 23:15:32 +00:00
|
|
|
end
|
|
|
|
if surface_node.name=="air" or surface_node.name=="ignore" then
|
|
|
|
itter = -1
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
-- go through nodes an find surface
|
|
|
|
while cnt < cnt_max do
|
|
|
|
-- Check Surface_node and Node above
|
|
|
|
--
|
|
|
|
if settlements.surface_mat[surface_node.name] then
|
2021-03-28 18:56:51 +00:00
|
|
|
local surface_node_plus_1 = get_node({ x=p6.x, y=p6.y+1, z=p6.z})
|
2021-01-27 08:56:53 +00:00
|
|
|
if surface_node_plus_1 and surface_node and
|
|
|
|
(string.find(surface_node_plus_1.name,"air") or
|
|
|
|
string.find(surface_node_plus_1.name,"snow") or
|
|
|
|
string.find(surface_node_plus_1.name,"fern") or
|
|
|
|
string.find(surface_node_plus_1.name,"flower") or
|
|
|
|
string.find(surface_node_plus_1.name,"bush") or
|
|
|
|
string.find(surface_node_plus_1.name,"tree") or
|
|
|
|
string.find(surface_node_plus_1.name,"grass"))
|
|
|
|
then
|
|
|
|
settlements.debug("find_surface7: " ..surface_node.name.. " " .. surface_node_plus_1.name)
|
|
|
|
return p6, surface_node.name
|
|
|
|
else
|
|
|
|
settlements.debug("find_surface2: wrong surface+1")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
settlements.debug("find_surface3: wrong surface "..surface_node.name.." at pos "..minetest.pos_to_string(p6))
|
|
|
|
end
|
|
|
|
|
|
|
|
p6.y = p6.y + itter
|
|
|
|
if p6.y < 0 then
|
|
|
|
settlements.debug("find_surface4: y<0")
|
|
|
|
return nil
|
|
|
|
end
|
2021-02-21 23:15:32 +00:00
|
|
|
cnt = cnt+1
|
2021-03-28 18:56:51 +00:00
|
|
|
surface_node = get_node(p6)
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
settlements.debug("find_surface5: cnt_max overflow")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- check distance for new building
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.check_distance(settlement_info, building_pos, building_size)
|
2021-01-29 18:49:33 +00:00
|
|
|
local distance
|
|
|
|
for i, built_house in ipairs(settlement_info) do
|
|
|
|
distance = math.sqrt(
|
|
|
|
((building_pos.x - built_house["pos"].x)*(building_pos.x - built_house["pos"].x))+
|
|
|
|
((building_pos.z - built_house["pos"].z)*(building_pos.z - built_house["pos"].z)))
|
|
|
|
if distance < building_size or distance < built_house["hsize"] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- save list of generated settlements
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.save()
|
2021-01-29 18:49:33 +00:00
|
|
|
local file = io.open(minetest.get_worldpath().."/settlements.txt", "w")
|
|
|
|
if file then
|
|
|
|
file:write(minetest.serialize(settlements_in_world))
|
|
|
|
file:close()
|
|
|
|
end
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- load list of generated settlements
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.load()
|
2021-01-29 18:49:33 +00:00
|
|
|
local file = io.open(minetest.get_worldpath().."/settlements.txt", "r")
|
|
|
|
if file then
|
|
|
|
local table = minetest.deserialize(file:read("*all"))
|
|
|
|
if type(table) == "table" then
|
|
|
|
return table
|
2021-01-28 19:12:16 +00:00
|
|
|
end
|
|
|
|
end
|
2021-01-29 18:49:33 +00:00
|
|
|
return {}
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- fill chests
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.fill_chest(pos, pr)
|
2021-01-29 18:49:33 +00:00
|
|
|
-- initialize chest (mts chests don't have meta)
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
if meta:get_string("infotext") ~= "Chest" then
|
|
|
|
-- For MineClone2 0.70 or before
|
|
|
|
-- minetest.registered_nodes["mcl_chests:chest"].on_construct(pos)
|
|
|
|
--
|
|
|
|
-- For MineClone2 after commit 09ab1482b5 (the new entity chests)
|
|
|
|
minetest.registered_nodes["mcl_chests:chest_small"].on_construct(pos)
|
|
|
|
end
|
|
|
|
-- fill chest
|
|
|
|
local inv = minetest.get_inventory( {type="node", pos=pos} )
|
|
|
|
|
2021-04-16 11:35:03 +00:00
|
|
|
local function get_treasures(prand)
|
2021-01-29 18:49:33 +00:00
|
|
|
local loottable = {{
|
2021-01-28 07:24:43 +00:00
|
|
|
stacks_min = 3,
|
|
|
|
stacks_max = 8,
|
|
|
|
items = {
|
|
|
|
{ itemstring = "mcl_core:diamond", weight = 3, amount_min = 1, amount_max = 3 },
|
|
|
|
{ itemstring = "mcl_core:iron_ingot", weight = 10, amount_min = 1, amount_max = 5 },
|
|
|
|
{ itemstring = "mcl_core:gold_ingot", weight = 5, amount_min = 1, amount_max = 3 },
|
|
|
|
{ itemstring = "mcl_farming:bread", weight = 15, amount_min = 1, amount_max = 3 },
|
|
|
|
{ itemstring = "mcl_core:apple", weight = 15, amount_min = 1, amount_max = 3 },
|
|
|
|
{ itemstring = "mcl_tools:pick_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_tools:sword_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_armor:chestplate_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_armor:helmet_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_armor:leggings_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_armor:boots_iron", weight = 5 },
|
|
|
|
{ itemstring = "mcl_core:obsidian", weight = 5, amount_min = 3, amount_max = 7 },
|
|
|
|
{ itemstring = "mcl_core:sapling", weight = 5, amount_min = 3, amount_max = 7 },
|
|
|
|
{ itemstring = "mcl_mobitems:saddle", weight = 3 },
|
|
|
|
{ itemstring = "mobs_mc:iron_horse_armor", weight = 1 },
|
|
|
|
{ itemstring = "mobs_mc:gold_horse_armor", weight = 1 },
|
|
|
|
{ itemstring = "mobs_mc:diamond_horse_armor", weight = 1 },
|
|
|
|
}
|
2021-01-29 18:49:33 +00:00
|
|
|
}}
|
2021-04-16 11:35:03 +00:00
|
|
|
local items = mcl_loot.get_multi_loot(loottable, prand)
|
2021-01-28 07:24:43 +00:00
|
|
|
return items
|
|
|
|
end
|
|
|
|
|
2021-01-29 18:49:33 +00:00
|
|
|
local items = get_treasures(pr)
|
2021-02-22 17:58:35 +00:00
|
|
|
mcl_loot.fill_inventory(inv, "main", items, pr)
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- initialize furnace
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.initialize_furnace(pos)
|
|
|
|
-- find chests within radius
|
2021-04-16 11:35:03 +00:00
|
|
|
local furnacepos = minetest.find_node_near(pos,
|
2021-01-27 08:56:53 +00:00
|
|
|
7, --radius
|
|
|
|
{"mcl_furnaces:furnace"})
|
|
|
|
-- initialize furnacepos (mts furnacepos don't have meta)
|
2021-04-16 11:35:03 +00:00
|
|
|
if furnacepos
|
2021-01-27 08:56:53 +00:00
|
|
|
then
|
|
|
|
local meta = minetest.get_meta(furnacepos)
|
2021-04-16 11:35:03 +00:00
|
|
|
if meta:get_string("infotext") ~= "furnace"
|
2021-01-27 08:56:53 +00:00
|
|
|
then
|
|
|
|
minetest.registered_nodes["mcl_furnaces:furnace"].on_construct(furnacepos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- initialize anvil
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.initialize_anvil(pos)
|
|
|
|
-- find chests within radius
|
2021-04-16 11:35:03 +00:00
|
|
|
local anvilpos = minetest.find_node_near(pos,
|
2021-01-27 08:56:53 +00:00
|
|
|
7, --radius
|
|
|
|
{"mcl_anvils:anvil"})
|
|
|
|
-- initialize anvilpos (mts anvilpos don't have meta)
|
2021-04-16 11:35:03 +00:00
|
|
|
if anvilpos
|
2021-01-27 08:56:53 +00:00
|
|
|
then
|
|
|
|
local meta = minetest.get_meta(anvilpos)
|
2021-04-16 11:35:03 +00:00
|
|
|
if meta:get_string("infotext") ~= "anvil"
|
2021-01-27 08:56:53 +00:00
|
|
|
then
|
|
|
|
minetest.registered_nodes["mcl_anvils:anvil"].on_construct(anvilpos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- randomize table
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function shuffle(tbl, pr)
|
|
|
|
local table = settlements.shallowCopy(tbl)
|
|
|
|
local size = #table
|
|
|
|
for i = size, 1, -1 do
|
|
|
|
local rand = pr:next(1, size)
|
|
|
|
table[i], table[rand] = table[rand], table[i]
|
|
|
|
end
|
|
|
|
return table
|
|
|
|
end
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- Set array to list
|
|
|
|
-- https://stackoverflow.com/questions/656199/search-for-an-item-in-a-lua-list
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
function settlements.Set (list)
|
2021-01-29 18:49:33 +00:00
|
|
|
local set = {}
|
|
|
|
for _, l in ipairs(list) do set[l] = true end
|
|
|
|
return set
|
2021-01-27 08:56:53 +00:00
|
|
|
end
|