MapGen: add worm cavern type
This commit is contained in:
@@ -1,13 +1,13 @@
|
|||||||
local assert = assert
|
local assert = assert
|
||||||
local pairs = pairs
|
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
|
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.NoiseParams {baseNoise:NoiseParams, xNoise?:NoiseParams, yNoise?:NoiseParams, zNoise?:NoiseParams}
|
||||||
---@alias MapGen.Cavern.ValueNoise {baseNoise:ValueNoise, xNoise?:ValueNoise, yNoise?:ValueNoise, zNoise?:ValueNoise}
|
---@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 type MapGen.Cavern.Type
|
||||||
---@field minY integer
|
---@field minY integer
|
||||||
---@field maxY integer
|
---@field maxY integer
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number?
|
||||||
---@field threshold 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 getNoise fun(self:MapGen.Cavern):MapGen.Cavern.ValueNoise
|
||||||
---@field initNoise fun(self:MapGen.Cavern)
|
---@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`.
|
---Definition table for the `MapGen.Cavern`.
|
||||||
---
|
---
|
||||||
@@ -36,10 +51,18 @@ local Cavern = {}
|
|||||||
---@field minY integer
|
---@field minY integer
|
||||||
---@field maxY integer
|
---@field maxY integer
|
||||||
---@field noiseParams MapGen.Cavern.NoiseParams
|
---@field noiseParams MapGen.Cavern.NoiseParams
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number?
|
||||||
---@field threshold 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 name string
|
||||||
---@param def MapGen.CavernDef
|
---@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`')
|
assert(noises.zNoise ~= nil, 'The cavern cannot be `warped` but has no `zNoise`')
|
||||||
end
|
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 _noiseParams = def.noiseParams
|
||||||
local _noise = {}
|
local _noise = {}
|
||||||
|
|
||||||
@@ -68,8 +101,17 @@ function Cavern:new(name, def)
|
|||||||
instance.maxY = def.maxY
|
instance.maxY = def.maxY
|
||||||
instance.smoothDistance = def.smoothDistance
|
instance.smoothDistance = def.smoothDistance
|
||||||
instance.threshold = def.threshold or 0.1
|
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()
|
function instance:getNoise()
|
||||||
return _noise
|
return _noise
|
||||||
end
|
end
|
||||||
@@ -123,7 +165,145 @@ function Cavern:isWarpedCavern(x, y, z, weight)
|
|||||||
return value * weight >= self.threshold
|
return value * weight >= self.threshold
|
||||||
end
|
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 x integer
|
||||||
---@param y integer
|
---@param y integer
|
||||||
---@param z integer
|
---@param z integer
|
||||||
@@ -140,10 +320,12 @@ function Cavern:isCavern(x, y, z, distance)
|
|||||||
|
|
||||||
local weight = self:calcCavernWeight(distance)
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
if self.type == 'warped' then
|
if self.type == 'cheesy' then
|
||||||
return self:isWarpedCavern(x, y, z, weight)
|
|
||||||
elseif self.type == 'cheesed' then
|
|
||||||
return self:isCheesedCavern(x, y, z, weight)
|
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
|
else
|
||||||
error(('unknown type of cave `%s`'):format(self.type))
|
error(('unknown type of cave `%s`'):format(self.type))
|
||||||
end
|
end
|
||||||
@@ -165,10 +347,12 @@ function Cavern:isLayeredCavern(x, y, z)
|
|||||||
|
|
||||||
local weight = self:calcCavernWeight(distance)
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
if self.type == 'warped' then
|
if self.type == 'cheesy' then
|
||||||
return self:isWarpedCavern(x, y, z, weight)
|
|
||||||
elseif self.type == 'cheesed' then
|
|
||||||
return self:isCheesedCavern(x, y, z, weight)
|
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
|
else
|
||||||
error(('unknown type of cave `%s`'):format(self.type))
|
error(('unknown type of cave `%s`'):format(self.type))
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -766,7 +766,7 @@ mapGenerator:registerBiome('midgard', 'green_meadow', {
|
|||||||
mapGenerator:registerBiome('midgard', 'test_factors_biome', {
|
mapGenerator:registerBiome('midgard', 'test_factors_biome', {
|
||||||
tempPoint = 10,
|
tempPoint = 10,
|
||||||
humidityPoint = 10,
|
humidityPoint = 10,
|
||||||
minY = -50,
|
minY = -1000,
|
||||||
maxY = 100,
|
maxY = 100,
|
||||||
groundNodes = {
|
groundNodes = {
|
||||||
soil = 'soils:humus',
|
soil = 'soils:humus',
|
||||||
@@ -790,15 +790,23 @@ mapGenerator:registerBiome('midgard', 'test_factors_biome', {
|
|||||||
mapGenerator:registerTree('test_tree', {
|
mapGenerator:registerTree('test_tree', {
|
||||||
path = core.get_modpath(core.get_current_modname())..'/schems/test_tree.mts',
|
path = core.get_modpath(core.get_current_modname())..'/schems/test_tree.mts',
|
||||||
})
|
})
|
||||||
--[[
|
|
||||||
mapGenerator:registerCavern('midgard', 'test_cavern', {
|
mapGenerator:registerCavern('midgard', 'test_cavern', {
|
||||||
type = 'warped',
|
type = 'worm',
|
||||||
|
regionSize = 80,
|
||||||
|
lenght = 100,
|
||||||
|
maxRadius = 6,
|
||||||
|
minRadius = 2,
|
||||||
|
wormStep = 1,
|
||||||
warpStrength = 5,
|
warpStrength = 5,
|
||||||
|
wormOffset = {
|
||||||
|
x = 0, y = 40, z = 0
|
||||||
|
},
|
||||||
noiseParams = {
|
noiseParams = {
|
||||||
baseNoise = {
|
baseNoise = {
|
||||||
offset = 0,
|
offset = 1.8,
|
||||||
scale = 1,
|
scale = 2,
|
||||||
spread = {x = 200, y = 8, z = 8},
|
spread = {x = 2, y = 2, z = 2},
|
||||||
seed = 88005553535,
|
seed = 88005553535,
|
||||||
octaves = 2,
|
octaves = 2,
|
||||||
persistence = 0.6,
|
persistence = 0.6,
|
||||||
@@ -806,8 +814,8 @@ mapGenerator:registerCavern('midgard', 'test_cavern', {
|
|||||||
},
|
},
|
||||||
xNoise = {
|
xNoise = {
|
||||||
offset = 0,
|
offset = 0,
|
||||||
scale = 4,
|
scale = 1,
|
||||||
spread = {x = 10, y = 30, z = 30},
|
spread = {x = 2, y = 10, z = 5},
|
||||||
seed = 42,
|
seed = 42,
|
||||||
octaves = 2,
|
octaves = 2,
|
||||||
persistence = 0.6,
|
persistence = 0.6,
|
||||||
@@ -815,8 +823,8 @@ mapGenerator:registerCavern('midgard', 'test_cavern', {
|
|||||||
},
|
},
|
||||||
yNoise = {
|
yNoise = {
|
||||||
offset = 0,
|
offset = 0,
|
||||||
scale = 4,
|
scale = 1,
|
||||||
spread = {x = 10, y = 10, z = 30},
|
spread = {x = 5, y = 10, z = 2},
|
||||||
seed = 34,
|
seed = 34,
|
||||||
octaves = 2,
|
octaves = 2,
|
||||||
persistence = 0.6,
|
persistence = 0.6,
|
||||||
@@ -824,8 +832,8 @@ mapGenerator:registerCavern('midgard', 'test_cavern', {
|
|||||||
},
|
},
|
||||||
zNoise = {
|
zNoise = {
|
||||||
offset = 0,
|
offset = 0,
|
||||||
scale = 4,
|
scale = 1,
|
||||||
spread = {x = 10, y = 30, z = 30},
|
spread = {x = 2, y = 10, z = 3},
|
||||||
seed = 12,
|
seed = 12,
|
||||||
octaves = 2,
|
octaves = 2,
|
||||||
persistence = 0.6,
|
persistence = 0.6,
|
||||||
@@ -838,6 +846,6 @@ mapGenerator:registerCavern('midgard', 'test_cavern', {
|
|||||||
minY = -500,
|
minY = -500,
|
||||||
isEroded = true,
|
isEroded = true,
|
||||||
step = 100
|
step = 100
|
||||||
})--]]
|
})
|
||||||
|
|
||||||
return mapGenerator
|
return mapGenerator
|
||||||
|
|||||||
Reference in New Issue
Block a user