Connecting isolated caves in procedural cave generation

Currently, I’m using cellular automata to create procedurally generated caves and it works fine except for one problem.


As you can see in the image, there are pockets of isolated caves that aren’t connected together. What would I have to do to connect them?

2 Likes

after finding an unconnected cave you could use a similar algorithm to the a-start path finding algorithm, this could give you a series of points to another cave that you use to generate the cave connecting them together

Your suggestion is very vague, could you care to elaborate?
Like for example, what do you mean by a “similar algorithm to the a-start path finding algorithm”
How would this give me a “series of points to another cave” ?

I recall that the YouTube Sebastian Lague has a video series about cave generation with cellular automata. He faced the same problem where he would like the caves to connect to each other.

Eventually, he managed to connect the caves.

Yeah, I’ve also seen his video series on procedural cave generation. Unfortunately, I’m having a hard time converting his code into Luau.

Ok, well what about this resource? The article contains an explanation about ‘The isolated cave problem’ and gives a multitude of ways to combat it.

self bump

I was thinking of flood-filling each different room, finding the centre of these regions and then using random walks to open up paths between these regions.
However, how would I use flood-fill to find and distinguish different regions of my cave, like so:

not a genius but could you loop through all the created caves, then check to see if its connected to another other cave, if not draw that new cave hallway to fit the distance needed to connect to the closets one

You went through exactly all of the logic behind it and none of the maths.
The problem I have with this is all the maths that needs to be done. Taking just one example in your suggestion, how would the script recognize a cave in the first place?

lmao i don’t know the math i am terrible at math, but.. would a maze generation algorithm on top of the cave generation work?

i never worked with cave generation before, but i assume caves are generated from points on a map right?

I’ve implemented a flood fill algorithm to my code, however, it’s still filling up isolated caves for some reason.

local function Flood(x,y)
	if not Grid[x][y].flood and not Grid[x][y].Alive then
		
		Grid[x][y].flood = true
		Grid[x][y].Part.Color = Color3.new(0, 1, 0)	
		wait(1 / 10)
		Grid[x][y].Part.Color = Color3.new(1, 0, 0)
		
		if x < #Grid[1] then
			coroutine.wrap(Flood)(x+1, y)
		end
		if x > 1 then
			coroutine.wrap(Flood)(x-1, y)
		end
		if y < #Grid then
			coroutine.wrap(Flood)(x, y+1)
		end
		if y > 1 then
			coroutine.wrap(Flood)(x, y-1)
		end
		if x < #Grid[1] and y > 1 then
			coroutine.wrap(Flood)(x+1, y-1)
		end
		if x > 1 and y > 1 then
			coroutine.wrap(Flood)(x-1, y-1)
		end
		if x < #Grid[1] and y < #Grid then
			coroutine.wrap(Flood)(x+1, y+1)
		end
		if x > 1 and y < #Grid then
			coroutine.wrap(Flood)(x-1, y+1)
		end
	end
end

I think the problem has to do with how I’m calling the starting cell position. In theory, I want the script to loop through all points in my array until it finds an open cell, then it would call the flood fill function once and break off the loop.
However, the loop still seems to continue after the first open cell was found.


	for x=1, SizeX do
		for y=1, SizeY do
			if not Grid[x][y].Alive then
				-- Cell is dead, therefore open
				Flood(x,y)
				break
			end
		end
	end