MapGen: Optimization: add Added detection of adjacent triangles. Close #14

This commit is contained in:
Van
2026-01-15 13:49:18 +03:00
parent c2b0c54236
commit 4f4b268d3c
4 changed files with 87 additions and 33 deletions

View File

@@ -1,28 +1,18 @@
local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_id
---@class MapGen.PastCalcNoiseValue
local pastCalcNoiseValue = {
---@type MapGen.Triangle?
triangle = nil,
---@type integer?
x = nil,
---@type integer?
z = nil,
---@type number?
noiseValue = nil,
}
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, noiseValue:number?}
---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
local pastNoise2DCalc = {
landscape = pastCalcNoiseValue,
temp = pastCalcNoiseValue,
humidity = pastCalcNoiseValue
landscape = {},
temp = {},
humidity = {}
}
local modInfo = Mod.getInfo('smc__core__map_gen')
local require = modInfo.require
---@type MapGen.Layer
local Layer = require('MapGen.Layer')
@@ -262,7 +252,7 @@ function MapGen:register2DPeaksFromFile(layerName, map, scale,zeroPos, category,
for _, peakPos in ipairs(peaksPos) do
---@type MapGen.PeakDef
local def = {
pos = vector.new((peakPos.x - zeroPos.x) * scale, 0, (peakPos.y - zeroPos.y) * scale),
pos = vector.new((peakPos.x - zeroPos.x - 1) * scale, 0, (peakPos.y - zeroPos.y - 1) * scale),
color = color,
groups = groups,
}
@@ -574,19 +564,34 @@ function MapGen:getNoises2dValue(category, layer, x, y, z)
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)
if pastCalc.triangle ~= nil then
---@type number?
local noiseValue
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
local triangles = {
pastCalc.triangle,
pastCalc.triangle.neighbours.t1,
pastCalc.triangle.neighbours.t2,
pastCalc.triangle.neighbours.t3,
}
return noiseValue
---@param t MapGen.Triangle
for _, t in ipairs(triangles) do
if pointInTriangle(x, z, t) then
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
return noiseValue
end
end
end
---@param triangle MapGen.Triangle
for _, triangle in ipairs(layer.trianglesList[category]) do
if pointInTriangle(x, z,triangle) then
if pointInTriangle(x, z, triangle) then
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
pastCalc.triangle = triangle
@@ -717,7 +722,6 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
return
end
-- Calculating landscape height
-- If the value of the noise is `nil`,
-- then the point does not fall within any of the triangles.
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) or 0.0

View File

@@ -54,6 +54,12 @@ end
-- --- Class registration ---
---Adjacent triangles:
--- * `t1` with a common side p1-p2
--- * `t2` with a common side p2-p3
--- * `t3` with a common side p3-p1
---@alias MapGen.Triangle.Neighbours {t1:MapGen.Triangle?,t2:MapGen.Triangle?,t3:MapGen.Triangle?}
---@class MapGen.Triangle
---@field p1 MapGen.Peak
---@field p2 MapGen.Peak
@@ -64,13 +70,16 @@ end
---@field h1 MapGen.Triangulation.Edge
---@field h2 MapGen.Triangulation.Edge
---@field h3 MapGen.Triangulation.Edge
local Triangle = {}
---@field neighbours MapGen.Triangle.Neighbours
local Triangle = {
neighbours = {}
}
---@param p1 MapGen.Peak
---@param p2 MapGen.Peak
---@param p3 MapGen.Peak
---@return MapGen.Triangle
function Triangle:new( p1, p2, p3 )
function Triangle:new(p1, p2, p3)
assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s")
:format(tostring(p1), tostring(p2), tostring(p3)))

View File

@@ -28,6 +28,22 @@ local Triangulation = {
convexMultiplier = 1e3,
}
---Finds and writes adjacent triangles into triangles.
---@param triangles MapGen.Triangle[]
local function findTrianglesNeighbours(triangles)
for _, triangle in ipairs(triangles) do
for _, t in ipairs(triangles) do
if triangle.e1 == t.e1 and triangle.p3 ~= t.p3 then
triangle.neighbours.t1 = t
elseif triangle.e2 == t.e2 and triangle.p3 ~= t.p1 then
triangle.neighbours.t2 = t
elseif triangle.e3 == t.e3 and triangle.p3 ~= t.p2 then
triangle.neighbours.t3 = t
end
end
end
end
---The Delaunay method divides a plane into triangles based on a list of their vertices.
---
---Doesn't take into account the Y coordinate of the vertices.
@@ -140,6 +156,8 @@ function Triangulation.triangulate( vertices )
tableRemove( vertices )
end
findTrianglesNeighbours(triangles)
return triangles
end