133 lines
2.8 KiB
Lua
133 lines
2.8 KiB
Lua
local mathFloor = math.floor
|
|
|
|
local v = vector.new
|
|
local VoxelManip, VoxelArea = VoxelManip, VoxelArea
|
|
local corePlaceSchematic, corePlaceSchematicOnVM, coreEmergeArea = core.place_schematic, core.place_schematic_on_vmanip, core.emerge_area
|
|
local CONTENT_IGNORE = core.CONTENT_IGNORE
|
|
|
|
local getInstance = Class.getInstance
|
|
|
|
local Node
|
|
|
|
if not Smc.isAsync then
|
|
Node = Smc.requireNode()
|
|
end
|
|
|
|
---@class MapGen.Tree
|
|
---@field name string
|
|
---@field path string
|
|
---@field placerName string
|
|
local Tree = {}
|
|
|
|
---Definition table for the `MapGen.Tree`.
|
|
---
|
|
---**Only for EmmyLua.**
|
|
---@class MapGen.TreeDef
|
|
---@field path string
|
|
|
|
---@param name string
|
|
---@param def MapGen.TreeDef
|
|
---@return MapGen.Tree
|
|
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
|
|
end
|
|
|
|
local node = Node:new({
|
|
settings = {
|
|
name = self.name..'_placer'
|
|
}
|
|
})
|
|
|
|
self.placerName = node.itemDef.settings.name
|
|
|
|
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 MapVector
|
|
function Tree:place(pos)
|
|
local pos1, pos2 = pos - v(16, 0, 16), pos + v(16, 80, 16) --[[@as MapVector]]
|
|
|
|
if isAreaGenerated(pos1, pos2) then
|
|
corePlaceSchematic(pos, self.path, getRotation(pos), nil, false, 'place_center_x, place_center_z')
|
|
else
|
|
coreEmergeArea(pos1, pos2)
|
|
end
|
|
end
|
|
|
|
---@param voxelManip VoxelManip
|
|
---@param pos MapVector
|
|
function Tree:placeOnVM(voxelManip, pos)
|
|
local result = corePlaceSchematicOnVM(voxelManip, pos, self.path, getRotation(pos), nil, false, '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
|