MapGen: add worm cavern type
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
local assert = assert
|
||||
local pairs = pairs
|
||||
|
||||
local mathMin, mathAbs, mathClamp = math.min, math.abs, math.clamp
|
||||
local mathFloor, mathMin, mathAbs, mathClamp, mathRound = math.floor, math.min, math.abs, math.clamp, math.round
|
||||
|
||||
local coreGetValueNoise = core.get_value_noise
|
||||
local coreGetValueNoise, corePosToString = core.get_value_noise, core.pos_to_string
|
||||
|
||||
local getInstance = Class.getInstance
|
||||
|
||||
---@alias MapGen.Cavern.Type 'cheesed'|'warped'
|
||||
---@alias MapGen.Cavern.Type 'cheesy'|'warped'|'worm'
|
||||
|
||||
---@alias MapGen.Cavern.NoiseParams {baseNoise:NoiseParams, xNoise?:NoiseParams, yNoise?:NoiseParams, zNoise?:NoiseParams}
|
||||
---@alias MapGen.Cavern.ValueNoise {baseNoise:ValueNoise, xNoise?:ValueNoise, yNoise?:ValueNoise, zNoise?:ValueNoise}
|
||||
@@ -18,14 +18,29 @@ local getInstance = Class.getInstance
|
||||
---@field type MapGen.Cavern.Type
|
||||
---@field minY integer
|
||||
---@field maxY integer
|
||||
---@field smoothDistance number
|
||||
---@field threshold number
|
||||
---@field smoothDistance number?
|
||||
---@field threshold number?
|
||||
---
|
||||
---@field warpStrength number
|
||||
---@field warpStrength number?
|
||||
---
|
||||
---@field regionSize integer?
|
||||
---@field wormStep integer?
|
||||
---@field lenght integer?
|
||||
---@field maxRadius integer?
|
||||
---@field minRadius integer?
|
||||
---@field defaulDirection IntegerPosition?
|
||||
---@field wormOffset IntegerPosition?
|
||||
---
|
||||
---@field getNoise fun(self:MapGen.Cavern):MapGen.Cavern.ValueNoise
|
||||
---@field initNoise fun(self:MapGen.Cavern)
|
||||
local Cavern = {}
|
||||
---
|
||||
---A table that stores pre-calculated coordinates of worm-type caves. Filled as needed
|
||||
---
|
||||
---TODO: It might be worth clearing the cache to save RAM.
|
||||
---@field wormCache table<string, table<string, boolean>>
|
||||
local Cavern = {
|
||||
wormCache = {}
|
||||
}
|
||||
|
||||
---Definition table for the `MapGen.Cavern`.
|
||||
---
|
||||
@@ -36,10 +51,18 @@ local Cavern = {}
|
||||
---@field minY integer
|
||||
---@field maxY integer
|
||||
---@field noiseParams MapGen.Cavern.NoiseParams
|
||||
---@field smoothDistance number
|
||||
---@field smoothDistance number?
|
||||
---@field threshold number?
|
||||
---
|
||||
---@field warpStrength number
|
||||
---@field warpStrength number?
|
||||
---
|
||||
---@field regionSize integer?
|
||||
---@field wormStep integer?
|
||||
---@field lenght integer?
|
||||
---@field maxRadius integer?
|
||||
---@field minRadius integer?
|
||||
---@field defaulDirection IntegerPosition?
|
||||
---@field wormOffset IntegerPosition?
|
||||
|
||||
---@param name string
|
||||
---@param def MapGen.CavernDef
|
||||
@@ -56,6 +79,16 @@ function Cavern:new(name, def)
|
||||
assert(noises.zNoise ~= nil, 'The cavern cannot be `warped` but has no `zNoise`')
|
||||
end
|
||||
|
||||
if def.type == 'worm' then
|
||||
local noises = def.noiseParams
|
||||
assert(def.regionSize ~= nil, 'The cavern cannot be `worm` but has no `regionSize`')
|
||||
assert(def.lenght ~= nil, 'The cavern cannot be `worm` but has no `lenght`')
|
||||
assert(def.maxRadius ~= nil, 'The cavern cannot be `worm` but has no `maxRadius`')
|
||||
assert(noises.xNoise ~= nil, 'The cavern cannot be `worm` but has no `xNoise`')
|
||||
assert(noises.yNoise ~= nil, 'The cavern cannot be `worm` but has no `yNoise`')
|
||||
assert(noises.zNoise ~= nil, 'The cavern cannot be `worm` but has no `zNoise`')
|
||||
end
|
||||
|
||||
local _noiseParams = def.noiseParams
|
||||
local _noise = {}
|
||||
|
||||
@@ -68,7 +101,16 @@ function Cavern:new(name, def)
|
||||
instance.maxY = def.maxY
|
||||
instance.smoothDistance = def.smoothDistance
|
||||
instance.threshold = def.threshold or 0.1
|
||||
instance.warpStrength = def.warpStrength
|
||||
|
||||
instance.warpStrength = def.warpStrength
|
||||
|
||||
instance.wormStep = def.wormStep or 1
|
||||
instance.regionSize = def.regionSize
|
||||
instance.lenght = def.lenght
|
||||
instance.maxRadius = def.maxRadius
|
||||
instance.minRadius = def.minRadius or 0
|
||||
instance.defaulDirection = def.defaulDirection or {x = 0, y = -1, z = 0}
|
||||
instance.wormOffset = def.wormOffset or {x = 0, y = 0, z = 0}
|
||||
|
||||
function instance:getNoise()
|
||||
return _noise
|
||||
@@ -123,7 +165,145 @@ function Cavern:isWarpedCavern(x, y, z, weight)
|
||||
return value * weight >= self.threshold
|
||||
end
|
||||
|
||||
---Returns `true` if there is a cavern at the specified coordinates.
|
||||
---Calculates the worm offset for one coordinate.
|
||||
---
|
||||
---If the worm is closer to the boundary than the radius of its tunnel,
|
||||
---it will be forced to turn in the opposite direction.
|
||||
---@param x integer
|
||||
---@param minX integer
|
||||
---@param maxX integer
|
||||
---@param noiseValue number
|
||||
---@param radius integer
|
||||
---@return integer
|
||||
local function calcWormOffset(x, minX, maxX, noiseValue, radius)
|
||||
local distance1 = x - minX
|
||||
local distance2 = maxX - x
|
||||
|
||||
if distance1 <= radius then
|
||||
return 1
|
||||
elseif distance2 <= radius then
|
||||
return -1
|
||||
else
|
||||
return mathRound(mathClamp(noiseValue, -1, 1))
|
||||
end
|
||||
end
|
||||
|
||||
local function drawSphere(wx, wy, wz, radius, cache)
|
||||
local radiusSqrt = (radius + 0.25) ^ 2 -- `+ 0.25` is needed to make the sphere appear more round.
|
||||
|
||||
for sx = -radius, radius do
|
||||
local xSqrt = sx * sx
|
||||
for sy = -radius, radius do
|
||||
local xySqrt = xSqrt + sy * sy
|
||||
|
||||
if xySqrt <= radiusSqrt then
|
||||
for sz = -radius, radius do
|
||||
if xySqrt + (sz * sz) <= radiusSqrt then
|
||||
local posString = corePosToString({x = wx+sx, y = wy+sy, z = wz+sz})
|
||||
|
||||
cache[posString] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---Calculates a full cave for a given region and saves it to the cache.
|
||||
---@param regionPos IntegerPosition
|
||||
---@return table<string, boolean>
|
||||
function Cavern:caclRegionWormCache(regionPos)
|
||||
---@type IntegerPosition
|
||||
local offset = self.wormOffset
|
||||
---@type integer, integer
|
||||
local maxRadius, minRadius = self.maxRadius, self.minRadius
|
||||
local step, size = self.wormStep, self.regionSize
|
||||
local noise = self:getNoise()
|
||||
local cache = {}
|
||||
|
||||
local regionMin = {
|
||||
x = regionPos.x * size + offset.x,
|
||||
y = regionPos.y * size + offset.y,
|
||||
z = regionPos.z * size + offset.z
|
||||
}
|
||||
|
||||
local regionMax = {
|
||||
x = regionMin.x + size,
|
||||
y = regionMin.y + size,
|
||||
z = regionMin.z + size
|
||||
}
|
||||
|
||||
-- Coordinates of the worm's head
|
||||
local wx = regionMin.x + mathFloor(0.5 * size)
|
||||
local wy = regionMin.y + mathFloor(0.5 * size)
|
||||
local wz = regionMin.z + mathFloor(0.5 * size)
|
||||
|
||||
-- Direction vector of the worm's head movement
|
||||
local wormDirection = {x = 0, y = - 1, z = 0}
|
||||
|
||||
for i=1, self.lenght do
|
||||
-- Calculating the new direction of movement of the worm
|
||||
wormDirection = {
|
||||
x = mathClamp(wormDirection.x + calcWormOffset(wx, regionMin.x, regionMax.x, noise.xNoise:get_3d({x=wx, y=wy, z=wz}), maxRadius), -1, 1),
|
||||
y = mathClamp(wormDirection.y + calcWormOffset(wy, regionMin.y, regionMax.y, noise.yNoise:get_3d({x=wx, y=wy, z=wz}), maxRadius), -1, 1),
|
||||
z = mathClamp(wormDirection.z + calcWormOffset(wz, regionMin.z, regionMax.z, noise.zNoise:get_3d({x=wx, y=wy, z=wz}), maxRadius), -1, 1)
|
||||
}
|
||||
|
||||
-- To prevent the worm from getting stuck in place,
|
||||
-- when receiving a zero motion vector, we reset the vector to the default
|
||||
if wormDirection.x == 0 and wormDirection.y == 0 and wormDirection.z == 0 then
|
||||
wormDirection = { x = 0, y = -1, z = 0}
|
||||
end
|
||||
|
||||
wx = wx + wormDirection.x
|
||||
wy = wy + wormDirection.y
|
||||
wz = wz + wormDirection.z
|
||||
|
||||
local radius = mathFloor(noise.baseNoise:get_3d({x = wz, y = wy, z = wz}))
|
||||
if radius > maxRadius then
|
||||
radius = maxRadius
|
||||
end
|
||||
if radius < minRadius then
|
||||
radius = minRadius
|
||||
end
|
||||
|
||||
if i % step == 0 then
|
||||
drawSphere(wx, wy, wz, radius, cache)
|
||||
end
|
||||
end
|
||||
|
||||
return cache
|
||||
end
|
||||
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
---@param weight number
|
||||
---@return boolean
|
||||
function Cavern:isWormCavern(x, y, z, weight)
|
||||
---@type IntegerPosition
|
||||
local offset = self.wormOffset
|
||||
|
||||
local regionPos = {
|
||||
x = mathFloor((x - offset.x)/self.regionSize),
|
||||
y = mathFloor((y - offset.y)/self.regionSize),
|
||||
z = mathFloor((z - offset.z)/self.regionSize)
|
||||
}
|
||||
|
||||
local regionPosString = corePosToString(regionPos)
|
||||
local posString = corePosToString({x = x, y = y, z = z})
|
||||
|
||||
local regionWormCache = self.wormCache[regionPosString]
|
||||
|
||||
if regionWormCache ~= nil then
|
||||
return regionWormCache[posString] == true
|
||||
else
|
||||
local regionCache = self:caclRegionWormCache(regionPos)
|
||||
self.wormCache[regionPosString] = regionCache
|
||||
return regionCache[posString] == true
|
||||
end
|
||||
end
|
||||
|
||||
---@param x integer
|
||||
---@param y integer
|
||||
---@param z integer
|
||||
@@ -140,10 +320,12 @@ function Cavern:isCavern(x, y, z, distance)
|
||||
|
||||
local weight = self:calcCavernWeight(distance)
|
||||
|
||||
if self.type == 'warped' then
|
||||
return self:isWarpedCavern(x, y, z, weight)
|
||||
elseif self.type == 'cheesed' then
|
||||
if self.type == 'cheesy' then
|
||||
return self:isCheesedCavern(x, y, z, weight)
|
||||
elseif self.type == 'warped' then
|
||||
return self:isWarpedCavern(x, y, z, weight)
|
||||
elseif self.type == 'worm' then
|
||||
return self:isWormCavern(x, y, z, weight)
|
||||
else
|
||||
error(('unknown type of cave `%s`'):format(self.type))
|
||||
end
|
||||
@@ -165,10 +347,12 @@ function Cavern:isLayeredCavern(x, y, z)
|
||||
|
||||
local weight = self:calcCavernWeight(distance)
|
||||
|
||||
if self.type == 'warped' then
|
||||
return self:isWarpedCavern(x, y, z, weight)
|
||||
elseif self.type == 'cheesed' then
|
||||
if self.type == 'cheesy' then
|
||||
return self:isCheesedCavern(x, y, z, weight)
|
||||
elseif self.type == 'warped' then
|
||||
return self:isWarpedCavern(x, y, z, weight)
|
||||
elseif self.type == 'worm' then
|
||||
return self:isWormCavern(x, y, z, weight)
|
||||
else
|
||||
error(('unknown type of cave `%s`'):format(self.type))
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user