MapGen: Moving triangulation outside of Lua scripts, using pre-triangulated JSON files

This commit is contained in:
Van
2026-03-09 20:07:00 +03:00
parent 2c13e1697f
commit 4ef2476771
6 changed files with 32766 additions and 163 deletions

View File

@@ -23,9 +23,6 @@ local Layer = require('MapGen.Layer')
---@type MapGen.StaticLayer
local StaticLayer = require('MapGen.StaticLayer')
---@type MapGen.Peak
local Peak = require('MapGen.Peak')
---@type MapGen.Biome
local Biome = require('MapGen.Biome')
@@ -203,7 +200,7 @@ end
function MapGen:pointInRegion2D(layer, regionName, point)
local region = layer.regionsByName[regionName]
assert(layer ~= nil, ('There is no region named `%s` registered.'):format(regionName))
assert(region ~= nil, ('There is no region named `%s` registered.'):format(regionName))
return region:pointInRegion(point)
end
@@ -418,105 +415,6 @@ function MapGen:registerStaticLayer(name, def)
end)
end
---Mark a world point as `MapGen.Peak` and assign it a specific color that will affect the generation.
---
---Note: The peaks shouldn't overlap.
---@param layerName string The name of the layer in which the new peak will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
function MapGen:registerPeak(layerName, category, def)
local layer = self:getLayerByName(layerName)
--assert(self:getPeakByPos(layer, category, def.pos) == nil,
--('In the `%s` layer in the `%s` category there is already a peak with coordinates: %s, %s, %s.'):format(
-- layerName, category, def.pos.x, def.pos.y, def.pos.z))
---@type MapGen.Peak
local peak = Peak:new(def)
layer:addPeak(category, peak)
end
---Mark a world point as `MapGen.Peak` and assign it a specific color that will affect the generation.
---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field.
---
---Note: The peaks shouldn't overlap.
---@param layerName string The name of the layer in which the new peak will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
function MapGen:register2DPeak(layerName, category, def)
def = table.copy_with_metatables(def)
def.pos.y = 0
if def.groups == nil then
def.groups = {is2d = 1}
else
def.groups.is2d = 1
end
self:registerPeak(layerName, category, def)
end
---Mark the world points as `MapGen.Peak` and assign them all a specific noise that will affect generation.
---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field.
---
---Note: The peaks shouldn't overlap.
---@param layerName string The name of the layer in which the new peaks will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
---@param defs MapGen.PeakDef[] Peaks description table, see details `MapGen.PeakDef`.
function MapGen:register2DPeaks(layerName,category, defs)
for _, def in ipairs(defs) do
self:register2DPeak(layerName, category, def)
end
end
---Extract the `MapGen.Peak` from the image and assign them all a specific color that will affect the generation.
---@param layerName string The name of the layer in which the new peaks will be included.
---@param map string The name of the image from which the peaks will be extracted. It should be located in the `<mod_name>/textures/maps` folder and have the following format `<image_name>.ff`
---@param scale number Multiplier of how many blocks (nodes) will be between image pixels. It is not recommended to set the value below 50 for performance reasons.
---@param zeroPos IntegerPosition2d The position in pixels where the zero world coordinate will be in the image.
---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
---@param groups? table<string, number> Groups that will be added to all peaks.
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
if scale < 2 then
error('The map scale should not be less than 2.')
end
local modPath = core.get_modpath(core.get_current_modname())
local peaks = ff2luat(modPath .. '/textures/maps/' .. map)
---@type {h:integer, w:integer}
local mapSize = peaks.size
peaks.size = nil
zeroPos.x = zeroPos.x + mathRound(mapSize.w/2)
zeroPos.y = zeroPos.y + mathRound(mapSize.h/2)
local x, z
for color, peaksPos in pairs(peaks) do
for _, peakPos in ipairs(peaksPos) do
---@type integer, integer
x, z = (peakPos.x - zeroPos.x - 1) * scale, (peakPos.y - zeroPos.y - 1) * -scale
-- Converting a rectangular grid into an isometric-like one.
if peakPos.y % 2 == 1 then
x = mathRound(x + (scale/2))
end
---@type MapGen.PeakDef
local def = {
pos = {x=x, y=0, z=z},
color = color,
groups = groups,
}
self:register2DPeak(layerName, category, def)
end
end
end
---Adds groups to the already registered groups in the peak by coordinate.
---If the peak does not exist, an error will be raised.
---@param layerName string The name of the layer in which the new peak will be included.
@@ -588,11 +486,11 @@ end
-- --- INITIALIZATION METHODS ---
---Initialize the boundary region for a triangulated area.
---Initialize the boundary region for a layer and category.
---@param layer MapGen.Layer
---@param category MapGen.Layer.Category
---@param triangles MapGen.Triangle[]
function MapGen:initTriangultaionBoundaryRegion(layer, category, triangles)
function MapGen:initTriangultaionBoundaryRegion(layer, category)
local triangles = layer.trianglesList[category]
local regionVertices = self:getTriangulateBoundaryVertices(triangles)
local regionName = category..'_boundary'
@@ -602,17 +500,13 @@ function MapGen:initTriangultaionBoundaryRegion(layer, category, triangles)
layer:addRegion(regionName, region)
end
---Divide all layers into triangles whose vertices are the `Mapgen.Peak` belonging to these layers.
function MapGen:initLayersTriangultaion()
---Initialize border regions for all layers and all categories.
function MapGen:initLayersBoundaryRegions()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
for category, peaks in pairs(layer.peaksList) do
local triangles = Triangulation.triangulate(peaks)
layer.trianglesList[category] = triangles
self:initTriangultaionBoundaryRegion(layer, category, triangles)
-- layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList) -- 3D not supported
end
self:initTriangultaionBoundaryRegion(layer, 'landscape')
self:initTriangultaionBoundaryRegion(layer, 'temp')
self:initTriangultaionBoundaryRegion(layer, 'humidity')
end
end
@@ -1079,6 +973,7 @@ end
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
-- We obtain a generation area for further manipulations
--local voxelManip, eMin, eMax = core.get_mapgen_object('voxelmanip')
core.log('Chunk run')
local eMin, eMax = voxelManip:get_emerged_area()
@@ -1108,6 +1003,8 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
end
end
core.log('Postprocces run')
-- Post-proccesing generation
for _, ppdata in pairs(postProcessing) do
if ppdata.biome.postProcessing ~= nil then
@@ -1157,9 +1054,10 @@ function MapGen:run()
end)
self:initBiomesDiagram()
self:initLayersTriangultaion()
self:initLayersBoundaryRegions()
MapGen.isRunning = true
core.log('MapGen run')
end
return MapGen