# Roguelike game in C++ - Map generation with Perlin noise

## Posted on July 25, 2012 by Paul

The code for this post is on GitHub: https://github.com/sol-prog/roguelike.

In the last post of this series we’ve added a dummy map to the game on which the main character was able to move freely. Now it is time to add a real map to our roguelike game, in order to be able to add elements on the map we are going to use a Perlin noise function. If you are interested in how I’ve implemented the Perlin noise function in C++ you could read my Perlin noise in C++11 article.

I’ll include here, for completeness, the PerlinNoise class definition. The complete code is on the Github repository posted at the beginning of this article:

Let’s start by adding a new function, gen_Perlin, to the Frame class created in the second part of this series, this function will use PerlinNoise to fill the map with lakes, planes, mountains and snow. For this, we’ll need to distribute the relief according to some arbitrary rule. Assuming the PerlinNoise will return values from 0 to 1, we could use the next rule for generating the terrain:

• from 0 to 0.35 we’ll have water or lakes
• from 0.35 to 0.6 we’ll have floor or planes
• from 0.6 to 0.8 we’ll have walls or mountains
• from 0.8 to 1.0 we’ll have snow

Using the above rule, we could implement the gen_Perlin member function:

For using gen_Perlin, we’ll need to change a single line in the main function of our game:

line 2 from above we’ll become:

The number 237 from above was chosen arbitrarily, it is an unsigned integer used to seed the random number engine that will be used to generate a permutation in the PerlinNoise object. We could change this number from game session to game session and we’ll have a large number of different maps.

Let’s start the game and see what we’ve got until now:

I think the terrain looks cool, but the fact that we can walk through walls and watter is not so realistic, we’ll need to add some rules to the game like e.g.:

• no walking through walls!
• no walking trough watter unless the @ has a boat or some magical spell (to be defined later).

Currently, the character movements are simulated trough this function:

Let’s add a quick and dirty test that will block @ from going through watter, walls and snow, see lines 4 and 6:

Let’s run the code:

No matter how many time the user will try, the main character won’t be able to go trough watter, walls and snow.

Next time, we are going to add objects on the map that will give the character special powers and maybe a cute little monster that needs to be slashed by the main character in order to win the game …

All posts from this series:

If you are interested in reading more about procedural texture generations and various noise functions, you could read Texturing and Modeling: A Procedural Approach by D. S. Ebert, F. K. Musgrave, D. Peachey, K. Perlin, S. Worley:

If you are interested in learning more about the new C++11 syntax I would recommend reading The C++ Programming Language by Bjarne Stroustrup.

or, Professional C++ by M. Gregoire, N. A. Solter, S. J. Kleper: