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

ActivateIfInactive();
//if there are no subgoals and there is still an edge left to traverse, add
//the edge as a subgoal
m_Status = ProcessSubgoals();
//if there are no subgoals present check to see if the path still has edges.
//if it does call Activate to grab the next edge.
if (m_Status == completed && !m_Path.empty())
{
Activate();
}
return m_Status;
}
The Goal_FollowPath::Terminate method contains no logic since there is
nothing to tidy up.
z
TIP When the Raven executable is run, there’s an option to view the goal list of
a selected agent in the menu. See Screenshot 9.1.
The figure is in grayscale but when you run the demo active goals will be drawn
in blue, completed in green, inactive in black, and failed in red. The indenting
shows how the goals are nested.
Goal_MoveToPosition
This composite goal is used to move a bot to any position on a map. Here
is its declaration:
class Goal_MoveToPosition : public Goal_Composite<Raven_Bot>
{
private:
//the position the bot wants to reach
Vector2D m_vDestination;
public:
Goal-Driven Agent Behavior
| 393
Examples of Goals Used by Raven Bots
Screenshot 9.1
Goal_MoveToPosition(Raven_Bot* pBot, Vector2D pos);
//the usual suspects
void Activate();
int Process();
void Terminate(){}
//this goal is able to accept messages
bool HandleMessage(const Telegram& msg);
};
Goal_MoveToPosition is instantiated with the location of the desired destina
-
tion. When the goal is activated it requests a path to that position from the
bot’s path planner. Since time-sliced pathfinding is used, the bot may have
a short wait until the path is formulated so in the interim
Goal_SeekTo
-
Position
is added. (See the section in Chapter 8 called “Preventing the
Twiddling of Thumbs” for further explanation.)
void Goal_MoveToPosition::Activate()
{
m_Status = active;
//make sure the subgoal list is clear
RemoveAllSubgoals();
//requests a path to the target position from the path planner. Because, for
//demonstration purposes, the Raven path planner uses time slicing when
//processing the path requests, the bot may have to wait a few update cycles
//before a path is calculated. Consequently, for appearance sake, it just
//seeks directly to the target position while it's awaiting notification
//that the path planning request has succeeded/failed
if (m_pOwner->GetPathPlanner()->RequestPathToTarget(m_vDestination))
{
AddSubgoal(new Goal_SeekToPosition(m_pOwner, m_vDestination));
}
}
Once the path is created, the path planner will notify the bot via a telegram,
which will be forwarded to any active goals. Therefore
Goal_MoveTo
-
Position
must have the capability to handle messages, enabling it to
respond appropriately — either by adding the follow path goal to its
subgoal list or by signaling failure if the planner reports no path is possible.
The listing is shown below. (Note how the message is forwarded to goals
farther down the hierarchy before this goal attempts to handle it.)
bool Goal_MoveToPosition::HandleMessage(const Telegram& msg)
{
//first, pass the message down the goal hierarchy
bool bHandled = ForwardMessageToFrontMostSubgoal(msg);
//if the msg was not handled, test to see if this goal can handle it
if (bHandled == false)
{
394 | 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