gale.ai.behavior_tree module
This file contains a behavior tree implementation to model the decision making of autonomous characters through a hierarchy of composable nodes.
Author: Alejandro Mujica (aledrums@gmail.com)
- class gale.ai.behavior_tree.Status(*values)[source]
Bases:
EnumResult of ticking a behavior tree node.
- SUCCESS = 'success'
- FAILURE = 'failure'
- RUNNING = 'running'
- class gale.ai.behavior_tree.Node[source]
Bases:
objectBase class for any node of a behavior tree.
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Action(function)[source]
Bases:
NodeLeaf node that executes a function and reports its result as the node status.
- Parameters:
function (Callable[[Any, float], Status])
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Condition(predicate)[source]
Bases:
NodeLeaf node that evaluates a predicate and succeeds or fails depending on its result.
- Parameters:
predicate (Callable[[Any], bool])
- predicate: Callable[[Any], bool]
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Composite(children)[source]
Bases:
NodeBase class for nodes that group several children nodes.
- Parameters:
children (List[Node])
- class gale.ai.behavior_tree.Sequence(children)[source]
Bases:
_IterativeCompositeRuns its children in order and succeeds only if all of them succeed. It stops (and fails) as soon as one of them fails, and reports RUNNING while the current child is still running, resuming from it on the next tick.
- Parameters:
children (List[Node])
- class gale.ai.behavior_tree.Selector(children)[source]
Bases:
_IterativeCompositeRuns its children in order and succeeds as soon as one of them succeeds. It only fails if all of them fail, and reports RUNNING while the current child is still running, resuming from it on the next tick.
- Parameters:
children (List[Node])
- class gale.ai.behavior_tree.Parallel(children, success_threshold=None, failure_threshold=None)[source]
Bases:
CompositeRuns all of its children on every tick and succeeds or fails according to how many of them succeeded or failed on this tick. Children that already reached SUCCESS or FAILURE are latched and no longer ticked on subsequent calls, so their side effects only run once, until this node itself finishes and resets.
- Parameters:
children (List[Node])
success_threshold (int | None)
failure_threshold (int | None)
- success_threshold: int
- failure_threshold: int
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Decorator(child)[source]
Bases:
NodeBase class for nodes that wrap and alter the behavior of a single child node.
- Parameters:
child (Node)
- class gale.ai.behavior_tree.Inverter(child)[source]
Bases:
DecoratorInverts the result of its child: SUCCESS becomes FAILURE and vice versa. RUNNING is left untouched.
- Parameters:
child (Node)
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Succeeder(child)[source]
Bases:
DecoratorAlways reports SUCCESS once its child finishes, regardless of its actual result.
- Parameters:
child (Node)
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Failer(child)[source]
Bases:
DecoratorAlways reports FAILURE once its child finishes, regardless of its actual result.
- Parameters:
child (Node)
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Repeater(child, times=None)[source]
Bases:
DecoratorRuns its child a fixed number of times (or forever), reporting RUNNING until it has finished all the repetitions.
- Parameters:
child (Node)
times (int | None)
- times: int | None
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.UntilSuccess(child)[source]
Bases:
DecoratorKeeps running its child until it succeeds.
- Parameters:
child (Node)
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.UntilFailure(child)[source]
Bases:
DecoratorKeeps running its child until it fails.
- Parameters:
child (Node)
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.Cooldown(child, duration)[source]
Bases:
DecoratorPrevents its child from being run again until a given amount of time has passed since the last time it finished, either by success or failure. While in cooldown, this node fails immediately.
- Parameters:
child (Node)
duration (float)
- duration: float
- tick(agent, dt)[source]
Execute this node.
- Parameters:
agent (Any) – The agent this behavior tree belongs to. It is passed down to every node so that leaves can inspect/modify it.
dt (float) – Time elapsed (in seconds) since the last tick.
- Returns:
The status of this node after this tick.
- Return type:
- class gale.ai.behavior_tree.BehaviorTree(root)[source]
Bases:
objectWraps a root node so that it can be conveniently ticked as a whole.
Usage example:
- tree = BehaviorTree(
- Selector([
- Sequence([
Condition(lambda agent: agent.can_see_enemy()), Action(lambda agent, dt: agent.attack(dt)),
]), Action(lambda agent, dt: agent.patrol(dt)),
])
) tree.tick(agent, dt)
- Parameters:
root (Node)