Cave generation code isn't working properly

  1. What do you want to achieve? Keep it simple and clear!;
    I’m trying to make my own cave generation system for an infinite mining game.

  2. What is the issue? Include screenshots / videos if possible!;
    The code I wrote is doing something, but it isn’t generating a cave.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?;

I’ve searched for devforum posts and searched on other platforms for a proper solution, but couldn’t find anything.

My code so far:

module.GeneratedPositions = {

}

module.CaveTypes = {
	["Regular Cave"] = {
		["Stone"] = "Stone",
		
		["MinDepth"] = 60, -- doesnt do anything for now
		["MaxDepth"] = 600, -- doesnt do anything for now either
		
		["CaveSizeX"] = 32,
		["CaveSizeY"] = 48,
		["CaveSizeZ"] = 32,
		
		["NoiseScale"] = 25,
		["NoiseAmplitude"] = 15,
		["DensityLimit1"] = -8,
		["DensityLimit2"] = -32,
	} 
}
local function RelativePosition(Position)
	return Position-Vector3.new(0,20000,0)
end

local function DepthPosition(Position)
	return math.abs(RelativePosition(Position).Y)
end
function module.GenerateCave(CaveType,Position)
	
	table.insert(module.GeneratedPositions,Position)
	
	local Seed = tick()
	local y = DepthPosition(Position)
	local Cave = module.CaveTypes[CaveType]
	
	local CaveSizeX = Cave["CaveSizeX"]
	local CaveSizeY = Cave["CaveSizeY"]
	local CaveSizeZ = Cave["CaveSizeZ"]
	
	local NoiseScale = Cave["NoiseScale"]
	local NoiseAmplitude = Cave["NoiseAmplitude"]
	local DensityLimit1 = Cave["DensityLimit1"]
	local DensityLimit2 = Cave["DensityLimit2"]
	
	if Cave["Seed"] ~= nil then
		Seed = Cave["Seed"]
	end
	
	local NoiseX
	local NoiseY
	local NoiseZ
	
	local Density
	
	local function snap(newcf)
		local posx = math.floor((newcf.X / 5) + 0.5) * 5
		local posy = math.floor((newcf.Y / 5) + 0.5) * 5
		local posz = math.floor((newcf.Z / 5) + 0.5) * 5

		return Vector3.new(posx,posy,posz)
	end
	
	for x = 0,CaveSizeX do
		for z = 0,CaveSizeZ do
			for y = 0,CaveSizeY do
				NoiseX = math.noise(y/NoiseScale,z/NoiseScale,Seed)*NoiseAmplitude
				NoiseY = math.noise(x/NoiseScale,z/NoiseScale,Seed)*NoiseAmplitude
				NoiseZ = math.noise(x/NoiseScale,y/NoiseScale,Seed)*NoiseAmplitude
				Density = NoiseX + NoiseY + NoiseZ
				local NewPosition = Position+Vector3.new(x*5,y*5,z*5)
				if Density < DensityLimit1 and Density > DensityLimit2 then -- 14, 14
					module.Generate(module.GetRandomOre(y,CaveType),NewPosition)
				else
					table.insert(module.GeneratedPositions,NewPosition)
				end
			end
		end
		task.wait()
	end
end

Results in this:

last time i checked this isnt what a cave looks like

1 Like