Lord Generic Productions

A Crash Course in Game Design and Production
  Week 7- Artificial Intelligence and the AI Specification

Welcome back! This is the seventh installment in "A Crash Course in Game Design and Production. Like most of the previous lessons, this lesson is in multiple parts. In PART ONE, we'll talk about the basic issues of Artificial Intelligence (AI) as it applies to computer games. In Part TWO, we'll discuss practical issues involved in getting your characters to do what you want them to do, and go discuss the AI issues we will face in our course project. In PART THREE we will write the sixth section of the Design Spec for our Course Project, the AI specification. This is part 1 of 3

Part 1 - Basics of Game AI

Artificial Intelligence is whenever the computer makes decisions based on available data. It could be changing the direction of a moving ball when it hits a wall or having a monster attack as soon as he "sees" the player, or analyzing a defensive strategy to find an exploitable weakness. It doesn't matter. It's all AI.

The Difference between "Real" intelligence and "Artificial" intelligence is who is deciding what to do. When a person is faced with a decision, he looks at what he knows, then using that information analyzes his various options and chooses the one most in line with his agenda. He uses his "Real" intelligence to make the choice. When a computer program is faced with a decision, it checks the relevant system data, checks its options list, ranks each outcome as it pertains to its personality script and its goals and chooses the one that has the highest "outcome score." It's "Artificial Intelligence" because it only knows what you tell it, and can only choose from a limited list of preprogrammed options.

The basic flow of AI programming is:

  1. Tell me what events I need to handle

  2. Tell me how to handle each event
  3. Tell me when an event happens
  4. Let me handle it

There are basically four types of computer game AI: Physics, Player, Enemy, and Opponent AI. Most games will have all four types, with many variations or "flavors" of each type available (and probably active) at the same time.

Physics AI

This is how the universe responds to the objects, enemies, and the player(s). When you design a game, you build a virtual universe wherein your characters move around and interact. That universe needs certain physical laws that everything needs to obey. These things may include:

Movement - Where can the object\player go? What directions can he move in? What is the speed limit? Where is the object\player now? Where is he going? How fast, what direction, What natural forces are working toward\against thatmovement (gravity, friction, viscosity, etc.)?

Rotation - Should he be able to turn now? Is he turning? How much does he want to turn? How far should he be able to turn now? what is helping or hindering the rotation?

Size - What is the maximum size he can be? How big is he now? Is the size changing (to simulate moving toward or away from the player)? Does that size change affect anything else? (speed, acceleration, etc.)

Collisions - Is he currently in collision with anything? (walls, other players, enemies, objects, etc.) Does he stick to the walls or bounce off them?

Multi-Frame Events - Is he exploding, hyperspacing, or in transport for awhile? How many frames does this take, what frame is he on, is he invincible, is he invisible right now? How will this event affect nearby objects?

Object Creation - Do I need to create other objects? What kind? How many? Where can they go? How long should they live?

Lifespan - How long will the object be active? How long has he been active thus far, does he need to die now?

Animation - How many animations does he have, what animation is he currently using, does he need to change animations, what frame is he on, does the animation need to recycle?

Physics AI looks at these issues on an object by object basis and makes adjustments to each object's attributes when required. If the object hits a wall, say, the Physics AI (either a general Physics AI routine to handle everything or a Physics AI specifically for that object type) may temporarily deform the object (change the object image for this frame, say) and change it's direction so next frame it moves away from the wall. It may then send the right image to the sprite engine to draw it to the screen.

There is a lot happening behind the scenes at any given time, and most of it the player or object has no control over. Your "characters" are given some initial parameters, and then are sent on their way, you don't usually know where. Every once in awhile, the Physics AI checks their progress, "corrects" them if necessary, then let them go and do whatever they are supposed to do.

Example: In OidZone, Our Oids need the following initial information to do their job (oiding around):
X Horizontal position in the OidZone
Y Vertical position in the OidZone
dx Horizontal differential offset left or right
dy Vertical differential offset up or down
size Size - large, medium, or small
anim Animation frame 1-30 to start on. Each Oid should start on a different frame.
Active Is this Oid still "alive?"
Type Is this an Oid or a Swarm Mine?

dx and dy determine which of the 12 possible directions the oid will move in. They are respectively the sine and cosine of the angle the oid is moving times the speed of the oid in pixels per frame.

During each frame, the Physics AI:
Adds dx to X and dy to Y to get the new location of the Oid Checks to see if it has reached either screen boundary

  If so, it wraps X or Y or both to the correct screen position
Chooses the next animation frame in the Oid Animation
Checks to see if it hit anything
  Was it the ship?
  Blow it up!
  Was it a shot?
  Pop the shot
  Can this Oid break?
  Erase the original oid
  Change the size
  Create a new oid the same size
  Make both oids move away from ship 90 degrees apart
  Erase the Oid and destroy it
Draw appropriate sprite into Virtual Screen
Go on to next Oid

This same process is followed for EVERY character.

In order for the Physics AI and the other AIs to do their jobs, we need to keep enough data for each object and the universe itself for each AI to make decisions.

Director Sequences
These basically "tell" something who they are, where they are, whether or not they can act now according to your physical laws, and if so, how. There are two types of Director Sequences:

Local Directors
These tell one object or group of objects who they are and how they should act. They are like your boss. When you come to work, they give you your job description and they give you tasks to do.

For example, OidZone has 3 local directors, shipdata, oidata, and shots. They keep the x,y,dx,dy,size,anim... information for the ship, oids, and shots, respectively.

Global Directors
These tell EVERYBODY what to do. They are like the CEO of your company. He tells your boss what to do and consequently, what you are to do. Local directors make decisions based on the situations at hand, and when they need guidance on dealing with a character, they ask the Global Director.

OidZone has one Global Director, angletable. It contains the dx and dy information everybody needs to move in any direction in the OidZone. Whenever something needs to change direction, it looks in angletable for the appropriate differential offsets to move it in the right direction.

As the object or player does his thing in the game universe, the Physics AI adjusts his Local (and maybe his Global) Director(s) as needed to keep him obeying the physical laws you set up.

Player AI

Player AI controls anything the player's character needs to do that he doesn't have direct control over. The player may tell his character to move forward, but the Player AI figures out how to actually DO it. It may initiate the move by setting the appropriate flags in the local director sequence and choose the right animation and image to play get it going. Between the beginning and ending of the movement, it may adjust the player's parameters in response to whatever the Physics AI tries to do to the player.

For example: Say your character needs to jump over a chasm. You tell him to back up and get a running start. When he gets to the edge of the chasm you hit the jump key and the Player AI animates the jump. As he is in the air, the Physics AI slows and pulls him downward. At some point the Player AI realizes that he isn't going to make it and causes him to flail his arms and legs trying to grab hold of the ledge to keep from falling to a grisly death.

At that point you hit the climb button and the Player AI fights the Physics AI to scramble up to the ledge. You hit it too late and Physics wins, causing him to lose his grip and fall to his demise. The Player AI causes him to flail and scream all the way down and make a sickening thud at the end.

The Player AI handles animation sequences, player input, damage, healing, scores, movements, character traits, boredom sequences, and anything else that the player's character does or needs to have happen to him that the player or other AIs have no control over.

Character Traits
Some games require that the player's character have some distinguishing characteristics that draw the player into the game. For example, Duke Nukem has a "dry, cool wit" that makes him an action hero. "Damn...I'm lookin' good!" "Heh-heh! What a mess!" Some heroes need to say or do something amusing or grin maniacally whenever they get their way. "Yeah, that's right! Who's the man?! Who's the MAN?!" The Player AI decides what to say and when to say it.
Boredom Sequences
It's a good idea for the player's character to do something while waiting for the player to do something. Maybe he looks at his watch and taps his foot in annoyance, maybe he makes funny faces at the player, maybe he cracks jokes or taunts the player to get moving. The Player AI decides when it's been too long and chooses the appropriate action from a programmed list.

All these things you have defined in the General Description, Art Spec, or Music and SFX Spec. When you design the Player AI, you make a table of what events prompt which decisions and if there are more than one option for an decision, what other data will determine which choice needs to be made. We'll talk about this in more detail in part 3.

Enemy AI

Enemy AI controls what the "enemies" and other objects "do" during your game. This can be as simple as moving in a constant direction and speed and animating over and over, like the Oids in OidZone, or the SS guys searching Castle Wolfenstein and relentlessly hunting you down like the dog you are. You set up a list of "actions" that the characters can do and what conditions cause them to do it.

This is the most fun AI to program because:

  1. Generally, it's pretty easy to do

  2. The scripts generally aren't very complicated

  3. It's neat to watch your "little guys" run around and try to do what you want them to

  4. You get to laugh your butt off when what you told them to do is really stupid, and they show you on screen

Most Enemy AI is centered around "Scripts." These are short instruction lists that are interpreted in some way by the Enemy AI "Script Interpreter" (SI) which sets the appropriate values in the character's local director sequence,
which causes it to do it's thing.

For example: In an arcade game the action in the script may be "fly in this direction for 10 frames", the script line may be


The SI would interpret the line and set the object's dx=-3 and dy=0 so it'll move in the right direction (in this case, left!), and set its frame counter to 10 frames. Every frame after that it'll tick down the frame counter and let the object do it's thing until the frame counter hits 0. When it does, the SI will get the next action and interpret it.

Some games may not need a Script Interpreter, maybe a string of if-then-else-endif constructs will suffice for the Enemy AI. Some games (like StoneKeep) require an entire Scripting Language to be developed to handle the complex action\reaction\interaction requirements. Whatever your game requires, you need to create some way to give your little guys a set of instructions to carry out and give them the info they need to make any decisions.

Action and Attack Scripts
Action Scripts tell the object what to do when it isn't attacking the player. It may be that your guard needs to march back and forth along his patrol route and look and listen for trouble, or the wave of attacking ships flies around in formation and gathers its forces for the coming attack. Whatever. You tell them what to do, how long to do it, where to go and when to come back.

Attack Scripts tell the object how to go get the player. Maybe the attacking bird needs to swoop down and poop on the player, or the Evil Mage chooses a powerful spell to cast on the party based on what he knows about them. (If your character is wearing a ring of protection against a chicken spell, and the mage recognizes it (because you stole it from him two levels ago), he might choose the frog spell instead.) Whatever the enemy needs to be able to do to your player, you need to script somehow. How to do what and when. When to advance, when to fight, when to use that killer secret move, when to retreat, when to run away, when to go back to work. (Action Script)

A typical game might have multiple interpreter routines for the different scripts, or one function that you pass the script line for any object to and it returns the appropriate values. Whatever the game requires.

In Part 2, I'll show you clips of the Enemy AI Script Interpreter for StarRanger, and we'll talk about action and attack scheme we'll use for our course project.

 Opponent AI

An ENEMY tries to kill or discourage the player, an OPPONENT tries to achieve the same game goal as the player by doing the same steps and making the same game decisions a real player would need to make. Opponent AI is the "smartest" of all the game AIs. It needs to know everything a real player needs to know to play the game. It needs to know the rules and it needs dynamically make goal-related decisions about every move it makes. It needs to have a Strategy, an Agenda, and a Personality, and these are tougher to program.

Before a human player starts a game, he has already determined:

  1. His game Goal,as well as secondary and tertiary goals.

  2. His opening move
  3. His general attack plan
  4. His general defense plan
  5. What game events will prompt which decisions
  6. What will cause him to panic

And other game specific strategy components. The Opponent AI needs to have all these things programmed into it. All game options, how to analyze his position in the game in relation to his goals, and how to choose the "right" option in any situation. This takes a LOT of programming and playtesting time to tweak the numbers to get them right.
There is a METHOD to the MADNESS.

Some players like to be an ENEMY as well as an OPPONENT. It's fun to win, but it's more fun to kick the other player's butt all over the battlefield. Some players will make informal alliances to gang up on another player and divide the spoils. Some will turn on those players at the first opportune time and take them out as well.(Hitler was an agenda player) While Strategy is concerned with playing and trying to win the game, Agenda is all about making the other player sorry he wanted to play the game in the first place. Players with Agendas don't lose gracefully, they kick and scream and bite and do suicidal things in an attempt to try and take another player out with them.
There is a MADNESS to the METHOD.

Agendas take precedence over strategy at some time in the game. Whenever he has got a comfortable lead and can afford to go after the other player or when the other player has a comfortable lead and THAT JUST TICKS HIM OFF! Play
Monopoly with a group of teenagers and you'll understand what I mean. At some point it seems reasonable to wager everything and buy Boardwalk and put 3 hotels on it JUST BECAUSE THE OTHER GUY WANTS IT.

When you program Agendas you list:

you get the idea.

Personality makes the Opponent AI seem more like a real player. Maybe it talks to you, cheers you on or taunts you mercilessly. When you program an Opponent AI, you ideally would like it to interact with the player some way. You determine what kinds of events would prompt a "personal response." "Heh-Heh! What a Mess!" when he jumps around a corner and tosses a pipebomb at you. "Et Tu, <your name>?" when you stab him in the back. "Are you even trying?" when the StarThieves take your fourth canister in a row. Whatever. Usually, Personality and Agenda are intimately linked. "Oooh, did YOU want that?" when he grabs the golden ring just before you could.

Opponent AI is by leaps and bounds the hardest AI to construct. To get the Strategy part, you teach it to play like you do. You make extensive notes as you play a game as to what items cause what decisions, then "teach" the Opponent AI to do those same kinds of things. Once it can play the game, you have it play 100 or more games against real people and tweak the numbers and decisions to make it play better than you do. Then you have it play against itself and see if it freaks out. Once you get the strategy working, you start on Agendas and then Personality. Depending on the game, this could take a day or a solid year or more to get right.

For our course project, we only need Physics, Player, and Enemy AI. The next part will cover Enemy AI and Script Interpretation in some detail using StarRanger as an example of one way you can approach script interpretation. Then we'll launch into the AI stuff we need to make our little guys move through the maze and reach their goals. Stay Tuned!

  End of Week 7- Artificial Intelligence and the AI Specification

If you have any questions for group discussion or have any other questions, comments or suggestions, email them to me to Pastor@BeRighteous.com

Mail monetary donations large or small to
 Lord Generic Productions 1218 Karen Ave Santa Ana, Ca 92704
  A Crash Course in Game Design and Production - Euphoria Edition
(C) Copyright 1996,2001 Lord Generic Productions - All Rights Reserved