-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbehaviour_tree.py
206 lines (171 loc) · 5.91 KB
/
behaviour_tree.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import random
class Treenode():
def __init__(self):
self.children = []
self.parent = None
self.curr_child = 0
self.black_board = {'player_distance':[0,0],'target_position':[0,0],'target':None}#save stuff:
def handle_input(self,input):
self.children[self.curr_child].handle_input(input)
def add_child(self, child):
child.parent = self
self.children.append(child)
def update(self):
#self.print_leaf()
for i in range(self.curr_child, len(self.children)):
response = self.children[i].update()
if response == 'SUCCESS':
self.curr_child = 0
return#reset tree to first
elif response == 'RUNNING':#need to remember what was runing for next, I think
self.curr_child = i#shoudl go to this child again in the next tick
return
elif response == 'FAILURE':
pass#next child
self.curr_child = 0
def get_level(self):
level = 0
p = self.parent
while p:
level += 1
p = p.parent
return level
def print_tree(self):
spaces = ' ' * self.get_level() * 3
prefix = spaces + "|__" if self.parent else ""
print(prefix + self.__class__.__name__)
for child in self.children:
child.print_tree()
#for cutscene to do nothing
def deactivate(self):
self.save_children = self.children.copy()
self.children = []
def activate(self):
self.children = self.save_children.copy()
self.save_children = []
def print_leaf(self):
self.children[self.curr_child].print_leaf()
class Sequence(Treenode):#returns FAILURE or RUNNING prematurly
def __init__(self):
super().__init__()
def update(self):
for i in range(self.curr_child, len(self.children)):
response = self.children[i].update()
if response == 'FAILURE':
self.curr_child = 0
return 'FAILURE'#reset
elif response == 'RUNNING':
self.curr_child = i
return 'RUNNING'
self.curr_child = 0
return 'SUCCESS'#reset
class Running_sequence(Treenode):#reset the run on running
def __init__(self):
super().__init__()
def update(self):
for i in range(self.curr_child, len(self.children)):
response = self.children[i].update()
if response == 'FAILURE':
self.curr_child = 0
return 'FAILURE'#reset
elif response == 'RUNNING':
self.curr_child = 0
return 'RUNNING'
self.curr_child = 0
return 'SUCCESS'#reset
class Selector(Treenode):#returns SUCCESS or RUNNING prematurly
def __init__(self):
super().__init__()
def update(self):
for i in range(self.curr_child, len(self.children)):
response = self.children[i].update()
if response == 'SUCCESS':
self.curr_child = 0
return 'SUCCESS'
elif response == 'RUNNING':
self.curr_child = i
return 'RUNNING'
self.curr_child = 0
return 'FAILURE'
class Random_selector(Treenode):#returns SUCCESS or RUNNING prematurly
def __init__(self):
super().__init__()
self.curr_child = None
def random_child(self):
if self.curr_child == None:#select rando child
self.curr_child = random.randint(0,len(self.children)-1)
def update(self):
for i in range(0, len(self.children)):
self.random_child()
response = self.children[self.curr_child].update()
if response == 'SUCCESS':
self.curr_child = None#reset child
return 'SUCCESS'
elif response == 'RUNNING':
return 'RUNNING'
self.curr_child = None
return 'FAILURE'#reset failed
#decorators, can have only one child
class Fail2Success(Treenode):#a decorator, returns sucess even if it failed
def __init__(self):
super().__init__()
def update(self):
response = self.children[0].update()
if response == 'FAILURE':
return 'SUCCESS'
return response
class Success2Fail(Treenode):#a decorator, returns sucess even if it failed
def __init__(self):
super().__init__()
def update(self):
response = self.children[0].update()
if response == 'SUCCESS':
return 'FAILURE'
return response
class Inverter(Treenode):#a decorator, returns sucess even if it failed
def __init__(self):
super().__init__()
def update(self):
response = self.children[0].update()
if response == 'SUCCESS':
return 'FAILURE'
elif response == 'FAILURE':
return 'SUCCESS'
return response
class Run2Success(Treenode):#a decorator, running is turnde to sucess
def __init__(self):
super().__init__()
def update(self):
response = self.children[0].update()
if response == 'RUNNING':
return 'SUCCESS'
return response
class Success2Run(Treenode):#a decorator, running is turnde to sucess
def __init__(self):
super().__init__()
def update(self):
response = self.children[0].update()
if response == 'SUCCESS':
return 'RUNNING'
return response
class FPS_limiter(Treenode):
def __init__(self):
super().__init__()
self.limit = 120
def update(self):
self.limit -= 1
if self.limit < 0:
self.limit = 120
return 'SUCCESS'
return 'FAILURE'
#leaves
class Leaf(Treenode):
def __init__(self,entity):
super().__init__()
self.entity = entity
def update(self):
pass
def handle_input(self,input):
pass
def print_leaf(self):
print(self)