MapGen: add post-processing generation in biomes. Close #30

This commit is contained in:
Van
2026-03-05 21:46:28 +03:00
parent efcbc909e0
commit 7d49bbf535
4 changed files with 139 additions and 50 deletions

View File

@@ -6,6 +6,8 @@ local getInstance = Class.getInstance
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
---@alias MapGen.factors {height:number, temp:number, humidity:number}
---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, noiseValue:number?}
---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
@@ -649,26 +651,58 @@ end
-- --- GENERATION METHODS ---
---The core logic of world generation. Determines which node type (water, rock, soil, air)
---and which biome will be generated at the specified coordinates.
---@alias MapGen.nodeType 'AIR'|'WATER'|'CAVERN_AIR'|'TURF'|'SOIL'|'ROCK'|'BOTTOM'
---@alias MapGen.postProcessingData {biome:MapGen.Biome, mapGenerator:MapGen, factors:MapGen.factors, data:number[], index:number, x:number, y:number, z:number, nodeType:MapGen.nodeType}
---@type table<number, MapGen.postProcessingData>
local postProcessing = {}
---@param biome MapGen.Biome
---@param mapGenerator MapGen
---@param layer MapGen.Layer
---@param height number
---@param temp number
---@param humidity number
---@param factors MapGen.factors
---@param data number[]
---@param index number
---@param x integer
---@param y integer
---@param z integer
local function generateNode(mapGenerator, layer, height, temp, humidity, data, index, x, y, z)
---@param nodeType MapGen.nodeType
local function addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, nodeType)
if biome.postProcessing == nil then
return
end
postProcessing[index] = {
biome = biome,
mapGenerator = mapGenerator,
factors = factors,
data = data,
index = index,
x = x,
y = y,
z = z,
nodeType = nodeType
}
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 layer MapGen.Layer
---@param factors MapGen.factors
---@param data number[]
---@param index number
---@param x integer
---@param y integer
---@param z integer
local function generateNode(mapGenerator, layer, factors, data, index, x, y, z)
local ids = mapGenerator.nodeIDs
local waterLevel = layer.waterLevel
local height, temp, humidity = factors.height, factors.temp, factors.humidity
-- Creating a rougher biome boundary.
local scatteringGorizontal = layer.biomesGorizontalScattering or 0
local scatteringVertical = layer.biomesVerticalScattering or 0
local scattTemp = temp
local scattTemp = factors.temp
local scattHumidity = humidity
---@type number
local scattHeight = y
@@ -695,33 +729,39 @@ local function generateNode(mapGenerator, layer, height, temp, humidity, data, i
-- If the block is above surface height and above water level...
if y > height and y > waterLevel then
-- ... then airspace is generated.
biome:generateAir(mapGenerator, data, index, x, y, z)
biome:generateAir(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'AIR')
-- If the block is above the surface height BUT below the water level...
elseif y > height and y <= waterLevel then
-- ... then underwater space is generated.
biome:generateWater(mapGenerator, data, index, x, y, z)
biome:generateWater(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'WATER')
-- If the block is below the surface height...
elseif y <= height then
-- ... and a cavern is generated in the block...
if mapGenerator:isCavern(layer, x, y, z) then
-- ... then airspace is generated.
biome:generateCavernAir(mapGenerator, data, index, x, y, z)
biome:generateCavernAir(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'CAVERN_AIR')
-- ... or if the surface height is above the water level...
elseif height > waterLevel then
--- ... and the height of the block is equal to the height of the surface...
if y == height then
-- ... then turf is generated.
biome:generateTurf(mapGenerator, data, index, x, y, z)
biome:generateTurf(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'TURF')
-- ... or if the height of the block falls within the thickness of the soil layer...
elseif y > height - biome.soilHeight then
-- ... then soil is generated.
biome:generateSoil(mapGenerator, data, index, x, y, z)
biome:generateSoil(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'SOIL')
else
-- ... else e a stone is generated.
biome:generateRock(mapGenerator, data, index, x, y, z)
-- ... else a stone is generated.
biome:generateRock(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'ROCK')
end
-- ... or if the surface height is below the water level...
@@ -729,10 +769,12 @@ local function generateNode(mapGenerator, layer, height, temp, humidity, data, i
-- ... and if the height of the block falls within the thickness of the soil layer...
if y > height - biome.soilHeight then
-- ... then the river/ocean bottom is generated.
biome:generateBottom(mapGenerator, data, index, x, y, z)
biome:generateBottom(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'BOTTOM')
else
-- ... else a stone is generated.
biome:generateRock(mapGenerator, data, index, x, y, z)
biome:generateRock(mapGenerator, factors, data, index, x, y, z)
addPostProccessing(biome, mapGenerator, factors, data, index, x, y, z, 'ROCK')
end
end
else
@@ -1019,7 +1061,14 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
noiseTempValue = layer:calcTemp(noiseTempValue, y)
noiseHumidityValue = layer:calcHumidity(noiseHumidityValue, y)
generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
---@type MapGen.factors
local factors = {
height = noiseHeightValue,
temp = noiseTempValue,
humidity = noiseHumidityValue,
}
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
end
---Chunk generation handler.
@@ -1059,6 +1108,21 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
end
end
-- Post-proccesing generation
for _, ppdata in pairs(postProcessing) do
if ppdata.biome.postProcessing ~= nil then
ppdata.biome:postProcessing(ppdata.mapGenerator,
ppdata.factors,
ppdata.data,
ppdata.index,
ppdata.x,
ppdata.y,
ppdata.z,
ppdata.nodeType,
area)
end
end
-- We make changes back to LVM and recalculate light & liquids
voxelManip:set_data(data)