MapGen: Zoning support 4/7. #17

This commit is contained in:
Van
2026-01-20 16:14:34 +03:00
parent 4b9afcf97d
commit 37d759e48a
2 changed files with 61 additions and 8 deletions

View File

@@ -114,6 +114,29 @@ function MapGen:getStaticByHeight(yPos)
end end
end end
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns nil.
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
---@param category MapGen.Layer.Category The category in which the peak is located.
---@param pos Position Coordinate of the desired peak.
---@return MapGen.Peak?
function MapGen:getPeakByPos(layer, category, pos)
if type(layer) == 'string' then
local layerName = layer
layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
end
pos = vector.new(pos)
---@param peak MapGen.Peak
for _, peak in ipairs(layer.peaksList[category]) do
if peak:getPeakPos() == pos then
return peak
end
end
end
-- --- REGISTRATION METHODS --- -- --- REGISTRATION METHODS ---
@@ -182,7 +205,7 @@ end
---Mark a world point as `MapGen.Peak` and assign it a specific color 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. ---Note: The peaks shouldn't overlap.
---@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 category MapGen.Layer.Category Category indicating what will be generated by peak. ---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`. ---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
@@ -192,7 +215,9 @@ function MapGen:registerPeak(layerName, category, def)
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName)) assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
--TODO: добавить проверку, что координата пиков не совпадают assert(self:getPeakByPos(layer, category, def.pos) == nil,
('In the `%s` layer in the `%s` category there is already a peak with coordinates: %s, %s, %s.'):format
(layerName, category, def.pos.x, def.pos.y, def.pos.z))
---@type MapGen.Peak ---@type MapGen.Peak
local peak = Peak:new(def) local peak = Peak:new(def)
@@ -203,7 +228,7 @@ end
---Mark a world point as `MapGen.Peak` and assign it a specific color 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. ---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. ---Note: The peaks shouldn't overlap.
---@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 category MapGen.Layer.Category Category indicating what will be generated by peak. ---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`. ---@param def MapGen.PeakDef Peak description table, see details `MapGen.PeakDef`.
@@ -224,7 +249,7 @@ 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.
---The Y coordinate will be forced to zero, and the `is2d` group will be added to the groups field. ---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. ---Note: The peaks shouldn't overlap.
---@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 category MapGen.Layer.Category Category indicating what will be generated by peaks. ---@param category MapGen.Layer.Category Category indicating what will be generated by peaks.
---@param defs MapGen.PeakDef[] Peaks description table, see details `MapGen.PeakDef`. ---@param defs MapGen.PeakDef[] Peaks description table, see details `MapGen.PeakDef`.
@@ -262,6 +287,25 @@ function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category,
end end
end end
---Adds groups to the already registered groups in the peak by coordinate.
---If the peak does not exist, an error will be raised.
---@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 pos Position The position of the peak to which the group should be added.
---@param groups table<string, number> Groups to be added.
function MapGen:addGroupsToPeakByPos(layerName, category, pos, groups)
---@type MapGen.Layer
local layer = self.layersByName[layerName]
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
local peak = self:getPeakByPos(layer, category, pos)
assert( peak ~= nil, ('In the `%s` layer in the `%s` category there is no peak with coordinates: %s, %s, %s.'):format
(layerName, category, pos.x, pos.y, pos.z))
peak:addGroups(groups)
end
---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`. ---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`.
---@param layerName string The name of the layer in which the new biome will be included. ---@param layerName string The name of the layer in which the new biome will be included.
---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome. ---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.

View File

@@ -1,8 +1,9 @@
---@class MapGen.Peak ---@class MapGen.Peak
---@field id number ---@field id number
---@field getPeakPos fun(self):vector ---@field getPeakPos fun(self):vector
---@field getColor fun(self):ColorString ---@field getColor fun(self):ColorString
---@field getGroups fun(self):table<string, number> ---@field getGroups fun(self):table<string, number>
---@field addGroups fun(self, groups:table<string, number>)
local Peak = { local Peak = {
id = 0, id = 0,
} }
@@ -50,6 +51,14 @@ function Peak:new(def)
return table.copy_with_metatables(_groups) return table.copy_with_metatables(_groups)
end end
function instance:addGroups(groups)
-- Groups that cannot be overwritten
groups.is2d = nil
groups.is3d = nil
_groups = table.merge(_groups, groups, true)
end
return instance return instance
end end