Transfer debug mapgen & create Region.weightFactor

This commit is contained in:
Van
2025-10-02 21:20:17 +03:00
parent 7e0586c55e
commit 1a116966f6
4 changed files with 51 additions and 92 deletions

View File

@@ -4,43 +4,3 @@ MapGen.Class = MapGen.require('MapGen')
Api.addModToGlobalSpace(MapGen, 'Core.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()

View File

@@ -1,2 +1,2 @@
name = smc__core__map_gen name = smc__core__map_gen
depends = smc__api__api_loader depends = smc__api__api_loader, smc__game__items__rocks, smc__game__items__liquids

View File

@@ -1,15 +1,5 @@
local mathCeil, id local mathCeil, mathAbs
= math.ceil, core.get_content_id = math.ceil, math.abs
-- --- Node's IDs for generation ---
-- Specials
local id_air = id("air")
-- Stones
-- Liquids
-- --- End Node's IDs
-- --- MapGen default moises --- -- --- MapGen default moises ---
local oceanNoise local oceanNoise
@@ -39,16 +29,19 @@ local Region = require("MapGen.Region")
---@field layersList 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 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 isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, string>
local MapGen = { local MapGen = {
multinoiseInitialized = false, multinoiseInitialized = false,
isRunning = false, isRunning = false,
} }
---@param nodeIDs table<string, string>
---@return MapGen ---@return MapGen
function MapGen:new() function MapGen:new(nodeIDs)
local instance = setmetatable({ local instance = setmetatable({
layersByName = {}, layersByName = {},
layersList = {}, layersList = {},
nodeIDs = nodeIDs,
--registeredRegions = {}, --registeredRegions = {},
}, {__index = self}) }, {__index = self})
@@ -97,9 +90,10 @@ end
---@param layerName string ---@param layerName string
---@param minPos table ---@param minPos table
---@param maxPos table ---@param maxPos table
---@param multinoise table ---@param multinoise table
---@param regionIs2D boolean? ---@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 ---@type MapGen.Layer
local layer = self.layersByName[layerName] local layer = self.layersByName[layerName]
@@ -112,8 +106,12 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D
maxPos.y = layer.maxY maxPos.y = layer.maxY
end end
if weightFactor == nil then
weightFactor = 1
end
---@type MapGen.Region ---@type MapGen.Region
local region = Region:new(minPos, maxPos, multinoise) local region = Region:new(minPos, maxPos, multinoise, weightFactor)
layer:addRegion(region) layer:addRegion(region)
end end
@@ -135,25 +133,41 @@ end
---@return number ---@return number
local function calculateWeight(minPos, maxPos, x, z) local function calculateWeight(minPos, maxPos, x, z, weightFactor)
--TODO: написать поддержку коэффициента, чтобы функция была не линейной и отключение учитывание веса if weightFactor == 0 then
return 1
end
local centerX = (minPos.x + maxPos.x) / 2 local centerX = (minPos.x + maxPos.x) / 2
local centerZ = (minPos.z + maxPos.z) / 2 local centerZ = (minPos.z + maxPos.z) / 2
local distX = 1 - math.abs(x - centerX) / (math.abs((maxPos.x - minPos.x)) / 2) local distX = 1 - (mathAbs(x - centerX) / (mathAbs((maxPos.x - minPos.x)) / 2))^weightFactor
local distZ = 1 - math.abs(z - centerZ) / (math.abs((maxPos.z - minPos.z)) / 2) local distZ = 1 - (mathAbs(z - centerZ) / (mathAbs((maxPos.z - minPos.z)) / 2))^weightFactor
---@type number
local weight = math.min(distX, distZ) local weight = math.min(distX, distZ)
return weight return weight
end 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 -- If generation occurs outside the layers, then a void is generated
local layer = mapGenerator:getLayerByHeight(y) local layer = mapGenerator:getLayerByHeight(y)
if layer == nil then if layer == nil then
data[index] = id_air data[index] = mapGenerator.nodeIDs.air
return return
end end
@@ -173,27 +187,12 @@ local function landscapeGeneration(mapGenerator, data, index, x, y, z)
local noise = region:getMultinoise().landscapeNoise local noise = region:getMultinoise().landscapeNoise
-- The further a point is from the center of a region, the less noise affects it. -- 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 ) noiseHeightValue = noiseHeightValue + ( noise:get_2d({x = x, y = z}) * weight )
end end
if y > noiseHeightValue and y >= 0 then generateNode(mapGenerator, noiseHeightValue, data, index, x, y, z)
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: генерация растительности
end end
---@param minPos table ---@param minPos table
@@ -217,22 +216,13 @@ function MapGen:onMapGenerated(minPos, maxPos, blockseed)
local index = area:index(minPos.x, y, z) local index = area:index(minPos.x, y, z)
for x = minPos.x, maxPos.x do for x = minPos.x, maxPos.x do
landscapeGeneration(self, data, index, x, y, z) generatorHandler(self, data, index, x, y, z)
index = index + 1 index = index + 1
end end
end 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 -- We make changes back to LVM and recalculate light & liquids
voxelManip:set_data(data) voxelManip:set_data(data)
-- voxelManip:set_param2_data(param2_data) -- voxelManip:set_param2_data(param2_data)

View File

@@ -18,13 +18,18 @@ end
---@param minPos table ---@param minPos table
---@param maxPos table ---@param maxPos table
---@param multinoiseParams MapGen.Region.MultinoiseParams ---@param multinoiseParams MapGen.Region.MultinoiseParams
---@param weightFactor number
---@return MapGen.Region ---@return MapGen.Region
function Region:new(minPos, maxPos, multinoiseParams) function Region:new(minPos, maxPos, multinoiseParams, weightFactor)
local _minPos = minPos local _minPos = minPos
local _maxPos = maxPos local _maxPos = maxPos
local _multinoiseParams = multinoiseParams local _multinoiseParams = multinoiseParams
if weightFactor < 0 then
weightFactor = 1
end
local _multinoise = {} local _multinoise = {}
local instance = setmetatable({}, {__index = self}) local instance = setmetatable({}, {__index = self})
@@ -49,6 +54,10 @@ function Region:new(minPos, maxPos, multinoiseParams)
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams) _multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
end end
function instance.getWeightFactor()
return weightFactor
end
return instance return instance
end end