MapGen: advanced tree generator
This commit is contained in:
@@ -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
|
||||
|
||||
if not Smc.isAsync then
|
||||
@@ -7,8 +13,9 @@ end
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
---@class MapGen.Tree
|
||||
---@field name string
|
||||
---@field path string
|
||||
---@field name string
|
||||
---@field path string
|
||||
---@field placerName string
|
||||
local Tree = {}
|
||||
|
||||
---Definition table for the `MapGen.Tree`.
|
||||
@@ -24,12 +31,14 @@ function Tree:new(name, def)
|
||||
local intance = getInstance(self, {})
|
||||
intance.name = name
|
||||
intance.path = def.path
|
||||
intance.placerName = Mod.getInfo().shortName..':'..name..'_placer'
|
||||
|
||||
intance:registerPlacer()
|
||||
|
||||
return intance
|
||||
end
|
||||
|
||||
---@return Node?
|
||||
function Tree:registerPlacer()
|
||||
if Node == nil then
|
||||
return
|
||||
@@ -41,27 +50,83 @@ function Tree:registerPlacer()
|
||||
}
|
||||
})
|
||||
|
||||
core.log(node.itemDef.settings.name)
|
||||
self.placerName = node.itemDef.settings.name
|
||||
|
||||
core.register_lbm({
|
||||
run_at_every_load = true,
|
||||
name = ':'..self.name..'_placer_lbm',
|
||||
core.register_abm({
|
||||
interval = 1,
|
||||
chance = 1,
|
||||
nodenames = {node.itemDef.settings.name},
|
||||
action = function(pos, node, dtime)
|
||||
self:place(pos)
|
||||
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
|
||||
|
||||
---@param pos MapPosition
|
||||
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
|
||||
|
||||
---@param voxelManip VoxelManip
|
||||
---@param pos MapPosition
|
||||
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
|
||||
|
||||
return Tree
|
||||
|
||||
Binary file not shown.
@@ -751,14 +751,12 @@ mapGenerator:registerBiome('midgard', 'green_meadow', {
|
||||
local value = ryeNoise:get_2d({x=x, y=z}) + math.random(0, 5)
|
||||
|
||||
if value > 8 then
|
||||
if math.random(0, 10) > 9 then
|
||||
if x % 10 == 0 and z % 10 == 0 then
|
||||
---@type MapVector
|
||||
local pos = v(x, y+1, z)
|
||||
mapGenerator:getTree('test_tree'):placeOnVM(voxelManip, pos)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
--generateRock = generateRock,
|
||||
--generateTurf = generateTurf,
|
||||
|
||||
Reference in New Issue
Block a user