Tricks of the Windows Game Programming Gurus:Making Silicon Think with Artificial Intelligence

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Patterns with Conditional Logic Processing

Patterns are cool, but they’re extremely deterministic. That is, once the player has memorized a pattern, it’s useless. Players can always beat your AI because they know what’s going to happen next. The solution to this problem, and to other problems that pop up with patterns, is to add a bit of conditional logic that selects patterns based on more than random selection, taking into account the conditions of the game world and the actual player. Take a look at Figure 12.8 to see this abstractly.

Figure 12.8 Patterns with conditional logic.

Patterns with conditional logic give you yet one more level of control over your AI models—you can select patterns that contain conditional branches as well as the patterns being selected based on conditional logic. For example, you might add a new instruction to the pattern language that is a conditional logic test:

    TEST_DISTANCE 7

The `TEST_DISTANCE` conditional might work by testing the distance of the player from the object performing the pattern. If the distance is too close, too far, or whatever, the pattern AI engine might change what it’s doing, making for a seemingly more intelligent opponent. For example, you might put a `TEST_DISTANCE` instruction every so many instructions in a standard pattern, like this:

    TURN_RIGHT_90, GO_FORWARD, STOP, ...TEST_DISTANCE,

    ...TURN_LEFT_90,...TEST_DISTANCE, ... GO_BACKWARD

The pattern does its thing, but every time a `TEST_DISTANCE` instruction is encountered, the pattern AI uses the operand following the `TEST_DISTANCE` instruction as a measure to test the player’s position. If the player is getting too far away, the pattern AI stops the current pattern and branches to another pattern. Or possibly better yet, it switches to a deterministic tracking algorithm to get closer to the player. Take a look at the following code:

    if (instruction_stream[instruction_ptr] == TEST_DISTANCE)

    {

    // obtain distance, note that on the test

    // instructions the operand is no

    // longer a time or cycle count

    // but becomes context dependent

    int min_distance = instruction_stream[instruction_ptr];

    // if test if player is too far

    if (Distance(player, object) > min_distance)

        {

        // set system state to switch to track

        ai_state = TRACK_PLAYER;

        // .. or you might just switch to

        // another pattern and hope

         // that the object gets closer

        } // end if

    } // end if

There’s no limit to the complexity of the conditional tests that you can perform in the pattern script. In addition, you may want to create patterns on-the-fly and then use them. One such example is to mimic the player’s motion. You could sample what the player does each time she kills one of your game characters, and then use the same tactic against her!

In conclusion, technology like this (although much more sophisticated) is used in many sports games, such as football, baseball, and hockey, as well as action and strategy games. It allows the game objects to make predictable moves, while still allowing them to “change their minds.”

As an example, `DEMO12_6.CPP|EXE` illustrates the conditional technique. You control a bat creature with the arrow keys, and there is an AI skeleton on the screen. The skeleton follows randomly selected patterns until you get too far away, and then it gets lonely and chases you because it wants your attention. (Reflect on what I just said… I placed an emotional motive on 100 lines of computer code. But isn’t that what it seems like, from a spectator’s point of view? Mr. Turing, are you there?)

### Modeling Behavioral State Systems

At this point, you have seen quite a few finite state machines in various forms—code to make lights blink, the main event loop state machines, and so forth. Now I want to formalize how FSMs (finite state machines) are used to generate AIs that exhibit intelligence.

To create a truly robust FSM, you need two properties:

  • A reasonable number of states, each of which represents a different goal or motive.
  • Lots of input to the FSM, such as the state of the environment and the other objects within the environment.

The premise of “a reasonable number of states” is easy enough to understand and appreciate. We humans have hundreds, if not thousands, of emotional states, and within each of these we may have further substates. The point is that a game character should be able to move around in a free manner, at the very least. For example, you may set up the following states:

  State 1: Move forward.
  State 2: Move backward.
  State 3: Turn.
  State 4: Stop.
  State 5: Fire weapon.
  State 6: Chase player.
  State 7: Evade player.

States 1 to 4 are straightforward, but states 5, 6, and 7 might need substates to be properly modeled. This means that there may be more than one phase to states 5, 6, and 7. For example, chasing the player might involve turning and then moving forward. Take a look at Figure 12.9 to see the concept of substates illustrated. However, don’t assume that substates must be based on states that actually exist—they may be totally artificial for the state in question.

Figure 12.9 A master FSM with substates.

The point of this discussion of states is that the game object needs to have enough variety to do “intelligent” things. If the only two states are stop and forward, there isn’t going to be much action! Remember those stupid remote-control cars that went forward and then reversed in a left turn? What fun was that?

Moving on to the second property of robust FSM AIs, you need to have feedback or input from the other objects in the game world and from the player and environment. If you simply enter a state and run it until completion, that’s pretty dumb. The state may have been selected intelligently, but that was 100 milliseconds ago. Now things have changed, and the player just did something that the AI needs to respond to. The FSM needs to track the game state and, if needed, be preempted from its current state into another one.

If you take all this into consideration, you can create an FSM that models commonly experienced behaviors such as aggression, curiosity, and so on. Let’s see how this works with some concrete examples, beginning with simple state machines and following up with more advanced personality-based FSMs.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.