MapGen: Added the ability to limit cave generation regionally. close #15
This commit is contained in:
@@ -206,6 +206,31 @@ function MapGen:pointInRegion2D(layer, regionName, point)
|
||||
return region:pointInRegion(point)
|
||||
end
|
||||
|
||||
---Returns `true` if there is a cavern at the specified coordinates.
|
||||
---@param layer MapGen.Layer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return boolean
|
||||
function MapGen:isCavern(layer, x, y, z)
|
||||
for _, cavern in ipairs(layer.cavernsList) do
|
||||
if cavern.isRegional then
|
||||
local inRegion, distance = self:pointInRegion2D(layer, cavern.name, {x=x,y=y,z=z})
|
||||
|
||||
if inRegion and distance ~= nil then
|
||||
if cavern:isCavern(x, y, z, distance) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
else
|
||||
if cavern:isLayeredCavern(x, y, z) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
-- --- REGISTRATION METHODS ---
|
||||
@@ -394,24 +419,34 @@ end
|
||||
---@param layerName string The name of the layer in which the new cavern will be included.
|
||||
---@param cavernName string The name to be assigned to the cavern. The name must be unique for each cavern.
|
||||
---@param def MapGen.CavernDef Cavern description table, see details `MapGen.CavernDef`
|
||||
function MapGen:registerCavern(layerName, cavernName, def)
|
||||
---@param regionVertices Position[]? The region to which the cave generation will be limited.
|
||||
function MapGen:registerCavern(layerName, cavernName, def, regionVertices)
|
||||
local layer = self.layersByName[layerName]
|
||||
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
|
||||
---TODO: перенести проверку на уникальность в конструктор
|
||||
assert(layer.cavernsByName[cavernName] == nil, ('A cavern named `%s` already exists in the `%s` layer.'):format(cavernName, layerName))
|
||||
|
||||
if regionVertices ~= nil then
|
||||
self:registerRegion(layerName, cavernName, {
|
||||
vertices = regionVertices
|
||||
})
|
||||
|
||||
def.isRegional = true
|
||||
end
|
||||
|
||||
local cavern = Cavern:new(cavernName, def)
|
||||
|
||||
layer:addCavern(cavernName, cavern)
|
||||
end
|
||||
|
||||
---Designate the world region between the vertices as `MapGen.Region` and give it a name.
|
||||
---@param layerName string The name of the layer in which the new region will be included.
|
||||
---@param regionName string The name to be assigned to the region. The name must be unique for each region.
|
||||
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
|
||||
---Mark the world region between the vertices as `MapGen.Region` and give it a name.
|
||||
---@param layerName string The name of the layer in which the new region will be included.
|
||||
---@param regionName string The name to be assigned to the region. The name must be unique for each region.
|
||||
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
|
||||
function MapGen:registerRegion(layerName, regionName, def)
|
||||
local layer = self.layersByName[layerName]
|
||||
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
|
||||
---TODO: перенести проверку на уникальность в конструктор
|
||||
assert(layer.regionsByName[regionName] == nil, ('A region named `%s` already exists in the `%s` layer.'):format(regionName, layerName))
|
||||
|
||||
local region = Region:new(regionName, def)
|
||||
@@ -469,22 +504,6 @@ end
|
||||
|
||||
-- --- GENERATION METHODS ---
|
||||
|
||||
---Returns `true` if there is a cavern at the specified coordinates.
|
||||
---@param layer MapGen.Layer
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return boolean
|
||||
local function isCavern(layer, x, y, z)
|
||||
for _, cavern in ipairs(layer.cavernsList) do
|
||||
if cavern:isCavern(x, y, z) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
---The core logic of world generation. Determines which node type (water, rock, soil, air)
|
||||
---and which biome will be generated at the specified coordinates.
|
||||
---@param mapGenerator MapGen
|
||||
@@ -541,7 +560,7 @@ local function generateNode(mapGenerator, layer, height, temp, humidity, data, i
|
||||
-- If the block is below the surface height...
|
||||
elseif y <= height then
|
||||
-- ... and a cavern is generated in the block...
|
||||
if isCavern(layer, x, y, z) then
|
||||
if mapGenerator:isCavern(layer, x, y, z) then
|
||||
-- ... then airspace is generated.
|
||||
biome:generateCavernAir(mapGenerator, data, index, x, y, z)
|
||||
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
local mathMin, mathMax, mathAbs
|
||||
= math.min, math.max, math.abs
|
||||
local mathMin, mathMax, mathAbs, mathClamp
|
||||
= math.min, math.max, math.abs, math.clamp
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
---@class MapGen.Cavern
|
||||
---@field name string
|
||||
---@field isRegional boolean
|
||||
---@field minY number
|
||||
---@field maxY number
|
||||
---@field smoothDistance number
|
||||
@@ -18,6 +19,7 @@ local Cavern = {}
|
||||
---
|
||||
---**Only for EmmyLua.**
|
||||
---@class MapGen.CavernDef
|
||||
---@field isRegional boolean?
|
||||
---@field minY number
|
||||
---@field maxY number
|
||||
---@field noiseParams NoiseParams
|
||||
@@ -58,6 +60,7 @@ function Cavern:new(name, def)
|
||||
maxY = def.maxY,
|
||||
smoothDistance = def.smoothDistance,
|
||||
threshold = def.threshold,
|
||||
isRegional = def.isRegional or false,
|
||||
})
|
||||
|
||||
function instance:getNoise()
|
||||
@@ -79,32 +82,49 @@ function Cavern:new(name, def)
|
||||
return instance
|
||||
end
|
||||
|
||||
---@param y number
|
||||
---@param minY number
|
||||
---@param maxY number
|
||||
---@param smoothDistance number
|
||||
---@return number
|
||||
local function calcCavernWeight(y, smoothDistance, minY, maxY)
|
||||
local maxDist = maxY - y
|
||||
local minDist = y - minY
|
||||
|
||||
return mathMax(0, mathMin(maxDist / smoothDistance, minDist / smoothDistance, 1))
|
||||
---@param distance number
|
||||
---@return number
|
||||
function Cavern:calcCavernWeight(distance)
|
||||
return mathClamp(distance / self.smoothDistance, 0, 1)
|
||||
end
|
||||
|
||||
---Returns `true` if there is a cavern at the specified coordinates.
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return boolean
|
||||
function Cavern:isCavern(x, y, z)
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@param distance number
|
||||
---@return boolean
|
||||
function Cavern:isCavern(x, y, z, distance)
|
||||
local minY, maxY = self.minY, self.maxY
|
||||
|
||||
if y >= maxY or y <= minY then
|
||||
return false
|
||||
end
|
||||
|
||||
distance = mathMin(maxY - y, y - minY, distance)
|
||||
|
||||
local noise = self:getNoise()
|
||||
local weight = self:calcCavernWeight(distance)
|
||||
|
||||
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||
end
|
||||
|
||||
---Returns `true` if there is a cavern at the specified coordinates.
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@return boolean
|
||||
function Cavern:isLayeredCavern(x, y, z)
|
||||
local minY, maxY = self.minY, self.maxY
|
||||
|
||||
if y > maxY or y < minY then
|
||||
return false
|
||||
end
|
||||
|
||||
local distance = mathMin(maxY - y, y - minY)
|
||||
|
||||
local noise = self:getNoise()
|
||||
local weight = calcCavernWeight(y, self.smoothDistance, minY, maxY)
|
||||
local weight = self:calcCavernWeight(distance)
|
||||
|
||||
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user