How a settlement decide what buildings to construct

First it might be good to check out the game mechanics for constructing buildings in a settlement. It can be found in this post: Construct buildings.

In the last update I described how a settlement considers its food situation and take the correct steps to make sure that the civilian population is well fed. What’s nearly as important is how the settlement constructs new buildings. A settlement, especially the capital, will be able to construct a large number of buildings and these buildings do not only affect the settlement itself, some of them influence the whole empire. One example of this is research buildings where research points are collected from all settlements and used by the empire itself to generate new technologies.

Each settlement has a settlement AI and the settlement AI is controlled by a Governor. The Governor is a set of variables that influences what actions the settlement AI prefers. In some cases the settlement AI ignores the Governor but building construction is not one of them. Let’s have a look at the Governor class:

public final String race;		// Available to this race. Can also be All for generic governors.
public final String name;		// Identifying name of governor
public final String preferredTechnology;	// Technology type that Governor requests if it can't build new or upgrade old buildings. Values can be:  Basic, Development, Enchantment, Food or Military

public final double type10;	// Factor market buildings
public final double type20;	// Factor research buildings
public final double type30;	// Factor military production buildings (only used if militaryCapacity = true)
public final double type50;	// Factor walls
public final double type80;	// Factor population buildings
public final double typeTotalCivilian;	// Sum excl. military buildings
public final double typeTotal;		// Total sum
public final double goodFoodSituation;	// preferred foodPerPopulation factor (default 1.5)
public final double poorFoodSituation;	// lowest wanted foodPerPopulation factor (default 1.0)
// Constructor
public Governor(String race, String name, String preferredTechnology, int type10, int type20, int type30, int type50, int type80, int typeTotalCivilian, int typeTotal, double goodFoodSituation, double poorFoodSituation) {
	this.race = race; = name;
	this.preferredTechnology = preferredTechnology;
	this.type10 = type10;
	this.type20 = type20;
	this.type30 = type30;
	this.type50 = type50;
	this.type80 = type80;
	this.typeTotalCivilian = typeTotalCivilian;
	this.typeTotal = typeTotal;
	this.goodFoodSituation = goodFoodSituation;
	this.poorFoodSituation = poorFoodSituation;

Race specifies what race that can use this governor and name is (as it says above) just to identify what governor it is. When a settlement can’t build more buildings (it has reached the maximum amount of buildings) and it can’t upgrade any building to a newer version it send a request for better technology. A research governor will of course ask for better research technology while a military governor might ask for technologies that allows for better defensive buildings.

Then we come to what is interesting for todays post, the building factors. These decide the weight of each building type when a settlement AI wants to start construction of a new building. The algorithm looks like this:

    Calculate the amount of each building type in the settlement
    Calculate how many buildings of each type the settlement wants to have. The formula looks like this:

      Total number of buildings in settlement * ( factor for type / total factor value )
    With the above data we can now calculate the chance that the settlement AI will construct a certain building type. We want the settlement AI to be able to build all types at all times (even if it has to many of a type) but we want to weigh the chance towards the types it has to little of. The formula looks like this:

      Value for each building type: 1/(constructed/wanted)^2
      Chance: ( value for each building type / total value for all building types ) *100 %

In the log it may look something like this:

Decide wanted building type – Settlement Capital (id: 1, uid: 2)
Type: 50 – Currently built: 3, target: 3,00. Factor: 1,00 gives chance: 32,87%
Type: 80 – Currently built: 2, target: 0,75. Factor: 0,14 gives chance: 4,62%
Type: 20 – Currently built: 5, target: 4,80. Factor: 0,92 gives chance: 30,29%
Type: 10 – Currently built: 5, target: 4,95. Factor: 0,98 gives chance: 32,22%
Settlement AI decide (dice: 48,00%, chance: 63,16%) to build a building of type: 20