Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Finite State Machines and Probability Distributions

A long title! But it promises to be a fairly short tutorial. I'll dispense with the obligatory attempt at a joke to avoid my inevitable embarrassment, and get to the point :) A definition is in order:

- Finite State Machine - A behaviour model wherein the "machine" (object) can act in only a finite number of ways.

As an example, consider your basic "enemy" in a game. We'll assign it three possible states: Seek, flee, and rest. This is the finite set of states for our "machine" (enemy). In order to have our enemy behave realistically, we will have to switch between these various states based on feedback from the game environment. If our enemy is weak (about to die) and the player is close, the enemy should flee. If the enemy is weak but the player is nowhere to be found, he should rest. If the enemy is strong, he should seek the player... you get the picture.

However, if our enemies behaved in this fashion they would be VERY predictable, and the player would have little trouble anticipating and killing them. A little randomness is in order!

- Probability Distribution - A list of possible states with percent probabilities assigned to each.

If we use a probability distribution with our set of three states, we can induce some variability. Some even call it personality. Observe:

  • Seek: 60%
  • Flee: 25%
  • Rest: 15%

    All we have to do is get a random number from 0-100, and determine which behaviour has been selected. This "behaviour selection" should be done at specific intervals, or based on stimuli from the environment. If we changed the state TOO often, nothing would be accomplished... nothing more than what resembles a seizure, anyway :)

    Now, it's likely apparent to you that different probability distributions would be ideal for different circumstances. For example, if the player is close, and the enemy is weak, this distribution would be more appropriate:

  • Seek: 5%
  • Flee: 80%
  • Rest: 15%

    This would result in optimal behaviour (fleeing) on most occasions, but there's still room for variability to keep the player on his toes. So, using variable probability distributions based on a finite number of states can result in very life-like behaviour patterns that'll keep your game players coming back for more!

    Click here to download the source for a little game that demonstrates these principles.