MapGen: removing unnecessary code. Completion of work on a new triangulation method. Close #25
This commit is contained in:
@@ -3,6 +3,17 @@ local mathAbs, mathRound, mathMin, mathMax, id
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
local LAST_PROFILE = 0.0
|
||||
local PROFILE = true
|
||||
local function profile(text)
|
||||
if not PROFILE then return end
|
||||
|
||||
local delta = LAST_PROFILE - core.get_us_time()
|
||||
LAST_PROFILE = core.get_us_time()
|
||||
|
||||
core.log(text..': '..tostring(delta))
|
||||
end
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||
local require = modInfo.require
|
||||
|
||||
@@ -26,9 +37,6 @@ local StaticLayer = require('MapGen.StaticLayer')
|
||||
---@type MapGen.Biome
|
||||
local Biome = require('MapGen.Biome')
|
||||
|
||||
---@type MapGen.Triangulation
|
||||
local Triangulation = require('MapGen.Triangulation')
|
||||
|
||||
---@type MapGen.Cavern
|
||||
local Cavern = require('MapGen.Cavern')
|
||||
|
||||
@@ -415,22 +423,6 @@ function MapGen:registerStaticLayer(name, def)
|
||||
end)
|
||||
end
|
||||
|
||||
---Adds groups to the already registered groups in the peak by coordinate.
|
||||
---If the peak does not exist, an error will be raised.
|
||||
---@param layerName string The name of the layer in which the new peak will be included.
|
||||
---@param category MapGen.Layer.Category Category indicating what will be generated by peak.
|
||||
---@param pos MapPosition The position of the peak to which the group should be added.
|
||||
---@param groups table<string, number> Groups to be added.
|
||||
function MapGen:addGroupsToPeakByPos(layerName, category, pos, groups)
|
||||
local layer = self:getLayerByName(layerName)
|
||||
|
||||
local peak = self:getPeakByPos(layer, category, pos)
|
||||
assert( peak ~= nil, ('In the `%s` layer in the `%s` category there is no peak with coordinates: %s, %s, %s.'):format(
|
||||
layerName, category, pos.x, pos.y, pos.z))
|
||||
|
||||
peak:addGroups(groups)
|
||||
end
|
||||
|
||||
---Mark a point on the biomes diagram and constrain the world area by height to mark it as `MapGen.Biome`.
|
||||
---@param layerName string The name of the layer in which the new biome will be included.
|
||||
---@param biomeName string The name to be assigned to the biome. The name must be unique for each biome.
|
||||
@@ -817,96 +809,6 @@ function MapGen:getNoises2dValue(category, layer, x, y, 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
|
||||
|
||||
--[[
|
||||
3D noise smoothing is too slow, so it was decided to stop using it.
|
||||
|
||||
---@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
|
||||
|
||||
---Calculates a smoothed 3D noise value based on a preliminary space partitioning.
|
||||
---
|
||||
---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?
|
||||
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 noiseValue = 0.0
|
||||
|
||||
local totalWeight = 0.0
|
||||
|
||||
-- ... we calculate the height for this point using smoothing.
|
||||
local weight = calcWeight(tetrahedron.h1, x, y, z)
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
weight = calcWeight(tetrahedron.h2, x, y, z)
|
||||
noiseValue = noiseValue + peak1:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
weight = calcWeight(tetrahedron.h3, x, y, z)
|
||||
noiseValue = noiseValue + peak2:getMultinoise().someNoise:get_3d({x = x, y = y, z = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
weight = calcWeight(tetrahedron.h4, x, y, z)
|
||||
noiseValue = noiseValue + peak3:getMultinoise().someNoise:get_2d({x = x, y = y, z = z}) * weight
|
||||
totalWeight = totalWeight + weight
|
||||
|
||||
return mathRound(noiseValue / 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
|
||||
--]]
|
||||
|
||||
---Generator handler for specific coordinates.
|
||||
---@param mapGenerator MapGen
|
||||
---@param data number[]
|
||||
@@ -915,6 +817,7 @@ end
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
LAST_PROFILE = core.get_us_time()
|
||||
-- If the node belongs to a static layer, then a static node is generated.
|
||||
local static = mapGenerator:getStaticByHeight(y)
|
||||
if static ~= nil then
|
||||
@@ -950,6 +853,7 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
data[index] = core.CONTENT_IGNORE
|
||||
return
|
||||
end
|
||||
profile('getNoisesValue')
|
||||
|
||||
-- We recalculate the climate based on altitude.
|
||||
noiseTempValue = layer:calcTemp(noiseTempValue, y)
|
||||
@@ -963,6 +867,8 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
|
||||
}
|
||||
|
||||
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
|
||||
profile('generateNode')
|
||||
--PROFILE = false
|
||||
end
|
||||
|
||||
---Chunk generation handler.
|
||||
|
||||
Reference in New Issue
Block a user