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

#### Basic Patterns

Creating patterns for game objects can be simple, depending on the game object itself. For example, motion control patterns are very simple to implement. Let’s say you’re writing a shoot-’em-up game similar to Phoenix or Galaxian. The alien attackers must follow a left-right pattern and then at some point attack you with a specific attack pattern. This kind of pattern or scripted AI can be achieved using a number of different techniques, but I think the easiest technique is based on interpreted motion instructions, as shown in Figure 12.5.

Figure 12.5 The pattern engine.

Each motion pattern is stored as a sequence of directions or instructions, as shown in Table 12.1.

Table 12.1 A Hypothetical Pattern Language Instruction Set

Instruction | Value
--- | ---

`GO_FORWARD` | 1
`GO_BACKWARD` | 2
`TURN_RIGHT_90` | 3
`TURN_LEFT_90` | 4
`SELECT_RANDOM_DIRECTION` | 5
`STOP` | 6

Along with each directional instruction might be another operand or piece of data that further qualifies the instruction, such as how long to do it. As a result, the pattern language instruction format might look like the following:

    INSTRUCTION OPERAND

`INSTRUCTION` is from the previous list (usually encoded as a single number), and `OPERAND` is another number that helps further define the behavior of the instruction. With this simple instruction format, you create a program (sequence of instructions) that defines the pattern. Then you write an interpreter that feeds from a source pattern and controls the game creature appropriately.

For example, let’s say your pattern language is formatted so that the first number is the instruction itself and the second number indicates how long to perform the motion, in cycles. Creating a square pattern with a spin and stop, as shown in Figure 12.6, would be trivial.

Figure 12.6 A detailed square pattern.

Here’s an example of that in coded `[INSTRUCTION, OPERAND]` format:

    int num_instructions = 6; // number of instructions in script pattern

    // this holds the actual pattern script

    int square_stop_spin[

        1,30, 4,1,     // go forward then turn right

        1,30, 4,1,     // go forward and turn right

        1,30, 4,1,     // go forward and turn right

        1,30,          // go forward and finish square

        6,60,          // stop for 60 cycles

        4,8, };          // spin for 8 cycles

> Note: Of course, you might want to use a better data structure than an array. For example, try using a class or structure containing a list of records in `[INSTRUCTION, OPERAND]` format, along with the number of instructions. That way you could very easily create an array of these structures, each containing a different pattern, and then select a pattern and pass it to the pattern processor.

To process the pattern instructions, all you need is a big `switch()` statement that interprets each instruction and tells the game creature what it’s supposed to do, like this:

    // points to first instruction (2 words per instruction)

    int instruction_ptr = 0;

    // first extract the number of cycles

    int cycles = square_stop_spin[instruction_ptr+1];

    // now process instruction

    switch(square_stop_spin[instruction_ptr])

    {

    case GO_FORWARD: // move creature forward...

        break;

    case GO_BACKWARD: // move creature backward...

        break;

    case TURN_RIGHT_90: // turn creature 90 degrees right...

            break;

    case TURN_LEFT_90: // turn creature 90 degrees left...

            break;

    case SELECT_RANDOM_DIECTION: // select random dir...

            break;

    case STOP: // stop the creature

            break;

    } // end switch

    // advance instruction pointer (2 words per instruction)

    instruction_ptr+=2;

    // test if end of sequence has been detected...

    if (instruction_ptr > num_instructions*2)

       { /* sequence over */ }

And, of course, you would add the logic to track the cycle counter and make the motion happen.

There’s one catch to all this pattern stuff: reasonable motion. Because the game object is feeding off a pattern, it might decide to select a pattern that forces the object to smash into something. If the pattern AI doesn’t take this into consideration, patterns will be followed blindly. As a result, you must have a feedback loop with your pattern AI (as with any AI) that instructs the AI that it has done something illegal, impossible, or unreasonable, and it must reset to another pattern or strategy. This is shown in Figure 12.7.

Figure 12.7 Pattern engine with feedback control.

Stop for a minute and think about the power of patterns. With them, you could record hundreds of moves and flight patterns. Patterns that would be nearly impossible to create in any reasonable amount of time using other AI techniques can be created in minutes with a tool (that you would write), recorded in a file, and then played back in your game. Using this technique, you can make a game creature look as if it’s extremely intelligent. This technique is used by nearly all games, including most fighting games such as Tekken, Soul Blade, Mortal Kombat, and so on.

Furthermore, there’s no need to stop with motion patterns. You could use patterns to control weapon selection, animation control, and so on. There’s no limit to how they can be applied. For an example of patterns in action, take a look at `DEMO12_5.CPP|EXE`, which demonstrates a monster that moves around using a number of patterns and selects a new pattern every so often.

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.