This is The Mechanic, where Alex Wiltshire invites developers to discuss the inner workings of their games. This time, Crusader Kings 2 [official site].

Meet Domnall, Earl of Osraige. He’s a pretty affable guy. He’s friends with his neighbouring rulers, and all seems peaceful. But he’s also ambitious and a just little crazy, and he’s about to make a big mess of the Emerald Isle.

Domnall is one of the hundreds of characters across Europe, the Middle East and North Africa that Crusader Kings 2 is simulating here in the year 1066. Whether the player is interacting with them or not, they’ll be vying with each other, allying, marrying, dying, giving birth, and generally doing all of the things that your ruler can do. Crusader Kings 2 is a game all about people. It’s about marriages and dependencies, accordances and kinship. And at the heart of how it models all these dense and messy human complexities is a single value that governs the way its little computer aristocrats behave:

THE MECHANIC: Opinions

To the computer, Domnall is long list of opinions: those he has of every other ruler in the game, and those every other ruler has of him. Each is recorded on a scale that goes from boiling hatred at –100 to dumbstruck love at 100, and at any time each might change according to how he acts and how he changes over the course of his life. And they affect everything that he decides to do to other rulers, and what they’ll do to him. This simple idea, that characters’ opinions of each other can differ and that they inform what they’ll do to each other, is a large part of how Crusader Kings 2’s medieval world feels so analogue, so human, despite comprising simply of numbers.

Now, our Domnall really likes three of his neighbours. His opinion of all of them is over 50. His opinion of the Earl Diarmait of Laigin is 25, a lower value but still pretty friendly. In normal circumstances he wouldn’t consider attacking him, since the standard threshold for an AI to consider attacking another ruler of the same religion and culture is an opinion below 20.

But Crusader Kings 2 is never this simple, and opinion is not the only characteristic that makes up Domnall. He also has a set of several traits, just like every other character. Traits cover such positive and negative peculiarities as Flamboyant Schemer, which grants bonuses to a ruler’s ability to scheme and set plots, and Leper, which makes a ruler more or less infertile and reduces their ability to conduct diplomacy.

All these characteristics are visible to the player, but every computer-controlled character also has another set of five hidden personality values. They’re the result of developer Paradox imagining what might influence the behaviour of medieval people: there’s zeal, the extent to which the ruler hates heretics and members of other religions. Honour is about sticking to deals. Greed affects a ruler’s tendency to try to accrue money. Aggression is a ruler’s propensity for warmongering and realising its ambitions, and rationality is how straightforward the ruler behaves.

Domnall has the Ambitious trait, which gives his internal aggression score +40, and the Arbitrary trait, which gives his rationality score –20. These two values are critical for his future. They upend Domnall’s friendly attitude, because the way the game works is to add half of each to the threshold for his willingness to go to war with people. This means that rather than having an opinion threshold of attacking others when his opinion on them is under 20, he’ll happily attack anyone with an opinion he holds of under 50.

Perhaps the Earl Diarmait of Laigin detected a certain drive burning in Domnall’s eye when they went out drinking one time. If he did, perhaps he thought that since their forces are evenly matched, Domnall wouldn’t actually act on it. Too bad for Diarmait, he didn’t factor in Domnall’s irrationality. Most rulers will only go to war against forces that they overpower by 20%. But Domnall’s low rationality tally means he has no such qualms, and now he presses for a Casus Belli: a justification for going to war.

This kind of scenario is being resolved by Crusader Kings 2 all the time across the entire map, whether you’re there to notice (perhaps you’re playing as Diarmait?) or not. Its AI isn’t omniscient, though – while the game is played in realtime, AI actions are taken in pseudo realtime to keep the speed of the game smooth. The game determines which day each character in the game will get its processing time, adding an element of chance, spreading out the load so it will work on lower spec computers.

When it’s time to process a ruler, the game first looks at its long-term strategic goals, and then various sub-systems begin managing different areas of its affairs in order to achieve them. One looks at the council and considers whether there are better candidates available. One commands military matters, setting up plans for armies and executing them. Others evaluate diplomatic actions with other characters and make choices for special events that might affect the ruler, applying its personality values to determine what happens. Some systems don’t fire every time – they might instead only run every year, or when the situation that relates to them changes.

One of those is the system that determines the ruler’s strategic goals, something that’s only invoked when its personality changes – characters sometimes receive new traits as they grow older – or if they marry, their liege is replaced, or someone nearby dies. They assemble a list of provinces they wish to take, characters they’d like to have assassinated, decide to get an heir, develop the size of their army: projects that tend to take months of planning. They have to constantly check these goals against their opinions, though; just as Domnall did, they will only aim to take provinces that their opinion thresholds will support.

For a ruler without an heir, other factors set in. If a woman, age is important because her fertility won’t last. Culture, religion and government are important, too, affecting whether the ruler can have concubines or multiple spouses. Perhaps the ruler is Christian and married, in which case they may ask the pope for a divorce.

Something very interesting about Crusader Kings 2, though, is that despite the apparent complexity of its model of human behaviour, its AI is not as deep as that in strategy stablemate Europa Universalis IV, which uses the same engine. “CK in many ways has the simplest AI of all of our games,” game director Henrik Fåhraeus tells me. As with any game AI, Crusader Kings 2 is routinely criticised for having its characters make apparently stupid or bizarre decisions that tend to lose them dynasties or simply appear ridiculous. But smartness isn’t really the point. “It’s a very conscious decision on our part to prioritise acting in accordance with personality and opinions rather than to be rational and just political about everything,” says Fåhraeus. Look back at history, and decisions are clearly made by actual people. “It should be personal, basically, it’s a game about people and their flaws.”

Scripts are a whole other side of how the game works. Written by Fåhraeus and his team, they pop up and tell stories, giving players and game characters choices to act on as they progress. Some are associated with specific and significant historical figures, such as Charlemagne, recounting his life and events of the period, but as Fåhraeus says, “I think that was probably not a very good decision on our part, because the early game doesn’t last for that long.” A game of Crusader Kings 2 tends to last hundreds of years, and all that special content is soon left in the past, so now the plan is to note and develop interesting archetypical relationships between people, such as that between Charlemagne and his controlling mother, and making procedural stories based on them available all the time.

Another thing Fåhraeus would like to develop is the game’s ability to build stories. He’s been exploring the idea of building a system that can detect the natural emergence of a story in the game (though he admits it’s very hard to do) and guiding emergence, trying to make it happen in a more directed and focused way, a little like a game master. As a first step on that he’s working on a system that maintains special relationships between the player and other characters, such as a nemesis that keeps coming back. “He shouldn’t be immune to death, but the random numbers should be in his favour, because it keeps him around and giving him comebacks and you will feel as a player, ‘Why won’t this guy ever die?!’ That’s what I’m after.” Similarly there might be mentor figures or comic sidekicks, attempting to make some of the hundreds of characters around you stand out and stick around.

Something else he’s working on is breaking down archetypal stories into their essential component pieces in such a way that should they be scattered in the game you’ll notice their pattern. “Like vengeance: how can we break that down into pieces? If we manage to do this correctly at least, you will get a picture when you pour your pieces on the floor. That’s what we’re hoping,” he says.

These are fine ideals which will only make Crusader Kings 2 richer, but all of it is built on the underlying network of relationships that span the gameworld. Opinion is a single figure, making it simple enough for the computer to use, but you can also imagine so much based on the behaviours around that figure – jealousy, admiration, wariness, love, hate, disinterest – because you’re able to colour it with context, personality traits, local and international politics. Domnall, our Irish earl, seems hell-bent on destroying his relationships with his friends. His ambition seems to have made him self-destructive, but maybe this is the start of a powerful dynasty? You can’t help but be curious as to what this bunch of numbers will do next.