Add base biome support

This commit is contained in:
Van
2025-10-10 19:46:18 +03:00
parent 2bdc874b7d
commit eff948e210
5 changed files with 208 additions and 28 deletions

View File

@@ -30,6 +30,37 @@ local rocksNoiseParams ={
lacunarity = 4, lacunarity = 4,
} }
---DEBUG NOISES
--- TODO удалить, после реализации климата от регионов
---@type ValueNoise
local humidityNoise
---@type NoiseParams
humidityNoiseParams = {
offset = 50,
scale = 25,
spread = {x = 10, y = 10, z = 10},
seed = 47,
octaves = 2,
persistence = 0.6,
lacunarity = 2,
}
---@type ValueNoise
local tempNoise
---@type NoiseParams
tempNoiseParams = {
offset = 50,
scale = 25,
spread = {x = 10, y = 10, z = 10},
seed = 12,
octaves = 2,
persistence = 0.6,
lacunarity = 2,
}
-- --- End default noises --- -- --- End default noises ---
local modInfo = Mod.getInfo() local modInfo = Mod.getInfo()
@@ -41,15 +72,22 @@ local Layer = require("MapGen.Layer")
---@type MapGen.Region ---@type MapGen.Region
local Region = require("MapGen.Region") local Region = require("MapGen.Region")
---@type MapGen.Biome
local Biome = require("MapGen.Biome")
---@class MapGen ---@class MapGen
---@field layersByName table ---@field layersByName table
---@field layersList table ---@field layersList table
---@field biomesList table
---@field biomesDiagram table
---@field multinoiseInitialized boolean Is the fractal noise of the regions initialized? This is necessary for a one-time noise initialization. ---@field multinoiseInitialized boolean Is the fractal noise of the regions initialized? This is necessary for a one-time noise initialization.
---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. ---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, string> ---@field nodeIDs table<string, string>
local MapGen = { local MapGen = {
layersByName = {}, layersByName = {},
layersList = {}, layersList = {},
biomesList = {},
biomesDiagram = {},
multinoiseInitialized = false, multinoiseInitialized = false,
isRunning = false, isRunning = false,
} }
@@ -114,7 +152,7 @@ end
---@param layerName string The name of the layer in which the new region will be included. ---@param layerName string The name of the layer in which the new region will be included.
---@param minPos vector ---@param minPos vector
---@param maxPos vector ---@param maxPos vector
---@param multinoise table Noise that will be assigned to the region and that will influence map generation ---@param multinoise MapGen.Region.MultinoiseParams Noise that will be assigned to the region and that will influence map generation
---@param regionIs2D boolean? If true, the transmitted Y coordinate will be overwritten by the layer boundaries. ---@param regionIs2D boolean? If true, the transmitted Y coordinate will be overwritten by the layer boundaries.
---@param weightFactor number? The coefficient by which the reduction in the impact of regional noise on generation will be calculated. If 0, there will be no reduction. ---@param weightFactor number? The coefficient by which the reduction in the impact of regional noise on generation will be calculated. If 0, there will be no reduction.
function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D, weightFactor) function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D, weightFactor)
@@ -130,6 +168,7 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D
maxPos.y = layer.maxY maxPos.y = layer.maxY
end end
--TODO: добавить проверку, что координаты вершин региона не совпадают (регион не является линией толщиной в 1 блок)
--TODO: Добавить проверку, что регион не выходит за границы слоя или/и автоматически обрубать регион до слоя --TODO: Добавить проверку, что регион не выходит за границы слоя или/и автоматически обрубать регион до слоя
if weightFactor == nil then if weightFactor == nil then
@@ -157,6 +196,42 @@ function MapGen:initRegionsMultinoise()
self.multinoiseInitialized = true self.multinoiseInitialized = true
end end
---@param name string
---@param tempPoint number
---@param humidityPoint number
---@param groundNodes MapGen.Biome.GroundNodes
---@param soilHeight number
function MapGen:RegisterBiome(name, tempPoint, humidityPoint, groundNodes, soilHeight)
local biome = Biome:new(name, tempPoint, humidityPoint, groundNodes, soilHeight)
table.insert(self.biomesList, biome)
end
function MapGen:initBiomesDiagram()
local diagram = self.biomesDiagram
for temp = 0, 100 do
diagram[temp] = {}
for humidity = 0, 100 do
local minDistance = math.huge
local closestBiome = nil
---@param biome MapGen.Biome
for _, biome in ipairs(self.biomesList) do
local distance = (temp - biome.tempPoint)^2 + (humidity - biome.humidityPoint)^2
if distance < minDistance then
minDistance = distance
closestBiome = biome
end
end
diagram[temp][humidity] = closestBiome
end
end
end
---Calculate the weight for noise based on ---Calculate the weight for noise based on
---the distance of the point from the center of the region. ---the distance of the point from the center of the region.
--- ---
@@ -180,11 +255,15 @@ local function calculateWeight2D(minPos, maxPos, x, z, weightFactor)
local distZ = 1 - (mathAbs(z - centerZ) / (mathAbs((maxPos.z - minPos.z)) / 2))^weightFactor local distZ = 1 - (mathAbs(z - centerZ) / (mathAbs((maxPos.z - minPos.z)) / 2))^weightFactor
---@type number ---@type number
local weight = mathMin(distX, distZ) local weight = distX * distZ -- mathMin(distX, distZ)
return weight return weight
end end
local function generateSoil(biome, data, index, x, y, z)
data[index] = core.get_content_id(biome.groundNodes.turf) -- TODO: убрать тут get_content_id(), переместить его куда-то "выше"
end
local function generateRock(ids, data, index, x, y, z) local function generateRock(ids, data, index, x, y, z)
if rocksNoise == nil then if rocksNoise == nil then
@@ -211,11 +290,15 @@ local function generateRock(ids, data, index, x, y, z)
end end
end end
local function generateNode(mapGenerator, hight, data, index, x, y, z) local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, y, z)
local ids = mapGenerator.nodeIDs local ids = mapGenerator.nodeIDs
if y > hight and y >= 0 then local biome = mapGenerator.biomesDiagram[temp][humidity]
if y > hight and y > 0 then
data[index] = ids.air data[index] = ids.air
elseif y == hight and y > 0 then
generateSoil(biome, data, index, x, y, z)
elseif y < hight then elseif y < hight then
generateRock(ids, data, index, x, y, z) generateRock(ids, data, index, x, y, z)
else else
@@ -238,24 +321,54 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
oceanNoise = core.get_value_noise(oceanNoiseParams) oceanNoise = core.get_value_noise(oceanNoiseParams)
end end
local noiseHeightValue = oceanNoise:get_2d({x = x, y = z}) -- Default noise's values.
local noiseHeightValue = oceanNoise:get_2d({x = x, y = z})
local noiseTempValue = 0.0
local noiseHumidityValue = 0.0
local tempValuesCount = 0
local humidityValuesCount = 0
local regions = layer:getRegionsByPos(x, y, z) local regions = layer:getRegionsByPos(x, y, z)
-- The height noise values ​​of all regions that a node is part of are added together. -- The height noise values ​​of all regions that a node is part of are added together.
---@param region MapGen.Region ---@param region MapGen.Region
for _, region in ipairs(regions) do for _, region in ipairs(regions) do
local noise = region:getMultinoise().landscapeNoise local heightNoise = region:getMultinoise().landscapeNoise
local tempNoise = region:getMultinoise().tempNoise
local humidityNoise = region:getMultinoise().humidityNoise
if noise ~= nil then if heightNoise ~= nil then
-- The further a point is from the center of a region, the less noise affects it. -- The further a point is from the center of a region, the less noise affects it.
local weight = calculateWeight2D(region:getMinPos(), region:getMaxPos(), x, z, region.getWeightFactor()) local weight = calculateWeight2D(region:getMinPos(), region:getMaxPos(), x, z, region.getWeightFactor())
noiseHeightValue = noiseHeightValue + ( noise:get_2d(vector.new(x, z, 0)) * weight ) noiseHeightValue = noiseHeightValue + ( heightNoise:get_2d({x = x, y = z}) * weight )
end
if tempNoise ~= nil then
noiseTempValue = noiseTempValue + tempNoise:get_2d({x = x, y = z})
tempValuesCount = tempValuesCount + 1
end
if humidityNoise ~= nil then
noiseHumidityValue = noiseHumidityValue + humidityNoise:get_2d({x = x, y = z})
humidityValuesCount = humidityValuesCount + 1
end end
end end
generateNode(mapGenerator, noiseHeightValue, data, index, x, y, z) noiseHeightValue = mathRound(noiseHeightValue)
if tempValuesCount ~= 0 then
noiseTempValue = mathRound( noiseTempValue / tempValuesCount )
end
if humidityValuesCount ~= 0 then
noiseHumidityValue = mathRound( noiseHumidityValue / humidityValuesCount )
end
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end end
---@param minPos table ---@param minPos table
@@ -307,6 +420,8 @@ function MapGen:run()
self:onMapGenerated(...) self:onMapGenerated(...)
end) end)
self:initBiomesDiagram()
MapGen.isRunning = true MapGen.isRunning = true
end end

View File

@@ -0,0 +1,28 @@
---@class MapGen.Biome
---@field name string
---@field tempPoint number
---@field humidityPoint number
---@field groundNodes MapGen.Biome.GroundNodes
---@field soilHeight number
local Biome = {}
---@param name string
---@param tempPoint number
---@param humidityPoint number
---@param groundNodes MapGen.Biome.GroundNodes
---@param soilHeight number
---@return MapGen.Biome
function Biome:new(name, tempPoint, humidityPoint, groundNodes, soilHeight)
---@type MapGen.Biome
local instance = setmetatable({
name = name,
tempPoint = tempPoint,
humidityPoint = humidityPoint,
groundNodes = groundNodes,
soilHeight = soilHeight,
}, {__index = self})
return instance
end
return Biome

View File

@@ -0,0 +1,8 @@
--- Only for EmmyLua
---@class MapGen.Biome.GroundNodes
---@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`
local GroundNodes = {}
return GroundNodes

View File

@@ -63,6 +63,7 @@ function Region:new(minPos, maxPos, multinoiseParams, weightFactor)
function instance:initMultinoise() function instance:initMultinoise()
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams) _multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
print(dump(_multinoise))
end end
function instance.getWeightFactor() function instance.getWeightFactor()

View File

@@ -22,35 +22,63 @@ local mapGenerator = Core.MapGen.Class:new({
mapGenerator:RegisterLayer("world", -1000, 500) mapGenerator:RegisterLayer("world", -1000, 500)
mapGenerator:RegisterRegion("world", mapGenerator:RegisterRegion("world",
{x = -100, y = 0, z = -100}, vector.new(-150,0,-150),
{x = 100, y = 0, z = 100}, vector.new(150,0,150),
{ {
landscapeNoise = { landscapeNoise = {
offset = 50, offset = 50,
scale = 10, scale = 10,
spread = {x = 10, y = 10, z = 10},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
}
},
true, 0.3)
--[[
mapGenerator:RegisterRegion("world",
{x = 50, y = 0, z = -100},
{x = 150, y = 0, z = 100},
{
landscapeNoise = {
offset = 100,
scale = 10,
spread = {x = 100, y = 100, z = 100}, spread = {x = 100, y = 100, z = 100},
seed = 47, seed = 47,
octaves = 8, octaves = 8,
persistence = 0.4, persistence = 0.4,
lacunarity = 2, lacunarity = 2,
},
tempNoise = {
offset = 50,
scale = 25,
spread = {x = 10, y = 10, z = 10},
seed = 12,
octaves = 2,
persistence = 0.6,
lacunarity = 2,
},
humidityNoise = {
offset = 50,
scale = 25,
spread = {x = 10, y = 10, z = 10},
seed = 12,
octaves = 2,
persistence = 0.6,
lacunarity = 2,
} }
}, },
true)--]] true, 1.5)
--[[
mapGenerator:RegisterRegion("world",
vector.new(-50, 0, -50),
vector.new(50, 0, 50),
{
landscapeNoise = {
offset = -60,
scale = 10,
spread = {x = 100, y = 100, z = 100},
seed = 41,
octaves = 3,
persistence = 0.4,
lacunarity = 2,
}
},
true, 1.5) --]]
mapGenerator:RegisterBiome("biome1", 0, 0, {
soil = "soils:clay_soil_baren",
turf = "soils:clay_soil_baren",
}, 1)
mapGenerator:RegisterBiome("biome2", 100, 100, {
soil = "soils:rocky_soil_baren",
turf = "soils:rocky_soil_baren",
}, 1)
mapGenerator:run() mapGenerator:run()