From 2c2bb2cd726d063d71a0ead876e08e753b1132c9 Mon Sep 17 00:00:00 2001 From: Vanic Date: Fri, 14 Nov 2025 21:04:55 +0300 Subject: [PATCH] Mapgen: write SuperUltraSigmaMapgen part 4 --- mods/CORE/MapGen/src/MapGen.lua | 140 ++++++++++-- mods/CORE/MapGen/src/MapGen/Layer.lua | 14 +- mods/CORE/MapGen/src/MapGen/Triangulation.lua | 60 ++++- mods/ENVIRONMENT/MapGenerator/init.lua | 213 +++++++++--------- 4 files changed, 282 insertions(+), 145 deletions(-) diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 489ceac..2984640 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -122,12 +122,7 @@ 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--') + layer.tetrahedronsList = Triangulation.tetrahedralize(layer.peaksList) end end @@ -285,6 +280,9 @@ end local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, y, z) local ids = mapGenerator.nodeIDs + temp = math.min(0, math.max(1, temp)) + humidity = math.min(0, math.max(1, humidity)) + local biome = mapGenerator.biomesDiagram[mathRound(temp)][mathRound(humidity)] if y > hight and y > 0 then @@ -298,15 +296,17 @@ local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, end end ----@param px number ----@param py number ----@param x1 number ----@param y1 number ----@param x2 number ----@param y2 number ----@param x3 number ----@param y3 number +---@param px number +---@param py number +---@param x1 number +---@param y1 number +---@param x2 number +---@param y2 number +---@param x3 number +---@param y3 number local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3) + -- TODO: переписать с учетом `vector` + -- Function for calculating the vector product local function crossProduct(ax, ay, bx, by) return ax * by - ay * bx @@ -325,6 +325,31 @@ local function pointInTriangle(px, py, x1, y1, x2, y2, x3, y3) return not (has_positive and has_negative) end +---@param P vector +---@param A vector +---@param B vector +---@param C vector +local function pointInTetrahedron(P, A, B, C, D) + local function det3(a, b, c) + return a.x * (b.y * c.z - b.z * c.y) + - a.y * (b.x * c.z - b.z * c.x) + + a.z * (b.x * c.y - b.y * c.x) + end + + local v0 = B - A + local v1 = C - A + local v2 = D - A + local v3 = P - A + + local d = det3(v0, v1, v2) + + local u = det3(v3, v1, v2) / d + local v = det3(v0, v3, v2) / d + local w = det3(v0, v1, v3) / d + + return (u >= 0 and v >= 0 and w >= 0 and (u + v + w) <= 1) +end + ---@param height MapGen.Triangulation.Edge ---@param x number ---@param y number @@ -357,7 +382,7 @@ end ---@param y number ---@param z number ---@return number? -function MapGen:getNoiseHeightValue(layer, x, y, z) +function MapGen:getNoises2dValues(layer, x, y, z) ---@param triangle MapGen.Triangle for _, triangle in ipairs(layer.trianglesList) do local peak1 = triangle.p1 @@ -403,7 +428,77 @@ function MapGen:getNoiseHeightValue(layer, x, y, z) -- If the point does not fall within the triangle, it is impossible to calculate the height. -- For debugging purposes only, may slow down generation: - --core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the height of the landscape is impossible.', tostring(x), tostring(y), tostring(z))) + --core.log('warning', string.format('The point %s %s %s does not fall within any triangle, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z))) +end + +--- +--- +---Returns `nil` if the point is not included in any tetrahedron. +---@param layer MapGen.Layer +---@param x number +---@param y number +---@param z number +---@return number?, number? +function MapGen:getNoises3dValues(layer, x, y, z) + ---@param triangle MapGen.Tetrahedron + for _, tetrahedron in ipairs(layer.tetrahedronsList) do + local peak1 = tetrahedron.p1 + local peak2 = tetrahedron.p2 + local peak3 = tetrahedron.p3 + local peak4 = tetrahedron.p4 + + local pos1 = peak1:getPeakPos() + local pos2 = peak2:getPeakPos() + local pos3 = peak3:getPeakPos() + local pos4 = peak4:getPeakPos() + + -- If the point is in the tetrahedron defined by the `MapGen.Peak`... + if pointInTetrahedron( + vector.new(x, y, z), + pos1, + pos2, + pos3, + pos4 + ) + then + local noiseTempValue = 0.0 + local noiseHumidityValue = 0.0 + + local totalWeight = 0.0 + + -- ... we calculate the height for this point using smoothing. + -- We calculate the height value using the smoothing formula: + -- ( peak1 * weight1 + peak2 * weight2 + peak3 * weight3) / totalWeight, when + -- totalWeight = weight1 + weight2 + weight3 + local weight = calcWeight(tetrahedron.h1, x, y, z) + noiseTempValue = noiseTempValue + peak1:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight + noiseHumidityValue = noiseHumidityValue + peak1:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight + + weight = calcWeight(tetrahedron.h2, x, y, z) + noiseTempValue = noiseTempValue + peak2:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight + noiseHumidityValue = noiseHumidityValue + peak2:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight + + weight = calcWeight(tetrahedron.h3, x, y, z) + noiseTempValue = noiseTempValue + peak3:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight + noiseHumidityValue = noiseHumidityValue + peak3:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight + + weight = calcWeight(tetrahedron.h4, x, y, z) + noiseTempValue = noiseTempValue + peak4:getMultinoise().tempNoise:get_2d({x = x, y = z}) * weight + noiseHumidityValue = noiseHumidityValue + peak4:getMultinoise().humidityNoise:get_2d({x = x, y = z}) * weight + totalWeight = totalWeight + weight + + -- The height must be an integer value + -- because the biome diagram is based on integers + return mathRound(noiseTempValue / totalWeight), mathRound(noiseHumidityValue / totalWeight) + end + end + + -- If the point does not fall within the tetrahedron, it is impossible to calculate the height. + -- For debugging purposes only, may slow down generation: + --core.log('warning', string.format('The point %s %s %s does not fall within any tetrahedron, calculating the noise value is impossible.', tostring(x), tostring(y), tostring(z))) end ---@param mapGenerator MapGen @@ -427,14 +522,21 @@ local function generatorHandler(mapGenerator, data, index, x, y, z) end -- Calculating landscape height - local noiseHeightValue = mapGenerator:getNoiseHeightValue(layer, x, y, z) + local noiseHeightValue = mapGenerator:getNoises2dValues(layer, x, y, z) if noiseHeightValue == nil then noiseHeightValue = 0.0 end - local noiseTempValue = 0.0 - local noiseHumidityValue = 0.0 + local noiseTempValue, noiseHumidityValue = mapGenerator:getNoises3dValues(layer, x, y, z) + + if noiseTempValue == nil then + noiseTempValue = 0.0 + end + + if noiseHumidityValue == nil then + noiseHumidityValue = 0.0 + end generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) end diff --git a/mods/CORE/MapGen/src/MapGen/Layer.lua b/mods/CORE/MapGen/src/MapGen/Layer.lua index 07062e6..7679c0b 100644 --- a/mods/CORE/MapGen/src/MapGen/Layer.lua +++ b/mods/CORE/MapGen/src/MapGen/Layer.lua @@ -1,16 +1,16 @@ local mathSqrt = math.sqrt ---@class MapGen.Layer ----@field name string ----@field minY number ----@field maxY number ----@field peaksList MapGen.Peak[] ----@field trianglesList MapGen.Triangle[] ----@field tetrahedrons MapGen.Tetrahedron[] +---@field name string +---@field minY number +---@field maxY number +---@field peaksList MapGen.Peak[] +---@field trianglesList MapGen.Triangle[] +---@field tetrahedronsList MapGen.Tetrahedron[] local Layer = { peaksList = {}, trianglesList = {}, - tetrahedrons = {}, + tetrahedronsList = {}, } ---@param name string diff --git a/mods/CORE/MapGen/src/MapGen/Triangulation.lua b/mods/CORE/MapGen/src/MapGen/Triangulation.lua index 5a6fa35..f86840a 100644 --- a/mods/CORE/MapGen/src/MapGen/Triangulation.lua +++ b/mods/CORE/MapGen/src/MapGen/Triangulation.lua @@ -29,12 +29,14 @@ local Triangulation = { ---Vertices that do not have a `is2d` group will not participate in the triangulation. ---@param vertices table function Triangulation.triangulate( vertices ) + vertices = table.copy_with_metatables(vertices) + -- Only peaks with a `is2d` group can be triangulated. - --[[for i, vertex in ipairs(vertices) do + for i, vertex in ipairs(vertices) do if vertex:getGroups().is2d == nil then table.remove(vertices, i) end - end--]] + end local nvertices = #vertices @@ -86,7 +88,6 @@ function Triangulation.triangulate( vertices ) ---@type MapGen.Triangle[] local triangles = {Triangle:new( vertices[nvertices + 1], vertices[nvertices + 2], vertices[nvertices + 3] )} - print('uu:', tostring(triangles[1])) for i = 1, nvertices do ---@type MapGen.Triangulation.Edge[] local edges = {} @@ -142,6 +143,8 @@ local require = modInfo.require ---@type MapGen.Triangle local Triangle = require('MapGen.Triangle') +---@type MapGen.Triangulation.Edge +local Edge = require('MapGen.Triangulation.Edge') ---@type MapGen.Triangulation.FakePeak local FakePeak = require('MapGen.Triangulation.FakePeak') @@ -151,6 +154,10 @@ local FakePeak = require('MapGen.Triangulation.FakePeak') ---@field p2 MapGen.Peak ---@field p3 MapGen.Peak ---@field p4 MapGen.Peak +---@field h1 MapGen.Triangulation.Edge +---@field h2 MapGen.Triangulation.Edge +---@field h3 MapGen.Triangulation.Edge +---@field h4 MapGen.Triangulation.Edge ---@field f1 MapGen.Triangle ---@field f2 MapGen.Triangle ---@field f3 MapGen.Triangle @@ -168,6 +175,10 @@ function Tetrahedron:new(p1, p2, p3, p4) p2 = p2, p3 = p3, p4 = p4, + h1 = self:getHeight(p1, p2, p3, p4), + h2 = self:getHeight(p2, p3, p4, p1), + h3 = self:getHeight(p3, p4, p1, p2), + h4 = self:getHeight(p4, p1, p2, p3), f1 = Triangle:new(p1, p2, p3), f2 = Triangle:new(p1, p2, p4), f3 = Triangle:new(p1, p3, p4), @@ -191,6 +202,41 @@ function Tetrahedron:toString() ) end +---@param p1 MapGen.Peak +---@param p2 MapGen.Peak +---@param p3 MapGen.Peak +---@param p4 MapGen.Peak +function Tetrahedron:getHeight(p1, p2, p3, p4) + local pos1 = p1:getPeakPos() + local pos2 = p2:getPeakPos() + local pos3 = p3:getPeakPos() + local pos4 = p4:getPeakPos() + + -- Векторы, лежащие в плоскости + local AB = pos3 - pos2 + local AC = pos4 - pos2 + + -- Нормаль к плоскости + local normal = AB:cross(AC) + local normal_length = normal:length() + + -- Если плоскость вырождена (точки на одной прямой) + if normal_length < 1e-10 then + return nil -- или другая обработка ошибки + end + + -- Вектор от точки на плоскости к нашей точке + local AP = pos1 - pos2 + + -- Расстояние от точки до плоскости + local distance = AP:dot(normal) / normal_length + + -- Проекция точки на плоскость (вектор!) + local projection = pos1 - (normal * (distance / normal_length)) + + return Edge:new(p1, FakePeak:new(projection)) +end + ---Returns the center of the circumscribed sphere. ---@return vector function Tetrahedron:getCircumSphereCenter() @@ -246,16 +292,14 @@ end ---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) + vertices = table.copy_with_metatables(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)) + end local nvertices = #vertices @@ -317,7 +361,6 @@ function Triangulation.tetrahedralize(vertices) local tetrahedrons = {Tetrahedron:new(p1, p2, p3, p4)} for i = 1, nvertices do - print(vertices[i]:toString()) ---@type MapGen.Triangle[] local faces = {} @@ -349,7 +392,6 @@ function Triangulation.tetrahedralize(vertices) 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. diff --git a/mods/ENVIRONMENT/MapGenerator/init.lua b/mods/ENVIRONMENT/MapGenerator/init.lua index 6413e27..85c1dd4 100644 --- a/mods/ENVIRONMENT/MapGenerator/init.lua +++ b/mods/ENVIRONMENT/MapGenerator/init.lua @@ -21,126 +21,119 @@ local mapGenerator = Core.MapGen.Class:new({ mapGenerator:RegisterLayer("world", -1000, 500) -local a = mapGenerator:RegisterPeak("world", +local land1 = { + offset = -10, + scale = 2, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.4, + lacunarity = 2, +} + +local land2 = { + offset = 20, + scale = 2, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.4, + lacunarity = 2, +} + +local climat1 = { + offset = 80, + scale = 10, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.6, + lacunarity = 2, +} + +local climat2 = { + offset = 30, + scale = 10, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.6, + lacunarity = 2, +} + +local climat0 = { + offset = 50, + scale = 0, + spread = {x = 10, y = 10, z = 10}, + seed = 47, + octaves = 8, + persistence = 0.4, + lacunarity = 2, +} + +---LAND +mapGenerator:RegisterPeak("world", vector.new(0,0,0), { - landscapeNoise = { - offset = 30, - scale = 2, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, + landscapeNoise = land1, + tempNoise = climat0, + humidityNoise = climat0, }, - {is3d = 1}) - -local b = mapGenerator:RegisterPeak("world", - vector.new(0,50,0), - { - landscapeNoise = { - offset = 30, - scale = 2, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, - }, - {is3d = 1}) - -local c = mapGenerator:RegisterPeak("world", - vector.new(50,0,0), - { - landscapeNoise = { - offset = 30, - scale = 2, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, - }, - {is3d = 1, is2d = 1}) ---[[local d = mapGenerator:RegisterPeak("world", - vector.new(50,50,0), - { - landscapeNoise = { - offset = 30, - scale = 2, - spread = {x = 10, y = 100, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, - }, - {is3d = 1, is2d = 1})--]] - -local e = mapGenerator:RegisterPeak("world", - vector.new(25,25,25), - { - landscapeNoise = { - offset = 10, - scale = 2, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, - }, - {is3d = 1, is2d = 1}) + {is2d = 1, is3d = 1}) mapGenerator:RegisterPeak("world", - vector.new(25,25,-25), + vector.new(25,0,25), { - landscapeNoise = { - offset = 20, - scale = 2, - spread = {x = 10, y = 10, z = 10}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, + landscapeNoise = land2, + tempNoise = climat1, + humidityNoise = climat1, }, - {is3d = 1, is2d = 1}) ---[[ - local e = mapGenerator:RegisterPeak("world", - vector.new(-50,0,50), - { - landscapeNoise = { - offset = 40, - scale = 0, - spread = {x = 100, y = 100, z = 100}, - seed = 47, - octaves = 8, - persistence = 0.4, - lacunarity = 2, - }, - }, - true, 1.0)--]] + {is2d = 1, is3d = 1}) ---[[ -mapGenerator:RegisterRegion("world", - vector.new(-50, 0, -50), - vector.new(50, 0, 50), +mapGenerator:RegisterPeak("world", + vector.new(-25,0,-25), { - landscapeNoise = { - offset = -60, - scale = 10, - spread = {x = 100, y = 100, z = 100}, - seed = 41, - octaves = 3, - persistence = 0.4, - lacunarity = 2, - } + landscapeNoise = land2, + tempNoise = climat1, + humidityNoise = climat1, }, - true, 1.5) --]] + {is2d = 1, is3d = 1}) + +mapGenerator:RegisterPeak("world", + vector.new(25,0, -25), + { + landscapeNoise = land2, + tempNoise = climat2, + humidityNoise = climat2, + }, + {is2d = 1, is3d = 1}) + +mapGenerator:RegisterPeak("world", + vector.new(-25,0,25), + { + landscapeNoise = land2, + tempNoise = climat2, + humidityNoise = climat2, + }, + {is2d = 1, is3d = 1}) + + mapGenerator:RegisterPeak("world", + vector.new(25,50,25), + { + landscapeNoise = land1, + tempNoise = climat1, + humidityNoise = climat1, + }, + {is3d = 1}) + +mapGenerator:RegisterPeak("world", + vector.new(-25,50,-25), + { + landscapeNoise = land1, + tempNoise = climat2, + humidityNoise = climat2, + }, + {is3d = 1}) mapGenerator:RegisterBiome("biome1", 0, 0, { soil = "soils:clay_soil_baren",