forked from INIT-SGGW/HackArena2.0-MonoTanks-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_map_parsing.py
73 lines (59 loc) · 2.49 KB
/
example_map_parsing.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
"""This is an example of a bot participating in the HackArena 2.0.
This bot will randomly move around the map,
use abilities and print the map to the console.
"""
import os
import random
from hackathon_bot import *
from tomasz.map import TomaszMap, TomaszMapWithHistory
import time
class ExampleBot(HackathonBot):
map = None
def on_lobby_data_received(self, lobby_data: LobbyData) -> None:
print(f"Lobby data received: {lobby_data}")
def next_move(self, game_state: GameState) -> ResponseAction:
t1 = time.perf_counter()
if self.map is None:
self.map = TomaszMapWithHistory(game_state)
else:
new_map = TomaszMap(game_state)
self.map.update(new_map)
t2 = time.perf_counter()
print(f"Time to update map: {1e3*(t2 - t1):.2f} ms")
print(self.map)
self.map.pretty_print()
# Check if the agent is dead
if game_state.my_agent.is_dead:
# Return pass to avoid warnings from the server
# when the bot tries to make an action with a dead tank
return Pass()
return self._get_random_action()
def on_game_ended(self, game_result: GameResult) -> None:
print(f"Game ended: {game_result}")
def on_warning_received(self, warning: WarningType, message: str | None) -> None:
print(f"Warning received: {warning} - {message}")
def _get_random_action(self):
return random.choice(
[
Movement(MovementDirection.FORWARD),
Movement(MovementDirection.BACKWARD),
Rotation(RotationDirection.LEFT, RotationDirection.LEFT),
Rotation(RotationDirection.LEFT, RotationDirection.RIGHT),
Rotation(RotationDirection.LEFT, None),
Rotation(RotationDirection.RIGHT, RotationDirection.LEFT),
Rotation(RotationDirection.RIGHT, RotationDirection.RIGHT),
Rotation(RotationDirection.RIGHT, None),
Rotation(None, RotationDirection.LEFT),
Rotation(None, RotationDirection.RIGHT),
Rotation(None, None), # Useless, better use Pass()
AbilityUse(Ability.FIRE_BULLET),
AbilityUse(Ability.FIRE_DOUBLE_BULLET),
AbilityUse(Ability.USE_LASER),
AbilityUse(Ability.USE_RADAR),
AbilityUse(Ability.DROP_MINE),
Pass(),
]
)
if __name__ == "__main__":
bot = ExampleBot()
bot.run()