@@ -37,10 +37,11 @@ def __init__(self, waypoint: location.Location, acceptance_radius: float) -> Non
37
37
# ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
38
38
# ============
39
39
40
+ self .above_destination = False
40
41
self .target_lp = None
41
42
42
- self .reached_wp = False
43
- self .reached_lp = False
43
+ self .approaching_wp = False
44
+ self .approaching_lp = False
44
45
45
46
# ============
46
47
# ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
@@ -64,6 +65,10 @@ def run(
64
65
put_output(command)
65
66
```
66
67
"""
68
+ # ============
69
+ # ↓ BOOTCAMPERS MODIFY BELOW THIS COMMENT ↓
70
+ # ============
71
+
67
72
# print("RUN")
68
73
# Default command
69
74
command = commands .Command .create_null_command ()
@@ -75,27 +80,32 @@ def run(
75
80
)
76
81
77
82
# Handle unexpected halts
78
- if report .position != report .destination :
83
+ delta_x = report .position .location_x - report .destination .location_x
84
+ delta_y = report .position .location_y - report .destination .location_y
85
+ self .above_destination = delta_x ** 2 + delta_y ** 2 < self .acceptance_radius ** 2
86
+
87
+ if not self .above_destination :
79
88
print ("^^UNEXPECTED HALT" )
80
89
command = commands .Command .create_set_relative_destination_command (
81
90
report .destination .location_x - report .position .location_x ,
82
91
report .destination .location_y - report .position .location_y ,
83
92
)
84
93
85
- elif not self .reached_wp :
94
+ elif not self .approaching_wp :
86
95
command = commands .Command .create_set_relative_destination_command (
87
96
self .waypoint .location_x , self .waypoint .location_y
88
97
)
89
- self .reached_wp = True
98
+ self .approaching_wp = True
90
99
print ("APPROACHING WP: " , self .waypoint .location_x , self .waypoint .location_y )
91
100
92
- elif not self .reached_lp :
93
- self .target_lp = landing_pad_locations [ self .find_closest_lp (landing_pad_locations )]
101
+ elif not self .approaching_lp :
102
+ self .target_lp = self .find_closest_lp (report , landing_pad_locations )
94
103
lp_relative_x , lp_relative_y = self .relative_target (self .target_lp , report )
104
+
95
105
command = commands .Command .create_set_relative_destination_command (
96
106
lp_relative_x , lp_relative_y
97
107
)
98
- self .reached_lp = True
108
+ self .approaching_lp = True
99
109
print ("APPROACHING LP: " , self .target_lp .location_x , self .target_lp .location_y )
100
110
101
111
else :
@@ -104,43 +114,45 @@ def run(
104
114
105
115
return command
106
116
107
- def find_closest_lp (self , landing_pad_locations : "list[location.Location]" ) -> int :
117
+ # ============
118
+ # ↑ BOOTCAMPERS MODIFY ABOVE THIS COMMENT ↑
119
+ # ============
120
+
121
+ def find_closest_lp (
122
+ self , report : drone_report .DroneReport , landing_pad_locations : "list[location.Location]"
123
+ ) -> int :
108
124
"""
109
125
Given a list of landing pads,
110
126
determines the index of the closest one to the waypoint.
111
127
"""
112
- if len (landing_pad_locations ) in [ 0 , 1 ] :
113
- return 0
128
+ if len (landing_pad_locations ) == 1 :
129
+ return landing_pad_locations [ 0 ]
114
130
115
- wp_x = self . waypoint .location_x
116
- wp_y = self . waypoint .location_y
131
+ drone_x = report . position .location_x
132
+ drone_y = report . position .location_y
117
133
118
134
i = 0
119
- winning_i = 0
120
- winning_dist = - 1
135
+ winning_pad = landing_pad_locations [ 0 ]
136
+ winning_sq_dist = float ( "inf" )
121
137
122
138
for lp in landing_pad_locations :
123
139
lp_x = lp .location_x
124
140
lp_y = lp .location_y
125
141
126
- same_loc = (
127
- wp_x - self .acceptance_radius <= lp_x <= wp_x + self .acceptance_radius
128
- ) and (wp_y - self .acceptance_radius <= lp_y <= wp_y + self .acceptance_radius )
129
-
130
- if same_loc :
131
- return i
142
+ delta_x = lp_x - drone_x
143
+ delta_y = lp_y - drone_y
132
144
133
- sq_dist = ( lp_x - wp_x ) ** 2 + ( lp_y - wp_y ) ** 2
145
+ sq_dist = delta_x ** 2 + delta_y ** 2
134
146
135
- if ( sq_dist < winning_dist ) or ( winning_dist == - 1 ) :
136
- winning_dist = sq_dist
137
- winning_i = i
147
+ if sq_dist < winning_sq_dist :
148
+ winning_sq_dist = sq_dist
149
+ winning_pad = landing_pad_locations [ i ]
138
150
139
151
i += 1
140
152
141
153
# print("TARGET LP FOUND, INDEX: ", winning_i)
142
154
# print("^^SQDIST FROM WP: ", winning_dist)
143
- return winning_i
155
+ return winning_pad
144
156
145
157
@staticmethod
146
158
def relative_target (
0 commit comments