diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 097931b..489ceac 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -48,9 +48,9 @@ local Biome = require("MapGen.Biome") local Triangulation = require("MapGen.Triangulation") ---@class MapGen ----@field layersByName table ----@field layersList table ----@field biomesList table +---@field layersByName table +---@field layersList MapGen.Layer[] +---@field biomesList MapGen.Biome[] ---@field biomesDiagram table ---@field multinoiseInitialized boolean Is the fractal noise of the peaks initialized? This is necessary for a one-time noise initialization. ---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work. @@ -118,10 +118,16 @@ function MapGen:RegisterLayer(name, minY, maxY) end) end -function MapGen:initLayersTrianglesList() +function MapGen:initLayersTrianglesTetrahedronsLists() ---@param layer MapGen.Layer for _, layer in ipairs(self.layersList) do layer.trianglesList = Triangulation.triangulate(layer.peaksList) + layer.tetrahedrons = Triangulation.tetrahedralize(layer.peaksList) + print('--debug--') + for _, t in ipairs(layer.tetrahedrons) do + print(tostring(t)) + end + print('--debug--') end end @@ -129,12 +135,11 @@ end ---Mark a peak of the world as a cubic peak by two opposite vertices. --- ---Peaks must be included in layers and may overlap each other. ----@param layerName string The name of the layer in which the new peak will be included. ----@param peakPos vector ----@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation ----@param peakIs2D boolean? If true, the transmitted Y coordinate will be overwritten by the layer boundaries. ----@param weightFactor number? The coefficient by which the reduction in the impact of peakal noise on generation will be calculated. If 0, there will be no reduction. -function MapGen:RegisterPeak(layerName, peakPos, multinoise, peakIs2D, weightFactor) +---@param layerName string The name of the layer in which the new peak will be included. +---@param peakPos vector +---@param multinoise MapGen.Peak.MultinoiseParams Noise that will be assigned to the peak and that will influence map generation +---@param groups table TODO: описание +function MapGen:RegisterPeak(layerName, peakPos, multinoise, groups) ---@type MapGen.Layer local layer = self.layersByName[layerName] @@ -142,21 +147,11 @@ function MapGen:RegisterPeak(layerName, peakPos, multinoise, peakIs2D, weightFac error("Invalid layer: " .. layerName) end - -- TODO: вероятно стоит убрать 2D ячейки - if peakIs2D then - peakPos.y = 0 - end - --TODO: добавить проверку, что координата ячейки не совпадают --TODO: Добавить проверку, что координата ячейки не выходит за границы слоя - -- TODO: вероятно стоит убрать вес - if weightFactor == nil then - weightFactor = 1 - end - ---@type MapGen.Peak - local peak = Peak:new(peakPos, multinoise, weightFactor) + local peak = Peak:new(peakPos, multinoise, groups) layer:addPeak(peak) @@ -494,7 +489,7 @@ function MapGen:run() end) self:initBiomesDiagram() - self:initLayersTrianglesList() + self:initLayersTrianglesTetrahedronsLists() MapGen.isRunning = true end diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index c11c1c5..07062e6 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -6,9 +6,11 @@ local mathSqrt = math.sqrt ---@field maxY number ---@field peaksList MapGen.Peak[] ---@field trianglesList MapGen.Triangle[] +---@field tetrahedrons MapGen.Tetrahedron[] local Layer = { peaksList = {}, trianglesList = {}, + tetrahedrons = {}, } ---@param name string @@ -29,7 +31,6 @@ end ---@param peak MapGen.Peak function Layer:addPeak(peak) table.insert(self.peaksList, peak) - print(dump(self.peaksList)) -- TODO: добавить сортировку массива для оптимизации. -- Вероятно будет лучше в отдельной фунции и засунуть её в MapGen.run end diff --git a/mods/CORE/MapGen/src/MapGen/Peak.lua b/mods/CORE/MapGen/src/MapGen/Peak.lua index 8cd7bfe..4f2b281 100644 --- a/mods/CORE/MapGen/src/MapGen/Peak.lua +++ b/mods/CORE/MapGen/src/MapGen/Peak.lua @@ -4,7 +4,7 @@ ---@field getMultinoiseParams fun():MapGen.Peak.MultinoiseParams ---@field getMultinoise fun():MapGen.Peak.Multinoise ---@field initMultinoise fun() ----@field getWeightFactor fun():number +---@field getGroups fun():table local Peak = { id = 0, } @@ -25,16 +25,20 @@ end ---@param peakPos vector ---@param multinoiseParams MapGen.Peak.MultinoiseParams ----@param weightFactor number TODO: возможно этот параметр не пригодится +---@param groups? table ---@return MapGen.Peak -function Peak:new(peakPos, multinoiseParams, weightFactor) +function Peak:new(peakPos, multinoiseParams, groups) ---@type vector local _peakPos = peakPos ---@type MapGen.Peak.MultinoiseParams local _multinoiseParams = multinoiseParams - if weightFactor < 0 then - weightFactor = 1 + local _groups + + if groups == nil then + _groups = {} + else + _groups = groups end ---@diagnostic disable-next-line: missing-fields @@ -50,7 +54,7 @@ function Peak:new(peakPos, multinoiseParams, weightFactor) }) function instance:getPeakPos() - return _peakPos + return vector.new(_peakPos.x, _peakPos.y, _peakPos.z) end function instance:getMultinoiseParams() @@ -69,8 +73,8 @@ function Peak:new(peakPos, multinoiseParams, weightFactor) _multinoise = multinoiseParamsToMultinoise(_multinoiseParams) end - function instance.getWeightFactor() - return weightFactor + function instance.getGroups() + return _groups end return instance @@ -79,7 +83,8 @@ 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 ) + local pos = self:getPeakPos() + return ('Peak (%s) x: %.2f y: %.2f z: %.2f'):format( self.id, pos.x, pos.y, pos.z) end ---@param other MapGen.Peak @@ -91,27 +96,4 @@ function Peak:eq( other ) return pos1.x == pos2.x and pos1.z == pos2.z end ----@param p MapGen.Peak -function Peak:dist2(p) - local pos1 = self:getPeakPos() - local pos2 = p:getPeakPos() - - local dx, dy = (pos1.x - pos2.x), (pos1.z - pos2.z) - return dx * dx + dy * dy -end - ----@param p MapGen.Peak -function Peak:dist(p) - return math.sqrt(self:dist2(p)) -end - -function Peak:isInCircle(cx, cy, r) - local pos = self:getPeakPos() - - local dx = (cx - pos.x) - local dy = (cy - pos.z) - - return ((dx * dx + dy * dy) <= (r * r)) -end - return Peak diff --git a/mods/CORE/MapGen/src/MapGen/Triangle.lua b/mods/CORE/MapGen/src/MapGen/Triangle.lua index 2a6ed5b..a9a2aa9 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangle.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangle.lua @@ -20,28 +20,33 @@ local function quatCross(a, b, c) 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() - local pos3 = p3:getPeakPos() - - local x1, x2 = pos2.x - pos1.x, pos3.x - pos2.x - local y1, y2 = pos2.z - pos1.z, pos3.z - pos2.z - return x1 * y2 - y1 * x2 -end - ----Checks if angle (p1-p2-p3) is flat +---Checks if points p1, p2, p3 are collinear (angle p1-p2-p3 is 180 degrees) ---@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) + local pos1 = p1:getPeakPos() + local pos2 = p2:getPeakPos() + local pos3 = p3:getPeakPos() + + local vec1 = pos1 - pos2 -- Vector from p2 to p1 + local vec2 = pos3 - pos2 -- Vector from p2 to p3 + + -- Normalize vectors + local len1 = vec1:length() + local len2 = vec2:length() + + if len1 < 0 or len2 < 0 then + return true -- Degenerate case: points are too close + end + + vec1 = vec1 / len1 + vec2 = vec2 / len2 + + -- If dot product is -1, vectors are opposite (180° angle) + local dot = vec1:dot(vec2) + return math.abs(dot + 1.0) < 0 end -- Class registration -- @@ -79,7 +84,8 @@ function Triangle:new( p1, p2, p3 ) }, { __index = self, - __tostring = self.toString + __tostring = self.toString, + __eq = self.eq }) return instance @@ -88,8 +94,43 @@ 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()))) + return ('Triangle: \n %s\n %s\n %s'):format( + tostring(self.p1.getPeakPos()), + tostring(self.p2.getPeakPos()), + tostring(self.p3.getPeakPos()) + ) +end + +---@param other MapGen.Triangle +---@return boolean +function Triangle:eq( other ) + return self.p1 == other.p1 and self.p2 == other.p2 and self.p3 == other.p3 +end + +---@param other MapGen.Triangle +---@return boolean +function Triangle:same(other) + return (self.p1 == other.p1 and self.p2 == other.p2 and self.p3 == other.p3) + or (self.p1 == other.p2 and self.p2 == other.p3 and self.p3 == other.p1) + or (self.p1 == other.p3 and self.p2 == other.p1 and self.p3 == other.p2) +end + +---Returns the coordinates of the center +---@return vector +function Triangle:getCenter() + local pos1 = self.p1:getPeakPos() + local pos2 = self.p2:getPeakPos() + local pos3 = self.p3:getPeakPos() + + return (pos1 + pos2 + pos3) / 3 +end + +---Returns the area +---@return number +function Triangle:getArea() + local a, b, c = self:getSidesLength() + + return (quatCross(a, b, c) / 4) end ---Returns the length of the perpendicular @@ -116,16 +157,32 @@ function Triangle:getHeight(p1, p2, p3) return Edge:new(p1, FakePeak:new(posP)) 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 +---Returns the length of the perpendicular +---from Peak1 to the line Peak2-Peak3 +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +---@return MapGen.Triangulation.Edge +function Triangle:getHeight2D(p1, p2, p3) + local pos1 = p1:getPeakPos() + local pos2 = p2:getPeakPos() + local pos3 = p3:getPeakPos() ----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) + pos1.y = 0 + pos2.y = 0 + pos3.y = 0 + + local v = pos3 - pos2 + ---@type vector + local w = pos1 - pos2 + + -- Formula: (w * v) / (v * v) + -- When * the dot product. + local t = w:dot(v) / v:dot(v) + + local posP = pos2 + v * t + + return Edge:new(p1, FakePeak:new(posP)) end ---Returns the length of the edges @@ -134,19 +191,29 @@ 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() +---Returns the length of the edges +--- +---Note: This function does not take into account the Y-coordinate, +---that is, the calculation is performed on the projection of the triangle XZ +---@return number, number, number +function Triangle:getSidesLength2D() local pos1 = self.p1:getPeakPos() local pos2 = self.p2:getPeakPos() local pos3 = self.p3:getPeakPos() - return (pos1 + pos2 + pos3) / 3 + pos1.y = 0 + pos2.y = 0 + pos3.y = 0 + + return (pos2 - pos1):length(), (pos3 - pos2):length(), (pos1 - pos3):length() end ---Returns the coordinates of the circumcircle center ----@return number, number -function Triangle:getCircumCenter() +--- +---Note: This function does not take into account the Y-coordinate, +---that is, the calculation is performed on the projection of the triangle XZ +---@return vector +function Triangle:getCircumCenter2D() local pos1 = self.p1:getPeakPos() local pos2 = self.p2:getPeakPos() local pos3 = self.p3:getPeakPos() @@ -161,39 +228,46 @@ function Triangle:getCircumCenter() ( pos2.x * pos2.x + pos2.z * pos2.z) * (pos1.x - pos3.x) + ( pos3.x * pos3.x + pos3.z * pos3.z) * (pos2.x - pos1.x)) - return (x / D), (z / D) + return vector.new(x / D, 0, z / D) end ---Returns the radius of the circumcircle +--- +---Note: This function does not take into account the Y-coordinate, +---that is, the calculation is performed on the projection of the triangle XZ ---@return number -function Triangle:getCircumRadius() - local a, b, c = self:getSidesLength() +function Triangle:getCircumRadius2D() + local a, b, c = self:getSidesLength2D() return ((a * b * c) / quatCross(a, b, c)) 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() +--- +---Note: This function does not take into account the Y-coordinate, +---that is, the calculation is performed on the projection of the triangle XZ +---@return vector, number +function Triangle:getCircumCircle2D() + local c = self:getCircumCenter2D() + local r = self:getCircumRadius2D() - return x, z, r -end - ----Returns the area ----@return number -function Triangle:getArea() - local a, b, c = self:getSidesLength() - - return (quatCross(a, b, c) / 4) + return c, r end ---Checks if a given point lies into the triangle circumcircle +--- +---Note: This function does not take into account the Y-coordinate, +---that is, the calculation is performed on the projection of the triangle XZ ---@param p MapGen.Peak ---@return boolean -function Triangle:inCircumCircle(p) - return p:isInCircle(self:getCircumCircle()) +function Triangle:inCircumCircle2D(p) + local pos = p:getPeakPos() + + local c, r = self:getCircumCircle2D() + + local distance = (c - pos):length() + + return distance <= r end return Triangle \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation.lua b/mods/CORE/MapGen/src/MapGen/Triangulation.lua index 6c45968..5a6fa35 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation.lua @@ -21,9 +21,21 @@ local Triangulation = { convexMultiplier = 1e3, } ----Triangulates a set of given vertices +---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. +---That is, all triangles that don't lie in the XZ plane will be projected onto it. +--- +---Vertices that do not have a `is2d` group will not participate in the triangulation. ---@param vertices table function Triangulation.triangulate( vertices ) + -- Only peaks with a `is2d` group can be triangulated. + --[[for i, vertex in ipairs(vertices) do + if vertex:getGroups().is2d == nil then + table.remove(vertices, i) + end + end--]] + local nvertices = #vertices assert( nvertices > 2, "Cannot triangulate, needs more than 3 vertices" ) @@ -32,11 +44,11 @@ function Triangulation.triangulate( vertices ) return {Triangle:new(vertices[1], vertices[2], vertices[3])} end - local trmax = nvertices * 4 - local minX, minY = vertices[1].getPeakPos().x, vertices[1].getPeakPos().z + local minPos = vertices[1].getPeakPos() + local minX, minY = minPos.x, minPos.z local maxX, maxY = minX, minY - for i = 1, #vertices do + for i = 1, nvertices do local vertex = vertices[i] local vertexPos = vertex.getPeakPos() vertex.id = i @@ -63,6 +75,7 @@ function Triangulation.triangulate( vertices ) local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult 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 )) local p3 = FakePeak:new( vector.new(midx + 2 * deltaMax,0, midy - deltaMax )) @@ -72,17 +85,17 @@ 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])) + print('uu:', tostring(triangles[1])) for i = 1, nvertices do ---@type MapGen.Triangulation.Edge[] local edges = {} - local ntriangles = #triangles + -- Finding bad triangles. for j = #triangles, 1, -1 do local curTriangle = triangles[j] - if curTriangle:inCircumCircle(vertices[i]) then + if curTriangle:inCircumCircle2D(vertices[i]) then edges[#edges + 1] = curTriangle.e1 edges[#edges + 1] = curTriangle.e2 edges[#edges + 1] = curTriangle.e3 @@ -90,6 +103,7 @@ function Triangulation.triangulate( vertices ) end end + -- Removing duplicate edges. 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 @@ -99,13 +113,13 @@ function Triangulation.triangulate( vertices ) end end + -- Create new triangles. for j = 1, #edges do - local n = #triangles - assert(n <= trmax, "Generated more than needed triangles") - triangles[n + 1] = Triangle:new(edges[j].p1, edges[j].p2, vertices[i]) + triangles[#triangles + 1] = Triangle:new(edges[j].p1, edges[j].p2, vertices[i]) end end + -- Remove tetrahedra associated with the vertices of a supertriangle. 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 @@ -113,6 +127,7 @@ function Triangulation.triangulate( vertices ) end end + -- Removing the vertices of a super supertriangle. for _ = 1,3 do tableRemove( vertices ) end @@ -120,4 +135,238 @@ function Triangulation.triangulate( vertices ) return triangles end +--- DEV --- + +local modInfo = Mod.getInfo() +local require = modInfo.require + +---@type MapGen.Triangle +local Triangle = require('MapGen.Triangle') +---@type MapGen.Triangulation.FakePeak +local FakePeak = require('MapGen.Triangulation.FakePeak') + +-- Класс Тетраэдр +---@class MapGen.Tetrahedron +---@field p1 MapGen.Peak +---@field p2 MapGen.Peak +---@field p3 MapGen.Peak +---@field p4 MapGen.Peak +---@field f1 MapGen.Triangle +---@field f2 MapGen.Triangle +---@field f3 MapGen.Triangle +---@field f4 MapGen.Triangle +local Tetrahedron = {} + +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +---@param p4 MapGen.Peak +---@return MapGen.Tetrahedron +function Tetrahedron:new(p1, p2, p3, p4) + local instance = setmetatable({ + p1 = p1, + p2 = p2, + p3 = p3, + p4 = p4, + f1 = Triangle:new(p1, p2, p3), + f2 = Triangle:new(p1, p2, p4), + f3 = Triangle:new(p1, p3, p4), + f4 = Triangle:new(p2, p3, p4), + }, { + __index = self, + __tostring = self.toString + }) + + return instance +end + +---Returns a string describing the object in a readable form. +---@return string +function Tetrahedron:toString() + return ('Tetrahedron: \n %s\n %s\n %s\n %s'):format( + tostring(self.p1.getPeakPos()), + tostring(self.p2.getPeakPos()), + tostring(self.p3.getPeakPos()), + tostring(self.p4.getPeakPos()) + ) +end + +---Returns the center of the circumscribed sphere. +---@return vector +function Tetrahedron:getCircumSphereCenter() + local pos1 = self.p1.getPeakPos() + local pos2 = self.p2.getPeakPos() + local pos3 = self.p3.getPeakPos() + local pos4 = self.p4.getPeakPos() + + local a = pos2 - pos1 + local b = pos3 - pos1 + local c = pos4 - pos1 + + local a2 = a:dot(a) + local b2 = b:dot(b) + local c2 = c:dot(c) + + local cross_bc = b:cross(c) + local cross_ca = c:cross(a) + local cross_ab = a:cross(b) + + local det = a:dot(b:cross(c)) * 2 + + if math.abs(det) < 0 then + return (pos1 + pos2 + pos3 + pos4) / 4 + end + + local center = pos1 + (cross_bc * a2 + cross_ca * b2 + cross_ab * c2) / det + + return center +end + +---Returns the radius of the circumscribed sphere. +---@return number +function Tetrahedron:getCircumSphereRadius() + local center = self:getCircumSphereCenter() + local pos1 = self.p1.getPeakPos() + return (pos1 - center):length() +end + +---Checks whether a given point lies within the circumscribed sphere of a tetrahedron. +---@param p MapGen.Peak +function Tetrahedron:inCircumSphere(p) + local pos = p.getPeakPos() + local c = self:getCircumSphereCenter() + local r = self:getCircumSphereRadius() + + local distance = (pos - c):length() + return distance <= r +end + +---The Delaunay method divides space into tetrahedra based on a list of their vertices. +--- +---Vertices that do not have a `is3d` group will not participate in the triangulation. +---@param vertices table +function Triangulation.tetrahedralize(vertices) + --[[vertices = table.copy(vertices) + + -- Only peaks with a `is3d` group can be triangulated. + for i, vertex in ipairs(vertices) do + if vertex:getGroups().is3d == nil then + table.remove(vertices, i) + end + end--]] + + print(dump(vertices)) + + local nvertices = #vertices + + assert(nvertices > 3, "Cannot tetrahedralize, needs more than 4 vertices") + + if nvertices == 4 then + return {Tetrahedron:new(vertices[1], vertices[2], vertices[3], vertices[4])} + end + + local minPos = vertices[1].getPeakPos() + local minX, minY, minZ = minPos.x, minPos.y, minPos.z + local maxX, maxY, maxZ = minX, minY, minZ + + for i = 1, nvertices do + local vertex = vertices[i] + local vertexPos = vertex.getPeakPos() + vertex.id = i + + if vertexPos.x < minX then + minX = vertexPos.x + end + + if vertexPos.y < minY then + minY = vertexPos.y + end + + if vertexPos.z < minZ then + minZ = vertexPos.z + end + + if vertexPos.x > maxX then + maxX = vertexPos.x + end + + if vertexPos.y > maxY then + maxY = vertexPos.y + end + + if vertexPos.z > maxZ then + maxZ = vertexPos.z + end + end + + -- Finding the vertices of a supertetrahedron + local convex_mult = Triangulation.convexMultiplier + local dx, dy, dz = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult, (maxZ - minZ) * convex_mult + local deltaMax = mathMax(dx, dy, dz) + local midx, midy, midz = (minX + maxX) * 0.5, (minY + maxY) * 0.5, (minZ + maxZ) * 0.5 + + local p1 = FakePeak:new(vector.new(midx - 2 * deltaMax, midy - deltaMax, midz - deltaMax)) + local p2 = FakePeak:new(vector.new(midx + 2 * deltaMax, midy - deltaMax, midz - deltaMax)) + local p3 = FakePeak:new(vector.new(midx, midy + 2 * deltaMax, midz - deltaMax)) + local p4 = FakePeak:new(vector.new(midx, midy, midz + 2 * deltaMax)) + + p1.id, p2.id, p3.id, p4.id = nvertices + 1, nvertices + 2, nvertices + 3, nvertices + 4 + vertices[p1.id], vertices[p2.id], vertices[p3.id], vertices[p4.id] = p1, p2, p3, p4 + + ---@type MapGen.Tetrahedron[] + local tetrahedrons = {Tetrahedron:new(p1, p2, p3, p4)} + + for i = 1, nvertices do + print(vertices[i]:toString()) + ---@type MapGen.Triangle[] + local faces = {} + + -- Finding bad tetrahedrons. + for j = #tetrahedrons, 1, -1 do + local curTetrahedron = tetrahedrons[j] + + if curTetrahedron:inCircumSphere(vertices[i]) then + faces[#faces + 1] = curTetrahedron.f1 + faces[#faces + 1] = curTetrahedron.f2 + faces[#faces + 1] = curTetrahedron.f3 + faces[#faces + 1] = curTetrahedron.f4 + tableRemove(tetrahedrons, j) + end + end + + -- Removing duplicate faces. + for j = #faces - 1, 1, -1 do + for k = #faces, j + 1, -1 do + if faces[j] and faces[k] and faces[j]:same(faces[k]) then + tableRemove( faces, j ) + tableRemove( faces, k-1 ) + end + end + end + + -- Create new tetrahedrons. + for j = 1, #faces do + tetrahedrons[#tetrahedrons + 1] = Tetrahedron:new(faces[j].p1, faces[j].p2, faces[j].p3, vertices[i]) + end + + print('aboba') + end + + -- Remove tetrahedra associated with the vertices of a supertetrahedron. + for i = #tetrahedrons, 1, -1 do + local tetrahedron = tetrahedrons[i] + if tetrahedron.p1.id > nvertices or tetrahedron.p2.id > nvertices or tetrahedron.p3.id > nvertices or tetrahedron.p4.id > nvertices then + tableRemove( tetrahedrons, i ) + end + end + + -- Removing the vertices of a supertetrahedron. + for i = 1, 4 do + tableRemove( vertices ) + end + + return tetrahedrons +end + + return Triangulation \ No newline at end of file diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua index ba01876..1e9835a 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation/Edge.lua @@ -28,17 +28,20 @@ end ---@param other MapGen.Triangulation.Edge ---@return boolean -function Edge:eq( other ) +function Edge:eq(other) return self.p1 == other.p1 and self.p2 == other.p2 end -function Edge:same(otherEdge) - return ((self.p1 == otherEdge.p1) and (self.p2 == otherEdge.p2)) - or ((self.p1 == otherEdge.p2) and (self.p2 == otherEdge.p1)) +---@param other MapGen.Triangulation.Edge +---@return boolean +function Edge:same(other) + return (self.p1 == other.p1 and self.p2 == other.p2) + or (self.p1 == other.p2 and self.p2 == other.p1) end +---@return number function Edge:length() - return self.p1:dist(self.p2) + return (self.p1.getPeakPos() - self.p2.getPeakPos()):length() end ---@return vector diff --git a/mods/ENVIRONMENT/MapGenerator/init.lua b/mods/ENVIRONMENT/MapGenerator/init.lua index 42217ea..6413e27 100644 --- a/mods/ENVIRONMENT/MapGenerator/init.lua +++ b/mods/ENVIRONMENT/MapGenerator/init.lua @@ -34,10 +34,10 @@ local a = mapGenerator:RegisterPeak("world", lacunarity = 2, }, }, - true, 1.0) + {is3d = 1}) local b = mapGenerator:RegisterPeak("world", - vector.new(-100,0,0), + vector.new(0,50,0), { landscapeNoise = { offset = 30, @@ -49,10 +49,10 @@ local b = mapGenerator:RegisterPeak("world", lacunarity = 2, }, }, - true, 1.0) + {is3d = 1}) local c = mapGenerator:RegisterPeak("world", - vector.new(0,0,-100), + vector.new(50,0,0), { landscapeNoise = { offset = 30, @@ -64,10 +64,9 @@ local c = mapGenerator:RegisterPeak("world", lacunarity = 2, }, }, - true, 1.0) - -local d = mapGenerator:RegisterPeak("world", - vector.new(-100,0,-100), + {is3d = 1, is2d = 1}) +--[[local d = mapGenerator:RegisterPeak("world", + vector.new(50,50,0), { landscapeNoise = { offset = 30, @@ -79,13 +78,13 @@ local d = mapGenerator:RegisterPeak("world", lacunarity = 2, }, }, - true, 1.0) + {is3d = 1, is2d = 1})--]] local e = mapGenerator:RegisterPeak("world", - vector.new(-50,0,-50), + vector.new(25,25,25), { landscapeNoise = { - offset = -10, + offset = 10, scale = 2, spread = {x = 10, y = 10, z = 10}, seed = 47, @@ -94,7 +93,22 @@ local e = mapGenerator:RegisterPeak("world", lacunarity = 2, }, }, - true, 1.0) + {is3d = 1, is2d = 1}) + +mapGenerator:RegisterPeak("world", + vector.new(25,25,-25), + { + landscapeNoise = { + offset = 20, + scale = 2, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.4, + lacunarity = 2, + }, + }, + {is3d = 1, is2d = 1}) --[[ local e = mapGenerator:RegisterPeak("world", vector.new(-50,0,50),