2 Commits

Author SHA1 Message Date
57de1f4b42 MapGen: add annotations 2026-01-12 19:43:54 +03:00
d0eba35e8d MapGen: remove unnecessary code 2026-01-12 18:43:09 +03:00
2 changed files with 33 additions and 132 deletions

View File

@@ -132,7 +132,7 @@ end
---
---Note: layers must not overlap.
---@param name string The name that will be assigned to the layer. The name must be unique for each layer.
---@param def MapGen.LayerDef Layer description table, see details `MapGen.LayerDef`
---@param def MapGen.LayerDef Layer description table, see details `MapGen.LayerDef`.
function MapGen:registerLayer(name, def)
-- Layers with the same name cannot exist.
assert(self.layersByName[name] == nil, ('A layer named `%s` is already registered.'):format(name))
@@ -152,10 +152,11 @@ function MapGen:registerLayer(name, def)
end)
end
---@param layerName string
---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString TODO: описание
---@param noiseParams NoiseParams
---Register the color and the noise parameters that correspond to it.
---@param layerName string The name of the layer for which the color will be registered.
---@param category MapGen.Layer.Category Category indicating what will be generated by noise.
---@param color ColorString Noise color. Must be unique.
---@param noiseParams NoiseParams Fractal noise parameters.
function MapGen:registerLayerNoiseColor(layerName, category, color, noiseParams)
---@type MapGen.Layer
local layer = self.layersByName[layerName]
@@ -168,8 +169,8 @@ end
---Mark the world area between two heights as a `MapGen.StaticLayer`.
---
---Note: layers must not overlap.
---@param name string The name that will be assigned to the static layer. The name must be unique for each layer.
---@param def MapGen.StaticLayerDef Static layer description table, see details `MapGen.StaticLayerDef`
---@param name string The name that will be assigned to the static layer. The name must be unique for each layer.
---@param def MapGen.StaticLayerDef Static layer description table, see details `MapGen.StaticLayerDef`.
function MapGen:registerStaticLayer(name, def)
-- Static layer with the same name cannot exist.
assert(self.staticsByName[name] == nil, ('A static layer named `%s` is already registered.'):format(name))
@@ -189,12 +190,12 @@ function MapGen:registerStaticLayer(name, def)
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 color that will affect 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 category MapGen.Layer.Category TODO: описание
---@param def MapGen.PeakDef
---@param layerName string The name of the layer in which the new peak will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
function MapGen:registerPeak(layerName, category, def)
---@type MapGen.Layer
local layer = self.layersByName[layerName]
@@ -209,13 +210,13 @@ function MapGen:registerPeak(layerName, category, def)
layer:addPeak(category, peak)
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 color that will affect the generation.
---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field.
---
---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 category MapGen.Layer.Category TODO: описание
---@param def MapGen.PeakDef
---@param layerName string The name of the layer in which the new peak will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
function MapGen:register2DPeak(layerName, category, def)
def = table.copy_with_metatables(def)
@@ -234,22 +235,22 @@ end
---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field.
---
---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 category MapGen.Layer.Category TODO: описание
---@param defs MapGen.PeakDef[]
---@param layerName string The name of the layer in which the new peaks will be included.
---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
---@param defs MapGen.PeakDef[] Peaks description table, see details `MapGen.PeakDef`.
function MapGen:register2DPeaks(layerName,category, defs)
for _, def in ipairs(defs) do
self:register2DPeak(layerName, category, def)
end
end
---TODO: описание
---Extract the `MapGen.Peak` from the image and assign them all a specific color that will affect the generation.
---@param layerName string The name of the layer in which the new peaks will be included.
---@param map string TODO: описание
---@param scale number TODO: описание
---@param zeroPos Position2d TODO: описание
---@param category MapGen.Layer.Category TODO: описание
---@param groups? table<string, number>
---@param map string The name of the image from which the peaks will be extracted. It should be located in the `<mod_name>/textures/maps` folder and have the following format `<image_name>.ff`
---@param scale number Multiplier of how many blocks (nodes) will be between image pixels. It is not recommended to set the value below 50 for performance reasons.
---@param zeroPos Position2d The position in pixels where the zero world coordinate will be in the image.
---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
---@param groups? table<string, number> Groups that will be added to all peaks.
function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category, groups)
local modPath = core.get_modpath(core.get_current_modname())
local peaks = ff2luat(modPath .. '/textures/maps/' .. map)
@@ -266,8 +267,6 @@ function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category,
groups = groups,
}
print(peakPos.x, peakPos.y)
self:register2DPeak(layerName, category, def)
end
end
@@ -317,6 +316,7 @@ function MapGen:initLayersTriangultaion()
end
end
---Initialize all layer noises. This should be called after the map object is loaded.
function MapGen:initLayersNoises()
---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do
@@ -556,48 +556,6 @@ local function calcNoise2dValue(category, layer, triangle, x, y, z)
return (noiseValue / totalWeight)
end
--[[
---Returns the weighted values of 2d noises.
---@param triangle MapGen.Triangle
---@param x number
---@param y number
---@param z number
---@return number, number, number
local function calcNoises2dValues(triangle, x, y, z)
local peak1 = triangle.p1
local peak2 = triangle.p2
local peak3 = triangle.p3
local noiseHeightValue = 0.0
local noiseTempValue = 0.0
local noiseHumidityValue = 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)
noiseHeightValue = noiseHeightValue + peak1:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
noiseTempValue = noiseTempValue + peak1:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak1:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(triangle.h2, x, y, z)
noiseHeightValue = noiseHeightValue + peak2:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
noiseTempValue = noiseTempValue + peak2:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak2:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
weight = calcWeight(triangle.h3, x, y, z)
noiseHeightValue = noiseHeightValue + peak3:getMultinoise().landscapeNoise:get_2d({x = x, y = z}) * weight
noiseTempValue = noiseTempValue + peak3:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight
noiseHumidityValue = noiseHumidityValue + peak3:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight
totalWeight = totalWeight + weight
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.
@@ -644,62 +602,6 @@ function MapGen:getNoises2dValue(category, layer, x, y, 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
--[[
---Calculates a smoothed 2D noise value based on a preliminary space partitioning.
---
---Returns `nil` if the point is not included in any triangle.
---@param layer MapGen.Layer
---@param x number
---@param y number
---@param z number
---@return number?, number?, number?
function MapGen:getNoises2dValues(layer, x, y, z)
if (pastNoise2DCalc.x ~= nil and pastNoise2DCalc.z ~= nil) and (x == pastNoise2DCalc.x and z == pastNoise2DCalc.z) then
return pastNoise2DCalc.noiseValues.height, pastNoise2DCalc.noiseValues.temp, pastNoise2DCalc.noiseValues.humidity
end
if pastNoise2DCalc.triangle ~= nil and pointInTriangle(x, z, pastNoise2DCalc.triangle) then
local noiseHeightValue, noiseTempValue, noiseHumidityValue = calcNoises2dValues(pastNoise2DCalc.triangle, x, y, z)
noiseTempValue, noiseHumidityValue = layer:calcTemp(noiseTempValue, y), layer:calcHumidity(noiseHumidityValue, y)
pastNoise2DCalc.x = x
pastNoise2DCalc.z = z
pastNoise2DCalc.noiseValues = {
height = noiseHeightValue,
temp = noiseTempValue,
humidity = noiseHumidityValue,
}
return noiseHeightValue, noiseTempValue, noiseHumidityValue
end
---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList) do
if pointInTriangle(x, z,triangle) then
local noiseHeightValue, noiseTempValue, noiseHumidityValue = calcNoises2dValues(triangle, x, y, z)
noiseTempValue, noiseHumidityValue = layer:calcTemp(noiseTempValue, y), layer:calcHumidity(noiseHumidityValue, y)
pastNoise2DCalc.triangle = triangle
pastNoise2DCalc.x = x
pastNoise2DCalc.z = z
pastNoise2DCalc.noiseValues = {
height = noiseHeightValue,
temp = noiseTempValue,
humidity = noiseHumidityValue,
}
return noiseHeightValue, noiseTempValue, noiseHumidityValue
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
--]]
--[[
3D noise smoothing is too slow, so it was decided to stop using it.
@@ -825,8 +727,6 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
noiseTempValue = layer:calcTemp(noiseTempValue, y)
noiseHumidityValue = layer:calcHumidity(noiseHumidityValue, y)
--print(noiseHeightValue, noiseTempValue, noiseHumidityValue)
generateNode(mapGenerator, layer, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end

View File

@@ -107,11 +107,12 @@ function Layer:new(name, def)
return instance
end
---A category of noise that affects specific map generation objects.
---@alias MapGen.Layer.Category 'landscape'|'temp'|'humidity'
---Adds a peak to the `Layer.peaksList`
---@param category MapGen.Layer.Category TODO: описание
---@param peak MapGen.Peak TODO: описание
---@param category MapGen.Layer.Category
---@param peak MapGen.Peak
function Layer:addPeak(category, peak)
if category == 'landscape' then
table.insert(self.peaksList.landscape, peak)
@@ -125,8 +126,8 @@ function Layer:addPeak(category, peak)
end
---Adds a NoiseParams color to the `Layer.noisesParamsByColor`
---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString TODO: описание
---@param category MapGen.Layer.Category
---@param color ColorString
---@param noiseParams NoiseParams
function Layer:addNoiseParamsColor(category, color, noiseParams)
color = core.colorspec_to_colorstring(core.colorspec_to_table(color))
@@ -148,8 +149,8 @@ function Layer:addNoiseParamsColor(category, color, noiseParams)
end
---Returns noise corresponding to a color.
---@param category MapGen.Layer.Category TODO: описание
---@param color ColorString TODO: описание
---@param category MapGen.Layer.Category
---@param color ColorString
---@return ValueNoise
function Layer:getNoiseByColor(category, color)
color = core.colorspec_to_colorstring(core.colorspec_to_table(color))