forked from seanKenkeremath/seanbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wombat.py
324 lines (276 loc) · 9.02 KB
/
wombat.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
log = {}
bored = 0
width = 7
height = 7
path = []
targetCoords = []
targetTile = 0
currState = 0
frontier = []
searched = []
fromTile = []
shotDamage = 10
global arena
def arena():
return currState['arena']
global tile
def tile(x, y):
return arena()[y][x]
global playerTile
def playerTile():
return tile(coords()[0], coords()[1])
global coords
def coords():
return currState['local-coords']
global tileType
def tileType(tile):
return tile['contents']['type']
global isPoison
def isPoison(tile):
return tileType(tile) == 'poison'
global isWall
def isWall(tile):
return isWoodWall(tile) or tileType(tile) =='steel-barrier'
global isWoodWall
def isWoodWall(tile):
return tileType(tile) == 'wood-barrier'
global isOtherWombat
def isOtherWombat(x, y, tile):
return tileType(tile) == 'wombat' and [x,y] != coords()
global hp
def hp(tile):
if 'contents' in tile.keys():
if 'hp' in tile['contents'].keys():
return tile['contents']['hp']
return 0
global isFog
def isFog(tile):
return tileType(tile) == 'fog'
global isSmoke
def isSmoke(tile):
return tileType(tile) == 'smoke'
global isEnemy
def isEnemy(x, y, tile):
return isOtherWombat(x, y, tile) or isWoodWall(tile) or tileType(tile) == 'zakano'
global tileValue
def tileValue(x, y, tile):
global shotDamage
if tileType(tile) == 'food':
if hp(playerTile()) < shotDamage * 2:
return 16
return 5
elif tileType(tile) == 'zakano':
if hp(tile) <= shotDamage:
return 10
elif hp(tile) <= shotDamage*2:
return 4
return 2
elif isOtherWombat(x, y, tile):
if hp(tile) <= shotDamage:
return 15
elif hp(tile) <= shotDamage*2:
return 7
return 3
elif isWoodWall(tile):
return 1.5
return 0
global tileEv
def tileEv(x, y, tile):
evPathToTile = pathToTile(x, y)
evTileValue = tileValue(x, y, tile)
evPathTime = pathTime(evPathToTile, [x,y], orientation())
if evPathTime == 0:
return 0
return evTileValue/float(evPathTime)
global search
def search(x, y, maxTile, maxEv):
def valid(coord):
global frontier
global searched
theTile = tile(coord[0], coord[1])
return coord not in searched and coord not in frontier and not isFog(theTile) and not isWall(theTile) and not isPoison(theTile)
def tryAdd(dx, dy, backVector):
if valid([dx, dy]):
global frontier
global fromTile
frontier.append([dx, dy])
fromTile[dx][dy] = backVector
searchTile = tile(x, y)
searchTileEv = tileEv(x, y, searchTile)
if searchTileEv > maxEv or maxTile == []:
maxEv = searchTileEv
maxTile = [x,y]
global frontier
global searched
global width
global height
searched.append([x, y])
if x > 0:
tryAdd(x-1, y, [1, 0])
if x < width - 1:
tryAdd(x+1, y, [-1, 0])
if y > 0:
tryAdd(x, y-1, [0, 1])
if y < height - 1:
tryAdd(x, y+1, [0, -1])
if len(frontier) == 0:
return maxTile
nextTile = frontier.pop(0)
return search(nextTile[0], nextTile[1], maxTile, maxEv)
global orientation
def orientation():
orientation = tile(coords()[0], coords()[1])['contents']['orientation']
if orientation == 'n':
return [0, -1]
if orientation == 'w':
return [-1, 0]
if orientation == 's':
return [0, 1]
if orientation == 'e':
return [1, 0]
global turnToVector
def turnToVector(vector):
orientationVector = orientation()
sumVector = [vector[0] + orientationVector[0], vector[1] + orientationVector[1]]
if sumVector == [0,0]:
return "about-face"
prod = sumVector[0] * sumVector[1]
if orientationVector[0] == 0:
#north/south facing
if prod == -1:
return "right"
return "left"
#east/west facing
if prod == -1:
return "left"
return "right"
global pathToTile
def pathToTile(x, y):
buildPath = []
currInPath = [x, y]
currVector = [0,0]
while currInPath != coords():
currVector = fromTile[currInPath[0]][currInPath[1]]
currVector = [currVector[0], currVector[1]]
currInPath[0] = currInPath[0] + currVector[0]
currInPath[1] = currInPath[1] + currVector[1]
currVector[0] = currVector[0] * -1
currVector[1] = currVector[1] * -1
buildPath.insert(0, currVector)
return buildPath
global pathTime
def pathTime(path, tileCoords, currDirection):
totalTime = 0
for i in range(0, len(path)):
totalTime += 1
if currDirection != path[i]:
totalTime += 1
currDirection = path[i]
if isEnemy(tileCoords[0], tileCoords[1], tile(tileCoords[0], tileCoords[1])):
# Add a turn to account for shooting
totalTime += 1
return totalTime
global turn
def turn(direction):
return {'action': 'turn', 'metadata': {'direction': direction}}
global moveForward
def moveForward():
return {'action': 'move', 'metadata': {}}
global shoot
def shoot():
return {'action': 'shoot'}
def wombat(state, time_left):
global bored
global path
global targetCoords
global targetTile
global width
global height
global currState
global fromTile
global searched
global frontier
global command
global log
global shotDamage
currState = state
for i in range(width):
fromTile.append([])
for j in range(height):
fromTile[i].append([0,0])
if 'saved-state' in state and state['saved-state']:
savedState = state['saved-state']
path = savedState['path']
targetCoords = savedState['targetCoords']
targetTile = savedState['targetTile']
bored = savedState['bored']
else:
path = []
targetCoords = []
targetTile = 0;
frontier = []
searched = []
#Calculate ev of our stored target using the remainder of our saved path
currTargetTile = 0
targetEv = 0
if len(targetCoords) > 1 and len(path) > 0:
currTargetTile = tile(targetCoords[0], targetCoords[1])
targetEv = tileValue(targetCoords[0], targetCoords[1], targetTile)/float(pathTime(path, targetCoords, orientation()))
#Perform search and calculate ev of max value target
newTargetCoords = search(coords()[0], coords()[1], [], 0)
newTargetTile = tile(newTargetCoords[0], newTargetCoords[1])
newTargetEv = tileEv(newTargetCoords[0], newTargetCoords[1], newTargetTile)
log['targetEv'] = targetEv
log['newSearchEv'] = newTargetEv
#If we have no path, we see something more valuable, or our target is no longer valid.
#Fog and smoke still count as valid because target is probably still there
if len(path) == 0 or len(targetCoords) < 2 or newTargetEv > targetEv or (tileType(currTargetTile) != tileType(targetTile) and not isSmoke(currTargetTile) and not isFog(currTargetTile)):
targetCoords = newTargetCoords
targetTile = newTargetTile
if targetCoords != coords() and len(targetCoords) > 1:
path = pathToTile(targetCoords[0], targetCoords[1])
if len(targetCoords) > 1:
log['targetHp'] = hp(targetTile)
log['targetValue'] = tileValue(targetCoords[0], targetCoords[1], targetTile)
#If there is something right in front of us, shoot
shooting = False
for i in range (1,5):
dx = coords()[0] + i * orientation()[0]
dy = coords()[1] + i * orientation()[1]
if dx > width -1 or dx < 0 or dy > height -1 or dy < 0:
break
sightTile = tile(coords()[0] + i * orientation()[0], coords()[1] + i * orientation()[1])
#Shoot if there's something in front, but only shoot at wall if you ahve nothing else to do
if isEnemy(dx, dy, sightTile) and not (len(path) > 0 and isWall(sightTile)):
command = shoot()
shooting = True
break
if not shooting:
move = [0,0]
#If we have no path, just start moving forward for a while
if len(path) == 0:
bored +=1
if bored > 8:
bored = 0
command = turn('right')
else:
command = moveForward()
move = orientation()
#Move along the path, turn if necessary
else:
bored = 0
move = path[0]
if orientation() == move:
command = moveForward()
path.pop(0)
else:
command = turn(turnToVector(move))
#If we're about to move into a wall or poison, turn right. this should only happen when no path
frontTile = tile(coords()[0] + orientation()[0], coords()[1] + orientation()[1])
if command['action'] == 'move':
if isPoison(frontTile) or isWall(frontTile):
command = turn('right')
return {
'command': command,
'state': {'path': path, 'targetCoords': targetCoords, 'targetTile': targetTile, 'bored': bored, 'log': log}
}