Hi all, Marc here. I have been working on the AI in Card Hunter for the last six months or so. Before Card Hunter, I worked with Jon at Irrational Games / 2K Australia where I did many interesting things, most notably creating the AI engine that powered Bioshock 1 and 2.
The “I” in “AI”
Many of you probably already know what “AI” means in games – those who don’t are probably impatiently waiting for me to stop talking in abbreviations and start making some sense. So here goes: AI stands for “Artificial Intelligence”, and refers to the code that controls computer opponents in games. Or the player’s computer allies. Or any entity in a game that is supposed to demonstrate some sort of intelligent behavior and that isn’t controlled by another human player.
The funny thing is, when it comes to games (as opposed to MIT researcher types who are trying to invent Skynet), the goal of AI programmers isn’t to make the opponents as smart as possible. It wouldn’t be fun for the player if the computer could think 20 moves ahead and could tell you that you were going to lose before you have even played your first move. It also wouldn’t make much sense if the rats you encountered were just as smart as the grand wizards. The goal is to make a challenging opponent, one that is interesting to fight but beatable.
So how does one go about doing such a thing? There are many methods and entire books written on the subject – in this post I will just briefly outline some of the approaches we took in Card Hunter. Be aware that you about to peek behind the curtain! Knowing that the ice dragon just moved to a square because of some number crunching on a server somewhere, and not because he has a secret vendetta against your priest, one that he has clearly demonstrated by killing her during your last three attempts at beating him, may change how you see the game. Or maybe not.
Let’s start with how the AI builds its deck. This bit is easy: it doesn’t have to. In the single player adventures, all the monsters use decks that have been lovingly crafted by (naturally) intelligent game designers, giving each monster a distinct way of interacting with the player. So half my work is already done – great! What’s left? Having the AI move around the board in smart ways and play the appropriate cards at the appropriate times. Still a significant challenge!
What card to play?
At the beginning of its turn, the AI goes through all the cards in all the hands of the monsters/characters it controls and scores each one. Cards that can’t be played at all (because they are out of range, for example) get a negative score. The highest scoring card is played.
How are these scores determined? It’s part experience, part math, and a lot of trial and error. Scores such as this are called heuristics – they are made up of equal parts number crunching and “rules of thumb”. For example, through many play-throughs of the game we have found that is is generally a good idea to play attack cards when it possible to do so. Attack cards will therefore get a higher base score than movement cards. This score will be further influenced by how many targets can be attacked, how much damage will be done, and where the attacker will end up on the board, amongst many other things.
As you can see in the above images, the AI picked Lord Grond’s ability Nimble Strike to get to the left of and behind my warrior and stab him mercilessly in the back.
Where to move?
But why did Lord Grond pick that particular square to attack from? That’s where the board evaluation comes in: it assigns a number from 0 to 100 to every square, 100 being the best. Since we use board evaluations so frequently, we put a little more effort in to making them display nicely:
Since Lord Grond is a melee character, he needs to get really close to the enemy to attack. He also prefers to get behind his opponent, and the scoring reflects that. Squares with bad terrain (like acid) are scored very low; victory squares are scored higher. A square that is a victory square and a good place from which to attack is scored the highest in this situation – unfortunately, Lord Grond can’t reach it on this move, so he went with the second best.
Board evaluations are character specific. Harry the Wizard, a spell caster who likes to keep at range, sees the board very differently:
Sometimes, it is very hard for the AI to know what action is best. Say you have a monster that can both heal and attack. Should that monster stay in the back and heal his allies, or should he be on the front lines, swinging away at his enemies? It really depends on how you want the fight to play out, which is really the scenario designer’s decision to make.
This is why most games give designers a lot of ways to modify an AI’s behavior in the encounter. In Card Hunter, designers can tag AI monsters with a number of pre-defined labels, each of which will influence the AI’s decisions in some way. For example, we have the “healer” tag: When applied to an AI, this AI will change his board evaluation to favor those squares from which he is in healing range of his allies. Tags are generally added when a designer wants achieve some play experience in a map that is hard to achieve with the AI’s default behavior. The designer will then make a request from the AI programmer. Yep, sometimes even AI programmers have to interact with other humans!
When an AI kills you, think of me
This was just a very brief introduction to some of the AI techniques used in Card Hunter. There are of course many more that I didn’t have the space to touch on. So with this new found appreciation I hope that when you play, you will be kind to the AI’s – it took a lot of work to make them what they are. Just kidding! Stab them, light them on fire, and take their stuff! They are secretly happy to know that their deaths served a greater purpose, namely entertaining you, the player.