Mapgen: write SuperUltraSigmaMapgen part 2,5 (ShitGen)

This commit is contained in:
Van
2025-11-07 20:11:39 +03:00
parent 9a49ee3c07
commit b3b74d6160
3 changed files with 367 additions and 46 deletions

View File

@@ -1,6 +1,8 @@
local mathCeil, mathAbs, mathRound, mathMin local mathCeil, mathAbs, mathRound, mathMin
= math.ceil, math.abs, math.round, math.min = math.ceil, math.abs, math.round, math.min
local BUFFER_ZONE_WIDTH = 30
-- --- MapGen default moises --- -- --- MapGen default moises ---
---@type ValueNoise ---@type ValueNoise
local oceanNoise local oceanNoise
@@ -51,7 +53,7 @@ local Biome = require("MapGen.Biome")
---@field biomesDiagram table ---@field biomesDiagram table
---@field multinoiseInitialized boolean Is the fractal noise of the cells initialized? This is necessary for a one-time noise initialization. ---@field multinoiseInitialized boolean Is the fractal noise of the cells 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. ---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
---@field nodeIDs table<string, string> ---@field nodeIDs table<string, number>
local MapGen = { local MapGen = {
layersByName = {}, layersByName = {},
layersList = {}, layersList = {},
@@ -234,6 +236,12 @@ local function generateSoil(biome, data, index, x, y, z)
data[index] = core.get_content_id(biome.groundNodes.turf) -- TODO: убрать тут get_content_id(), переместить его куда-то "выше" data[index] = core.get_content_id(biome.groundNodes.turf) -- TODO: убрать тут get_content_id(), переместить его куда-то "выше"
end end
---@param ids table<string, number>
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function generateRock(ids, data, index, x, y, z) local function generateRock(ids, data, index, x, y, z)
if rocksNoise == nil then if rocksNoise == nil then
@@ -260,6 +268,15 @@ local function generateRock(ids, data, index, x, y, z)
end end
end end
---@param mapGenerator MapGen
---@param hight number
---@param temp number
---@param humidity number
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, y, z) local function generateNode(mapGenerator, hight, temp, humidity, data, index, x, y, z)
local ids = mapGenerator.nodeIDs local ids = mapGenerator.nodeIDs
@@ -276,12 +293,19 @@ local function generateNode(mapGenerator, hight, temp, humidity, data, index, x,
end end
end end
---@param mapGenerator MapGen
---@param cell MapGen.Cell
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function oneCellGeneratorHandler(mapGenerator, cell, data, index, x, y, z) local function oneCellGeneratorHandler(mapGenerator, cell, data, index, x, y, z)
---@type ValueNoise ---@type ValueNoise?
local heightNoise = cell:getMultinoise().landscapeNoise local heightNoise = cell:getMultinoise().landscapeNoise
---@type ValueNoise ---@type ValueNoise?
local tempNoise = cell:getMultinoise().tempNoise local tempNoise = cell:getMultinoise().tempNoise
---@type ValueNoise ---@type ValueNoise?
local humidityNoise = cell:getMultinoise().humidityNoise local humidityNoise = cell:getMultinoise().humidityNoise
-- Default noise's values. -- Default noise's values.
@@ -304,19 +328,40 @@ local function oneCellGeneratorHandler(mapGenerator, cell, data, index, x, y, z)
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end end
local function bufferZoneGeneratorHanler(mapGenerator, cellA, cellB, distanceA, distanceB, data, index, x, y, z) ---@return number
---@type ValueNoise local function smootherstep2(valueA, valueB, x)
x = x + (BUFFER_ZONE_WIDTH / 2)
-- Clamp x to the range [edge0, edge1] and normalize to [0, 1]
---@type number
local t = math.max(0, math.min(1, x / BUFFER_ZONE_WIDTH ))
-- Quintic polynomial: 6t^5 - 15t^4 + 10t^3
local new_t = t * t * t * (t * (t * 6 - 15) + 10)
return valueB + (valueA - valueB) * t--new_t
end
---@param mapGenerator MapGen
---@param cellA MapGen.Cell
---@param cellB MapGen.Cell
---@param distance number
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function twoCellsGeneratorHanler(mapGenerator, cellA, cellB, distance, data, index, x, y, z)
---@type ValueNoise?
local heightNoiseA = cellA:getMultinoise().landscapeNoise local heightNoiseA = cellA:getMultinoise().landscapeNoise
---@type ValueNoise ---@type ValueNoise?
local tempNoiseA = cellA:getMultinoise().tempNoise local tempNoiseA = cellA:getMultinoise().tempNoise
---@type ValueNoise ---@type ValueNoise?
local humidityNoiseA = cellA:getMultinoise().humidityNoise local humidityNoiseA = cellA:getMultinoise().humidityNoise
---@type ValueNoise ---@type ValueNoise?
local heightNoiseB = cellB:getMultinoise().landscapeNoise local heightNoiseB = cellB:getMultinoise().landscapeNoise
---@type ValueNoise ---@type ValueNoise?
local tempNoiseB = cellB:getMultinoise().tempNoise local tempNoiseB = cellB:getMultinoise().tempNoise
---@type ValueNoise ---@type ValueNoise?
local humidityNoiseB = cellB:getMultinoise().humidityNoise local humidityNoiseB = cellB:getMultinoise().humidityNoise
-- Default noise's values. -- Default noise's values.
@@ -325,7 +370,10 @@ local function bufferZoneGeneratorHanler(mapGenerator, cellA, cellB, distanceA,
local noiseHumidityValue = 0.0 local noiseHumidityValue = 0.0
if heightNoiseA ~= nil and heightNoiseB ~= nil then if heightNoiseA ~= nil and heightNoiseB ~= nil then
noiseHeightValue = mathRound(heightNoiseA:get_2d({x = x, y = z})) --TODO интерполяция noiseHeightValue = mathRound(smootherstep2(
heightNoiseA:get_2d({x = x, y = z}),
heightNoiseB:get_2d({x = x, y = z}),
distance))
end end
if tempNoiseA ~= nil and tempNoiseB ~= nil then if tempNoiseA ~= nil and tempNoiseB ~= nil then
@@ -339,7 +387,119 @@ local function bufferZoneGeneratorHanler(mapGenerator, cellA, cellB, distanceA,
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z) generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end end
---@return number
local function smootherstep3(valueA, valueB, valueC, x, y)
x = x + (BUFFER_ZONE_WIDTH / 2)
y = y + (BUFFER_ZONE_WIDTH / 2)
-- Clamp x to the range [edge0, edge1] and normalize to [0, 1]
---@type number
local tx = math.max(0, math.min(1, x / BUFFER_ZONE_WIDTH ))
-- Quintic polynomial: 6t^5 - 15t^4 + 10t^3
--local new_t = t * t * t * (t * (t * 6 - 15) + 10)
local valueAB = valueB + (valueA - valueB) * tx--new_t
local ty = math.max(0, math.min(1, y / BUFFER_ZONE_WIDTH ))
--new_t = t * t * t * (t * (t * 6 - 15) + 10)
local T = 1 + tx + ty
local valueAC = valueC + (valueA - valueC) * ty--new_t
return (valueA * math.min(tx, ty) + valueB * (1-tx) + valueC * (1-ty))/(math.min(tx, ty) + (1-tx) + (1-tx))-- + valueC * (ty/T)) --(valueAC + valueAB)/2--valueA + (valueB - valueA) * tx + (valueC - valueA) * ty
end
---@param mapGenerator MapGen ---@param mapGenerator MapGen
---@param cellA MapGen.Cell
---@param cellB MapGen.Cell
---@param cellC MapGen.Cell
---@param distanceAB number
---@param distanceAC number
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function threeCellsGeneratorHanler(mapGenerator, cellA, cellB, cellC, distanceAB, distanceAC, data, index, x, y, z)
---@type ValueNoise?
local heightNoiseA = cellA:getMultinoise().landscapeNoise
---@type ValueNoise?
local tempNoiseA = cellA:getMultinoise().tempNoise
---@type ValueNoise?
local humidityNoiseA = cellA:getMultinoise().humidityNoise
---@type ValueNoise?
local heightNoiseB = cellB:getMultinoise().landscapeNoise
---@type ValueNoise?
local tempNoiseB = cellB:getMultinoise().tempNoise
---@type ValueNoise?
local humidityNoiseB = cellB:getMultinoise().humidityNoise
---@type ValueNoise?
local heightNoiseC = cellC:getMultinoise().landscapeNoise
---@type ValueNoise?
local tempNoiseC = cellC:getMultinoise().tempNoise
---@type ValueNoise?
local humidityNoiseC = cellC:getMultinoise().humidityNoise
-- Default noise's values.
local noiseHeightValue = oceanNoise:get_2d({x = x, y = z})
local noiseTempValue = 0.0
local noiseHumidityValue = 0.0
local cellPos = (cellC.getCellPos() + cellB.getCellPos())/2
local distanceBC = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2
if heightNoiseA ~= nil and heightNoiseB ~= nil and heightNoiseC ~=nil then
noiseHeightValue = mathRound(smootherstep3(
heightNoiseA:get_2d({x = x, y = z}),
heightNoiseB:get_2d({x = x, y = z}),
heightNoiseC:get_2d({x = x, y = z}),
distanceAB,
distanceAC))
end
if tempNoiseA ~= nil and tempNoiseB ~= nil then
noiseTempValue = mathRound(tempNoiseA:get_2d({x = x, y = z})) --TODO интерполяция
end
if humidityNoiseA ~= nil and humidityNoiseB ~= nil then
noiseHumidityValue = mathRound(humidityNoiseA:get_2d({x = x, y = z})) --TODO интерполяция
end
generateNode(mapGenerator, noiseHeightValue, noiseTempValue, noiseHumidityValue, data, index, x, y, z)
end
---@param pos0 vector
---@param pos1 vector
---@return number
local function calcDistance(pos0, pos1, posN)
-- We find the direction vector of line.
local v = pos1 - pos0
-- We find the coefficients for the parametric notation of the line equation.
local a = pos1.x - pos0.x
local b = pos1.y - pos0.y
local c = pos1.z - pos0.z
-- We find the parameter `t` based on the perpendicular
-- from the point of the generated node to the line.
local t = (a * (posN.x - pos0.x) + b * (posN.y - pos0.y) + c * (posN.z - pos0.z)) / (a^2 + b^2 + c^2)
-- We find the projection point of the generated
-- node point through the parametric equation.
local projection = pos0 + t*v
-- We find the distance between the generated node's point and its projection
-- (we obtain the length of the perpendicular to line).
return math.sqrt((pos0.x - projection.x)^2 + (pos0.y - projection.y)^2 + (pos0.z - projection.z)^2)
end
---@param mapGenerator MapGen
---@param data number[]
---@param index number
---@param x number
---@param y number
---@param z number
local function generatorHandler(mapGenerator, data, index, x, y, z) local function generatorHandler(mapGenerator, data, index, x, y, z)
-- If generation occurs outside the layers, then a void is generated -- If generation occurs outside the layers, then a void is generated
local layer = mapGenerator:getLayerByHeight(y) local layer = mapGenerator:getLayerByHeight(y)
@@ -355,8 +515,8 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
end end
--- We get the two nearest cells. --- We get the two nearest cells.
---@type MapGen.Cell, MapGen.Cell? ---@type MapGen.Cell, MapGen.Cell?, MapGen.Cell?
local cellA, cellB = layer:getCellsByPos(x, y, z) local cellA, cellB, cellC = layer:getCellsByPos(x, y, z)
--- The second cell may not exist, so we will generate only the first one. --- The second cell may not exist, so we will generate only the first one.
if cellB == nil then if cellB == nil then
@@ -364,16 +524,74 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
return return
end end
--- We calculate the distance to the cell centers --[[- We calculate the distance to the cell centers
local cellPos = cellA.getCellPos() local cellPos = cellA.getCellPos()
local distanceA = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2 local distanceA = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2
cellPos = cellB.getCellPos() cellPos = cellB.getCellPos()
local distanceB = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2 local distanceB = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2
if cellC ~= nil then
cellPos = cellC.getCellPos()
local distanceC = (x - cellPos.x)^2 + (y - cellPos.y)^2 + (z - cellPos.z)^2
if mathAbs(distanceB - distanceA) <= BUFFER_ZONE_WIDTH^2 and
mathAbs(distanceC - distanceB) <= BUFFER_ZONE_WIDTH^2 and
mathAbs(distanceC - distanceA) <= BUFFER_ZONE_WIDTH^2
then
threeCellsGeneratorHanler(mapGenerator, cellA, cellB, cellC, distanceA, distanceB, distanceC, data, index, x, y, z)
return
end
end
if x == -45 then
print(x, math.sqrt(distanceB), math.sqrt(distanceA))
end
--- If the point is in the buffer zone... --- If the point is in the buffer zone...
if mathAbs(distanceA - distanceB) <= 1 then if mathAbs(math.sqrt(distanceB) - math.sqrt(distanceA)) <= BUFFER_ZONE_WIDTH^2 then
--- ... we will smooth out the noise. --- ... we will smooth out the noise.
bufferZoneGeneratorHanler() twoCellsGeneratorHanler(mapGenerator, cellA, cellB, distanceA, distanceB, data, index, x, y, z)
return
else
--- ... otherwise we will use the nearest cell.
oneCellGeneratorHandler(mapGenerator, cellA, data, index, x, y, z)
return
end--]]
--[[local pos0 = ( cellB.getCellPos() + cellA.getCellPos() ) / 2
local posB = cellB.getCellPos()
local a = posB.x - pos0.x
local b = posB.y - pos0.y
local c = posB.z - pos0.z
local t = (a * (x - pos0.x) + b * (y - pos0.y) + c * (z - pos0.z)) / (a^2 + b^2 + c^2)
local projection = vector.new(
pos0.x + a * t,
pos0.y + a * t,
pos0.z + a * t
)--]]
local posA = cellA.getCellPos()
local posB = cellB.getCellPos()
local distanceAB = calcDistance( (posB + posA) / 2, posB, vector.new(x,y,z))
if cellC ~= nil then
local posC = cellC.getCellPos()
local distanceAC = calcDistance( (posC + posA) / 2, posC, vector.new(x,y,z))
local distanceBC = calcDistance( (posC + posB) / 2, posC, vector.new(x,y,z))
if distanceAC <= BUFFER_ZONE_WIDTH / 2 and distanceAB <= BUFFER_ZONE_WIDTH / 2 and distanceBC <= BUFFER_ZONE_WIDTH / 2 then
threeCellsGeneratorHanler(mapGenerator, cellA, cellB, cellC, distanceAB, distanceAC, data, index, x, y, z)
return
end
end
--- If the point is in the buffer zone...
if distanceAB <= BUFFER_ZONE_WIDTH / 2 then
--- ... we will smooth out the noise.
twoCellsGeneratorHanler(mapGenerator, cellA, cellB, distanceAB, data, index, x, y, z)
return return
else else
--- ... otherwise we will use the nearest cell. --- ... otherwise we will use the nearest cell.

View File

@@ -25,6 +25,7 @@ end
---@param cell MapGen.Cell ---@param cell MapGen.Cell
function Layer:addCell(cell) function Layer:addCell(cell)
table.insert(self.cellsList, cell) table.insert(self.cellsList, cell)
print(dump(self.cellsList))
-- TODO: добавить сортировку массива для оптимизации. -- TODO: добавить сортировку массива для оптимизации.
-- Вероятно будет лучше в отдельной фунции и засунуть её в MapGen.run -- Вероятно будет лучше в отдельной фунции и засунуть её в MapGen.run
end end
@@ -33,18 +34,20 @@ end
---@param xPos number ---@param xPos number
---@param yPos number ---@param yPos number
---@param zPos number ---@param zPos number
---@return MapGen.Cell, MapGen.Cell? ---@return MapGen.Cell, MapGen.Cell?, MapGen.Cell?
function Layer:getCellsByPos(xPos, yPos, zPos) function Layer:getCellsByPos(xPos, yPos, zPos)
-- TODO: дописать алгоритмы оптимизации: -- TODO: дописать алгоритмы оптимизации:
-- * по сортированному массиву -- * по сортированному массиву
-- * с помощью минимальных расстояний-гарантов -- * с помощью минимальных расстояний-гарантов
---@type MapGen.Cell, MapGen.Cell ---@type MapGen.Cell, MapGen.Cell, MapGen.Cell
local cellA, cellB local cellA, cellB, cellC
-- Note: to optimize the distance, the dots are always in a square. -- Note: to optimize the distance, the dots are always in a square.
local pastDistance = math.huge local pastDistanceA = math.huge
local newDistance = 0.0 local pastDistanceB = math.huge
local pastDistanceC = math.huge
local newDistance
--- @type vector --- @type vector
local cellPos local cellPos
@@ -54,22 +57,49 @@ function Layer:getCellsByPos(xPos, yPos, zPos)
if cellA == nil then if cellA == nil then
cellA = cell cellA = cell
cellPos = cell.getCellPos()
pastDistanceA = (xPos - cellPos.x)^2 + (yPos - cellPos.y)^2 + (zPos - cellPos.z)^2
goto continue goto continue
end end
cellPos = cell.getCellPos() cellPos = cell.getCellPos()
newDistance = (xPos - cellPos.x)^2 + (yPos - cellPos.y)^2 + (zPos - cellPos.z)^2 newDistance = (xPos - cellPos.x)^2 + (yPos - cellPos.y)^2 + (zPos - cellPos.z)^2
if newDistance < pastDistance then if newDistance < pastDistanceA then
cellC = cellB
cellB = cellA cellB = cellA
cellA = cellB cellA = cell
pastDistance = newDistance
pastDistanceC = pastDistanceB
pastDistanceB = pastDistanceA
pastDistanceA = newDistance
goto continue
end
if newDistance < pastDistanceB then
cellC = cellB
cellB = cell
pastDistanceC = pastDistanceB
pastDistanceB = newDistance
goto continue
end
if newDistance < pastDistanceC then
cellC = cell
pastDistanceC = newDistance
goto continue
end end
::continue:: ::continue::
end end
return cellA, cellB return cellA, cellB, cellC
end end
return Layer return Layer

View File

@@ -22,37 +22,110 @@ local mapGenerator = Core.MapGen.Class:new({
mapGenerator:RegisterLayer("world", -1000, 500) mapGenerator:RegisterLayer("world", -1000, 500)
mapGenerator:RegisterCell("world", mapGenerator:RegisterCell("world",
vector.new(-150,0,-150), vector.new(0,0,0),
{ {
landscapeNoise = { landscapeNoise = {
offset = 50, offset = 10,
scale = 10, scale = 0,
spread = {x = 100, y = 100, z = 100}, spread = {x = 100, y = 100, z = 100},
seed = 47, seed = 47,
octaves = 8, octaves = 8,
persistence = 0.4, persistence = 0.4,
lacunarity = 2, lacunarity = 2,
}, },
tempNoise = { },
offset = 50, true, 1.0)
scale = 25,
spread = {x = 10, y = 10, z = 10}, mapGenerator:RegisterCell("world",
seed = 12, vector.new(-100,0,0),
octaves = 2, {
persistence = 0.6, landscapeNoise = {
offset = 10,
scale = 0,
spread = {x = 100, y = 100, z = 100},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2, lacunarity = 2,
}, },
humidityNoise = {
offset = 50,
scale = 25,
spread = {x = 10, y = 10, z = 10},
seed = 12,
octaves = 2,
persistence = 0.6,
lacunarity = 2,
}
}, },
true, 1.5) true, 1.0)
mapGenerator:RegisterCell("world",
vector.new(0,0,-100),
{
landscapeNoise = {
offset = 10,
scale = 0,
spread = {x = 100, y = 100, z = 100},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
},
},
true, 1.0)
mapGenerator:RegisterCell("world",
vector.new(-100,0,-100),
{
landscapeNoise = {
offset = 10,
scale = 0,
spread = {x = 100, y = 100, z = 100},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
},
},
true, 1.0)
mapGenerator:RegisterCell("world",
vector.new(-50,0,50),
{
landscapeNoise = {
offset = 10,
scale = 0,
spread = {x = 100, y = 100, z = 100},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
},
},
true, 1.0)
mapGenerator:RegisterCell("world",
vector.new(50,0,-50),
{
landscapeNoise = {
offset = 10,
scale = 0,
spread = {x = 100, y = 100, z = 100},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
},
},
true, 1.0)
mapGenerator:RegisterCell("world",
vector.new(-50,0,-50),
{
landscapeNoise = {
offset = -20,
scale = 0,
spread = {x = 10, y = 10, z = 10},
seed = 47,
octaves = 8,
persistence = 0.4,
lacunarity = 2,
},
},
true, 2.0)
--[[ --[[
mapGenerator:RegisterRegion("world", mapGenerator:RegisterRegion("world",
vector.new(-50, 0, -50), vector.new(-50, 0, -50),