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

The Terminate method turns off the steering behaviors and resets the bot’s
maximum speed back to normal.
void Goal_TraverseEdge::Terminate()
{
//turn off steering behaviors
m_pOwner->GetSteering()->SeekOff();
m_pOwner->GetSteering()->ArriveOff();
//return max speed back to normal
m_pOwner->SetMaxSpeed(script->GetDouble("Bot_MaxSpeed"));
}
Let’s now move on to examine some composite goals.
Goal_FollowPath
This directs a bot along a path by repeatedly popping edges from the front
of the path and pushing traverse edge type goals onto the front of its
subgoal list.
Here’s its declaration:
class Goal_FollowPath : public Goal_Composite<Raven_Bot>
{
private:
//a local copy of the path returned by the path planner
std::list<PathEdge> m_Path;
public:
Goal_FollowPath(Raven_Bot* pBot, std::list<PathEdge> path);
//the usual suspects
void Activate();
int Process();
void Terminate(){}
};
In addition to having specific terrain types associated with them, graph
edges may also require a bot to use a specific action to move along them.
For example, an edge may require that an agent fly, jump, or even use a
grappling hook to move along it. This type of movement constraint cannot
be handled by simply adjusting the maximum speed and animation cycle of
the agent. Instead, a unique traverse edge type goal must be created for
each action. The follow path goal can then query the edge flags within its
Activate method and add the correct type of traverse edge goal to its
subgoal list as it pops edges from the path. To clarify, here’s the listing for
the
Activate method:
void Goal_FollowPath::Activate()
{
m_iStatus = active;
Goal-Driven Agent Behavior
| 391
Examples of Goals Used by Raven Bots
//get a reference to the next edge
PathEdge edge = m_Path.front();
//remove the edge from the path
m_Path.pop_front();
//some edges specify that the bot should use a specific behavior when
//following them. This switch statement queries the edge behavior flag and
//adds the appropriate goals(s) to the subgoal list.
switch(edge.GetBehaviorFlags())
{
case NavGraphEdge::normal:
{
AddSubgoal(new Goal_TraverseEdge(m_pOwner, edge, m_Path.empty()));
}
break;
case NavGraphEdge::goes_through_door:
{
//also add a goal that is able to handle opening the door
AddSubgoal(new Goal_NegotiateDoor(m_pOwner, edge, m_Path.empty()));
}
break;
case NavGraphEdge::jump:
{
//add subgoal to jump along the edge
}
break;
case NavGraphEdge::grapple:
{
//add subgoal to grapple along the edge
}
break;
default:
throw
std::runtime_error("<Goal_FollowPath::Activate>: Unrecognized edge type");
}
}
For efficiency, notice how only one edge at a time is removed from the
path. To facilitate this, the
Process method calls Activate each time it
detects its subgoals are complete and the path is not empty. Here’s how:
int Goal_FollowPath::Process()
{
//if status is inactive, call Activate()
392 | Chapter 9
Examples of Goals Used by Raven Bots

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