.Netterpillars: Artificial Intelligence and Sprites - Artificial Intelligence
(Page 3 of 9 )
AI, for our purposes, is the code in a program that determines the behavior of an object—in other words, how each game object will act upon and react to the game environment in each specific time frame.
The game’s AI is often confused with the game physics, or the simulation as some gamers prefer to call it. While the AI decides what to do, the physics sets the constraints and limits of all players in the system, including your own game character. Some examples will make this distinction clearer:
- Classic pinball games have no AI, only physics.
- In the SimCity game series, when players can’t build a new residential block over a river, it’s the game physics acting. When the Sims start creating their houses, it’s the game AI’s turn.
- In the 3-D maze fever started long ago by Castle Wolfenstein, the game physics tells players that they can’t go through walls, and that their bullets will lower the enemy’s energy until death. The game AI tells the enemy to turn around and fire at players if they shoot him, or if he “hears” them shooting.
A good game project usually has the physics and the AI very well defined and separated, and most times the AI acts just like a player over the game physics. For example, in a multiplayer race game, the players control some cars, and the AI will drive all cars with no pilots, ideally with the same difficulties that the human players have.
AI Categories You can divide the AI into three categories:
- Environmental AI: The kind of AI found in games like SimCity, where the environment (in this example, the city) acts as a lifelike environment, reacting to the player’s input and including some unexpected behavior of its own.
- Opposing player AI: Used in games where the AI will act like a player playing against the human. For example, in chess and other board games, you usually have a very sophisticated AI to play the part of an opponent.
- Nonplayer characters (NPCs): Many games have computer-controlled characters that could be friendly (for example, the warriors that join players in a quest on role-playing games, or RPGs, like Diablo), unfriendly (the monsters and enemies in 3-D mazes), or neutral (the characters are there just to add color to the environment, such as the cooker at the Scumm bar in LucasArts’s The Secret of Monkey Island).
Of course, this division exists only for teaching purposes; sometimes there’s no distinct barrier between the categories.
General AI Considerations Without entering into specific details, there are some things you have to remember when writing AI code:
- Don’t let users find out that the AI has access to their internal data. For example, in games like Microsoft’s Age of Empires, players only see part of the map. Even though the AI can access the full map, the computer-controlled tribes don’t act as if they know all the players’ characters positions.
- Create different levels of difficulty. Having a game with different levels lets players decide how tough they want their opponents to be. In some chess games, for example, players can choose how many future moves the computer will analyze, making the game easier or harder.
- Let the AI fail sometimes. If there’s anything computers do well, it’s executing code exactly the same way over and over. If you are coding a shooter game where the computer can shoot the player, don’t forget to make the computer miss sometimes; and don’t forget that an opponent that never misses is as bad as an opponent that always misses. Players play the game to win, but if they don’t find it challenging, they’ll never play your game again.
- Don’t forget to take into account the environment variables. If players can’t see through the walls, the NPCs must act as if they can’t either. If the computer-controlled adversary has low energy, but is very well protected by walls, he or she won’t run away. If players can hear sounds when someone is approaching or when someone shoots, the NPCs must act like they hear it, too.
- Always add some random behavior. The correct balance of randomness will challenge players more, without making the game so unpredictable that it becomes unplayable. If the game has no element of chance, players can find a “golden path” that will allow them to always win when using a specific strategy.
- Let the AI “predict” players’ moves. In some games, it’s possible to predict players’ moves by analyzing the possibilities based on the current situation, like in a checkers game. But in other games the AI can “cheat” a little, pretending that it predicted the moves of a good human player. For example, if the AI discovers that a player is sending soldiers through a narrow passage in the direction of its headquarters, it can put a sentinel in the passage and pretend that it “had considered” that someone could use that passage. And never forget to give players a chance (they can kill the sentinel, for example)!
Common AI Techniques When talking about AI, it’s usual to hear about neural networks, genetic algorithms, fuzzy logic, and other technical terms. It’s beyond the scope of this book to explain each of these approaches, but those who want to get deeper into the AI topic can look in Appendix A to find more information.
These terms, when applied to games, have the main goals of adding unpredictability to the game actions and helping to create a game that seems to learn players’ tricks and adapt to them to be more challenging. To take a more practical approach, you can obtain these results by applying some simple tricks that will require a lot less effort. In the next sections, we discuss some of these tricks.
Adaptable Percentage Tables
A neural network can be simplified as a table with adaptable results, represented by percentages. For example, when coding a war game, you can create a table to help the AI choose the tactics with which to attack the other players. The AI will use each tactic a set percentage of the time depending on the success rate that is represented by the percentage. The greater the success rate, the more often this tactic will be used. The table can be filled with some initial values, as shown in Table 2-2, and can evolve according to the results of each turn in the game.
| ATTACK TYPE | PERCENTAGE |
| Attack with “V” formation | 20 percent |
| Divide the soldiers in small groups and attack in waves | 20 percent |
| Guerrilla attack—surprise attack with a few soldiers, shoot and run away | 20 percent |
| Attack with full force, in a big group | 20 percent |
| Surround the player and attack from every direction | 20 percent |
Table 2-2. Starting Values for an Adaptable Percentage Table
After each attack, you’ll change the table values according to the results. For example, if the attack is successful, you can add 10 percent to its corresponding percentage column on the table; if not, subtract 10 percent, distributing the difference to the other attack types. After some attacks, the program will “learn” which kind of attack is most efficient against the current player. For example, if the AI uses the first kind of attack (in “V” formation) and it was successful, the table would be updated to the values shown in Table 2-3.
| ATTACK TYPE | PERCENTAGE |
| Attack with “V” formation | 30 percent |
| Divide the soldiers into small groups and attack in waves | 17.5 percent |
| Guerrilla attack—surprise attack with a few soldiers, shoot and run away | 17.5 percent |
| Attack with full force, in a big group | 17.5 percent |
| Surround the player and attack from every direction | 17.5 percent |
Table 2-3. Adaptable Percentage Table Values After a Successful “V” Formation Attack
In the next turn, if the AI tries an attack using the guerrilla tactic and it fails, the table will be updated again, to the values shown in Table 2-4.
Table 2-4. Adaptable Percentage Table Values After a Failed Guerrilla
| ATTACK TYPE | PERCENTAGE |
| Attack with “V” formation | 32.25 percent |
| Divide the soldiers in small groups and attack in waves | 20 percent |
| Guerrilla attack—surprise attack with a few soldiers, shoot and run away | 7.75 percent |
| Attack with full force, in a big group | 20 percent |
| Surround the player and attack from every direction | 20 percent |
And so on . . .
Of course, in a real game it’s better to add many interacting factors. For example, you can choose the best attack for each type of terrain or climatic condition. The more factors you take into account, the better results you’ll have. In games like SimCity, there are dozens (sometimes even hundreds) of factors that contribute to generating the desired result.
This chapter is from Beginning .NET Game Programming in C# by Ellen Hatton et al. (Apress, 2004, ISBN: 1590593197). Check it out at your favorite bookstore today. Buy this book now.
|
Next: Line of Sight >>
More ASP.NET Articles
More By Apress Publishing