The AI in Card Hunter

May 23rd, 2012 at 11:47 pm

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.

An AI test map pitting three AI characters (top) against three human ones (bottom). The heavily armored Lord Grond just moved from the center top to the far left.
AI programmers spend a lot of time looking at cryptic debug displays such as this.

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:

The board evaluation for Lord Grond just before he moved to attack.

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:

The board as seen from Harry’s perspective. Harry is the AI character in the top right.

Tweaking behavior

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.

15 Responses to “The AI in Card Hunter”

  1. Can’t wait to be entertained! Nice to see you guys are alive and well!

  2. It’s really neat to see the work that goes down behind the scenes and just what goes into making an AI. As a sucker for TBS games, particularly online and/or card games, I am uber stoked for this to release. Until then… PoxNora… AWAY!

  3. I’d like to see more AI developer diaries. Both as a code monkey and a future player. Educational and… satisfies my inner min/max demons!

  4. Seems to me you guys are at the stage where you could use some beta tester to make sure all of these complicated algorithms actually work. I just want you to know I’m here and I’m available.

  5. This game looks great and I’ve been trying to follow most of the developer’s diaries. I haven’t read them all quite yet, but out of what I have read “When an AI kills you, think of me” is one of the more entertaining sentences I have seen. Good job Marc, I look forward to leaving your AI in the dust!

  6. @Justin Hicks: I hope people don’t take dying to an AI too personally – I’d hate to have to fend off angry player hordes when the game comes out. 🙂

  7. This was a really interesting column. I’d appreciate more AI stuff, if possible.

  8. Well when I kill your AI, think of me 😀

  9. @zipdrive
    I agree. This AI info is interesting stuff.

  10. very nice intro to game ai 🙂
    out of curiosity: are you going to use ai vs ai battles for bug fixing and balancing reasons?
    If so, please do a blog post about these skirmishes 😀

    keep up the good work!

  11. @sarrt: We probably won’t use the AI for balancing because it doesn’t really play at a competitive skill level relative to good human players. However we may well use it for some automated testing down the track.

    Marc can feel free to correct me if he disagrees with the first part of that… 🙂

  12. It is true – Jon can still beat the AI. 🙂 Our attention has definitely been focused on making the AI work well for the single player adventures, and not as a replacement for an MP opponent. That would be a *much* bigger challenge. The reason I use skirmishes a lot for testing is simple: I can customize the battles and decks quite easily and load them up quickly in the game’s interface… ;p

  13. This just had my mind blown. I enjoyed the ending very much too.

  14. I’ve played games in which certain opponents preferentially targeted a particular class. I can imagine a scenario in which “number crunching on a server” and a “vendetta against your priest” are the same thing.

  15. pretty cool

Leave a Reply

#