Looks like a cave doesn't it? This is done using a type of Cellular Automata. Let's begin talking about how this is actually done.
The first thing we have to do is fill the map. In order for this to work we provide a random chance that the map is filled with a '#' or a ' '. In the case above, we filled 45% of the map with '#'. These will represent our walls and the ' ' represents open cave. Below we can see how we filled it.
We can see that we fill the walls with '#' pieces and the rest of the map is given a percent chance to be a wall or an open space. When we run this we get a map that looks like this.
This looks very odd and nothing like a cave. But we can work with this! We can see perfectly that 45% of the map is '#' and the other 55% is ' '. The next step is where everything starts to take shape.
In the next step we check what each tile is. If the tile is a '#' and there are 4 or more walls around it we keep a '#' symbol there. However, if there are less than 2 walls around it we make it a ' ' tile. Then, if the tile is a ' ' and and there are 5 or more '#' tiles around it, we make it a '#' tile otherwise it stays the same. This is tested on every tile and generally over multiple iterations. We can see this implemented below.
Using 1 seed with this algorithm has the ability to generate more than 1 map though depending on the number of iterations. Below we can see the map with the seed '1234' over 1 iteration.
This generates a good looking cave. It has some rough edges though so lets make it over 5 iterations and see what we get below.
The cave looks very similar, maintaining the same shape, the main difference is that it looks much smoother due to the extra iterations. The number of iterations can be changed depending on the type of cave that you want, the choice is completely arbitrary.
So that is how we make random caves using Cellular Automata. The process is extremely simple but generates a unique output each time! I hope you learned something new or interesting. As usual the complete source code can be found on my Github here. See you next week for a new topic!
No comments:
Post a Comment