O'Reilly logo

Programming Game AI by Example by Mat Buckland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

return false; //send message to global message handler
}
Here is some example output from the WestWorldWithMessaging program.
You can see clearly where the preceding message sequence occurs.
Miner Bob: Goin' to the bank. Yes siree
Elsa: Moppin' the floor
Miner Bob: Depositin' gold. Total savings now: 5
Miner Bob: Woohoo! Rich enough for now. Back home to mah li'l lady
Miner Bob: Leavin' the bank
Miner Bob: Walkin' home
Instant telegram dispatched at time: 4.20062 by Miner Bob for Elsa. Msg is
HiHoneyImHome
Message received by Elsa at time: 4.20062
Elsa: Hi honey. Let me make you some of mah fine country stew
Elsa: Puttin' the stew in the oven
Delayed telegram from Elsa recorded at time 4.20062 for Elsa. Msg is StewReady
Elsa: Fussin' over food
Miner Bob: ZZZZ...
Elsa: Fussin' over food
Miner Bob: ZZZZ...
Elsa: Fussin' over food
Miner Bob: ZZZZ...
Elsa: Fussin' over food
Queued telegram ready for dispatch: Sent to Elsa. Msg is StewReady
Message received by Elsa at time: 5.10162
Elsa: Stew ready! Let's eat
Instant telegram dispatched at time: 5.10162 by Elsa for Miner Bob. Msg is
StewReady
Message received by Miner Bob at time: 5.10162
Miner Bob: Okay hun, ahm a-comin'!
Miner Bob: Smells reaaal goood, Elsa!
Elsa: Puttin' the stew on the table
Elsa: Time to do some more housework!
Miner Bob: Tastes real good too!
Miner Bob: Thank ya li'l lady. Ah better get back to whatever ah wuz doin'
Elsa: Washin' the dishes
Miner Bob: ZZZZ...
Elsa: Makin' the bed
Miner Bob: All mah fatigue has drained away. Time to find more gold!
Miner Bob: Walkin' to the gold mine
Summing Up
This chapter has shown you the skills required to create very flexible and
extensible finite state machines for your own games. As you have seen, the
addition of messaging has enhanced the illusion of intelligence a great deal
— the output from the WestWorldWithMessaging program is starting to
look like the actions and interactions of two real people. What’s more, this
is only a very simple example. The complexity of the behavior you can cre
-
ate with finite state machines is only limited by your imagination. You
don’t have to restrict your game agents to just one finite state machine
82 | Chapter 2
Summing Up
either. Sometimes it may be a good idea to use two FSMs working in paral
-
lel: one to control a character’s movement and one to control the weapon
selection, aiming, and firing, for example. It’s even possible to have a state
itself contain a state machine. This is known as a hierarchical state
machine. For instance, your game agent may have the states Explore,
Combat, and Patrol. In turn, the Combat state may own a state machine
that manages the states required for combat such as Dodge, ChaseEnemy,
and Shoot.
Practice Makes Perfect
Before you dash away and start coding your own finite state machines, you
may find it good practice to expand the WestWorldWithMessaging project
to include an additional character. For example, you could add a Bar Fly
who insults Miner Bob in the saloon and they get into a fight. Before you
write the code, grab a pencil and a sheet of paper and sketch out the state
transition diagrams for each new character. Have fun!
State-Driven Agent Design | 83
Summing Up
This page intentionally left blank.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required