MapGen: Added check for points belonging to the triangulation area
This commit is contained in:
@@ -232,6 +232,128 @@ function MapGen:isCavern(layer, x, y, z)
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Creates a unique key for a vertex (using X and Z coordinates).
|
||||||
|
---@param peak MapGen.Peak
|
||||||
|
---@return string
|
||||||
|
local function vertexKey(peak)
|
||||||
|
local pos = peak:getPeakPos()
|
||||||
|
|
||||||
|
return string.format("%.6f,%.6f", pos.x, pos.z)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Creates a canonical (sorted) key for an edge.
|
||||||
|
---@param edge MapGen.Triangulation.Edge
|
||||||
|
---@return string
|
||||||
|
local function edgeKey(edge)
|
||||||
|
local k1 = vertexKey(edge.p1)
|
||||||
|
local k2 = vertexKey(edge.p2)
|
||||||
|
|
||||||
|
if k1 < k2 then
|
||||||
|
return k1 .. "|" .. k2
|
||||||
|
else
|
||||||
|
return k2 .. "|" .. k1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
---Calculates the outer boundary of a triangulated region.
|
||||||
|
---Works on the XZ horizontal plane.
|
||||||
|
---@param triangles MapGen.Triangle[]
|
||||||
|
---@return table
|
||||||
|
function MapGen:getTriangulateBoundaryVertices(triangles)
|
||||||
|
-- Count how many triangles share each edge
|
||||||
|
local edgeCount = {}
|
||||||
|
---@type table<string, MapGen.Triangulation.Edge>
|
||||||
|
local edgeData = {}
|
||||||
|
|
||||||
|
for _, triangle in ipairs(triangles) do
|
||||||
|
local edges = {
|
||||||
|
triangle.e1,
|
||||||
|
triangle.e2,
|
||||||
|
triangle.e3
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, edge in ipairs(edges) do
|
||||||
|
local key = edgeKey(edge)
|
||||||
|
|
||||||
|
edgeCount[key] = (edgeCount[key] or 0) + 1
|
||||||
|
edgeData[key] = edge
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Boundary edges belong to exactly ONE triangle
|
||||||
|
---@type MapGen.Triangulation.Edge[]
|
||||||
|
local boundaryEdges = {}
|
||||||
|
|
||||||
|
for key, count in pairs(edgeCount) do
|
||||||
|
if count == 1 then
|
||||||
|
table.insert(boundaryEdges, edgeData[key])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if #boundaryEdges == 0 then
|
||||||
|
return {}
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Build adjacency list for boundary vertices
|
||||||
|
---@type table<string, MapGen.Peak>
|
||||||
|
local adjacency = {}
|
||||||
|
---@type table<string, MapGen.Peak>
|
||||||
|
local vertexData = {}
|
||||||
|
|
||||||
|
for _, edge in ipairs(boundaryEdges) do
|
||||||
|
local k1 = vertexKey(edge.p1)
|
||||||
|
local k2 = vertexKey(edge.p2)
|
||||||
|
|
||||||
|
vertexData[k1] = edge.p1
|
||||||
|
vertexData[k2] = edge.p2
|
||||||
|
|
||||||
|
adjacency[k1] = adjacency[k1] or {}
|
||||||
|
adjacency[k2] = adjacency[k2] or {}
|
||||||
|
|
||||||
|
table.insert(adjacency[k1], k2)
|
||||||
|
table.insert(adjacency[k2], k1)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Walk along boundary edges to create ordered vertex list
|
||||||
|
---@type Position[]
|
||||||
|
local boundary = {}
|
||||||
|
---@type table<string, boolean>
|
||||||
|
local visited = {}
|
||||||
|
|
||||||
|
if boundaryEdges[1] == nil then
|
||||||
|
error()
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type string?
|
||||||
|
local currentKey = vertexKey(boundaryEdges[1].p1)
|
||||||
|
|
||||||
|
while currentKey and not visited[currentKey] do
|
||||||
|
visited[currentKey] = true
|
||||||
|
local pos = vertexData[currentKey]:getPeakPos()
|
||||||
|
|
||||||
|
table.insert(boundary, {
|
||||||
|
x = pos.x,
|
||||||
|
y = 0,
|
||||||
|
z = pos.z
|
||||||
|
})
|
||||||
|
|
||||||
|
-- Move to next unvisited neighbor
|
||||||
|
local nextKey = nil
|
||||||
|
|
||||||
|
for _, neighborKey in ipairs(adjacency[currentKey] or {}) do
|
||||||
|
if not visited[neighborKey] then
|
||||||
|
nextKey = neighborKey
|
||||||
|
break
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
currentKey = nextKey
|
||||||
|
end
|
||||||
|
|
||||||
|
return boundary
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- --- REGISTRATION METHODS ---
|
-- --- REGISTRATION METHODS ---
|
||||||
|
|
||||||
@@ -446,6 +568,12 @@ end
|
|||||||
---@param regionName string The name to be assigned to the region. The name must be unique for each region.
|
---@param regionName string The name to be assigned to the region. The name must be unique for each region.
|
||||||
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
|
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
|
||||||
function MapGen:registerRegion(layerName, regionName, def)
|
function MapGen:registerRegion(layerName, regionName, def)
|
||||||
|
if regionName == 'landscape_boundary' or
|
||||||
|
regionName == 'temp_boundary' or
|
||||||
|
regionName == 'humidity_boundary' then
|
||||||
|
error(('The name `%s` is reserved for special regions and cannot be used.'):format(regionName))
|
||||||
|
end
|
||||||
|
|
||||||
local layer = self:getLayerByName(layerName)
|
local layer = self:getLayerByName(layerName)
|
||||||
|
|
||||||
local region = Region:new(regionName, def)
|
local region = Region:new(regionName, def)
|
||||||
@@ -454,15 +582,32 @@ function MapGen:registerRegion(layerName, regionName, def)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- --- INITIALIZATION METHODS ---
|
-- --- INITIALIZATION METHODS ---
|
||||||
|
|
||||||
|
---Initialize the boundary region for a triangulated area.
|
||||||
|
---@param layer MapGen.Layer
|
||||||
|
---@param category MapGen.Layer.Category
|
||||||
|
---@param triangles MapGen.Triangle[]
|
||||||
|
function MapGen:initTriangultaionBoundaryRegion(layer, category, triangles)
|
||||||
|
local regionVertices = self:getTriangulateBoundaryVertices(triangles)
|
||||||
|
|
||||||
|
local regionName = category..'_boundary'
|
||||||
|
|
||||||
|
local region = Region:new(regionName, {vertices = regionVertices})
|
||||||
|
|
||||||
|
layer:addRegion(regionName, region)
|
||||||
|
end
|
||||||
|
|
||||||
---Divide all layers into triangles whose vertices are the `Mapgen.Peak` belonging to these layers.
|
---Divide all layers into triangles whose vertices are the `Mapgen.Peak` belonging to these layers.
|
||||||
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
|
||||||
for category, peaks in pairs(layer.peaksList) do
|
for category, peaks in pairs(layer.peaksList) do
|
||||||
|
local triangles = Triangulation.triangulate(peaks)
|
||||||
|
|
||||||
layer.trianglesList[category] = Triangulation.triangulate(peaks)
|
layer.trianglesList[category] = triangles
|
||||||
|
self:initTriangultaionBoundaryRegion(layer, category, triangles)
|
||||||
-- layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList) -- 3D not supported
|
-- layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList) -- 3D not supported
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -712,6 +857,10 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
---@param triangle MapGen.Triangle
|
---@param triangle MapGen.Triangle
|
||||||
for _, triangle in ipairs(layer.trianglesList[category]) do
|
for _, triangle in ipairs(layer.trianglesList[category]) do
|
||||||
if pointInTriangle2D(x, z, triangle) then
|
if pointInTriangle2D(x, z, triangle) then
|
||||||
@@ -847,9 +996,23 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
|||||||
|
|
||||||
-- If the value of the noise is `nil`,
|
-- If the value of the noise is `nil`,
|
||||||
-- then the point does not fall within any of the triangles.
|
-- then the point does not fall within any of the triangles.
|
||||||
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) or 0.0
|
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z)
|
||||||
local noiseTempValue = mapGenerator:getNoises2dValue('temp', layer, x, y, z) or 0.0
|
if noiseHeightValue == nil then
|
||||||
local noiseHumidityValue = mapGenerator:getNoises2dValue('humidity', layer, x, y, z) or 0.0
|
data[index] = core.CONTENT_IGNORE
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local noiseTempValue = mapGenerator:getNoises2dValue('temp', layer, x, y, z)
|
||||||
|
if noiseTempValue == nil then
|
||||||
|
data[index] = core.CONTENT_IGNORE
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local noiseHumidityValue = mapGenerator:getNoises2dValue('humidity', layer, x, y, z)
|
||||||
|
if noiseHumidityValue == nil then
|
||||||
|
data[index] = core.CONTENT_IGNORE
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- We recalculate the climate based on altitude.
|
-- We recalculate the climate based on altitude.
|
||||||
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ local land2 = {
|
|||||||
local land3 = {
|
local land3 = {
|
||||||
offset = 40,
|
offset = 40,
|
||||||
scale = 20,
|
scale = 20,
|
||||||
spread = {x = 5, y = 40, z = 5},
|
spread = {x = 45, y = 20, z = 45},
|
||||||
seed = 472,
|
seed = 472,
|
||||||
octaves = 3,
|
octaves = 3,
|
||||||
persistence = 0.3,
|
persistence = 0.3,
|
||||||
|
|||||||
Reference in New Issue
Block a user