Cellular Automata for Caves

When you’re creating a 2d game about exploration, the missing dimension surely makes it a bit more difficult to layer in detail. In a normal 3d exploration game, I could get away with not having caves underground. I could just fill the terrain with faun and fauna with extra detail and have a pretty basic planet already going on. When you remove the third (or rather neglect to add), it becomes apparent that a simple terrain just wont cut it.

In the earlier stages of my terrain generator, the world was relatively flat with no holes or caves in it. Just endless plains with a few highlands. Exploring this world wasn’t all that exciting. From a technological standpoint, it was still great for me. I had generated a world that had it’s own terrain that was created by maths… and then what? I couldn’t imagine a player caring about how cool it was that a basic terrain was made by maths. This was to be only an internal hurrah!

screen star
Nope. Still can’t imagine a player relishing this

Caves were obviously on my mind from the beginning of developing this but I really had no clue where to start. I looked around the GameMaker Community to see whether anyone else had attempted this and I came across the term “Cellular Automaton”. Another quick search and I found this great tutorial. So let me detail the actually very simple steps of cellular automaton.

First the terrain is filled with 1s and 0s. For GameMaker users, this will be in your ds_grid or array (I recommend ds_grid). Here “1” stands for being alive while “0” stands for being dead. The first cycle loops through each cell looking at it’s neighbours left, right up and down (you can even include diagonal cells as well). It sums up the value of alive cells around it. If the value of alive cells is greater than or equal to 4 and the cell is dead, the cell is made to be alive (change it’s value to 1). Also, if the cell is alive and the sum of alive cells is >= 4 then it stays alive. That’s it! That’s all you need to get basic cellular automaton working and it can lead to some good looking caves although you have to be careful.

In the beginning, I was making large chunks of my terrain dead and this lead to some interesting results:

This method sometimes rendered scrotum resembling terrain

This was frustrating as I needed enterances into the cave but all my methods were giving me rather rough terrain in order to achieve that. After a few days of playing around with parameters, I finally cracked it. I created small holes in the terrain at the top for enterances. Then, when generating the random holes for the cellular automaton, the chance that a dead cell was generated was proportional to it’s depth in the terrain. The deeper it was in the terrain the more likely a cell was going to be dead. This gave me terrain like this:

no light

and this:

proper caves
Oooh Baby!

While still allowing for caves like this:


The cave generation was done-ish 🙂

In other news, I have now opened up a public trello roadmap for the game that you can view here: http://bit.ly/1OhSGpX

You can also check the development on tigsource through this link: http://bit.ly/1IVA3vg

Please support the development by sharing this post and leaving a comment below 🙂 Thanks for reading.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s