MapGen: add cavern type warped. Old caverns is now cheesed type
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
local mathRound, mathRandom, mathClamp, tableInsert
|
local mathRound, mathRandom, mathClamp, tableInsert
|
||||||
= math.round, math.random, math.clamp, table.insert
|
= math.round, math.random, math.clamp, table.insert
|
||||||
|
|
||||||
local posToString, getUsTime = core.pos_to_string, core.get_us_time
|
local corePosToString, coreGetUsTime = core.pos_to_string, core.get_us_time
|
||||||
|
|
||||||
local pairs, ipairs, assert = pairs, ipairs, assert
|
local pairs, ipairs, assert = pairs, ipairs, assert
|
||||||
|
|
||||||
@@ -236,7 +236,7 @@ end
|
|||||||
function MapGen:isCavern(layer, x, y, z)
|
function MapGen:isCavern(layer, x, y, z)
|
||||||
for _, cavern in ipairs(layer.cavernsList) do
|
for _, cavern in ipairs(layer.cavernsList) do
|
||||||
if cavern.isRegional then
|
if cavern.isRegional then
|
||||||
local inRegion, distance = self:pointInRegion2D(layer, cavern.name, vector.new(x, y, z))
|
local inRegion, distance = self:pointInRegion2D(layer, cavern.name, v(x, y, z))
|
||||||
|
|
||||||
if inRegion and distance ~= nil then
|
if inRegion and distance ~= nil then
|
||||||
if cavern:isCavern(x, y, z, distance) then
|
if cavern:isCavern(x, y, z, distance) then
|
||||||
@@ -766,7 +766,7 @@ end
|
|||||||
---@param z integer
|
---@param z integer
|
||||||
---@return number?, number?, number?
|
---@return number?, number?, number?
|
||||||
function MapGen:getFactors(layer, x, y, z)
|
function MapGen:getFactors(layer, x, y, z)
|
||||||
local posString = posToString({x=x,y=0,z=z})
|
local posString = corePosToString({x=x,y=0,z=z})
|
||||||
|
|
||||||
local factors = pastNoiseValues[layer.name][posString]
|
local factors = pastNoiseValues[layer.name][posString]
|
||||||
if factors ~= nil then
|
if factors ~= nil then
|
||||||
@@ -774,7 +774,7 @@ function MapGen:getFactors(layer, x, y, z)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- TODO: это сильно тормозит обычные чанки, но ускоряет те, что находятся за границей
|
-- TODO: это сильно тормозит обычные чанки, но ускоряет те, что находятся за границей
|
||||||
if not self:pointInRegion2D(layer, 'triangles_boundary', vector.new(x, y, z)) then
|
if not self:pointInRegion2D(layer, 'triangles_boundary', v(x, y, z)) then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -848,7 +848,7 @@ local function generatorHandler(mapGenerator, layer, data, data2, index, x, y, z
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
local endGetLayerTime = getUsTime()
|
local endGetLayerTime = coreGetUsTime()
|
||||||
local startNoiseCalcTime = endGetLayerTime
|
local startNoiseCalcTime = endGetLayerTime
|
||||||
-- If the value of the noise is `nil`,
|
-- If the value of the noise is `nil`,
|
||||||
-- then the point does not fall within any of the triangles.
|
-- then the point does not fall within any of the triangles.
|
||||||
@@ -879,7 +879,7 @@ end
|
|||||||
---@param maxPos MapVector
|
---@param maxPos MapVector
|
||||||
---@param blockseed number
|
---@param blockseed number
|
||||||
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
||||||
local minPosStr, maxPosStr = posToString(minPos), posToString(maxPos)
|
local minPosStr, maxPosStr = corePosToString(minPos), corePosToString(maxPos)
|
||||||
local startTime, endTime = 0.0, 0.0
|
local startTime, endTime = 0.0, 0.0
|
||||||
|
|
||||||
if LOG_PROFILE then
|
if LOG_PROFILE then
|
||||||
@@ -887,7 +887,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
|||||||
'min_pos: %s\n'..
|
'min_pos: %s\n'..
|
||||||
'max_pos: %s\n',
|
'max_pos: %s\n',
|
||||||
minPosStr, maxPosStr)
|
minPosStr, maxPosStr)
|
||||||
startTime = getUsTime()
|
startTime = coreGetUsTime()
|
||||||
end
|
end
|
||||||
|
|
||||||
local eMin, eMax = voxelManip:get_emerged_area()
|
local eMin, eMax = voxelManip:get_emerged_area()
|
||||||
@@ -951,7 +951,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed)
|
|||||||
voxelManip:update_liquids()
|
voxelManip:update_liquids()
|
||||||
|
|
||||||
if PROFILE_CHUNK then
|
if PROFILE_CHUNK then
|
||||||
endTime = getUsTime()
|
endTime = coreGetUsTime()
|
||||||
mglog('Chunk end:\n'..
|
mglog('Chunk end:\n'..
|
||||||
'min_pos: %s\n'..
|
'min_pos: %s\n'..
|
||||||
'max_pos: %s\n'..
|
'max_pos: %s\n'..
|
||||||
|
|||||||
@@ -1,31 +1,45 @@
|
|||||||
local mathMin, mathMax, mathAbs, mathClamp
|
local assert = assert
|
||||||
= math.min, math.max, math.abs, math.clamp
|
|
||||||
|
local mathMin, mathAbs, mathClamp
|
||||||
|
= math.min, math.abs, math.clamp
|
||||||
|
|
||||||
local getInstance = Class.getInstance
|
local getInstance = Class.getInstance
|
||||||
|
|
||||||
|
---@alias MapGen.Cavern.Type 'cheesed'|'warped'
|
||||||
|
|
||||||
---@class MapGen.Cavern
|
---@class MapGen.Cavern
|
||||||
---@field name string
|
---@field name string
|
||||||
---@field isRegional boolean
|
---@field isRegional boolean?
|
||||||
|
---@field type MapGen.Cavern.Type
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number
|
||||||
---@field threshold number
|
---@field threshold number
|
||||||
---@field getNoise fun():ValueNoise
|
---
|
||||||
|
---@field step integer?
|
||||||
|
---@field warpStrength number
|
||||||
|
---
|
||||||
|
---@field getNoise fun():MapGen.Cavern.ValueNoise
|
||||||
---@field initNoise fun()
|
---@field initNoise fun()
|
||||||
---@field getGroups fun():table<string, number>
|
|
||||||
local Cavern = {}
|
local Cavern = {}
|
||||||
|
|
||||||
|
---@alias MapGen.Cavern.NoiseParams {baseNoise:NoiseParams, xNoise?:NoiseParams, yNoise?:NoiseParams, zNoise?:NoiseParams}
|
||||||
|
---@alias MapGen.Cavern.ValueNoise {baseNoise:ValueNoise, xNoise?:ValueNoise, yNoise?:ValueNoise, zNoise?:ValueNoise}
|
||||||
|
|
||||||
---Definition table for the `MapGen.Cavern`.
|
---Definition table for the `MapGen.Cavern`.
|
||||||
---
|
---
|
||||||
---**Only for EmmyLua.**
|
---**Only for EmmyLua.**
|
||||||
---@class MapGen.CavernDef
|
---@class MapGen.CavernDef
|
||||||
---@field isRegional boolean?
|
---@field isRegional boolean?
|
||||||
|
---@field type MapGen.Cavern.Type
|
||||||
---@field minY number
|
---@field minY number
|
||||||
---@field maxY number
|
---@field maxY number
|
||||||
---@field noiseParams NoiseParams
|
---@field noiseParams MapGen.Cavern.NoiseParams
|
||||||
---@field smoothDistance number
|
---@field smoothDistance number
|
||||||
---@field threshold number?
|
---@field threshold number?
|
||||||
---@field groups table<string, number>?
|
---
|
||||||
|
---@field step integer?
|
||||||
|
---@field warpStrength number
|
||||||
|
|
||||||
---@param name string
|
---@param name string
|
||||||
---@param def MapGen.CavernDef
|
---@param def MapGen.CavernDef
|
||||||
@@ -34,49 +48,41 @@ function Cavern:new(name, def)
|
|||||||
assert(def.maxY > def.minY, 'maxY there should be more minY')
|
assert(def.maxY > def.minY, 'maxY there should be more minY')
|
||||||
assert(def.smoothDistance < mathAbs(def.maxY - def.minY)/2, 'The smoothing distance cannot be greater than half the distance between minY and maxY')
|
assert(def.smoothDistance < mathAbs(def.maxY - def.minY)/2, 'The smoothing distance cannot be greater than half the distance between minY and maxY')
|
||||||
|
|
||||||
---@type NoiseParams
|
if def.type == 'warped' then
|
||||||
|
local noises = def.noiseParams
|
||||||
|
assert(def.step ~= nil, 'The cavern cannot be `warped` but has no `step`')
|
||||||
|
assert(def.warpStrength ~= nil, 'The cavern cannot be `warped` but has no `warpStrength`')
|
||||||
|
assert(noises.xNoise ~= nil, 'The cavern cannot be `warped` but has no `xNoise`')
|
||||||
|
assert(noises.yNoise ~= nil, 'The cavern cannot be `warped` but has no `yNoise`')
|
||||||
|
assert(noises.zNoise ~= nil, 'The cavern cannot be `warped` but has no `zNoise`')
|
||||||
|
end
|
||||||
|
|
||||||
|
---@type MapGen.Cavern.NoiseParams
|
||||||
local _noiseParams = def.noiseParams
|
local _noiseParams = def.noiseParams
|
||||||
|
|
||||||
---@diagnostic disable-next-line: missing-fields
|
---@type MapGen.Cavern.ValueNoise
|
||||||
---@type ValueNoise
|
|
||||||
local _noise = {} -- note: must be nil until luanti mapgen objects are loaded.
|
local _noise = {} -- note: must be nil until luanti mapgen objects are loaded.
|
||||||
|
|
||||||
if def.threshold == nil then
|
|
||||||
def.threshold = 0.1
|
|
||||||
end
|
|
||||||
|
|
||||||
local _groups
|
|
||||||
|
|
||||||
if def.groups == nil then
|
|
||||||
_groups = {}
|
|
||||||
else
|
|
||||||
_groups = def.groups
|
|
||||||
end
|
|
||||||
|
|
||||||
---@type MapGen.Cavern
|
---@type MapGen.Cavern
|
||||||
local instance = getInstance(self, {
|
local instance = getInstance(self, {})
|
||||||
name = name,
|
instance.name = name
|
||||||
minY = def.minY,
|
instance.isRegional = def.isRegional
|
||||||
maxY = def.maxY,
|
instance.type = def.type
|
||||||
smoothDistance = def.smoothDistance,
|
instance.minY = def.minY
|
||||||
threshold = def.threshold,
|
instance.maxY = def.maxY
|
||||||
isRegional = def.isRegional or false,
|
instance.smoothDistance = def.smoothDistance
|
||||||
})
|
instance.threshold = def.threshold or 0.1
|
||||||
|
instance.step = def.step
|
||||||
|
instance.warpStrength = def.warpStrength
|
||||||
|
|
||||||
function instance:getNoise()
|
function instance:getNoise()
|
||||||
return _noise
|
return _noise
|
||||||
end
|
end
|
||||||
|
|
||||||
function instance:initNoise()
|
function instance:initNoise()
|
||||||
if not table.is_empty(_noise) then
|
for noiseName, noiseParams in pairs(_noiseParams) do
|
||||||
minetest.log('warning', 'Cavern noise has already been initialized. Re-initialization is not recommended.')
|
_noise[noiseName] = core.get_value_noise(_noiseParams[noiseName])
|
||||||
end
|
end
|
||||||
|
|
||||||
_noise = core.get_value_noise(_noiseParams)
|
|
||||||
end
|
|
||||||
|
|
||||||
function instance:getGroups()
|
|
||||||
return table.copy_with_metatables(_groups)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
@@ -88,6 +94,40 @@ function Cavern:calcCavernWeight(distance)
|
|||||||
return mathClamp(distance / self.smoothDistance, 0, 1)
|
return mathClamp(distance / self.smoothDistance, 0, 1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param x integer
|
||||||
|
---@param y integer
|
||||||
|
---@param z integer
|
||||||
|
---@param weight number
|
||||||
|
---@return boolean
|
||||||
|
function Cavern:isCheesedCavern(x, y, z, weight)
|
||||||
|
local noises = self:getNoise()
|
||||||
|
|
||||||
|
return noises.baseNoise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param x integer
|
||||||
|
---@param y integer
|
||||||
|
---@param z integer
|
||||||
|
---@param weight number
|
||||||
|
---@return boolean
|
||||||
|
function Cavern:isWarpedCavern(x, y, z, weight)
|
||||||
|
local noises = self:getNoise()
|
||||||
|
assert(noises.xNoise ~= nil and noises.yNoise ~= nil and noises.zNoise ~= nil )
|
||||||
|
local warpX = noises.xNoise:get_3d({x = x, y = y, z = z})
|
||||||
|
local warpY = noises.yNoise:get_3d({x = x, y = y, z = z})
|
||||||
|
local warpZ = noises.zNoise:get_3d({x = x, y = y, z = z})
|
||||||
|
|
||||||
|
local warpStrength = self.warpStrength
|
||||||
|
|
||||||
|
local warpedX = x + warpX * warpStrength
|
||||||
|
local warpedY = y + warpY * warpStrength
|
||||||
|
local warpedZ = z + warpZ * warpStrength
|
||||||
|
|
||||||
|
local value = noises.baseNoise:get_3d({x = warpedX, y = warpedY, z = warpedZ})
|
||||||
|
|
||||||
|
return value * weight > self.threshold
|
||||||
|
end
|
||||||
|
|
||||||
---Returns `true` if there is a cavern at the specified coordinates.
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
---@param x integer
|
---@param x integer
|
||||||
---@param y integer
|
---@param y integer
|
||||||
@@ -103,10 +143,15 @@ function Cavern:isCavern(x, y, z, distance)
|
|||||||
|
|
||||||
distance = mathMin(maxY - y, y - minY, distance)
|
distance = mathMin(maxY - y, y - minY, distance)
|
||||||
|
|
||||||
local noise = self:getNoise()
|
|
||||||
local weight = self:calcCavernWeight(distance)
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
if self.type == 'warped' then
|
||||||
|
return self:isWarpedCavern(x, y, z, weight)
|
||||||
|
elseif self.type == 'cheesed' then
|
||||||
|
return self:isCheesedCavern(x, y, z, weight)
|
||||||
|
else
|
||||||
|
error(('unknown type of cave `%s`'):format(self.type))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Returns `true` if there is a cavern at the specified coordinates.
|
---Returns `true` if there is a cavern at the specified coordinates.
|
||||||
@@ -123,10 +168,15 @@ function Cavern:isLayeredCavern(x, y, z)
|
|||||||
|
|
||||||
local distance = mathMin(maxY - y, y - minY)
|
local distance = mathMin(maxY - y, y - minY)
|
||||||
|
|
||||||
local noise = self:getNoise()
|
|
||||||
local weight = self:calcCavernWeight(distance)
|
local weight = self:calcCavernWeight(distance)
|
||||||
|
|
||||||
return noise:get_3d({x = x, y = y, z = z}) * weight > self.threshold
|
if self.type == 'warped' then
|
||||||
|
return self:isWarpedCavern(x, y, z, weight)
|
||||||
|
elseif self.type == 'cheesed' then
|
||||||
|
return self:isCheesedCavern(x, y, z, weight)
|
||||||
|
else
|
||||||
|
error(('unknown type of cave `%s`'):format(self.type))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return Cavern
|
return Cavern
|
||||||
|
|||||||
Reference in New Issue
Block a user