The Unbreakable AI

The First Pirate Code AI

I first sat down to create an AI for the original Pirate Code in January of 2009. At the time I didn’t know anything about AI so I did the same thing everyone does, I googled it. But everything I found was about fuzzy logic and decision trees. Also, apparently only first person shooters need AI. So after several days I decided to buy some books. I found two books on AI that both seemed promising and ordered them. But again, I was disappointed. There was a lot of effort put into path finding, which is a serious problem for many games, but I wasn’t worried about that. I just wanted my ships to shoot at each other.

This is one of the oldest known pictures of Pirate Code, taken near the end of 2008. At this point we had no AI.

Eventually I had to code something, so I decided to start simple. I’d pick a random ship, pick a random move, and then do that move. This got the ball rolling. Next I decided to ask each move if they were applicable to the situation before using them. So before using a healing move, I’d ask the move if it was useful. The move would check with the ship to see if the ship was damaged and reply accordingly. Out of this a point system evolved where each move would give itself a score and the highest score would be used.

Limitations

The first problem with this first round of AI was that it was slow. To make up for this I had to limit the number of moves I could look into. The second problem was that it couldn’t look into the future. For example, we couldn’t test out two moves in succession. This became a problem as more actions became available. Both of these problems compounded when a ship was blessed with lot’s of movement and long range cannons. Each move had to be tested on each target tile.

The AI in Pirate Code eventually became quite good, but to this day there’s a noticeable delay while it tries to navigate complex situations with large fleets. Speed is just as important as intelligence.

Over the years I rewrote, entirely, the AI four times. Each time I made it faster but the underlying frame work of scoring each move remained. Then I came up with the big idea that would change everything for Pirate Code 2!

Building a Better AI!

So to bring this full circle, how can I change the rules of the battle without changing the AI? Because the AI no longer asks each move to score itself. Think about how that would work with people. Some people would always so that they are super important. Other people would prefer to stay in the shadows. The same thing happened with the actions. Some actions would always get used because they said they were important. But it was only sometimes true. To really measure the value of an action you have to score the entire battle field, do the action, and then score again after. The difference is the true value of the action.

The new AI can handle complex situations. In this screen shot the attacking ship will do damage to two ships, but will be killed by the counter attack. The AI can determine if this is worth the sacrifice based on how much it values its own ships compared to doing damage to enemy ships.

The beauty of this method is that you can score multiple actions. But to pull off a change like this I would have to be able to rewind the entire battle. The AI in Pirate Code 2 does this for every action, hundreds of times, without skipping a beat. Written in lightning fast C++, the new AI walks through every possible action and takes before and after scores for each. It then keeps the action with the best outcome. The only thing that remains is how to score the battle field. This adds some variety to the AI. For example, some AIs can prefer defensive behavior by scoring its own ships health higher than it scores damaging enemy ships. The reverse is true as well. If an AI places little value on the life of its own ships then it will be blood thirsty. We procedurally generate the AI’s preferences so like everything else in Pirate Code 2, the AI you’re up against will be unique to the world seed.

Hopefully that makes sense, but if not, the only thing you need to take away is that the AI is genuinely good at this game. And if you’re building a strategy game, that’s important. You’ll probably lose to it in a fair fight, but pirates don’t fight fair.

Have a bad experience with game AI? Talk about it below.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *