-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyBot.py
More file actions
462 lines (381 loc) · 17.7 KB
/
MyBot.py
File metadata and controls
462 lines (381 loc) · 17.7 KB
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
import hlt
import logging
from operator import itemgetter
game = hlt.Game("D4m0b0t - v2.2a - smarter offense")
def docked_actions(current_ship):
"""
Determine what to do with our docked ship
:param Ship current_ship:
:return: command to append to the command_queue
:rtype: List
"""
if DEBUGGING['method_entry']:
log.debug("docked_actions():")
if DEBUGGING['ship_loop']:
log.debug("-+=Docked ship #" + str(current_ship.id) + "=+-")
#did we just complete docking?
if current_ship in dock_process_list.keys():
if DEBUGGING['docking_procedures']:
log.debug(" - completed docking")
dock_process_list.remove(current_ship)
if ALGORITHM['boobytrapping']: #fully docked
#is it time to bid thee farewell?
if current_ship.planet.remaining_resources <= (current_ship.planet.num_docking_spots * DOCKING_TURNS * PRODUCTION) + 10:
#syntax/logic in the following conditional (specifically the 'not') may be phrased wrong
if not current_ship.planet in planets_to_avoid:
if DEBUGGING['boobytrapping']:
log.debug("Leaving a present")
planets_to_avoid.append(current_ship.planet)
undock_process_list[current_ship] = current_ship.planet
command_queue.append(ship.undock(current_ship.planet))
def target_planet(current_ship, planets_ranked_by_distance, planets_ranked_ours_by_docked, planets_ranked_untapped):
"""
Determine if a planet is a viable target for mining; create the navigation command if so
:param Ship current_ship:
:param List of Tuples planets_ranked_by_distance:
:param List of Tuples planets_ranked_ours_by_docked:
:param List of Tuples planets_ranked_untapped:
:return: navigation command for command_queue or None
:rtype: String or None
"""
navigate_command = None
#do we navigate to a planet, reinforce, or go offensive?
#navigate to a planet or begin docking (this also currently handles reinforcing)
for potential_planet in remove_held_planets(planets_ranked_untapped):
if (potential_planet['entity_object'] in targeted_list) or \
(potential_planet['entity_object'].num_docking_spots == len(potential_planet['entity_object'].all_docked_ships())):
if DEBUGGING['targeting']:
log.debug(" - skipping already targeted or full planet #" + str(potential_planet['entity_object'].id))
continue
if current_ship.can_dock(potential_planet['entity_object']): #why ship & not current_ship again?
if DEBUGGING['planet_selection']:
log.debug(" - docking with planet #" + str(potential_planet['entity_object'].id))
#dock_process_list[current_ship] = potential_planet['entity_object']
navigate_command = current_ship.dock(potential_planet['entity_object'])
if potential_planet['entity_object'] in targeted_list:
if DEBUGGING['planet_selection']:
log.debug(" - removing planet #" + str(potential_planet['entity_object'].id) + " from targeted_list")
targeted_list.remove(potential_planet['entity_object'])
break
elif potential_planet['entity_object'] not in targeted_list:
if DEBUGGING['targeting']:
log.debug(" - navigating to planet #" + str(potential_planet['entity_object'].id))
targeted_list.append(potential_planet['entity_object'])
navigate_command = current_ship.navigate(
current_ship.closest_point_to(potential_planet['entity_object']),
game_map,
speed = default_speed,
ignore_ships = False)
break
return navigate_command
def go_offensive(current_ship, enemies):
"""
Returns navigation command for offense, or None if not the best course of action at this juncture
:param Ship current_ship:
:param List of Tuples enemies:
:return navigation_command or None:
:rtype: String or None
"""
navigate_command = None
if DEBUGGING['offense']:
log.debug("Engaging enemy")
close_enemies = entity_sort_by_distance(current_ship, enemies)
closest_enemy = close_enemies[0]['entity_object']
close_friendlies = entity_sort_by_distance(current_ship, game_map.get_me().all_ships())
#implementation of kamikaze was never completed
if ALGORITHM['kamikaze']:
potential_kamikaze_angle = other_entities_in_vicinity(current_ship, enemies, 100) #note '100' was for debugging
if DEBUGGING['kamikaze']:
log.debug(" - potential_kamikaze_angle: " + str(potential_kamikaze_angle))
if potential_kamikaze_angle:
if DEBUGGING['kamikaze'] and DEBUGGING['offense']:
log.debug(" - going kamikaze")
navigate_command = current_ship.thrust(hlt.constants.MAX_SPEED, potential_kamikaze_angle)
if not ALGORITHM['kamikaze'] or not navigate_command:
if DEBUGGING['offense']:
log.debug(" - engaging ship #" + str(closest_enemy.id))
num_enemies_in_range = count_ships_in_firing_range(current_ship, close_enemies, MAX_FIRING_DISTANCE)
num_friendlies_in_range = count_ships_in_firing_range(current_ship, close_friendlies, MAX_FIRING_DISTANCE)
if not ALGORITHM['ram_ships_when_weak'] or (closest_enemy.health <= current_ship.health and
num_enemies_in_range <= num_friendlies_in_range):
#standard offense, stay with 'em and shoot 'em
if DEBUGGING['ram_ships_when_weak']:
log.debug(" - firing on enemy, not ramming")
navigate_command = current_ship.navigate(
current_ship.closest_point_to(closest_enemy),
game_map,
speed = default_speed,
ignore_ships = False)
else:
#RAMMING SPEED!
if DEBUGGING['ram_ships_when_weak'] and (num_enemies_in_range <= num_friendlies_in_range):
log.debug(" - ship #" + str(closest_enemy.id) + " is stronger w/" + str(closest_enemy.health) + \
" health vs my " + str(current_ship.health) + " - ramming speed!")
elif DEBUGGING['ram_ships_when_weak']:
log.debug(" - my ship #" + str(current_ship.id) + " is outnumbered " + str(num_enemies_in_range) + ":" + \
str(num_friendlies_in_range) + " - ramming speed!")
navigate_command = current_ship.navigate(
closest_enemy,
game_map,
speed = hlt.constants.MAX_SPEED,
avoid_obstacles = True,
ignore_ships = True,
ignore_planets = False)
return navigate_command
def count_ships_in_firing_range(current_ship, entities_for_consideration, max_range):
"""
Determine how many ships are within our offensive bubble
:param Ship current_ship:
:param List of Tuples entities_for_consideration:
:param float max_range:
:return int ships in range:
:rtype: int
"""
cntr = 0
#enemies_potentially_in_range = entity_sort_by_distance(current_ship, enemies)
for current_enemy in entities_for_consideration:
if current_enemy['distance'] <= max_range:
cntr += 1
else:
break #don't waste the processing time
return cntr
def reinforce_planet(current_ship, our_planets_by_docked, our_ranked_untapped_planets):
"""
Create navigation command to reinforce the nearest planet with an open docking spot
NOTE: This is currently not utilized, and almost certainly broken
:param Ship current_ship: derp
:param List our_planets_by_docked: List of Tuples
:param List our_ranked_untapped_planets: List of Tuples
:return String navigation_command:
:rtype: String
"""
navigation_command = None
#reinforce that sucker
if DEBUGGING['reinforce']:
log.debug("Reinforcing planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
if current_ship.can_dock(ranked_our_planets_by_docked[0]['entity_object']):
if DEBUGGING['reinforce']:
log.debug(" - docking @ planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
navigate_command = current_ship.dock(ranked_our_planets_by_docked[0]['entity_object'])
else:
if DEBUGGING['reinforce']:
log.debug(" - navigating to reinforce planet #" + str(ranked_untapped_planets[0]['entity_object']))
navigate_command = current_ship.navigate(
current_ship.closest_point_to(ranked_untapped_planets[0]['entity_object']),
game_map,
speed = default_speed,
ignore_ships = False)
return navigation_command
def undocked_actions(current_ship):
"""
Determine what to do with the undocked ship
:param Ship current_ship:
:return: command to append to the command_queue
:rtype: List
"""
if DEBUGGING['method_entry']:
log.debug("undocked_actions():")
if DEBUGGING['ship_loop']:
log.debug("-+=Undocked ship #" + str(current_ship.id) + "=+-")
#did we just complete undocking?
if current_ship in undock_process_list.keys():
if DEBUGGING['docking_procedures']:
log.debug(" - completed undocking")
undock_process_list.remove(current_ship)
success = False
ranked_planets_by_distance = entity_sort_by_distance(current_ship, game_map.all_planets())
ranked_our_planets_by_docked = planet_sort_ours_by_docked(game_map.all_planets())
ranked_untapped_planets = remove_tapped_planets(ranked_planets_by_distance, planets_to_avoid)
enemies = get_enemy_ships()
#get our command, if navigation to/docking with a planet is the best course of action
#else None
navigate_command = target_planet(current_ship, ranked_planets_by_distance, ranked_our_planets_by_docked, \
ranked_untapped_planets)
if not navigate_command:
#potential_angle = other_entities_in_vicinity(current_ship, enemies, ranked_untapped_planets[0]['distance'])
if ALGORITHM['offense']: # and potential_angle:
navigate_command = go_offensive(current_ship, enemies)
elif ALGORITHM['reinforce'] and len(ranked_our_planets_by_docked) > 0:
navigate_command = reinforce_planet(current_ship, ranked_our_planets_by_docked, ranked_untapped_planets)
return navigate_command
def remove_held_planets(planets_list):
"""
Remove all planets from the list that are already held by a player
:param List planets_list: List of Tuples containing planet_object => object
:return List with owned planets removed:
:rtype: List of Typles
"""
if DEBUGGING['method_entry']:
log.debug("remove_held_planets():")
for possibly_owned_planet in planets_list:
if not possibly_owned_planet:
if DEBUGGING['targeting']:
log.debug(" - removing owned planet #" + str(possibly_owned_planet['entity_object'].id) + " from list")
planets_list.remove(possibly_owned_planet)
return planets_list
def entity_sort_by_distance(current_ship, planet_list):
"""
Sort the given solar system into planets weighted by least distance from given ship to planet
:param Ship current_ship:
:param List planet_list:
:return: List of tuples containing entity_object & distance from current_ship
:rtype: List of Tuples
"""
if DEBUGGING['method_entry']:
log.debug("entity_sort_by_distance():")
nang = []
for ouah in planet_list:
nang.append({'entity_object' : ouah, 'distance' : ouah.calculate_distance_between(current_ship)})
return sorted(nang, key=itemgetter('distance'))
def planet_sort_ours_by_docked(planet_list):
"""
Sort the given solar system into planets weighted by least ships docked
:param List planet_list: List of planets to be weighted
:return: List of tuples of weighted planets
:rtype: List of Tuples
"""
if DEBUGGING['method_entry']:
log.debug("planet_sort_by_docked():")
nang = []
for ouah in planet_list:
if ouah.owner == game_map.get_me():
nang.append({'entity_object' : ouah, 'number_docked' : len(ouah.all_docked_ships())})
if len(nang) > 0:
#remove planets with no docking slots open
for ouah in nang:
if ouah['number_docked'] >= ouah['entity_object'].num_docking_spots:
nang.remove(ouah)
return sorted(nang, key=itemgetter('number_docked'))
def other_entities_in_vicinity(current_entity, other_entities, scan_distance):
"""
Check to see if there are any more specified entities within the immediate vicinity
:param Entity current_entity:
:param List other_entities:
:param integer scan_distance:
:return: Collision angle if any
:rtype: Collision angle or none
"""
if DEBUGGING['method_entry']:
log.debug("other_entities_in_vicinity()")
#closest_docked_distance = scan_distance
target_planet = None
for other_entity in other_entities:
#if other_entity.docking_status == current_entity.DockingStatus.DOCKED or \
# other_entity.docking_status == current_entity.DockingStatus.DOCKING:
if current_entity.planet:
continue
proximity = int(current_entity.calculate_distance_between(other_entity))
if DEBUGGING['kamikaze']:
log.debug("\t- current_entity's proximity: " + str(proximity) + " vs scan_distance: " + str(scan_distance))
if proximity < scan_distance:
if DEBUGGING['kamikaze']:
log.debug("\t- proximity is less than scan_distance")
if current_entity.docking_status == current_entity.DockingStatus.DOCKED or \
current_entity.docking_status == current_entity.DockingStatus.DOCKING:
if DEBUGGING['kamikaze']:
log.debug("\t\t- setting target_planet to current_entity.planet")
target_planet = current_entity.planet
break
else:
continue
if target_planet:
return current_entity.calculate_angle_between(target_planet)
return None
def offensive_targeting(current_entity, other_entities):
"""
Check for enemies within firing range
:param Entity current_entity:
:param List other_entities:
:return: intercept angle or None
:rtype: float
"""
if DEBUGGING['method_entry']:
log.debug("offensive_targeting():")
enemy_intercept_angle = other_entities_in_vicinity(current_entity, other_entities, 5)
if enemy_intercept_angle:
return enemy_intercept_angle
else:
return None
def get_enemy_ships():
"""
Retrieve all enemy ships
:return: all enemy ships
:rtype: List of ships
"""
if DEBUGGING['method_entry']:
log.debug("get_enemy_ships():")
enemy_ships = []
for jackass in game_map.all_players():
if not jackass == game_map.get_me():
for ship in jackass.all_ships():
enemy_ships.append(ship)
return enemy_ships
def remove_tapped_planets(testing_planets, avoid_planets):
"""
Remove all avoid_planets from testing_planets
:param List testing_planets:
:param List avoid_planets:
:return: planets sans tapped planets
:rtype: List of planets
"""
if DEBUGGING['method_entry']:
log.debug("remove_tapped_planets():")
for bogus in avoid_planets:
if bogus in testing_planets:
testing_planets.remove(bogus) #this is going to fail if python passes immutably
return testing_planets
#entrance
#constants
DEBUGGING = {
'ship_loop': True,
'docking_procedures': False,
'reinforce': False,
'offense': True,
'ram_ships_when_weak': True,
'kamikaze': False,
'planet_selection': False,
'targeting': False,
'boobytrapping': False,
'method_entry': True
}
ALGORITHM = {
'reinforce': False,
'offense': True,
'ram_ships_when_weak': True,
'kamikaze': False,
'boobytrapping': True
}
PRODUCTION = 6
DOCKING_TURNS = 5
MAX_FIRING_DISTANCE = 5
planets_to_avoid = []
dock_process_list = {}
undock_process_list = {}
enemy_data = {}
#init
log = logging.getLogger(__name__)
logging.info("D4m0b0t v2.2a active")
#begin primary game loop
while True:
if DEBUGGING['ship_loop']:
log.debug("-+Beginning turn+-")
game_map = game.update_map()
my_id = game_map.get_me().id
#default_speed = int(hlt.constants.MAX_SPEED / 2)
#default_speed = int(hlt.constants.MAX_SPEED / 1.75)
default_speed = hlt.constants.MAX_SPEED
command_queue = []
targeted_list = []
for ship in game_map.get_me().all_ships():
if ship.docking_status == ship.DockingStatus.DOCKED:
new_command = docked_actions(ship)
if new_command:
command_queue.append(new_command)
continue
elif ship.docking_status == ship.DockingStatus.UNDOCKED:
new_command = undocked_actions(ship)
if new_command:
command_queue.append(new_command)
continue
#end per-ship iteration
game.send_command_queue(command_queue)