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

View File

@@ -12,6 +12,7 @@ local Peak = {
---@param multinoiseParams MapGen.Peak.MultinoiseParams
---@return MapGen.Peak.Multinoise
local function multinoiseParamsToMultinoise(multinoiseParams)
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise
local multinoise = {}
@@ -36,11 +37,17 @@ function Peak:new(peakPos, multinoiseParams, weightFactor)
weightFactor = 1
end
---@diagnostic disable-next-line: missing-fields
---@type MapGen.Peak.Multinoise
local _multinoise = {} -- note: must be empty until luanti mapgen objects are loaded.
---@type MapGen.Peak
local instance = setmetatable({}, {__index = self})
local instance = setmetatable({},
{
__index = self,
__tostring = self.toString,
__eq = self.eq
})
function instance:getPeakPos()
return _peakPos
@@ -69,18 +76,21 @@ function Peak:new(peakPos, multinoiseParams, weightFactor)
return instance
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
function Peak:__eq( other )
---@return boolean
function Peak:eq( other )
local pos1 = self:getPeakPos()
local pos2 = other:getPeakPos()
return pos1.x == pos2.x and pos1.z == pos2.z
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
function Peak:dist2(p)
local pos1 = self:getPeakPos()

View File

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

View File

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

View File

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