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

View File

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