DevDiary #17 – “Location, Location, Location” – April 2021
Hello fellow Guildians,
This time around, our DevDiary #17 will concern itself with a small piece of a very large and complex system of The Guild 3 – the AI. The Guild 3 is a very complicated game, offering a vast amount of actions to take and choices to make at any point in time. In order to present a challenge to the player, the AI has to be able to pick a reasonable course of action, and follow it.
We’ve previously presented some areas of the AI, so this time we will concentrate on one system we recently improved upon for the coming patch: Where the AI places its buildings!
This might initially sound like a trivial task: “Just pick a good spot and build there!”. For a human it’s easy to pick a free spot for a new building, but for an AI, unless completely random placement is acceptable, several considerations have to be taken into account. We recently looked at where our AIs put their businesses and residences and determined that we needed something better. To understand what we arrived at, we must first see where we started:
The old building placement was very simplistic. It basically followed three separate rules, based on what type of building needed to be built:
– Residential buildings are simply built close to existing residential buildings. The average position of all owned residences is taken as a starting point, and nearby streets are searched for a free spot. This will cause families to build clusters of homes. The only time a family will “break out” of their starting area is when they upgrade to a house that can only be built in a city – they will then simply search for a buildable spot until they reach a city, and build there.
– Warehouses are built in the middle of all owned businesses. This follows the same logic as above: Take the average position of all businesses, and start looking for a free spot on the closest street.
– Businesses are built close to existing warehouses. If the family doesn’t own any warehouses, they will try to build close to a market instead. Similar to warehouses and residences, all businesses are close together, with the only outliers happening once city exclusive businesses are getting built.
It is pretty obvious that this will always manage to find some valid spot for placing a building, the construction sites will likely be far from optimal. A fishing hut, for example, should try to build close to a fishing spot, and a herb hut should be placed close to a source of herbs, while a barber or preacher should try to find a spot that is in the middle of a highly populated area.
Therefore, we set out to improve on the old building placement logic, and came up with a few key aspects that should be supported:
– It should be possible to specify individual rules per building type.
– It has to be possible to specify buildings you want to build close to (e.g. resource providers, markets, etc…).
– It has to be possible to specify buildings that you want to avoid (e.g. the competition, arsenals, robbers, etc…).
– There should be no limit on the number of rules you can specify per building type.
– The process of finding a spot that satisfies all those rules should be fairly quick (so that the game doesn’t hitch whenever an AI tries to place a building).
– The process of finding a spot should be fault tolerant (so that if you specify e.g. rules only for buildings that don’t exist on the map yet, we still find a valid spot).
Those are a lot of constraints! After we gave it a lot of thought, we decided to implement this via a building placement heatmap.
Essentially, we are now filling the entire street network with values that represent how ‘good’ the street is. Are buildings on the street that you want to build close to? Increase the value of the street! Are there buildings that you should avoid? Lower the value of the street!
After assigning these values to the entire map, we order the streets by their accumulated value, and then try placing the building on them one after another.
So how does this measure up against the criteria we established above?
To adjust the placement of a building type, the designers can specify a list of rules, which looks like this:
PlacementRules = array
("Grove", false, 2.0),
("Market", false, 2.0),
("*", true, 1.0),
("Warehouse*", true, 2.0)
Each line specifies one building type that is of interest to the placement. Wildcards are supported, so while “Grove” will really only match buildings of the type “Grove”, “Warehouse*” will match buildings of any type that begin with “Warehouse”- like the “Warehouse”, and the “WarehouseCountry”. A simple “*” will match ANY building.
The next parameter, true or false, specifies if we are only interested in our own buildings of that type, or in all buildings of that type. So while the rules above are interested in all groves and markets on the map, the last two lines specify that they only apply to all of our own buildings, and all of our own warehouses.
The final parameter determines how important it is for us to be close to that building type. Higher values make the buildings more “attractive”, while lower values mean that they are less important to us. Negative values can even make buildings repulsive, pushing building placement away!
Entering numbers and hoping that everything will work out can only get you so far, so we also added a development switch that allows us to see the importance heatmap while placing buildings. So what does it look like, for example for the rules above?
This shows the heatmap for placing a herb hut, which uses the example rules.There is a grove at the end of the bright red street, which causes this location to be especially attractive for placement. There is no free spot immediately next to the grove, so the AI would likely place a herb hut at one of the free spots along the orange colored streets.
To demonstrate how e.g. warehouses affect building placements, we added two warehouses in the lower right corner of the depicted area. This moves the “attractiveness” to somewhere in the middle between the grove and the warehouses:
Finally, here’s what the same area looks like when finding a good spot for a fishing hut:
There is a fishing spot located at the end of the dark red road in the upper right corner, so the AI would try to find a spot very close to it.
We hope that DevDiary #17 gave you a bit of insight into a tiny part of the AI code in The Guild 3! As mentioned above, this will be part of the coming patch end of May / beginning of June. The patch will include a lot of other AI improvements, but that is another story.
Purple Lamp Studio