Dividing a procedurally generated world into countries: a quick and dirty implementation

When I first started developing Winds of Trade, I thought the whole game in terms of cities. You could go with your ships, visiting different ports and doing the usual buy high and sell low stuff.

But after a while that felt a bit unrealistic and not too interesting in terms of gameplay, so I decided that every city in the game had to belong to a country. That opened lots of new possibilities, like having your ships recruited by a ruler because his country was going to war with another; having economic cycles that affect all of the cities of a territory; or having some countries ban certain goods, opening the doors to the wonderful world of smuggling.

That, of course, brought some new challenges. My main two problems were: how to determine what country each city belongs to and how to determine who owns every pixel of virtual land available in the world. Here you can find the incredibly simple, quick and dirty solutions I used to face those problems.

Chapter 1: "Who runs this town?"

Warning: illustrations made by a programmer ahead!

The algorithm I decided to use for deciding who owns what (i.e. what country every city belongs to) is as simple as this:

1) Have your randomly generated map with its randomly placed cities handy. That will be something like this:


2) Decide on a number of countries (in this example, three) and randomly assign a capital city to each one. Here you can see the capital city of Red Country, Green Country and Blue Country:


3) Make a circle around each city that has an owner. Every non-owned city inside that radius will become owned. Repeat the process with larger and larger radii until you have no more non-owned cities. As the old saying goes, an animated gif is worth a thousand words per frame:


And there you go, every city now belongs to a country! But... Who owns each one of the beautiful pixels that make the heightmap of this wonderful world?

Chapter 2: "This pixel is mine!"

Well, once I established who owns every city in the world I proceed to do a similar process in order to determine land ownership. What the algorithm does, basically, is to make a growing spiral out of every city that goes claiming each piece of land until all of it belongs to someone. When doing this humble animation I skipped lots of steps and used squares instead of spirals (that's why the result doesn't look too good) because, honestly, I have very little patience. Sorry about that!


But, as you can see, this incredibly simple algorithm works relatively well. Even if the result doesn't look too natural, that can always be fixed later with some smoothing of the borders and removing very small areas that are isolated from the rest (like the area the Green Country owns in the west part of the northern Blue island) 

Here you can see an example map actually generated in Winds of Trade using these algorithms, where the squares represent cities:


I understand that all of these algorithms are far from being clever or bright, but they were really easy to design and implement and give reasonable results, so I thought that sharing them here could eventually help someone. I hope that's the case!

Comentarios

  1. This algorithm is the pixel-version of a Voronoi diagram, right?

    ResponderEliminar
    Respuestas
    1. I didn't think of it that way but I think you're right! The difference would be that in this case you start with only a few "colored" cells and then spread the color to the nearer ones, but it's quite similar

      Eliminar
  2. I like this a lot. It's really cool. It would be cooler if you went the Dwarf Fortress route and simulated the entire history of founding new cities, claiming territories, fighting over boarders and conquering neighboring lands. But, I can see why you didn't.

    Also, it seems like this system doesn't allow for city-states that are not associated with any country and don't control any territory outside of their city. Or unincorporated land that isn't part of any country.

    ResponderEliminar
  3. I like this a lot. It's really cool. It would be cooler if you went the Dwarf Fortress route and simulated the entire history of founding new cities, claiming territories, fighting over boarders and conquering neighboring lands. But, I can see why you didn't.

    Also, it seems like this system doesn't allow for city-states that are not associated with any country and don't control any territory outside of their city. Or unincorporated land that isn't part of any country.

    ResponderEliminar
    Respuestas
    1. Yes, I agree, that going that route would make much more interesting worlds, but it's also quite harder to implement. Regarding city-states and unclaimed land, you are correct, this doesn't allow it, but I might change the algorithm in the future to take that into account. Thanks for your thoughts!

      Eliminar

Publicar un comentario