MapGen: advanced tree generator

This commit is contained in:
Van
2026-06-15 22:19:23 +03:00
parent 098f247928
commit 6cec8806ed
3 changed files with 74 additions and 11 deletions

View File

@@ -1,3 +1,9 @@
local v = vector.new
local mathFloor = math.floor
local corePlaceSchematic, corePlaceSchematicOnVM, coreEmergeArea = core.place_schematic, core.place_schematic_on_vmanip, core.emerge_area
local CONTENT_IGNORE = core.CONTENT_IGNORE
local VoxelManip, VoxelArea = VoxelManip, VoxelArea
local Node local Node
if not Smc.isAsync then if not Smc.isAsync then
@@ -7,8 +13,9 @@ end
local getInstance = Class.getInstance local getInstance = Class.getInstance
---@class MapGen.Tree ---@class MapGen.Tree
---@field name string ---@field name string
---@field path string ---@field path string
---@field placerName string
local Tree = {} local Tree = {}
---Definition table for the `MapGen.Tree`. ---Definition table for the `MapGen.Tree`.
@@ -24,12 +31,14 @@ function Tree:new(name, def)
local intance = getInstance(self, {}) local intance = getInstance(self, {})
intance.name = name intance.name = name
intance.path = def.path intance.path = def.path
intance.placerName = Mod.getInfo().shortName..':'..name..'_placer'
intance:registerPlacer() intance:registerPlacer()
return intance return intance
end end
---@return Node?
function Tree:registerPlacer() function Tree:registerPlacer()
if Node == nil then if Node == nil then
return return
@@ -41,27 +50,83 @@ function Tree:registerPlacer()
} }
}) })
core.log(node.itemDef.settings.name) self.placerName = node.itemDef.settings.name
core.register_lbm({ core.register_abm({
run_at_every_load = true, interval = 1,
name = ':'..self.name..'_placer_lbm', chance = 1,
nodenames = {node.itemDef.settings.name}, nodenames = {node.itemDef.settings.name},
action = function(pos, node, dtime) action = function(pos, node, dtime)
self:place(pos) self:place(pos)
end end
}) })
return node
end
---@param pos MapVector
---@return '0'|'90'|'180'|'270
local function getRotation(pos)
local rotation = mathFloor((pos.x + pos.y + pos.z) % 4)
if rotation == 0 then
return '0'
elseif rotation == 1 then
return '90'
elseif rotation == 2 then
return '180'
elseif rotation == 3 then
return '270'
else
error()
end
end
---@param pos1 MapVector
---@param pos2 MapVector
---@return boolean
local function isAreaGenerated(pos1, pos2)
local voxelManip = VoxelManip(pos1, pos2)
local eMin, eMax = voxelManip:get_emerged_area()
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
local index
local data = voxelManip:get_data()
for z = pos1.z, pos2.z, 16 do
for y = pos1.y, pos2.y, 16 do
for x = pos1.x, pos2.x, 16 do
index = area:index(x, y, z)
if data[index] == CONTENT_IGNORE then
return false
end
end
end
end
return true
end end
---@param pos MapPosition ---@param pos MapPosition
function Tree:place(pos) function Tree:place(pos)
core.place_schematic(pos, self.path, 'random') ---@type MapVector, MapVector
local pos1, pos2 = pos - v(16, 0, 16), pos + v(16, 80, 16)
if isAreaGenerated(pos1, pos2) then
corePlaceSchematic(pos, self.path, getRotation(pos), nil, nil, 'place_center_x, place_center_z')
else
coreEmergeArea(pos1, pos2)
end
end end
---@param voxelManip VoxelManip ---@param voxelManip VoxelManip
---@param pos MapPosition ---@param pos MapPosition
function Tree:placeOnVM(voxelManip, pos) function Tree:placeOnVM(voxelManip, pos)
core.place_schematic_on_vmanip(voxelManip, pos, self.path, 'random', nil, nil, 'place_center_x, place_center_z') local result = corePlaceSchematicOnVM(voxelManip, pos, self.path, getRotation(pos), nil, nil, 'place_center_x, place_center_z')
if not result then
voxelManip:set_node_at(pos, {name = self.placerName, force_place = true})
end
end end
return Tree return Tree

View File

@@ -751,14 +751,12 @@ mapGenerator:registerBiome('midgard', 'green_meadow', {
local value = ryeNoise:get_2d({x=x, y=z}) + math.random(0, 5) local value = ryeNoise:get_2d({x=x, y=z}) + math.random(0, 5)
if value > 8 then if value > 8 then
if math.random(0, 10) > 9 then if x % 10 == 0 and z % 10 == 0 then
---@type MapVector ---@type MapVector
local pos = v(x, y+1, z) local pos = v(x, y+1, z)
mapGenerator:getTree('test_tree'):placeOnVM(voxelManip, pos) mapGenerator:getTree('test_tree'):placeOnVM(voxelManip, pos)
end end
end end
end end
--generateRock = generateRock, --generateRock = generateRock,
--generateTurf = generateTurf, --generateTurf = generateTurf,