Skip to content

Commit 6a8ffeb

Browse files
authored
Merge pull request #17 from dgets/develop
Develop
2 parents 65e24ea + 2d56578 commit 6a8ffeb

File tree

2 files changed

+156
-69
lines changed

2 files changed

+156
-69
lines changed

MyBot-v2b.py

+155-69
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import logging
33
from operator import itemgetter
44

5-
game = hlt.Game("D4m0b0t - v2b - rewrite & flow restructuring")
5+
game = hlt.Game("D4m0b0t - v2.1b - rewrite & flow restructuring")
66

77
def docked_actions(current_ship):
88
"""
@@ -36,41 +36,21 @@ def docked_actions(current_ship):
3636
undock_process_list[current_ship] = current_ship.planet
3737
command_queue.append(ship.undock(current_ship.planet))
3838

39-
def undocked_actions(current_ship):
39+
def target_planet(current_ship, planets_ranked_by_distance, planets_ranked_ours_by_docked, planets_ranked_untapped):
4040
"""
41-
Determine what to do with the undocked ship
41+
Determine if a planet is a viable target for mining; create the navigation command if so
4242
:param Ship current_ship:
43-
:return: command to append to the command_queue
44-
:rtype: List
43+
:param List of Tuples planets_ranked_by_distance:
44+
:param List of Tuples planets_ranked_ours_by_docked:
45+
:param List of Tuples planets_ranked_untapped:
46+
:return: navigation command for command_queue or None
47+
:rtype: String or None
4548
"""
46-
if DEBUGGING['method_entry']:
47-
log.debug("undocked_actions():")
48-
49-
if DEBUGGING['ship_loop']:
50-
log.debug("-+=Undocked ship #" + str(current_ship.id) + "=+-")
51-
52-
#did we just complete undocking?
53-
if current_ship in undock_process_list.keys():
54-
if DEBUGGING['docking_procedures']:
55-
log.debug(" - completed undocking")
56-
57-
undock_process_list.remove(current_ship)
58-
59-
success = False
60-
ranked_planets_by_distance = entity_sort_by_distance(current_ship, game_map.all_planets())
61-
ranked_our_planets_by_docked = planet_sort_ours_by_docked(game_map.all_planets())
62-
enemies = get_enemy_ships()
6349
navigate_command = None
6450

65-
#avoid boobytraps in our considerations
66-
#if len(planets_to_avoid) > 0:
67-
ranked_untapped_planets = remove_tapped_planets(ranked_planets_by_distance, planets_to_avoid)
68-
#else:
69-
# ranked_untapped_planets = ranked_planets_by_distance
70-
7151
#do we navigate to a planet, reinforce, or go offensive?
7252
#navigate to a planet or begin docking (this also currently handles reinforcing)
73-
for potential_planet in remove_held_planets(ranked_untapped_planets):
53+
for potential_planet in remove_held_planets(planets_ranked_untapped):
7454
if (potential_planet['entity_object'] in targeted_list) or \
7555
(potential_planet['entity_object'].num_docking_spots == len(potential_planet['entity_object'].all_docked_ships())):
7656
if DEBUGGING['targeting']:
@@ -85,7 +65,7 @@ def undocked_actions(current_ship):
8565
navigate_command = current_ship.dock(potential_planet['entity_object'])
8666
if potential_planet['entity_object'] in targeted_list:
8767
if DEBUGGING['planet_selection']:
88-
log.debug(" - removing planet #" + str(potential_planet['entity_object'].id + " from targeted_list"))
68+
log.debug(" - removing planet #" + str(potential_planet['entity_object'].id) + " from targeted_list")
8969

9070
targeted_list.remove(potential_planet['entity_object'])
9171
break
@@ -101,37 +81,117 @@ def undocked_actions(current_ship):
10181
ignore_ships = False)
10282
break
10383

104-
if not navigate_command:
105-
#potential_angle = other_entities_in_vicinity(current_ship, enemies, ranked_untapped_planets[0]['distance'])
106-
if ALGORITHM['offense']: # and potential_angle:
107-
#another entity is closer or at the same distance; we need to go offensive
108-
if DEBUGGING['offense']:
109-
log.debug("Engaging enemy")
84+
return navigate_command
11085

111-
navigate_command = current_ship.navigate(
112-
current_ship.closest_point_to(entity_sort_by_distance(current_ship, enemies)[0]['entity_object']),
113-
game_map,
114-
speed = default_speed,
115-
ignore_ships = False)
116-
elif ALGORITHM['reinforce'] and len(ranked_our_planets_by_docked) > 0:
117-
#reinforce that sucker
118-
if DEBUGGING['reinforce']:
119-
log.debug("Reinforcing planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
120-
121-
if current_ship.can_dock(ranked_our_planets_by_docked[0]['entity_object']):
122-
if DEBUGGING['reinforce']:
123-
log.debug(" - docking @ planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
86+
def go_offensive(current_ship, enemies):
87+
"""
88+
Returns navigation command for offense, or None if not the best course of action at this juncture
89+
:param Ship current_ship:
90+
:param List of Tuples enemies:
91+
:return navigation_command or None:
92+
:rtype: String or None
93+
"""
94+
navigate_command = None
95+
96+
if DEBUGGING['offense']:
97+
log.debug("Engaging enemy")
98+
99+
closest_enemy = entity_sort_by_distance(current_ship, enemies)[0]['entity_object']
100+
101+
if ALGORITHM['kamikaze']:
102+
potential_kamikaze_angle = other_entities_in_vicinity(current_ship, enemies, 100)
103+
104+
if DEBUGGING['kamikaze']:
105+
log.debug(" - potential_kamikaze_angle: " + str(potential_kamikaze_angle))
106+
107+
if potential_kamikaze_angle:
108+
if DEBUGGING['kamikaze'] and DEBUGGING['offense']:
109+
log.debug(" - going kamikaze")
110+
111+
navigate_command = current_ship.thrust(hlt.constants.MAX_SPEED, potential_kamikaze_angle)
112+
113+
if not navigate_command:
114+
if DEBUGGING['offense']:
115+
log.debug(" - engaging ship #" + str(closest_enemy.id))
116+
117+
navigate_command = current_ship.navigate(
118+
current_ship.closest_point_to(closest_enemy),
119+
game_map,
120+
speed = default_speed,
121+
ignore_ships = False)
122+
123+
return navigate_command
124+
125+
def reinforce_planet(current_ship, our_planets_by_docked, our_ranked_untapped_planets):
126+
"""
127+
Create navigation command to reinforce the nearest planet with an open docking spot
128+
NOTE: This is currently not utilized, and almost certainly broken
129+
:param Ship current_ship: derp
130+
:param List our_planets_by_docked: List of Tuples
131+
:param List our_ranked_untapped_planets: List of Tuples
132+
:return String navigation_command:
133+
:rtype: String
134+
"""
135+
navigation_command = None
136+
137+
#reinforce that sucker
138+
if DEBUGGING['reinforce']:
139+
log.debug("Reinforcing planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
140+
141+
if current_ship.can_dock(ranked_our_planets_by_docked[0]['entity_object']):
142+
if DEBUGGING['reinforce']:
143+
log.debug(" - docking @ planet #" + str(ranked_our_planets_by_docked[0]['entity_object'].id))
144+
145+
navigate_command = current_ship.dock(ranked_our_planets_by_docked[0]['entity_object'])
146+
else:
147+
if DEBUGGING['reinforce']:
148+
log.debug(" - navigating to reinforce planet #" + str(ranked_untapped_planets[0]['entity_object']))
149+
150+
navigate_command = current_ship.navigate(
151+
current_ship.closest_point_to(ranked_untapped_planets[0]['entity_object']),
152+
game_map,
153+
speed = default_speed,
154+
ignore_ships = False)
155+
156+
return navigation_command
157+
158+
def undocked_actions(current_ship):
159+
"""
160+
Determine what to do with the undocked ship
161+
:param Ship current_ship:
162+
:return: command to append to the command_queue
163+
:rtype: List
164+
"""
165+
if DEBUGGING['method_entry']:
166+
log.debug("undocked_actions():")
167+
168+
if DEBUGGING['ship_loop']:
169+
log.debug("-+=Undocked ship #" + str(current_ship.id) + "=+-")
170+
171+
#did we just complete undocking?
172+
if current_ship in undock_process_list.keys():
173+
if DEBUGGING['docking_procedures']:
174+
log.debug(" - completed undocking")
124175

125-
navigate_command = current_ship.dock(ranked_our_planets_by_docked[0]['entity_object'])
126-
else:
127-
if DEBUGGING['reinforce']:
128-
log.debug(" - navigating to reinforce planet #" + str(ranked_untapped_planets[0]['entity_object']))
176+
undock_process_list.remove(current_ship)
129177

130-
navigate_command = current_ship.navigate(
131-
current_ship.closest_point_to(ranked_untapped_planets[0]['entity_object']),
132-
game_map,
133-
speed = default_speed,
134-
ignore_ships = False)
178+
success = False
179+
ranked_planets_by_distance = entity_sort_by_distance(current_ship, game_map.all_planets())
180+
ranked_our_planets_by_docked = planet_sort_ours_by_docked(game_map.all_planets())
181+
ranked_untapped_planets = remove_tapped_planets(ranked_planets_by_distance, planets_to_avoid)
182+
enemies = get_enemy_ships()
183+
184+
#get our command, if navigation to/docking with a planet is the best course of action
185+
#else None
186+
navigate_command = target_planet(current_ship, ranked_planets_by_distance, ranked_our_planets_by_docked, \
187+
ranked_untapped_planets)
188+
189+
if not navigate_command:
190+
#potential_angle = other_entities_in_vicinity(current_ship, enemies, ranked_untapped_planets[0]['distance'])
191+
if ALGORITHM['offense']: # and potential_angle:
192+
navigate_command = go_offensive(current_ship, enemies)
193+
elif ALGORITHM['reinforce'] and len(ranked_our_planets_by_docked) > 0:
194+
navigate_command = reinforce_planet(current_ship, ranked_our_planets_by_docked, ranked_untapped_planets)
135195

136196
return navigate_command
137197

@@ -200,15 +260,41 @@ def other_entities_in_vicinity(current_entity, other_entities, scan_distance):
200260
:param Entity current_entity:
201261
:param List other_entities:
202262
:param integer scan_distance:
203-
:return: Angle between this entity and the first other found within collision risk area, or None
204-
:rtype: float
263+
:return: Collision angle if any
264+
:rtype: Collision angle or none
205265
"""
206266
if DEBUGGING['method_entry']:
207267
log.debug("other_entities_in_vicinity()")
208-
268+
269+
#closest_docked_distance = scan_distance
270+
target_planet = None
271+
209272
for other_entity in other_entities:
210-
if current_entity.calculate_distance_between(other_entity) <= scan_distance:
211-
return current_entity.calculate_angle_between(other_entity)
273+
#if other_entity.docking_status == current_entity.DockingStatus.DOCKED or \
274+
# other_entity.docking_status == current_entity.DockingStatus.DOCKING:
275+
if current_entity.planet:
276+
continue
277+
278+
proximity = int(current_entity.calculate_distance_between(other_entity))
279+
if DEBUGGING['kamikaze']:
280+
log.debug("\t- current_entity's proximity: " + str(proximity) + " vs scan_distance: " + str(scan_distance))
281+
282+
if proximity < scan_distance:
283+
if DEBUGGING['kamikaze']:
284+
log.debug("\t- proximity is less than scan_distance")
285+
286+
if current_entity.docking_status == current_entity.DockingStatus.DOCKED or \
287+
current_entity.docking_status == current_entity.DockingStatus.DOCKING:
288+
if DEBUGGING['kamikaze']:
289+
log.debug("\t\t- setting target_planet to current_entity.planet")
290+
291+
target_planet = current_entity.planet
292+
break
293+
else:
294+
continue
295+
296+
if target_planet:
297+
return current_entity.calculate_angle_between(target_planet)
212298

213299
return None
214300

@@ -268,17 +354,19 @@ def remove_tapped_planets(testing_planets, avoid_planets):
268354
#constants
269355
DEBUGGING = {
270356
'ship_loop': True,
271-
'docking_procedures': True,
272-
'reinforce': True,
357+
'docking_procedures': False,
358+
'reinforce': False,
273359
'offense': True,
360+
'kamikaze': False,
274361
'planet_selection': True,
275362
'targeting': True,
276-
'boobytrapping': True,
363+
'boobytrapping': False,
277364
'method_entry': True
278365
}
279366
ALGORITHM = {
280367
'reinforce': False,
281368
'offense': True,
369+
'kamikaze': False,
282370
'boobytrapping': True
283371
}
284372

@@ -288,6 +376,7 @@ def remove_tapped_planets(testing_planets, avoid_planets):
288376
planets_to_avoid = []
289377
dock_process_list = {}
290378
undock_process_list = {}
379+
enemy_data = {}
291380

292381
#init
293382
log = logging.getLogger(__name__)
@@ -321,6 +410,3 @@ def remove_tapped_planets(testing_planets, avoid_planets):
321410
#end per-ship iteration
322411

323412
game.send_command_queue(command_queue)
324-
325-
326-

version.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v2.0

0 commit comments

Comments
 (0)