Posted February 18, 2021 by TenFootMole
Being a moderately passionate Dwarf Fortress player, I couldn't avoid the temptation to include a complex world generation algorithm into my game. I've been toying with those for a long time, but the current version is actually the best I've ever done. I think.
Like many other attempts I've made myself or seen around, my implementation of worldgen involves a liberal use of the Perlin noise function. Many layers of Perlin noise are generated for many different purposes, mixed around, modified and then interpreted as values such as "land", "water", "forest", "mountain", etc.
First, let me show off a couple screenshots of the worlds I've been able to make:
Now, here's the algorithm I came up with:
As for areas themselves, I've been able to come up with a relatively quick and dirty way of generating those. Inside each area is a smaller 2D array of cells, each with its own type ("road", "house", etc) and additional properties ("drink_price" for taverns, for instance).
In areas, movement is only vertical and horizontal; this is part homage to MUDs and part practical. See, as you enter a new room in a dungeon, you can see the available paths, but not the rooms those paths lead to. Like this:
I imagine that seeing 5-7 question marks around your character at all times while exploring a dungeon wouldn't be that fun, would it? Seeing 1-4 options, on the other hand, is quite easy to grasp. Besides, it does remind me of the classic 4-directional movement in games like Zork and its derivatives (including MUDs).
So far, there are 3 types of areas in the game: caves, towns and villages. Technically there are also castles, but if I tried entering one - I would get a crash, since they're not actually implemented from the inside. Scary stuff.
Caves are, simply put, random blobs of connected rooms. Each room has a chance to contain some enemies and some loot the player can pick up. This is a good example of a cave layout:
I generate caves by picking random horizontally/vertically adjacent cells and making them into cave cells, then recursively repeating until a certain size is met. Then, a random cell is turned into an exit ("<" on the picture above).
Gameplay-wise, a town is a network of roads with some buildings in most of the adjacent cells. Here's an example of a town:
Towns are interesting. I've thought about them for a while, and I ended up implementing a rather interesting algorithm that works surprisingly well despite its hackiness. Here's how it works:
Villages are basically smaller towns with a different set of buildings. Here is an example of a village:
There's still much to do before this world generator can be called complete. For example, I'd love to figure out a way to make distinct continents or more realistic mountain ranges. However, I do believe that this implementation is more than enough for now - I'd better focus on actual gameplay, combat mechanics, etc. It's never too late to revisit the world generator and add new content.