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)
|
return region:pointInRegion(point)
|
||||||
end
|
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 ---
|
-- --- REGISTRATION METHODS ---
|
||||||
@@ -394,24 +419,34 @@ end
|
|||||||
---@param layerName string The name of the layer in which the new cavern will be included.
|
---@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 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`
|
---@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]
|
local layer = self.layersByName[layerName]
|
||||||
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
|
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(layerName))
|
||||||
---TODO: перенести проверку на уникальность в конструктор
|
---TODO: перенести проверку на уникальность в конструктор
|
||||||
assert(layer.cavernsByName[cavernName] == nil, ('A cavern named `%s` already exists in the `%s` layer.'):format(cavernName, layerName))
|
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)
|
local cavern = Cavern:new(cavernName, def)
|
||||||
|
|
||||||
layer:addCavern(cavernName, cavern)
|
layer:addCavern(cavernName, cavern)
|
||||||
end
|
end
|
||||||
|
|
||||||
---Designate the world region between the vertices as `MapGen.Region` and give it a name.
|
---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 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 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`
|
---@param def MapGen.RegionDef Region description table, see details `MapGen.RegionDef`
|
||||||
function MapGen:registerRegion(layerName, regionName, def)
|
function MapGen:registerRegion(layerName, regionName, def)
|
||||||
local layer = self.layersByName[layerName]
|
local layer = self.layersByName[layerName]
|
||||||
assert(layer ~= nil, ('There is no layer named `%s` registered.'):format(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))
|
assert(layer.regionsByName[regionName] == nil, ('A region named `%s` already exists in the `%s` layer.'):format(regionName, layerName))
|
||||||
|
|
||||||
local region = Region:new(regionName, def)
|
local region = Region:new(regionName, def)
|
||||||
@@ -469,22 +504,6 @@ end
|
|||||||
|
|
||||||
-- --- GENERATION METHODS ---
|
-- --- 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)
|
---The core logic of world generation. Determines which node type (water, rock, soil, air)
|
||||||
---and which biome will be generated at the specified coordinates.
|
---and which biome will be generated at the specified coordinates.
|
||||||
---@param mapGenerator MapGen
|
---@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...
|
-- If the block is below the surface height...
|
||||||
elseif y <= height then
|
elseif y <= height then
|
||||||
-- ... and a cavern is generated in the block...
|
-- ... 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.
|
-- ... then airspace is generated.
|
||||||
biome:generateCavernAir(mapGenerator, data, index, x, y, z)
|
biome:generateCavernAir(mapGenerator, data, index, x, y, z)
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
local mathMin, mathMax, mathAbs
|
local mathMin, mathMax, mathAbs, mathClamp
|
||||||
= math.min, math.max, math.abs
|
= math.min, math.max, math.abs, math.clamp
|
||||||
|
|
||||||
local getInstance = Class.getInstance
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
---@class MapGen.Cavern
|
---@class MapGen.Cavern
|
||||||
---@field name string
|
---@field name string
|
||||||
|
---@field isRegional boolean
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number
|
||||||
@@ -18,6 +19,7 @@ local Cavern = {}
|
|||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class MapGen.CavernDef
|
---@class MapGen.CavernDef
|
||||||
|
---@field isRegional boolean?
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field noiseParams NoiseParams
|
---@field noiseParams NoiseParams
|
||||||
@@ -58,6 +60,7 @@ function Cavern:new(name, def)
|
|||||||
maxY = def.maxY,
|
maxY = def.maxY,
|
||||||
smoothDistance = def.smoothDistance,
|
smoothDistance = def.smoothDistance,
|
||||||
threshold = def.threshold,
|
threshold = def.threshold,
|
||||||
|
isRegional = def.isRegional or false,
|
||||||
})
|
})
|
||||||
|
|
||||||
function instance:getNoise()
|
function instance:getNoise()
|
||||||
@@ -79,16 +82,31 @@ function Cavern:new(name, def)
|
|||||||
return instance
|
return instance
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param y number
|
---@param distance number
|
||||||
---@param minY number
|
|
||||||
---@param maxY number
|
|
||||||
---@param smoothDistance number
|
|
||||||
---@return number
|
---@return number
|
||||||
local function calcCavernWeight(y, smoothDistance, minY, maxY)
|
function Cavern:calcCavernWeight(distance)
|
||||||
local maxDist = maxY - y
|
return mathClamp(distance / self.smoothDistance, 0, 1)
|
||||||
local minDist = y - minY
|
end
|
||||||
|
|
||||||
return mathMax(0, mathMin(maxDist / smoothDistance, minDist / smoothDistance, 1))
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
|
---@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
|
end
|
||||||
|
|
||||||
---Returns `true` if there is a cavern at the specified coordinates.
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
@@ -96,15 +114,17 @@ end
|
|||||||
---@param y integer
|
---@param y integer
|
||||||
---@param z integer
|
---@param z integer
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function Cavern:isCavern(x, y, z)
|
function Cavern:isLayeredCavern(x, y, z)
|
||||||
local minY, maxY = self.minY, self.maxY
|
local minY, maxY = self.minY, self.maxY
|
||||||
|
|
||||||
if y > maxY or y < minY then
|
if y > maxY or y < minY then
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local distance = mathMin(maxY - y, y - minY)
|
||||||
|
|
||||||
local noise = self:getNoise()
|
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
|
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -169,18 +169,18 @@ for _, c in ipairs({'landscape', 'temp', 'humidity'}) do
|
|||||||
})--]]
|
})--]]
|
||||||
end
|
end
|
||||||
|
|
||||||
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 32,{x=0, y=0}, 'landscape')
|
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 100,{x=3, y=3}, 'landscape')
|
||||||
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 150,{x=0, y=0}, 'temp')
|
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 150,{x=3, y=3}, 'temp')
|
||||||
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 200,{x=0, y=0}, 'humidity')
|
mapGenerator:register2DPeaksFromFile('midgard', 'map.ff', 200,{x=3, y=3}, 'humidity')
|
||||||
|
|
||||||
--[[
|
-- [[
|
||||||
mapGenerator:registerCavern('midgard', 'cavern1', {
|
mapGenerator:registerCavern('midgard', 'cavern1', {
|
||||||
minY = -48,
|
minY = -48,
|
||||||
maxY = 50,
|
maxY = 100,
|
||||||
smoothDistance = 20,
|
smoothDistance = 20,
|
||||||
threshold = 0.2,
|
threshold = 0.1,
|
||||||
noiseParams ={
|
noiseParams ={
|
||||||
offset = 0,
|
offset = 0.2,
|
||||||
scale = 1,
|
scale = 1,
|
||||||
spread = {x = 10, y = 250, z = 30},
|
spread = {x = 10, y = 250, z = 30},
|
||||||
seed = 5934,
|
seed = 5934,
|
||||||
@@ -188,6 +188,12 @@ mapGenerator:registerCavern('midgard', 'cavern1', {
|
|||||||
persistence = 0.4,
|
persistence = 0.4,
|
||||||
lacunarity = 2.0
|
lacunarity = 2.0
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
{x=100, y=0, z=100},
|
||||||
|
{x=100, y=0, z=200},
|
||||||
|
{x=200, y=0, z=200},
|
||||||
|
{x=200, y=0, z=100},
|
||||||
})--]]
|
})--]]
|
||||||
|
|
||||||
-- --- Biomes ---
|
-- --- Biomes ---
|
||||||
|
|||||||
Reference in New Issue
Block a user