Gridwright Store

21 Nov 2026 · By Gridwright Studio

Procedural Cave Generation with Cellular Automata

Gridwright pack artwork for Procedural Cave Generation with Cellular Automata

Caves are the easiest procedural maps to make look natural. Rooms and corridors need rules about doors, sizes and layout, while a cave only needs rock that looks worn away. The method most games use for that is a cellular automaton: start from noise, smooth it a few times, and the noise turns into caverns. This guide walks through each step, the settings that matter, and what you still have to fix before a cave is fun to play.

The idea in one paragraph

A cellular automaton is a grid where every cell looks at its neighbours and changes by a fixed rule, all cells at the same time. For caves the grid holds two states, rock and floor. You fill it at random, then apply one rule again and again: a cell becomes rock when most of its eight neighbours are rock. Lone rock cells in open floor disappear, lone floor cells inside rock fill in, and after a few passes the noise has clumped into smooth walls and open caverns.

Step by step

  • Fill the grid with rock at a set chance per cell, for example 45%, and make the outer border rock so the cave is closed.
  • Smooth it: for every cell, count the rock cells among its eight neighbours, counting cells outside the map as rock. Five or more makes the cell rock, four or fewer makes it floor.
  • Write the results into a second grid and swap the two at the end of the pass, so every cell reads the old state, not a neighbour that has already changed.
  • Repeat the smoothing four or five times. Each pass rounds the walls a little more, and after that the shape barely changes.
  • Find the caverns with a flood fill: every group of floor cells joined side to side is one cavern.
  • Fill the small caverns back in with rock, and join the rest to the biggest one with tunnels.
  • Place the start, the exit, props and enemies on floor the player can reach.

The settings that change the cave

The rock chance is the main dial. Lower it and the caverns merge into one open hall. Raise it and the cave breaks into small pockets joined by thin passages. Somewhere in the middle, often in the mid forties, you get the classic look of chambers that open into each other. Try a few values on the same seed and you will see the whole range in a minute.

The number of smoothing passes decides how soft the walls are. One or two passes leave ragged, noisy edges, which can suit an ice or crystal cave. Four or five give the rounded walls people expect from a cave. More than that changes very little, because the shape has settled.

The seed makes it repeatable. Use your own random number generator, started from a number or a word, and the same seed always rebuilds the same cave. That lets you share a level by its seed, store a whole map as one value in a save file, and rebuild the exact map a player sent in a bug report.

Dungeon & Map Generator: a crystal cave made by cellular automata, with lakes, crystals, mushrooms and spawn points
A 64x48 cave: rock 46%, five smoothing passes, small pockets filled, the rest joined by tunnels.

Fixing what the automaton gets wrong

The rule knows nothing about players, so a raw result often has caverns that can't be reached from the rest. The flood fill finds them. Pockets of a few cells aren't worth a tunnel: fill them with rock. For the bigger ones, dig a tunnel from each to the nearest floor cell of the main cavern. A straight line works, and a path that wanders a little looks more like a cave.

Then check the map the way a level designer would:

  • Every floor cell can be reached from the start.
  • The exit is far enough from the start to make the trip worth it.
  • No prop that blocks movement sits in a passage one tile wide.
  • No enemy spawns right next to the start.
  • Liquid never covers the only way through.

Run the checks on many seeds, not one. A generator that works on ten maps can still fail on the hundredth, usually at the extremes: a tiny map, a very high rock chance, or a size that leaves room for one cavern only. Those cases need a fallback, such as lowering the rock chance and trying again, rather than a crash or a map with no start.

Wandering tunnels: the other cave method

A random walk, often called the drunkard's walk, makes a different kind of cave. Start a digger in solid rock and move it one cell at a time in a random direction, turning each cell it visits into floor, until a set share of the map is open. The result is a network of winding tunnels rather than round chambers.

Because the digger never jumps, everything it digs is connected, so this method needs no flood fill to find lost pockets. Use it alone for narrow mines and lava tubes, or run it after the automaton to add a few long tunnels between chambers.

Water, lava and props

A cave map is more than floor and rock. Lakes and lava pools come from the same idea at a larger scale: a second noise layer, or a flood from a low point, marks some floor as liquid. Keep liquid out of tunnels that are the only way through, or give the player a bridge.

Props such as stalagmites, crystals, mushrooms and bones look best near walls and in dead ends, where they hint that nobody comes here. Check each prop that blocks movement against the path from start to exit before you keep it, and drop it if it cuts the path.

From a grid to tiles

The automaton gives you a grid of two values. The game needs tiles: wall edges that join, floor variants and collision. Most engines handle the joining with autotiling. Godot 4 calls it terrains, and Tiled calls it terrain or Wang sets. Paint the rock cells with a wall terrain and the floor with a floor terrain, and the engine picks the right corner and edge pieces for you. Give the wall tiles collision in the TileSet, and the cave can be walked.

If you work on an isometric game, the same grid works: generate it flat, then place isometric wall and floor pieces on it. Our Isometric Dungeon & Caves pack has cave rock, dirt, moss, crystal and volcanic ground on the 32x16 diamond grid, with cave water and magma, and the Dungeon Tileset covers top-down stone halls.

Isometric Dungeon & Caves: an isometric pixel art cave with water, crystal rock, magma, mine rails and glowing mushrooms
The same cave ideas in isometric: water, crystals, magma and mine rails from Isometric Dungeon & Caves.

Try it without writing the code

Our Dungeon & Map Generator has a cave mode built on these steps: cellular automata or wandering tunnels, a rock slider, smoothing steps, the smallest cavern it keeps, tunnel width, and lakes or lava with stalagmites, crystals and mushrooms. Every cave is checked so all floor can be reached from the start, and the same seed always gives the same map. The full edition of Gridwright Dungeon & Map Generator saves caves as PNG, Tiled maps with spawn objects, Godot 4 scenes with collision and terrains, and a JSON grid. The free demo covers dungeons up to 64x64.

For dungeons with rooms and corridors instead of caves, read dungeon generators for game developers and dungeon level design: rooms, traps and lighting.

Found it useful? Share it

Get the art for your game

Our packs together in the $20 Megabundle, a custom pack drawn for your game, or the packs in this post from $9.99 each.

Best value

Gridwright Megabundle

Our packs and tools on itch.io in one bundle, for less than three of them one by one.

Get it for $20 (opens in a new tab)

Custom packs from $49

Not finding it? We draw it for your game, in 14 days or your money back.

Order a custom pack

Packs mentioned

Related guides