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

calories thinking about a goal until it is absolutely necessary. For instance,
you wouldn’t decide how to open a can of beans until you have it in your
hand or how to tie your shoelaces until your shoes are on your feet.
A goal-directed agent mimics this behavior. Each think update the agent
examines the game state and selects from a set of predefined high-level
goals, or strategies — the one it believes will most likely enable it to sat
-
isfy its strongest desire (usually, to win the game). The agent will then
attempt to follow this goal by decomposing it into any constituent
subgoals, satisfying each one in turn. It will continue doing this until the
goal is either satisfied or has failed, or until the game state necessitates a
change of strategy.
The Return of Eric the Brave
Let’s walk through an example using our favorite game agent Eric, who
has recently found employment in the RPG “Dragon Slayer 2.” Eric’s AI
programmer has created several strategies for him to select from including
Defend from Dragon, Attack Dragon, Buy Sword, Get Food, and Get
Drunk. These strategies represent high-level abstract goals that are com-
prised of smaller subgoals such as Create Path, Follow Path, Traverse
Path Edge, Stab Dragon, Slice Dragon, Run Away, and Hide. Therefore,
to complete a strategy Eric must decompose it into its relevant subgoals
and satisfy each in turn (decomposing them further if necessary).
Eric has just entered the game world and since he’s not carrying a
weapon and is therefore feeling vulnerable, his strongest desire is to find
some sort of pointy stick before a dragon spots him. His “brain” (a special
type of goal capable of making decisions) considers all the strategies avail
-
able and finds that Buy Sword fits the bill admirably, so this is assigned as
the goal to pursue until he elects otherwise. See Figure 9.1.
Eric cannot act upon this goal though because at this level it’s too abstract
and needs to be decomposed — or expanded, if you prefer to think of it
that way — into its constituent subgoals. For the purposes of this example
we’ll assume Buy Sword consists of the subgoals shown in Figure 9.2. To
380 | Chapter 9
The Return of Eric the Brave
Figure 9.1. The Buy Sword goal slotted into Eric’s brain
obtain a sword Eric must first find some gold, then walk to the smithy
where the blacksmith will gladly accept it as payment.
Agents satisfy goals consecutively so Go To Smithy will not be evaluated
until Get Gold has been completed. This is another composite goal, how-
ever, so to complete it Eric must expand the hierarchy further. Get Gold
consists of the subgoals Plan Path (Goldmine), Follow Path, and Pick
Up Nugget. See Figure 9.3.
The Plan Path (Goldmine) goal is satisfied by sending a request to the
path planner to plan a path to the gold mine. It is then removed from the
goal list. The next one Eric considers is Follow Path, which can be further
decomposed into several atomic Traverse Edge goals, each of which con
-
tain the logic required to follow an edge of the path leading to the gold
mine. See Figure 9.4.
Goal-Driven Agent Behavior | 381
The Return of Eric the Brave
Figure 9.2. The Buy Sword goal is expanded into its constituent parts.
Figure 9.3. Expansion of the Get Gold goal

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