MapGen: Moving triangulation outside of Lua scripts, using pre-triangulated JSON files
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,5 +1,13 @@
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||
local require = modInfo.require
|
||||
|
||||
---@type MapGen.Peak
|
||||
local Peak = require('MapGen.Peak')
|
||||
---@type MapGen.Triangle
|
||||
local Triangle = require('MapGen.Triangle')
|
||||
|
||||
---@class MapGen.Layer
|
||||
---@field name string
|
||||
---@field minY integer
|
||||
@@ -11,9 +19,10 @@ local getInstance = Class.getInstance
|
||||
---@field waterLevel integer
|
||||
---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number
|
||||
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number
|
||||
---@field peaksList {['landscape']:MapGen.Peak[],['temp']:MapGen.Peak[],['humidity']:MapGen.Peak[]}
|
||||
---@field noisesByColor {['landscape']:table<ColorString, ValueNoise>,['temp']:table<ColorString, ValueNoise>,['humidity']:table<ColorString, ValueNoise>}
|
||||
---@field noisesParamsByColor {['landscape']:table<ColorString, NoiseParams>,['temp']:table<ColorString, NoiseParams>,['humidity']:table<ColorString, NoiseParams>}
|
||||
---@field peaksList table<MapGen.Layer.Category, MapGen.Peak[]>
|
||||
---@field peaksByPos table<MapGen.Layer.Category, table<string, MapGen.Peak>>
|
||||
---@field noisesByColor table<MapGen.Layer.Category, table<ColorString, ValueNoise>>
|
||||
---@field noisesParamsByColor table<MapGen.Layer.Category, table<ColorString, NoiseParams>>
|
||||
---@field biomesByName table<string, MapGen.Biome>
|
||||
---@field biomesList MapGen.Biome[]
|
||||
---@field biomesDiagram table
|
||||
@@ -22,10 +31,15 @@ local getInstance = Class.getInstance
|
||||
---@field cavernsByName table<string, MapGen.Cavern>
|
||||
---@field cavernsList MapGen.Cavern[]
|
||||
---@field regionsByName table<string, MapGen.Region>
|
||||
---@field trianglesList {['landscape']:MapGen.Triangle[],['temp']:MapGen.Triangle[],['humidity']:MapGen.Triangle[]}
|
||||
---@field trianglesList table<MapGen.Layer.Category, MapGen.Triangle[]>
|
||||
---@field tetrahedronsList MapGen.Tetrahedron[] **Not supported**
|
||||
local Layer = {
|
||||
peaksList = {
|
||||
peaksList = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {},
|
||||
},
|
||||
peaksByPos = {
|
||||
landscape = {},
|
||||
temp = {},
|
||||
humidity = {},
|
||||
@@ -58,17 +72,23 @@ local Layer = {
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class MapGen.LayerDef
|
||||
---@field minY number
|
||||
---@field maxY number
|
||||
---@field minTemp number
|
||||
---@field maxTemp number
|
||||
---@field minHumidity number
|
||||
---@field maxHumidity number
|
||||
---@field waterLevel? number
|
||||
---@field minY integer
|
||||
---@field maxY integer
|
||||
---@field minTemp integer
|
||||
---@field maxTemp integer
|
||||
---@field minHumidity integer
|
||||
---@field maxHumidity integer
|
||||
---@field waterLevel? integer
|
||||
---@field calcTemp (fun(self:MapGen.Layer, value:number, height:number):number)?
|
||||
---@field calcHumidity (fun(self:MapGen.Layer, value:number, height:number):number)?
|
||||
---@field biomesGorizontalScattering number?
|
||||
---@field biomesVerticalScattering number?
|
||||
---@field biomesGorizontalScattering integer?
|
||||
---@field biomesVerticalScattering integer?
|
||||
---@field triangulationLandscapeFile string
|
||||
---@field triangulationTempFile string
|
||||
---@field triangulationHumidityFile string
|
||||
---@field landscapeMapScale number
|
||||
---@field tempMapScale number
|
||||
---@field humidityMapScale number
|
||||
|
||||
---@param self MapGen.Layer
|
||||
---@param value number
|
||||
@@ -78,6 +98,74 @@ local function defaultCalcFunc(self, value, height)
|
||||
return value
|
||||
end
|
||||
|
||||
---Checks `def` for compliance with `MapGen.PeakDef` and creates a new peak
|
||||
---if a peak with such coordinates has not yet been registered or returns an existing one.
|
||||
---
|
||||
---**Note:** If the colors of the new peak and the existing one at the same coordinates
|
||||
---are different, an error will be called.
|
||||
---@param def MapGen.PeakDef
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param scale number
|
||||
---@return MapGen.Peak
|
||||
function Layer:registerPeak(def, category, scale)
|
||||
assert(def.pos ~= nil)
|
||||
assert(def.pos.x ~= nil)
|
||||
assert(def.pos.z ~= nil)
|
||||
assert(def.color ~= nil)
|
||||
assert(type(def.color) == 'string')
|
||||
|
||||
def.pos.x = def.pos.x * scale
|
||||
def.pos.z = def.pos.z * scale
|
||||
def.pos.y = 0
|
||||
|
||||
local positionStr = core.pos_to_string(def.pos)
|
||||
|
||||
local peak = self.peaksByPos[category][positionStr]
|
||||
|
||||
if peak == nil then
|
||||
peak = Peak:new(def)
|
||||
self.peaksByPos[category][positionStr] = peak
|
||||
elseif peak.getColor() ~= def.color then
|
||||
error('There cannot be two peaks with the same coordinates but different colors.')
|
||||
end
|
||||
|
||||
return peak
|
||||
end
|
||||
|
||||
function Layer:initTrianglesNeighbours()
|
||||
for _, triangles in pairs(self.trianglesList) do
|
||||
for _, triangle in ipairs(triangles) do
|
||||
for _, id in ipairs(triangle.neighboursIDs) do
|
||||
table.insert(triangle.neighbours, triangles[id])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param fileName string
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param scale number
|
||||
function Layer:getTriaglesFromFile(fileName, category, scale)
|
||||
local file = io.open(core.get_modpath(core.get_current_modname())..'/maps/'..fileName, 'r')
|
||||
assert(file ~= nil)
|
||||
|
||||
local json = core.parse_json(file:read('*a')) or {}
|
||||
file:close()
|
||||
|
||||
local triangles = json.triangles
|
||||
assert(triangles ~= nil)
|
||||
|
||||
for _, tr in ipairs(triangles) do
|
||||
local p1 = self:registerPeak( tr.vertices[1], category, scale)
|
||||
local p2 = self:registerPeak( tr.vertices[2], category, scale)
|
||||
local p3 = self:registerPeak( tr.vertices[3], category, scale)
|
||||
|
||||
local triangle = Triangle:new(tr.id,p1, p2, p3, tr.adjacent_triangle_ids)
|
||||
|
||||
self:addTriangle(category, triangle)
|
||||
end
|
||||
end
|
||||
|
||||
---@param name string
|
||||
---@param def MapGen.LayerDef
|
||||
---@return MapGen.Layer
|
||||
@@ -93,20 +181,34 @@ function Layer:new(name, def)
|
||||
end
|
||||
|
||||
---@type MapGen.Layer
|
||||
local instance = getInstance(self, {
|
||||
name = name,
|
||||
minY = def.minY,
|
||||
maxY = def.maxY,
|
||||
minTemp = def.minTemp,
|
||||
maxTemp = def.maxTemp,
|
||||
minHumidity = def.minHumidity,
|
||||
maxHumidity = def.maxHumidity,
|
||||
waterLevel = def.waterLevel or 0,
|
||||
calcTemp = def.calcTemp,
|
||||
calcHumidity = def.calcHumidity,
|
||||
biomesGorizontalScattering = def.biomesGorizontalScattering or 0,
|
||||
biomesVerticalScattering = def.biomesVerticalScattering or 0,
|
||||
})
|
||||
local instance = getInstance(self, {})
|
||||
instance.name = name
|
||||
instance.minY = def.minY
|
||||
instance.maxY = def.maxY
|
||||
instance.minTemp = def.minTemp
|
||||
instance.maxTemp = def.maxTemp
|
||||
instance.minHumidity = def.minHumidity
|
||||
instance.maxHumidity = def.maxHumidity
|
||||
instance.waterLevel = def.waterLevel or 0
|
||||
instance.calcTemp = def.calcTemp
|
||||
instance.calcHumidity = def.calcHumidity
|
||||
instance.biomesGorizontalScattering = def.biomesGorizontalScattering or 0
|
||||
instance.biomesVerticalScattering = def.biomesVerticalScattering or 0
|
||||
|
||||
instance:getTriaglesFromFile(
|
||||
def.triangulationLandscapeFile,
|
||||
'landscape',
|
||||
def.landscapeMapScale)
|
||||
instance:getTriaglesFromFile(
|
||||
def.triangulationTempFile,
|
||||
'temp',
|
||||
def.tempMapScale)
|
||||
instance:getTriaglesFromFile(
|
||||
def.triangulationHumidityFile,
|
||||
'humidity',
|
||||
def.humidityMapScale)
|
||||
|
||||
instance:initTrianglesNeighbours()
|
||||
|
||||
return instance
|
||||
end
|
||||
@@ -114,6 +216,18 @@ end
|
||||
---A category of noise that affects specific map generation objects.
|
||||
---@alias MapGen.Layer.Category 'landscape'|'temp'|'humidity'
|
||||
|
||||
---Adds a triangle to the `Layer.trianglesList`
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param triangle MapGen.Triangle
|
||||
function Layer:addTriangle(category, triangle)
|
||||
local triangles = self.trianglesList[category]
|
||||
if triangles ~= nil then
|
||||
triangles[triangle.id] = triangle
|
||||
else
|
||||
error(('There is no peak category named %s.'):format(category))
|
||||
end
|
||||
end
|
||||
|
||||
---Adds a peak to the `Layer.peaksList`
|
||||
---@param category MapGen.Layer.Category
|
||||
---@param peak MapGen.Peak
|
||||
@@ -134,14 +248,11 @@ end
|
||||
---@param pos Position Coordinate of the desired peak.
|
||||
---@return MapGen.Peak?
|
||||
function Layer:getPeakByPos(category, pos)
|
||||
pos = vector.new(pos)
|
||||
local positionStr = core.pos_to_string(pos)
|
||||
|
||||
---@param peak MapGen.Peak
|
||||
for _, peak in ipairs(self.peaksList[category]) do
|
||||
if peak:getPeakPos() == pos then
|
||||
return peak
|
||||
end
|
||||
end
|
||||
local peak = self.peaksByPos[category][positionStr]
|
||||
|
||||
return peak
|
||||
end
|
||||
|
||||
---Adds a NoiseParams color to the `Layer.noisesParamsByColor`
|
||||
|
||||
@@ -63,6 +63,7 @@ end
|
||||
---@alias MapGen.Triangle.Neighbours {t1:MapGen.Triangle?,t2:MapGen.Triangle?,t3:MapGen.Triangle?}
|
||||
|
||||
---@class MapGen.Triangle
|
||||
---@field id integer
|
||||
---@field p1 MapGen.Peak
|
||||
---@field p2 MapGen.Peak
|
||||
---@field p3 MapGen.Peak
|
||||
@@ -72,8 +73,9 @@ end
|
||||
---@field h1 MapGen.Triangulation.Edge
|
||||
---@field h2 MapGen.Triangulation.Edge
|
||||
---@field h3 MapGen.Triangulation.Edge
|
||||
---@field neighbours MapGen.Triangle.Neighbours
|
||||
---@field color ColorString?
|
||||
---@field neighboursIDs integer[]
|
||||
---@field neighbours MapGen.Triangle.Neighbours
|
||||
---@field color ColorString?
|
||||
local Triangle = {
|
||||
neighbours = {}
|
||||
}
|
||||
@@ -82,7 +84,7 @@ local Triangle = {
|
||||
---@param p2 MapGen.Peak
|
||||
---@param p3 MapGen.Peak
|
||||
---@return MapGen.Triangle
|
||||
function Triangle:new(p1, p2, p3)
|
||||
function Triangle:new(id, p1, p2, p3, neighboursIDs)
|
||||
assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s")
|
||||
:format(tostring(p1), tostring(p2), tostring(p3)))
|
||||
|
||||
@@ -96,6 +98,7 @@ function Triangle:new(p1, p2, p3)
|
||||
end
|
||||
|
||||
local instance = getInstance(self, {
|
||||
id = id,
|
||||
p1 = p1,
|
||||
p2 = p2,
|
||||
p3 = p3,
|
||||
@@ -105,7 +108,8 @@ function Triangle:new(p1, p2, p3)
|
||||
e1 = Edge:new(p1, p2),
|
||||
e2 = Edge:new(p2, p3),
|
||||
e3 = Edge:new(p3, p1),
|
||||
color = color
|
||||
neighboursIDs = neighboursIDs,
|
||||
color = color,
|
||||
},
|
||||
{
|
||||
__tostring = self.toString,
|
||||
|
||||
Reference in New Issue
Block a user