def get_attack(gc, unit, location, targeting_units, priority = knight_unit_priority): enemy_team = variables.enemy_team vuln_enemies = gc.sense_nearby_units_by_team(location, unit.attack_range(), enemy_team) if len(vuln_enemies)==0: return None #return vuln_enemies[0] best = None lowest_health = float('inf') for enemy in vuln_enemies: if enemy.id in targeting_units: if enemy.unit_type == variables.unit_types["knight"]: mult = 30 - enemy.knight_defense() else: mult = 30 remaining_health = enemy.health - targeting_units[enemy.id] * mult if remaining_health > 0 and remaining_health < lowest_health: best = enemy lowest_health = remaining_health if best is not None: return best return max(vuln_enemies, key=lambda x: Ranger.coefficient_computation(gc, unit, x, x.location.map_location(), location))
def knight_sense(gc, unit, battle_locs, knight_roles, location, direction_to_coord, bfs_array, targeting_units, rocket_locs): enemies = gc.sense_nearby_units_by_team(location, unit.vision_range, variables.enemy_team) if unit.id in knight_roles["go_to_mars"]: return go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, bfs_array, targeting_units, rocket_locs) signals = {} dir = None attack = None javelin = None closest_enemy = None move_then_attack = False visible_enemies = False # if variables.print_count < 10: # print("Sensing nearby units:", time.time() - start_time) if len(enemies) > 0: visible_enemies = True closest_enemy = None closest_dist = -float('inf') for enemy in enemies: enemy_loc = enemy.location if enemy_loc.is_on_map(): enemy_map_loc = enemy_loc.map_location() coeff = Ranger.coefficient_computation(gc, unit, enemy, enemy_map_loc, location) # dist = sense_util.distance_squared_between_maplocs(loc.map_location(), location) if coeff > closest_dist: closest_dist = coeff closest_enemy = enemy # if variables.print_count < 10: # print("Getting closest enemy:", time.time() - start_time) # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location)) # closest_enemy = closest_among_ungarrisoned(sorted_enemies) attack = get_attack(gc, unit, location, targeting_units, knight_unit_priority) if attack is not None: if closest_enemy is not None: dir = Ranger.go_to_loc(unit, location, closest_enemy.location.map_location()) # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) else: if gc.is_move_ready(unit.id): if closest_enemy is not None: dir = Ranger.go_to_loc(unit, location, closest_enemy.location.map_location()) # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) if dir is None or dir == variables.directions[8]: dir = Ranger.optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) next_turn_loc = location.add(dir) attack = get_attack(gc, unit, next_turn_loc, targeting_units, knight_unit_priority) if attack is not None: move_then_attack = True else: if variables.curr_planet == bc.Planet.Earth: # print('IS RUNNING TOWARDS INIT LOC') dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord) else: # print('EXPLORING') dir = Ranger.get_explore_dir(gc, unit, location) # if variables.print_count < 10: # print("Getting direction:", time.time() - start_time) else: # if there are no enemies in sight, check if there is an ongoing battle. If so, go there. if len(rocket_locs) > 0 and gc.round() > 660 and variables.curr_planet == bc.Planet.Earth: dir = Ranger.move_to_rocket(gc, unit, location, direction_to_coord, bfs_array) if dir is not None: return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals if len(battle_locs) > 0: # print('IS GOING TO BATTLE') dir = Ranger.go_to_battle_new(gc, unit, battle_locs, location, direction_to_coord) # queued_paths[unit.id] = target else: # dir = move_away(gc, unit, battle_locs) if variables.curr_planet == bc.Planet.Earth: # print('IS RUNNING TOWARDS INIT LOC') dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord) else: # print('EXPLORING') dir = Ranger.get_explore_dir(gc, unit, location) # if variables.print_count < 10: # print("regular movement:", time.time() - start_time) return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals