Solarian Programmer

My programming ramblings

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

Posted on July 25, 2012 by Sol

The code for this post is on GitHub:

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:

 1 class PerlinNoise {
 2 	// The permutation vector
 3 	std::vector<int> p;
 4 public:
 5 	// Initialize with the reference values for the permutation vector
 6 	PerlinNoise();
 7 	// Generate a new permutation vector based on the value of seed
 8 	PerlinNoise(unsigned int seed);
 9 	// Get a noise value, for 2D images z can have any value
10 	double noise(double x, double y, double z);
11 private:
12 	double fade(double t);
13 	double lerp(double t, double a, double b);
14 	double grad(int hash, double x, double y, double z);
15 };

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:

 1 // Fill the map with lakes, planes, mountains and snow
 2 void Frame::gen_Perlin(const unsigned int &seed) {
 3 	// Create a PerlinNoise object with a random permutation vector generated with seed
 4 	PerlinNoise pn(seed);
 6 	for(int i = 0; i < _height; ++i) {     // y
 7 		for(int j = 0; j < _width; ++j) {  // x
 8 			double x = (double)j/((double) _width);
 9 			double y = (double)i/((double) _height);
11 			double n = pn.noise(10 * x, 10 * y, 0.8);
13 			// Watter (or a Lakes)
14 			if(n < 0.35) {
15 				mvwaddch(_w, i, j, '~');
16 			}
17 			// Floors (or Planes)
18 			else if (n >= 0.35 && n < 0.6) {
19 				mvwaddch(_w, i, j, '.');
20 			}
21 			// Walls (or Mountains)
22 			else if (n >= 0.6 && n < 0.8) {
23 				mvwaddch(_w, i, j, '#');
24 			}
25 			// Ice (or Snow)
26 			else {
27 				mvwaddch(_w, i, j, 'S');
28 			}
29 		}
30 	}
31 }

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 numbers
2 	game_map.fill_window();

the highlighted line we’ll become:

1 	// Fill the game map with lakes, planes, mountains and snow using a Perlin noise function
2 	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:

Map generation with Perlin noise

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 window
2 void Frame::add(Character &x, int row_0, int col_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:

 1 void Frame::add(Character &x, int row_0, int col_0) {
 2 	if((row_0 >= 0 && row_0 < _height) && (col_0 >= 0 && col_0 < _width)) {
 3 		// Get the element at the target position:
 4 		char target = mvwinch(_w, row_0, col_0);
 5 		// If the target position is watter, wall or snow stop the character to go trough it
 6 		if (target == '~' || target == '#' || target == 'S') {
 7 			return;
 8 		}
 9 		erase(x);
10 		mvwaddch(_w, row_0, col_0, x.symbol());
11 		x.pos(row_0, col_0);	
12 	}
13 }

Let’s run the code:

Main character marking the map

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 2nd edition:

comments powered by Disqus