MapGen: add ore generation and minor refactoring
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
local mathRound, mathRandom, mathClamp, tableInsert
|
||||
= math.round, math.random, math.clamp, table.insert
|
||||
local assert = assert
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
|
||||
local corePosToString, coreGetUsTime = core.pos_to_string, core.get_us_time
|
||||
|
||||
local pairs, ipairs, assert = pairs, ipairs, assert
|
||||
local mathRound, mathRandom, mathClamp = math.round, math.random, math.clamp
|
||||
local tableInsert, tableSort = table.insert, table.sort
|
||||
|
||||
local v = vector.new
|
||||
local id = core.get_content_id
|
||||
local AIR_ID = core.CONTENT_AIR
|
||||
local corePosToString, coreGetUsTime = core.pos_to_string, core.get_us_time
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
@@ -18,16 +18,15 @@ local function mglog(text, ...)
|
||||
core.log((text):format(...))
|
||||
end
|
||||
|
||||
local modInfo = Mod.getInfo('smc__core__map_gen')
|
||||
local require = modInfo.require
|
||||
local require = Mod.getInfo('smc__core__map_gen').require
|
||||
|
||||
---Numeric values defining the surface height and climate for the node.
|
||||
---@alias MapGen.factors {height:number, temp:number, humidity:number}
|
||||
---@alias MapGen.Factors {height:number, temp:number, humidity:number}
|
||||
|
||||
---Table of all past generation factor calculations for nodes.
|
||||
---
|
||||
---Contains generation factors by layer name and coordinate.
|
||||
---@type table<string, table<string, MapGen.factors>>
|
||||
---@type table<string, table<string, MapGen.Factors>>
|
||||
local pastNoiseValues = {}
|
||||
|
||||
---The triangle in which generation occurred for the previous node.
|
||||
@@ -46,6 +45,8 @@ local Cavern = require('MapGen.Cavern')
|
||||
local Region = require('MapGen.Region')
|
||||
---@type MapGen.Tree
|
||||
local Tree = require('MapGen.Tree')
|
||||
---@type MapGen.Ore
|
||||
local Ore = require('MapGen.Ore')
|
||||
|
||||
---@class MapGen
|
||||
---@field layersByName table<string, MapGen.Layer>
|
||||
@@ -58,10 +59,11 @@ local Tree = require('MapGen.Tree')
|
||||
---
|
||||
---@field isLayersNoisesInitialized boolean Is the fractal noise of the layer initialized? This is necessary for a one-time noise initialization.
|
||||
---@field isCavernsNoiseInitialized boolean Is the fractal noise of the caverns initialized? This is necessary for a one-time noise initialization.
|
||||
---@field isOresNoiseInitialized boolean Is the fractal noise of the ores initialized? This is necessary for a one-time noise initialization.
|
||||
---@field isRunning boolean A static field that guarantees that only one instance of the `MapGen` class will work.
|
||||
---
|
||||
---@field nodes table<string, string>
|
||||
---@field nodeIDs table<string, number>
|
||||
---@field nodeIDs table<string, integer>
|
||||
local MapGen = {
|
||||
layersByName = {},
|
||||
layersList = {},
|
||||
@@ -70,6 +72,7 @@ local MapGen = {
|
||||
treesByName = {},
|
||||
isLayersNoisesInitialized = false,
|
||||
isCavernsNoiseInitialized = false,
|
||||
isOresNoiseInitialized = false,
|
||||
isRunning = false,
|
||||
}
|
||||
|
||||
@@ -109,7 +112,7 @@ function MapGen:getLayerByName(layerName)
|
||||
end
|
||||
|
||||
---Determines which layer a node belongs to based on its height coordinate.
|
||||
---@param y number
|
||||
---@param y integer
|
||||
---@return MapGen.Layer?
|
||||
function MapGen:getLayerByHeight(y)
|
||||
--- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
@@ -126,7 +129,7 @@ function MapGen:getLayerByHeight(y)
|
||||
end
|
||||
|
||||
---Determines which statics layer a node belongs to based on its height coordinate.
|
||||
---@param y number
|
||||
---@param y integer
|
||||
---@return MapGen.StaticLayer?
|
||||
function MapGen:getStaticByHeight(y)
|
||||
--- TODO: возможно здесь получится сделать более оптимизированный алгоритм
|
||||
@@ -231,7 +234,7 @@ function MapGen:registerLayer(name, def)
|
||||
pastNoiseValues[name] = {}
|
||||
|
||||
-- Sort layers by maxY descending for processing priority.
|
||||
table.sort(self.layersList, function(a, b)
|
||||
tableSort(self.layersList, function(a, b)
|
||||
return a.maxY > b.maxY
|
||||
end)
|
||||
end
|
||||
@@ -266,7 +269,7 @@ function MapGen:registerStaticLayer(name, def)
|
||||
tableInsert(self.staticsList, static)
|
||||
|
||||
-- Sort srtatics by maxY descending for processing priority.
|
||||
table.sort(self.staticsList, function(a, b)
|
||||
tableSort(self.staticsList, function(a, b)
|
||||
return a.maxY > b.maxY
|
||||
end)
|
||||
end
|
||||
@@ -331,6 +334,17 @@ function MapGen:registerTree(name, def)
|
||||
self.treesByName[name] = tree
|
||||
end
|
||||
|
||||
---@param layerName string
|
||||
---@param name string
|
||||
---@param def MapGen.OreDef
|
||||
function MapGen:registerOre(layerName, name, def)
|
||||
local layer = self:getLayerByName(layerName)
|
||||
|
||||
local ore = Ore:new(name, def)
|
||||
|
||||
layer:addOre(name, ore)
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- --- HELPER METHODS ---
|
||||
@@ -406,7 +420,7 @@ end
|
||||
local function vertexKey(peak)
|
||||
local pos = peak.pos
|
||||
|
||||
return string.format("%.6f,%.6f", pos.x, pos.z)
|
||||
return ('%.6f,%.6f'):format(pos.x, pos.z)
|
||||
end
|
||||
|
||||
---Creates a canonical (sorted) key for an edge.
|
||||
@@ -576,13 +590,25 @@ function MapGen:initCavernsNoise()
|
||||
self.isCavernsNoiseInitialized = true
|
||||
end
|
||||
|
||||
---Initialize ores's noises. This should be called after the map object is loaded.
|
||||
function MapGen:initOresNoise()
|
||||
---@param layer MapGen.Layer
|
||||
for _, layer in ipairs(self.layersList) do
|
||||
for _, ore in pairs(layer.oresByName) do
|
||||
ore:initNoise()
|
||||
end
|
||||
end
|
||||
|
||||
self.isOresNoiseInitialized = true
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- --- GENERATION METHODS ---
|
||||
|
||||
---@alias MapGen.nodeType 'AIR'|'WATER'|'CAVERN_AIR'|'TURF'|'SOIL'|'ROCK'|'BOTTOM'
|
||||
---@alias MapGen.dataPostProcessingData {biome:MapGen.Biome, mapGenerator:MapGen, factors:MapGen.factors, index:number, x:number, y:number, z:number, nodeType:MapGen.nodeType}
|
||||
---@alias MapGen.vmPostProcessingData {biome:MapGen.Biome, mapGenerator:MapGen, factors:MapGen.factors, x:number, y:number, z:number, nodeType:MapGen.nodeType}
|
||||
---@alias MapGen.dataPostProcessingData {biome:MapGen.Biome, mapGenerator:MapGen, factors:MapGen.Factors, index:integer, x:integer, y:integer, z:integer, nodeType:MapGen.nodeType}
|
||||
---@alias MapGen.vmPostProcessingData {biome:MapGen.Biome, mapGenerator:MapGen, factors:MapGen.Factors, x:integer, y:integer, z:integer, nodeType:MapGen.nodeType}
|
||||
|
||||
|
||||
---@type table<number, MapGen.dataPostProcessingData>
|
||||
@@ -592,8 +618,8 @@ local vmPostProcessing = {}
|
||||
|
||||
---@param biome MapGen.Biome
|
||||
---@param mapGenerator MapGen
|
||||
---@param factors MapGen.factors
|
||||
---@param index number
|
||||
---@param factors MapGen.Factors
|
||||
---@param index integer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
@@ -629,15 +655,14 @@ end
|
||||
---and which biome will be generated at the specified coordinates.
|
||||
---@param mapGenerator MapGen
|
||||
---@param layer MapGen.Layer
|
||||
---@param factors MapGen.factors
|
||||
---@param data number[]
|
||||
---@param data2 number[]
|
||||
---@param index number
|
||||
---@param factors MapGen.Factors
|
||||
---@param data integer[]
|
||||
---@param data2 integer[]
|
||||
---@param index integer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
local function generateNode(mapGenerator, layer, factors, data, data2, index, x, y, z)
|
||||
|
||||
local ids = mapGenerator.nodeIDs
|
||||
local waterLevel = layer.waterLevel
|
||||
local height, temp, humidity = factors.height, factors.temp, factors.humidity
|
||||
@@ -645,9 +670,8 @@ local function generateNode(mapGenerator, layer, factors, data, data2, index, x,
|
||||
-- Creating a rougher biome boundary.
|
||||
local scatteringGorizontal = layer.biomesGorizontalScattering or 0
|
||||
local scatteringVertical = layer.biomesVerticalScattering or 0
|
||||
local scattTemp = factors.temp
|
||||
local scattTemp = temp
|
||||
local scattHumidity = humidity
|
||||
---@type number
|
||||
local scattHeight = y
|
||||
|
||||
if scatteringGorizontal ~= 0 then
|
||||
@@ -726,6 +750,14 @@ local function generateNode(mapGenerator, layer, factors, data, data2, index, x,
|
||||
Logger.warningLog('If you see this error, it means that the map generator does'..
|
||||
'not handle all cases of generation on coordinates: %s, %s, %s.', x, y, z)
|
||||
end
|
||||
|
||||
-- Ore generation
|
||||
-- Biome ores are generated first.
|
||||
-- If no ores are generated, the ores registered in the layer are generated.
|
||||
-- This means that biome ores have priority over layer ores.
|
||||
if not biome:generateOre(factors, data, data2, index, x, y, z) then
|
||||
layer:generateOre(factors, data, data2, index, x, y, z)
|
||||
end
|
||||
end
|
||||
|
||||
---Calculates a weight for a coordinate using the projection onto
|
||||
@@ -870,9 +902,9 @@ end
|
||||
---Generator handler for specific coordinates.
|
||||
---@param mapGenerator MapGen
|
||||
---@param layer MapGen.Layer?
|
||||
---@param data number[]
|
||||
---@param data2 number[]
|
||||
---@param index number
|
||||
---@param data integer[]
|
||||
---@param data2 integer[]
|
||||
---@param index integer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
@@ -907,7 +939,7 @@ local function generatorHandler(mapGenerator, layer, data, data2, index, x, y, z
|
||||
temp = layer:calcTemp(temp, y)
|
||||
humidity = layer:calcHumidity(humidity, y)
|
||||
|
||||
---@type MapGen.factors
|
||||
---@type MapGen.Factors
|
||||
local factors = {
|
||||
height = height,
|
||||
temp = temp,
|
||||
@@ -944,6 +976,10 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||
self:initCavernsNoise()
|
||||
end
|
||||
|
||||
if not self.isOresNoiseInitialized then
|
||||
self:initOresNoise()
|
||||
end
|
||||
|
||||
local data, data2 = voxelManip:get_data(), voxelManip:get_param2_data()
|
||||
-- local param2_data = voxelManip:get_param2_data()
|
||||
local area = VoxelArea:new({MinEdge = eMin, MaxEdge = eMax})
|
||||
|
||||
Reference in New Issue
Block a user