7. AI in Games

Reference Material

John Funge's Cognitive Modeling Paper (pdf)

Ian Wilson's Emotion Modeling Paper (pdf)

John Laird's Autonomous Synthesis Paper (pdf)

Step by Step toward AI

Artificial intelligence covers a very broad area. This notes is only scratching the surface. We will try to demonstrate the elements ofAI that can be introducedin game design. The sky is the limit.

Exampe 1. Tracking an object.

There is a single player and a single game object. Call it a creature. Our goal is to make the creature chase the player. Both the player and the creature have coordinates. because we have the coordinateinformation, we can calculate the direction of the player and distance and move the creature nearer to the player. Algorithm 1 does this.

Algorithm 1. Pursuit of an object.

Given (Px, Py) - player position
(Cx, Cy) Creature position

begin

// Do x-tracking

if (Px > Cx) then Cx++;
else if (Px < Cx) then Cx--;

// Do y-tracking

if (Py > Cy) then Cy++;
else if (Py < Cy) then Cy--;

end

Exampe 2. Evasion

The inverse of pursuit is evasion. Here we assume that the creature is evading the player.

Algorithm 2. The Evasion Algorithm.

Given (Px, Py) - player position
(Cx, Cy) Creature position

begin

// Do x-tracking

if (Px > Cx) then Cx--;
else if (Px < Cx) then Cx++;

// Do y-tracking

if (Py > Cy) then Cy--;
else if (Py < Cy) then Cy++;

end

A Pursuit and Evasion Game

Make a spider chase (track) a fly. When you press the T key, the tables are turned - the fly chases the spider!
You are the fly. You control it using the numeric keypad.

Program Structure:

Preliminaries:

includes: include files
Defines: (States in which the spider can be in. Attacking and evading

Data Structures: Game imagery, backdrop

Sprites used in the Game: Sprite for the player and the spider.

Main routine: The main is divided into several sections.

Section 1. Put the PC into mode 13h, allocate the double buffer, and load the background image pcx file, which is a spider's web.

Section 2. Load another PCX file that contaoins all the imagery for the fly and the spider.  We then extract the appropriate bit maps from the PCX files and create the sprite objects used in the game.

Section 3. Enter the event loop and perform the first phase of the animation.

Section 4. Implement the chasing algorithm

Section5. call this when the tables are turned. The fly is chasing the spider!

Section 6. If the spider hits the screen boundary, it is pushed back.

Section 7. Code for the player's fly begins.

Section 8. fly hitting the screen boundary problem

Section 9. The fly is animated

Section 10. Control returned to the PC (text mode)

Behavior Element in the Game (Using Fuzzy Logic)

Suppose we want a group of alien spaceships attack our player's creature. We could use our Pursuit algorithm 1. This is not going to be interesting because the creature is doomed from the beginning. It has no chance to escape. So instead of making the attacker home toward the character we can use a random number generator to decide the direction of the attack. This may lead tpo awkward situations. We could go a step further. We select the direction of attack byselecting one of three rules:

Rule 1. Chase the Player's character
Rule 2. Retreat from the Player's character
Rule 3. Move randomly

To use these rules, we will do the following. Select an integer random number between 1 and 3. If the random number selected is i, select rule i. In the case of Rule 3, we would select two more random numbers and use them to determine the direction of the new trajectory, as shown below.

velocity_x = rand()%max_velocity;
velocity_y = rand()%max_velocity;

Moving Creature Using Patterns

The behavior displayed by a creature can be predetermined in the form of a pattern or sequence. An example is the sequence of ritual steps you follow every day before you start the car. Using the same pattern becomes too predictable and too boring. So we can use a random number to pick one from a set of patterns.

Suppose we want our creature to execute certain flight patterns - loops, curls, etc.For this the program can selects, at random, an entry from a table of patterns and plays out that pattern. For example, one can store four patterns, each pattern containng a maximum of 6o instructions. These instructions can bve decoded into directions, and velocity vectors. The numbers 1,2,3,..8 can stand for 8 directions and the number 0 could mean "stay put" and -1 means end of a pattern.

Now a pattern sequence 1,1,1,2,2,2,...777,88 could mean make steps in to the east, 2 steps to the north, etc.

A pattern can be used as a seed for  arandom number generator so that thesequence of random numbers generated can be changed.

State Machines

The state machine allows us build-in some memory. When we use state transition diagrams we can use each state to represent, say, a mode of operation of the machine.

The next state is typically dependent upon the current state and the input. Suppose wedefine three states for a creature: Alive, dead, in the process of dying.

We can now create some code that looks like this.

#define ALIVE 0
#define DEAD 1
#d3fine DYING 2

int alien_state = ALIVE;

//begin main loop

while (!done)
        {
        if (alien_state =ALIVE)
            {
        do some thing(s) with the alien creature
        if (alien has been shot by a player)
                {
                //change the alien state to DYING
                alien_state = DYING;
                }//endif
            } endif alive
        else
        if (alien_state =DYING)
            {
            show death sequence
           //test if death sequence is over
            if (death sequence is done)
            {
                //change the alien state to DEAD
                alien_state = DEAD;
                }//endif death is done
            } endif dying
        else //alien must be dead
        {
        //alien is dead
kill alien datastructure and take alien out of the game

Imparting Personalities to Creatures