def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in self.universe.find_planets(player): if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available_at_turn[planet][distance-planet.distance(planet_to_attack)] result += ships_avail return result
def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in self.universe.find_planets(player): if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available[planet] + (distance-planet.distance(planet_to_attack)) * planet.growth_rate ships_avail -= self.get_scheduled_fleets_shipcount_from_within_distance(planet, distance-planet.distance(planet_to_attack)) result += ships_avail return result
def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in (list(self.universe.find_planets(player)) + self.get_neutrals_under_player_attack(player)): if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available_at_turn[planet][distance-planet.distance(planet_to_attack)] # if planet_to_attack.id == 0: # log.info("get avail from %s = %s" % (planet, ships_avail)) result += ships_avail return result
def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in self.universe.find_planets(player): if planet.id != planet_to_attack.id and planet.distance(planet_to_attack) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available[planet] if len(planet.attacking_fleets) == 0 and len(self.get_scheduled_fleets_from(planet)) == 0: ships_avail += (distance-planet.distance(planet_to_attack)) * planet.growth_rate result += ships_avail return result
def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in self.universe.find_planets(player): if planet.id != planet_to_attack.id and planet.distance( planet_to_attack ) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available_at_turn[planet][ distance - planet.distance(planet_to_attack)] result += ships_avail return result
def get_available_ships_within_distance(self, planet_to_attack, player, distance): result = 0 for planet in self.universe.find_planets(player): if planet.id != planet_to_attack.id and planet.distance( planet_to_attack ) <= distance and self.ships_needed[planet] == 0: ships_avail = self.ships_available[planet] + ( distance - planet.distance(planet_to_attack)) * planet.growth_rate ships_avail -= self.get_scheduled_fleets_shipcount_from_within_distance( planet, distance - planet.distance(planet_to_attack)) result += ships_avail return result
def doPrep(self): log.info("Prep phase") self.max_distance_between_planets = 0 for p1 in self.all_planets: for p2 in self.all_planets: self.max_distance_between_planets = max(self.max_distance_between_planets, p1.distance(p2)) #log.info("Max distance: %s" % self.max_distance_between_planets) # calculate current high level metrics self.my_total_ships_available = 0 self.my_total_ships = 0 self.my_total_growth_rate = 0 self.enemy_total_ships_available = 0 self.enemy_total_ships = 0 self.enemy_total_growth_rate = 0 self.ships_available = {} self.ships_needed = {} self.planet_timeline = {} for planet in self.all_planets: if len(planet.attacking_fleets) == 0: self.ships_available[planet] = planet.ship_count self.ships_needed[planet] = 0 simulation_distance = self.max_distance_between_planets self.planet_timeline[planet] = planet.in_future_timeline(simulation_distance) else: simulation_distance = self.max_distance_between_planets self.planet_timeline[planet] = planet.in_future_timeline(simulation_distance) max_needed = 0 min_available = 1000000 #log.info("timeline for %s: %s" % (planet, self.planet_timeline[planet])) for step in self.planet_timeline[planet]: owner = step[0] ship_count = step[1] if owner != planet.owner: max_needed = max(max_needed, ship_count) else: min_available = min(min_available, ship_count) if max_needed > 0: # do we bail if we are going to lose this planet anyway? self.ships_available[planet] = 0 self.ships_needed[planet] = max_needed else: self.ships_available[planet] = min_available self.ships_needed[planet] = 0 if (planet.owner == player.ME): self.my_total_ships_available += self.ships_available[planet] self.my_total_growth_rate += planet.growth_rate self.my_total_ships += planet.ship_count else: self.enemy_total_ships_available += self.ships_available[planet] self.enemy_total_growth_rate += planet.growth_rate self.enemy_total_ships += planet.ship_count #log.info("avail ships for %s: %s" % (planet, self.ships_available[planet])) # prevent initial overexpansion if self.universe.game.turn_count <= 2: for my_planet in self.my_planets: for enemy_planet in self.enemy_planets: max_enemy_fleet = self.ships_available[enemy_planet] distance = my_planet.distance(enemy_planet) ships_needed_for_safety = max_enemy_fleet-distance*my_planet.growth_rate if ships_needed_for_safety > (my_planet.ship_count - self.ships_available[my_planet]): deficit = ships_needed_for_safety - (my_planet.ship_count - self.ships_available[my_planet]) #log.info("deficit for %s: %s" % (my_planet, deficit)) if deficit > self.ships_available[my_planet]: deficit = self.ships_available[my_planet] self.ships_available[my_planet] -= deficit self.my_total_ships_available -= deficit self.my_total_ships += self.total_fleet_ship_count(player.ME) self.enemy_total_ships += self.total_fleet_ship_count(player.NOT_ME) # calculate enemy's center of mass weighted_x = 0 weighted_y = 0 div = 0 for planet in self.enemy_planets: weighted_x += planet.position.x * (self.ships_available[planet] + planet.growth_rate) weighted_y += planet.position.y * (self.ships_available[planet] + planet.growth_rate) div += self.ships_available[planet] + planet.growth_rate if div == 0: div = 1 self.enemy_com = Planet(self.universe, 666, weighted_x/div, weighted_y/div, 2, 0, 0) weighted_x = 0 weighted_y = 0 div = 0 for planet in self.enemy_planets: weighted_x += planet.position.x * (planet.growth_rate+1) weighted_y += planet.position.y * (planet.growth_rate+1) div += planet.growth_rate+1 self.enemy_growth_com = Planet(self.universe, 666, weighted_x/div, weighted_y/div, 2, 0, 0) # For every planet, and every turn, calculate how many ships the enemy CAN sent to it's aid self.max_aid_at_turn = {} self.max_aid_at_turn_single = {} enemy_planets_incl_candidates = list(self.enemy_planets) + self.get_neutrals_under_enemy_attack() for planet in self.all_planets: self.max_aid_at_turn[planet] = {} self.max_aid_at_turn_single[planet] = {} for turn in range(1, self.max_distance_between_planets+1): max_aid = 0 max_aid_single = 0 #for enemy_planet in self.all_planets: for enemy_planet in enemy_planets_incl_candidates: if enemy_planet.id != planet.id and planet.distance(enemy_planet) < turn: enemy_planet_time_step = self.planet_timeline[enemy_planet][turn - planet.distance(enemy_planet)] if (enemy_planet_time_step[0] in player.ENEMIES): max_aid += enemy_planet_time_step[1] max_aid_single = max(max_aid_single, enemy_planet_time_step[1]) #log.info("adding to max aid: %s" % enemy_planet_time_step[1]) else: if enemy_planet.id != planet.id and planet.distance(enemy_planet) == turn: enemy_planet_time_step = self.planet_timeline[enemy_planet][0] if (enemy_planet_time_step[0] in player.ENEMIES): max_aid += enemy_planet.ship_count max_aid_single = max(max_aid_single, enemy_planet.ship_count) if self.planet_timeline[planet][turn-1][0] in player.ENEMIES: max_aid += self.planet_timeline[planet][turn-1][1] max_aid_single = max(max_aid_single, self.planet_timeline[planet][turn-1][1]) #log.info("self aid: %s" % self.planet_timeline[planet][turn-1][1]) self.max_aid_at_turn[planet][turn] = max_aid self.max_aid_at_turn_single[planet][turn] = max_aid_single #log.info("Max aid for %s at %s: %s" % (planet.id, turn, self.max_aid_at_turn[planet][turn])) #log.info("Max aid: %s" % self.max_aid_at_turn) # For every planet, and every turn, calculate how many ships I CAN send to its aid self.my_max_aid_at_turn = {} my_planets_incl_candidates = list(self.my_planets) + self.get_neutrals_under_my_attack() for planet in self.all_planets: self.my_max_aid_at_turn[planet] = {} for turn in range(1, self.max_distance_between_planets+1): max_aid = 0 #for my_planet in self.my_planets: for my_planet in my_planets_incl_candidates: if my_planet.id != planet.id and planet.distance(my_planet) < turn: my_planet_time_step = self.planet_timeline[my_planet][turn - planet.distance(my_planet)] if (my_planet_time_step[0] == player.ME): max_aid += my_planet_time_step[1] #max_aid = max(max_aid, my_planet_time_step[1]) #log.info("adding to my max aid: %s" % my_planet_time_step[1]) else: if my_planet.id != planet.id and planet.distance(my_planet) == turn: my_planet_time_step = self.planet_timeline[my_planet][0] if (my_planet_time_step[0] == player.ME): max_aid += my_planet.ship_count #max_aid = max(max_aid, my_planet.ship_count) #if self.planet_timeline[planet][turn-1][0] == player.ME: #max_aid += self.planet_timeline[planet][turn-1][1] #log.info("self aid: %s" % self.planet_timeline[planet][turn-1][1]) self.my_max_aid_at_turn[planet][turn] = max_aid #log.info("My Max aid for %s at %s: %s" % (planet.id, turn, self.my_max_aid_at_turn[planet][turn])) #log.info("My Max aid: %s" % self.my_max_aid_at_turn) log.info("MY STATUS: %s/%s - %s available" % (self.my_total_ships, self.my_total_growth_rate, self.my_total_ships_available)) log.info("ENEMY STATUS: %s/%s - %s available" % (self.enemy_total_ships, self.enemy_total_growth_rate, self.enemy_total_ships_available))
def doPrep(self): log.info("Prep phase") if self.current_turn == 1: self.my_home = list(self.my_planets)[0] self.enemy_home = list(self.enemy_planets)[0] self.max_distance_between_planets = 0 for p1 in self.all_planets: for p2 in self.all_planets: self.max_distance_between_planets = max(self.max_distance_between_planets, p1.distance(p2)) #log.info("Max distance: %s" % self.max_distance_between_planets) # calculate current high level metrics self.total_ships = {PLAYER1:0, PLAYER2:0} self.total_growth_rate = {PLAYER1:0, PLAYER2:0} self.ships_available_at_turn = {} self.ships_needed = {} self.ships_needed_at_turn = {} self.ships_needed_later = {} self.planet_timeline = {} for planet in self.all_planets: self.ships_available_at_turn[planet] = {} scheduled_fleets_to_planet = self.get_scheduled_fleets_to(planet) scheduled_fleets_from_planet = self.get_scheduled_fleets_from(planet) self.planet_timeline[planet] = planet.in_future_timeline(self.max_distance_between_planets + 20, scheduled_fleets_to_planet, scheduled_fleets_from_planet) need_help = False if planet.id == 7: log.info("timeline for %s: %s" % (planet, self.planet_timeline[planet])) #log.info("attacking fleets by me: %s" % (self.universe.find_fleets(PLAYER1, destination=planet))) prev_owner = planet.owner for step in self.planet_timeline[planet]: owner = step[0] ship_count = step[1] if owner != prev_owner and prev_owner == planet.owner and prev_owner != NOBODY and not need_help: self.ships_needed[planet] = ship_count self.ships_needed_at_turn[planet] = self.planet_timeline[planet].index(step) + 1 need_help = True self.ships_needed_later[planet] = [] #log.info("Planet %s needs help %s at %s" % (planet, ship_count, self.ships_needed_at_turn[planet])) prev_owner = owner if not need_help: self.ships_needed[planet] = 0 min_available = 1000000 step_index = len(self.planet_timeline[planet]) for step in reversed(self.planet_timeline[planet]): ship_count = step[1] min_available = min(min_available, ship_count) if step[0] == NOBODY: min_available = 0 if min_available < 0: log.info("Negative min_available: %s for %s" % (min_available, planet)) min_available = 0 self.ships_available_at_turn[planet][step_index] = min_available #log.info("avail for %s at %s: %s" % (planet, step_index, min_available)) step_index -= 1 self.ships_available_at_turn[planet][0] = max(0,min(planet.ship_count, self.ships_available_at_turn[planet][1] - planet.growth_rate)) else: for step_index in range(0, len(self.planet_timeline[planet])+1): self.ships_available_at_turn[planet][step_index] = 0 if planet.owner != NOBODY: self.total_ships[planet.owner] += planet.ship_count self.total_growth_rate[planet.owner] += planet.growth_rate # if planet.id == 14: # log.info("avail timeline for %s is: %s" % (planet, self.ships_available_at_turn[planet])) self.total_ships[PLAYER1] += self.total_fleet_ship_count(PLAYER1) self.total_ships[PLAYER2] += self.total_fleet_ship_count(PLAYER2) for my_planet in [self.my_home]: for enemy_planet in [self.enemy_home]: # if self.ships_available_at_turn[enemy_planet][0] < self.ships_available_at_turn[my_planet][0]: # continue if my_planet.owner != PLAYER1 or enemy_planet.owner != PLAYER2: continue max_enemy_fleet = self.ships_available_at_turn[enemy_planet][0] distance = my_planet.distance(enemy_planet) ships_needed_for_safety = max_enemy_fleet-(self.planet_timeline[my_planet][distance-1][1] - my_planet.ship_count) - enemy_planet.growth_rate #ships_needed_for_safety = max_enemy_fleet-(self.planet_timeline[my_planet][distance-1][1] - my_planet.ship_count) if ships_needed_for_safety > (my_planet.ship_count - self.ships_available_at_turn[my_planet][0]): deficit = ships_needed_for_safety - (my_planet.ship_count - self.ships_available_at_turn[my_planet][0]) #log.info("deficit for %s: %s, max enemy fleet %s" % (my_planet, deficit, max_enemy_fleet)) if deficit > self.ships_available_at_turn[my_planet][0]: deficit = self.ships_available_at_turn[my_planet][0] self.decrease_ships_available(my_planet, 0, deficit) # calculate enemy's center of mass weighted_x = 0 weighted_y = 0 div = 0 for planet in self.enemy_planets: weighted_x += planet.position.x * (self.ships_available_at_turn[planet][0] + planet.growth_rate) weighted_y += planet.position.y * (self.ships_available_at_turn[planet][0] + planet.growth_rate) div += self.ships_available_at_turn[planet][0] + planet.growth_rate if div == 0: div = 1 self.enemy_com = Planet(self.universe, 666, weighted_x/div, weighted_y/div, 2, 0, 0) # For every planet, and every turn, calculate how many ships each player can send to it # TODO should we use ships_available_at_turn here? self.max_aid_at_turn = {PLAYER1:{}, PLAYER2:{}} for player in (PLAYER1 | PLAYER2): source_planets = list(self.universe.find_planets(player)) + self.get_neutrals_under_player_attack(player) for planet in self.all_planets: self.max_aid_at_turn[player][planet] = {} for turn in range(1, self.max_distance_between_planets+21): max_aid = 0 for source_planet in source_planets: if source_planet.id != planet.id and planet.distance(source_planet) < turn: source_planet_time_step = self.planet_timeline[source_planet][turn - planet.distance(source_planet) - 1] if (source_planet_time_step[0] == player): #log.info("Max aid by %s for %s from %s at %s: %s" % (player.id, planet.id, source_planet.id, turn, source_planet_time_step[1])) max_aid += source_planet_time_step[1] else: if source_planet.id != planet.id and planet.distance(source_planet) == turn: if (source_planet.owner == player): max_aid += source_planet.ship_count self.max_aid_at_turn[player][planet][turn] = max_aid #log.info("Max aid by %s for %s at %s: %s" % (player.id, planet.id, turn, self.max_aid_at_turn[player][planet][turn])) log.info("MY STATUS: %s/%s" % (self.total_ships[PLAYER1], self.total_growth_rate[PLAYER1])) log.info("ENEMY STATUS: %s/%s" % (self.total_ships[PLAYER2], self.total_growth_rate[PLAYER2]))
def average_enemy_planet_distance(self, p): distance_sum = sum( [planet.distance(p) for planet in self.universe.enemy_planets]) if len(self.universe.enemy_planets) == 0: return -1 return distance_sum / len(self.universe.enemy_planets)
def average_enemy_planet_distance(self, p): distance_sum = sum( [ planet.distance(p) for planet in self.universe.enemy_planets ] ) if len(self.universe.enemy_planets) == 0: return -1 return distance_sum/len(self.universe.enemy_planets)
def doPrep(self): log.info("Prep phase") self.max_distance_between_planets = 0 for p1 in self.all_planets: for p2 in self.all_planets: self.max_distance_between_planets = max(self.max_distance_between_planets, p1.distance(p2)) #log.info("Max distance: %s" % self.max_distance_between_planets) # calculate current high level metrics self.total_ships = {PLAYER1:0, PLAYER2:0} self.total_growth_rate = {PLAYER1:0, PLAYER2:0} self.ships_available = {} self.ships_needed = {} self.ships_needed_at_turn = {} self.planet_timeline = {} for planet in self.all_planets: scheduled_fleets_to_planet = self.get_scheduled_fleets_to(planet) scheduled_fleets_from_planet = self.get_scheduled_fleets_from(planet) self.planet_timeline[planet] = planet.in_future_timeline(self.max_distance_between_planets + 20, scheduled_fleets_to_planet, scheduled_fleets_from_planet) need_help = False min_available = 1000000 #log.info("timeline for %s: %s" % (planet, self.planet_timeline[planet])) prev_owner = planet.owner for step in self.planet_timeline[planet]: owner = step[0] ship_count = step[1] if owner == PLAYER2 and prev_owner == PLAYER1 and not need_help: self.ships_needed[planet] = ship_count self.ships_needed_at_turn[planet] = self.planet_timeline[planet].index(step) + 1 need_help = True log.info("Planet %s needs help %s at %s" % (planet, ship_count, self.ships_needed_at_turn[planet])) if owner == planet.owner: min_available = min(min_available, ship_count) if min_available < 0: log.info("Negative min_available: %s for %s" % (min_available, planet)) min_available = 0 prev_owner = owner if need_help: self.ships_available[planet] = 0 else: self.ships_available[planet] = min(min_available, planet.ship_count) self.ships_needed[planet] = 0 if planet.owner != NOBODY: self.total_ships[planet.owner] += planet.ship_count self.total_growth_rate[planet.owner] += planet.growth_rate self.total_ships[PLAYER1] += self.total_fleet_ship_count(PLAYER1) self.total_ships[PLAYER2] += self.total_fleet_ship_count(PLAYER2) if self.universe.game.turn_count <= 2: for my_planet in self.my_planets: for enemy_planet in self.enemy_planets: max_enemy_fleet = self.ships_available[enemy_planet] distance = my_planet.distance(enemy_planet) ships_needed_for_safety = max_enemy_fleet-distance*my_planet.growth_rate if ships_needed_for_safety > (my_planet.ship_count - self.ships_available[my_planet]): deficit = ships_needed_for_safety - (my_planet.ship_count - self.ships_available[my_planet]) #log.info("deficit for %s: %s" % (my_planet, deficit)) if deficit > self.ships_available[my_planet]: deficit = self.ships_available[my_planet] self.ships_available[my_planet] -= deficit # calculate enemy's center of mass weighted_x = 0 weighted_y = 0 div = 0 for planet in self.enemy_planets: weighted_x += planet.position.x * (self.ships_available[planet] + planet.growth_rate) weighted_y += planet.position.y * (self.ships_available[planet] + planet.growth_rate) div += self.ships_available[planet] + planet.growth_rate if div == 0: div = 1 self.enemy_com = Planet(self.universe, 666, weighted_x/div, weighted_y/div, 2, 0, 0) # For every planet, and every turn, calculate how many ships each player can send to it self.max_aid_at_turn = {PLAYER1:{}, PLAYER2:{}} for player in (PLAYER1 | PLAYER2): source_planets = list(self.universe.find_planets(player)) + self.get_neutrals_under_player_attack(player) for planet in self.all_planets: self.max_aid_at_turn[player][planet] = {} for turn in range(1, self.max_distance_between_planets+21): max_aid = 0 for source_planet in source_planets: if source_planet.id != planet.id and planet.distance(source_planet) < turn: source_planet_time_step = self.planet_timeline[source_planet][turn - planet.distance(source_planet)] if (source_planet_time_step[0] == player): max_aid += source_planet_time_step[1] else: if source_planet.id != planet.id and planet.distance(source_planet) == turn: source_planet_time_step = self.planet_timeline[source_planet][0] if (source_planet_time_step[0] == player): max_aid += source_planet.ship_count self.max_aid_at_turn[player][planet][turn] = max_aid #log.info("Max aid by %s for %s at %s: %s" % (player.id, planet.id, turn, self.max_aid_at_turn[player][planet][turn])) log.info("MY STATUS: %s/%s" % (self.total_ships[PLAYER1], self.total_growth_rate[PLAYER1])) log.info("ENEMY STATUS: %s/%s" % (self.total_ships[PLAYER2], self.total_growth_rate[PLAYER2]))