From 1a116966f66151eececcb493e0ccdd8f62fe6af8 Mon Sep 17 00:00:00 2001 From: Vanic Date: Thu, 2 Oct 2025 21:20:17 +0300 Subject: [PATCH] Transfer debug mapgen & create Region.weightFactor --- mods/CORE/MapGen/init.lua | 40 ------------ mods/CORE/MapGen/mod.conf | 2 +- mods/CORE/MapGen/src/MapGen.lua | 90 ++++++++++++-------------- mods/CORE/MapGen/src/MapGen/Region.lua | 11 +++- 4 files changed, 51 insertions(+), 92 deletions(-) diff --git a/mods/CORE/MapGen/init.lua b/mods/CORE/MapGen/init.lua index 172818f..1d4e918 100644 --- a/mods/CORE/MapGen/init.lua +++ b/mods/CORE/MapGen/init.lua @@ -4,43 +4,3 @@ MapGen.Class = MapGen.require('MapGen') Api.addModToGlobalSpace(MapGen, 'Core.MapGen') --- DEBUG MAPGEN - ----@type MapGen -local mapGenerator = MapGen.Class:new() - -mapGenerator:RegisterLayer("world", -1000, 500) - -mapGenerator:RegisterRegion("world", - {x = -100, y = 0, z = -100}, - {x = 100, y = 0, z = 100}, - { - landscapeNoise = { - offset = 50, - scale = 10, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - } - }, - true) - -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, - } - }, - true)--]] - -mapGenerator:run() diff --git a/mods/CORE/MapGen/mod.conf b/mods/CORE/MapGen/mod.conf index b54774d..3767411 100644 --- a/mods/CORE/MapGen/mod.conf +++ b/mods/CORE/MapGen/mod.conf @@ -1,2 +1,2 @@ name = smc__core__map_gen -depends = smc__api__api_loader \ No newline at end of file +depends = smc__api__api_loader, smc__game__items__rocks, smc__game__items__liquids \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index f3f0d56..e3966eb 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -1,15 +1,5 @@ -local mathCeil, id - = math.ceil, core.get_content_id - --- --- Node's IDs for generation --- --- Specials -local id_air = id("air") - --- Stones - --- Liquids - --- --- End Node's IDs +local mathCeil, mathAbs + = math.ceil, math.abs -- --- MapGen default moises --- local oceanNoise @@ -39,16 +29,19 @@ local Region = require("MapGen.Region") ---@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. +---@field nodeIDs table local MapGen = { multinoiseInitialized = false, isRunning = false, } +---@param nodeIDs table ---@return MapGen -function MapGen:new() +function MapGen:new(nodeIDs) local instance = setmetatable({ layersByName = {}, - layersList = {}, + layersList = {}, + nodeIDs = nodeIDs, --registeredRegions = {}, }, {__index = self}) @@ -97,9 +90,10 @@ end ---@param layerName string ---@param minPos table ---@param maxPos table ----@param multinoise table +---@param multinoise table ---@param regionIs2D boolean? -function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D) +---@param weightFactor number? +function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D, weightFactor) ---@type MapGen.Layer local layer = self.layersByName[layerName] @@ -112,8 +106,12 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D maxPos.y = layer.maxY end + if weightFactor == nil then + weightFactor = 1 + end + ---@type MapGen.Region - local region = Region:new(minPos, maxPos, multinoise) + local region = Region:new(minPos, maxPos, multinoise, weightFactor) layer:addRegion(region) end @@ -135,25 +133,41 @@ end ---@return number -local function calculateWeight(minPos, maxPos, x, z) - --TODO: написать поддержку коэффициента, чтобы функция была не линейной и отключение учитывание веса +local function calculateWeight(minPos, maxPos, x, z, weightFactor) + if weightFactor == 0 then + return 1 + end + local centerX = (minPos.x + maxPos.x) / 2 local centerZ = (minPos.z + maxPos.z) / 2 - local distX = 1 - math.abs(x - centerX) / (math.abs((maxPos.x - minPos.x)) / 2) - local distZ = 1 - math.abs(z - centerZ) / (math.abs((maxPos.z - minPos.z)) / 2) + local distX = 1 - (mathAbs(x - centerX) / (mathAbs((maxPos.x - minPos.x)) / 2))^weightFactor + local distZ = 1 - (mathAbs(z - centerZ) / (mathAbs((maxPos.z - minPos.z)) / 2))^weightFactor + ---@type number local weight = math.min(distX, distZ) return weight end -local function landscapeGeneration(mapGenerator, data, index, x, y, z) +local function generateNode(mapGenerator, hight, data, index, x, y, z) + local ids = mapGenerator.nodeIDs + if y > hight and y >= 0 then + data[index] = ids.air + elseif y < hight then + data[index] = ids.sylite -- TODO: сделать генерацию разных камней + else + data[index] = ids.water + end +end + +---@param mapGenerator MapGen +local function generatorHandler(mapGenerator, data, index, x, y, z) -- If generation occurs outside the layers, then a void is generated local layer = mapGenerator:getLayerByHeight(y) if layer == nil then - data[index] = id_air + data[index] = mapGenerator.nodeIDs.air return end @@ -173,27 +187,12 @@ local function landscapeGeneration(mapGenerator, data, index, x, y, z) local noise = region:getMultinoise().landscapeNoise -- The further a point is from the center of a region, the less noise affects it. - local weight = calculateWeight(region:getMinPos(), region:getMaxPos(), x, z) + local weight = calculateWeight(region:getMinPos(), region:getMaxPos(), x, z, region.getWeightFactor()) noiseHeightValue = noiseHeightValue + ( noise:get_2d({x = x, y = z}) * weight ) end - if y > noiseHeightValue and y >= 0 then - data[index] = id_air - elseif y < noiseHeightValue then - data[index] = core.get_content_id("rocks:falmyte") -- TODO: сделать генерацию разных камней - else - data[index] = core.get_content_id("liquids:water_source") -end - -end - -local function postprocessGeneration(mapGenerator, data, area, x, y, z) - local index = area:index(x, y, z) - - -- TODO: генерация покрытия - - -- TODO: генерация растительности + generateNode(mapGenerator, noiseHeightValue, data, index, x, y, z) end ---@param minPos table @@ -217,22 +216,13 @@ function MapGen:onMapGenerated(minPos, maxPos, blockseed) local index = area:index(minPos.x, y, z) for x = minPos.x, maxPos.x do - landscapeGeneration(self, data, index, x, y, z) + generatorHandler(self, data, index, x, y, z) index = index + 1 end end end - -- Post-processing: adding cover and vegetation - --[[for z = minPos.z, maxPos.z do - for y = minPos.y, maxPos.y do - for x = minPos.x, maxPos.x do - postprocessGeneration(mapGenerator, data, area, x, y, z) - end - end - end --]] - -- We make changes back to LVM and recalculate light & liquids voxelManip:set_data(data) -- voxelManip:set_param2_data(param2_data) diff --git a/mods/CORE/MapGen/src/MapGen/Region.lua b/mods/CORE/MapGen/src/MapGen/Region.lua index cf83a45..95a1328 100644 --- a/mods/CORE/MapGen/src/MapGen/Region.lua +++ b/mods/CORE/MapGen/src/MapGen/Region.lua @@ -18,13 +18,18 @@ end ---@param minPos table ---@param maxPos table ---@param multinoiseParams MapGen.Region.MultinoiseParams +---@param weightFactor number ---@return MapGen.Region -function Region:new(minPos, maxPos, multinoiseParams) +function Region:new(minPos, maxPos, multinoiseParams, weightFactor) local _minPos = minPos local _maxPos = maxPos local _multinoiseParams = multinoiseParams + if weightFactor < 0 then + weightFactor = 1 + end + local _multinoise = {} local instance = setmetatable({}, {__index = self}) @@ -49,6 +54,10 @@ function Region:new(minPos, maxPos, multinoiseParams) _multinoise = multinoiseParamsToMultinoise(_multinoiseParams) end + function instance.getWeightFactor() + return weightFactor + end + return instance end