MapGen: fastgen?

This commit is contained in:
Van
2026-03-11 23:34:09 +03:00
parent e34fd29a4b
commit df247108ef
2 changed files with 55 additions and 25 deletions

View File

@@ -1,17 +1,19 @@
local mathAbs, mathRound, mathMin, mathMax, id
= math.abs, math.round, math.min, math.max, core.get_content_id
local mathAbs, mathRound, mathMin, mathMax, id, posToString
= math.abs, math.round, math.min, math.max, core.get_content_id, core.pos_to_string
local getInstance = Class.getInstance
local LAST_PROFILE = 0.0
local PROFILE = true
local function profile(text)
if not PROFILE then return end
local function calculateAverage(t)
if #t == 0 then return 0 end -- Защита от деления на ноль
local sum = 0
for i = 1, #t do
sum = sum + t[i]
end
return sum / #t
end
local delta = LAST_PROFILE - core.get_us_time()
LAST_PROFILE = core.get_us_time()
core.log(text..': '..tostring(delta))
local function mglog(text, ...)
core.log((text):format(...))
end
local modInfo = Mod.getInfo('smc__core__map_gen')
@@ -581,6 +583,7 @@ end
---@param y integer
---@param z integer
local function generateNode(mapGenerator, layer, factors, data, index, x, y, z)
local ids = mapGenerator.nodeIDs
local waterLevel = layer.waterLevel
local height, temp, humidity = factors.height, factors.temp, factors.humidity
@@ -816,14 +819,15 @@ end
---@param x integer
---@param y integer
---@param z integer
---@return number, number, number
local function generatorHandler(mapGenerator, data, index, x, y, z)
LAST_PROFILE = core.get_us_time()
local startGetLayerTime = 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
static:generateStaticNode(mapGenerator, data, index, x, y, z)
return
return core.get_us_time() - startGetLayerTime, 0, 0
end
-- If generation occurs outside the layers, then a void is generated.
@@ -831,29 +835,33 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
if layer == nil then
data[index] = mapGenerator.nodeIDs.air
return
return core.get_us_time() - startGetLayerTime, 0, 0
end
local endGetLayerTime = core.get_us_time()
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
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
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
return
return endGetLayerTime - startGetLayerTime, core.get_us_time() - startNoiseCalcTime, 0
end
profile('getNoisesValue')
-- We recalculate the climate based on altitude.
noiseTempValue = layer:calcTemp(noiseTempValue, y)
@@ -866,9 +874,14 @@ local function generatorHandler(mapGenerator, data, index, x, y, z)
humidity = noiseHumidityValue,
}
local endNoiseCalcTime = core.get_us_time()
local startGenNodeTime = endNoiseCalcTime
generateNode(mapGenerator, layer, factors, data, index, x, y, z)
profile('generateNode')
--PROFILE = false
local endGenNodeTime = core.get_us_time()
return endGetLayerTime - startGetLayerTime, endNoiseCalcTime - startNoiseCalcTime, endGenNodeTime - startGenNodeTime
end
---Chunk generation handler.
@@ -877,9 +890,14 @@ end
---@param maxPos MapPosition
---@param blockseed number
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
-- We obtain a generation area for further manipulations
--local voxelManip, eMin, eMax = core.get_mapgen_object('voxelmanip')
core.log('Chunk run')
local minPosStr, maxPosStr = posToString(minPos), posToString(maxPos)
local getLayerTime, NoiseCalcTime, GenNodeTime = {}, {}, {}
mglog('Chunk run:\n'..
'min_pos: %s\n'..
'max_pos: %s\n',
minPosStr, maxPosStr)
local startTime = core.get_us_time()
local eMin, eMax = voxelManip:get_emerged_area()
@@ -903,13 +921,16 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
index = area:index(x, y, z)
if data[index] == core.CONTENT_AIR then
generatorHandler(self, data, index, x, y, z)
local t1, t2, t3 = generatorHandler(self, data, index, x, y, z)
table.insert(getLayerTime, t1)
table.insert(NoiseCalcTime, t2)
table.insert(GenNodeTime, t3)
end
end
end
end
core.log('Postprocces run')
-- Post-proccesing generation
for _, ppdata in pairs(postProcessing) do
@@ -937,7 +958,14 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
voxelManip:calc_lighting()
voxelManip:update_liquids()
--voxelManip:write_to_map() -- Async edit
local endTime = core.get_us_time()
mglog('Chunk end:\n'..
'min_pos: %s\n'..
'max_pos: %s\n'..
'time (sec): %.3f\n'..
'GenerateHandler time (us): %.3f %.3f %.3f\n'..
'---------------',
minPosStr, maxPosStr, (endTime - startTime)/1000000,calculateAverage(getLayerTime), calculateAverage(NoiseCalcTime), calculateAverage(GenNodeTime))
end