Rewrite mapgen

This commit is contained in:
Van
2026-01-10 17:12:06 +03:00
parent d9f9bd7a6c
commit dfd8021808
6 changed files with 283 additions and 195 deletions

View File

@@ -1,21 +1,23 @@
local mathAbs, mathRound, mathMin, mathMax, id local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_id = math.abs, math.round, math.min, math.max, core.get_content_id
local pastNoise2DCalc = { ---@class MapGen.PastCalcNoiseValue
local pastCalcNoiseValue = {
---@type MapGen.Triangle? ---@type MapGen.Triangle?
triangle = nil, triangle = nil,
---@type number? ---@type number?
x = nil, x = nil,
---@type number? ---@type number?
y = nil, z = nil,
noiseValues = {
---@type number? ---@type number?
height = nil, noiseValue = nil,
---@type number? }
humidity = nil,
---@type number? ---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
temp = nil, local pastNoise2DCalc = {
} landscape = pastCalcNoiseValue,
temp = pastCalcNoiseValue,
humidity = pastCalcNoiseValue
} }
local modInfo = Mod.getInfo('smc__core__map_gen') local modInfo = Mod.getInfo('smc__core__map_gen')
@@ -44,7 +46,7 @@ local Cavern = require('MapGen.Cavern')
---@field layersList MapGen.Layer[] ---@field layersList MapGen.Layer[]
---@field staticsByName table<string, MapGen.StaticLayer> ---@field staticsByName table<string, MapGen.StaticLayer>
---@field staticsList MapGen.StaticLayer[] ---@field staticsList MapGen.StaticLayer[]
---@field peaksMultinoiseInitialized boolean Is the fractal noise of the peaks initialized? This is necessary for a one-time noise initialization. ---@field layersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization.
---@field cavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization. ---@field cavernsNoiseInitialized boolean Is the fractal noise of the caverns 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, number> ---@field nodeIDs table<string, number>
@@ -53,7 +55,7 @@ local MapGen = {
layersList = {}, layersList = {},
staticsByName = {}, staticsByName = {},
staticsList = {}, staticsList = {},
peaksMultinoiseInitialized = false, layersNoisesInitialized = false,
cavernsNoiseInitialized = false, cavernsNoiseInitialized = false,
isRunning = false, isRunning = false,
} }
@@ -148,6 +150,19 @@ function MapGen:registerLayer(name, def)
end) end)
end end
---@param layerName string
---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString TODO: описание
---@param noiseParams NoiseParams
function MapGen:registerLayerNoiseColor(layerName, category, color, noiseParams)
---@type MapGen.Layer
local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
layer:addNoiseParamsColor(category, color, noiseParams)
end
---Mark the world area between two heights as a `MapGen.StaticLayer`. ---Mark the world area between two heights as a `MapGen.StaticLayer`.
--- ---
---Note: layers must not overlap. ---Note: layers must not overlap.
@@ -176,25 +191,20 @@ end
--- ---
---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation. ---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation.
---@param layerName string The name of the layer in which the new peak will be included. ---@param layerName string The name of the layer in which the new peak will be included.
---@param peakPos vector The coordinate where the peak will be placed. ---@param category MapGen.Layer.Category TODO: описание
---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation ---@param def MapGen.PeakDef
---@param groups table<string, number>? Peak groups that may affect processing. function MapGen:registerPeak(layerName, category, def)
function MapGen:registerPeak(layerName, peakPos, multinoise, groups)
---@type MapGen.Layer ---@type MapGen.Layer
local layer = self.layersByName[layerName] local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
if not layer then
error('Invalid layer: ' .. layerName)
end
--TODO: добавить проверку, что координата пиков не совпадают --TODO: добавить проверку, что координата пиков не совпадают
---@type MapGen.Peak ---@type MapGen.Peak
local peak = Peak:new(peakPos, multinoise, groups) local peak = Peak:new(def)
layer:addPeak(peak) layer:addPeak(category, peak)
end end
---Mark a world point as `MapGen.Peak` and assign it a specific noise that will affect the generation. ---Mark a world point as `MapGen.Peak` and assign it a specific noise that will affect the generation.
@@ -202,20 +212,20 @@ end
--- ---
---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation. ---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation.
---@param layerName string The name of the layer in which the new peak will be included. ---@param layerName string The name of the layer in which the new peak will be included.
---@param peakPos vector The coordinate where the peak will be placed. ---@param category MapGen.Layer.Category TODO: описание
---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation. ---@param def MapGen.PeakDef
---@param groups table<string, number>? Peak groups that may affect processing. function MapGen:register2DPeak(layerName, category, def)
function MapGen:register2DPeak(layerName, peakPos, multinoise, groups) def = table.copy_with_metatables(def)
local _peakPos = table.copy_with_metatables(peakPos)
_peakPos.y = 0
if groups == nil then def.pos.y = 0
groups = {is2d = 1}
if def.groups == nil then
def.groups = {is2d = 1}
else else
groups.is2d = 1 def.groups.is2d = 1
end end
self:registerPeak(layerName, _peakPos, multinoise, groups) self:registerPeak(layerName, category, def)
end end
---Mark the world points as `MapGen.Peak` and assign them all a specific noise that will affect generation. ---Mark the world points as `MapGen.Peak` and assign them all a specific noise that will affect generation.
@@ -223,36 +233,40 @@ end
--- ---
---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation. ---Note: The peaks shouldn't overlap. This won't cause any errors, but it might ruin the generation.
---@param layerName string The name of the layer in which the new peaks will be included. ---@param layerName string The name of the layer in which the new peaks will be included.
---@param multinoise MapGen.Peak.MultinoiseParams The noise that will be assigned to each peak and will affect the map generation. ---@param category MapGen.Layer.Category TODO: описание
---@param peakPoses vector[] Set the coordinates where the peaks will be placed. ---@param defs MapGen.PeakDef[]
---@param groups table<string, number>? Peak groups that may affect processing. function MapGen:register2DPeaks(layerName,category, defs)
function MapGen:register2DPeaks(layerName, multinoise, peakPoses, groups) for _, def in ipairs(defs) do
for _, peakPos in ipairs(peakPoses) do self:register2DPeak(layerName, category, def)
self:register2DPeak(layerName, peakPos, multinoise, groups)
end end
end end
---TODO: описание
---@alias PeakColors table<ColorString, {noise:MapGen.Peak.MultinoiseParams, groups:table<string, number>?}>
---TODO: описание ---TODO: описание
---@param layerName string The name of the layer in which the new peaks will be included. ---@param layerName string The name of the layer in which the new peaks will be included.
---@param map string TODO ---@param map string TODO: описание
---@param zeroPos Position2d ---@param scale number TODO: описание
---@param peakColors PeakColors TODO ---@param zeroPos Position2d TODO: описание
function MapGen:register2DPeaksFromFile(layerName, map, zeroPos,peakColors) ---@param category MapGen.Layer.Category TODO: описание
---@param groups? table<string, number>
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
local modPath = core.get_modpath(core.get_current_modname()) local modPath = core.get_modpath(core.get_current_modname())
local peaks = Api.ff2luat(modPath .. '/textures/maps/' .. map) local peaks = Api.ff2luat(modPath .. '/textures/maps/' .. map)
local mapSize = peaks.size local mapSize = peaks.size
peaks.size = nil peaks.size = nil
print(dump(peaks))
for color, peaksPos in pairs(peaks) do for color, peaksPos in pairs(peaks) do
local peakParams = peakColors[color]
assert(peakParams ~= nil, ('There are no generation settings declared for the color `%s`'):format(color))
for _, peakPos in ipairs(peaksPos) do for _, peakPos in ipairs(peaksPos) do
self:register2DPeak(layerName, vector.new(peakPos.x - zeroPos.x, 0, peakPos.y - zeroPos.y), peakParams.noise, peakParams.groups) ---@type MapGen.PeakDef
local def = {
pos = vector.new((peakPos.x - zeroPos.x) * scale, 0, (peakPos.y - zeroPos.y) * scale),
color = color,
groups = groups,
}
print(peakPos.x, peakPos.y)
self:register2DPeak(layerName, category, def)
end end
end end
end end
@@ -293,8 +307,18 @@ end
function MapGen:initLayersTriangultaion() function MapGen:initLayersTriangultaion()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
layer.trianglesList = Triangulation.triangulate(layer.peaksList) for category, peaks in pairs(layer.peaksList) do
-- layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList)
layer.trianglesList[category] = Triangulation.triangulate(peaks)
-- layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList) -- 3D not supported
end
end
end
function MapGen:initLayersNoises()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
layer:initNoises()
end end
end end
@@ -306,22 +330,6 @@ function MapGen:initBiomesDiagram()
end end
end end
---Initialize peak's noises. This should be called after the map object is loaded.
function MapGen:initPeaksMultinoise()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
---@param peak MapGen.Peak
for _, peak in ipairs(layer.peaksList) do
peak:initMultinoise()
end
end
self.peaksMultinoiseInitialized = true
end
---Initialize caverns's noises. This should be called after the map object is loaded. ---Initialize caverns's noises. This should be called after the map object is loaded.
function MapGen:initCavernsNoise() function MapGen:initCavernsNoise()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
@@ -507,6 +515,46 @@ local function pointInTriangle(px, pz, triangle)
return (u >= 0) and (v >= 0) and (u + v <= 1) return (u >= 0) and (v >= 0) and (u + v <= 1)
end end
---Returns the weighted values of 2d noise.
---@param category MapGen.Layer.Category
---@param layer MapGen.Layer
---@param triangle MapGen.Triangle
---@param x number
---@param y number
---@param z number
---@return number
local function calcNoise2dValue(category, layer, triangle, x, y, z)
local peak1 = triangle.p1
local peak2 = triangle.p2
local peak3 = triangle.p3
local noise1 = layer:getNoiseByColor(category, peak1:getColor())
local noise2 = layer:getNoiseByColor(category, peak2:getColor())
local noise3 = layer:getNoiseByColor(category, peak3:getColor())
local noiseValue = 0.0
local totalWeight = 0.0
-- We calculate the noise value using the smoothing formula:
-- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when
-- totalWeight = weight1 + weight2 + weight3
local weight = calcWeight(triangle.h1, x, y, z)
noiseValue = noiseValue + noise1:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(triangle.h2, x, y, z)
noiseValue = noiseValue + noise2:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(triangle.h3, x, y, z)
noiseValue = noiseValue + noise3:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
return (noiseValue / totalWeight)
end
--[[
---Returns the weighted values of 2d noises. ---Returns the weighted values of 2d noises.
---@param triangle MapGen.Triangle ---@param triangle MapGen.Triangle
---@param x number ---@param x number
@@ -546,8 +594,55 @@ local function calcNoises2dValues(triangle, x, y, z)
totalWeight = totalWeight + weight totalWeight = totalWeight + weight
return noiseHeightValue / totalWeight, noiseTempValue / totalWeight, noiseHumidityValue / totalWeight return noiseHeightValue / totalWeight, noiseTempValue / totalWeight, noiseHumidityValue / totalWeight
end--]]
---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
---
---Returns `nil` if the point is not included in any triangle.
---@param category MapGen.Layer.Category
---@param layer MapGen.Layer
---@param x number
---@param y number
---@param z number
---@return number?
function MapGen:getNoises2dValue(category, layer, x, y, z)
---@type MapGen.PastCalcNoiseValue
local pastCalc = pastNoise2DCalc[category]
if (pastCalc.x ~= nil and pastCalc.z ~= nil) and (x == pastCalc.x and z == pastCalc.z) then
return pastCalc.noiseValue
end
if pastCalc.triangle ~= nil and pointInTriangle(x, z, pastCalc.triangle) then
local noiseValue = calcNoise2dValue(category, layer, pastCalc.triangle, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList[category]) do
if pointInTriangle(x, z,triangle) then
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
pastCalc.triangle = triangle
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end
-- If the point does not fall within the triangle, it is impossible to calculate the height.
-- For debugging purposes only, may slow down generation:
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
end end
--[[
---Calculates a smoothed 2D noise value based on a preliminary space partitioning. ---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
--- ---
---Returns `nil` if the point is not included in any triangle. ---Returns `nil` if the point is not included in any triangle.
@@ -601,6 +696,7 @@ function MapGen:getNoises2dValues(layer, x, y, z)
-- For debugging purposes only, may slow down generation: -- For debugging purposes only, may slow down generation:
--core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z))) --core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z)))
end end
--]]
--[[ --[[
3D noise smoothing is too slow, so it was decided to stop using it. 3D noise smoothing is too slow, so it was decided to stop using it.
@@ -717,17 +813,18 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
end end
-- Calculating landscape height -- Calculating landscape height
local noiseHeightValue, noiseTempValue, noiseHumidityValue = mapGenerator:getNoises2dValues(layer, x, y, z) -- If the value of the noise is `nil`,
-- If the height of the landscape is `nil`,
-- then the point does not fall within any of the triangles. -- then the point does not fall within any of the triangles.
if noiseHeightValue == nil then local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) or 0.0
noiseHeightValue = 0.0 local noiseTempValue = mapGenerator:getNoises2dValue('temp', layer, x, y, z) or 0.0
noiseTempValue = 0.0 local noiseHumidityValue = mapGenerator:getNoises2dValue('humidity', layer, x, y, z) or 0.0
noiseHumidityValue = 0.0
end -- We recalculate the climate based on altitude.
noiseTempValue = layer:calcTemp(noiseTempValue, y)
noiseHumidityValue = layer:calcHumidity(noiseHumidityValue, y)
--print(noiseHeightValue, noiseTempValue, noiseHumidityValue)
---@diagnostic disable-next-line: param-type-not-match
generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end end
@@ -742,8 +839,8 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
local eMin, eMax = voxelManip:get_emerged_area() local eMin, eMax = voxelManip:get_emerged_area()
if not self.peaksMultinoiseInitialized then if not self.layersNoisesInitialized then
self:initPeaksMultinoise() self:initLayersNoises()
end end
if not self.cavernsNoiseInitialized then if not self.cavernsNoiseInitialized then
@@ -804,6 +901,10 @@ function MapGen:run()
self:initLayersTriangultaion() self:initLayersTriangultaion()
MapGen.isRunning = true MapGen.isRunning = true
for _, peak in ipairs(self.layersList[1].peaksList["landscape"]) do
print(tostring(peak))
end
end end
return MapGen return MapGen

View File

@@ -10,7 +10,8 @@
---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number ---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number ---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number
---@field peaksList {['landscape']:MapGen.Peak[],['temp']:MapGen.Peak[],['humidity']:MapGen.Peak[]} ---@field peaksList {['landscape']:MapGen.Peak[],['temp']:MapGen.Peak[],['humidity']:MapGen.Peak[]}
---@field colorsList {['landscape']:table<ColorString, NoiseParams>,['temp']:table<ColorString, NoiseParams>,['humidity']:table<ColorString, NoiseParams>} ---@field noisesByColor {['landscape']:table<ColorString, ValueNoise>,['temp']:table<ColorString, ValueNoise>,['humidity']:table<ColorString, ValueNoise>}
---@field noisesParamsByColor {['landscape']:table<ColorString, NoiseParams>,['temp']:table<ColorString, NoiseParams>,['humidity']:table<ColorString, NoiseParams>}
---@field biomesByName table<string, MapGen.Biome> ---@field biomesByName table<string, MapGen.Biome>
---@field biomesList MapGen.Biome[] ---@field biomesList MapGen.Biome[]
---@field biomesDiagram table ---@field biomesDiagram table
@@ -18,15 +19,20 @@
---@field biomesVerticalScattering number ---@field biomesVerticalScattering number
---@field cavernsByName table<string, MapGen.Cavern> ---@field cavernsByName table<string, MapGen.Cavern>
---@field cavernsList MapGen.Cavern[] ---@field cavernsList MapGen.Cavern[]
---@field trianglesList MapGen.Triangle[] ---@field trianglesList {['landscape']:MapGen.Triangle[],['temp']:MapGen.Triangle[],['humidity']:MapGen.Triangle[]}
---@field tetrahedronsList MapGen.Tetrahedron[] ---@field tetrahedronsList MapGen.Tetrahedron[] **Not supported**
local Layer = { local Layer = {
peaksList = { peaksList = {
landscape = {}, landscape = {},
temp = {}, temp = {},
humidity = {}, humidity = {},
}, },
colorsList = { noisesByColor = {
landscape = {},
temp = {},
humidity = {},
},
noisesParamsByColor = {
landscape = {}, landscape = {},
temp = {}, temp = {},
humidity = {}, humidity = {},
@@ -36,7 +42,11 @@ local Layer = {
biomesDiagram = {}, biomesDiagram = {},
cavernsByName = {}, cavernsByName = {},
cavernsList = {}, cavernsList = {},
trianglesList = {}, trianglesList = {
landscape = {},
temp = {},
humidity = {},
},
tetrahedronsList = {}, tetrahedronsList = {},
} }
@@ -97,9 +107,11 @@ function Layer:new(name, def)
return instance return instance
end end
---@alias MapGen.Layer.Category 'landscape'|'temp'|'humidity'
---Adds a peak to the `Layer.peaksList` ---Adds a peak to the `Layer.peaksList`
---@param category 'landscape'|'temp'|'humidity' ---@param category MapGen.Layer.Category TODO: описание
---@param peak MapGen.Peak ---@param peak MapGen.Peak TODO: описание
function Layer:addPeak(category, peak) function Layer:addPeak(category, peak)
if category == 'landscape' then if category == 'landscape' then
table.insert(self.peaksList.landscape, peak) table.insert(self.peaksList.landscape, peak)
@@ -112,20 +124,49 @@ function Layer:addPeak(category, peak)
end end
end end
---Adds a color to the `Layer.colorsList` ---Adds a NoiseParams color to the `Layer.noisesParamsByColor`
---@param category 'landscape'|'temp'|'humidity' ---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString ---@param color ColorString TODO: описание
function Layer:addColor(category, color) ---@param noiseParams NoiseParams
function Layer:addNoiseParamsColor(category, color, noiseParams)
color = core.colorspec_to_colorstring(core.colorspec_to_table(color)) color = core.colorspec_to_colorstring(core.colorspec_to_table(color))
local noisesParams = self.noisesParamsByColor
if category == 'landscape' then if category == 'landscape' then
table.insert(self.colorsList.landscape, color) assert(noisesParams.landscape[color] == nil, ('The color `%s` already exists.'):format(color))
noisesParams.landscape[color] = noiseParams
elseif category == 'temp' then elseif category == 'temp' then
table.insert(self.colorsList.temp, color) assert(noisesParams.temp[color] == nil, ('The color `%s` already exists.'):format(color))
noisesParams.temp[color] = noiseParams
elseif category == 'humidity' then elseif category == 'humidity' then
table.insert(self.colorsList.humidity, color) assert(noisesParams.humidity[color] == nil, ('The color `%s` already exists.'):format(color))
noisesParams.humidity[color] = noiseParams
else else
error(('There is no color category named %s.'):format(category)) error(('There is no color category named `%s`.'):format(category))
end
end
---Returns noise corresponding to a color.
---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString TODO: описание
---@return ValueNoise
function Layer:getNoiseByColor(category, color)
color = core.colorspec_to_colorstring(core.colorspec_to_table(color))
local noises = self.noisesByColor
if category == 'landscape' then
assert(noises.landscape[color] ~= nil, ('`%s` color does not exist.'):format(color))
return noises.landscape[color]
elseif category == 'temp' then
assert(noises.temp[color] ~= nil, ('`%s` color does not exist.'):format(color))
return noises.temp[color]
elseif category == 'humidity' then
assert(noises.humidity[color] ~= nil, ('`%s` color does not exist.'):format(color))
return noises.humidity[color]
else
error(('There is no color category named `%s`.'):format(category))
end end
end end
@@ -164,6 +205,19 @@ local function getBiomesNamesByHeight(height, biomesList)
return biomesNames, biomes return biomesNames, biomes
end end
---Initializes noise for generation.
---
---Should only be called after the mapgen object has been loaded.
function Layer:initNoises()
local noises = self.noisesByColor
for category, noisesParams in pairs(self.noisesParamsByColor) do
for color, noiseParams in pairs(noisesParams) do
noises[category][color] = core.get_value_noise(noiseParams)
end
end
end
---Initialization of the biome diagram using the Voronoi method. ---Initialization of the biome diagram using the Voronoi method.
function Layer:initBiomesDiagram() function Layer:initBiomesDiagram()
local diagram = self.biomesDiagram local diagram = self.biomesDiagram

View File

@@ -1,56 +1,35 @@
---@class MapGen.Peak ---@class MapGen.Peak
---@field id number ---@field id number
---@field getPeakPos fun():vector ---@field getPeakPos fun():vector
---@field getMultinoiseParams fun():MapGen.Peak.MultinoiseParams ---@field getColor fun():ColorString
---@field getMultinoise fun():MapGen.Peak.Multinoise
---@field initMultinoise fun()
---@field getGroups fun():table<string, number> ---@field getGroups fun():table<string, number>
local Peak = { local Peak = {
id = 0, id = 0,
} }
-- --- Helpers functions ---
---@param multinoiseParams MapGen.Peak.MultinoiseParams
---@return MapGen.Peak.Multinoise
local function multinoiseParamsToMultinoise(multinoiseParams)
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise
local multinoise = {}
for noiseName, noiseParams in pairs(multinoiseParams) do
multinoise[noiseName] = core.get_value_noise(noiseParams)
end
return multinoise
end
-- --- Class registration --- -- --- Class registration ---
---@param peakPos vector ---Definition table for the `MapGen.Peak`.
---@param multinoiseParams MapGen.Peak.MultinoiseParams ---
---@param groups? table<string, number> ---**Only for EmmyLua.**
---@return MapGen.Peak ---@class MapGen.PeakDef
function Peak:new(peakPos, multinoiseParams, groups) ---@field pos vector
---@type vector ---@field color ColorString
local _peakPos = peakPos ---@field groups? table<string, number>
---@type MapGen.Peak.MultinoiseParams
local _multinoiseParams = multinoiseParams
---@param def MapGen.PeakDef
---@return MapGen.Peak
function Peak:new(def)
local _pos = def.pos
local _color = def.color
local _groups local _groups
if groups == nil then if def.groups == nil then
_groups = {} _groups = {}
else else
_groups = groups _groups = def.groups
end end
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise
local _multinoise = {} -- note: must be empty until luanti mapgen objects are loaded.
---@type MapGen.Peak ---@type MapGen.Peak
local instance = setmetatable({}, local instance = setmetatable({},
{ {
@@ -60,35 +39,15 @@ function Peak:new(peakPos, multinoiseParams, groups)
}) })
function instance:getPeakPos() function instance:getPeakPos()
return vector.new(_peakPos.x, _peakPos.y, _peakPos.z) return _pos:copy()
end end
function instance:getMultinoiseParams() function instance:getColor()
return _multinoiseParams return _color
end
function instance:getMultinoise()
if table.is_empty(_multinoise) then
error('Attempt to get `MapGen.Triangulation.FakePeak` noise (empty noise).')
end
return _multinoise
end
function instance:initMultinoise()
if not table.is_empty(_multinoise) then
Logger.warningLog('Multinoise has already been initialized. Re-initialization is not recommended.')
end
if table.is_empty(_multinoiseParams) then
error('Attempt to initialize `MapGen.Triangulation.FakePeak` noise (noise parameter empty).')
end
_multinoise = multinoiseParamsToMultinoise(_multinoiseParams)
end end
function instance:getGroups() function instance:getGroups()
return _groups return table.copy_with_metatables(_groups)
end end
return instance return instance

View File

@@ -17,8 +17,7 @@ local FakePeak = Class.extend(Peak, {})
---@param peakPos vector ---@param peakPos vector
---@return MapGen.Peak ---@return MapGen.Peak
function FakePeak:new(peakPos) function FakePeak:new(peakPos)
---@diagnostic disable-next-line: missing-fields, param-type-not-match local instance = Peak:new({pos = peakPos, color = '#000000'})
local instance = Peak:new(peakPos, {}, 1)
return instance return instance
end end

View File

@@ -133,41 +133,16 @@ mapGenerator:register2DPeaks('midgard',
v(200, 0, 100), v(200, 0, 100),
v(-300, 0, -200), v(-300, 0, -200),
})--]] })--]]
--[[
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', {x=250, y=250}, {
['#396FFFFF'] = {noise = {
landscapeNoise = land1,
tempNoise = climat2,
humidityNoise = climat2,
}
},
['#3FFF26FF'] = {noise = {
landscapeNoise = land2,
tempNoise = climat2,
humidityNoise = climat2,
}
}
})--]]
local N = 32 for _, c in ipairs({'landscape', 'temp', 'humidity'}) do
for x = -10, 10 do mapGenerator:registerLayerNoiseColor('midgard', c, '#4978c6', land1)
for z = - 10, 10 do mapGenerator:registerLayerNoiseColor('midgard', c, '#77ea72', land2)
local land
if math.random(0,1) == 0 then
land = land1
else
land = land2
end
print(x)
mapGenerator:register2DPeak('midgard', vector.new(x*N, 0, z*N), {
landscapeNoise = land,
tempNoise = climat2,
humidityNoise = climat2,
})
end
end end
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 64,{x=8, y=8}, 'landscape')
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=8, y=8}, 'temp')
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=8, y=8}, 'humidity')
--[[ --[[
mapGenerator:registerCavern('midgard', 'cavern1', { mapGenerator:registerCavern('midgard', 'cavern1', {
minY = -48, minY = -48,

Binary file not shown.