-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
645 lines (518 loc) · 23.1 KB
/
main.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
from selenium.webdriver import Firefox
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import ElementNotInteractableException
from selenium.webdriver.firefox.options import Options as FirefoxOptions
import numpy as np
import time
import re
import math
import sys
import msvcrt
TAKEOFF_QUEUE = 0
DEPARTURE = 1
ARRIVAL = 2
APPROACHING = 3
# Plane States is an array that stores the state of each plane in play
# It has a list of sub-arrays corresponding to each possible state
# 0 - planes waiting to takeoff - (Callsign, Runway, Destination)
# 1 - planes departing
# 2 - planes being guided to their final approach
# 3 - planes on final approach
# Each index will contain an array of each plane in that state
plane_list = []
plane_states = [[], [], [], []]
taking_off = []
speeding_up = []
intercepting = {}
arrival_states = {}
clear_max_speed = {}
handoffs = 0
landings = 0
# Position of Waypoints
WAYPTS = {}
POS_EGLL = (800, 500)
# Target points with 09 landing runway
TARGET_POINTS_09_N = [(350, 750), (350, 550)]
TARGET_POINTS_09_S = [(350, 250), (350, 450)]
# Target points with 27 landing runway
TARGET_POINTS_27_N = [(1350, 750), (1350, 550)]
TARGET_POINTS_27_S = [(1350, 250), (1350, 450)]
# Target points
target_points = []
landing_rwy = ''
target_rwy = ''
# Parse the data shown on the 'strips' on the right side of the screen
def parse_plane_strips(html):
global plane_states, handoffs, landings
plane_list = []
temp = []
# Regex expression to parse the strips of the planes waiting to takeoff
to_queue_expression = \
r'<div id="(.+?)" name="\1".+? rgb\(192, 228, 250\);">\1 (\d{1,2}[LR]).+?To: (.{3,6})<'
for match in re.findall(to_queue_expression, html):
# print('Departure Callsign: {}, Runway: {}, Destination: {}'.format(match[0], match[1], match[2]))
temp.append([match[0], match[1], match[2]])
plane_list.append(match[0])
plane_states[TAKEOFF_QUEUE] = temp
temp = []
# Regex expression to parse the strips of the planes climbing to cruise
departure_expression = r'<div id="(.+?)" name="\1".+? rgb\(192, 228, 250\);">\1 (\D.+?) '
for match in re.findall(departure_expression, html):
# print('Departure Callsign: {}, Destination: {}'.format(match[0], match[1]))
temp.append([match[0], match[1]])
plane_list.append(match[0])
if match[0] in taking_off:
taking_off.remove(match[0])
for plane in plane_states[DEPARTURE]:
if len(plane) < 1:
continue
callsign = plane[0]
present = False
for plane_2 in temp:
if len(plane_2) < 1:
continue
if callsign == plane_2[0]:
present = True
break
if not present:
handoffs += 1
plane_states[DEPARTURE] = temp
temp = []
# Regex expression to parse the strips of the planes descending towards the airport
arrival_expression = r'<div id="(.+?)" name="\1".+? rgb\(252, 240, 198\);">\1 (\w[A-Z]{2,5}|\d{2,3}°)'
for match in re.findall(arrival_expression, html):
# print('Arrival Callsign: {}, Heading: {}'.format(match[0], match[1]))
temp.append([match[0], match[1].replace('°', '')])
plane_list.append(match[0])
plane_states[ARRIVAL] = temp
temp = []
# Regex expression to parse the strips of the planes on approach
approach_expression = r'<div id="(.+?)" name="\1".+? rgb\(252, 240, 198\);">\1 ((?:9|27)[LR])'
for match in re.findall(approach_expression, html):
# print('Approach Callsign: {}, Runway: {}'.format(match[0], match[1]))
temp.append([match[0], match[1]])
plane_list.append(match[0])
if match[0] in arrival_states.keys():
arrival_states.pop(match[0])
if match[0] in intercepting.keys():
intercepting.pop(match[0])
for plane in plane_states[APPROACHING]:
if len(plane) < 1:
continue
callsign = plane[0]
present = False
for plane_2 in temp:
if len(plane_2) < 1:
continue
if callsign == plane_2[0]:
present = True
break
if not present:
landings += 1
plane_states[APPROACHING] = temp
# Parse the data shown on the radar screen
def parse_canvas(html):
parse_expression = r'<div id="(.+?)" class="SanSerif12".+?left: (.+?)px; top: (.+?)px.*\1<br>(\d{3}).(\d{2})'
for match in re.findall(parse_expression, html):
callsign = match[0]
# Iterate through each plane to find the matching callsign and then append the values of x, y and alt
# Quite inefficient but it works because there are only around 10 - 20 planes tops
# Might update indexing later to make this prettier
for category in plane_states:
for plane in category:
if plane[0] == callsign:
plane.append(int(match[1]) + 25) # x coord
plane.append(950 - int(match[2])) # y coord
plane.append(int(match[3]) * 100) # alt
plane.append(int(match[4])) # spd
def parse_waypts(html):
parse_expression = r'<img src="draw_.+\.php\?ID=(.+?)&TYPE=[01]" style="position: absolute; left: (-?\d+)px; top: (-?\d+)px'
for match in re.findall(parse_expression, html):
name = match[0]
pos_x = int(match[1]) + 25
pos_y = 950 - int(match[2])
WAYPTS[name] = (pos_x, pos_y)
# Calculate the heading a plane needs to take to get from its current pos to a point
def calculate_heading(pos1, pos2):
dx = pos2[0] - pos1[0]
dy = pos2[1] - pos1[1]
initial_hdg = math.degrees(math.atan2(dx, dy))
if initial_hdg < 0:
initial_hdg += 360
return round(initial_hdg)
# Functions to help find intersection of plane paths
def check_headings(point1, bearing1, point2, bearing2, intsec):
c = 450*math.pi/180
if (math.cos(c-(bearing1*math.pi/180))*(intsec[0]-point1[0]) > 0 or math.sin(c-(bearing1*math.pi/180))*(intsec[1]-point1[1]) > 0)\
and (math.cos(c-(bearing2*math.pi/180))*(intsec[0]-point2[0]) > 0 or math.sin(c-(bearing2*math.pi/180))*(intsec[1]-point2[1]) > 0):
# function returns a tuple with intersection (x,y) or None if there will be no intersection
return intsec
else:
return None
# points to be given in tuple (x,y), bearing in degrees
def calculate_intersection(point1, bearing1, point2, bearing2):
if bearing1 not in [0, 180]:
# equation in both x and y
gradient1 = math.tan((math.pi/2)-(bearing1*math.pi/180))
eq1 = np.array([1, -gradient1, point1[1]-(gradient1*point1[0])])
else:
# equation only in x
eq1 = np.array([0, 1, point1[0]])
if bearing2 not in [0, 180]:
# equation in both x and y
gradient2 = math.tan((math.pi/2)-(bearing2*math.pi/180))
eq2 = np.array([1, -gradient2, point2[1]-(gradient2*point2[0])])
else:
# equation only in x
eq2 = np.array([0, 1, point2[0]])
aug = np.array([eq1[-1], eq2[-1]])
augMatrix = np.array([eq1[:-1], eq2[:-1]])
try:
solution = np.linalg.solve(augMatrix, aug)
intsec = (solution[1], solution[0])
# returns tuple of intersection point or None
return check_headings(point1, bearing1, point2, bearing2, intsec)
except:
return None
# Calculate the squared distance between 2 points
def calculate_sqr_distance(pos1, pos2):
dx = pos2[0] - pos1[0]
dy = pos2[1] - pos1[1]
sqr_d = (dx ** 2) + (dy ** 2)
return sqr_d
# Calculate the distance between 2 points
def calculate_distance(pos1, pos2):
return calculate_sqr_distance(pos1, pos2) ** 0.5
# Calculates the difference between 2 headings
def calculate_del_heading(hdg1, hdg2):
return abs((hdg2 - hdg1 + 540) % 360 - 180)
def get_command_list():
command_list = []
# Index 0 is Left Rwy, Index 1 is Right Rwy
safe_runways = [True, True]
# Update keys of cleared_max_speed according to the overall plane list
to_pop = []
for k in clear_max_speed.keys():
if not k in plane_list:
to_pop.append(k)
for p in to_pop:
clear_max_speed.pop(p)
# First find if it is safe for a plane to takeoff
# Check if the previous departure has achieved a particular speed in its takeoff run
# Also checks the position of the closest arrival
# When the plane reaches this speed it will have reached 1000 feet before the previous planes' departure
for departure in plane_states[DEPARTURE]:
if 'BEE' in departure[0] and departure[1] == 'BUZAD' and departure[4] == 200:
command_list.append('{} C 11 EX'.format(departure[0]))
if len(departure) >= 6 and departure[4] < 200:
safe_runways = [False, False]
for approaching in plane_states[APPROACHING]:
if len(approaching) >= 5 and (abs(approaching[2] - POS_EGLL[0]) < 45 or approaching[4] < 900):
if 'L' in approaching[1]:
safe_runways[0] = False
elif 'R' in approaching[1]:
safe_runways[1] = False
# Clear planes for takeoff accordingly
for rto in plane_states[TAKEOFF_QUEUE]:
if 'L' in rto[1] and safe_runways[0]:
callsign = rto[0]
if not callsign in taking_off:
destination = rto[2]
command_list.append(
'{} C {} C 11 T'.format(callsign, destination))
taking_off.append(callsign)
safe_runways[0] = False
elif 'R' in rto[1] and safe_runways[1]:
callsign = rto[0]
if not callsign in taking_off:
destination = rto[2]
command_list.append(
'{} C {} C 11 T'.format(callsign, destination))
taking_off.append(callsign)
safe_runways[1] = False
# Stores the squared distances to final
distances_to_final = {}
# Calculate headings for each plane on the approaching list
for arrival in plane_states[ARRIVAL]:
global target_rwy
callsign = arrival[0]
plane_heading = int(arrival[1])
plane_pos = (arrival[2], arrival[3])
if plane_pos[1] < 500:
if landing_rwy == '9':
target_points = TARGET_POINTS_09_S
intercept_hdg = 45
else:
target_points = TARGET_POINTS_27_S
intercept_hdg = 315
else:
if landing_rwy == '9':
target_points = TARGET_POINTS_09_N
intercept_hdg = 135
else:
target_points = TARGET_POINTS_27_N
intercept_hdg = 225
if not callsign in arrival_states:
if landing_rwy == '27':
if plane_pos[1] > 200 and plane_pos[1] < 800 and plane_pos[0] > 1350:
arrival_states[callsign] = 1
command_list.append('{} C 2 EX'.format(callsign))
else:
arrival_states[callsign] = 0
command_list.append('{} C 4'.format(callsign))
else:
if plane_pos[1] > 200 and plane_pos[1] < 800 and plane_pos[0] < 350:
arrival_states[callsign] = 1
command_list.append('{} C 2 EX'.format(callsign))
else:
arrival_states[callsign] = 0
command_list.append('{} C 4'.format(callsign))
elif arrival_states[callsign] == len(target_points):
command_list.append('{} L {}'.format(
callsign, intercepting[callsign]))
continue
if arrival_states[callsign] >= 0:
target_point = target_points[arrival_states[callsign]]
else:
target_point = WAYPTS['BNN']
sqr_distance_to_target = calculate_sqr_distance(
plane_pos, target_point)
distances_to_final[callsign] = sqr_distance_to_target
if arrival_states[callsign] <= 0:
distances_to_final[callsign] += 40000
if arrival_states[callsign] < 0:
distances_to_final[callsign] += calculate_sqr_distance(WAYPTS['BNN'], TARGET_POINTS_09_N[0]
if landing_rwy == '9' else TARGET_POINTS_27_N[0])
# Check if the plane is near the target point
if sqr_distance_to_target < 1000:
arrival_states[callsign] += 1
command_list.append('{} C {}'.format(
callsign, 4 - arrival_states[callsign]))
# Check if the plane is at the last point
if arrival_states[callsign] == len(target_points):
if 'L' in target_rwy:
target_rwy = target_rwy.replace('L', 'R')
else:
target_rwy = target_rwy.replace('R', 'L')
hdg_str = str(intercept_hdg)
if len(hdg_str) < 3:
hdg_str = '0' + hdg_str
command_list.append('{} C {}'.format(callsign, hdg_str))
command_list.append('{} L {}'.format(callsign, target_rwy))
intercepting[callsign] = target_rwy
continue
target_heading = calculate_heading(plane_pos, target_point)
if abs(target_heading - plane_heading) > 5:
hdg_str = str(target_heading)
while len(hdg_str) < 3:
hdg_str = '0' + hdg_str
command_list.append('{} C {}'.format(arrival[0], hdg_str))
# Ensure proper separation of arrival aircraft
for arrival in plane_states[ARRIVAL]:
if len(arrival) < 6:
continue
callsign = arrival[0]
if arrival_states[callsign] == len(target_points):
continue
plane_pos = (arrival[2], arrival[3])
speed = arrival[5] * 10
hdg = int(arrival[1])
clear_max_speed[callsign] = True
for arrival_2 in plane_states[ARRIVAL]:
callsign_2 = arrival_2[0]
if len(arrival_2) < 6 or callsign_2 == callsign:
continue
pos_2 = (arrival_2[2], arrival_2[3])
hdg_2 = int(arrival[1])
if arrival_states[callsign_2] == len(target_points):
continue
distance_btw_planes = calculate_sqr_distance(
plane_pos, pos_2)
# Scale target distance by separation in headings
# Enables better dynamic spacing
target_distance = 85 + (1.5 * calculate_del_heading(hdg, hdg_2))
if distance_btw_planes < target_distance ** 2:
if distances_to_final[callsign_2] < distances_to_final[callsign]:
clear_max_speed[callsign] = False
break
# Ensure approaching planes are at 160 knots
for approaching in plane_states[APPROACHING]:
if len(approaching) < 6:
continue
callsign = approaching[0]
alt = approaching[4]
speed = approaching[5] * 10
if speed < 160 and alt > 900:
command_list.append('{} S 160'.format(callsign))
# Ensure approaching planes don't collide
for approaching in plane_states[APPROACHING]:
if len(approaching) < 5:
continue
callsign = approaching[0]
rwy = approaching[1]
pos = (approaching[2], approaching[3])
if approaching[4] <= 200:
continue
for approaching_2 in plane_states[APPROACHING]:
if len(approaching_2) < 4:
continue
if approaching_2[4] <= 200:
continue
callsign_2 = approaching_2[0]
rwy_2 = approaching_2[1]
if callsign == callsign_2 or rwy != rwy_2:
continue
pos_2 = (approaching_2[2], approaching_2[3])
distance_btw_planes = calculate_sqr_distance(
pos, pos_2)
# Order go-around if dangerously close, go to BNN from where the plane will be re-sequenced
if distance_btw_planes < 30 ** 2:
if approaching[4] == approaching_2[4]:
if ('27' in rwy and (pos[0] > pos_2[0])) or ('9' in rwy and (pos[0] < pos_2[0])):
command_list.append('{} A C 7 EX C {}'.format(
callsign, calculate_heading(pos, WAYPTS['BNN'])))
arrival_states[callsign] = -1
elif approaching[4] > approaching_2[4]:
command_list.append('{} A C 7 EX C {}'.format(
callsign, calculate_heading(pos, WAYPTS['BNN'])))
arrival_states[callsign] = -1
# Ensure arrival planes don't crash into departing planes
for arrival in plane_states[ARRIVAL]:
if len(arrival) < 6:
continue
callsign = arrival[0]
hdg = int(arrival[1])
pos = (arrival[2], arrival[3])
alt = arrival[4]
spd = arrival[5]
for departure in plane_states[DEPARTURE]:
if len(departure) < 6:
continue
callsign_2 = departure[0]
dest_2 = departure[1]
pos_2 = (departure[2], departure[3])
alt_2 = departure[4]
spd_2 = departure[5]
clear_max_speed[callsign_2] = True
distance_btw_planes = calculate_distance(pos, pos_2)
if not distance_btw_planes < 110 or abs(alt - alt_2) < 1000 or (alt - alt_2) > 200:
continue
hdg_2 = calculate_heading(pos_2, WAYPTS[dest_2])
intersect = calculate_intersection(pos, hdg, pos_2, hdg_2)
if not intersect:
continue
rel_time_1 = calculate_distance(pos, intersect) / spd
rel_time_2 = calculate_distance(pos_2, intersect) / spd_2
if abs(rel_time_1 - rel_time_2) > 5:
continue
plane_to_slow = callsign if rel_time_1 > rel_time_2 else callsign_2
clear_max_speed[plane_to_slow] = False
for plane in plane_states[DEPARTURE] + plane_states[ARRIVAL]:
if len(plane) < 6:
continue
callsign = plane[0]
speed = plane[5] * 10
if not callsign in clear_max_speed.keys():
continue
if clear_max_speed[callsign] and speed < 240 and not callsign in speeding_up \
and plane[4] > 1000 and 'BEE' not in callsign:
command_list.append('{} S 240'.format(callsign))
speeding_up.append(callsign)
elif not clear_max_speed[callsign] and (speed == 240 or callsign in speeding_up):
command_list.append('{} S 160'.format(callsign))
if callsign in speeding_up:
speeding_up.remove(callsign)
elif speed == 240 and callsign in speeding_up:
speeding_up.remove(callsign)
return command_list
def execute_commands(commands):
for command in commands:
print("Executing command:", command)
command_input.send_keys(command)
command_input.send_keys(Keys.ENTER)
if __name__ == '__main__':
# Force background rendering to allow OBS recording
options = FirefoxOptions()
options.set_preference(
'widget.windows.window_occlusion_tracking.enabled', False)
# Start up firefox and open the website
driver = Firefox(options=options)
driver.maximize_window()
driver.get('http://atc-sim.com/')
# Login if details provided
if len(sys.argv) >= 3:
email = sys.argv[1]
pswd = sys.argv[2]
driver.find_element(by=By.XPATH,
value='/html/body/div[3]/div/div/div/a[1]').click()
driver.find_element(by=By.XPATH,
value='/html/body/div[4]/div[1]/table[1]/tbody/tr/td[1]/form/div/table/tbody/tr[1]/td[2]/input')\
.send_keys(email)
driver.find_element(by=By.XPATH,
value='/html/body/div[4]/div[1]/table[1]/tbody/tr/td[1]/form/div/table/tbody/tr[2]/td[2]/input')\
.send_keys(pswd)
driver.find_element(by=By.XPATH,
value='/html/body/div[4]/div[1]/table[1]/tbody/tr/td[1]/form/div/table/tbody/tr[3]/td[2]/input').click()
time.sleep(1)
# Change the airport and start the game
driver.find_element(by=By.XPATH,
value='/html/body/div[4]/div[1]/form/table/tbody/tr/td[1]/div[1]/select/option[48]').click()
else:
# Change the airport and start the game
driver.find_element(by=By.XPATH,
value='/html/body/div[4]/div[1]/form/table/tbody/tr/td[1]/div[1]/select/option[4]').click()
if len(sys.argv) == 4:
if sys.argv[3] == 'landing':
driver.find_element(by=By.XPATH,
value='//*[@id="frmOptions"]/table/tbody/tr/td[1]/div[7]/select/option[3]').click()
elif sys.argv[3] == 'takeoff':
driver.find_element(by=By.XPATH,
value='//*[@id="frmOptions"]/table/tbody/tr/td[1]/div[7]/select/option[4]').click()
driver.find_element(by=By.XPATH,
value='//*[@id="frmOptions"]/table/tbody/tr/td[1]/input[1]').click()
time.sleep(3)
if len(sys.argv) < 3:
failed = True
while failed:
try:
driver.find_element(
by=By.XPATH, value='//*[@id="btnclose"]').click()
failed = False
except ElementNotInteractableException:
time.sleep(1)
wind_dir = int(driver.find_element(by=By.XPATH,
value='//*[@id="winddir"]').get_attribute('innerHTML').split('<br>')[1].replace('°', ''))
# Check if the landing runway is 09 or 27
if calculate_del_heading(90, wind_dir) < calculate_del_heading(270, wind_dir):
landing_rwy = '9'
target_rwy = '9L'
else:
landing_rwy = '27'
target_rwy = '27R'
command_input = driver.find_element(by=By.XPATH,
value='//*[@id="canvas"]/div[1]/div/form/input[1]')
canvas_text = driver.find_element(by=By.XPATH,
value='//*[@id="canvas"]').get_attribute('innerHTML')
parse_waypts(canvas_text)
while True:
if msvcrt.kbhit() and ord(msvcrt.getch()) == 13:
execute_commands(['EXIT'])
time.sleep(1)
break
driver.switch_to.frame('ProgressStrips')
strips_text = driver.find_element(by=By.XPATH,
value='//*[@id="strips"]').get_attribute('innerHTML')
parse_plane_strips(strips_text)
driver.switch_to.parent_frame()
canvas_text = driver.find_element(by=By.XPATH,
value='//*[@id="canvas"]').get_attribute('innerHTML')
parse_canvas(canvas_text)
command_list = get_command_list()
execute_commands(command_list)
count_display = driver.find_element(by=By.XPATH,
value='/html/body/div[1]/div/div[6]')
text = 'Takeoffs: {}\\n Landings: {}'.format(handoffs, landings)
driver.execute_script(
"arguments[0].innerText = '{}'".format(text), count_display)
time.sleep(2)