559 lines
16 KiB
Lua
559 lines
16 KiB
Lua
local assert, dump, type = assert, dump, type
|
|
local pairs, ipairs = pairs, ipairs
|
|
|
|
local mathHuge, mathClamp = math.huge, math.clamp
|
|
local tableInsert, tableSort = table.insert, table.sort
|
|
|
|
local id = core.get_content_id
|
|
local corePosToString, coreGetValueNoise = core.pos_to_string, core.get_value_noise
|
|
|
|
local getInstance = Class.getInstance
|
|
|
|
local require = Mod:getInfo('smc__core__map_gen').require
|
|
|
|
---@type MapGen.Peak
|
|
local Peak = require('MapGen.Peak')
|
|
---@type MapGen.Triangle
|
|
local Triangle = require('MapGen.Triangle')
|
|
|
|
---@class MapGen.Layer
|
|
---@field name string
|
|
---@field minY integer
|
|
---@field maxY integer
|
|
---@field minTemp integer
|
|
---@field maxTemp integer
|
|
---@field minHumidity integer
|
|
---@field maxHumidity integer
|
|
---@field waterLevel integer
|
|
---
|
|
---@field calcTemp fun(self:MapGen.Layer, value:number, height:integer):number
|
|
---@field calcHumidity fun(self:MapGen.Layer, value:number, height:integer):number
|
|
---
|
|
---@field peaksList MapGen.Peak[]
|
|
---@field peaksByPos table<string, MapGen.Peak>
|
|
---
|
|
---@field noisesParamsByColor table<MapGen.Layer.Category, table<ColorString, NoiseParams>>
|
|
---@field noisesByColor table<MapGen.Layer.Category, table<ColorString, ValueNoise>>
|
|
---
|
|
---@field biomesGorizontalScattering integer
|
|
---@field biomesVerticalScattering integer
|
|
---@field biomesByName table<string, MapGen.Biome>
|
|
---@field biomesList MapGen.Biome[]
|
|
---@field biomesDiagram table<integer, table<integer, table<integer, {biome:MapGen.Biome, weight:number}>>>
|
|
---
|
|
---@field cavernsByName table<string, MapGen.Cavern>
|
|
---@field cavernsList MapGen.Cavern[]
|
|
---
|
|
---@field regionsByName table<string, MapGen.Region>
|
|
---
|
|
---@field oresByName table<string, MapGen.Ore>
|
|
---@field oresByWhereinName table<string, MapGen.Ore[]>
|
|
---@field oresByWhereinID table<integer, MapGen.Ore[]>
|
|
---
|
|
---@field trianglesList MapGen.Triangle[]
|
|
local Layer = {
|
|
peaksList = {},
|
|
peaksByPos = {},
|
|
|
|
noisesByColor = {
|
|
landscape = {},
|
|
temp = {},
|
|
humidity = {},
|
|
},
|
|
noisesParamsByColor = {
|
|
landscape = {},
|
|
temp = {},
|
|
humidity = {},
|
|
},
|
|
|
|
biomesByName = {},
|
|
biomesList = {},
|
|
biomesDiagram = {},
|
|
|
|
cavernsByName = {},
|
|
cavernsList = {},
|
|
|
|
regionsByName = {},
|
|
|
|
oresByName = {},
|
|
oresByWhereinName = {},
|
|
oresByWhereinID = {},
|
|
|
|
trianglesList = {},
|
|
}
|
|
|
|
---Definition table for the `MapGen.Layer`.
|
|
---
|
|
---**Only for EmmyLua.**
|
|
---@class MapGen.LayerDef
|
|
---@field minY integer
|
|
---@field maxY integer
|
|
---@field minTemp integer
|
|
---@field maxTemp integer
|
|
---@field minHumidity integer
|
|
---@field maxHumidity integer
|
|
---@field waterLevel? integer
|
|
---@field calcTemp (fun(self:MapGen.Layer, value:number, height:integer):number)?
|
|
---@field calcHumidity (fun(self:MapGen.Layer, value:number, height:integer):number)?
|
|
---@field biomesGorizontalScattering integer?
|
|
---@field biomesVerticalScattering integer?
|
|
---@field triangulationLandscapeFile string
|
|
---@field triangulationTempFile string
|
|
---@field triangulationHumidityFile string
|
|
---@field landscapeMapScale integer
|
|
---@field tempMapScale integer
|
|
---@field humidityMapScale integer
|
|
|
|
---@param self MapGen.Layer
|
|
---@param value number
|
|
---@param height integer
|
|
---@return number
|
|
local function defaultCalcFunc(self, value, height)
|
|
return value
|
|
end
|
|
|
|
---@param name string
|
|
---@param def MapGen.LayerDef
|
|
---@return MapGen.Layer
|
|
function Layer:new(name, def)
|
|
if def.calcTemp == nil then
|
|
Logger.infoLog('MapGen.Layer: The `%s` layer does not have a specified `calcTemp()` function. The default function is used.', name)
|
|
def.calcTemp = defaultCalcFunc
|
|
end
|
|
|
|
if def.calcHumidity == nil then
|
|
Logger.infoLog('MapGen.Layer: The `%s` layer does not have a specified `calcHumidity()` function. The default function is used.', name)
|
|
def.calcHumidity = defaultCalcFunc
|
|
end
|
|
|
|
---@type MapGen.Layer
|
|
local instance = getInstance(self, {})
|
|
instance.name = name
|
|
instance.minY = def.minY
|
|
instance.maxY = def.maxY
|
|
instance.minTemp = def.minTemp
|
|
instance.maxTemp = def.maxTemp
|
|
instance.minHumidity = def.minHumidity
|
|
instance.maxHumidity = def.maxHumidity
|
|
instance.waterLevel = def.waterLevel or 0
|
|
instance.calcTemp = def.calcTemp
|
|
instance.calcHumidity = def.calcHumidity
|
|
instance.biomesGorizontalScattering = def.biomesGorizontalScattering or 0
|
|
instance.biomesVerticalScattering = def.biomesVerticalScattering or 0
|
|
|
|
instance:getTriaglesFromFile(
|
|
def.triangulationLandscapeFile,
|
|
'landscape',
|
|
def.landscapeMapScale)
|
|
|
|
instance:getTriaglesFromFile(
|
|
def.triangulationTempFile,
|
|
'temp',
|
|
def.tempMapScale)
|
|
|
|
instance:getTriaglesFromFile(
|
|
def.triangulationHumidityFile,
|
|
'humidity',
|
|
def.humidityMapScale)
|
|
|
|
instance:initTrianglesNeighbours()
|
|
|
|
return instance
|
|
end
|
|
|
|
|
|
---Checks `def` for compliance with `MapGen.PeakDef` and creates a new peak
|
|
---if a peak with such coordinates has not yet been registered or returns an existing one.
|
|
---
|
|
---**Note:** If the colors of the new peak and the existing one at the same coordinates
|
|
---are different, an error will be called.
|
|
---@param def MapGen.PeakDef
|
|
---@param scale integer
|
|
---@return MapGen.Peak
|
|
function Layer:registerPeak(def, scale)
|
|
assert(def.pos ~= nil)
|
|
assert(def.pos.x ~= nil)
|
|
assert(def.pos.z ~= nil)
|
|
assert(def.color ~= nil)
|
|
assert(type(def.color) == 'string')
|
|
|
|
def.pos.x = def.pos.x * scale
|
|
def.pos.z = def.pos.z * scale
|
|
def.pos.y = 0
|
|
|
|
local positionStr = corePosToString(def.pos)
|
|
|
|
local peak = self.peaksByPos[positionStr]
|
|
|
|
if peak == nil then
|
|
peak = Peak:new(def)
|
|
self.peaksByPos[positionStr] = peak
|
|
elseif peak.color ~= def.color then
|
|
error('There cannot be two peaks with the same coordinates but different colors.')
|
|
end
|
|
|
|
return peak
|
|
end
|
|
|
|
function Layer:initTrianglesNeighbours()
|
|
local triangles = self.trianglesList
|
|
|
|
for _, triangle in pairs(triangles) do
|
|
for _, id in ipairs(triangle.neighboursIDs) do
|
|
tableInsert(triangle.neighbours, triangles[id])
|
|
end
|
|
end
|
|
end
|
|
|
|
---@param fileName string
|
|
---@param category MapGen.Layer.Category
|
|
---@param scale integer
|
|
function Layer:getTriaglesFromFile(fileName, category, scale)
|
|
local file = io.open(core.get_modpath(core.get_current_modname())..'/maps/'..fileName, 'r')
|
|
assert(file ~= nil)
|
|
|
|
local json = core.parse_json(file:read('*a')) or {}
|
|
file:close()
|
|
|
|
local triangles = json.triangles
|
|
assert(triangles ~= nil)
|
|
|
|
for _, tr in ipairs(triangles) do
|
|
local p1 = self:registerPeak( tr.vertices[1], scale)
|
|
local p2 = self:registerPeak( tr.vertices[2], scale)
|
|
local p3 = self:registerPeak( tr.vertices[3], scale)
|
|
|
|
local triangle = Triangle:new(tr.id,p1, p2, p3, tr.adjacent_triangle_ids)
|
|
|
|
self:addTriangle(category, triangle)
|
|
end
|
|
end
|
|
|
|
---A category of noise that affects specific map generation objects.
|
|
---@alias MapGen.Layer.Category 'landscape'|'temp'|'humidity'
|
|
|
|
---Adds a triangle to the `Layer.trianglesList`
|
|
---@param category MapGen.Layer.Category
|
|
---@param triangle MapGen.Triangle
|
|
function Layer:addTriangle(category, triangle)
|
|
local triangles = self.trianglesList
|
|
if triangles ~= nil then
|
|
triangles[triangle.id] = triangle
|
|
else
|
|
error(('There is no peak category named %s.'):format(category))
|
|
end
|
|
end
|
|
|
|
---Adds a peak to the `Layer.peaksList`
|
|
---@param category MapGen.Layer.Category
|
|
---@param peak MapGen.Peak
|
|
function Layer:addPeak(category, peak)
|
|
tableInsert(self.peaksList, peak)
|
|
end
|
|
|
|
---Returns the `MapGen.Peak` at the coordinates. If there is no peak at these coordinates, it returns `nil`.
|
|
---@param pos Position Coordinate of the desired peak.
|
|
---@return MapGen.Peak?
|
|
function Layer:getPeakByPos(pos)
|
|
local positionStr = corePosToString(pos)
|
|
|
|
local peak = self.peaksByPos[positionStr]
|
|
|
|
return peak
|
|
end
|
|
|
|
---Adds a NoiseParams color to the `Layer.noisesParamsByColor`
|
|
---@param category MapGen.Layer.Category
|
|
---@param color ColorString
|
|
---@param noiseParams NoiseParams
|
|
function Layer:addNoiseParamsColor(category, color, noiseParams)
|
|
color = Smc.fixColorString(color)
|
|
|
|
local noisesParams = self.noisesParamsByColor
|
|
|
|
if category == 'landscape' then
|
|
assert(noisesParams.landscape[color] == nil, ('The color `%s` already exists.'):format(color))
|
|
noisesParams.landscape[color] = noiseParams
|
|
elseif category == 'temp' then
|
|
assert(noisesParams.temp[color] == nil, ('The color `%s` already exists.'):format(color))
|
|
noisesParams.temp[color] = noiseParams
|
|
elseif category == 'humidity' then
|
|
assert(noisesParams.humidity[color] == nil, ('The color `%s` already exists.'):format(color))
|
|
noisesParams.humidity[color] = noiseParams
|
|
else
|
|
error(('There is no color category named `%s`.'):format(category))
|
|
end
|
|
end
|
|
|
|
---Returns noise corresponding to a color.
|
|
---@param category MapGen.Layer.Category
|
|
---@param color ColorString
|
|
---@return ValueNoise
|
|
function Layer:getNoiseByColor(category, color)
|
|
color = Smc.fixColorString(color)
|
|
|
|
local noises = self.noisesByColor
|
|
|
|
if category == 'landscape' then
|
|
assert(noises.landscape[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
|
return noises.landscape[color]
|
|
elseif category == 'temp' then
|
|
assert(noises.temp[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
|
return noises.temp[color]
|
|
elseif category == 'humidity' then
|
|
assert(noises.humidity[color] ~= nil, ('`%s` color does not exist.'):format(color))
|
|
return noises.humidity[color]
|
|
else
|
|
error(('There is no color category named `%s`.'):format(category))
|
|
end
|
|
end
|
|
|
|
---Adds a biome to the `Layer.biomesByName` & `Layer.biomesList`
|
|
---@param biomeName string
|
|
---@param biome MapGen.Biome
|
|
function Layer:addBiome(biomeName, biome)
|
|
assert(self.biomesByName[biomeName] == nil,
|
|
('A biome named `%s` already exists in the `%s` layer.'):format(
|
|
biomeName, self.name))
|
|
|
|
self.biomesByName[biomeName] = biome
|
|
tableInsert(self.biomesList, biome)
|
|
end
|
|
|
|
---Returns a biome by name, if there is no such biome, it will raise an error.
|
|
---@param biomeName string
|
|
---@return MapGen.Biome
|
|
function Layer:getBiomeByName(biomeName)
|
|
local biome = self.biomesByName[biomeName]
|
|
assert(biome ~= nil, ('Biome named `%s` does not exist in the `%s` layer.'):format(
|
|
biomeName, self.name))
|
|
|
|
return biome
|
|
end
|
|
|
|
---Adds a cavern to the `Layer.cavernsByName` & `Layer.cavernsList`
|
|
---@param cavernName string
|
|
---@param cavern MapGen.Cavern
|
|
function Layer:addCavern(cavernName, cavern)
|
|
assert(self.cavernsByName[cavernName] == nil,
|
|
('A cavern named `%s` already exists in the `%s` layer.'):format(
|
|
cavernName, self.name))
|
|
|
|
self.cavernsByName[cavernName] = cavern
|
|
tableInsert(self.cavernsList, cavern)
|
|
end
|
|
|
|
---Returns a cavern by name, if there is no such cavern, it will raise an error.
|
|
---@param cavernName string
|
|
---@return MapGen.Cavern
|
|
function Layer:getCavernByName(cavernName)
|
|
local cavern = self.cavernsByName[cavernName]
|
|
assert(cavern ~= nil, ('Cavern named `%s` does not exist in the `%s` layer.'):format(
|
|
cavernName, self.name))
|
|
|
|
return cavern
|
|
end
|
|
|
|
function Layer:addRegion(regionName, region)
|
|
assert(self.regionsByName[regionName] == nil,
|
|
('A region named `%s` already exists in the `%s` layer.'):format(
|
|
regionName, self.name))
|
|
|
|
self.regionsByName[regionName] = region
|
|
--table.insert(self.regionsList, region)
|
|
end
|
|
|
|
---Returns a region by name, if there is no such region, it will raise an error.
|
|
---@param regionName string
|
|
---@return MapGen.Region
|
|
function Layer:getRegionByName(regionName)
|
|
local region = self.regionsByName[regionName]
|
|
assert(region ~= nil, ('Region named `%s` does not exist in the `%s` layer.'):format(
|
|
regionName, self.name))
|
|
|
|
return region
|
|
end
|
|
|
|
---@param oreName string
|
|
---@param ore MapGen.Ore
|
|
function Layer:addOre(oreName, ore)
|
|
assert(self.oresByName[oreName] == nil, ('A ore named `%s` is already registered.'):format(oreName))
|
|
|
|
self.oresByName[oreName] = ore
|
|
|
|
for _, nodeName in ipairs(ore.wherein) do
|
|
if self.oresByWhereinName[nodeName] == nil then
|
|
self.oresByWhereinName[nodeName] = { ore }
|
|
else
|
|
tableInsert(self.oresByWhereinName[nodeName], ore)
|
|
end
|
|
|
|
local nodeID = id(nodeName)
|
|
if self.oresByWhereinID[nodeID] == nil then
|
|
self.oresByWhereinID[nodeID] = { ore }
|
|
else
|
|
tableInsert(self.oresByWhereinID[nodeID], ore)
|
|
end
|
|
end
|
|
|
|
if ore.biomes ~= nil then
|
|
for _, biomeName in ipairs(ore.biomes) do
|
|
local biome = self:getBiomeByName(biomeName)
|
|
|
|
biome:addOre(oreName, ore)
|
|
end
|
|
end
|
|
end
|
|
|
|
---@param oreName string
|
|
---@return MapGen.Ore
|
|
function Layer:getOreByName(oreName)
|
|
local ore = self.oresByName[oreName]
|
|
assert(ore ~= nil, ('Ore named `%s` does not exist in the `%s` layer.'):format(
|
|
oreName, self.name))
|
|
|
|
return ore
|
|
end
|
|
|
|
---@param nodeName string
|
|
---@return MapGen.Ore[]?
|
|
function Layer:getOresByWhereinName(nodeName)
|
|
local ores = self.oresByWhereinName[nodeName]
|
|
|
|
return ores
|
|
end
|
|
|
|
---@param nodeID integer
|
|
---@return MapGen.Ore[]?
|
|
function Layer:getOresByWhereinID(nodeID)
|
|
local ores = self.oresByWhereinID[nodeID]
|
|
|
|
return ores
|
|
end
|
|
|
|
---@param factors MapGen.Factors
|
|
---@param data integer[]
|
|
---@param data2 integer[]
|
|
---@param index integer
|
|
---@param x integer
|
|
---@param y integer
|
|
---@param z integer
|
|
---@return boolean
|
|
function Layer:generateOre(factors, data, data2, index, x, y, z)
|
|
local ores = self:getOresByWhereinID(data[index])
|
|
if ores == nil then
|
|
return false
|
|
end
|
|
|
|
for _, ore in ipairs(ores) do
|
|
if ore:generate(factors, data, data2, index, x, y, z) then
|
|
return true
|
|
end
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
---@param height number
|
|
---@param biomesList MapGen.Biome[]
|
|
---@return string[], MapGen.Biome[]
|
|
local function getBiomesNamesByHeight(height, biomesList)
|
|
local biomesNames = {}
|
|
local biomes = {}
|
|
|
|
for _, biome in ipairs(biomesList) do
|
|
if height >= biome.minY and height <= biome.maxY then
|
|
tableInsert(biomesNames, biome.name)
|
|
tableInsert(biomes, biome)
|
|
end
|
|
end
|
|
|
|
tableSort(biomesNames)
|
|
|
|
return biomesNames, biomes
|
|
end
|
|
|
|
---Initializes noise for generation.
|
|
---
|
|
---Should only be called after the mapgen object has been loaded.
|
|
function Layer:initNoises()
|
|
local noises = self.noisesByColor
|
|
|
|
for category, noisesParams in pairs(self.noisesParamsByColor) do
|
|
for color, noiseParams in pairs(noisesParams) do
|
|
noises[category][color] = coreGetValueNoise(noiseParams)
|
|
end
|
|
end
|
|
end
|
|
|
|
---Initialization of the biome diagram using the Voronoi method.
|
|
function Layer:initBiomesDiagram()
|
|
local diagram = self.biomesDiagram
|
|
local pastBiomesNames
|
|
local pastDiagramSlice
|
|
|
|
-- A counter for logging unique slices in the biome diagram.
|
|
local uniqueDiagramSlices = 0
|
|
|
|
for height = self.minY, self.maxY do
|
|
diagram[height] = {}
|
|
|
|
local newBiomesNames, biomes = getBiomesNamesByHeight(height, self.biomesList)
|
|
|
|
if pastBiomesNames ~= nil and dump(pastBiomesNames) == dump(newBiomesNames) then
|
|
diagram[height] = pastDiagramSlice
|
|
|
|
goto continue
|
|
end
|
|
|
|
pastBiomesNames = newBiomesNames
|
|
for temp = self.minTemp, self.maxTemp do
|
|
diagram[height][temp] = {}
|
|
|
|
for humidity = self.minHumidity, self.maxHumidity do
|
|
local minDistance = mathHuge
|
|
local pastClosestBiome = nil
|
|
local closestBiome = nil
|
|
|
|
---@param biome MapGen.Biome
|
|
for _, biome in ipairs(biomes) do
|
|
local distance = (temp - biome.tempPoint)^2 + (humidity - biome.humidityPoint)^2
|
|
|
|
if distance < minDistance then
|
|
minDistance = distance
|
|
pastClosestBiome = closestBiome
|
|
closestBiome = biome
|
|
end
|
|
end
|
|
|
|
if closestBiome == nil then
|
|
closestBiome = self.biomesList[1]
|
|
end
|
|
|
|
local weight
|
|
if pastClosestBiome ~= nil then
|
|
---Distances from the center of the nearest biome to the current point.
|
|
local distance1 = (temp - closestBiome.tempPoint)^2 + (humidity - closestBiome.humidityPoint)^2
|
|
---Half the distance between the centers of two nearest biomes (i.e. the distance to the biome boundary)
|
|
local distance2 = (pastClosestBiome.tempPoint - closestBiome.tempPoint)^2 + (pastClosestBiome.humidityPoint - closestBiome.humidityPoint) / 2
|
|
|
|
weight = 1 - mathClamp(distance1 / distance2, 0, 1)
|
|
else
|
|
weight = 1
|
|
end
|
|
|
|
diagram[height][temp][humidity] = { biome = closestBiome, weight = weight}
|
|
end
|
|
end
|
|
|
|
pastDiagramSlice = diagram[height]
|
|
uniqueDiagramSlices = uniqueDiagramSlices + 1
|
|
|
|
::continue::
|
|
end
|
|
|
|
Logger.infoLog('MapGen.Layer.initBiomesDiagram(): Initialized to %s unique slices for the layer `%s` biome diagram.', uniqueDiagramSlices, self.name)
|
|
end
|
|
|
|
return Layer
|