def assign_to_quadrant(gc, unit, unit_loc): """ Assigns knight to a quadrant in need of help. """ quadrant_battles = variables.quadrant_battle_locs assigned_knights = variables.assigned_knights if variables.curr_planet == bc.Planet.Earth: diagonal = (variables.earth_diagonal) else: diagonal = (variables.mars_diagonal) best_quadrant = (None, None) best_coeff = -float('inf') for quadrant in quadrant_battles: q_info = quadrant_battles[quadrant] if q_info.target_loc is not None: coeff = q_info.urgency_coeff("knight") bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value(unit_loc) target_coords_val = Ranger.get_coord_value(q_info.target_loc) if bfs_array[our_coords_val, target_coords_val] != float('inf'): distance = bfs_array[our_coords_val, target_coords_val] coeff += 3 * (1 - distance / diagonal) if coeff > best_coeff: best_quadrant = quadrant best_coeff = coeff if best_coeff > 0: assigned_knights[unit.id] = quadrant_battles[best_quadrant].target_loc return True, assigned_knights[unit.id] return False, None
def is_accessible(unit_loc, target_loc): bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value(unit_loc) target_coords_val = Ranger.get_coord_value(target_loc) if bfs_array[our_coords_val, target_coords_val] != float('inf'): return True return False
def assign_to_quadrant(gc, unit, unit_loc): """ Assigns knight to a quadrant in need of help. """ quadrant_battles = variables.quadrant_battle_locs assigned_healers = variables.assigned_healers best_quadrant = (None, None) best_coeff = -float('inf') for quadrant in quadrant_battles: q_info = quadrant_battles[quadrant] coeff = q_info.urgency_coeff("healer") # distance = ADD DISTANCE COEFF TOO if coeff > best_coeff and q_info.healer_loc is not None: bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value(unit_loc) target_coords_val = Ranger.get_coord_value(q_info.healer_loc) if bfs_array[our_coords_val, target_coords_val] != float('inf'): best_quadrant = quadrant best_coeff = coeff if best_coeff > 0: assigned_healers[unit.id] = quadrant_battles[best_quadrant].healer_loc quadrant_battles[best_quadrant].assigned_healers.add(unit.id) return True, assigned_healers[unit.id] return False, None
def go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, bfs_array, targeting_units, rocket_locs): # print('GOING TO MARS') signals = {} dir = None attack = None blink = None closest_enemy = None move_then_attack = False visible_enemies = False if len(enemies) > 0: visible_enemies = True attack = Ranger.get_attack(gc, unit, location, targeting_units) start_coords = (location.x, location.y) # # rocket was launched if unit.id not in variables.which_rocket or variables.which_rocket[ unit.id][1] not in variables.rocket_locs: variables.mage_roles["go_to_mars"].remove(unit.id) return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals target_loc = variables.which_rocket[unit.id][0] # # rocket was destroyed if not gc.has_unit_at_location(target_loc): variables.mage_roles["go_to_mars"].remove(unit.id) return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals # print(unit.id) # print('MY LOCATION:', start_coords) # print('GOING TO:', target_loc) if max(abs(target_loc.x - start_coords[0]), abs(target_loc.y - start_coords[1])) == 1: rocket = gc.sense_unit_at_location(target_loc) if gc.can_load(rocket.id, unit.id): gc.load(rocket.id, unit.id) else: target_coords = (target_loc.x, target_loc.y) start_coords_val = Ranger.get_coord_value(start_coords) target_coords_val = Ranger.get_coord_value(target_coords) # target_coords_thirds = (int(target_loc.x / bfs_fineness), int(target_loc.y / bfs_fineness)) if bfs_array[start_coords_val, target_coords_val] != float('inf'): best_dirs = Ranger.use_dist_bfs(start_coords, target_coords, bfs_array) choice_of_dir = random.choice(best_dirs) shape = direction_to_coord[choice_of_dir] options = sense_util.get_best_option(shape) for option in options: if gc.can_move(unit.id, option): dir = option break # print(dir) return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals
def is_accessible_from_init_loc(self, coords): """ Determines if location 'coords' is accessible from any of our initial locations. """ accessible = False if variables.curr_planet == bc.Planet.Earth: for init_loc in variables.our_init_locs: bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value( (init_loc.x, init_loc.y)) target_coords_val = Ranger.get_coord_value(coords) if bfs_array[our_coords_val, target_coords_val] != float('inf'): accessible = True else: accessible = True return accessible
def try_move_smartly(unit, map_loc1, map_loc2): if variables.gc.is_move_ready(unit.id): our_coords = map_loc1 target_coords = map_loc2 bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value(our_coords) target_coords_val = Ranger.get_coord_value(target_coords) if bfs_array[our_coords_val, target_coords_val]!=float('inf'): best_dirs = Ranger.use_dist_bfs(our_coords, target_coords, bfs_array) choice_of_dir = random.choice(best_dirs) shape = variables.direction_to_coord[choice_of_dir] options = sense_util.get_best_option(shape) for option in options: if variables.gc.can_move(unit.id, option): variables.gc.move_robot(unit.id, option) add_new_location(unit.id, our_coords, option) break
def update_healer_ideal_loc(self): if variables.curr_planet == bc.Planet.Earth: passable_locations = variables.passable_locations_earth else: passable_locations = variables.passable_locations_mars neighbor_quadrants = self.get_neighboring_quadrants() enemies = set() most_enemies = 0 worst_quadrant = None for quadrant in neighbor_quadrants: if quadrant in variables.quadrant_battle_locs: q_enemies = variables.quadrant_battle_locs[quadrant].enemies enemies.update(q_enemies) if len(q_enemies) > most_enemies: most_enemies = len(q_enemies) worst_quadrant = quadrant if len(enemies) == 0: return worst_middle = variables.quadrant_battle_locs[worst_quadrant].middle furthest_away = sorted( list(self.quadrant_locs), key=lambda x: sense_util.distance_squared_between_coords( x, worst_middle), reverse=True) for loc in furthest_away: accessible = False if variables.curr_planet == bc.Planet.Earth: for init_loc in variables.our_init_locs: bfs_array = variables.bfs_array our_coords_val = Ranger.get_coord_value( (init_loc.x, init_loc.y)) target_coords_val = Ranger.get_coord_value(loc) if bfs_array[our_coords_val, target_coords_val] != float('inf'): accessible = True else: accessible = True if passable_locations[loc] and accessible: self.healer_loc = loc break