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

View File

@@ -1,6 +1,8 @@
-- World generation occurs in a separate thread (MapGen Environment). -- World generation occurs in a separate thread (MapGen Environment).
-- But for the main thread, the mapgenerator object is also initialized. -- But for the main thread, the mapgenerator object is also initialized.
core.set_mapgen_setting('chunksize', '{x=1,y=5,z=1}', true)
local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require local mapGeneneratorRequire = Mod.getInfo('smc__core__map_generator').require
-- Main env init -- Main env init