Mapgen: write SuperUltraSigmaMapgen part 8.1

This commit is contained in:
Van
2025-11-26 18:39:31 +03:00
parent e61ea85c9a
commit b18c8d0276
13 changed files with 202 additions and 134 deletions

View File

@@ -27,14 +27,14 @@ local Layer = require('MapGen.Layer')
---@type MapGen.Peak ---@type MapGen.Peak
local Peak = require('MapGen.Peak') local Peak = require('MapGen.Peak')
---@type MapGen.Layer.Biome ---@type MapGen.Biome
local Biome = require('MapGen.Layer.Biome') local Biome = require('MapGen.Biome')
---@type MapGen.Triangulation ---@type MapGen.Triangulation
local Triangulation = require('MapGen.Triangulation') local Triangulation = require('MapGen.Triangulation')
---@type MapGen.Layer.Cavern ---@type MapGen.Cavern
local Cavern = require('MapGen.Layer.Cavern') local Cavern = require('MapGen.Cavern')
---@class MapGen ---@class MapGen
---@field layersByName table<string, MapGen.Layer> ---@field layersByName table<string, MapGen.Layer>
@@ -53,6 +53,7 @@ local MapGen = {
} }
---@param nodeIDs table<string, number> ---@param nodeIDs table<string, number>
---@param waterLevel number
---@return MapGen ---@return MapGen
function MapGen:new(nodeIDs, waterLevel) function MapGen:new(nodeIDs, waterLevel)
---@type MapGen ---@type MapGen
@@ -90,9 +91,9 @@ end
---Mark the world area between two heights as a `MapGen.Layer`. ---Mark the world area between two heights as a `MapGen.Layer`.
--- ---
---Note: layers must not overlap. ---Note: layers must not overlap.
---@param name string ---@param name string The name that will be assigned to the layer. The name must be unique for each layer.
---@param minY number ---@param minY number Minimum layer generation height.
---@param maxY number ---@param maxY number Maximum layer generation height.
function MapGen:RegisterLayer(name, minY, maxY) function MapGen:RegisterLayer(name, minY, maxY)
-- Layers with the same name cannot exist. -- Layers with the same name cannot exist.
assert(self.layersByName[name] == nil, ('A layer named `%s` is already registered.'):format(name)) assert(self.layersByName[name] == nil, ('A layer named `%s` is already registered.'):format(name))
@@ -112,14 +113,13 @@ function MapGen:RegisterLayer(name, minY, maxY)
end) end)
end end
-- TODO: переписать описание функции ---Mark a world point as `MapGen.Peak` and assign it a specific noise that will affect the generation.
---Mark a peak of the world as a cubic peak by two opposite vertices.
--- ---
---Peaks must be included in layers and may overlap each other. ---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation.
---@param layerName string The name of the layer in which the new peak will be included. ---@param layerName string The name of the layer in which the new peak will be included.
---@param peakPos vector ---@param peakPos vector The coordinate where the peak will be placed.
---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation ---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation
---@param groups table<string, number>? TODO: описание ---@param groups table<string, number>? Peak groups that may affect processing.
function MapGen:registerPeak(layerName, peakPos, multinoise, groups) function MapGen:registerPeak(layerName, peakPos, multinoise, groups)
---@type MapGen.Layer ---@type MapGen.Layer
local layer = self.layersByName[layerName] local layer = self.layersByName[layerName]
@@ -130,22 +130,22 @@ function MapGen:registerPeak(layerName, peakPos, multinoise, groups)
error('Invalid layer: ' .. layerName) error('Invalid layer: ' .. layerName)
end end
--TODO: добавить проверку, что координата ячейки не совпадают --TODO: добавить проверку, что координата пиков не совпадают
--TODO: Добавить проверку, что координата ячейки не выходит за границы слоя
---@type MapGen.Peak ---@type MapGen.Peak
local peak = Peak:new(peakPos, multinoise, groups) local peak = Peak:new(peakPos, multinoise, groups)
layer:addPeak(peak) layer:addPeak(peak)
return peak --TODO: временно?
end end
---TODO: описание ---Mark a world point as `MapGen.Peak` and assign it a specific noise 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. This won't cause any errors, but it might ruin the generation.
---@param layerName string The name of the layer in which the new peak will be included. ---@param layerName string The name of the layer in which the new peak will be included.
---@param peakPos vector ---@param peakPos vector The coordinate where the peak will be placed.
---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation ---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation.
---@param groups table<string, number>? TODO: описание ---@param groups table<string, number>? Peak groups that may affect processing.
function MapGen:register2DPeak(layerName, peakPos, multinoise, groups) function MapGen:register2DPeak(layerName, peakPos, multinoise, groups)
local _peakPos = table.copy_with_metatables(peakPos) local _peakPos = table.copy_with_metatables(peakPos)
_peakPos.y = 0 _peakPos.y = 0
@@ -159,21 +159,24 @@ function MapGen:register2DPeak(layerName, peakPos, multinoise, groups)
self:registerPeak(layerName, _peakPos, multinoise, groups) self:registerPeak(layerName, _peakPos, multinoise, groups)
end end
---TODO: описание ---Mark the world points as `MapGen.Peak` and assign them all a specific noise that will affect generation.
---@param layerName string The name of the layer in which the new peak will be included. ---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field.
---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation ---
---@param peakPoses vector[] ---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation.
---@param groups table<string, number>? TODO: описание ---@param layerName string The name of the layer in which the new peaks will be included.
---@param multinoise MapGen.Peak.MultinoiseParams The noise that will be assigned to each peak and will affect the map generation.
---@param peakPoses vector[] Set the coordinates where the peaks will be placed.
---@param groups table<string, number>? Peak groups that may affect processing.
function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups) function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups)
for _, peakPos in ipairs(peakPoses) do for _, peakPos in ipairs(peakPoses) do
self:register2DPeak(layerName, peakPos, multinoise, groups) self:register2DPeak(layerName, peakPos, multinoise, groups)
end end
end end
---TODO: описание ---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`.
---@param layerName string ---@param layerName string The name of the layer in which the new biome will be included.
---@param biomeName string ---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.
---@param def MapGen.Layer.BiomeDef ---@param def MapGen.BiomeDef Biome description table, see details `MapGen.BiomeDef`
function MapGen:registerBiome(layerName, biomeName, def) function MapGen:registerBiome(layerName, biomeName, def)
local layer = self.layersByName[layerName] local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
@@ -181,14 +184,13 @@ function MapGen:registerBiome(layerName, biomeName, def)
local biome = Biome:new(biomeName, def) local biome = Biome:new(biomeName, def)
layer.biomesByName[biomeName] = biome layer:addBiome(biomeName, biome)
table.insert(layer.biomesList, biome)
end end
---TODO: описание ---Mark the world area between two heights as a `MapGen.Cavern` and assign the noise that the cave will be generated from.
---@param layerName string ---@param layerName string The name of the layer in which the new cavern will be included.
---@param cavernName string ---@param cavernName string The name to be assigned to the cavern. The name must be unique for each cavern.
---@param def MapGen.Layer.CavernDef ---@param def MapGen.CavernDef Cavern description table, see details `MapGen.CavernDef`
function MapGen:registerCavern(layerName, cavernName, def) function MapGen:registerCavern(layerName, cavernName, def)
local layer = self.layersByName[layerName] local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
@@ -196,15 +198,14 @@ function MapGen:registerCavern(layerName, cavernName, def)
local cavern = Cavern:new(cavernName, def) local cavern = Cavern:new(cavernName, def)
layer.cavernsByName[cavernName] = cavern layer:addCavern(cavernName, cavern)
table.insert(layer.cavernsList, cavern)
end end
-- --- INITIALIZATION METHODS --- -- --- INITIALIZATION METHODS ---
---Initializing layer triangulation. ---Divide all layers into triangles whose vertices are the `Mapgen.Peak` belonging to these layers.
function MapGen:initLayersTriangultaion() function MapGen:initLayersTriangultaion()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
@@ -213,6 +214,15 @@ function MapGen:initLayersTriangultaion()
end end
end end
---Initialization of the biome diagram using the Voronoi method.
function MapGen:initBiomesDiagram()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
layer:initBiomesDiagram()
end
end
---Initialize peak's noises. This should be called after the map object is loaded. ---Initialize peak's noises. This should be called after the map object is loaded.
function MapGen:initPeaksMultinoise() function MapGen:initPeaksMultinoise()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
@@ -228,7 +238,7 @@ function MapGen:initPeaksMultinoise()
self.peaksMultinoiseInitialized = true self.peaksMultinoiseInitialized = true
end end
---TODO: описание ---Initialize caverns's noises. This should be called after the map object is loaded.
function MapGen:initCavernsNoise() function MapGen:initCavernsNoise()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
@@ -243,18 +253,11 @@ function MapGen:initCavernsNoise()
self.cavernsNoiseInitialized = true self.cavernsNoiseInitialized = true
end end
---Initialization of the biome diagram using the Voronoi method.
function MapGen:initBiomesDiagram()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
layer:initBiomesDiagram()
end
end
-- --- GENERATION METHODS --- -- --- GENERATION METHODS ---
---Returns `true` if there is a cavern at the specified coordinates.
---@param layer MapGen.Layer ---@param layer MapGen.Layer
---@param x number ---@param x number
---@param y number ---@param y number
@@ -270,6 +273,8 @@ local function isCavern(layer, x, y, z)
return false return false
end end
---The core logic of world generation. Determines which node type (water, rock, soil, air)
---and which biome will be generated at the specified coordinates.
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param layer MapGen.Layer ---@param layer MapGen.Layer
---@param height number ---@param height number
@@ -290,7 +295,7 @@ local function generateNode(mapGenerator, layer, height, temp, humidity, data, i
humidity = mathMin(100, mathMax(0, humidity + m)) humidity = mathMin(100, mathMax(0, humidity + m))
--_height = mathMin(layer.maxY, mathMax(layer.minY, y + m)) --_height = mathMin(layer.maxY, mathMax(layer.minY, y + m))
---@type MapGen.Layer.Biome ---@type MapGen.Biome
local biome = layer.biomesDiagram[y][mathRound(temp)][mathRound(humidity)] local biome = layer.biomesDiagram[y][mathRound(temp)][mathRound(humidity)]
if y > height and y > waterLevel then if y > height and y > waterLevel then
@@ -309,10 +314,15 @@ local function generateNode(mapGenerator, layer, height, temp, humidity, data, i
end end
else else
data[index] = ids.air data[index] = ids.air
-- TODO: добавить error
Logger.warningLog('If you see this error, it means that the map generator does'..
'not handle all cases of generation on coordinates: %s, %s, %s.', x, y, z)
end end
end end
---Calculates a weight for a coordinate using the projection onto
---the triangle's height and specifying the distance to the peak
---for which the weight is calculated.
---@param height MapGen.Triangulation.Edge ---@param height MapGen.Triangulation.Edge
---@param x number ---@param x number
---@param y number ---@param y number
@@ -336,6 +346,8 @@ local function calcWeight(height, x, y, z)
return 1 - vector.distance(pos2, posP) / height:length() return 1 - vector.distance(pos2, posP) / height:length()
end end
---Returns `true` if the point is within the triangle's region.
---The barycentric coordinate method is used.
---@param px number ---@param px number
---@param pz number ---@param pz number
---@param triangle MapGen.Triangle ---@param triangle MapGen.Triangle
@@ -362,6 +374,7 @@ local function pointInTriangle(px, pz, triangle)
return (u >= 0) and (v >= 0) and (u + v <= 1) return (u >= 0) and (v >= 0) and (u + v <= 1)
end end
---Returns the weighted values of 2d noises.
---@param triangle MapGen.Triangle ---@param triangle MapGen.Triangle
---@param x number ---@param x number
---@param y number ---@param y number
@@ -542,6 +555,7 @@ function MapGen:getNoises3dValues(layer, x, y, z)
end end
--]] --]]
---Generator handler for specific coordinates.
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param data number[] ---@param data number[]
---@param index number ---@param index number
@@ -572,6 +586,7 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end end
---Chunk generation handler.
---@param voxelManip VoxelManip ---@param voxelManip VoxelManip
---@param minPos table ---@param minPos table
---@param maxPos table ---@param maxPos table

View File

@@ -1,4 +1,4 @@
---@class MapGen.Layer.Biome ---@class MapGen.Biome
---@field name string ---@field name string
---@field tempPoint number ---@field tempPoint number
---@field humidityPoint number ---@field humidityPoint number
@@ -6,27 +6,30 @@
---@field maxY number ---@field maxY number
---@field groundNodes MapGen.Biome.GroundNodes ---@field groundNodes MapGen.Biome.GroundNodes
---@field soilHeight number ---@field soilHeight number
---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number) ---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number) ---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number) ---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)
local Biome = {} local Biome = {}
---@class MapGen.Layer.BiomeDef ---Definition table for the `MapGen.Biome`.
---
---**Only for EmmyLua.**
---@class MapGen.BiomeDef
---@field tempPoint number ---@field tempPoint number
---@field humidityPoint number ---@field humidityPoint number
---@field minY number ---@field minY number
---@field maxY number ---@field maxY number
---@field groundNodes MapGen.Biome.GroundNodes ---@field groundNodes MapGen.Biome.GroundNodes
---@field soilHeight number ---@field soilHeight number
---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number)? ---@field generateRock fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number)? ---@field generateSoil fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Layer.Biome, data:number[], index:number, x:number, y:number, z:number)? ---@field generateBottom fun(mapGenerator:MapGen, biome:MapGen.Biome, data:number[], index:number, x:number, y:number, z:number)?
-- Default generate functions -- Default generate functions
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param biome MapGen.Layer.Biome ---@param biome MapGen.Biome
---@param data number[] ---@param data number[]
---@param index number ---@param index number
---@param x number ---@param x number
@@ -37,7 +40,7 @@ local defaultGenerateRock = function(mapGenerator, biome, data, index, x, y, z)
end end
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param biome MapGen.Layer.Biome ---@param biome MapGen.Biome
---@param data number[] ---@param data number[]
---@param index number ---@param index number
---@param x number ---@param x number
@@ -48,7 +51,7 @@ local defaultGenerateSoil = function(mapGenerator, biome, data, index, x, y, z)
end end
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param biome MapGen.Layer.Biome ---@param biome MapGen.Biome
---@param data number[] ---@param data number[]
---@param index number ---@param index number
---@param x number ---@param x number
@@ -60,25 +63,25 @@ end
---@param name string ---@param name string
---@param def MapGen.Layer.BiomeDef ---@param def MapGen.BiomeDef
---@return MapGen.Layer.Biome ---@return MapGen.Biome
function Biome:new(name, def) function Biome:new(name, def)
if def.generateRock == nil then if def.generateRock == nil then
Logger.warningLog('MapGen.Layer.Biome: The `%s` biome does not have a specified `generateRock()` function. The default function is used.', name) Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateRock()` function. The default function is used.', name)
def.generateRock = defaultGenerateRock def.generateRock = defaultGenerateRock
end end
if def.generateSoil == nil then if def.generateSoil == nil then
Logger.warningLog('MapGen.Layer.Biome: The `%s` biome does not have a specified `generateSoil()` function. The default function is used.', name) Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateSoil()` function. The default function is used.', name)
def.generateSoil = defaultGenerateSoil def.generateSoil = defaultGenerateSoil
end end
if def.generateBottom == nil then if def.generateBottom == nil then
Logger.warningLog('MapGen.Layer.Biome: The `%s` biome does not have a specified `generateBottom()` function. The default function is used.', name) Logger.warningLog('MapGen.Biome: The `%s` biome does not have a specified `generateBottom()` function. The default function is used.', name)
def.generateBottom = defaultGenerateBottom def.generateBottom = defaultGenerateBottom
end end
---@type MapGen.Layer.Biome ---@type MapGen.Biome
local instance = setmetatable({ local instance = setmetatable({
name = name, name = name,
tempPoint = def.tempPoint, tempPoint = def.tempPoint,

View File

@@ -1,5 +1,6 @@
--- Only for EmmyLua --- Only for EmmyLua
---**Only for EmmyLua.**
---@class MapGen.Biome.GroundNodes ---@class MapGen.Biome.GroundNodes
---@field turf string The soil covering the surface of the biome ---@field turf string The soil covering the surface of the biome
---@field soil string The soil beneath the turf. The height of this layer depends on `Biome.soilHeight` ---@field soil string The soil beneath the turf. The height of this layer depends on `Biome.soilHeight`

View File

@@ -1,7 +1,7 @@
local mathMin, mathMax, mathAbs local mathMin, mathMax, mathAbs
= math.min, math.max, math.abs = math.min, math.max, math.abs
---@class MapGen.Layer.Cavern ---@class MapGen.Cavern
---@field name string ---@field name string
---@field minY number ---@field minY number
---@field maxY number ---@field maxY number
@@ -12,10 +12,10 @@ local mathMin, mathMax, mathAbs
---@field getGroups fun():table<string, number> ---@field getGroups fun():table<string, number>
local Cavern = {} local Cavern = {}
---Definition table for the `MapGen.Layer.Cavern`. ---Definition table for the `MapGen.Cavern`.
--- ---
---**Only for EmmyLua.** ---**Only for EmmyLua.**
---@class MapGen.Layer.CavernDef ---@class MapGen.CavernDef
---@field minY number ---@field minY number
---@field maxY number ---@field maxY number
---@field noiseParams NoiseParams ---@field noiseParams NoiseParams
@@ -24,8 +24,8 @@ local Cavern = {}
---@field groups table<string, number>? ---@field groups table<string, number>?
---@param name string ---@param name string
---@param def MapGen.Layer.CavernDef ---@param def MapGen.CavernDef
---@return MapGen.Layer.Cavern ---@return MapGen.Cavern
function Cavern:new(name, def) function Cavern:new(name, def)
assert(def.maxY > def.minY, 'maxY there should be more minY') assert(def.maxY > def.minY, 'maxY there should be more minY')
assert(def.smoothDistance < mathAbs(def.maxY - def.minY)/2, 'The smoothing distance cannot be greater than half the distance between minY and maxY') assert(def.smoothDistance < mathAbs(def.maxY - def.minY)/2, 'The smoothing distance cannot be greater than half the distance between minY and maxY')
@@ -49,7 +49,7 @@ function Cavern:new(name, def)
_groups = def.groups _groups = def.groups
end end
---@type MapGen.Layer.Cavern ---@type MapGen.Cavern
local instance = setmetatable({ local instance = setmetatable({
name = name, name = name,
minY = def.minY, minY = def.minY,
@@ -89,6 +89,7 @@ local function calcCavernWeight(y, smoothDistance, minY, maxY)
return mathMax(0, mathMin(maxDist / smoothDistance, minDist / smoothDistance, 1)) return mathMax(0, mathMin(maxDist / smoothDistance, minDist / smoothDistance, 1))
end end
---Returns `true` if there is a cavern at the specified coordinates.
---@param x number ---@param x number
---@param y number ---@param y number
---@param z number ---@param z number

View File

@@ -0,0 +1,15 @@
---**NOT IMPLEMENTED**
---@class MapGen.Decoration
local Decoration = {}
---@return MapGen.Decoration
function Decoration:new()
---@type MapGen.Decoration
local instance = setmetatable({
}, {__index = self})
return instance
end
return Decoration

View File

@@ -1,15 +1,13 @@
local mathSqrt = math.sqrt
---@class MapGen.Layer ---@class MapGen.Layer
---@field name string ---@field name string
---@field minY number ---@field minY number
---@field maxY number ---@field maxY number
---@field peaksList MapGen.Peak[] ---@field peaksList MapGen.Peak[]
---@field biomesByName table<string, MapGen.Layer.Biome> ---@field biomesByName table<string, MapGen.Biome>
---@field biomesList MapGen.Layer.Biome[] ---@field biomesList MapGen.Biome[]
---@field biomesDiagram table ---@field biomesDiagram table
---@field cavernsByName table<string, MapGen.Layer.Cavern> ---@field cavernsByName table<string, MapGen.Cavern>
---@field cavernsList MapGen.Layer.Cavern[] ---@field cavernsList MapGen.Cavern[]
---@field trianglesList MapGen.Triangle[] ---@field trianglesList MapGen.Triangle[]
---@field tetrahedronsList MapGen.Tetrahedron[] ---@field tetrahedronsList MapGen.Tetrahedron[]
local Layer = { local Layer = {
@@ -38,14 +36,31 @@ function Layer:new(name, minY, maxY)
return instance return instance
end end
---Adds a peak to the `Layer.peaksList`
---@param peak MapGen.Peak ---@param peak MapGen.Peak
function Layer:addPeak(peak) function Layer:addPeak(peak)
table.insert(self.peaksList, peak) table.insert(self.peaksList, peak)
end end
---Adds a biome to the `Layer.biomesByName` & `Layer.biomesList`
---@param biomeName string
---@param biome MapGen.Biome
function Layer:addBiome(biomeName, biome)
self.biomesByName[biomeName] = biome
table.insert(self.biomesList, self)
end
---Adds a cavern to the `Layer.cavernsByName` & `Layer.cavernsList`
---@param cavernName string
---@param cavern MapGen.Cavern
function Layer:addCavern(cavernName, cavern)
self.cavernsByName[cavernName] = cavern
table.insert(self.cavernsList, cavern)
end
---@param height number ---@param height number
---@param biomesList MapGen.Layer.Biome[] ---@param biomesList MapGen.Biome[]
---@return string[], MapGen.Layer.Biome[] ---@return string[], MapGen.Biome[]
local function getBiomesNamesByHeight(height, biomesList) local function getBiomesNamesByHeight(height, biomesList)
local biomesNames = {} local biomesNames = {}
local biomes = {} local biomes = {}
@@ -90,7 +105,7 @@ function Layer:initBiomesDiagram()
local minDistance = math.huge local minDistance = math.huge
local closestBiome = nil local closestBiome = nil
---@param biome MapGen.Layer.Biome ---@param biome MapGen.Biome
for _, biome in ipairs(biomes) do for _, biome in ipairs(biomes) do
local distance = (temp - biome.tempPoint)^2 + (humidity - biome.humidityPoint)^2 local distance = (temp - biome.tempPoint)^2 + (humidity - biome.humidityPoint)^2

View File

@@ -1,14 +0,0 @@
---@class MapGen.Layer.Decoration
local Decoration = {}
---@return MapGen.Layer.Decoration
function Decoration:new(tempPoint, humidityPoint, groundNodes, soilHeight)
---@type MapGen.Layer.Decoration
local instance = setmetatable({
}, {__index = self})
return instance
end
return Decoration

View File

@@ -9,6 +9,8 @@ local Peak = {
id = 0, id = 0,
} }
-- --- Helpers functions ---
---@param multinoiseParams MapGen.Peak.MultinoiseParams ---@param multinoiseParams MapGen.Peak.MultinoiseParams
---@return MapGen.Peak.Multinoise ---@return MapGen.Peak.Multinoise
local function multinoiseParamsToMultinoise(multinoiseParams) local function multinoiseParamsToMultinoise(multinoiseParams)
@@ -23,6 +25,10 @@ local function multinoiseParamsToMultinoise(multinoiseParams)
return multinoise return multinoise
end end
-- --- Class registration ---
---@param peakPos vector ---@param peakPos vector
---@param multinoiseParams MapGen.Peak.MultinoiseParams ---@param multinoiseParams MapGen.Peak.MultinoiseParams
---@param groups? table<string, number> ---@param groups? table<string, number>
@@ -62,12 +68,20 @@ function Peak:new(peakPos, multinoiseParams, groups)
end end
function instance:getMultinoise() function instance:getMultinoise()
if not table.is_empty(_multinoise) then
error('Attempt to get `MapGen.Triangulation.FakePeak` noise (empty noise).')
end
return _multinoise return _multinoise
end end
function instance:initMultinoise() function instance:initMultinoise()
if not table.is_empty(_multinoise) then if not table.is_empty(_multinoise) then
minetest.log('warning', 'Multinoise has already been initialized. Re-initialization is not recommended.') Logger.warningLog('Multinoise has already been initialized. Re-initialization is not recommended.')
end
if not table.is_empty(_multinoiseParams) then
error('Attempt to initialize `MapGen.Triangulation.FakePeak` noise (noise parameter empty).')
end end
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams) _multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
@@ -87,9 +101,10 @@ function Peak:toString()
return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z) return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z)
end end
---Returns true if the peaks are equivalent.
---@param other MapGen.Peak ---@param other MapGen.Peak
---@return boolean ---@return boolean
function Peak:eq( other ) function Peak:eq(other)
local pos1 = self:getPeakPos() local pos1 = self:getPeakPos()
local pos2 = other:getPeakPos() local pos2 = other:getPeakPos()

View File

@@ -1,5 +1,6 @@
---For EmmyLua only ---For EmmyLua only
---**For EmmyLua only**
---@class MapGen.Peak.Multinoise ---@class MapGen.Peak.Multinoise
---@field landscapeNoise ValueNoise ---@field landscapeNoise ValueNoise
---@field tempNoise ValueNoise ---@field tempNoise ValueNoise

View File

@@ -1,5 +1,6 @@
---For EmmyLua only ---For EmmyLua only
---**For EmmyLua only**
---@class MapGen.Peak.MultinoiseParams ---@class MapGen.Peak.MultinoiseParams
---@field landscapeNoise NoiseParams ---@field landscapeNoise NoiseParams
---@field tempNoise NoiseParams ---@field tempNoise NoiseParams

View File

@@ -9,7 +9,7 @@ local Edge = require('MapGen.Triangulation.Edge')
---@type MapGen.Triangulation.FakePeak ---@type MapGen.Triangulation.FakePeak
local FakePeak = require('MapGen.Triangulation.FakePeak') local FakePeak = require('MapGen.Triangulation.FakePeak')
-- Helpers functions -- -- --- Helpers functions ---
---Triangle semi-perimeter by Heron's formula ---Triangle semi-perimeter by Heron's formula
---@param a number ---@param a number
@@ -50,7 +50,9 @@ local function isFlatAngle(p1, p2, p3)
return mathAbs(dot + 1.0) < 0 return mathAbs(dot + 1.0) < 0
end end
-- Class registration --
-- --- Class registration ---
---@class MapGen.Triangle ---@class MapGen.Triangle
---@field p1 MapGen.Peak ---@field p1 MapGen.Peak
@@ -102,12 +104,14 @@ function Triangle:toString()
) )
end end
---Returns true if the triangles are equivalent.
---@param other MapGen.Triangle ---@param other MapGen.Triangle
---@return boolean ---@return boolean
function Triangle:eq( other ) function Triangle:eq( other )
return self.p1 == other.p1 and self.p2 == other.p2 and self.p3 == other.p3 return self.p1 == other.p1 and self.p2 == other.p2 and self.p3 == other.p3
end end
---Returns true if the triangles are similar.
---@param other MapGen.Triangle ---@param other MapGen.Triangle
---@return boolean ---@return boolean
function Triangle:same(other) function Triangle:same(other)
@@ -116,7 +120,7 @@ function Triangle:same(other)
or (self.p1 == other.p3 and self.p2 == other.p1 and self.p3 == other.p2) or (self.p1 == other.p3 and self.p2 == other.p1 and self.p3 == other.p2)
end end
---Returns the coordinates of the center ---Returns the coordinates of the triangle's center.
---@return vector ---@return vector
function Triangle:getCenter() function Triangle:getCenter()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()
@@ -126,7 +130,7 @@ function Triangle:getCenter()
return (pos1 + pos2 + pos3) / 3 return (pos1 + pos2 + pos3) / 3
end end
---Returns the area ---Returns the area of ​​a triangle.
---@return number ---@return number
function Triangle:getArea() function Triangle:getArea()
local a, b, c = self:getSidesLength() local a, b, c = self:getSidesLength()
@@ -134,8 +138,7 @@ function Triangle:getArea()
return (quatCross(a, b, c) / 4) return (quatCross(a, b, c) / 4)
end end
---Returns the length of the perpendicular ---Returns the perpendicular from the vertex Peak1 to the line Peak2-Peak3.
---from Peak1 to the line Peak2-Peak3
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@param p3 MapGen.Peak ---@param p3 MapGen.Peak
@@ -158,8 +161,10 @@ function Triangle:getHeight(p1, p2, p3)
return Edge:new(p1, FakePeak:new(posP)) return Edge:new(p1, FakePeak:new(posP))
end end
---Returns the length of the perpendicular ---Returns the perpendicular from the vertex Peak1 to the line Peak2-Peak3.
---from Peak1 to the line Peak2-Peak3 ---
---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ.
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@param p3 MapGen.Peak ---@param p3 MapGen.Peak
@@ -192,10 +197,10 @@ function Triangle:getSidesLength()
return self.e1:length(), self.e2:length(), self.e3:length() return self.e1:length(), self.e2:length(), self.e3:length()
end end
---Returns the length of the edges ---Returns the length of the edges.
--- ---
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ ---that is, the calculation is performed on the projection of the triangle XZ.
---@return number, number, number ---@return number, number, number
function Triangle:getSidesLength2D() function Triangle:getSidesLength2D()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()
@@ -209,10 +214,10 @@ function Triangle:getSidesLength2D()
return (pos2 - pos1):length(), (pos3 - pos2):length(), (pos1 - pos3):length() return (pos2 - pos1):length(), (pos3 - pos2):length(), (pos1 - pos3):length()
end end
---Returns the coordinates of the circumcircle center ---Returns the coordinates of the circumcircle center.
--- ---
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ ---that is, the calculation is performed on the projection of the triangle XZ.
---@return vector ---@return vector
function Triangle:getCircumCenter2D() function Triangle:getCircumCenter2D()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()
@@ -232,10 +237,10 @@ function Triangle:getCircumCenter2D()
return vector.new(x / D, 0, z / D) return vector.new(x / D, 0, z / D)
end end
---Returns the radius of the circumcircle ---Returns the radius of the circumcircle.
--- ---
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ ---that is, the calculation is performed on the projection of the triangle XZ.
---@return number ---@return number
function Triangle:getCircumRadius2D() function Triangle:getCircumRadius2D()
local a, b, c = self:getSidesLength2D() local a, b, c = self:getSidesLength2D()
@@ -243,10 +248,10 @@ function Triangle:getCircumRadius2D()
return ((a * b * c) / quatCross(a, b, c)) return ((a * b * c) / quatCross(a, b, c))
end end
---Returns the coordinates of the circumcircle center and its radius ---Returns the coordinates of the circumcircle center and its radius.
--- ---
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ ---that is, the calculation is performed on the projection of the triangle XZ.
---@return vector, number ---@return vector, number
function Triangle:getCircumCircle2D() function Triangle:getCircumCircle2D()
local c = self:getCircumCenter2D() local c = self:getCircumCenter2D()
@@ -255,10 +260,10 @@ function Triangle:getCircumCircle2D()
return c, r return c, r
end end
---Checks if a given point lies into the triangle circumcircle ---Checks if a given point lies into the triangle circumcircle.
--- ---
---Note: This function does not take into account the Y-coordinate, ---Note: This function does not take into account the Y-coordinate,
---that is, the calculation is performed on the projection of the triangle XZ ---that is, the calculation is performed on the projection of the triangle XZ.
---@param p MapGen.Peak ---@param p MapGen.Peak
---@return boolean ---@return boolean
function Triangle:inCircumCircle2D(p) function Triangle:inCircumCircle2D(p)

View File

@@ -7,6 +7,7 @@ local Edge = {}
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@return MapGen.Triangulation.Edge ---@return MapGen.Triangulation.Edge
function Edge:new( p1, p2 ) function Edge:new( p1, p2 )
---@type MapGen.Triangulation.Edge
local instance = setmetatable({ local instance = setmetatable({
p1 = p1, p1 = p1,
p2 = p2 p2 = p2
@@ -26,12 +27,14 @@ function Edge:toString()
return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2))) return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2)))
end end
---Returns true if the edges are equivalent.
---@param other MapGen.Triangulation.Edge ---@param other MapGen.Triangulation.Edge
---@return boolean ---@return boolean
function Edge:eq(other) function Edge:eq(other)
return self.p1 == other.p1 and self.p2 == other.p2 return self.p1 == other.p1 and self.p2 == other.p2
end end
---Returns true if the edges are similar.
---@param other MapGen.Triangulation.Edge ---@param other MapGen.Triangulation.Edge
---@return boolean ---@return boolean
function Edge:same(other) function Edge:same(other)
@@ -39,11 +42,13 @@ function Edge:same(other)
or (self.p1 == other.p2 and self.p2 == other.p1) or (self.p1 == other.p2 and self.p2 == other.p1)
end end
---Returns the length of an edge.
---@return number ---@return number
function Edge:length() function Edge:length()
return (self.p1.getPeakPos() - self.p2.getPeakPos()):length() return (self.p1.getPeakPos() - self.p2.getPeakPos()):length()
end end
---Returns the vector of the point located in the middle of the edge.
---@return vector ---@return vector
function Edge:getMidPoint() function Edge:getMidPoint()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()

View File

@@ -4,11 +4,16 @@ local require = modInfo.require
---@type MapGen.Peak ---@type MapGen.Peak
local Peak = require('MapGen.Peak') local Peak = require('MapGen.Peak')
---Allows you to create a `MapGen.Peak` without specifying a noise parameter.
---
---**WARNING: MUST NOT BE PARTICIPATED IN GENERATION**
---@class MapGen.Triangulation.FakePeak : MapGen.Peak ---@class MapGen.Triangulation.FakePeak : MapGen.Peak
---@field id number ---@field id number
---@field getPeakPos fun():vector ---@field getPeakPos fun():vector
local FakePeak = Mod:getClassExtended(Peak, {}) local FakePeak = Mod:getClassExtended(Peak, {})
---Returns a fake `MapGen.Peak` that has no noise parameters.
---Note that the initialized peak should not be used in generation.
---@param peakPos vector ---@param peakPos vector
---@return MapGen.Peak ---@return MapGen.Peak
function FakePeak:new(peakPos) function FakePeak:new(peakPos)