diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index db1dc5c..e2c561d 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -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 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 diff --git a/mods/CORE/MapGen/src/MapGen/Biome.lua b/mods/CORE/MapGen/src/MapGen/Biome.lua new file mode 100644 index 0000000..7f729c4 --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Biome.lua @@ -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 \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Biome/GroundNodes.lua b/mods/CORE/MapGen/src/MapGen/Biome/GroundNodes.lua new file mode 100644 index 0000000..235fa44 --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Biome/GroundNodes.lua @@ -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 \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Region.lua b/mods/CORE/MapGen/src/MapGen/Region.lua index c9b127d..0c98e16 100644 --- a/mods/CORE/MapGen/src/MapGen/Region.lua +++ b/mods/CORE/MapGen/src/MapGen/Region.lua @@ -63,6 +63,7 @@ function Region:new(minPos, maxPos, multinoiseParams, weightFactor) function instance:initMultinoise() _multinoise = multinoiseParamsToMultinoise(_multinoiseParams) + print(dump(_multinoise)) end function instance.getWeightFactor() diff --git a/mods/ENVIRONMENT/MapGenerator/init.lua b/mods/ENVIRONMENT/MapGenerator/init.lua index 6da3dc3..cc2ef35 100644 --- a/mods/ENVIRONMENT/MapGenerator/init.lua +++ b/mods/ENVIRONMENT/MapGenerator/init.lua @@ -22,35 +22,63 @@ local mapGenerator = Core.MapGen.Class:new({ mapGenerator:RegisterLayer("world", -1000, 500) mapGenerator:RegisterRegion("world", - {x = -100, y = 0, z = -100}, - {x = 100, y = 0, z = 100}, + vector.new(-150,0,-150), + vector.new(150,0,150), { landscapeNoise = { offset = 50, 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}, seed = 47, octaves = 8, persistence = 0.4, 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()