/
bot.py
256 lines (236 loc) · 8.88 KB
/
bot.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
from __future__ import division
import random
import env
engine = env.engine
from avatar import Avatar
from charge import Charge
class Bot(Avatar):
bot_image = False
images = None
mask = None
def __init__(self, matrix, x, y):
Avatar.__init__(self, matrix, x, y)
if not self.bot_image:
image = engine.Surface((50,50), engine.SRCALPHA)
color1 = (100,80,120)
color2 = (200,80,100)
color3 = (200,50,50)
points = [(p[0]*2,p[1]*2) for p in [(12,0),(16,10),(24,13),(16,18),(12,24),(8,18),(0,13),(8,10)]]
engine.draw.polygon(image, color1, points, 0)
points = [(p[0]*2,p[1]*2) for p in [(16,11),(23,13),(16,17)]]
engine.draw.polygon(image, color2, points, 0)
points = [(p[0]*2,p[1]*2) for p in [(8,11),(1,13),(8,17)]]
engine.draw.polygon(image, color2, points, 0)
points = [(p[0]*2,p[1]*2) for p in [(12,8),(15,11),(15,17),(12,20),(9,17),(9,11)]]
engine.draw.polygon(image, color3, points, 0)
image = engine.transform.flip(image, True, True)
self.images['normal'] = image
self.mask = engine.mask.from_surface(self.images['normal'])
self.bot_image = image
self.image = self.images['normal']
self.rect = self.image.get_rect(center=(int(self.x),int(self.y)))
self.targets = [self.matrix.avatars]
self.field = {'u':50,'d':350,'l':50,'r':450}
self.pulse_aim = 'd'
self.nemesis = self.matrix.avatar
self.identity = 'Bot'
self.motion['d'] = True
self.moving = True
self.behaviours = {'roam':self.auto_move, 'node_attack':self.node_damage, 'target':self.target, 'evade':self.evade}
self.behaviours_types = list(self.behaviours.keys())
self.behaviours_special = {'move_reverse':self.move_reverse, 'move_forward':self.move_forward, 'inactive':self.offline, 'stalk':self.stalk}
self.behaviour = self.behaviours['roam']
self.behaviour_type = 'roam'
def behaviour_mode(self):
self.behaviour = self.behaviours[random.choice(self.behaviours_types)]
return
def action(self):
pass
def shot(self):
Avatar.shot(self)
self.behaviour = self.behaviours['evade']
self.behaviour_type = 'evade'
if self.power <= 0.0:
if env.sound:
self.sound['explode'].play()
self.kill()
def auto_move(self):
if not self.moving:
direction_choice = False
while not direction_choice:
direction = random.choice(self.matrix.directions)
if (direction == 'l' and self.node_position[0]>self.field['l']) or \
(direction == 'r' and self.node_position[0]<self.field['r']) or \
(direction == 'u' and self.node_position[1]>self.field['u']) or \
(direction == 'd' and self.node_position[1]<self.field['d']):
direction_choice = True
self.motion[direction] = True
self.moving = True
def momentum(self):
Avatar.momentum(self)
if not self.moving:
if self.behaviour_type == 'move_reverse':
return
if not self.matrix.charge_check(self):
self.behaviour = None
self.behaviour_type = None
else:
self.behaviour = self.behaviours['roam']
self.behaviour_type = 'roam'
def auto_shoot(self):
if random.random() > 0.95:
self.activate_pulse()
def target(self):
if not self.matrix.node_damage_check(self.nemesis):
self.behaviour = None
self.behaviour_type = None
return
if not self.moving:
if self.x == self.nemesis.x:
if self.y < self.nemesis.y:
self.pulse_aim = 'd'
elif self.y > self.nemesis.y:
self.pulse_aim = 'u'
self.activate_pulse()
elif self.y == self.nemesis.y:
if self.x > self.nemesis.x:
self.pulse_aim = 'l'
elif self.x < self.nemesis.x:
self.pulse_aim = 'r'
self.activate_pulse()
else:
if self.x > self.nemesis.x:
direction = 'l'
else:
direction = 'r'
self.motion[direction] = True
self.moving = True
def stalk(self):
if self.moving:
return None
directions = []
if self.x > self.nemesis.x:
directions.append('l')
else:
directions.append('r')
if self.y > self.nemesis.y:
directions.append('u')
else:
directions.append('d')
if directions:
direction = random.choice(directions)
else:
direction = None
if not direction:
self.behaviour = None
self.behaviour_type = None
else:
self.motion[direction] = True
self.moving = True
return direction
def evade(self):
if self.moving:
return None
if self.matrix.separation(self, self.nemesis) > 100:
self.behaviour = None
self.behaviour_type = None
return None
directions = []
if self.x == self.nemesis.x:
if self.node_position[0]>self.field['l']:
directions.append('l')
else:
directions.append('r')
if self.node_position[0]<self.field['r']:
directions.append('r')
else:
directions.append('l')
elif self.y == self.nemesis.y:
if self.node_position[1]>self.field['u']:
directions.append('u')
else:
directions.append('d')
if self.node_position[1]<self.field['d']:
directions.append('d')
else:
directions.append('u')
if directions:
direction = random.choice(directions)
self.motion[direction] = True
self.moving = True
return direction
if self.x > self.nemesis.x:
if self.node_position[0]<self.field['r']:
directions.append('r')
else:
directions.append('l')
else:
if self.node_position[0]>self.field['l']:
directions.append('l')
else:
directions.append('r')
if self.y > self.nemesis.y:
if self.node_position[1]<self.field['d']:
directions.append('d')
else:
directions.append('u')
else:
if self.node_position[1]>self.field['u']:
directions.append('u')
else:
directions.append('d')
if directions:
direction = random.choice(directions)
else:
direction = None
if direction:
self.motion[direction] = True
self.moving = True
return direction
def move_destination(self, position):
pass
def move_forward(self):
if not self.moving:
if self.y > self.field['u']:
self.behaviour = None
self.behaviour_type = None
else:
self.motion['d'] = True
self.moving = True
def move_reverse(self):
if not self.moving:
self.field['u'] = -1000
if self.y < self.field['u']:
self.behaviour = self.behaviours_special['inactive']
self.behaviour_type = 'inactive'
else:
self.motion['u'] = True
self.moving = True
def offline(self):
if self.power > 0.3:
self.behaviour = self.behaviours_special['move_forward']
self.behaviour_type = 'move_forward'
def retreat(self):
self.behaviour = self.behaviours_special['move_reverse']
self.behaviour_type = 'move_reverse'
def node_damage(self, position=None):
if self.power < 0.2 or random.random() > 0.1:
self.behaviour = None
self.behaviour_type = None
return
if not position:
node = self.matrix.node_check(self)
if node and not node.offline and node.id[1] != 1:
charge = Charge(self.matrix, self.x, self.y, 90)
self.matrix.charges.add(charge)
else:
charge = Charge(self.matrix, position[0], position[1], 90)
self.matrix.charges.add(charge)
self.behaviour = self.behaviours['roam']
self.behaviour_type = 'roam'
def update(self):
if not self.behaviour:
self.behaviour_mode()
if self.behaviour:
self.behaviour()
Avatar.update(self)