Code snippets for behavior tree patterns. Copy-paste ready.
from behavior_tree import Selector, Sequence, Action, Condition
tree = Selector([
Sequence([
Condition(lambda: see_threat(), name="Threat check"),
Action(lambda: alert(), name="Alert"),
]),
Action(lambda: patrol(), name="Patrol"),
])from behavior_tree import Repeater, Action
tree = Repeater(
Action(lambda: try_connect(), name="Connect"),
max_repeats=3
)from behavior_tree import Parallel, Action
tree = Parallel([
Action(lambda: task_a(), name="Task A"),
Action(lambda: task_b(), name="Task B"),
], success_threshold=2)from behavior_tree import Inverter, Condition
tree = Inverter(
Condition(lambda: is_daytime(), name="Daytime check")
)
# Returns SUCCESS at night, FAILURE during dayfrom behavior_tree import Sequence, Condition, Action
tree = Sequence([
Condition(lambda: is_hungry(), name="Hunger check"),
Action(lambda: find_food(), name="Find food"),
Action(lambda: eat(), name="Eat"),
])Public domain. Use in your projects.