def in_future_timeline(self, turns=1, fleets=None, departingFleets=None): """Calculates state of planet in `turns' turns.""" result = [] planet = copy(self) arriving_fleets = list(self.universe.find_fleets(destination=self)) if fleets is not None: arriving_fleets += fleets for i in range(1, turns + 1): # account planet growth if planet.owner != player.NOBODY: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [x for x in arriving_fleets if x.turns_remaining == i] # if planet.id == 4: # log.info("arriving at %s fleets: %s" % (i,fleets)) # assuming 2-player scenario! ships = [] for id in [1, 2]: count = sum([ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ]) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count # if count > 0: ships.append({'player': PLAYER_MAP.get(id), 'ships': count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({ 'player': player.NOBODY, 'ships': planet.ship_count }) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s: s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.ship_count = 0 else: planet.owner = winner['player'] planet.ship_count = winner['ships'] - second['ships'] # get fleets which will depart in that turn if departingFleets is not None: fleets = [x for x in departingFleets if x.turns_remaining == i] for fleet in fleets: planet.ship_count -= fleet.ship_count result.append((planet.owner, planet.ship_count)) return result
def in_future_timeline(self, turns=1, fleets=None, departingFleets=None): """Calculates state of planet in `turns' turns.""" result = [] planet = copy(self) arriving_fleets = list(self.universe.find_fleets(destination=self)) if fleets is not None: arriving_fleets += fleets for i in range(1, turns+1): # account planet growth if planet.owner != player.NOBODY: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [ x for x in arriving_fleets if x.turns_remaining == i ] # if planet.id == 4: # log.info("arriving at %s fleets: %s" % (i,fleets)) # assuming 2-player scenario! ships = [] for id in [1,2]: count = sum( [ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ] ) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count # if count > 0: ships.append({'player':PLAYER_MAP.get(id), 'ships':count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({'player':player.NOBODY,'ships':planet.ship_count}) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s : s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.ship_count=0 else: planet.owner=winner['player'] planet.ship_count=winner['ships'] - second['ships'] # get fleets which will depart in that turn if departingFleets is not None: fleets = [ x for x in departingFleets if x.turns_remaining == i ] for fleet in fleets: planet.ship_count -= fleet.ship_count result.append((planet.owner, planet.ship_count)) return result
def in_future(self, turns=1, fleet=None): """Calculates state of planet in `turns' turns.""" planet = copy(self) arriving_fleets = self.universe.find_fleets(destination=self) if fleet is not None: arriving_fleets |= fleet for i in range(1, turns + 1): # account planet growth if planet.owner != player.NOBODY: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [x for x in arriving_fleets if x.turns_remaining == i] # assuming 2-player scenario! ships = [] for id in [1, 2]: count = sum([ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ]) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count # if count > 0: ships.append({'player': PLAYER_MAP.get(id), 'ships': count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({ 'player': player.NOBODY, 'ships': planet.ship_count }) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s: s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.ship_count = 0 else: planet.owner = winner['player'] planet.ship_count = winner['ships'] - second['ships'] return planet
def in_future(self, turns=1): #BAD CHANGES MADE """Calculates state of planet in `turns' turns.""" arriving_fleets = self.universe.find_fleets(destination=self) first_uncached = len(self._future_cache) if first_uncached == 0: self._future_cache.append(copy(self)) first_uncached = 1 if turns < first_uncached: return self._future_cache[turns] else: planet = copy(self._future_cache[-1]) for i in range(first_uncached, turns+1): # account planet growth if planet.owner != player.NOBODY: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [ x for x in arriving_fleets if x.turns_remaining == i ] # assuming 2-player scenario! ships = [] for id in [1,2]: count = sum( [ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ] ) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count # if count > 0: ships.append({'player':PLAYER_MAP.get(id), 'ships':count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({'player':player.NOBODY,'ships':planet.ship_count}) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s : s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.ship_count=0 else: planet.owner=winner['player'] planet.ship_count=winner['ships'] - second['ships'] self._future_cache.append(copy(planet)) return planet
def __init__(self, universe, id, x, y, owner, ship_count, growth_rate): self.universe = universe self.id = int(id) self.position = Point(float(x), float(y)) self.owner = PLAYER_MAP.get(int(owner)) self.ship_count = int(ship_count) self.growth_rate = int(growth_rate)
def in_future(self, turns=None): """Calculates state of planet in `turns' turns. If `turns' is None, all fleets on the way to the planet are taken into account.""" planet = copy(self) arriving_fleets = self.universe.find_fleets(destination=self) if turns is None: turns = reduce(lambda a, b: max(a, b.turns_remaining), arriving_fleets, 0) for i in range(1, turns+1): # account planet growth if planet.owner != player.NOBODY: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [ x for x in arriving_fleets if x.turns_remaining == i ] # assuming 2-player scenario! ships = [] for id in [1,2]: count = sum( [ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ] ) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count if count > 0: ships.append({'player':PLAYER_MAP.get(id), 'ships':count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({'player':player.NOBODY,'ships':planet.ship_count}) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s : s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.ship_count=0 else: planet.owner=winner['player'] planet.ship_count=winner['ships'] - second['ships'] return planet
def __init__(self, universe, id, owner, ship_count, source, destination, trip_length, turns_remaining): self.universe = universe self.id = int(id) self.owner = PLAYER_MAP.get(int(owner)) self.ship_count = int(ship_count) self.source = self.universe._planets.get(int(source)) self.destination = self.universe._planets.get(int(destination)) self.trip_length = int(trip_length) self.turns_remaining = int(turns_remaining)
def __init__(self, universe, id, owner, source, destination, ship_count, priority = LOW_PRIORITY, turns_to_wait=0): self.universe = universe self.id = id self.owner = PLAYER_MAP.get(int(owner)) self.source = source self.destination = destination self.ship_count = int(ship_count) self.turns_to_wait = int(turns_to_wait) self.priority = priority self.trip_length = source.distance(destination) self.turns_remaining = int(self.trip_length) + int(self.turns_to_wait)
def __init__(self, universe, id, x, y, owner, ship_count, growth_rate): self.universe = universe self.id = int(id) self.position = Point(float(x), float(y)) self.owner = PLAYER_MAP.get(int(owner)) self.ship_count = int(ship_count) self.growth_rate = int(growth_rate) self.mCost = 0 # total move cost to reach this node self.parent = None # parent node self.score = int(growth_rate) # calculated score for this node
def update(self, owner, ship_count): self.owner = PLAYER_MAP.get(int(owner)) self.ship_count = int(ship_count)
def in_future(self, turns): """Calculates state of planet in `turns' turns.""" planet = copy(self) planet.wait_extra_turn = 0 arriving_fleets = self.universe.find_fleets(destination=self) arriving_planned_attacks = self.universe.find_attacks(source=self.universe.planets - self, destination=self) departing_planned_attacks = self.universe.find_attacks(source=self, destination=self.universe.planets - self) if turns is None: turns = reduce(lambda a, b: max(a, b.turns_remaining), arriving_fleets, 0) turns = reduce(lambda a, b: max(a, b.turns_remaining), arriving_planned_attacks, turns) turns = reduce(lambda a, b: max(a, b.turns_to_wait), departing_planned_attacks, turns) #log.info(" turns:%s" % (turns)) #planned fleets with same dest and source are not actually counted or sent #they are just reserving ships to prevent from them leaving the attacked planet reserved_planned_attacks = self.universe.find_attacks(source=self) reserved_ships_to_send = sum([x.ship_count for x in reserved_planned_attacks if (x.turns_to_wait > turns)]) #Account from [0 until turns], planned departures for the current turn will leave at 0 for i in range(0, turns+1): # account planet growth, not including this turns if planet.owner != player.NOBODY and i > 0: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [ x for x in arriving_fleets if x.turns_remaining == i ] future_fleets = [ x for x in arriving_planned_attacks if x.turns_remaining == i] departing_future_fleets = [ x for x in departing_planned_attacks if x.turns_to_wait == i] # assuming 2-player scenario! ships = [] for id in [1,2]: count = sum( [ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ] ) count += sum([x.ship_count for x in future_fleets if x.owner == PLAYER_MAP.get(int(id)) ]) count -= sum([x.ship_count for x in departing_future_fleets if x.owner == PLAYER_MAP.get(int(id)) ]) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count #count will equal 0 if sending all ships if count > 0 or (id == 1 and departing_future_fleets): ships.append({'player':PLAYER_MAP.get(id), 'ships':count}) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({'player':player.NOBODY,'ships':planet.ship_count}) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s : s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.owner=planet.owner planet.ship_count=0 else: planet.owner=winner['player'] planet.ship_count=winner['ships'] - second['ships'] elif len(ships) == 1: planet.ship_count=ships[0]['ships'] if planet.id == -4: log.info("fleets:%s" % (fleets)) log.info("future_fleets:%s" % (future_fleets)) log.info("departing_future_fleets:%s" % (departing_future_fleets)) log.info("reserved_planned_attacks:%s " % (reserved_planned_attacks)) log.info("ships:%s reserved_ships_to_send%s" % (ships, reserved_ships_to_send)) log.info("i%s planet:%s planet.owner%s \n" % (i, planet, planet.owner)) if planet.owner == player.ME: planet.ship_count -= reserved_ships_to_send return planet
def in_future(self, turns): """Calculates state of planet in `turns' turns.""" planet = copy(self) planet.wait_extra_turn = 0 arriving_fleets = self.universe.find_fleets(destination=self) arriving_planned_attacks = self.universe.find_attacks( source=self.universe.planets - self, destination=self) departing_planned_attacks = self.universe.find_attacks( source=self, destination=self.universe.planets - self) if turns is None: turns = reduce(lambda a, b: max(a, b.turns_remaining), arriving_fleets, 0) turns = reduce(lambda a, b: max(a, b.turns_remaining), arriving_planned_attacks, turns) turns = reduce(lambda a, b: max(a, b.turns_to_wait), departing_planned_attacks, turns) #log.info(" turns:%s" % (turns)) #planned fleets with same dest and source are not actually counted or sent #they are just reserving ships to prevent from them leaving the attacked planet reserved_planned_attacks = self.universe.find_attacks(source=self) reserved_ships_to_send = sum([ x.ship_count for x in reserved_planned_attacks if (x.turns_to_wait > turns) ]) #Account from [0 until turns], planned departures for the current turn will leave at 0 for i in range(0, turns + 1): # account planet growth, not including this turns if planet.owner != player.NOBODY and i > 0: planet.ship_count = planet.ship_count + self.growth_rate # get fleets which will arrive in that turn fleets = [x for x in arriving_fleets if x.turns_remaining == i] future_fleets = [ x for x in arriving_planned_attacks if x.turns_remaining == i ] departing_future_fleets = [ x for x in departing_planned_attacks if x.turns_to_wait == i ] # assuming 2-player scenario! ships = [] for id in [1, 2]: count = sum([ x.ship_count for x in fleets if x.owner == PLAYER_MAP.get(int(id)) ]) count += sum([ x.ship_count for x in future_fleets if x.owner == PLAYER_MAP.get(int(id)) ]) count -= sum([ x.ship_count for x in departing_future_fleets if x.owner == PLAYER_MAP.get(int(id)) ]) if PLAYER_MAP[id] == planet.owner: count += planet.ship_count #count will equal 0 if sending all ships if count > 0 or (id == 1 and departing_future_fleets): ships.append({ 'player': PLAYER_MAP.get(id), 'ships': count }) # neutral planet has own fleet if planet.owner == player.NOBODY: ships.append({ 'player': player.NOBODY, 'ships': planet.ship_count }) # calculate outcome if len(ships) > 1: s = sorted(ships, key=lambda s: s['ships'], reverse=True) winner = s[0] second = s[1] if winner['ships'] == second['ships']: planet.owner = planet.owner planet.ship_count = 0 else: planet.owner = winner['player'] planet.ship_count = winner['ships'] - second['ships'] elif len(ships) == 1: planet.ship_count = ships[0]['ships'] if planet.id == -4: log.info("fleets:%s" % (fleets)) log.info("future_fleets:%s" % (future_fleets)) log.info("departing_future_fleets:%s" % (departing_future_fleets)) log.info("reserved_planned_attacks:%s " % (reserved_planned_attacks)) log.info("ships:%s reserved_ships_to_send%s" % (ships, reserved_ships_to_send)) log.info("i%s planet:%s planet.owner%s \n" % (i, planet, planet.owner)) if planet.owner == player.ME: planet.ship_count -= reserved_ships_to_send return planet