This repository has been archived by the owner on Jun 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
299 lines (265 loc) · 10.7 KB
/
helpers.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
from config import *
import random
def calc_client_hash(username):
if len(username) > 20:
return 0
ascii_list = []
ascii_total = 0
for letter in username:
ascii_value = ord(letter)
ascii_list.append(ascii_value)
ascii_total += ascii_value
cl_hash = (ascii_total * 1000) % 65536
print(f"clhash:{cl_hash}")
return cl_hash
def calc_srv_key(cl_hash, kid):
return (cl_hash + S_KEYS[kid]) % 65536
def calc_cl_key(cl_hash, kid):
return (cl_hash + C_KEYS[kid]) % 65536
class Robot:
def __init__(self):
self.direction = None
self.prev_x = None
self.prev_y = None
self.obstacle_dodging = False
self.first_move = True
self.dodging_moves = [SERVER_TURN_LEFT, SERVER_MOVE, SERVER_TURN_RIGHT, SERVER_MOVE, SERVER_MOVE,
SERVER_TURN_RIGHT, SERVER_MOVE]
self.dodging_moves_R = [SERVER_TURN_RIGHT, SERVER_MOVE, SERVER_TURN_LEFT, SERVER_MOVE, SERVER_MOVE,
SERVER_TURN_LEFT, SERVER_MOVE]
self.dodging_moves_L = [SERVER_TURN_LEFT, SERVER_MOVE, SERVER_TURN_RIGHT, SERVER_MOVE, SERVER_MOVE,
SERVER_TURN_RIGHT, SERVER_MOVE]
self.wanted_direction = None
self.obstacle_first = False
self.second_move = False
self.commands = []
self.expected = ()
def get_wanted_direction(self, x, y):
target = max(abs(x), abs(y))
if abs(x) == abs(y):
if x < 0:
return 'E'
elif y < 0:
return 'N'
elif x > 0:
return 'W'
elif y > 0:
return 'S'
else:
return self.direction
elif target == abs(x):
if x > 0:
return 'W'
elif x < 0:
return 'E'
elif x == 0:
if y > 0:
return 'S'
else:
return 'N'
elif target == abs(y):
if y > 0:
return 'S'
elif y < 0:
return 'N'
elif y == 0:
if x > 0:
return 'W'
else:
return 'E'
def get_direction(self, x, y):
if x > self.prev_x:
return 'E'
elif x < self.prev_x:
return 'W'
elif y > self.prev_y:
return 'N'
elif y < self.prev_y:
return 'S'
else:
print("Prev is same, probably hit obstacle...")
return self.direction
def turn_to_target(self):
if self.direction == 'W' and self.wanted_direction == 'S':
self.direction = 'S'
return SERVER_TURN_LEFT
elif self.direction == 'W' and self.wanted_direction == 'N':
self.direction = 'N'
return SERVER_TURN_RIGHT
elif self.direction == 'S' and self.wanted_direction == 'E':
self.direction = 'E'
return SERVER_TURN_LEFT
elif self.direction == 'S' and self.wanted_direction == 'W':
self.direction = 'W'
return SERVER_TURN_RIGHT
elif self.direction == 'N' and self.wanted_direction == 'E':
self.direction = 'E'
return SERVER_TURN_RIGHT
elif self.direction == 'N' and self.wanted_direction == 'W':
self.direction = 'W'
return SERVER_TURN_LEFT
elif self.direction == 'E' and self.wanted_direction == 'S':
self.direction = 'S'
return SERVER_TURN_RIGHT
elif self.direction == 'E' and self.wanted_direction == 'N':
self.direction = 'N'
return SERVER_TURN_LEFT
elif self.direction == 'W' and self.wanted_direction == 'E':
print("Zasranej komouš")
self.direction = 'N'
return SERVER_TURN_RIGHT
elif self.direction == 'E' and self.wanted_direction == 'W':
self.direction = 'N'
return SERVER_TURN_LEFT
elif self.direction == 'N' and self.wanted_direction == 'S':
self.direction = 'E'
return SERVER_TURN_RIGHT
elif self.direction == 'S' and self.wanted_direction == 'N':
self.direction = 'W'
return SERVER_TURN_RIGHT
if self.direction == self.wanted_direction:
return SERVER_MOVE
def set_expected(self, x, y):
if self.direction == "N":
self.expected = (x, y + 1)
elif self.direction == "S":
self.expected = (x, y - 1)
elif self.direction == "W":
self.expected = (x - 1, y)
elif self.direction == "E":
self.expected = (x + 1, y)
def move_to_0(self, x, y):
y = y.split('\a\b')[0]
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(f'My new pos: {x},{y}')
if "." in x:
x_float = True
else:
x_float = False
if "." in y:
y_float = True
else:
y_float = False
if x_float or y_float:
return SERVER_SYNTAX_ERROR
x = int(x)
y = int(y)
print("Expected:")
print(self.expected)
if len(self.commands) > 0:
if self.commands[-1:] == [SERVER_MOVE] and not self.second_move:
self.direction = self.get_direction(x, y)
print(f"Direction: {self.get_direction(x, y)}")
self.wanted_direction = self.get_wanted_direction(x, y)
print(f"Wanted direction: {self.wanted_direction}")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
if len(self.commands) > 0:
print(self.commands)
if self.first_move:
if x == 0 and y == 0:
self.commands.append(SERVER_PICK_UP)
return SERVER_PICK_UP
print(f"First move")
self.first_move = False
self.prev_x = x
self.prev_y = y
self.first_move = False
self.second_move = True
self.commands.append(SERVER_MOVE)
self.commands.append(SERVER_MOVE)
return SERVER_MOVE
if self.second_move:
if x == 0 and y == 0:
self.commands.append(SERVER_PICK_UP)
return SERVER_PICK_UP
if self.prev_x == x and self.prev_y == y and (not self.obstacle_first):
print(f"Second move")
print("Hit obstacle on first move, going around")
print("Cannot determine direction")
self.obstacle_first = True
self.second_move = True
command = random.choice([SERVER_TURN_LEFT, SERVER_TURN_RIGHT])
self.commands.append(command)
return command
elif self.obstacle_first:
print(f"Second move p2")
self.obstacle_first = False
self.second_move = False
self.prev_x = x
self.prev_y = y
self.commands.append(SERVER_MOVE)
return SERVER_MOVE
else:
print(f"Second move")
self.direction = self.get_direction(x, y)
self.wanted_direction = self.get_wanted_direction(x, y)
self.prev_x = x
self.prev_y = y
self.second_move = False
self.commands.append(SERVER_MOVE)
self.set_expected(x, y)
return SERVER_MOVE
if x == 0 and y == 0:
self.commands.append(SERVER_PICK_UP)
return SERVER_PICK_UP
elif self.direction is not None and self.direction != self.wanted_direction \
and self.commands[-1:] == [SERVER_MOVE] and not self.obstacle_dodging:
print(f"DIRECTION IS NOT RIGHT, wanted {self.wanted_direction}, now {self.direction}")
turn = self.turn_to_target()
self.commands.append(turn)
return turn
elif self.direction is not None and self.direction != self.wanted_direction \
and self.commands[-2:] == [SERVER_MOVE, SERVER_MOVE]:
print(f"DIRECTION IS NOT RIGHT, wanted {self.wanted_direction}, now {self.direction}")
if self.obstacle_dodging:
print(f"Wanted dir changed during dodging...")
self.obstacle_dodging = False
turn = self.turn_to_target()
self.commands.append(turn)
return turn
elif (x, y) != self.expected \
and self.commands[-2:] == [SERVER_MOVE, SERVER_MOVE] and (not self.obstacle_dodging):
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(f"OBSTACLE HIT {self.expected}")
if x == 0 and y == 1:
self.dodging_moves = self.turn_to_target()
elif x == 1 and y == 0:
self.dodging_moves = self.turn_to_target()
else:
self.dodging_moves = [SERVER_TURN_LEFT, SERVER_MOVE, SERVER_TURN_RIGHT, SERVER_MOVE, SERVER_MOVE,
SERVER_TURN_RIGHT, SERVER_MOVE]
self.obstacle_dodging = True
move = self.dodging_moves[0]
print(f"Dodging, move now is {move}")
self.dodging_moves.pop(0)
self.prev_x = x
self.prev_y = y
self.commands.append(move)
return move
elif self.obstacle_dodging:
if len(self.dodging_moves) <= 1:
self.obstacle_dodging = False
self.dodging_moves = [SERVER_TURN_LEFT, SERVER_MOVE, SERVER_TURN_RIGHT, SERVER_MOVE, SERVER_MOVE,
SERVER_TURN_RIGHT, SERVER_MOVE]
print("Dodging complete")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
self.prev_x = x
self.prev_y = y
self.commands.append(SERVER_MOVE)
self.set_expected(x, y)
return SERVER_MOVE
move = self.dodging_moves[0]
print(f"Dodging, move now is {move}")
self.dodging_moves = self.dodging_moves[1:]
self.prev_x = x
self.prev_y = y
self.commands.append(move)
return move
else:
print("Move forward:")
print(f"{x}, {y}")
self.prev_x = x
self.prev_y = y
self.set_expected(x, y)
self.commands.append(SERVER_MOVE)
return SERVER_MOVE