MapGen: There is now only one triangulation map instead of three.

This commit is contained in:
Van
2026-03-16 20:59:39 +03:00
parent 93bda75727
commit e986a4c538
2 changed files with 75 additions and 110 deletions

View File

@@ -26,14 +26,10 @@ local require = modInfo.require
---@alias MapGen.factors {height:number, temp:number, humidity:number}
---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, noiseValue:number?}
---@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, height:number?, temp:number?, humidity:number?}
---@type table<MapGen.Layer.Category, MapGen.PastCalcNoiseValue>
local pastNoise2DCalc = {
landscape = {},
temp = {},
humidity = {}
}
---@type MapGen.PastCalcNoiseValue
local pastNoisesCalc = {}
---@type MapGen.Layer
local Layer = require('MapGen.Layer')
@@ -146,16 +142,15 @@ end
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
---@param layer string|MapGen.Layer The layer or name of the layer in which the peak is located.
---@param category MapGen.Layer.Category The category in which the peak is located.
---@param pos MapPosition Coordinate of the desired peak.
---@return MapGen.Peak?
function MapGen:getPeakByPos(layer, category, pos)
function MapGen:getPeakByPos(layer, pos)
if type(layer) == 'string' then
local layerName = layer
layer = self:getLayerByName(layerName)
end
return layer:getPeakByPos(category, pos)
return layer:getPeakByPos(pos)
end
---Checks if a point is inside a triangle using the vector product method.
@@ -189,14 +184,13 @@ function MapGen:isPointInTriangle(triangle, pointPos)
end
---@param layer MapGen.Layer The layer of the layer the triangle is in.
---@param category MapGen.Layer.Category The category in which the triangle is in.
---@param x integer
---@param z integer
---@param triangle MapGen.Triangle?
---@return MapGen.Triangle?
function MapGen:findTriangle(layer, category, x, z, triangle)
function MapGen:findTriangle(layer, x, z, triangle)
if triangle == nil then
triangle = layer.trianglesList[category][1]
triangle = layer.trianglesList[1]
end
local iterationCount = 1000000
@@ -209,7 +203,9 @@ function MapGen:findTriangle(layer, category, x, z, triangle)
end
iterationCount = iterationCount - 1
assert(triangle)
if triangle == nil then
return nil
end
isFound, triangle = self:isPointInTriangle(triangle, v(x, 0, z))
end
@@ -498,9 +494,8 @@ end
---Initialize the boundary region for a layer and category.
---@param layer MapGen.Layer
---@param category MapGen.Layer.Category
function MapGen:initTriangultaionBoundaryRegion(layer, category)
local triangles = layer.trianglesList[category]
local triangles = layer.trianglesList
local regionVertices = self:getTriangulateBoundaryVertices(triangles)
local regionName = category..'_boundary'
@@ -771,55 +766,61 @@ end
---@param x integer
---@param y integer
---@param z integer
---@return number?
---@return number?, number?, number?
function MapGen:getNoises2dValue(category, layer, x, y, z)
---@type MapGen.PastCalcNoiseValue
local pastCalc = pastNoise2DCalc[category]
if (pastCalc.x ~= nil and pastCalc.z ~= nil) and (x == pastCalc.x and z == pastCalc.z) then
return pastCalc.noiseValue
if (pastNoisesCalc.x ~= nil and pastNoisesCalc.z ~= nil) and (x == pastNoisesCalc.x and z == pastNoisesCalc.z) then
return pastNoisesCalc.height, pastNoisesCalc.temp, pastNoisesCalc.humidity
end
-- TODO: это сильно тормозит обычные чанки, но ускоряет те, что находятся за границей
if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then
return nil
return
end
if pastCalc.triangle ~= nil then
---@type number?
local noiseValue
if pastNoisesCalc.triangle ~= nil then
local triangles = {
pastCalc.triangle,
pastCalc.triangle.neighbours[1],
pastCalc.triangle.neighbours[2],
pastCalc.triangle.neighbours[3],
pastNoisesCalc.triangle,
pastNoisesCalc.triangle.neighbours[1],
pastNoisesCalc.triangle.neighbours[2],
pastNoisesCalc.triangle.neighbours[3],
}
---@param t MapGen.Triangle
for _, t in ipairs(triangles) do
if self:isPointInTriangle(t, v(x, 0, z)) then
noiseValue = calcNoise2dValue(category, layer, t, x, y, z)
---@param triangle MapGen.Triangle
for _, triangle in ipairs(triangles) do
if self:isPointInTriangle(triangle, v(x, 0, z)) then
local height = calcNoise2dValue('landscape', layer, triangle, x, y, z)
local temp = calcNoise2dValue('temp', layer, triangle, x, y, z)
local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z)
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
pastNoisesCalc.x = x
pastNoisesCalc.z = z
pastNoisesCalc.height = height
pastNoisesCalc.temp = temp
pastNoisesCalc.humidity = humidity
return noiseValue
return height, temp, humidity
end
end
end
local triangle = self:findTriangle(layer, category, x, z, pastCalc.triangle)
assert(triangle ~= nil)
local triangle = self:findTriangle(layer, x, z, pastNoisesCalc.triangle)
if triangle == nil then
return
end
local noiseValue = calcNoise2dValue(category, layer, triangle, x, y, z)
local height = calcNoise2dValue('landscape', layer, triangle, x, y, z)
local temp = calcNoise2dValue('temp', layer, triangle, x, y, z)
local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z)
pastCalc.triangle = triangle
pastCalc.x = x
pastCalc.z = z
pastCalc.noiseValue = noiseValue
pastNoisesCalc.triangle = triangle
pastNoisesCalc.x = x
pastNoisesCalc.z = z
pastNoisesCalc.height = height
pastNoisesCalc.temp = temp
pastNoisesCalc.humidity = humidity
return noiseValue
return height, temp, humidity
end
---Generator handler for specific coordinates.
@@ -852,36 +853,22 @@ local function generatorHandler(mapGenerator, layer, data, index, x, y, z)
local startNoiseCalcTime = endGetLayerTime
-- If the value of the noise is `nil`,
-- then the point does not fall within any of the triangles.
local noiseHeightValue = mapGenerator:getNoises2dValue('landscape', layer, x, y, z)
if noiseHeightValue == nil then
data[index] = core.CONTENT_IGNORE
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
end
local noiseTempValue = mapGenerator:getNoises2dValue('temp', layer, x, y, z)
if noiseTempValue == nil then
data[index] = core.CONTENT_IGNORE
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
end
local noiseHumidityValue = mapGenerator:getNoises2dValue('humidity', layer, x, y, z)
if noiseHumidityValue == nil then
local height, temp, humidity = mapGenerator:getNoises2dValue('landscape', layer, x, y, z)
if height == nil or temp == nil or humidity == nil then
data[index] = core.CONTENT_IGNORE
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
end
-- We recalculate the climate based on altitude.
noiseTempValue = layer:calcTemp(noiseTempValue, y)
noiseHumidityValue = layer:calcHumidity(noiseHumidityValue, y)
temp = layer:calcTemp(temp, y)
humidity = layer:calcHumidity(humidity, y)
---@type MapGen.factors
local factors = {
height = noiseHeightValue,
temp = noiseTempValue,
humidity = noiseHumidityValue,
height = height,
temp = temp,
humidity = humidity,
}
local endNoiseCalcTime = core.get_us_time()

View File

@@ -19,8 +19,8 @@ local Triangle = require('MapGen.Triangle')
---@field waterLevel integer
---@field calcTemp fun(self:MapGen.Layer, value:number, height:number):number
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:number):number
---@field peaksList table<MapGen.Layer.Category, MapGen.Peak[]>
---@field peaksByPos table<MapGen.Layer.Category, table<string, MapGen.Peak>>
---@field peaksList MapGen.Peak[]
---@field peaksByPos table<string, MapGen.Peak>
---@field noisesByColor table<MapGen.Layer.Category, table<ColorString, ValueNoise>>
---@field noisesParamsByColor table<MapGen.Layer.Category, table<ColorString, NoiseParams>>
---@field biomesByName table<string, MapGen.Biome>
@@ -31,18 +31,10 @@ local Triangle = require('MapGen.Triangle')
---@field cavernsByName table<string, MapGen.Cavern>
---@field cavernsList MapGen.Cavern[]
---@field regionsByName table<string, MapGen.Region>
---@field trianglesList table<MapGen.Layer.Category, MapGen.Triangle[]>
---@field trianglesList MapGen.Triangle[]
local Layer = {
peaksList = {
landscape = {},
temp = {},
humidity = {},
},
peaksByPos = {
landscape = {},
temp = {},
humidity = {},
},
peaksList = {},
peaksByPos = {},
noisesByColor = {
landscape = {},
temp = {},
@@ -59,11 +51,7 @@ local Layer = {
cavernsByName = {},
cavernsList = {},
regionsByName = {},
trianglesList = {
landscape = {},
temp = {},
humidity = {},
},
trianglesList = {},
tetrahedronsList = {},
}
@@ -103,10 +91,9 @@ end
---**Note:** If the colors of the new peak and the existing one at the same coordinates
---are different, an error will be called.
---@param def MapGen.PeakDef
---@param category MapGen.Layer.Category
---@param scale number
---@return MapGen.Peak
function Layer:registerPeak(def, category, scale)
function Layer:registerPeak(def, scale)
assert(def.pos ~= nil)
assert(def.pos.x ~= nil)
assert(def.pos.z ~= nil)
@@ -119,11 +106,11 @@ function Layer:registerPeak(def, category, scale)
local positionStr = core.pos_to_string(def.pos)
local peak = self.peaksByPos[category][positionStr]
local peak = self.peaksByPos[positionStr]
if peak == nil then
peak = Peak:new(def)
self.peaksByPos[category][positionStr] = peak
self.peaksByPos[positionStr] = peak
elseif peak.color ~= def.color then
error('There cannot be two peaks with the same coordinates but different colors.')
end
@@ -132,11 +119,11 @@ function Layer:registerPeak(def, category, scale)
end
function Layer:initTrianglesNeighbours()
for _, triangles in pairs(self.trianglesList) do
for _, triangle in ipairs(triangles) do
for _, id in ipairs(triangle.neighboursIDs) do
table.insert(triangle.neighbours, triangles[id])
end
local triangles = self.trianglesList
for _, triangle in pairs(triangles) do
for _, id in ipairs(triangle.neighboursIDs) do
table.insert(triangle.neighbours, triangles[id])
end
end
end
@@ -155,9 +142,9 @@ function Layer:getTriaglesFromFile(fileName, category, scale)
assert(triangles ~= nil)
for _, tr in ipairs(triangles) do
local p1 = self:registerPeak( tr.vertices[1], category, scale)
local p2 = self:registerPeak( tr.vertices[2], category, scale)
local p3 = self:registerPeak( tr.vertices[3], category, scale)
local p1 = self:registerPeak( tr.vertices[1], scale)
local p2 = self:registerPeak( tr.vertices[2], scale)
local p3 = self:registerPeak( tr.vertices[3], scale)
local triangle = Triangle:new(tr.id,p1, p2, p3, tr.adjacent_triangle_ids)
@@ -219,7 +206,7 @@ end
---@param category MapGen.Layer.Category
---@param triangle MapGen.Triangle
function Layer:addTriangle(category, triangle)
local triangles = self.trianglesList[category]
local triangles = self.trianglesList
if triangles ~= nil then
triangles[triangle.id] = triangle
else
@@ -231,25 +218,16 @@ end
---@param category MapGen.Layer.Category
---@param peak MapGen.Peak
function Layer:addPeak(category, peak)
if category == 'landscape' then
table.insert(self.peaksList.landscape, peak)
elseif category == 'temp' then
table.insert(self.peaksList.temp, peak)
elseif category == 'humidity' then
table.insert(self.peaksList.humidity, peak)
else
error(('There is no peak category named %s.'):format(category))
end
table.insert(self.peaksList, peak)
end
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
---@param category MapGen.Layer.Category The category in which the peak is located.
---@param pos Position Coordinate of the desired peak.
---@return MapGen.Peak?
function Layer:getPeakByPos(category, pos)
function Layer:getPeakByPos(pos)
local positionStr = core.pos_to_string(pos)
local peak = self.peaksByPos[category][positionStr]
local peak = self.peaksByPos[positionStr]
return peak
end