Mapgen: write SuperUltraSigmaMapgen part 3,1

This commit is contained in:
Van
2025-11-11 21:10:07 +03:00
parent 6084b6e96c
commit f19f1dbe15
5 changed files with 68 additions and 37 deletions

View File

@@ -1,8 +1,5 @@
local mathCeil, mathAbs, mathRound, mathMin local mathAbs, mathRound
= math.ceil, math.abs, math.round, math.min = math.abs, math.round
local BUFFER_ZONE_WIDTH = 3000
local MAX_DISTANCE = 3000
-- --- MapGen default moises --- -- --- MapGen default moises ---
---@type ValueNoise ---@type ValueNoise
@@ -124,9 +121,7 @@ end
function MapGen:initLayersTrianglesList() function MapGen:initLayersTrianglesList()
---@param layer MapGen.Layer ---@param layer MapGen.Layer
for _, layer in ipairs(self.layersList) do for _, layer in ipairs(self.layersList) do
print('A: ', #layer.peaksList)
layer.trianglesList = Triangulation.triangulate(layer.peaksList) layer.trianglesList = Triangulation.triangulate(layer.peaksList)
print('ABOBA:', dump(layer.trianglesList))
end end
end end

View File

@@ -12,6 +12,7 @@ local Peak = {
---@param multinoiseParams MapGen.Peak.MultinoiseParams ---@param multinoiseParams MapGen.Peak.MultinoiseParams
---@return MapGen.Peak.Multinoise ---@return MapGen.Peak.Multinoise
local function multinoiseParamsToMultinoise(multinoiseParams) local function multinoiseParamsToMultinoise(multinoiseParams)
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise ---@type MapGen.Peak.Multinoise
local multinoise = {} local multinoise = {}
@@ -36,11 +37,17 @@ function Peak:new(peakPos, multinoiseParams, weightFactor)
weightFactor = 1 weightFactor = 1
end end
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise ---@type MapGen.Peak.Multinoise
local _multinoise = {} -- note: must be empty until luanti mapgen objects are loaded. local _multinoise = {} -- note: must be empty until luanti mapgen objects are loaded.
---@type MapGen.Peak ---@type MapGen.Peak
local instance = setmetatable({}, {__index = self}) local instance = setmetatable({},
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})
function instance:getPeakPos() function instance:getPeakPos()
return _peakPos return _peakPos
@@ -69,18 +76,21 @@ function Peak:new(peakPos, multinoiseParams, weightFactor)
return instance return instance
end end
---Returns a string describing the object in a readable form.
---@return string
function Peak:toString()
return ('Peak (%s) x: %.2f y: %.2f'):format( self.id, self:getPeakPos().x, self:getPeakPos().y )
end
---@param other MapGen.Peak ---@param other MapGen.Peak
function Peak:__eq( other ) ---@return boolean
function Peak:eq( other )
local pos1 = self:getPeakPos() local pos1 = self:getPeakPos()
local pos2 = other:getPeakPos() local pos2 = other:getPeakPos()
return pos1.x == pos2.x and pos1.z == pos2.z return pos1.x == pos2.x and pos1.z == pos2.z
end end
function Peak:toString()
return ('Peak (%s) x: %.2f y: %.2f'):format( self.id, self:getPeakPos().x, self:getPeakPos().y )
end
---@param p MapGen.Peak ---@param p MapGen.Peak
function Peak:dist2(p) function Peak:dist2(p)
local pos1 = self:getPeakPos() local pos1 = self:getPeakPos()

View File

@@ -1,4 +1,4 @@
local sqrt = math.sqrt local mathSqrt = math.sqrt
local modInfo = Mod.getInfo() local modInfo = Mod.getInfo()
local require = modInfo.require local require = modInfo.require
@@ -17,13 +17,14 @@ local FakePeak = require('MapGen.Triangulation.FakePeak')
---@return number ---@return number
local function quatCross(a, b, c) local function quatCross(a, b, c)
local p = (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c) local p = (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c)
return sqrt( p ) return mathSqrt(p)
end end
---Cross product (p1-p2, p2-p3) ---Cross product (p1-p2, p2-p3)
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@param p3 MapGen.Peak ---@param p3 MapGen.Peak
---@return number
local function crossProduct(p1, p2, p3) local function crossProduct(p1, p2, p3)
local pos1 = p1:getPeakPos() local pos1 = p1:getPeakPos()
local pos2 = p2:getPeakPos() local pos2 = p2:getPeakPos()
@@ -38,6 +39,7 @@ end
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
---@param p2 MapGen.Peak ---@param p2 MapGen.Peak
---@param p3 MapGen.Peak ---@param p3 MapGen.Peak
---@return boolean
local function isFlatAngle(p1, p2, p3) local function isFlatAngle(p1, p2, p3)
return (crossProduct(p1, p2, p3) == 0) return (crossProduct(p1, p2, p3) == 0)
end end
@@ -74,11 +76,22 @@ function Triangle:new( p1, p2, p3 )
e1 = Edge:new(p1, p2), e1 = Edge:new(p1, p2),
e2 = Edge:new(p2, p3), e2 = Edge:new(p2, p3),
e3 = Edge:new(p3, p1) e3 = Edge:new(p3, p1)
}, {__index = self}) },
{
__index = self,
__tostring = self.toString
})
return instance return instance
end end
---Returns a string describing the object in a readable form.
---@return string
function Triangle:toString()
return (('Triangle: \n %s\n %s\n %s')
:format(tostring(self.p1.getPeakPos()), tostring(self.p2.getPeakPos()), tostring(self.p3.getPeakPos())))
end
---Returns the length of the perpendicular ---Returns the length of the perpendicular
---from Peak1 to the line Peak2-Peak3 ---from Peak1 to the line Peak2-Peak3
---@param p1 MapGen.Peak ---@param p1 MapGen.Peak
@@ -103,27 +116,26 @@ function Triangle:getHeight(p1, p2, p3)
return Edge:new(p1, FakePeak:new(posP)) return Edge:new(p1, FakePeak:new(posP))
end end
function Triangle:toString()
return (('Triangle: \n %s\n %s\n %s')
:format(tostring(self.p1.getPeakPos()), tostring(self.p2.getPeakPos()), tostring(self.p3.getPeakPos())))
end
---Checks if the triangle is defined clockwise (sequence p1-p2-p3) ---Checks if the triangle is defined clockwise (sequence p1-p2-p3)
---@return boolean
function Triangle:isCW() function Triangle:isCW()
return (crossProduct(self.p1, self.p2, self.p3) < 0) return (crossProduct(self.p1, self.p2, self.p3) < 0)
end end
---Checks if the triangle is defined counter-clockwise (sequence p1-p2-p3) ---Checks if the triangle is defined counter-clockwise (sequence p1-p2-p3)
---@return boolean
function Triangle:isCCW() function Triangle:isCCW()
return (crossProduct(self.p1, self.p2, self.p3) > 0) return (crossProduct(self.p1, self.p2, self.p3) > 0)
end end
---Returns the length of the edges ---Returns the length of the edges
---@return number, number, number
function Triangle:getSidesLength() function Triangle:getSidesLength()
return self.e1:length(), self.e2:length(), self.e3:length() return self.e1:length(), self.e2:length(), self.e3:length()
end end
---Returns the coordinates of the center ---Returns the coordinates of the center
---@return vector
function Triangle:getCenter() function Triangle:getCenter()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos() local pos2 = self.p2:getPeakPos()
@@ -133,6 +145,7 @@ function Triangle:getCenter()
end end
---Returns the coordinates of the circumcircle center ---Returns the coordinates of the circumcircle center
---@return number, number
function Triangle:getCircumCenter() function Triangle:getCircumCenter()
local pos1 = self.p1:getPeakPos() local pos1 = self.p1:getPeakPos()
local pos2 = self.p2:getPeakPos() local pos2 = self.p2:getPeakPos()
@@ -151,7 +164,8 @@ function Triangle:getCircumCenter()
return (x / D), (z / D) return (x / D), (z / D)
end end
--- Returns the radius of the circumcircle ---Returns the radius of the circumcircle
---@return number
function Triangle:getCircumRadius() function Triangle:getCircumRadius()
local a, b, c = self:getSidesLength() local a, b, c = self:getSidesLength()
@@ -159,6 +173,7 @@ function Triangle:getCircumRadius()
end end
---Returns the coordinates of the circumcircle center and its radius ---Returns the coordinates of the circumcircle center and its radius
---@return number, number, number
function Triangle:getCircumCircle() function Triangle:getCircumCircle()
local x, z = self:getCircumCenter() local x, z = self:getCircumCenter()
local r = self:getCircumRadius() local r = self:getCircumRadius()
@@ -167,6 +182,7 @@ function Triangle:getCircumCircle()
end end
---Returns the area ---Returns the area
---@return number
function Triangle:getArea() function Triangle:getArea()
local a, b, c = self:getSidesLength() local a, b, c = self:getSidesLength()
@@ -175,6 +191,7 @@ end
---Checks if a given point lies into the triangle circumcircle ---Checks if a given point lies into the triangle circumcircle
---@param p MapGen.Peak ---@param p MapGen.Peak
---@return boolean
function Triangle:inCircumCircle(p) function Triangle:inCircumCircle(p)
return p:isInCircle(self:getCircumCircle()) return p:isInCircle(self:getCircumCircle())
end end

View File

@@ -3,9 +3,9 @@
-- Date of copying: 07.11.2025 -- Date of copying: 07.11.2025
-- Commit: e8d26241767cd6af377b5eda0c1e27122b6afe8a -- Commit: e8d26241767cd6af377b5eda0c1e27122b6afe8a
local tostring, assert = tostring, assert local assert = assert
local max = math.max local mathMax = math.max
local remove = table.remove local tableRemove = table.remove
local modInfo = Mod.getInfo() local modInfo = Mod.getInfo()
local require = modInfo.require local require = modInfo.require
@@ -61,7 +61,7 @@ function Triangulation.triangulate( vertices )
---Finding the vertices of a supertriangle ---Finding the vertices of a supertriangle
local convex_mult = Triangulation.convexMultiplier local convex_mult = Triangulation.convexMultiplier
local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult
local deltaMax = max(dx, dy) local deltaMax = mathMax(dx, dy)
local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5 local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5
local p1 = FakePeak:new(vector.new( midx - 2 * deltaMax,0, midy - deltaMax )) local p1 = FakePeak:new(vector.new( midx - 2 * deltaMax,0, midy - deltaMax ))
local p2 = FakePeak:new(vector.new(midx,0, midy + 2 * deltaMax )) local p2 = FakePeak:new(vector.new(midx,0, midy + 2 * deltaMax ))
@@ -72,6 +72,7 @@ function Triangulation.triangulate( vertices )
---@type MapGen.Triangle[] ---@type MapGen.Triangle[]
local triangles = {Triangle:new( vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3] )} local triangles = {Triangle:new( vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3] )}
print('Tria:', tostring(triangles[1]))
for i = 1, nvertices do for i = 1, nvertices do
---@type MapGen.Triangulation.Edge[] ---@type MapGen.Triangulation.Edge[]
@@ -85,15 +86,15 @@ function Triangulation.triangulate( vertices )
edges[#edges + 1] = curTriangle.e1 edges[#edges + 1] = curTriangle.e1
edges[#edges + 1] = curTriangle.e2 edges[#edges + 1] = curTriangle.e2
edges[#edges + 1] = curTriangle.e3 edges[#edges + 1] = curTriangle.e3
remove( triangles, j ) tableRemove( triangles, j )
end end
end end
for j = #edges - 1, 1, -1 do for j = #edges - 1, 1, -1 do
for k = #edges, j + 1, -1 do for k = #edges, j + 1, -1 do
if edges[j] and edges[k] and edges[j]:same(edges[k]) then if edges[j] and edges[k] and edges[j]:same(edges[k]) then
remove( edges, j ) tableRemove( edges, j )
remove( edges, k-1 ) tableRemove( edges, k-1 )
end end
end end
end end
@@ -108,12 +109,12 @@ function Triangulation.triangulate( vertices )
for i = #triangles, 1, -1 do for i = #triangles, 1, -1 do
local triangle = triangles[i] local triangle = triangles[i]
if triangle.p1.id > nvertices or triangle.p2.id > nvertices or triangle.p3.id > nvertices then if triangle.p1.id > nvertices or triangle.p2.id > nvertices or triangle.p3.id > nvertices then
remove( triangles, i ) tableRemove( triangles, i )
end end
end end
for _ = 1,3 do for _ = 1,3 do
remove( vertices ) tableRemove( vertices )
end end
return triangles return triangles

View File

@@ -10,18 +10,26 @@ function Edge:new( p1, p2 )
local instance = setmetatable({ local instance = setmetatable({
p1 = p1, p1 = p1,
p2 = p2 p2 = p2
}, {__index = self}) },
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})
return instance return instance
end end
---@param other MapGen.Triangulation.Edge ---Returns a string describing the object in a readable form.
function Edge:__eq( other ) ---@return string
return self.p1 == other.p1 and self.p2 == other.p2 function Edge:toString()
return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2)))
end end
function Edge:__tostring() ---@param other MapGen.Triangulation.Edge
return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2))) ---@return boolean
function Edge:eq( other )
return self.p1 == other.p1 and self.p2 == other.p2
end end
function Edge:same(otherEdge) function Edge:same(otherEdge)