def avoidance_explore(self, my_ant, enemy_ants): 'explore under enemies presence' score, distance = battle.eval_formation(self.gamestate, [my_ant], enemy_ants) if score < 1: # be safer is more enemies are around safe_distance = self.gamestate.euclidean_distance_add(self.gamestate.attackradius2, 1) else: # don't initiate 1 on 1 change, but don't be afraid safe_distance = self.gamestate.attackradius2 nav_info = [] for loc in self.gamestate.passable_neighbours(my_ant) + [my_ant]: influence = self.strat_influence.map[loc] distance = min([self.gamestate.euclidean_distance2(loc, enemy_ant) for enemy_ant in enemy_ants]) nav_info.append((influence, distance, loc)) # go for lowest influence that's safe nav_info.sort() best_loc = None for info in nav_info: (influence, distance, loc) = info if distance > safe_distance: best_loc = loc break # if no safe move try largest distance if best_loc is None: (influence, distance, loc) = sorted(nav_info, key=lambda x: x[1], reverse=True)[0] best_loc = loc # do the move directions = self.gamestate.direction(my_ant, best_loc) + [None] self.gamestate.issue_order((my_ant, directions[0])) logging.debug('moving %s' % str((my_ant, directions[0])))
def test_eval_formation_4(self): """ 11 0 """ pair = ([(3, 2)], [(0, 1), (0, 2)]) score, distance = battle_line.eval_formation(self.gamestate, pair[0], pair[1]) self.assertEqual(score, 0.5) self.assertEqual(distance, 9)
def test_eval_formation_1(self): """ 1 0 0 """ pair = ([(2, 0), (2, 2)], [(0, 1)]) score, distance = battle_line.eval_formation(self.gamestate, pair[0], pair[1]) self.assertEqual(score, 2.0) self.assertEqual(distance, 5)
def test_eval_formation_5(self): """ 0123456 1 1 0 """ pair = ([(3, 3)], [(0, 2), (1, 1)]) score, distance = battle_line.eval_formation(self.gamestate, pair[0], pair[1]) self.assertEqual(score, 0.5) self.assertEqual(distance, 8)