Skip to main content

Algorithm

To help you understand the generation settings, it can help to have an understanding of the generation algorithm. At the highest level, the generation can be broken into two phases: the buildings and people, and then drawing the map.

Buildings and people

The first phase of the generation generates the people, their relationships, as well as the buildings that will exist in the settlement.

Building creation

When buildings are created in this phase, we generate the building name and description, as well as any people who live there. When the building is created, it may have a list of job postings - if it does, we fill as many of those postings with the available unemployed people in the settlement, and keep track of any unfilled postings for later. Afterwards, we try to fill unfilled postings in the settlement with remaining unemployed residents.

Each building has its own internal settings for how it should generate residents - for example residences will generate a family or a house-share, and temples will generate individual people who don't have partners. When creating buildings we keep track of people who have moved out of their parents home, as well as people who are looking for a partner. When creating families/residents, we can take people from these lists.

Initial state

The create settlement form includes some options which force some buildings & people to be created. These are:

  • Factions settings
  • Government settings
  • Religion / military settings
  • Individual buildings settings

First, we decide which factions to create. The faction settings in the create settlement form allows you to specify the number of factions to randomly create, as well as a list of manually defined factions. So initially we randomly generate the requested number of factions, and combine those with the manually defined ones. At this stage, we just create the building for the faction - we don't link members to the faction until the end of this phase.

Similarly, the government settings may configure a building to be created (e.g. a castle or assembly hall). At this stage we create the building, as well as the configured family for the building if appropriate (e.g. the king and queen).

For the religion/military settings, we create buildings until we reach the needed number of those types of buildings.

The individual building settings can configure an initial number of buildings. This is handled similarly to the above, but here we also add the created building information to the dependency graph.

Remaining buildings and people

After the initial state step, we may have some buildings and people already in the settlement, but usually this is well below the population target for the settlement. The rest of this phase of generation uses the dependency graph to determine what buildings to create next. At it's most basic, this step is just a loop:

  1. Ask the dependency graph what building to create next
  2. Create that building (& creating residents)
  3. Repeat steps 1-2 until we reach the population requirement

Post-processing

Once we have all the people in the settlement, we can tidy up some loose ends.

  • Add people to factions based on the membership criteria
  • Generate friendship groups based on interests
  • Generate rivalries

Map

This phase takes the buildings created in the previous stage, and draws the final map.

First, we generate any initial bodies of water (or take the drawn water state and put it on the map).

Road network & districts

The next step is to draw the road network, and place the buildings into relevant districts.

Lots

An area of land surrounded by roads is called a lot, and buildings are placed in these.

The road algorithm essentially places one road at a time, optimising it (e.g. making it longer / moving it so it snaps to an intersection), and makes sure that the road is legal (e.g. roads aren't too close together / roads don't end in bodies of water). When we create a road, if it intersects with another road, we run lot detection. This will find the newly created lots (and potentially the one that was just split in two). We keep track of the lots as we go through generation, with some metadata like has the lot already been used to create buildings.

This step is done in stages (as configured in the settlement layout settings in the create settlement form). There are different types of stages:

District stages

In these stages, we create large lots which form the outline of districts. When one of these lots is created, we decide what kind of district to place in it (based on criteria like the remaining unplaced buildings, and what the lot is next to). Each district type has its own placement logic, but most essentially do:

  1. Place large landmark buildings (e.g. market / temple)
  2. Divide the lot into smaller ones
  3. Place buildings into the lot
  4. Decorate

For a deeper dive into what this looks like, and how the create settlement form parameters affect this, see District road stages.

Suburb stage

As mentioned above, buildings can only be placed in lots - but we want to be able to place buildings on the fringes of settlement. For example, in villages or hamlets, buildings aren't surrounded by roads. To get around this limitation, we just sidestep it - we still place buildings in lots - but we make some of the lot edges invisible.

Unlike the district stages, the suburb stage creates small lots from the outset, and places buildings immediately in those lots.

For a deeper dive into what this looks like, and how the create settlement form parameters affect this, see Suburb stage.

Outer settlement

In this step, we want to place all the buildings that shouldn't appear in the main settlement (e.g. farms), as well as any other landscape features (e.g. farmland / forests).

At the start of this step, we extend the existing main roads until they hit the map border. At this point, we know the lots of land we have outside the main settlement. We then estimate how many lots we need for the number of buildings + landscape features, and then divide the lots into smaller ones until we hit that number.

We then place each of the landscape features, putting the relevant buildings on them, trying to place similar features next to each other on the map.