You now probably have a pretty good idea of what AI is. So let's focus for a while on what you can do with it. More specifically, we will talk about the role(s) of AI in gaming. Here are some of the major uses of AI in games today.
Pathfinding. This is one of the biggest topics for game developers, especially those who are new to AI. Pathfinding is the act of finding a path between one point and another. In a game like Diablo (or in our example iso_world.fla file from the last chapter), you click to walk to a location. If there is an object in your way, then you walk around it. In most advanced games of this sort, the entire path that you will walk is calculated at the moment you click (rather than as you walk).
Pathfinding also works for your enemies. Using various pathfinding techniques, an evil critter may be programmed to scamper around objects and toward you.
Although there are many types of pathfinding algorithms, the one that is considered by all authorities to be the best is A* (pronounced "A Star"). This algorithm will be covered in the last section of this chapter.
Level generation. Some games rely on random but intelligent level creation at runtime. For instance, if you play a certain game twice, the level architecture (walls and rooms) may be the same from one game to the next, but the enemy placement and secret items may be in new positions. Alternatively, the entire level may be completely new from one game to the next, as is the maze in this chapter. This type of level generation is driven by an AI. Sometimes the AI may be a popular algorithm known to many developers; at other times it may have been created from scratch for a particular game.
Enemy behavior. Using pathfinding, an enemy may know how to find you, but what does he do when he gets to you? He may hit you with a sword; he may change his mind and run away, or maybe he just wants to chat. In addition to the pathfinding AI, there is a separate AI that controls enemy-behavior options. This can be one of the most complicated components to program in enormous games created for worldwide distribution (or so I've heard). However, an AI of this sort for an RPG in Flash could be much simpler, as the rest of the game would probably be much simpler (as compared with one from the big boys).
Neural networks. A neural network is an AI that can learn. It gives results based on internal numeric parameters that are adjusted in real time. The result is a machine that can behave differently in different situations. This concept has only recently begun being used in games. Imagine some sort of strategy war game you versus the AI, each armed with planes, tanks, ships, and soldiers. If you repeatedly use your planes to attack the enemy, then the AI will learn from this. It will think, "Hey, I need to take measures to prevent more plane attacks. I've got it! I'll send my tanks in to blow up his planes." This makes for a very "human" AI. A pattern is identified and deemed bad, and then measures are taken to disrupt it. Neural networks are also largely used in e-learning applications. For example, an AI in a software application that instructs you in touch typing might be programmed to chastise you if you type naughty words, or to suggest you take a break when it detects that you are pounding on all the keys at once.
Turn-based games. An AI can be written to play turn-based games such as checkers and chess. There are many different levels of AI for these types of games. The main two are ones that look at the board now and just decide what the next best move is; and ones that form strategies, have a sense of history, and look ahead. You can find a lot of AI algorithms (not many in ActionScript) on the Internet for games like chess, checkers, Connect Four, and Scrabble.
Custom logic. Any of the types of AI I've already mentioned can make use of custom logic. I list this as a separate category to include the miscellaneous AI uses that don't belong in the other categories. You can use AI to control just about anything in a game, from behavior to colors to volume to repercussions to speed to difficulty. As a basic example, in the Chapter09 directory you'll find a simple game of Pong with an AI. The opponent paddle is intelligent enough to follow the ball. Or you can create an AI to determine when a certain event should happen in a game. For instance, if you think the user is performing too well in a car-racing game (and if you like being cruel to your game players), you might initialize a thunderstorm to make the game a little more difficult.
In the rest of this chapter we'll look at custom AI, AI used to generate a random but perfect maze, and A* pathfinding.