forked from Apancore.Studio/simple-makes-complex
Annotations changes: clarification of types, addition of missing annotations
This commit is contained in:
@@ -31,23 +31,24 @@ local Region = require("MapGen.Region")
|
||||
---@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 = {
|
||||
layersByName = {},
|
||||
layersList = {},
|
||||
multinoiseInitialized = false,
|
||||
isRunning = false,
|
||||
isRunning = false,
|
||||
}
|
||||
|
||||
---@param nodeIDs table<string, string>
|
||||
---@return MapGen
|
||||
function MapGen:new(nodeIDs)
|
||||
---@type MapGen
|
||||
local instance = setmetatable({
|
||||
layersByName = {},
|
||||
layersList = {},
|
||||
nodeIDs = nodeIDs,
|
||||
--registeredRegions = {},
|
||||
nodeIDs = nodeIDs,
|
||||
}, {__index = self})
|
||||
|
||||
return instance
|
||||
end
|
||||
|
||||
---Determines which layer a node belongs to based on its height coordinate.
|
||||
---@param yPos number
|
||||
---@return MapGen.Layer?
|
||||
function MapGen:getLayerByHeight(yPos)
|
||||
@@ -66,6 +67,9 @@ function MapGen:getLayerByHeight(yPos)
|
||||
end
|
||||
end
|
||||
|
||||
---Mark the world area between two heights as a `MapGen.Layer`.
|
||||
---
|
||||
---Note: layers must not overlap.
|
||||
---@param name string
|
||||
---@param minY number
|
||||
---@param maxY number
|
||||
@@ -87,12 +91,15 @@ function MapGen:RegisterLayer(name, minY, maxY)
|
||||
end)
|
||||
end
|
||||
|
||||
---@param layerName string
|
||||
---@param minPos table
|
||||
---@param maxPos table
|
||||
---@param multinoise table
|
||||
---@param regionIs2D boolean?
|
||||
---@param weightFactor number?
|
||||
---Mark a region of the world as a cubic region by two opposite vertices.
|
||||
---
|
||||
---Regions must be included in layers and may overlap each other.
|
||||
---@param layerName string The name of the layer in which the new region will be included.
|
||||
---@param minPos vector
|
||||
---@param maxPos vector
|
||||
---@param multinoise table Noise that will be assigned to the region and that will influence map generation
|
||||
---@param regionIs2D boolean? If true, the transmitted Y coordinate will be overwritten by the layer boundaries.
|
||||
---@param weightFactor number? The coefficient by which the reduction in the impact of regional noise on generation will be calculated. If 0, there will be no reduction.
|
||||
function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D, weightFactor)
|
||||
---@type MapGen.Layer
|
||||
local layer = self.layersByName[layerName]
|
||||
@@ -106,6 +113,8 @@ function MapGen:RegisterRegion(layerName, minPos, maxPos, multinoise, regionIs2D
|
||||
maxPos.y = layer.maxY
|
||||
end
|
||||
|
||||
--TODO: Добавить проверку, что регион не выходит за границы слоя или/и автоматически обрубать регион до слоя
|
||||
|
||||
if weightFactor == nil then
|
||||
weightFactor = 1
|
||||
end
|
||||
@@ -131,9 +140,18 @@ function MapGen:initRegionsMultinoise()
|
||||
self.multinoiseInitialized = true
|
||||
end
|
||||
|
||||
|
||||
---@return number
|
||||
local function calculateWeight(minPos, maxPos, x, z, weightFactor)
|
||||
---Calculate the weight for noise based on
|
||||
---the distance of the point from the center of the region.
|
||||
---
|
||||
---If the `weightFactor` is `0`, then the region's
|
||||
---weight is not calculated and is always equal to `1`.
|
||||
---@param minPos vector
|
||||
---@param maxPos vector
|
||||
---@param x number
|
||||
---@param z number
|
||||
---@param weightFactor number
|
||||
---@return number
|
||||
local function calculateWeight2D(minPos, maxPos, x, z, weightFactor)
|
||||
if weightFactor == 0 then
|
||||
return 1
|
||||
end
|
||||
@@ -187,7 +205,7 @@ local function generatorHandler(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, region.getWeightFactor())
|
||||
local weight = calculateWeight2D(region:getMinPos(), region:getMaxPos(), x, z, region.getWeightFactor())
|
||||
|
||||
noiseHeightValue = noiseHeightValue + ( noise:get_2d({x = x, y = z}) * weight )
|
||||
end
|
||||
@@ -232,9 +250,12 @@ function MapGen:onMapGenerated(minPos, maxPos, blockseed)
|
||||
voxelManip:write_to_map()
|
||||
end
|
||||
|
||||
---Run world generation through this mapgen object
|
||||
---
|
||||
---Note: only one instance of a `MapGen` class can be running.
|
||||
function MapGen:run()
|
||||
if MapGen.isRunning then
|
||||
error('Only one instance of a `MapGen` class can be used.')
|
||||
error('Only one instance of a `MapGen` class can be running.')
|
||||
end
|
||||
|
||||
core.register_on_generated(function(...)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
---@field name string
|
||||
---@field minY number
|
||||
---@field maxY number
|
||||
---@field regionsList table
|
||||
---@field regionsList table
|
||||
local Layer = {
|
||||
regionsList = {}
|
||||
}
|
||||
@@ -30,12 +30,14 @@ end
|
||||
---@param xPos number
|
||||
---@param yPos number
|
||||
---@param zPos number
|
||||
---@return table
|
||||
---@return MapGen.Region[]
|
||||
function Layer:getRegionsByPos(xPos, yPos, zPos)
|
||||
--- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
--- учитывая тот факт, что эта функция вызывается для каждой ноды в on_generated
|
||||
--- и может быть даже не один раз
|
||||
--- Можно подумать над тем, чтобы использовать сортированный список
|
||||
-- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
-- учитывая тот факт, что эта функция вызывается для каждой ноды в on_generated
|
||||
-- и может быть даже не один раз
|
||||
-- Можно подумать над тем, чтобы использовать сортированный список
|
||||
|
||||
---@type MapGen.Region[]
|
||||
local regions = {}
|
||||
|
||||
---@param region MapGen.Region
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
-- TODO: replace `table` with `vector` for minPos and maxPos
|
||||
---@class MapGen.Region
|
||||
---@field getMinPos fun():vector
|
||||
---@field getMaxPos fun():vector
|
||||
---@field getMultinoiseParams fun():MapGen.Region.MultinoiseParams
|
||||
---@field getMultinoise fun():MapGen.Region.Multinoise
|
||||
---@field initMultinoise fun()
|
||||
---@field getWeightFactor fun():number
|
||||
local Region = {}
|
||||
|
||||
---@param multinoiseParams MapGen.Region.MultinoiseParams -- ??? TODO: should be it's own class in EmmyLua
|
||||
---@param multinoiseParams MapGen.Region.MultinoiseParams
|
||||
---@return MapGen.Region.Multinoise
|
||||
local function multinoiseParamsToMultinoise(multinoiseParams)
|
||||
---@type MapGen.Region.Multinoise
|
||||
@@ -15,37 +20,43 @@ local function multinoiseParamsToMultinoise(multinoiseParams)
|
||||
return multinoise
|
||||
end
|
||||
|
||||
---@param minPos table
|
||||
---@param maxPos table
|
||||
---@param minPos vector
|
||||
---@param maxPos vector
|
||||
---@param multinoiseParams MapGen.Region.MultinoiseParams
|
||||
---@param weightFactor number
|
||||
---@return MapGen.Region
|
||||
function Region:new(minPos, maxPos, multinoiseParams, weightFactor)
|
||||
|
||||
---@type vector
|
||||
local _minPos = minPos
|
||||
---@type vector
|
||||
local _maxPos = maxPos
|
||||
---@type MapGen.Region.MultinoiseParams
|
||||
local _multinoiseParams = multinoiseParams
|
||||
|
||||
if weightFactor < 0 then
|
||||
weightFactor = 1
|
||||
end
|
||||
|
||||
---@type MapGen.Region.Multinoise
|
||||
local _multinoise = {}
|
||||
|
||||
---@type MapGen.Region
|
||||
local instance = setmetatable({}, {__index = self})
|
||||
|
||||
---@return {x:number, y:number, z:number}
|
||||
function instance:getMinPos()
|
||||
return _minPos
|
||||
end
|
||||
|
||||
---@return vector
|
||||
function instance:getMaxPos()
|
||||
return _maxPos
|
||||
end
|
||||
|
||||
function instance:getMultinoiseParams()
|
||||
return _multinoiseParams
|
||||
end
|
||||
|
||||
---@return MapGen.Region.Multinoise
|
||||
function instance:getMultinoise()
|
||||
return _multinoise
|
||||
end
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
---For EmmyLua only
|
||||
|
||||
-- TODO: Когда в хелпер добавятся аннотации шумов, надо будет удалить этот класс и заменить его на хелперский
|
||||
---@class MapGen.Region.NoiseParams
|
||||
---@field offset number
|
||||
---@field scale number
|
||||
---@field spread vector -- TODO: describe vector class from Luanti (push Alek)
|
||||
---@field spread vector
|
||||
---@field seed number
|
||||
---@field octaves number
|
||||
---@field persistence number
|
||||
|
||||
Reference in New Issue
Block a user