From 640d7a4deba4cb20a5a1f0e16861ab69f6a83a61 Mon Sep 17 00:00:00 2001 From: Vanic Date: Wed, 19 Nov 2025 21:22:19 +0300 Subject: [PATCH] Mapgen: write SuperUltraSigmaMapgen part 6.2 --- mods/CORE/MapGen/src/MapGen.lua | 58 +++++++------------ mods/CORE/MapGen/src/MapGen/Layer.lua | 32 ++++++++++ .../MapGen/src/MapGen/{ => Layer}/Biome.lua | 6 +- mods/ENVIRONMENT/MapGenerator/init.lua | 4 +- 4 files changed, 58 insertions(+), 42 deletions(-) rename mods/CORE/MapGen/src/MapGen/{ => Layer}/Biome.lua (87%) diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 2a8e44c..d45d757 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -46,8 +46,8 @@ local Layer = require('MapGen.Layer') ---@type MapGen.Peak local Peak = require('MapGen.Peak') ----@type MapGen.Biome -local Biome = require('MapGen.Biome') +---@type MapGen.Layer.Biome +local Biome = require('MapGen.Layer.Biome') ---@type MapGen.Triangulation local Triangulation = require('MapGen.Triangulation') @@ -58,8 +58,6 @@ local Cavern = require('MapGen.Layer.Cavern') ---@class MapGen ---@field layersByName table ---@field layersList MapGen.Layer[] ----@field biomesList MapGen.Biome[] ----@field biomesDiagram table ---@field peaksMultinoiseInitialized boolean Is the fractal noise of the peaks initialized? This is necessary for a one-time noise initialization. ---@field cavernsNoiseInitialized boolean TODO: описание ---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. @@ -67,8 +65,6 @@ local Cavern = require('MapGen.Layer.Cavern') local MapGen = { layersByName = {}, layersList = {}, - biomesList = {}, - biomesDiagram = {}, peaksMultinoiseInitialized = false, cavernsNoiseInitialized = false, isRunning = false, @@ -188,19 +184,24 @@ function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups) end end ----@param name string ----@param tempPoint number ----@param humidityPoint number ----@param groundNodes MapGen.Biome.GroundNodes +---TODO: описание +---@param layerName string +---@param biomeName 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) +function MapGen:registerBiome(layerName, biomeName, tempPoint, humidityPoint, groundNodes, soilHeight) + local layer = self.layersByName[layerName] + assert(layer.biomesByName[biomeName] == nil, ('A biome named `%s` already exists in the `%s` layer.'):format(biomeName, layerName)) - --TODO: добавить MapGen.biomesByName и проверку на существование биома с таким же именем, аналогично `MapGen:registerCavern` - --TODO: перенести Biomes в слой - table.insert(self.biomesList, biome) + local biome = Biome:new(biomeName, tempPoint, humidityPoint, groundNodes, soilHeight) + + layer.biomesByName[biomeName] = biome + table.insert(layer.biomesList, biome) end +---TODO: описание ---@param layerName string ---@param cavernName string ---@param minY number @@ -246,6 +247,7 @@ function MapGen:initPeaksMultinoise() self.peaksMultinoiseInitialized = true end +---TODO: описаение function MapGen:initCavernsNoise() ---@param layer MapGen.Layer for _, layer in ipairs(self.layersList) do @@ -262,27 +264,9 @@ end ---Initialization of the biome diagram using the Voronoi method. 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 + ---@param layer MapGen.Layer + for _, layer in ipairs(self.layersList) do + layer:initBiomesDiagram() end end @@ -359,7 +343,7 @@ local function generateNode(mapGenerator, layer, hight, temp, humidity, data, in temp = mathMin(100, mathMax(0, temp + m)) humidity = mathMin(100, mathMax(0, humidity + m)) - local biome = mapGenerator.biomesDiagram[mathRound(temp)][mathRound(humidity)] + local biome = layer.biomesDiagram[mathRound(temp)][mathRound(humidity)] if y > hight and y > 0 then data[index] = ids.air diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index d512ad2..598051c 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -5,12 +5,18 @@ local mathSqrt = math.sqrt ---@field minY number ---@field maxY number ---@field peaksList MapGen.Peak[] +---@field biomesByName table +---@field biomesList MapGen.Layer.Biome[] +---@field biomesDiagram table ---@field cavernsByName table ---@field cavernsList MapGen.Layer.Cavern[] ---@field trianglesList MapGen.Triangle[] ---@field tetrahedronsList MapGen.Tetrahedron[] local Layer = { peaksList = {}, + biomesByName = {}, + biomesList = {}, + biomesDiagram = {}, cavernsByName = {}, cavernsList = {}, trianglesList = {}, @@ -73,4 +79,30 @@ function Layer:getPeaksByPos(xPos, yPos, zPos, radius) return peaks, totalWeight end +---Initialization of the biome diagram using the Voronoi method. +function Layer: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.Layer.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 + return Layer diff --git a/mods/CORE/MapGen/src/MapGen/Biome.lua b/mods/CORE/MapGen/src/MapGen/Layer/Biome.lua similarity index 87% rename from mods/CORE/MapGen/src/MapGen/Biome.lua rename to mods/CORE/MapGen/src/MapGen/Layer/Biome.lua index 17a8dab..f5d28a6 100644 --- a/mods/CORE/MapGen/src/MapGen/Biome.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer/Biome.lua @@ -1,4 +1,4 @@ ----@class MapGen.Biome +---@class MapGen.Layer.Biome ---@field name string ---@field tempPoint number ---@field humidityPoint number @@ -11,9 +11,9 @@ local Biome = {} ---@param humidityPoint number ---@param groundNodes MapGen.Biome.GroundNodes ---@param soilHeight number ----@return MapGen.Biome +---@return MapGen.Layer.Biome function Biome:new(name, tempPoint, humidityPoint, groundNodes, soilHeight) - ---@type MapGen.Biome + ---@type MapGen.Layer.Biome local instance = setmetatable({ name = name, tempPoint = tempPoint, diff --git a/mods/ENVIRONMENT/MapGenerator/init.lua b/mods/ENVIRONMENT/MapGenerator/init.lua index 97e4eb0..ff60f6a 100644 --- a/mods/ENVIRONMENT/MapGenerator/init.lua +++ b/mods/ENVIRONMENT/MapGenerator/init.lua @@ -115,12 +115,12 @@ mapGenerator:registerCavern('world', 'cavern1', -100, -10, 20, { lacunarity = 2.0 }) -mapGenerator:registerBiome("biome1", 0, 0, { +mapGenerator:registerBiome('world', "biome1", 0, 0, { soil = "soils:clay_soil_baren", turf = "soils:clay_soil_baren", }, 1) -mapGenerator:registerBiome("biome2", 100, 100, { +mapGenerator:registerBiome('world', "biome2", 100, 100, { soil = "soils:rocky_soil_baren", turf = "soils:rocky_soil_baren", }, 1)