Keywords: ai, enemy, spies, tutorial
AI is way under-utilized in BYOND games. It is difficult to create so people downplay its importance and create games that are multiplayer-only. This is why I created a tutorial on AI. Earlier today I came across a blog post by Foomer about the importance of being able to play a game as a single player:

Foomer hit the nail on the head, was right on the money, 100% correct. I could not agree more...

But I could elaborate more =)

Here are some more benefits of having AI in your games (even if it's a multiplayer game):

1. Being able to play against AI (even bad AI) lets people experience part of the game. It's better to hear, "the game is neat but the AI stinks" than to have people log out, unsure if there's any content other than the title screen.

2. AI is hard to develop but it can be an important development tool. You can use AI players to easily test complex gameplay situations that require multiple players.

3. AI players can be used to fill spots. Some games might require even teams or a certain number of players (ex: playing a 6 player map in an RTS). Even in games that require mutltiple players, some people might perfer to play with their friends against a team of bots.

4. Being able to play a game by yourself lets you practice. In many games your success is determined by familiarity with the game (the maps, controls, abilities, etc.). If you're playing for the first time and have to play against live players you'll be at a huge disadvantage.

5. You can use your AI routines to suggest moves to a human player. Turn-based strategy games can be complex and hard to learn. Using AI to make suggestions can help people learn to play the game faster. Even bad AI can give decent suggestions to a clueless player.

AI is hard to program because it is unlike any other part of making a game. It's almost paradoxical: the only way to learn how to make AI is to make AI. If you don't know how, it's hard to get started (but not impossible). My hope is that my AI tutorial will show people that they can understand AI on some level and give them a starting point.

I am working on a tutorial that picks up where the first one leaves off. However, before moving forward I'd like to offer a very practical look at how to apply concepts from the first tutorial. The examples in the first tutorial were all fairly simple and modular so I'm not sure it's clear how to tie ideas together to create AI for a game. Hopefully this helps.

AI Case Study: Spies!

Here is an explanation of how the AI in Spies! works.

Here's what a map looks like in the map editor:

Click to view the full-sized image

The green numbers are used to tell AI players how to reach the documents. We link together green numbers if they:
• are lined up horizontally or vertically
• do not have a dense turf between them (window and doors are objs, so they're okay)
• do not have another green number between them

This is the result:

Click to view the full-sized image

For each green number we can find its neighbors. For example, the neighbors of 8 are 4, 9, and 21. The neighbors of 32 are 12, 18, 31, and 33. AI mobs only walk along these black lines. When they reach a green number they pick one of its neighbors and move to that node next. This keeps pathing simple because they only need to walk in straight lines.

To get to the enemy's documents (or back to your base) the AI mobs follow the numbers in order, either increasing or decreasing. If we start at zero, the spawn point for the blue team, here are arrows showing links pointing towards the higher number.

Click to view the full-sized image

Moving towards higher numbers guarantees progress towards red's documents. The AI mob doesn't have to move towards the neighboring node with the highest number, they just need to move towards a node with a higher number. For example, the blue AI mob starting at zero could move this way:

Down to 20
Right to 21
Right to 24
Right to 35
Right to 37
Right to 38 (grabs documents)

Or they could move this way:

Up to 1
Right to 3
Up to 6
Right to 13
Up to 14
Right to 15
Right to 17
Down to 18
Down to 32
Right to 33
Down to 60 (grabs documents)

Once a mob gets the enemy's documents they reverse direction. Moving towards lower numbers will guide the blue mob back to its starting location (to capture the documents).

While mobs follow these paths they use a state machine to determine how they react to other mobs. When an enemy mob is seen they make a decision to fight, flee, or ignore the mob based on the situation (do they have documents? does their enemy have documents? are they low on health? etc.). Most abilities are used as a reaction to this decision (if they decide to flee they'll drop a smoke bomb and a decoy). In some situations they will temporarily reverse direction (in terms of the green numbers they are following) for a node or two to take a different path.

That's basically all there is to the AI. ~100 lines of code were needed to create the markers and identify which nodes are neighbors. ~170 lines of code were needed to handle movement along these paths and the state machine for the mob's actions.

The power of the AI comes from the variety of abilities that players have and the ability, as the developer, to re-use code. I had already written a proc that makes a mob use the decoy ability, the AI routine just needs to decide when to use it and call src.decoy().