VoxeLibre/mods/MAPGEN/mcl_villages/foundation.lua

201 lines
7.5 KiB
Lua
Raw Normal View History

local foundation_materials = {}
foundation_materials["mcl_core:sand"] = "mcl_core:sandstone"
2024-07-23 01:09:17 +02:00
foundation_materials["mcl_core:redsand"] = "mcl_core:redsandstone"
2024-07-19 14:56:06 +02:00
local function is_air(node)
2024-07-23 01:09:17 +02:00
return not node or node.name == "air" or node.name == "ignore" or node.name == "mcl_villages:no_paths"
2024-07-19 14:56:06 +02:00
end
local function is_solid(node)
2024-07-23 01:09:17 +02:00
if not node or node.name == "air" or node.name == "ignore" or node.name == "mcl_villages:no_paths" then return false end
2024-07-19 14:56:06 +02:00
--if string.find(node.name,"leaf") then return false end
--if string.find(node.name,"tree") then return false end
local ndef = minetest.registered_nodes[node.name]
return ndef and ndef.walkable
end
2024-07-20 02:10:25 +02:00
local function make_solid(lvm, cp, with, except)
local cur = lvm:get_node_at(cp)
2024-07-23 01:09:17 +02:00
if not is_solid(cur) or (except and cur.name == except.name) then
lvm:set_node_at(cp, with)
2024-07-20 02:10:25 +02:00
end
end
2024-07-19 14:56:06 +02:00
local function excavate(lvm,xi,yi,zi,pr)
local pos, n, c = vector.new(xi,yi,zi), nil, 0
local node = lvm:get_node_at(pos)
if is_air(node) then return false end -- already empty, nothing to do
pos.y = pos.y-1
if not is_air(lvm:get_node_at(pos)) then return false end -- below is solid, do not clear above anymore
-- count empty nodes below otherwise
for x = xi-1,xi+1 do
for z = zi-1,zi+1 do
pos.x, pos.z = x, z
if is_air(lvm:get_node_at(pos)) then c = c + 1 end
end
end
2024-07-19 14:56:06 +02:00
-- try to completely remove trees overhead
if not string.find(node.name, "leaf") and not string.find(node.name, "tree") then
-- stop randomly depending on fill, to narrow down the caves
2024-07-23 01:09:17 +02:00
if (pr:next(0,1e9)/1e9)^2 > c/9.1 then return false end
2024-07-19 14:56:06 +02:00
end
lvm:set_node_at(vector.new(xi, yi, zi),{name="air"})
return true -- modified
end
local function grow_foundation(lvm,xi,yi,zi,pr,surface_mat,platform_mat)
local pos, n, c = vector.new(xi,yi,zi), nil, 0
if is_solid(lvm:get_node_at(pos)) then return false end -- already solid, nothing to do
pos.y = pos.y+1
local cur = lvm:get_node_at(pos)
if not is_solid(cur) then return false end -- above is empty, do not fill below
2024-07-23 01:09:17 +02:00
if cur and cur.name and cur.name ~= surface_mat.name then platform_mat = cur end
2024-07-19 14:56:06 +02:00
if pr:next(1,5) == 5 then -- randomly switch to stone sometimes
2024-07-23 01:09:17 +02:00
platform_mat = { name = "mcl_core:stone" }
2024-07-19 14:56:06 +02:00
end
-- count solid nodes above otherwise
for x = xi-1,xi+1 do
for z = zi-1,zi+1 do
pos.x, pos.z = x, z
if is_solid(lvm:get_node_at(pos)) then c = c + 1 end
end
end
2024-07-19 14:56:06 +02:00
-- stop randomly depending on fill, to narrow down the foundation
2024-07-23 01:09:17 +02:00
if (pr:next(0,1e9)/1e9)^2 > c/9.1 then return false end
lvm:set_node_at(vector.new(xi, yi, zi), platform_mat)
2024-07-19 14:56:06 +02:00
return true -- modified
end
-------------------------------------------------------------------------------
2021-04-16 13:35:03 +02:00
-- function clear space above baseplate
-------------------------------------------------------------------------------
2024-07-23 01:09:17 +02:00
function mcl_villages.terraform(lvm, settlement, pr)
-- TODO: further optimize by using raw data arrays instead of set_node_at. But OK for a first draft.
2024-07-19 14:56:06 +02:00
--local lvm, emin, emax = minetest.get_mapgen_object("voxelmanip")
2024-07-23 01:09:17 +02:00
--local lvm = VoxelManip()
2024-07-23 01:09:17 +02:00
-- we make the foundations 1 node wider than requested, to have one node for path laying
for i, building in ipairs(settlement) do
--lvm:read_from_map(vector.new(pos.x-2, pos.y-20, pos.z-2), vector.new(pos.x+sx+2, pos.y+sy+20, pos.z+sz+2))
--lvm:get_data()
if not building.no_clearance then
local pos, size = building.pos, building.size
pos = vector.offset(pos, -math.floor(size.x/2)-1, 0, -math.floor(size.z/2)-1)
mcl_villages.clearance(lvm, pos.x, pos.y, pos.z, size.x+2, size.y, size.z+2, pr)
2021-01-29 19:49:33 +01:00
end
2024-07-23 01:09:17 +02:00
--lvm:write_to_map(false)
end
for i, building in ipairs(settlement) do
if not building.no_ground_turnip then
local pos, size = building.pos, building.size
local surface_mat = building.surface_mat
local platform_mat = building.platform_mat or { name = foundation_materials[surface_mat.name] or "mcl_core:dirt" }
building.platform_mat = platform_mat -- remember for use in schematic placement
pos = vector.offset(pos, -math.floor(size.x/2)-1, 0, -math.floor(size.z/2)-1)
mcl_villages.foundation(lvm, pos.x, pos.y, pos.z, size.x+2, -4, size.z+2, surface_mat, platform_mat, pr)
2024-07-20 02:10:25 +02:00
end
end
end
2024-07-23 01:09:17 +02:00
local AIR = {name = "air"}
function mcl_villages.clearance(lvm, px, py, pz, sx, sy, sz, pr)
2024-07-20 02:10:25 +02:00
-- excavate the needed volume, some headroom, and add a baseplate
2024-07-23 01:09:17 +02:00
for xi = px,px+sx-1 do
for zi = pz,pz+sz-1 do
lvm:set_node_at(vector.new(xi, py+1, zi),AIR)
-- py+2 to py+5 are filled larger below!
for yi = py+6,py+sy do
lvm:set_node_at(vector.new(xi, yi, zi),AIR)
2024-07-19 14:56:06 +02:00
end
end
2024-07-20 02:10:25 +02:00
end
-- slightly widen the cave, to make easier to enter for mobs
2024-07-23 01:09:17 +02:00
for xi = px-1,px+sx do
for zi = pz-1,pz+sz do
for yi = py+2,py+5 do
lvm:set_node_at(vector.new(xi, yi, zi),AIR)
2024-07-19 14:56:06 +02:00
end
end
2024-07-20 02:10:25 +02:00
end
-- some extra gaps
2024-07-23 01:09:17 +02:00
for xi = px-2,px+sx+1 do
for zi = pz-2,pz+sz+1 do
2024-07-20 02:10:25 +02:00
if pr:next(1,4) == 1 then
2024-07-23 01:09:17 +02:00
for yi = py+3,py+5 do
lvm:set_node_at(vector.new(xi, yi, zi),AIR)
2024-07-19 14:56:06 +02:00
end
end
end
2024-07-20 02:10:25 +02:00
end
2024-07-23 01:09:17 +02:00
-- cave some additional area overhead, try to make it interesting though
for yi = py+3,py+sy*3 do
local active = false
for xi = px-2,px+sx+1 do
for zi = pz-2,pz+sz+1 do
if excavate(lvm,xi,yi,zi,pr) then active = true end
end
end
if not active and yi > py+sy+5 then break end
end
end
function mcl_villages.foundation(lvm, px, py, pz, sx, sy, sz, surface_mat, platform_mat, pr)
-- generate a baseplate
for xi = px,px+sx-1 do
for zi = pz,pz+sz-1 do
lvm:set_node_at(vector.new(xi, py, zi), surface_mat)
make_solid(lvm, vector.new(xi, py - 1, zi), platform_mat)
end
end
2024-07-20 02:10:25 +02:00
-- slightly widen the baseplate, to make easier to enter for mobs
2024-07-23 01:09:17 +02:00
for xi = px,px+sx-1 do
make_solid(lvm, vector.new(xi, py-1, pz-1), surface_mat, platform_mat)
make_solid(lvm, vector.new(xi, py-1, pz), platform_mat)
make_solid(lvm, vector.new(xi, py-1, pz+sz-1), platform_mat)
make_solid(lvm, vector.new(xi, py-1, pz+sz), surface_mat, platform_mat)
end
for zi = pz,pz+sz-1 do
make_solid(lvm, vector.new(px-1, py-1, zi), surface_mat, platform_mat)
make_solid(lvm, vector.new(px, py-1, zi), platform_mat)
make_solid(lvm, vector.new(px+sx-1, py-1, zi), platform_mat)
make_solid(lvm, vector.new(px+sx, py-1, zi), surface_mat, platform_mat)
2024-07-20 02:10:25 +02:00
end
-- make some additional steps, along both x sides
2024-07-23 01:09:17 +02:00
for xi = px,px+sx-1 do
local cp = vector.new(xi, py-3, pz-1)
2024-07-20 02:10:25 +02:00
if is_solid(lvm:get_node_at(cp)) then
2024-07-23 01:09:17 +02:00
cp = vector.new(xi, py-2, pz-1)
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-23 01:09:17 +02:00
cp.z = pz-2
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-19 14:56:06 +02:00
end
2024-07-23 01:09:17 +02:00
local cp = vector.new(xi, py-3, pz+sz)
2024-07-20 02:10:25 +02:00
if is_solid(lvm:get_node_at(cp)) then
2024-07-23 01:09:17 +02:00
cp = vector.new(xi, py-2, pz+sz)
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-23 01:09:17 +02:00
cp.z = pz + sz + 1
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-19 14:56:06 +02:00
end
2024-07-20 02:10:25 +02:00
end
-- make some additional steps, along both z sides
2024-07-23 01:09:17 +02:00
for zi = pz,pz+sz-1 do
local cp = vector.new(px-1, py-3, zi)
2024-07-20 02:10:25 +02:00
if is_solid(lvm:get_node_at(cp)) then
2024-07-23 01:09:17 +02:00
cp = vector.new(px-1, py-2, zi)
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-23 01:09:17 +02:00
cp.x = px-2
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-19 14:56:06 +02:00
end
2024-07-23 01:09:17 +02:00
local cp = vector.new(px+sx, py-3, zi)
2024-07-20 02:10:25 +02:00
if is_solid(lvm:get_node_at(cp)) then
2024-07-23 01:09:17 +02:00
cp = vector.new(px+sx, py-2, zi)
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-23 01:09:17 +02:00
cp.x = px+sx+1
2024-07-20 02:10:25 +02:00
make_solid(lvm, cp, surface_mat, platform_mat)
2024-07-19 14:56:06 +02:00
end
2024-07-20 02:10:25 +02:00
end
-- construct additional baseplate below, also try to make it interesting
2024-07-23 01:09:17 +02:00
for yi = py-2,py-20,-1 do
2024-07-20 02:10:25 +02:00
local active = false
2024-07-23 01:09:17 +02:00
for xi = px-1,px+sx do
for zi = pz-1,pz+sz do
2024-07-20 02:10:25 +02:00
if grow_foundation(lvm,xi,yi,zi,pr,surface_mat,platform_mat) then active = true end
2021-01-29 19:49:33 +01:00
end
end
2024-07-23 01:09:17 +02:00
if not active and yi < py + sy then break end
2021-01-29 19:49:33 +01:00
end
end