My programming ramblings

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

Posted on July 25, 2012 by Paul

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:

1 // Fill the game map with numbers2 game_map.fill_window();

line 2 from above we’ll become:

1 // Fill the game map with lakes, planes, mountains and snow using a Perlin noise function2 game_map.gen_Perlin(237);

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:

1 // Add a character at a specific position to the window2 voidFrame::add(Character&x,introw_0,intcol_0){3 if((row_0>=0&&row_0<_height)&&(col_0>=0&&col_0<_width)){4 erase(x);5 mvwaddch(_w,row_0,col_0,x.symbol());6 x.pos(row_0,col_0);7 }8 }

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

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.

Disclaimer:All data and information provided on this site is for informational purposes only. solarianprogrammer.com makes no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this site and will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use. All information is provided on an as-is basis. solarianprogrammer.com does not collect any personal information about its visitors except that which they provide voluntarily when leaving comments. This information will never be disclosed to any third party for any purpose. Some of the links contained within this site have my referral id, which provides me with a small commission for each sale. Thank you for understanding.