Artificial Intelligence

Now that a foundation exists with regard to object creation and the game world, let’s discuss briefly some of the behaviors that players might encounter in the form of artificial intelligence. The main purpose of AI is to present the player with competition or companionship during the game. The AI routines discussed in this section draw lineage from the classic coin-operated games of the ’70s and ’80s but remain remarkably valid even today for certain types of games. As the complexity of game projects increase, so too will the expectation of challenge to the players. Let’s look at a few basic ways to get enemy actors moving around in Java.

Before getting started, it’s a good idea to note that the algorithms presented here are designed to function within the Actor class’s Update method. This will allow the Actor to automatically know where to be positioned based on the outcome of the algorithm.

Deterministic AI: This is the most basic form of AI in games. There really isn’t a lot to it. Actor classes equipped with this AI update their positions in the world based on a preset increment and continue to do so as long as they are on screen. Certain games such as Asteroids fit this model perfectly. The player is in an asteroid belt, and the big rocks just float by due to gravity or other force. As the player shoots at these asteroids, they exhibit the results of the force, breaking into smaller pieces and taking new vectors. Figure 2.8 shows an image and vector of an object using deterministic AI.

image from book
Figure 2.8: Deterministic AI.

Coding this out is pretty straightforward:

Enemy.setX(Enemy.getX()+Enemy.getXVector());

This code takes the values stored in the class and updates this object in relation to its Vector’s X component only.

Tracking: The tracking algorithm is a much more recognizable algorithm for games that hail from the old days. The fundamental idea behind this algorithm is that the enemy can always ask for the current position of the player, which allows the AI to “see” the player mathematically, then adjust his position based on the player’s current position. As long as the player is skilled and a bit faster than the AI, he should be able to stay away from him. Figure 2.9 shows before and after shots from one frame to the next as the tracking AI updates according to the player’s position.

image from book
Figure 2.9: Tracking AI.

To program a tracking AI, the player’s coordinates must be accessible to the AI that is doing the tracking. An example of how to do this in code follows:

If(player.GetX()>enemy.GetX())    Enemy.setX(enemy.GetX()+=2); If(player.GetX()>enemy.GetX())    Enemy.setX(enemy.GetX()-=2); If(player.GetY()>enemy.GetY())    Enemy.setY(enemy.GetY()+=2); If(player.GetY()>enemy.GetY())    Enemy.setY(enemy.GetY()-=2);

As you can see, the enemy updates his position by 2 in whatever direction needed to move toward the player. This algorithm is particularly useful to motivate a player by chasing him.

Evasion: The evasion algorithm is the opposite of the tracking algorithm just discussed. It takes the current position of the player and uses it to increase the distance between the player and the enemy or target. The basic code structure for this algorithm follows:

If(player.GetX()>enemy.GetX())    Enemy.setX(enemy.GetX()-=2); If(player.GetX()>enemy.GetX())    Enemy.setX(enemy.GetX()+=2); If(player.GetY()>enemy.GetY())    Enemy.setY(enemy.GetY()-=2); If(player.GetY()>enemy.GetY())    Enemy.setY(enemy.GetY()+=2);

Figure 2.10 shows this algorithm in a before-and-after format.

image from book
Figure 2.10: Evasion AI.

The basic idea here is that the enemy tries to keep the maximum distance between himself and the player. A careful player can corner this algorithm, so be careful not to let that happen.

These basic AI algorithms don’t even begin to touch the tip of the iceberg that is game AI. The reader is encouraged to continue algorithmic research to find the best AI algorithms for a current game. Always remember that the game AI has a radical impact on the player’s expectations of the game and the game’s ability to function in a cohesive and fun manner.



Practical Java Game Programming
Practical Java Game Programming (Charles River Media Game Development)
ISBN: 1584503262
EAN: 2147483647
Year: 2003
Pages: 171

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net