-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainbt.py
303 lines (245 loc) · 8.21 KB
/
trainbt.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import bt
import time
import math
import sys
import random
from termcolor import colored
class WalkAction(bt._BaseNode):
def __init__(self, dest):
self.dest = dest
def tick(self, ctx):
agent = ctx.target
if agent.pos == self.dest:
return bt.OK
agent.begin_walkToward(self.dest)
return bt.RUNNING
class WalkAwayFromEnemy(bt._BaseNode):
def tick(self, ctx):
agent = ctx.target
agent.begin_retreating()
return bt.RUNNING
class AttackEnemy(bt._BaseNode):
def tick(self, ctx):
agent = ctx.target
targets = agent.nearbyThings(agent.attack_range)
if not targets:
return bt.FAIL
else:
if agent.begin_attacking(targets[0]):
return bt.RUNNING
else:
return bt.FAIL
class Rest(bt._BaseNode):
def tick(self, ctx):
ctx.target.begin_resting()
class IsEnemyNear(bt._BaseNode):
def tick(self, ctx):
agent = ctx.target
things = agent.nearbyThings(agent.attack_range)
if things:
return bt.OK
else:
return bt.FAIL
class IsHealthyCheck(bt._BaseNode):
def tick(self, ctx):
if ctx.target.health >= 50:
return bt.OK
else:
return bt.FAIL
class Thing(object):
pos = 0
class Agent(object):
char = 'x'
pos = 0,0
walking_speed = 2.0
health = 100
dead = False
attack_range = 3
attack_rate = 1.0
attack_damage = (1, 4)
rest_rate = 0.5
restore_amount = (2, 4)
target_pos = None
def __init__(self, world):
self.states = set()
self.world = world
def tick(self, context):
for state in list(self.states):
m = getattr(self, 'tick_{0}'.format(state), lambda x:None)
m(context)
def nearbyThings(self, max_distance):
near = []
for thing in self.world['things']:
if thing == self:
continue
d = self.distanceTo(thing.pos)
if d <= max_distance:
near.append((d, thing))
near = sorted(near, key=lambda x:x[0])
return [x[1] for x in near]
def distanceTo(self, pos):
dir_x = float(pos[0]) - self.pos[0]
dir_y = float(pos[1]) - self.pos[1]
return (dir_x**2 + dir_y**2)**0.5
def begin_attacking(self, what):
# XXX the math is probably wrong
if self.distanceTo(what.pos) <= self.attack_range:
self.states.add('attacking')
self.punching_target = what
self.begin_walkToward(what.pos)
return True
if 'attacking' in self.states:
self.states.remove('attacking')
return False
def tick_attacking(self, context):
if self.distanceTo(self.punching_target.pos) > self.attack_range:
self.states.remove('attacking')
return
attack_timer = getattr(self, 'attack_timer', 0)
attack_timer -= context.time_delta
if attack_timer < 0:
self.punching_target.bedamaged(random.randint(*self.attack_damage))
self.attack_timer = self.attack_rate + attack_timer
else:
self.attack_timer = attack_timer
def begin_resting(self):
# XXX the math is probably wrong
self.states.add('resting')
def tick_resting(self, context):
if len(self.states) > 1:
self.states.remove('resting')
return
rest_timer = getattr(self, 'rest_timer', 0)
rest_timer -= context.time_delta
if rest_timer < 0:
self.restorehealth(random.randint(*self.restore_amount))
self.rest_timer = self.rest_rate + rest_timer
else:
self.rest_timer = rest_timer
def begin_retreating(self):
self.states.add('retreating')
def tick_retreating(self, context):
nearby = self.nearbyThings(self.attack_range + 2)
if nearby:
nearest = nearby[0]
v = self.unitVector(nearest.pos, self.pos) * self.attack_range
if v == (0,0):
v = (math.randint(0,1), math.randint(0, 1))
target = self.pos[0] + v[0], self.pos[1] + v[1]
self.begin_walkToward(target)
else:
self.states.remove('retreating')
def begin_walkToward(self, pos):
self.target_pos = pos
self.states.add('walking')
def tick_walking(self, context):
# XXX no obstacles handled and the math is probably wrong
v = self.unitVector(self.pos, self.target_pos)
distance_walked = context.time_delta * self.walking_speed
distance_left = self.distanceTo(self.target_pos)
distance_walked = min([distance_walked, distance_left])
amt_x = math.copysign(min([abs(v[0]), abs(v[0]) * distance_walked]), v[0])
amt_y = math.copysign(min([abs(v[1]), abs(v[1]) * distance_walked]), v[1])
self.pos = amt_x + self.pos[0], amt_y + self.pos[1]
if self.pos == self.target_pos:
self.states.remove('walking')
def unitVector(self, a, b):
dir_x = float(b[0]) - a[0]
dir_y = float(b[1]) - a[1]
total = abs(dir_x) + abs(dir_y)
try:
amt_x = math.copysign(abs(dir_x) / total, dir_x)
except ZeroDivisionError:
amt_x = 0
try:
amt_y = math.copysign(abs(dir_y) / total, dir_y)
except ZeroDivisionError:
amt_y = 0
return amt_x, amt_y
def bedamaged(self, amount):
self.health -= amount
if self.health <= 0:
self.health = 0
self.dead = True
def restorehealth(self, amount):
self.health += amount
if self.health > 100:
self.health = 100
class EngineContext(object):
def __init__(self, time_delta):
self.time_delta = time_delta
def displayBoard(agents, h=10, w=10):
mapping = {}
for a in agents:
color = 'green'
if a.health <= 0:
color = 'red'
elif a.health <= 50:
color = 'yellow'
elif a.health <= 75:
color = 'white'
char = colored(a.char, color)
mapping[(int(a.pos[0]), int(a.pos[1]))] = char
print '{char}: {a.health} {states}'.format(char=char, a=a, states=list(a.states))
for r in xrange(h):
if r == 0:
sys.stdout.write('0' * (w+1) + '\n')
for c in xrange(w):
if c == 0 or c == w-1:
sys.stdout.write('0')
char = mapping.get((r,c), ' ')
sys.stdout.write(char)
sys.stdout.write('\n')
if r == h-1:
sys.stdout.write('0' * (w+1) + '\n')
if __name__ == '__main__':
tree = bt.BehaviorTree(
bt.Priority('behave', [
bt.Sequence('handle enemy', [
IsEnemyNear(),
bt.Priority('fight or flight', [
bt.Sequence('flight', [
bt.Inverter(IsHealthyCheck()),
WalkAwayFromEnemy(),
]),
bt.Sequence('fight', [
IsHealthyCheck(),
AttackEnemy(),
])
])
]),
bt.Sequence('be healthy', [
bt.Inverter(IsHealthyCheck()),
Rest(),
]),
bt.Sequence('walk around', [
bt.WaitAction(1),
bt.MemSequence('walk around', [
WalkAction((2,0)),
WalkAction((2,2)),
WalkAction((0,2)),
WalkAction((2,4)),
]),
]),
])
)
world = {'things': []}
agent1 = Agent(world)
agent2 = Agent(world)
agent2.pos = 8, 4
agent2.char = 'T'
agent2.attack_damage = (3, 5)
world['things'].extend([agent1, agent2])
blackboard1 = bt.Blackboard()
blackboard2 = bt.Blackboard()
last_time = time.time()
while True:
tree.tick(agent1, blackboard1)
tree.tick(agent2, blackboard2)
now = time.time()
ctx = EngineContext(now - last_time)
agent1.tick(ctx)
agent2.tick(ctx)
last_time = now
displayBoard([agent1, agent2])
time.sleep(0.05)