Add base biome support
This commit is contained in:
@@ -30,6 +30,37 @@ local rocksNoiseParams ={
|
||||
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 ---
|
||||
|
||||
local modInfo = Mod.getInfo()
|
||||
@@ -41,15 +72,22 @@ local Layer = require("MapGen.Layer")
|
||||
---@type MapGen.Region
|
||||
local Region = require("MapGen.Region")
|
||||
|
||||
---@type MapGen.Biome
|
||||
local Biome = require("MapGen.Biome")
|
||||
|
||||
---@class MapGen
|
||||
---@field layersByName 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 isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
|
||||
---@field nodeIDs table<string, string>
|
||||
local MapGen = {
|
||||
layersByName = {},
|
||||
layersList = {},
|
||||
biomesList = {},
|
||||
biomesDiagram = {},
|
||||
multinoiseInitialized = 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 minPos 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 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)
|
||||
@@ -130,6 +168,7 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D
|
||||
maxPos.y = layer.maxY
|
||||
end
|
||||
|
||||
--TODO: добавить проверку, что координаты вершин региона не совпадают (регион не является линией толщиной в 1 блок)
|
||||
--TODO: Добавить проверку, что регион не выходит за границы слоя или/и автоматически обрубать регион до слоя
|
||||
|
||||
if weightFactor == nil then
|
||||
@@ -157,6 +196,42 @@ function MapGen:initRegionsMultinoise()
|
||||
self.multinoiseInitialized = true
|
||||
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
|
||||
---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
|
||||
|
||||
---@type number
|
||||
local weight = mathMin(distX, distZ)
|
||||
local weight = distX * distZ -- mathMin(distX, distZ)
|
||||
|
||||
return weight
|
||||
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)
|
||||
|
||||
if rocksNoise == nil then
|
||||
@@ -211,11 +290,15 @@ local function generateRock(ids, data, index, x, y, z)
|
||||
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
|
||||
|
||||
if y > hight and y >= 0 then
|
||||
local biome = mapGenerator.biomesDiagram[temp][humidity]
|
||||
|
||||
if y > hight and y > 0 then
|
||||
data[index] = ids.air
|
||||
elseif y == hight and y > 0 then
|
||||
generateSoil(biome, data, index, x, y, z)
|
||||
elseif y < hight then
|
||||
generateRock(ids, data, index, x, y, z)
|
||||
else
|
||||
@@ -238,24 +321,54 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
oceanNoise = core.get_value_noise(oceanNoiseParams)
|
||||
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)
|
||||
|
||||
-- The height noise values of all regions that a node is part of are added together.
|
||||
---@param region MapGen.Region
|
||||
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.
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
---@param minPos table
|
||||
@@ -307,6 +420,8 @@ function MapGen:run()
|
||||
self:onMapGenerated(...)
|
||||
end)
|
||||
|
||||
self:initBiomesDiagram()
|
||||
|
||||
MapGen.isRunning = true
|
||||
end
|
||||
|
||||
|
||||
28
mods/CORE/MapGen/src/MapGen/Biome.lua
Normal file
28
mods/CORE/MapGen/src/MapGen/Biome.lua
Normal 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
|
||||
8
mods/CORE/MapGen/src/MapGen/Biome/GroundNodes.lua
Normal file
8
mods/CORE/MapGen/src/MapGen/Biome/GroundNodes.lua
Normal 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
|
||||
@@ -63,6 +63,7 @@ function Region:new(minPos, maxPos, multinoiseParams, weightFactor)
|
||||
|
||||
function instance:initMultinoise()
|
||||
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
|
||||
print(dump(_multinoise))
|
||||
end
|
||||
|
||||
function instance.getWeightFactor()
|
||||
|
||||
Reference in New Issue
Block a user