AI Behaviour & Ecosystems

In this blog, I will be discussing some of the amusing challenges I’ve faced while introducing improvements and new behaviours in both player units and enemies, as well as briefly mentioning how progress is getting on with chapter 2. If you’d like to check out the patch notes for the recent update to the game, you can find them here: https://store.steampowered.com/news/app/805520/view/4638114851904486650

Getting Stuck

A few months ago I talked about AI and how it would get stuck in very specific or unusual circumstances, usually in caverns mode due to its procedural nature. I talked about rivers obstructing the path to an enemy, but that was just the tip of the iceberg.

Unit trying to attack a goblin across a river

A long time ago, I realised that preventing more than one unit from moving on the same tile on the same team, had some undesirable effects. The moment one unit had to go one way, and another had to go the other way, they’d be stuck staring at each other arguing about who was going to move. So I introduced a ‘push’ behaviour, where one dwarf would push another out of their way. This was all very well, but what if the other dwarf couldn’t go anywhere? Consequently, if two dwarves were stuck like this, I would let them go through each other after a couple seconds, as long as they weren’t too stacked up.

It also used to be the case that a stuck unit would constantly try to re-evaluate their path. This is dreadful for performance, so I added a wait or give-up behaviour, depending on the situation. If the obstruction is a wall or another static entity, they give up. If the obstruction is another unit on their team however, they wait.

The problem was this would occasionally cause what I call a rush-hour scenario. Let’s say we have four or more dwarves trying to reach a place across a river, two dwarves ahead of the other two. In the same few moments when the first dwarves encounter the river and give up on their task to look for a new one, the other two are obstructed by the first two. Now we have two dwarves by a river trying to leave, and two dwarves behind them trying to get past. In this situation, nobody can go where they want to, and since their obstructions are units on the same team, they all just wait around like lemons.

Clan of dwarves in a traffic jam to attack a goblin claim

This is one of many specific situations I’ve discovered, had to reason through and try to resolve. It has taken a lot of work and playtesting, and while there are too many of these to mention in one blog post, I believe that work is mostly done. There are still a few that crop up, like an engineer building themself into a wall, but it’s becoming increasingly unlikely.

New Behaviours

A unit that is sufficiently injured may now flee from battle, if there’s a spare bed. Balancing this mechanic was difficult, since ideally you want the player to actually be able to kill the goblins without chasing them all the time.

Jobs are now chosen more intelligently. Miners will look for the closest instructions, engineers will try to build in the order you set the instructions, and warriors will attack the closest buildings within them. This also had some repercussions for AI clans. What if the closest instruction was obstructed? Such a simple problem could cripple an AI team and stop their miners from working. Consequently I introduced a way of remembering if particular locations are unreachable, and avoiding instructions in those locations for a while.

Spiders

Spiders have had substantial development in the recent update. They now select a ‘spider mother’ based on which spider has the highest level. This spider mother then has the ability to spawn burrows, which allows other spiders to burrow beneath walls or obstructions.

The spiders’ ecosystem involves making webs, collecting food from webs and then returning to the nest to place an egg. Spiders were pretty easy to kill however, so now like any living creature would, they flee when they’re injured. Since spiders don’t have beds, they instead rest at their nest, and it costs them their food to do so. This means even if you don’t kill a spider, you still thwart their growth.

A spider nest with a spider mother (centre-left) and several burrows

The cover image at the top of this blog shows one of the pre-patch playtests, while I was working on the spiders. They got completely out of control! One of the main perils of trying to build AI as an ecosystem is finding a good balance of limiting factors, so that they are neither too stunted nor too powerful. This is part of the fun of development however, and the greatest reward of building an ecosystem like this, is that the player’s actions on the environment will greatly impact them. If you open up a spider cave to a vast cavernous space, you can expect their numbers to grow rapidly.

Agent Behaviour (Technical Bit)

Unlike in many games, the AI in TDoG is actually comprised of independent agents. Each unit is an independently thinking machine, with what you might call a bulletin board of jobs to do, that any unit can add to. This method does have some complications, for example a dwarf deciding which class they should be. Currently, each dwarf is given an index (based on age), and decides the best class based on that index, clan size, types of jobs need doing, whether the clan’s lacking specific resources and whether enemies are nearby.

Once a job is chosen by a unit, that job then issues a number of decisions that are required to complete it, a bit like following a manual. These decisions could be moving somewhere, crafting something, using or harvesting something, mining, attacking, resting, assembling and so on. The unit retains a queue of these decisions and tries to carry them out, although not always successfully, i.e. when there’s an obstruction or not enough resources. That unit then has to decide whether to forget that job for the moment, or keep trying, and this is mostly where the AI gets stuck. Up until this point, everything the AI was doing were ‘intentions’, and you know what happens when intentions meet reality.

Progress on Chapter 2

As most members of the team have been busy with work, progress with chapter 2 has been slow. We have all the assets we need, it’s just a case of finishing off the levels.

We have a basic plan for marketing the chapter 2 release; we want to put out all the stops and make a lot of noise. Since this is a big update, we’ve also decided we’re going to put the game on sale. If chapter 2 is when we put our marketing hats on, we also have to make sure that a player’s first steps in the game are as polished and enjoyable as possible, and that they don’t get stuck. This was part of the motivation to make the guide (explained in the previous blog), and is the other major area of development we need to focus on, besides chapter 2 content.

We’ll keep you informed as we continue to make progress. That’s all for now. Until next time, keep your helmets on and your axes sharp!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.