Post feature RSS S&D: Generating a believable world map

We talked about how do we generate a fantasy world map for Swords & Dungeons.

Posted by on

Hello everyone! I'm the programmer behind The Swords & Dungeons! We have been quietly working to make a minimum playable version of the game. We improved the game so much after the battle demo build. I want to explain all the features we made but writing articles like this takes so much of our time. So I can't possibly write an article for every feature we added. Sorry about that. This week I want to talk about the map generation system.

Ingame screenshot

Current state:

This is the place which you’ll interact with the world. Currently, the map generates grasslands and lakes, forests, mountains, national capitals and roads between them, castles and villages


Terrain generation

Firstly, the game generates a noise map with custom size and seed parameter. This seed is used to generate everything you see in the world. If the seed is the same, everything will be the same. Hopefully, you’ll have an option to enter a seed. Currently, it’s random with every generation. After the game generates the noise map. The game uses it to generate elevation. High elevation means mountain, low means grassland. Every tile on the map stores its elevation and tints its color accordingly. In future versions, elevation will affect the scene that battle will take place. After this first generation, the game generates another slightly shifted noise map. It’s using it for the lake/water generation. If the noise map value bigger than a threshold it generates a water tile. Then the game generates another slightly shifted noise map and uses it to generate forest with the same logic as the water generation.


Capital, castle and village generation

Generator starts with randomly placing 5 capitals and makes sure they’re not too close to each other. Then it randomly places castles but, like capital generation, it makes sure they’re not too close to other castles and capitals. Nomad’s capital, Hiwa, won’t own a castle at the start of the game for story reasons. After placing them, it finds the closest capital and assigns itself. After all that, generator randomly places villages and like similar to castles, assign itself to the nearest castle.

ss3 1

Now it comes to placing roads between the capitals. Currently, the main reason for roads is to make it easier to recognize the environment and get player to know where each capital is. We tried to make every settlement connected but it created too much road. If the whole map is road then it means nowhere is road. It loses its purpose. Anyway, we used A* pathfinding algorithm to calculate the shortest path between all capitals. It’s relatively slow but it gets the job done. We might change the algorithm later to reduce loading time. After the generator finds all the shortest paths, it replaces terrain tiles with road tiles. Road system doesn’t respect terrain because we want to make sure there isn’t a capital that isn’t accessible. Then the generator runs through all road tiles and connects them to each other and settlements. When we tested this generation system, we found that sometimes there are inaccessible castles and villages. So we created a second road algorithm that finds the shortest path between all settlements but it doesn’t generate roads. It just turns impassable tiles with grassland. The video below shows the entire process.

Region generation

We used a pretty basic algorithm for regions that we might change later. Current algorithm works with turns. In the first turn, every castle and capital capture its 1 size surrounding tiles. In the second, they capture 2 size surrounding tiles etc. The algorithm ends when there are no empty tiles available. The final result can be seen in the video below:

You will be able to see and interact with this nation balance. Maybe I’ll write another article about it. Right now, there is no nation AI to back this system up but I’m working on it. You’ll be able to interact with the armies of this nations. They will declare war and make peace with each other. Their armies will siege, ambush or complete a set of missions with a commander as their leader. Anyway enough spoilers ;)

Follow us on Twitter to see what we are up to:

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.