Overworld Generation Part 2: Rivers
To recap quickly, last time I got some basic terrain working with several discrete heights, mountains in the North and a sea in the South. There were ponds randomly scattered through the terrain, but I mentioned that I would really like a river to run through the center of the map.
I’ve now got the river working! Details are inside.
River generation works better with the high-res terrain we have before the blockification and flattening processes described briefly in the previous post, so that’s where we start. Blockification and flattening are run after the river is created.
I want there to be one nice-sized lake and one river flowing into the sea in the South. These will provide settings for story events in the game. I don’t mind there being a few ponds around just for variety too.
So the first thing we do is search for the bodies of water in the map, labeled in the next screenshot. I do this with a depth-first search.
The largest body of water is chosen as the lake, and the North-most one as the river. This improves the chances of the river being a decent length.
Of course, this river is still just a pond at the moment. We need to drag it out to sea somehow. This gets slightly tricky because we want two very different things:
- The river must behave realistically around the terrain, flowing around mountains rather than up them; and
- The river must flow to the South or it will not reach the sea!
Imagine if the “river” pond was surrounded by mountains on all sides - how should it reach the sea?
I came to a compromise: for the sake of level design I prioritize the river reaching the sea over it behaving realistically around terrain. It’s not really a river without connecting with the sea. I think that as long as the river at least looks like it’s behaving correctly around terrain, that should be fine.
So the solution is to make some fake terrain. I start with a conical slope:
If the river followed just this slope it would go out to sea in a straight line. So I combine it with the actual terrain to produce this, which has all the same main features (mountain-tops, valleys, etc.) as the actual terrain, but ensures the river won’t get trapped:
(The actual function used to combine them is an interpolated weighted average. Halfway up the cone, the values of the slope and the terrain are averaged; at the top and the bottom, just the value of the cone is used. The weighting of the terrain and the slope is interpolated between those points.)
This fake terrain isn’t what you’ll see in the game; it’s just to help work out the path for the river.
Then generating the river is just a case of simulating it running down this fake terrain to the bottom, but drawing it onto the actual terrain, as on the following animated GIF:
The end result is exactly what I wanted. The river generally goes around hills and mountains but it can occasionally carve a path through them if there is no alternative. Of course, this happens in real life sometimes; they’re known as gorges.
The prototype is also now able to divide the map up into the discrete screens and determine which areas of floor are contiguous (i.e. are not separated by cliffs and walls).
The next step will be tiling this with floors, water tiles, cliffs/ledges and staircases. It’ll no longer be a prototype, so you can look forward to some in-game screenshots next time!
blog comments powered by Disqus