-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathgraphviz_demo.py
More file actions
68 lines (50 loc) · 1.75 KB
/
Copy pathgraphviz_demo.py
File metadata and controls
68 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Print the graphviz DOT representation of an attack-drone behavior tree.
Builds an attack-drone tree (circle the target, attack when in range, give
up when too far). Calls `BT.graphviz()` to emit
a DOT string. Paste the output into <https://dreampuf.github.io/GraphvizOnline/>
to render the tree visually.
Demonstrates `BT.graphviz()`, and composition with `While` / `Sequence` / `WhenAny` /
`Wait` / `WaitForever` / `Action`.
Run:
python bonsai-py/examples/graphviz_demo.py
"""
from __future__ import annotations
from dataclasses import dataclass
import bonsai_bt as bt
# Payload-less actions are plain strings; payload variants are frozen
# dataclasses (hashable, immutable, work as bt.Action(...) values).
CIRCLING = "Circling"
FLY_TOWARD_PLAYER = "FlyTowardPlayer"
@dataclass(frozen=True)
class PlayerWithinDistance:
distance: float
@dataclass(frozen=True)
class PlayerFarAwayFromTarget:
distance: float
@dataclass(frozen=True)
class AttackPlayer:
damage: float
def build_tree() -> bt.Behavior:
circling = bt.Action(CIRCLING)
circle_until_player_within_distance = bt.Sequence([
bt.While(bt.Wait(5.0), [circling]),
bt.While(bt.Action(PlayerWithinDistance(50.0)), [circling]),
])
give_up_or_attack = bt.WhenAny([
bt.Action(PlayerFarAwayFromTarget(100.0)),
bt.Sequence([
bt.Action(PlayerWithinDistance(10.0)),
bt.Action(AttackPlayer(0.1)),
]),
])
attack_attempt = bt.While(give_up_or_attack, [bt.Action(FLY_TOWARD_PLAYER)])
return bt.While(
bt.WaitForever(),
[circle_until_player_within_distance, attack_attempt],
)
def main() -> None:
tree_bt = bt.BT(build_tree(), {})
print(tree_bt.graphviz())
if __name__ == "__main__":
main()