From 9252cf429cbe2f2700ef8407b12d4d7a41046b25 Mon Sep 17 00:00:00 2001 From: varttist Date: Sat, 13 Jun 2026 20:22:32 +0300 Subject: [PATCH] `MapGen`: add cavern type `warped`. Old caverns is now `cheesed` type --- mods/CORE/MapGen/src/MapGen.lua | 24 ++--- mods/CORE/MapGen/src/MapGen/Cavern.lua | 136 +++++++++++++++++-------- 2 files changed, 105 insertions(+), 55 deletions(-) diff --git a/mods/CORE/MapGen/src/MapGen.lua b/mods/CORE/MapGen/src/MapGen.lua index 1ee8c52..9c288a9 100644 --- a/mods/CORE/MapGen/src/MapGen.lua +++ b/mods/CORE/MapGen/src/MapGen.lua @@ -1,7 +1,7 @@ local mathRound, mathRandom, mathClamp, tableInsert = 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 @@ -56,10 +56,10 @@ local Region = require('MapGen.Region') ---@field nodes table ---@field nodeIDs table local MapGen = { - layersByName = {}, - layersList = {}, - staticsByName = {}, - staticsList = {}, + layersByName = {}, + layersList = {}, + staticsByName = {}, + staticsList = {}, isLayersNoisesInitialized = false, isCavernsNoiseInitialized = false, isRunning = false, @@ -236,7 +236,7 @@ end 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, vector.new(x, y, z)) + local inRegion, distance = self:pointInRegion2D(layer, cavern.name, v(x, y, z)) if inRegion and distance ~= nil then if cavern:isCavern(x, y, z, distance) then @@ -766,7 +766,7 @@ end ---@param z integer ---@return number?, number?, number? 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] if factors ~= nil then @@ -774,7 +774,7 @@ function MapGen:getFactors(layer, x, y, z) end -- 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 end @@ -848,7 +848,7 @@ local function generatorHandler(mapGenerator, layer, data, data2, index, x, y, z return end - local endGetLayerTime = getUsTime() + local endGetLayerTime = coreGetUsTime() local startNoiseCalcTime = endGetLayerTime -- If the value of the noise is `nil`, -- then the point does not fall within any of the triangles. @@ -879,7 +879,7 @@ end ---@param maxPos MapVector ---@param blockseed number 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 if LOG_PROFILE then @@ -887,7 +887,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed) 'min_pos: %s\n'.. 'max_pos: %s\n', minPosStr, maxPosStr) - startTime = getUsTime() + startTime = coreGetUsTime() end local eMin, eMax = voxelManip:get_emerged_area() @@ -951,7 +951,7 @@ function MapGen:onMapGenerated(voxelManip, minPos, maxPos, blockseed) voxelManip:update_liquids() if PROFILE_CHUNK then - endTime = getUsTime() + endTime = coreGetUsTime() mglog('Chunk end:\n'.. 'min_pos: %s\n'.. 'max_pos: %s\n'.. diff --git a/mods/CORE/MapGen/src/MapGen/Cavern.lua b/mods/CORE/MapGen/src/MapGen/Cavern.lua index e1a67eb..094b92c 100644 --- a/mods/CORE/MapGen/src/MapGen/Cavern.lua +++ b/mods/CORE/MapGen/src/MapGen/Cavern.lua @@ -1,31 +1,45 @@ -local mathMin, mathMax, mathAbs, mathClamp - = math.min, math.max, math.abs, math.clamp +local assert = assert + +local mathMin, mathAbs, mathClamp + = math.min, math.abs, math.clamp local getInstance = Class.getInstance +---@alias MapGen.Cavern.Type 'cheesed'|'warped' + ---@class MapGen.Cavern ---@field name string ----@field isRegional boolean +---@field isRegional boolean? +---@field type MapGen.Cavern.Type ---@field minY number ---@field maxY number ---@field smoothDistance number ---@field threshold number ----@field getNoise fun():ValueNoise ----@field initNoise fun() ----@field getGroups fun():table +--- +---@field step integer? +---@field warpStrength number +--- +---@field getNoise fun():MapGen.Cavern.ValueNoise +---@field initNoise fun() 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`. --- ---**Only for EmmyLua.** ---@class MapGen.CavernDef ---@field isRegional boolean? +---@field type MapGen.Cavern.Type ---@field minY number ---@field maxY number ----@field noiseParams NoiseParams +---@field noiseParams MapGen.Cavern.NoiseParams ---@field smoothDistance number ---@field threshold number? ----@field groups table? +--- +---@field step integer? +---@field warpStrength number ---@param name string ---@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.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 - ---@diagnostic disable-next-line: missing-fields - ---@type ValueNoise + ---@type MapGen.Cavern.ValueNoise 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 - local instance = getInstance(self, { - name = name, - minY = def.minY, - maxY = def.maxY, - smoothDistance = def.smoothDistance, - threshold = def.threshold, - isRegional = def.isRegional or false, - }) + local instance = getInstance(self, {}) + instance.name = name + instance.isRegional = def.isRegional + instance.type = def.type + instance.minY = def.minY + instance.maxY = def.maxY + instance.smoothDistance = def.smoothDistance + instance.threshold = def.threshold or 0.1 + instance.step = def.step + instance.warpStrength = def.warpStrength function instance:getNoise() return _noise end function instance:initNoise() - if not table.is_empty(_noise) then - minetest.log('warning', 'Cavern noise has already been initialized. Re-initialization is not recommended.') + for noiseName, noiseParams in pairs(_noiseParams) do + _noise[noiseName] = core.get_value_noise(_noiseParams[noiseName]) end - - _noise = core.get_value_noise(_noiseParams) - end - - function instance:getGroups() - return table.copy_with_metatables(_groups) end return instance @@ -88,6 +94,40 @@ function Cavern:calcCavernWeight(distance) return mathClamp(distance / self.smoothDistance, 0, 1) 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. ---@param x integer ---@param y integer @@ -103,10 +143,15 @@ function Cavern:isCavern(x, y, z, distance) 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 + 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 ---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 noise = self:getNoise() 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 return Cavern