-
Notifications
You must be signed in to change notification settings - Fork 0
/
p4_brains.py
169 lines (140 loc) · 4.83 KB
/
p4_brains.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
import random
# EXAMPLE STATE MACHINE
class MantisBrain:
def __init__(self, body):
self.body = body
self.state = 'idle'
self.target = None
def handle_event(self, message, details):
if self.state is 'idle':
if message == 'timer':
# go to a random point, wake up sometime in the next 10 seconds
world = self.body.world
x, y = random.random()*world.width, random.random()*world.height
self.body.go_to((x,y))
self.body.set_alarm(random.random()*10)
elif message == 'collide' and details['what'] == 'Slug':
# a slug bumped into us; get curious
self.state = 'curious'
self.body.set_alarm(1) # think about this for a sec
self.body.stop()
self.target = details['who']
elif self.state == 'curious':
if message == 'timer':
# chase down that slug who bumped into us
if self.target:
if random.random() < 0.5:
self.body.stop()
self.state = 'idle'
else:
self.body.follow(self.target)
self.body.set_alarm(1)
elif message == 'collide' and details['what'] == 'Slug':
# we meet again!
slug = details['who']
slug.amount -= 0.01 # take a tiny little bite
class SlugBrain:
def __init__(self, body):
self.body = body
self.state = 'idle'
self.have_resource = False
def follow_nearest_mantis(self):
try:
self.body.follow(self.body.find_nearest('Mantis'))
except:
print('no more Mantises')
self.idle()
def go_to_nearest_nest(self):
try:
self.body.go_to(self.body.find_nearest('Nest'))
except:
print('no more Nests')
self.idle()
def go_to_nearest_resource(self):
try:
self.body.go_to(self.body.find_nearest('Resource'))
except:
print('no more Resources')
self.idle()
def move(self, coordinates):
self.state = 'move'
self.body.go_to(coordinates)
def idle(self):
self.state = 'idle'
self.body.stop()
def attack(self):
self.state = 'attack'
self.follow_nearest_mantis()
self.body.set_alarm(2)
def build(self):
self.state = 'build'
self.go_to_nearest_nest()
self.body.set_alarm(2)
def harvest(self):
self.state = 'harvest'
if self.have_resource:
self.go_to_nearest_nest()
else:
self.go_to_nearest_resource()
self.body.set_alarm(2)
def flee(self):
self.state = 'flee'
self.go_to_nearest_nest()
self.body.set_alarm(2)
def handle_event(self, message, details):
if message == 'order':
# if the message is order and details is a tuple, then it's a move order
if isinstance(details, tuple):
self.move(details)
# if the message is order and details is a string, then it's a key order
elif isinstance(details, str):
if details == 'i':
self.idle()
elif details == 'a':
print("attack!")
self.attack()
elif details == 'b':
self.build()
elif details == 'h':
self.harvest()
elif message == 'collide':
# if we're attacking and colliding with a mantis, do damage to the mantis
if self.state == 'attack':
if details['what'] == 'Mantis':
details['who'].amount -= 0.05
elif self.state == 'build':
if details['what'] == 'Nest':
details['who'].amount += 0.01
elif self.state == 'harvest':
if details['what'] == 'Resource' and not self.have_resource:
details['who'].amount -= 0.25
self.have_resource = True
elif details['what'] == 'Nest':
self.have_resource = False
elif self.state == 'flee':
if details['what'] == 'Nest':
self.body.amount = 1
elif message == 'timer':
# if an alarm goes off and we're attacking, it's to update which mantis we're following
if self.state == 'attack':
self.attack()
elif self.state == 'build':
self.build()
elif self.state == 'harvest':
self.harvest()
if self.body.amount < 0.5:
print('flee!')
self.flee()
self.body.set_alarm(2)
world_specification = {
'worldgen_seed': 13, # comment-out to randomize
'nests': 2,
'obstacles': 25,
'resources': 5,
'slugs': 5,
'mantises': 5,
}
brain_classes = {
'mantis': MantisBrain,
'slug': SlugBrain,
}