-
Notifications
You must be signed in to change notification settings - Fork 1
/
appleBot.py
198 lines (166 loc) · 6.29 KB
/
appleBot.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
import logging
import math
import random
import time
from SimulationHandler import SimulationHandler
from utils import *
NAMES = [
"Appbomination",
"Belta Lowda",
"Marco Inaros",
"Aegis",
"Black Hole",
"Newton's Bane",
"Waymaker",
"Gatecrasher",
"Unmakyr",
"RED/BLUE PILL",
"Chicxulub",
"Seeker"]
class AppleBot:
def __init__(self, socket_manager):
self.logger = logging.getLogger(__name__)
# Set up SimulationHandler and precompile functions
self.simulation = SimulationHandler(self)
self.simulation.compile_functions()
# Set uo ConnectionHandler and initialize connection
self.connection = socket_manager
self.connection.initialize()
self.id = -1
self.name = ""
self.planets = []
self.players = {}
self.opponents = []
self.ignored_opponents = []
self.energy = 0
self.last_name_update = time.time()
self.update_flag = False
def update_simulation(self):
if self.id == -1:
return
if self.id not in self.players:
return
if not self.planets:
return
if not self.opponents:
return
self.logger.info("Updating simulation...")
self.simulation.update_field()
def choose_name(self):
pick_names = NAMES.copy()
try:
pick_names.remove(self.name)
except ValueError:
pass
name = random.choice(pick_names)
self.connection.send_str(f"n {name}")
def process_incoming(self, log=False):
struct_data = self.connection.receive_struct("II")
# recv timed out
if struct_data is None:
return False
# unpack struct
msg_type, payload = struct_data
# bot has joined
if msg_type == 1:
self.id = payload
self.update_flag = True
if log:
self.logger.info(f"RECV: Own id set to {payload}")
# player left
elif msg_type == 2:
del self.players[payload]
self.opponents.remove(payload)
if payload in self.ignored_opponents:
self.ignored_opponents.remove(payload)
self.update_flag = True
if log:
self.logger.info(f"RECV: Player {payload} left the game.")
# player joined/moved
elif msg_type == 3:
x, y = self.connection.receive_struct("ff")
if payload not in self.players:
self.logger.info(f"RECV: Player {payload} joined the game at ({round(x)},{round(y)})")
if payload != self.id:
self.opponents.append(payload)
new_player = Player(x, y, payload)
else:
self.logger.info(f"RECV: Player {payload} moved to ({round(x)},{round(y)})")
if payload in self.ignored_opponents:
self.ignored_opponents.remove(payload)
elif payload == self.id:
self.ignored_opponents.clear()
new_player = self.players[payload]
new_player.position[0] = x
new_player.position[1] = y
self.players[payload] = new_player
self.update_flag = True
# shot finished
elif msg_type == 4:
self.logger.error(
"RECV: ERROR! WRONG BOT PROTOCOL VERSION DETECTED (VER < 8)! THIS MSG_TYPE SHOULD NOT BE RECEIVED!")
exit(1)
# shot begin
elif msg_type == 5:
_, _ = self.connection.receive_struct("dd")
# shot end
elif msg_type == 6:
# discard all shot data
_, _, length = self.connection.receive_struct("ddI")
for i in range(length):
_ = self.connection.receive_struct("ff")
# game mode, deprecated
elif msg_type == 7:
self.logger.error(
"RECV: ERROR! WRONG BOT PROTOCOL VERSION DETECTED (VER <= 8)! THIS MSG_TYPE SHOULD NOT BE RECEIVED!")
exit(1)
# own energy
elif msg_type == 8:
self.energy = math.floor(self.connection.receive_struct("d")[0])
# planet pos
elif msg_type == 9:
# discard planet byte count
self.connection.receive_struct("I")
self.planets = []
for i in range(payload):
x, y, radius, mass = self.connection.receive_struct("dddd")
self.planets.append(Planet(x, y, radius, mass, i))
self.logger.info(f"RECV: Map data changed. {payload} planets received.")
self.choose_name()
self.update_flag = True
# unknown MSG_TYPE
else:
self.logger.warning(f"RECV: Unexpected message_type: '{msg_type}'\n\t- data: '{payload}'")
# try to discard all data appended to this message type
self.connection.discard_all(.1)
return False
return True
def loop(self):
if self.process_incoming():
return
self.scan_field()
def scan_field(self):
possible_targets = list(set(self.opponents).difference(set(self.ignored_opponents)))
# No viable opponents found to target (any opponents still on the board haven't moved since last scan)
if not possible_targets:
return
# update simulation field if flag is set
if self.update_flag:
self.logger.info("Update flag set, updating field...")
self.simulation.update_field()
self.update_flag = False
# if field is not ready yet, return
elif not self.simulation.initialized:
return
target_player = sorted(possible_targets, key=lambda x: dist(self.players[x].position, self.players[self.id].position))[0]
result = self.simulation.scan_for(target_player)
# field changed
if result == -2:
return
# target parameters found
elif result != -1:
self.connection.send_str(f"c\nv {result[1]}")
self.connection.send_str(f"{math.degrees(result[0])}")
self.ignored_opponents.append(target_player)
if not list(set(self.opponents).difference(set(self.ignored_opponents))):
self.logger.info("No remaining viable opponents.")