diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index bd0556e..10a6730 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -1,5 +1,9 @@ -local mathAbs, mathRound, mathMin, mathMax, posToString - = math.abs, math.round, math.min, math.max, core.pos_to_string +local mathAbs, mathRound, mathMin, mathMax, mathRandom, mathClamp, tableInsert + = math.abs, math.round, math.min, math.max, math.random, math.clamp, table.insert + +local posToString, getUsTime = core.pos_to_string, core.get_us_time + +local pairs, ipairs, assert = pairs, ipairs, assert local v = vector.new local id = core.get_content_id @@ -26,10 +30,11 @@ local require = modInfo.require ---@alias MapGen.factors {height:number, temp:number, humidity:number} ----@alias MapGen.PastCalcNoiseValue {triangle:MapGen.Triangle?, x:integer?, z:integer?, height:number?, temp:number?, humidity:number?} +---@type table> +local pastNoiseValues = {} ----@type MapGen.PastCalcNoiseValue -local pastNoisesCalc = {} +---@type MapGen.Triangle? +local pastTriangle ---@type MapGen.Layer local Layer = require('MapGen.Layer') @@ -46,8 +51,6 @@ local Cavern = require('MapGen.Cavern') ---@type MapGen.Region local Region = require('MapGen.Region') -local ff2luat = require('MapGen.ff2luat') - ---@class MapGen ---@field layersByName table ---@field layersList MapGen.Layer[] @@ -307,7 +310,7 @@ function MapGen:getTriangulateBoundaryVertices(triangles) for key, count in pairs(edgeCount) do if count == 1 then - table.insert(boundaryEdges, edgeData[key]) + tableInsert(boundaryEdges, edgeData[key]) end end @@ -331,8 +334,8 @@ function MapGen:getTriangulateBoundaryVertices(triangles) adjacency[k1] = adjacency[k1] or {} adjacency[k2] = adjacency[k2] or {} - table.insert(adjacency[k1], k2) - table.insert(adjacency[k2], k1) + tableInsert(adjacency[k1], k2) + tableInsert(adjacency[k2], k1) end -- Walk along boundary edges to create ordered vertex list @@ -352,7 +355,7 @@ function MapGen:getTriangulateBoundaryVertices(triangles) visited[currentKey] = true local pos = vertexData[currentKey].pos - table.insert(boundary, { + tableInsert(boundary, { x = pos.x, y = 0, z = pos.z @@ -394,7 +397,9 @@ function MapGen:registerLayer(name, def) local layer = Layer:new(name, def) self.layersByName[name] = layer - table.insert(self.layersList, layer) + tableInsert(self.layersList, layer) + + pastNoiseValues[name] = {} -- Sort layers by maxY descending for processing priority. table.sort(self.layersList, function(a, b) @@ -429,7 +434,7 @@ function MapGen:registerStaticLayer(name, def) local static = StaticLayer:new(name, def) self.staticsByName[name] = static - table.insert(self.staticsList, static) + tableInsert(self.staticsList, static) -- Sort srtatics by maxY descending for processing priority. table.sort(self.staticsList, function(a, b) @@ -494,11 +499,11 @@ end ---Initialize the boundary region for a layer and category. ---@param layer MapGen.Layer -function MapGen:initTriangultaionBoundaryRegion(layer, category) +function MapGen:initTriangultaionBoundaryRegion(layer) local triangles = layer.trianglesList local regionVertices = self:getTriangulateBoundaryVertices(triangles) - local regionName = category..'_boundary' + local regionName = 'triangles_boundary' local region = Region:new(regionName, {vertices = regionVertices}) @@ -506,12 +511,10 @@ function MapGen:initTriangultaionBoundaryRegion(layer, category) end ---Initialize border regions for all layers and all categories. -function MapGen:initLayersBoundaryRegions() +function MapGen:initLayersBoundaryRegion() ---@param layer MapGen.Layer for _, layer in ipairs(self.layersList) do - self:initTriangultaionBoundaryRegion(layer, 'landscape') - self:initTriangultaionBoundaryRegion(layer, 'temp') - self:initTriangultaionBoundaryRegion(layer, 'humidity') + self:initTriangultaionBoundaryRegion(layer) end end @@ -608,17 +611,17 @@ local function generateNode(mapGenerator, layer, factors, data, index, x, y, z) local scattHeight = y if scatteringGorizontal ~= 0 then - scattTemp = scattTemp + math.random(scatteringGorizontal * -1, scatteringGorizontal) - scattHumidity = scattHumidity + math.random(scatteringGorizontal * -1, scatteringGorizontal) + scattTemp = scattTemp + mathRandom(scatteringGorizontal * -1, scatteringGorizontal) + scattHumidity = scattHumidity + mathRandom(scatteringGorizontal * -1, scatteringGorizontal) end if scatteringVertical ~= 0 then - scattHeight = scattHeight + math.random(scatteringVertical * -1, scatteringVertical) + scattHeight = scattHeight + mathRandom(scatteringVertical * -1, scatteringVertical) end - scattTemp = math.clamp(scattTemp, layer.minTemp, layer.maxTemp) - scattHumidity = math.clamp(scattHumidity, layer.minHumidity, layer.maxHumidity) - scattHeight = math.clamp(scattHeight, layer.minY, layer.maxY) + scattTemp = mathClamp(scattTemp, layer.minTemp, layer.maxTemp) + scattHumidity = mathClamp(scattHumidity, layer.minHumidity, layer.maxHumidity) + scattHeight = mathClamp(scattHeight, layer.minY, layer.maxY) scattHeight, scattTemp, scattHumidity = mathRound(scattHeight), mathRound(scattTemp), mathRound(scattHumidity) height, temp, humidity = mathRound(height), mathRound(scattTemp), mathRound(scattHumidity) @@ -694,7 +697,7 @@ end ---@param z integer ---@return number local function calcWeight(height, x, y, z) - local pos1 = vector.new(x, y, z) + local pos1 = v(x, y, z) local pos2 = height.p1.pos local pos3 = height.p2.pos @@ -708,7 +711,7 @@ local function calcWeight(height, x, y, z) local posP = pos2 + v * t - return 1 - vector.distance(pos2, posP) / height:length() + return 1 - pos2:distance(posP) / height:length() end ---Returns the weighted values of 2d noise. @@ -761,29 +764,30 @@ end ---Calculates a smoothed 2D noise value based on a preliminary space partitioning. --- ---Returns `nil` if the point is not included in any triangle. ----@param category MapGen.Layer.Category ---@param layer MapGen.Layer ---@param x integer ---@param y integer ---@param z integer ---@return number?, number?, number? -function MapGen:getNoises2dValue(category, layer, x, y, z) +function MapGen:getFactors(layer, x, y, z) + local posString = posToString({x=x,y=0,z=z}) - if (pastNoisesCalc.x ~= nil and pastNoisesCalc.z ~= nil) and (x == pastNoisesCalc.x and z == pastNoisesCalc.z) then - return pastNoisesCalc.height, pastNoisesCalc.temp, pastNoisesCalc.humidity + local factors = pastNoiseValues[layer.name][posString] + if factors ~= nil then + return factors.height, factors.temp, factors.humidity end -- TODO: это сильно тормозит обычные чанки, но ускоряет те, что находятся за границей - if not self:pointInRegion2D(layer, category..'_boundary', {x=x, y=y, z=z}) then + if not self:pointInRegion2D(layer, 'triangles_boundary', {x=x, y=y, z=z}) then return end - if pastNoisesCalc.triangle ~= nil then + if pastTriangle ~= nil then local triangles = { - pastNoisesCalc.triangle, - pastNoisesCalc.triangle.neighbours[1], - pastNoisesCalc.triangle.neighbours[2], - pastNoisesCalc.triangle.neighbours[3], + pastTriangle, + pastTriangle.neighbours[1], + pastTriangle.neighbours[2], + pastTriangle.neighbours[3], } ---@param triangle MapGen.Triangle @@ -793,18 +797,18 @@ function MapGen:getNoises2dValue(category, layer, x, y, z) local temp = calcNoise2dValue('temp', layer, triangle, x, y, z) local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z) - pastNoisesCalc.x = x - pastNoisesCalc.z = z - pastNoisesCalc.height = height - pastNoisesCalc.temp = temp - pastNoisesCalc.humidity = humidity + pastNoiseValues[layer.name][posString] = { + height = height, + temp = temp, + humidity = humidity + } return height, temp, humidity end end end - local triangle = self:findTriangle(layer, x, z, pastNoisesCalc.triangle) + local triangle = self:findTriangle(layer, x, z, pastTriangle) if triangle == nil then return end @@ -813,12 +817,12 @@ function MapGen:getNoises2dValue(category, layer, x, y, z) local temp = calcNoise2dValue('temp', layer, triangle, x, y, z) local humidity = calcNoise2dValue('humidity', layer, triangle, x, y, z) - pastNoisesCalc.triangle = triangle - pastNoisesCalc.x = x - pastNoisesCalc.z = z - pastNoisesCalc.height = height - pastNoisesCalc.temp = temp - pastNoisesCalc.humidity = humidity + pastTriangle = triangle + pastNoiseValues[layer.name][posString] = { + height = height, + temp = temp, + humidity = humidity + } return height, temp, humidity end @@ -833,31 +837,31 @@ end ---@param z integer ---@return number, number, number local function generatorHandler(mapGenerator, layer, data, index, x, y, z) - local startGetLayerTime = core.get_us_time() + local startGetLayerTime = getUsTime() -- 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 core.get_us_time() - startGetLayerTime, 0, 0 + return getUsTime() - startGetLayerTime, 0, 0 end -- If generation occurs outside the layers, then a void is generated. if layer == nil then data[index] = mapGenerator.nodeIDs.air - return core.get_us_time() - startGetLayerTime, 0, 0 + return getUsTime() - startGetLayerTime, 0, 0 end - local endGetLayerTime = core.get_us_time() + local endGetLayerTime = getUsTime() local startNoiseCalcTime = endGetLayerTime -- If the value of the noise is `nil`, -- then the point does not fall within any of the triangles. - local height, temp, humidity = mapGenerator:getNoises2dValue('landscape', layer, x, y, z) + local height, temp, humidity = mapGenerator:getFactors(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 + return endGetLayerTime - startGetLayerTime, getUsTime() - startNoiseCalcTime, 0 end -- We recalculate the climate based on altitude. @@ -871,12 +875,12 @@ local function generatorHandler(mapGenerator, layer, data, index, x, y, z) humidity = humidity, } - local endNoiseCalcTime = core.get_us_time() + local endNoiseCalcTime = getUsTime() local startGenNodeTime = endNoiseCalcTime generateNode(mapGenerator, layer, factors, data, index, x, y, z) - local endGenNodeTime = core.get_us_time() + local endGenNodeTime = getUsTime() return endGetLayerTime - startGetLayerTime, endNoiseCalcTime - startNoiseCalcTime, endGenNodeTime - startGenNodeTime end @@ -896,7 +900,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed) 'max_pos: %s\n', minPosStr, maxPosStr) end - local startTime = core.get_us_time() + local startTime = getUsTime() local eMin, eMax = voxelManip:get_emerged_area() @@ -927,9 +931,9 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed) if data[index] == core.CONTENT_AIR then local t1, t2, t3 = generatorHandler(self, layer, data, index, x, y, z) - table.insert(getLayerTime, t1) - table.insert(NoiseCalcTime, t2) - table.insert(GenNodeTime, t3) + tableInsert(getLayerTime, t1) + tableInsert(NoiseCalcTime, t2) + tableInsert(GenNodeTime, t3) end end end @@ -962,7 +966,9 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed) voxelManip:calc_lighting() voxelManip:update_liquids() - local endTime = core.get_us_time() + pastTriangle = nil + + local endTime = getUsTime() if LOG_PROFILE then mglog('Chunk end:\n'.. 'min_pos: %s\n'.. @@ -998,7 +1004,7 @@ function MapGen:run() end) self:initBiomesDiagram() - self:initLayersBoundaryRegions() + self:initLayersBoundaryRegion() MapGen.isRunning = true core.log('MapGen run')