Pages

Tuesday, February 19, 2013

Random River Generation

A few weeks ago, I did a tutorial on generating random dungeons. Today I'll continue the 'tradition' and show you a nifty way of generating random rivers or roads. You can see a modified version of the algorithm running here: River Generator.

To complete this tutorial you'll need a noise generator (Perlin, Simplex, Voronoi, etc...) as well as an implementation of the A* algorithm (or something similar).

Step 1

Generate a tile map. The map should be as big as the bounding box that encapsulates your path. That is, if your path stretches from [10, 10] to [110, 110], your tile map will be 100 x 100.

Step 2

Populate your map with random values based on whichever noise function you chose. Note that you can simply make it completely random but for best results, don't. In this screenshot, I've used Perlin noise to generate a map which I then drew by interpreting each cell's value as the green value.

Step 3

This is the important bit: Set up your A* function to calculate the F costs (distance from your source to your target, where the source is our river's starting point and the target is your river's endpoint) to include each cell's random value. Each cell's F cost will be equal to: distance to the target + cell's random value.

Step 4
Using your AI, build a path between the two points. Because we used the noise function to influence our F costs the path will be interesting and unique, rather than straight and boring.

Further Steps

You can also implement a little recursive function that will 'fill' an area where the river ends by interpreting the height at the endpoint and making the water tile overflow onto any tiles that are lower than your initial tile.