diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 3b048ef..69958a1 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -21,10 +21,12 @@ local Layer = require("MapGen.Layer") local Region = require("MapGen.Region") ---@class MapGen ----@field layersByName table ----@field layersList table ----@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. +---@field layersByName table +---@field layersList 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. local MapGen = { + multinoiseInitialized = false, isRunning = false, } @@ -99,16 +101,30 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D ---@type MapGen.Region local region = Region:new(minPos, maxPos, multinoise) - --TODO: реализовать регистрацию допольнительных регионов для сглаживания шумов + --TODO: реализовать регистрацию дополнительных регионов для сглаживания шумов layer:addRegion(region) end -function MapGen:landscapeGeneration(data, area, x, y, z) - local index = area:index(x, y, z) +---Initialize region noise. This should be called after the map object is loaded. +function MapGen:initRegionsMultinoise() + ---@param layer MapGen.Layer + for _, layer in ipairs(self.layersList) do + + ---@param region MapGen.Region + for _, region in ipairs(layer.regionsList) do + region:initMultinoise() + end + + end + + self.multinoiseInitialized = true +end + +local function landscapeGeneration(mapGenerator, data, index, x, y, z) -- If generation occurs outside the layers, then a void is generated - local layer = self:getLayerByHeight(y) + local layer = mapGenerator:getLayerByHeight(y) if layer == nil then data[index] = id_air @@ -128,11 +144,9 @@ function MapGen:landscapeGeneration(data, area, 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 - if region.multinoise.landscapeNoise ~= nil then - local noise = core.get_value_noise(region.multinoise.landscapeNoise) - noiseHeightValue = noiseHeightValue + noise:get_2d({x = x, y = z}) + local noise = region:getMultinoise().landscapeNoise - end + noiseHeightValue = noiseHeightValue + noise:get_2d({x = x, y = z}) end -- ...And the average height is found: this provides smoothing @@ -141,11 +155,12 @@ function MapGen:landscapeGeneration(data, area, x, y, z) if y > noiseHeightValue then data[index] = id_air else - data[index] = core.get_content_id("rocks:tauitite") -- TODO: прописать заполнение камнем + data[index] = core.get_content_id("rocks:sylite") -- TODO: прописать заполнение камнем end + end -function MapGen:postprocessGeneration(data, area, x, y, z) +local function postprocessGeneration(mapGenerator, data, area, x, y, z) local index = area:index(x, y, z) -- TODO: генерация покрытия @@ -159,19 +174,25 @@ end function MapGen:onMapGenerated(minPos, maxPos, blockseed) -- We obtain a generation area for further manipulations local voxelManip, eMin, eMax = core.get_mapgen_object("voxelmanip") + + if not self.multinoiseInitialized then + self:initRegionsMultinoise() + end + local data = voxelManip:get_data() - local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax}) -- local param2_data = voxelManip:get_param2_data() + local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax}) -- Initial generation: landscape for z = minPos.z, maxPos.z do for y = minPos.y, maxPos.y do - for x = minPos.x, maxPos.x do - self:landscapeGeneration(data, area, x, y, z) - --core.handle_async(self.landscapeGeneration, callback, self, data, area, x, y, z) - -- вероятно благодаря 2D шуму высоты генерацию ландшафта можно оптимизировать - -- до генерации столбами (т. к. по x, z можно найти требуемый y) - end + local index = area:index(minPos.x, y, z) + + for x = minPos.x, maxPos.x do + landscapeGeneration(self, data, index, x, y, z) + + index = index + 1 + end end end @@ -179,7 +200,7 @@ function MapGen:onMapGenerated(minPos, maxPos, blockseed) --[[for z = minPos.z, maxPos.z do for y = minPos.y, maxPos.y do for x = minPos.x, maxPos.x do - MapGen:postprocessGeneration(data, area, x, y, z) + postprocessGeneration(mapGenerator, data, area, x, y, z) end end end --]] @@ -189,6 +210,7 @@ function MapGen:onMapGenerated(minPos, maxPos, blockseed) -- voxelManip:set_param2_data(param2_data) voxelManip:update_liquids() voxelManip:calc_lighting() + voxelManip:write_to_map() end diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index ae525fe..2a9a745 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -42,13 +42,13 @@ function Layer:getRegionsByPos(xPos, yPos, zPos) for _, region in ipairs(self.regionsList) do if ( - xPos >= region.minPos.x and - yPos >= region.minPos.y and - zPos >= region.minPos.z) and + xPos >= region:getMinPos().x and + yPos >= region:getMinPos().y and + zPos >= region:getMinPos().z) and ( - xPos <= region.maxPos.x and - yPos <= region.maxPos.y and - zPos <= region.maxPos.z + xPos <= region:getMaxPos().x and + yPos <= region:getMaxPos().y and + zPos <= region:getMaxPos().z ) then table.insert(regions, region) end diff --git a/mods/CORE/MapGen/src/MapGen/Region.lua b/mods/CORE/MapGen/src/MapGen/Region.lua index fdc65e6..cf83a45 100644 --- a/mods/CORE/MapGen/src/MapGen/Region.lua +++ b/mods/CORE/MapGen/src/MapGen/Region.lua @@ -1,6 +1,5 @@ -- TODO: replace `table` with `vector` for minPos and maxPos ---@class MapGen.Region - local Region = {} ---@param multinoiseParams MapGen.Region.MultinoiseParams -- ??? TODO: should be it's own class in EmmyLua @@ -30,6 +29,7 @@ function Region:new(minPos, maxPos, multinoiseParams) local instance = setmetatable({}, {__index = self}) + ---@return {x:number, y:number, z:number} function instance:getMinPos() return _minPos end @@ -39,6 +39,8 @@ function Region:new(minPos, maxPos, multinoiseParams) function instance:getMultinoiseParams() return _multinoiseParams end + + ---@return MapGen.Region.Multinoise function instance:getMultinoise() return _multinoise end diff --git a/mods/CORE/MapGen/src/MapGen/Region/Trinoise.lua b/mods/CORE/MapGen/src/MapGen/Region/Multinoise.lua similarity index 100% rename from mods/CORE/MapGen/src/MapGen/Region/Trinoise.lua rename to mods/CORE/MapGen/src/MapGen/Region/Multinoise.lua diff --git a/mods/CORE/MapGen/src/MapGen/Region/MultinoiseParams.lua b/mods/CORE/MapGen/src/MapGen/Region/MultinoiseParams.lua new file mode 100644 index 0000000..ba085d9 --- /dev/null +++ b/mods/CORE/MapGen/src/MapGen/Region/MultinoiseParams.lua @@ -0,0 +1,19 @@ +---For EmmyLua only + +---@class MapGen.Region.NoiseParams +---@field offset number +---@field scale number +---@field spread vector -- TODO: describe vector class from Luanti (push Alek) +---@field seed number +---@field octaves number +---@field persistence number +---@field lacunarity number +---@field flags string? + +---@class MapGen.Region.MultinoiseParams +---@field landscapeNoise MapGen.Region.NoiseParams? +---@field tempNoise MapGen.Region.NoiseParams? +---@field humidityNoise MapGen.Region.NoiseParams? +local MultinoiseParams = {} + +return MultinoiseParams diff --git a/mods/CORE/MapGen/src/MapGen/Region/TrinoiseParams.lua b/mods/CORE/MapGen/src/MapGen/Region/TrinoiseParams.lua deleted file mode 100644 index b0a7065..0000000 --- a/mods/CORE/MapGen/src/MapGen/Region/TrinoiseParams.lua +++ /dev/null @@ -1,19 +0,0 @@ ----For EmmyLua only - ----@class MapGen.Region.NoiseParams ----@field offset number ----@field scale number ----@field spread vector -- TODO: describe vector class from Luanti (push Alek) ----@field seed number ----@field octaves number ----@field persistence number ----@field lacunarity number ----@field flags string? - ----@class MapGen.Region.MultinoiseParams ----@field landscapeNoiseParams MapGen.Region.NoiseParams? ----@field tempNoiseParams MapGen.Region.NoiseParams? ----@field humidityNoiseParams MapGen.Region.NoiseParams? -local MultinoiseParams = {} - -return MultinoiseParams