-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCoders_strike_back.py
548 lines (405 loc) · 16 KB
/
Coders_strike_back.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"""
Coders Strike back Gold League Bot.
Currently standing in at around 100th in Gold league.
All angles in radians where possible in scale of pi to -pi
cp = cp
rel = relation
agl = angle
d = d
"""
import sys
import math
# for vector math refernce.
# https://www.oreilly.com/library/view/machine-learning-with/9781491989371/ch01.html
pi = 3.14159
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++POINT & VECTOR+++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class point:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
x = self.x + other.x
y = self.y + other.y
return point(x, y)
def __sub__(self, other):
x = self.x - other.x
y = self.y - other.y
return point(x, y)
def __mul__(self, num):
x = self.x * num
y = self.y * num
return point(x, y)
def flip(self):
""" Flip around axis
Only to be used when pod is taken as (0,0)"""
x = self.x * -1
y = self.y * -1
return point(x, y)
class vector:
def __init__(self, vx, vy):
self.x = vx
self.y = vy
self.angle = math.atan2(self.y, self.x)
self.abs = math.hypot(self.x, self.y)
def get_quadrant(self):
"""Get the quadrant a vector is facing."""
if self.x > 0 and self.y > 0:
self.quadrant = 1
elif self.x < 0 and self.y > 0:
self.quadrant = 2
elif self.x < 0 and self.y < 0:
self.quadrant = 3
elif self.x > 0 and self.y < 0:
self.quadrant = 4
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++CHECKPOINT+++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class cp: # Checkpoint
def __init__(self, pos, id):
self.pos = pos
self.id = id
class rel:
def __init__(self, pod, cp):
self.d = get_distance(pod.pos, cp.pos)
self.parent_cp = cp
self.vector_pod_cp = get_vector(pod.pos, cp.pos)
self.abs_angle = self.vector_pod_cp.angle
self.facing_offset = get_signed_angle(
self.abs_angle, pod.angle_facing)
self.heading_offset = get_signed_angle(
self.abs_angle, pod.vector.angle)
def add_compensation_angle(self, pod, limit=7000):
global_overshoot = get_overshoot_pos(
self, pod, pod.vector.angle, pod.current_cp_rel.heading_offset)
# compensation values (point opposite target from overshoot)
self.compensation = constrain_point(global_overshoot.flip(), -limit, limit)
def compensated_heading(self):
return self.parent_cp.pos + self.compensation
class heading:
def __init__(self, pod):
self.pod
class compensation:
def __init__(vector, target)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++++++++++POD+++++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
class pod:
def __init__(self):
self.pos = None
self.global_vector = None
self.angle_facing = None
self.current_cp = None
def calc_basic(self):
self.vector = vector(self._global_vector.x, self._global_vector.y * -1)
self.last_cp = cps[(_current_cp.id - 1) % (cp_count)] # cp just passed
self.next_cp = cps[(_current_cp.id + 1) % (cp_count)] # cp after current
self.next_cp2 = cps[(_current_cp.id + 2) % (cp_count)] # cp after next
class my_pod(pod):
def calc(self):
self.current_cp_rel = rel(self, self.current_cp)
self.next_cp_rel = rel(self, self.next_cp)
self.heading = heading(self)
def get_heading(self):
# Set a base heading in case none of the if statements catch
self.current_cp_rel.add_compensation_angle(self, limit=5000)
base_heading = self.current_cp_rel.compensated_heading()
self.heading = base_heading
self.thrust = 100
# +++++++ HEADING ALGORITHM +++++++
# If far enough, boost
if (
self.current_cp_rel.d > 6000 and
abs(self.current_cp_rel.facing_offset) < 0.1):
self.thrust = "BOOST"
# +++ Getting info about the corner to take+++
self.angle_pod_current_next = get_angle_between_three_points(
self.pos,
self.current_cp.pos,
self.next_cp.pos)
d_last_cp_current_cp = get_distance(
self.last_cp.pos,
self.current_cp.pos)
d_pod_last_cp = get_distance(
self.pos,
self.last_cp.pos)
# if far enough, and heading in the right direction
# swing out in preparation for the corner.
if (
# d_pod_last_cp > 1000 and
self.current_cp_rel.d > d_pod_last_cp * 3 and
self.current_cp_rel.heading_offset < pi/4 and
d_last_cp_current_cp > 5000):
debug("status - prepping corner")
self.heading = self.prepare_corner()
# if heading is good, activate corner procedure
if (
self.vector.abs > 0 and
abs(self.current_cp_rel.heading_offset) < 0.7):
debug("status - cornering")
self.corner()
# if facing the wrong direction, do not thrust...
# facing_compensation(self)
def prepare_corner(self, limit=5000):
direction = left_or_right(
self.pos,
self.current_cp.pos,
self.next_cp.pos)
mag = pi/10 # magnitude of compensation move
if direction == "left":
sim_heading = self.current_cp_rel.abs_angle - mag
elif direction == "right":
sim_heading = self.current_cp_rel.abs_angle + mag
prep_heading = get_overshoot_pos(
self.current_cp_rel, self, sim_heading, mag)
prep_heading = constrain_point(prep_heading, -limit, limit)
new_heading = self.current_cp.pos + prep_heading
return new_heading
def corner(self):
time_to_target = (self.current_cp_rel.d / self.vector.abs)
self.next_cp_rel.add_compensation_angle(self, limit=5000)
#next_heading = self.next_cp_rel.compensated_heading()
next_heading = self.next_cp.pos
debug(self.current_cp_rel.heading_offset)
if abs(self.angle_pod_current_next) > pi * 4/5:
print(f"full speed", file=sys.stderr)
if time_to_target < 5.5:
self.heading = next_heading
self.thrust = "BOOST"
elif abs(self.angle_pod_current_next) > pi * 3/5:
print(f"soft", file=sys.stderr)
if time_to_target < 5.5:
self.heading = next_heading
self.thrust = 100
elif abs(self.angle_pod_current_next) > pi * 2/5:
print(f"90", file=sys.stderr)
if time_to_target < 4.5:
self.heading = next_heading
self.thrust = 100
elif time_to_target < 5.5:
self.heading = next_heading
self.thrust = 10
elif abs(self.angle_pod_current_next) > pi * 1/5:
print(f"hard", file=sys.stderr)
if time_to_target < 3.05:
self.heading = next_heading
self.thrust = 100
elif time_to_target < 5.5:
self.heading = next_heading
self.thrust = 20
elif abs(self.angle_pod_current_next) < pi * 1/5:
print(f"hairpin", file=sys.stderr)
if time_to_target < 5:
self.heading = next_heading
self.thrust = 0
def predict_next_pos(self):
new_x = self.pos.x + self.global_vector.x
new_y = self.pos.y + self.global_vector.y
self.next_pos = point(new_x, new_y)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++ANGLE FUNCTIONS+++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def flip_rotation_direction(angle, type="radians"):
if type == "degrees":
angle = (-angle) % 360
elif type == "radians":
angle = (-angle) % pi
return angle
def change_angle_scale_to_180(angle):
angle = (angle - 180) % 360 - 180
return angle
def degree_to_rads(angle):
angle = angle * (pi / 180)
return angle
def find_quadrant(origin, target):
"""Get the target quadrant from an x and y position."""
if target.x > origin.x and target.y < origin.y:
return 1
elif target.x < origin.x and target.y < origin.y:
return 2
elif target.x < origin.x and target.y > origin.y:
return 3
elif target.x > origin.x and target.y > origin.y:
return 4
def get_signed_angle(a1, a2):
diff = a1 - a2
if diff > pi:
diff -= pi*2
if diff < -pi:
diff += pi*2
return diff
def facing_compensation(pod):
if abs(pod.current_cp_rel.facing_offset) > pi * 4/5:
pod.thrust = 20
elif abs(pod.current_cp_rel.facing_offset) > pi * 3/5:
pod.thrust = 30
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++UTILITY FUNCTIONS+++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
def debug(var_name="", variable=""):
print(f"{var_name}, {variable}", file=sys.stderr)
def constrain(val, min_val, max_val):
"""Constrain value between min and max."""
val = int(min(max_val, max(min_val, val)))
return val
def constrain_point(pos, min_val, max_val):
""" Constrain the magnitude from (0,0) of a point
Generally used to limit the compensation of a heading."""
x = int(constrain(pos.x, min_val, max_val))
y = int(constrain(pos.y, min_val, max_val))
return point(x, y)
def get_distance(point1, point2):
""" Get distance between two points """
x = point2.x - point1.x
y = point2.y - point1.y
d = math.hypot(x, y)
return d
def get_vector(p1, p2):
""" Get vector from two positions """
quadrant = find_quadrant(p1, p2)
x = 0
y = 0
if quadrant == 1:
x = p2.x - p1.x
y = p1.y - p2.y
elif quadrant == 2:
x = (p1.x - p2.x) * -1
y = p1.y - p2.y
elif quadrant == 3:
x = (p1.x - p2.x) * -1
y = (p2.y - p1.y) * -1
elif quadrant == 4:
x = p2.x - p1.x
y = (p2.y - p1.y) * -1
return vector(x, y)
def get_global_angle(p1, p2):
"""
Get global angle between two points
That is, if a p1 is the pod, at (0, 0)
and p2 is at (-100, 100)
The global angle would be 3/4 pi
"""
d = get_distance(p1, p2)
q = find_quadrant(p1, p2)
diff = p1 - p2
local_angle = math.atan2(diff.y, diff.x)
global_angle = 0
if q == 1:
global_angle = pi - local_angle
elif q == 2:
global_angle = pi - local_angle
elif q == 3:
global_angle = -pi - local_angle
elif q == 4:
global_angle = -pi - local_angle
return global_angle
def get_angle_between_three_points(p1, p2, p3):
""" Get angle between three points
At p2 between p1 and p3 """
d_p1_p2 = get_distance(p1, p2)
d_p2_p3 = get_distance(p2, p3)
d_p1_p3 = get_distance(p1, p3)
# law of cosines
angle = (
math.acos(
(
d_p1_p3 ** 2 - d_p1_p2 ** 2 - d_p2_p3 ** 2
) / (
-2 * d_p1_p2 * d_p2_p3
)
)
)
return angle
def get_overshoot_pos(
cp_rel, pod, pod_heading, angle):
""" Get the point where the current heading will overshoot the target
"""
d_overshoot_target = abs(cp_rel.d * math.tan(abs(angle)))
# the d between pod and the overshoot point.
d_pod_overshoot = math.hypot(cp_rel.d, d_overshoot_target)
# coordinates of overshoot relative to pod
x_overshoot = (d_pod_overshoot * math.cos(pod_heading))
y_overshoot = (d_pod_overshoot * math.sin(pod_heading))
relative_overshoot = point(
pod.pos.x + x_overshoot, pod.pos.y - y_overshoot)
global_overshoot = relative_overshoot - cp_rel.parent_cp.pos
return global_overshoot
def left_or_right(p1, p2, p3):
""" Determine whether from p1, passing p2
to reach p3 will be a left or right turn """
global_angle_p1_p2 = get_global_angle(p1, p2)
global_angle_p2_p3 = get_global_angle(p2, p3)
angle = get_signed_angle(global_angle_p1_p2, global_angle_p2_p3)
if angle < 0:
return "left"
else:
return "right"
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# ++++++++++++++++INITIALIZATION++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
pods = {pod1, pod2}
enemy_pods = {}
laps = int(input())
cp_count = int(input())
cps = {}
for i in range(cp_count):
cp_x, cp_y = [int(j) for j in input().split()]
cp_pos = point(cp_x, cp_y)
cps[i] = cp(cp_pos, i)
counter = 0
last_shield_activation = [0, 0]
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# +++++++++++++++++++++GAME LOOP++++++++++++++++++++++++++
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
while True:
counter += 1
for i in range(2):
x, y, global_vx, global_vy, angle_facing, current_cp_id = [
int(j) for j in input().split()]
print(f"pod {i}", end=" ", file=sys.stderr)
angle_facing = flip_rotation_direction(angle_facing, "degrees")
angle_facing += 5 # original angle seems to be off by 5 degrees
angle_facing = change_angle_scale_to_180(angle_facing)
angle_facing_in_rads = degree_to_rads(angle_facing)
pod_pos = point(x, y)
pod_vector = vector(global_vx, global_vy)
current_cp = cps[current_cp_id]
current_pod = pod(pod_pos, pod_vector, angle_facing_in_rads, current_cp)
current_pod.get_heading()
#if i == 1 and counter < 10:
# current_pod.thrust = 10
current_pod.predict_next_pos()
pods[i] = current_pod
for i in range(2):
# OPPONENT
x_2, y_2, global_vx_2, global_vy_2, angle_2, current_check_point_id_2 = [int(j) for j in input().split()]
print(f"enemy_pod {i}", file=sys.stderr)
angle_facing = flip_rotation_direction(angle_2, "degrees")
angle_facing += 5 # original angle seems to be off by 5 degrees
angle_facing = change_angle_scale_to_180(angle_facing)
angle_facing_in_rads = degree_to_rads(angle_facing)
pod_pos = point(x_2, y_2)
pod_vector = vector(global_vx_2, global_vy_2)
current_cp = cps[current_check_point_id_2]
current_pod = pod(pod_pos, pod_vector, angle_facing_in_rads, current_cp)
current_pod.predict_next_pos()
enemy_pods[i] = current_pod
# collisions
collision_rg = 700
if counter > 10:
for p in pods.keys():
print(f"pod: {p} ", file=sys.stderr)
print(f"counter: {counter} ", file=sys.stderr)
print(f"last_shield_activation: {last_shield_activation[p]} ", file=sys.stderr)
for ep in enemy_pods.keys():
x_diff = abs(pods[p].next_pos.x - enemy_pods[ep].next_pos.x)
y_diff = abs(pods[p].next_pos.y - enemy_pods[ep].next_pos.y)
if (
abs(pods[p].next_pos.x - enemy_pods[ep].next_pos.x) < collision_rg and
abs(pods[p].next_pos.y - enemy_pods[ep].next_pos.y) < collision_rg):
if counter - last_shield_activation[p] > 15:
pods[p].thrust = "SHIELD"
last_shield_activation[p] = counter
print(f"{pods[0].heading.x} {pods[0].heading.y} {pods[0].thrust}")
print(f"{pods[1].heading.x} {pods[1].heading.y} {pods[1].thrust}")