Realistic Cave Generation

To preface this, let me just mention I have very little experience with math.noise/perlin noise and I imagine that’d be a big part of this, so explain it like I’m five.

I’ve been trying to make a script for a mining game, one that generates believable caves on command, think Minecraft or Azure Mines on ROBLOX, the issue is, my method (preset vectors randomized by 1 block) is very laggy to generate and typically results in players falling below the mine.

I don’t like forking from/referencing other games code (especially for something I intend to market and profit from), so I’ve been considering referencing the Azure Mines open-source project as a sort of “last resort”.

My best option with that in mind was to raise the question here.


Brief Explanation of the System:

You break a block with a pickaxe and it generates a block in each direction (above, below, left, right) where a block has not existed prior, fairly simple system.

How could I take this system and implement cave generation into it?

2 Likes

As far as cave generation algorithms go, that requires some good math and calculations. However, there’s also the cave presets method. Which you design and create caves that are sort of like building blocks. You can plug one into the other and modify them through code.

For the block generation, lets say each block is a 4x4x4. You can have an array generated and used as a map. Depending on which block is hit, you calculate the array positioning (number) using the blocks position in the world (Calculated using the positioning and scale of the block. As well as the height and width of the play area). Then, you generate the blocks around the array. For example:

1 = Grass
2 = Stone
3 = Copper

MapArray =
{
1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 3, 2, 2, 3, 2, 2,
2, 2, 2, 2, 2, 2, 3, 2,
2, 2, 3, 2, 2, 2, 2, 2,
}

This is just one of many ways to tackle the situation.

Blocks can already be generated freshly with no presets, the only issue is generating the air blocks randomly and then generating blocks around those air blocks.

Perlin worms are usually the best solution for caves

2 Likes

I tried the perlin worm solution, however it didn’t produce a result I particularly favored.

I am looking to randomly generate caves that look like this:


However Perlin Worms were unable to accomplish this.

1 Like

Perlin worms can 100% accomplish that

The video just shows the “path” the cave would take. Would would also have a cave “width” and “height” value determined with perlin too. You’d then clear out any blocks on that path ± width and height and generate blocks on the outside of that

I suppose my original code was improper then as the extent of it’s capabilities were limited.

Let’s say I have a basic (6,6,6) cube that can only generate bricks in 6 directions (pictured)


How would I write a Perlin Worm function that reliably generates caves that can be easily implemented? Like I said, I am not well-versed with Perlin Noise so I apologize for my lack of knowledge on the subject.

1 Like

Well if your blocks are a set size and you know they’re 6x6x6, calculate the positioning around the block and check a dictionary if that position has had a block spawned before. If it hasn’t, insert the new position into a dictionary and spawn the block.

For example, first block is a 6x6x6 on position (0, 0, 0). Add the tostring() value of the position into a dictionary as follows:

SpawnedBlocks[tostring(Block.Position)] = true --(Or maybe store the block type)

When you mine the block, in all directions, you do the following:

if (SpawnedBlocks[tostring(Block.Position + Vector3.new(6,0,0,))] == nil then -- Check if the space has not been occupied before
   SpawnedBlocks[tostring(Block.Position + Vector3.new(6,0,0,))]  = true; -- Spawns a block to the right.
end

You can also have blacklist X,Y,Z integers if you don’t want blocks to go a certain way. This is just one of many ways to tackle this problem.

This issue has already been rectified as my code already handles the basic mining algorithms to spawn blocks in the proper directions (the ones pictured previously), my issue is generating random caves when the situation calls for it.

Hey, sorry for the bump, but did you get this to work? I have the same problem.

Sorry for the bump here, but the algorithm you were looking for was just plain ol’ 3D perlin noise. (math.noise)

There is a 3D mining kit made by berezaa using the same cave generating code. You could’ve looked at it and then have figured out how to do it.