Game Development Reference
In-Depth Information
an example of a decision tree that could be used to replace the transitional logic for
the patrolling guard above.
The diamonds represent decision nodes, while the rounded rectangles represent
action nodes. This simple decision tree can be applied each time a decision needs to
be made by the guard. Decision trees can easily be shared, and individual nodes can
be shared across different trees. Decision trees are often built from XML data defini-
tions, which are in turn are generated from visual tools over which designers have
control. The programmer writes different decision nodes and action nodes, while
the designer uses them to build the desired behavior.
Writing a simple decision tree system is relatively simple. Let
'
s start with a definition
of decision nodes:
DecisionNode = class(nil,
{
_brain = nil,
_trueNode = nil,
_falseNode = nil,
});
function DecisionNode:Decide()
error(
“
Calling unimplemented function DecisionNode.Decide()
”
);
return nil;
end
function DecisionNode:SetTrueNode(node)
self._trueNode = node;
end
function DecisionNode:SetFalseNode(node)
self._falseNode = node;
end
A decision node has a back reference to the brain, the
true
node, and the
false
node. Since this is an abstract base class, the
Decide()
function is defined with the
same error pattern as above. It will eventually return the action to perform, which it
does by recursively calling the appropriate child. This class also defines functions for
adding a
true
node and
false
node.
Here is the action node definition:
ActionNode = class(DecisionNode,
{
_action = nil;
});