forked from pedralmeida22/IA-Bomberman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stuPath.py
411 lines (335 loc) · 18.2 KB
/
stuPath.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import sys
import json
import asyncio
import websockets
import getpass
import os
from defs2 import *
from mapa import Map
from Node import *
from path import *
from bomb import *
async def agent_loop(server_address="localhost:8000", agent_name="student"):
async with websockets.connect(f"ws://{server_address}/player") as websocket:
# Receive information about static game properties
await websocket.send(json.dumps({"cmd": "join", "name": agent_name}))
msg = await websocket.recv()
game_properties = json.loads(msg)
# You can create your own map representation or use the game representation:
mapa = Map(size=game_properties["size"], mapa=game_properties["map"])
previous_key = ""
calc_hide_pos = False
previous_level = None
previous_lives = None
previos_pos = None
samePosCounter = 0
positions = []
positionsCiclo = []
history = []
limite = 0
got_powerup = False
powerup = [0,0]
detonador = False
wallpass = False
bombpass = False
change=False
enemyCloseCounter = 0
goal = []
samePosBomba = 0
corner = None
tentativasRun = 0
while True:
try:
state = json.loads(
await websocket.recv()
) # receive game state, this must be called timely or your game will get out of sync with the server
# Next lines are only for the Human Agent, the key values are nonetheless the correct ones!
if state['level'] == 15 and state['enemies'] == []:
return 0
if state['lives'] == 0:
return 0
key = ""
print(state)
# atualizar mapa
mapa.walls = state['walls']
level = state['level']
if previous_level != None and previous_lives != None:
# se morrer ou passar de nível faz reset às variáveis globais
if previous_level != state['level']:
got_powerup = False
powerup = [0,0]
previos_pos = None
samePosCounter = 0
if previous_level != state['level'] or previous_lives != state['lives']:
calc_hide_pos = False
previous_level = state['level']
previous_lives = state['lives']
positions = []
history = []
goal = []
enemyCloseCounter = 0
if corner == None:
corner = find_corner(mapa)
# ignora powerups não utilizados
if level == 2 or level == 5 or level == 6 or level == 10 or level == 11 or level == 12 or level == 13 or level == 14 or level==15:
got_powerup = True
if detonador:
if level == 8 or level == 13:
got_powerup = True
my_pos = state['bomberman']
ways = get_possible_ways(mapa, my_pos)
print('ways: ', end='')
print(ways)
# verificar se tem detonador
if my_pos == powerup:
got_powerup = True
if level == 3 or level == 8 or level == 13:
detonador = True
if level == 9:
bombpass = True
# fuga recursiva
if state['bombs'] != [] and not calc_hide_pos:
tentativasRun += 1
if tentativasRun > 3:
print("pos da bomba(stuck):", state['bombs'][0][0])
goal,calc_hide_pos = choose_hide_pos2(state['bombs'][0][0],state['bombs'][0], mapa, '', 0, 60, state['enemies'],detonador)
else:
print("calcurar hide pos")
goal, calc_hide_pos = choose_hide_pos2(my_pos, state['bombs'][0], mapa, '', 0, 60, state['enemies'],detonador)
print('my pos:', my_pos)
print('hide pos calculado:',goal)
print('hide pos: ' + str(calc_hide_pos))
key = choose_move(my_pos, ways, goal)
# key = choose_key(mapa, my_pos, positions, goal, True)
print('key hide pos in cacl:', key)
change = False
elif state['bombs'] != [] and calc_hide_pos:
tentativasRun = 0
print('já sabe a hide pos!')
if detonador:
if samePosBomba >=3:
change = True
if my_pos == previos_pos:
samePosBomba += 1
else:
samePosBomba = 0
print('change: ' , change)
if not change:
if dist_to(my_pos, goal) != 0:
print("ir para hide pos")
key = choose_move(my_pos, ways, goal)
print('hide pos: ', goal)
# key = choose_key(mapa, my_pos, positions, goal, True)
print('key hide pos :', key)
else: # esta seguro, espera ate a bomba rebentar
#if detonador:
print('Usar detonador')
key = 'A'
ways.append('A')
positions = []
#else:
# print("Esperar que a bomba rebente...")
# key = ''
# positions = []
else:
print("A ir para o [1,1]! ZWA")
change = False
goal, calc_hide_pos = choose_hide_pos2(my_pos, state['bombs'][0], mapa, '', 0, 60, state['enemies'],detonador)
print('nova hide pos: ',goal)
key=choose_move(my_pos,ways,goal)
elif state['bombs'] == []: # nao ha bombas
calc_hide_pos = False
# enquanto nao tiver detonador nao procura ballons
if detonador == True:
enemies = state['enemies']
else:
enemies = [e for e in state['enemies'] if e['name'] in ['Oneal','Minvo','Kondoria','Ovapi','Pass']]
enemies = state['enemies']
# só há inimigos vai atras deles
if state['walls'] == [] and state['enemies'] != [] and state['powerups'] == []:
enemies = [e for e in state['enemies'] if e['name'] in ['Oneal','Minvo','Kondoria','Ovapi','Pass']]
if enemies !=[]:
enemies.sort(key=lambda x: dist_to(my_pos, x['pos']))
distToClosestEnemy = dist_to(my_pos, enemies[0]['pos'])
print('DisToClosestEnemy: ' + str(distToClosestEnemy))
print('enemy_pos: ' + str(enemies[0]['pos']))
key = pathToEnemy(mapa, my_pos, enemies[0]['pos'])
goal = enemies[0]['pos']
# se tiver perto do inimigo incrementa o contador
if distToClosestEnemy < 2.5:
print('Perto do inimigo!')
enemyCloseCounter += 1
print('enemyCloseCounter: ',enemyCloseCounter)
elif enemyCloseCounter > 20:
print('Ciclo infinito encontrado!!!'.center(50, '-'))
# vai para uma parede
#print('Encontrar caminho até à parede alvo: ' + str(wall))
goal = list(mapa.bomberman_spawn)
key = choose_move(my_pos,ways,goal)
enemyCloseCounter = 0
print('goal: ',goal)
elif dist_to(my_pos, corner) == 0:
print("going to kill enemies")
enemies = state['enemies']
enemies.sort(key=lambda x: dist_to(my_pos, x['pos']))
if dist_to(list(corner), enemies[0]['pos']) < 6:
key = 'B'
ways.append('B')
else:
key, positions = choose_key(mapa, ways, my_pos, positions, list(corner), True)
goal = list(corner)
# apanhar powerups
elif state['powerups'] != []:
print("going to powerups")
#key = choose_move(my_pos,ways,state['powerups'][0][0])
#key,positions = choose_key(mapa, ways, my_pos, positions, state['powerups'][0][0], True)
powerup = state['powerups'][0][0]
key, positions, goal = goTo(mapa, my_pos, ways, positions, powerup, True)
print('positions: ' + str(positions))
print('key from goTo (powerup): ' + key)
print('goal' + str(goal))
if state['walls']:
parede = min(state['walls'], key=lambda x: dist_to(my_pos, x))
if dist_to(my_pos, parede) <= 1:
key = 'B'
ways.append('B')
# ir para 'exit'
elif got_powerup and state['enemies'] == [] and state['exit'] != []:
print("going to exit")
#goal = state['exit']
key, positions,goal = goTo(mapa, my_pos, ways, positions, state['exit'], True)
print('positions: ' + str(positions))
print('key from goTo (exit): ' + key)
print('goal' + str(goal))
if state['walls']:
parede = min(state['walls'], key=lambda x: dist_to(my_pos, x))
if dist_to(my_pos, parede) <= 1:
key = 'B'
ways.append('B')
# ha paredes
elif state['walls'] != []:
print("Escolher parede alvo...")
print('my' + str(my_pos))
'''if positions == [] or positions is None:
print("Escolher nova parede: ")'''
wall = next_wall(my_pos, state['walls'])
print('parede: ', wall)
print('dist to wall: ', end='')
print(dist_to(my_pos, wall))
if len(enemies) == 1 and not got_powerup: #para apanhar o powerup
enemies = []
# por bomba se tiver perto da parede
if dist_to(my_pos, wall) <= 1:
print('Cheguei à parede! Pôr bomba!')
key = 'B'
ways.append('B')
# ha inimigos, procura caminho para eles se nao encontar pocura para a parade mais proxima
elif enemies !=[] :
enemies.sort(key=lambda x: dist_to(my_pos, x['pos']))
distToClosestEnemy = dist_to(my_pos, enemies[0]['pos'])
print('DisToClosestEnemy: ' + str(distToClosestEnemy))
# se tiver perto do inimigo incrementa o contador
if distToClosestEnemy < 2.5:
print('Perto do inimigo!')
enemyCloseCounter += 1
# verificar ciclo com inimigo
if enemyCloseCounter > 20:
print('Ciclo infinito com inimigo encontrado!!!'.center(50, '-'))
# vai destruir parede mais proxima
key, positions, goal = goTo(mapa, my_pos, ways, positions, wall, False)
print('Encontrar caminho até à parede alvo: ' + str(wall))
enemyCloseCounter = 0
print('positions: ' + str(positions))
print('key from ciclo enimie: ' + key)
print('goal: ',goal)
# procura caminho para inimigo e parede
else:
if positions !=[] or (level == 1 and state['step'] >= 500):
key, positions, goal = goTo(mapa, my_pos, ways, positions, wall, False)
print('positions: ' + str(positions))
print('key from goto Wall: ' + key)
print('goal' + str(goal))
else:
# procura caminho para inimigo
print('Procurar caminho para inimigo....')
key = pathToEnemy(mapa, my_pos, enemies[0]['pos'])
print('key pathEnemy: ', key)
if key == '':
print('Procurar para a parede...')
key, positions, goal = goTo(mapa, my_pos, ways, positions, wall, False)
if key == '':
print('Pensar outra vez')
positions = []
key, positions, goal = goTo(mapa, my_pos, ways, positions, wall, False)
print('positions: ' + str(positions))
print('key from goTo (wall): ' + key)
print('goal' + str(goal))
else:
print('Encontrar caminho até à parede alvo: ' + str(wall))
key, positions, goal = goTo(mapa, my_pos, ways, positions, wall, False)
print('positions: ' + str(positions))
print('key from goToWall: ' + key)
print('goal' + str(goal))
if state['enemies'] != [] and state['bombs'] == []:
##17/10 - Fugir dos inimigos
enemies = state['enemies']
enemies.sort(key=lambda x: dist_to(my_pos, x['pos']))
if key in ['w','s','d','a']:
if in_range(mapa.calc_pos(my_pos,key), 1, enemies[0]['pos'], mapa):
print('Enemie close! Pôr bomba! (Calculado)')
key = 'B'
ways.append('B')
if in_range(my_pos, 1, enemies[0]['pos'], mapa):
print('Enemie close! Pôr bomba!')
key = 'B'
ways.append('B')
if my_pos == previos_pos:
samePosCounter += 1
if samePosCounter >= 20:
print('Suicidio'.center(80, '/'))
if state['bombs'] != []:
if detonador:
key = 'A'
ways.append('A')
else:
key = choose_random_move(ways)
print ('key random:'+ key)
samePosCounter = 0
else:
print('Reset samePosCounter!')
samePosCounter = 0
# garantir que key é válida
if key != '' or key == None:
if not key in ways:
print('Caminho impossivel... escolhendo novo')
print('goal: ',goal) #quando vai matar inimigos e entra em ciclo infinito o goal que passa é [], não sei porque
if goal:
key = choose_move(my_pos, ways, goal)
else:
key = choose_move(my_pos,ways,next_wall(my_pos,state['walls']))
history.append(my_pos)
previous_level = state['level']
previous_lives = state['lives']
previous_key = key
previos_pos = my_pos
print('Sending key: ' + key + '\n\n')
print("got_powerup: ",got_powerup)
print('Detonador: ', detonador)
print('Bombpass: ', bombpass)
print('corner: ', str(corner))
print('enemyCloseCounter: ', enemyCloseCounter)
await websocket.send(
json.dumps({"cmd": "key", "key": key})
) # send key command to server - you must implement this send in the AI agent
# break
except websockets.exceptions.ConnectionClosedOK:
print("Server has cleanly disconnected us")
return
# DO NOT CHANGE THE LINES BELLOW
# You can change the default values using the command line, example:
# $ NAME='bombastico' python3 client.py
loop = asyncio.get_event_loop()
SERVER = os.environ.get("SERVER", "localhost")
PORT = os.environ.get("PORT", "8000")
NAME = os.environ.get("NAME", getpass.getuser())
loop.run_until_complete(agent_loop(f"{SERVER}:{PORT}", NAME))