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

Let’s talk about the code. To begin with, the current state is processed. This involves local logic, algorithms, and even function calls to other AIs, such as pattern processing. After the state has been processed, the state counter is updated and the code tests to see if the state is complete. If so, a new state is selected. If the new state needs to be set up, the setup is performed. Finally, a new state count is selected using a random number and the cycle continues.

There are a lot of improvements that you can make. You could mix the state transitions with the state processing, and you might want to use much more involved logic to make state transitions and decisions.

#### Adding More Robust Behaviors with Personality

A personality is nothing more than a collection of predictable behaviors. For example, I have friend with a very “tough guy” personality. I can guarantee that if you say something that he doesn’t like, he’ll probably let you know with a swift blow to the head. Furthermore, he’s very impatient and doesn’t like to think that much. On the other hand, I have another friend who’s very small and wimpy. He has learned that due to his size, he can’t speak his mind because he might get smacked. So he has a much more passive personality.

Of course, human beings are a lot more complex than these examples suggest, but these are still adequate descriptions of those people. Thus, you should be able to model personality types using logic and probability distributions that track a few behavioral traits and place a probability on each. This probability graph can be used to make state transitions. Take a look at Figure 12.11 to see what I’m talking about.

Figure 12.11 Personality distribution for basic behavioral states.

There are four states or behaviors in this model:

  State 1: Attack
  State 2: Retreat
  State 3: Stop
  State 4: Random

Instead of selecting a new state at random as before, you create a probability distribution that defines the personality of each creature as a function of these states. For example, Table 12.2 illustrates the probability distributions of my friends Rex (the tough one) and Joel (the wimpy one).

Table 12.2 Personality Probability Distributions

State | Rex p(x) | Joel p(x)
--- | --- | ---

`ATTACK` | 50% | 15%
`RETREAT` | 20% | 40%
`STOP` | 5% | 30%
`RANDOM` | 25% | 15%

If you look at the hypothetical data, it seems to make sense. Rex likes to attack without thinking, while Joel thinks much more and likes to run if he can. In addition, Rex isn’t that much of a planner, so he does a lot of random things—smashes walls, eats glass, and cheats on his girlfriend—whereas Joel knows what he’s doing most of the time.

This entire example has been totally artificial, and Rex and Joel don’t really exist. But I’ll bet that you have a picture of Rex and Joel in your head, or you know people like them. Hence, my supposition is true—the outward behaviors of a person define their personality as perceived by others (at least in a general way). This is a very important asset to your AI modeling and state selection.

To use this technique of probability distribution, you simply set up a table that has, say, 20-50 entries (where each entry is a state), and then fill the table so that the probabilities are what you want. When you select a new state, you’ll get one that has a little personality in it. For example, here’s Rex’s probability table in the form of a 20-element array—that is, each element has a 5 percent weight:

    int rex_pers[20] = {1,1,1,1,1,1,1,1,1,1,2,2,2,2,3,4,4,4,4,4}

In addition to this technique, you might want to add radii of influence. This means that you switch probability distributions based on some variable, like distance to the player or some other object, as shown in Figure 12.12. The figure illustrates that when the game creature gets too far away, it switches to a non-aggressive search mode instead of the aggressive combat mode used when it’s in close quarters. In other words, another probability table is used.

Figure 12.12 Switching personality probability distributions based on distance.

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.