Ejemplo n.º 1
0
def defend_weakest_ally(state):
    success = False
    my_planets_under_attack = [planet for planet in state.my_planets() if surplus(state, planet) < 0]

    for planet_under_attack in my_planets_under_attack:
        enemy_fleets = sorted([fleet for fleet in state.enemy_fleets() if fleet.destination_planet == planet_under_attack.ID],
                              key=lambda f:f.turns_remaining)

        turns_capped, my_ships, last_arrival = inf, planet_under_attack.num_ships, 0
        for fleet in enemy_fleets:
            my_ships -= (fleet.num_ships - (planet_under_attack.growth_rate * (fleet.turns_remaining - last_arrival)))
            last_arrival = fleet.turns_remaining
            if my_ships < 0 and turns_capped == inf:
                turns_capped = fleet.turns_remaining
        
        #check if I have another planet close enough to assist
        my_other_planets = [planet for planet in state.my_planets() if planet not in my_planets_under_attack]

        for other_planet in my_other_planets:
            if state.distance(other_planet.ID, planet_under_attack.ID) < turns_capped and surplus(state, other_planet) > fabs(my_ships):
                issue_order(state, other_planet.ID, planet_under_attack.ID, fabs(my_ships) + 1)
                success = True
                my_planets_under_attack.remove(planet_under_attack)
                break
            
    return success
Ejemplo n.º 2
0
def production(state):
    my_planets = iter(
        sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))

    target_planets = [
        planet for planet in state.not_my_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    target_planets = iter(
        sorted(target_planets, key=lambda p: p.num_ships, reverse=True))

    success = False

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            if target_planet.owner == 0:
                required_ships = target_planet.num_ships + 1
            else:
                required_ships = target_planet.num_ships + \
                                    state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID,
                            required_ships)
                success = True
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                target_planet = next(target_planets)

    except StopIteration:
        return success
Ejemplo n.º 3
0
def do_turn(universe):
    # (1) If we currently have a fleet in flight, just do nothing.
    if len(universe.my_fleets()) >= 1:
        return
  
    # (2) Find my strongest planet.
    source = -1
    source_score = -999999.0
    source_num_ships = 0
    for p in universe.my_planets():
        score = float(p.num_ships)
    if score > source_score:
        source_score = score
        source = p._id
        source_num_ships = p.num_ships

    # (3) Find the weakest enemy or neutral planet.
    dest = -1
    dest_score = -999999.0
    for p in universe.other_planets():
        score = 1.0 / (1 + p.num_ships)
        if score > dest_score:
            dest_score = score
            dest = p._id

    # (4) Send half the ships from my strongest planet to the weakest
    # planet that I do not own.
    if source >= 0 and dest >= 0:
        num_ships = source_num_ships / 2
        issue_order(source, dest, num_ships)
Ejemplo n.º 4
0
def spread_to_weakest_neutral_planet(state):
    # (1) If we currently have a fleet in flight, just do nothing.

    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)

    # (3) Find the weakest neutral planet.
    weakest_planet = min(state.neutral_planets(), key=lambda p: p.num_ships, default=None)
    dist_check = 9999
    dist = 0
    # first_planet = state.my_planets()[0]
    # for planets in state.neutral_planets():
    #     dist = state.distance(planets.ID, first_planet.ID)
    #    if dist < dist_check:
    #        dist_check = dist
    #        weakest_planet = planets
    if not strongest_planet or not weakest_planet:
        # No legal source or destination
        return False
    else:
        # (4) Send half the ships from my strongest planet to the weakest enemy planet.
        if strongest_planet.num_ships>weakest_planet.num_ships:
            issue_order(state, strongest_planet.ID, weakest_planet.ID,weakest_planet.num_ships)
        else:
            issue_order(state, strongest_planet.ID, weakest_planet.ID,strongest_planet.num_ships-20)
        return
Ejemplo n.º 5
0
def retaliate_with_fury(state):
    # (1) Sort our planets from strongest to weakest.
    fortresses = iter(
        sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))

    # (2) Find the weakest hostile planet.
    weakest_target = min(state.enemy_planets(),
                         key=lambda t: t.num_ships,
                         default=None)
    if not fortresses or not weakest_target:
        return False

    # (3) Send in joint invasion force from multiple bases.
    else:
        try:
            our_fortress = next(fortresses)
            #-- determine required force, considering distance to target and its production rate --
            legion_size = weakest_target.num_ships + \
                            state.distance(our_fortress.ID, weakest_target.ID) * weakest_target.growth_rate + 1
            while True:
                #-- if enough available ships on standby, with still enough for defence, deploy --
                detachment = int(our_fortress.num_ships * 0.75)
                if detachment > legion_size:
                    issue_order(state, our_fortress.ID, weakest_target.ID,
                                detachment)
                    break
                #-- otherwise, send 75% of available ships and check next base  --
                else:
                    issue_order(state, our_fortress.ID, weakest_target.ID,
                                detachment)
                    legion_size -= detachment
                    our_fortress = next(fortresses)

        except StopIteration:
            return
Ejemplo n.º 6
0
def cheese(state):

    #sending_planet = find_my_strongest_planet(state)
    #enemy_planet = find_enemy_weakest_planet(state)

    #sending_planet = max(state.my_planets(), key=lambda t: t.num_ships, default=None)
    #enemy_planet = min(state.enemy_planets(), key=lambda t: t.num_ships, default=None)

    #check = find_available_ships(state, sending_planet)
    for enemy_planet in state.enemy_planets():
        for my_planet in state.my_planets():
            fleets_in_progress = 0
            for my_fleets in state.my_fleets():
                if(my_fleets.destination_planet == enemy_planet.ID):
                    fleets_in_progress = fleets_in_progress + my_fleets.num_ships


            future_forces = enemy_planet.num_ships + state.distance(enemy_planet.ID, my_planet.ID)*enemy_planet.growth_rate
            if(future_forces < find_available_ships(state, my_planet) and fleets_in_progress < future_forces):
                if(find_available_ships(state, my_planet) > enemy_planet.num_ships):
                    issue_order(state, my_planet.ID, enemy_planet.ID, enemy_planet.num_ships)
    #return issue_order(state, sending_planet.ID, enemy_planet.ID, find_available_ships(state, sending_planet))


    return False
Ejemplo n.º 7
0
def spread_to_weakest_neutral_planet(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    neutral_planets = [
        planet for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    neutral_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(neutral_planets)

    send = 0
    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID,
                            required_ships)
                send += 1
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        if send > 0:
            return True
        else:
            return False
Ejemplo n.º 8
0
def attack(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    enemy_planets = [planet for planet in state.enemy_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    enemy_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(enemy_planets)

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + \
                                 state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return
Ejemplo n.º 9
0
def spread(state):
    # (1) If we currently have a fleet in flight, just do nothing.

    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))
    neutral_planets = [
        planet for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    neutral_planets.sort(key=lambda p: p.num_ships)
    target_planets = iter(neutral_planets)
    sent = 0

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID,
                            required_ships)
                sent = 1
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)
    except StopIteration:
        if sent > 0:
            return True
        else:
            return False
    """if len(state.my_fleets()) >= 1:
Ejemplo n.º 10
0
def spread_default(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))
    neutral_planets = [
        planet
        for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())
    ]
    neutral_planets.sort(key=lambda p: p.num_ships)
    target_planets = iter(neutral_planets)
    distance = 200

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        required_ships = target_planet.num_ships + 1
        while True:
            while my_planet is not None and target_planet is not None:
                curr_dist = state.distance(my_planet.ID, target_planet.ID)
                if distance >= curr_dist and my_planet.num_ships > required_ships:
                    distance = curr_dist
                    tmp_my_planet = my_planet
                    tmp_target_planet = target_planet
                    required_ships = tmp_target_planet.num_ships + 1
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            issue_order(state, tmp_my_planet.ID, tmp_target_planet.ID, required_ships)
    except StopIteration:
        return
Ejemplo n.º 11
0
def snipe(state):
    targets = sorted(state.neutral_planets(), key = lambda p: p.growth_rate, reverse = True)
    danger = defaultdict(int)
    min_turns = defaultdict(lambda: inf)
    for fleet in state.enemy_fleets():
        if fleet.destination_planet in [target.ID for target in targets]:
            danger[fleet.destination_planet] += fleet.num_ships
            min_turns[fleet.destination_planet] = min(min_turns[fleet.destination_planet], fleet.turns_remaining)
    for fleet in state.my_fleets():
        if fleet.destination_planet in [target.ID for target in targets]:
            danger[fleet.destination_planet] -= fleet.num_ships
    valid_targets = [target for target in targets if under_attack(state, target)]
    if not valid_targets:
        logging.log (logging.DEBUG, "Snipe: No valid targets")
        return False
    else:
        logging.log(logging.DEBUG, 'yes valid targets')
    for target in valid_targets:
        logging.log(logging.DEBUG, 'target~')
        danger[target.ID] += target.growth_rate + 1
        for planet in smallest_first(state):
            logging.log(logging.DEBUG, 'planet~ : {} {}'.format(state.distance (planet.ID, target.ID), min_turns[target.ID]))
            distance = state.distance (planet.ID, target.ID)
            turns = min_turns[target.ID]
            if distance == turns + 1:
                logging.log(logging.DEBUG, 'sending_fleet: {} {}'.format(state.distance(planet.ID, target.ID), min_turns[target.ID]))
                ships = planet.num_ships
                danger_level = danger[target.ID]
                reenforcements = min (ships, danger_level)
                if reenforcements > 0:
                    issue_order(state, planet.ID, target.ID, reenforcements)
                    danger[target.ID] -= reenforcements
    return True
Ejemplo n.º 12
0
def expandToNeutrals(state):
    enemyTargets = []
    #planets that are being targeted by the enemy
    for fleet in state.enemy_fleets():
        enemyTargets.append(fleet.destination_planet)

    easyPickings = []
    # find neutral planets that are not the target of an enemy fleet to expand to
    for planet in state.neutral_planets():
        if planet.ID not in enemyTargets:
            #This needed to be a tupple or else game would crash
            easyPickings.append(
                (planet, state.distance(state.my_planets()[0].ID, planet.ID) +
                 planet.num_ships))

    #sort to find the closest and weakest neutral planets
    easyPickings = sorted(easyPickings, key=lambda x: x[1])
    neutralTargets = easyPickings[:4]

    #ATTACK!!!
    for target in neutralTargets:
        issue_order(state,
                    state.my_planets()[0].ID, target[0].ID,
                    (target[0].num_ships + 1) * target[0].growth_rate)

    return True
Ejemplo n.º 13
0
def spread_to_weakest_neutral_planet(state):
    # (1) If we currently have a fleet in flight, just do nothing.
    if len(state.my_fleets()) >= 1:
        return False

    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)

    neutral_planets = [planet for planet in state.neutral_planets()
                       if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    # neutral_planets.sort(key=lambda p: p.num_ships)
    neutral_planets.sort(key=lambda p: p.num_ships + state.distance(strongest_planet.ID, p.ID))

    target_planets = iter(neutral_planets)

    try:
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if strongest_planet.num_ships > required_ships:
                issue_order(state, strongest_planet.ID, target_planet.ID, required_ships)
                target_planet = next(target_planets)
            else:
                return False

    except StopIteration:
        return
Ejemplo n.º 14
0
def send_aid_to_invaded_planet(state):

    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    try:
        my_planet = next(my_planets)
        #target_planet = next(target_planets)
        while my_planet:

            if any(fleet.destination_planet == my_planet.ID for fleet in state.enemy_fleets()):
                if not any(fleet.destination_planet == my_planet.ID for fleet in state.my_fleets()):
                    for fleet in state.enemy_fleets():
                        if fleet.destination_planet == my_planet.ID:
                            if (my_planet.num_ships + (fleet.turns_remaining * my_planet.growth_rate)
                                    + (strongest_planet.num_ships/15+2) + 5 >= fleet.num_ships +
                                    (state.distance(strongest_planet.ID, my_planet.ID)-fleet.turns_remaining)
                                    * my_planet.growth_rate):
                                issue_order(state, strongest_planet.ID, my_planet.ID, strongest_planet.num_ships / 15 + 2)
                my_planet = next(my_planets)
                    #target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return
Ejemplo n.º 15
0
def assult(state):
    #find strongest planet
    readyPlanets = max(
        [planet for planet in state.my_planets() if planet.num_ships >= 100],
        key=lambda p: p.num_ships)

    #if we dont have a planet with enough forces we abort
    if not readyPlanets:
        return False

    # find a good weak target planet to attack
    weakPlanets = []
    for planet in state.not_my_planets():
        weakPlanets.append(
            (planet,
             state.distance(readyPlanets.ID, planet.ID) + planet.num_ships))

    #fin the weakest 3 planets to attack
    weakPlanets = sorted(weakPlanets, key=lambda x: x[1])
    weakPlanets = weakPlanets[:3]

    #ATTACK
    for target in weakPlanets:
        issue_order(state, readyPlanets.ID, target[0].ID,
                    (target[0].num_ships + 1) * target[0].growth_rate)
Ejemplo n.º 16
0
def spread(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    neutral_planets = [
        planet for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    neutral_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(neutral_planets)

    success = False

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                success = True
                issue_order(state, my_planet.ID, target_planet.ID,
                            required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return success
Ejemplo n.º 17
0
def barrage(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships, reverse = True))

    enemy_planets = [planet for planet in state.enemy_planets()
                     if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    enemy_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(enemy_planets)

    if len(state.enemy_planets()) > 3:
        return
    try:
        target_planet = next(target_planets)
        while True:
            cluster = []
            i = 0
            my_ships = 0

            while i < 9:
                cluster.append(next(my_planets))
                i += 1
            for planet in cluster:
                issue_order(state, planet.ID, target_planet.ID, planet.num_ships/2)
            target_planet = next(target_planets)
    except StopIteration:
        return
Ejemplo n.º 18
0
def multi_spread(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships, reverse = True))
    neutral_planets = [planet for planet in state.neutral_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    neutral_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(neutral_planets)

    try:
        neutral_planet = next(target_planets)
        while True:
            cluster = []
            i = 0
            my_ships = 0

            while i < 10:
                cluster.append(next(my_planets))
                my_ships += cluster[0].num_ships/8 + 2
                i += 1
            required_neutral = neutral_planet.num_ships
            if my_ships > required_neutral:
                for planet in cluster:
                    issue_order(state, planet.ID, neutral_planet.ID, (planet.num_ships/8 + 2))
                    return
            else:
                return
    except StopIteration:
        return
Ejemplo n.º 19
0
def send_defensive_fleet(state):
    planets_in_danger = get_planets_in_danger(state)
    planets_helping = []

    for key in planets_in_danger:
        for planet in state.my_planets():
            if state.distance(key,planet.ID) <= planets_in_danger[key][2] and planet not in planets_in_danger and state.distance(key,planet.ID) != 0:
                planets_helping.append(planet)

        while True:
            remove_planets_list = []
            for index, planet_helping in enumerate(planets_helping):
                if planet_helping.num_ships <= math.ceil((planets_in_danger[key][1] + 1)/len(planets_helping)):
                    remove_planets_list.append(index)
                    break
            if len(remove_planets_list):
                for id in remove_planets_list:
                    planets_helping.pop(id)
            elif len(planets_helping):
                for planet_helping in planets_helping:
                    issue_order(state, planet_helping.ID, key, math.ceil((planets_in_danger[key][1] + 1)/len(planets_helping)))
                break
            else:
                break
    return False
Ejemplo n.º 20
0
def attack_weakest_enemy_planet(state):
    # (1) If we currently have a fleet in flight, abort plan.
    if len(state.my_fleets()) >= 3:
        return False

    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(),
                           key=lambda t: t.num_ships,
                           default=None)

    # (3) Find the weakest enemy planet.
    weakest_planet = min(state.enemy_planets(),
                         key=lambda t: t.num_ships,
                         default=None)

    if not strongest_planet or not weakest_planet:
        # No legal source or destination
        return False
    else:
        # required ships along with safety net
        required_ships = weakest_planet.num_ships + state.distance(
            strongest_planet.ID,
            strongest_planet.ID) * weakest_planet.growth_rate + 20
        if strongest_planet.num_ships < required_ships:
            weakest_neutral = min(state.neutral_planets(),
                                  key=lambda p: p.num_ships,
                                  default=None)
            if not weakest_neutral:
                return False
            issue_order(state, strongest_planet.ID, weakest_neutral.ID,
                        weakest_neutral.num_ships + 1)
        return issue_order(state, strongest_planet.ID, weakest_planet.ID,
                           required_ships)
Ejemplo n.º 21
0
def take_defenseless_territory(state):
    # (1) Sort our planets from weakest to strongest.
    fortresses = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    # (2) Sort neutral planets from weakest to strongest.
    wishlist = [
        planet for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    wishlist.sort(key=lambda p: p.num_ships)
    wishlist = iter(wishlist)

    # (3) Given enough ships for a target, send an invasion force to capture it.
    try:
        our_fortress = next(fortresses)
        target = next(wishlist)
        while True:
            legion_size = target.num_ships + 1

            if our_fortress.num_ships > legion_size:
                issue_order(state, our_fortress.ID, target.ID, legion_size)
                our_fortress = next(fortresses)
                target = next(wishlist)
            else:
                our_fortress = next(fortresses)

    except StopIteration:
        return
Ejemplo n.º 22
0
def attack(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))
    enemy_planets = [
        planet for planet in state.enemy_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    enemy_planets.sort(key=lambda p: p.num_ships)
    target_planets = iter(enemy_planets)

    success = False

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + \
                                 state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                success = True
                issue_order(state, my_planet.ID, target_planet.ID,
                            required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return success
Ejemplo n.º 23
0
def attack_recent_lost_neutral_planet(state):
    # (1) If we currently have a fleet in flight, just do nothing.
    if len(state.my_fleets()) >= 1:
        return False

    # (2) Get target enemy planet
    enemy_planets = [planet for planet in state.enemy_planets()
                     if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    enemy_planets.sort(key=lambda p: p.num_ships)
    iter_planets = iter(enemy_planets)

    # (3) Get my planet iter
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    # (4) Calculate the least amount of ships we need to send
    try:
        my_planet = next(my_planets)
        target_planet = next(iter_planets)
        while True:
            required_ships = target_planet.num_ships + \
                                 state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships + 20)
                my_planet = next(my_planets)
                target_planet = next(iter_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return
Ejemplo n.º 24
0
def do_turn(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))

    target_planets = [planet for planet in state.not_my_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    target_planets = iter(sorted(target_planets, key=lambda p: p.num_ships, reverse=True))

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            if target_planet.owner == 0:
                required_ships = target_planet.num_ships + 1
            else:
                required_ships = target_planet.num_ships + \
                                 state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                target_planet = next(target_planets)

    except StopIteration:
        return
Ejemplo n.º 25
0
def attack_plan(state):
    my_planets = iter(
        sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))
    target_planets = [planet for planet in state.enemy_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    target_planets = iter(
        sorted(target_planets, key=lambda p: p.num_ships, reverse=True))
    sent_ship = False
    try:
        # my_planet = next(my_planets)
        target_planet = next(target_planets)
        my_planet = closest_ally(target_planet, state)
        while True:
            required_ships = target_planet.num_ships + \
                state.distance(my_planet.ID, target_planet.ID) * \
                target_planet.growth_rate + 1
            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID,
                            target_planet.ID, required_ships)
                sent_ship = True
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                target_planet = next(target_planets)

    except StopIteration:
        return sent_ship
Ejemplo n.º 26
0
def defend_my_planets(state):
    my_planets = [planet for planet in state.my_planets()]
    if not my_planets:
        return

    avg = sum(strength(planet) for planet in my_planets) / len(my_planets)

    weak_planets = [planet for planet in my_planets if strength(planet) < avg]
    strong_planets = [planet for planet in my_planets if strength(planet) > avg]

    if (not weak_planets) or (not strong_planets):
        return

    weak_planets = iter(sorted(weak_planets, key=strength))
    strong_planets = iter(sorted(strong_planets, key=strength, reverse=True))

    try:
        weak_planet = next(weak_planets)
        strong_planet = next(strong_planets)
        while True:
            need = int(avg - strength(weak_planet))
            have = int(strength(strong_planet) - avg)

            if have >= need > 0:
                issue_order(state, strong_planet.ID, weak_planet.ID, need)
                weak_planet = next(weak_planets)
            elif have > 0:
                issue_order(state, strong_planet.ID, weak_planet.ID, have)
                strong_planet = next(strong_planets)
            else:
                strong_planet = next(strong_planets)

    except StopIteration:
        return
Ejemplo n.º 27
0
def spread_to_weakest_and_closest_planet(state):

    close_and_weak = None
    close_and_weak_dst = float("inf")

    #check how many planets you own first to ensure survival
    if len(state.my_planets()) >= 1:
        strongest_planet = max(state.my_planets(),
                               key=lambda p: p.num_ships,
                               default=None)
    else:
        return False

        # now check planets that are not mine
    for planets in state.not_my_planets():
        # get a combined value that will determine if this planet is weak and has a small fleet
        dst = planet_distance(
            strongest_planet,
            planets) + planets.num_ships / (1 + planets.growth_rate)

        if dst < close_and_weak_dst:
            close_and_weak_dst = dst
            close_and_weak = planets

    #now that you know the closest and weakest planet we need to see what to do with it
    if close_and_weak is not None and close_and_weak.num_ships + 10 < strongest_planet.num_ships:
        required_ships = close_and_weak.num_ships + \
                         state.distance(strongest_planet.ID, close_and_weak.ID) * close_and_weak.growth_rate + 2
        issue_order(state, strongest_planet.ID, close_and_weak.ID,
                    required_ships)

    return False
Ejemplo n.º 28
0
def attack_weakest_planet(state):
    # Sort player and enemy planets
    my_planets = sort_player_planets(state)
    enemy_planets = sort_enemy_planets(state)
    neutral_planets = sort_neutral_planets(state)

    # Combine neutral and enemy lists
    neutral_and_enemy = neutral_planets + enemy_planets
    target_planets = iter(neutral_and_enemy)

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + \
                                 state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return False
Ejemplo n.º 29
0
def attack_with_no_mercy(state):
    # (1) Sort our planets from weakest to strongest.
    fortresses = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    # (2) Sort enemy planets from weakest to strongest.
    hostiles = [
        planet for planet in state.enemy_planets()
        if not any(fleet.destination_planet == planet.ID
                   for fleet in state.my_fleets())
    ]
    hostiles.sort(key=lambda p: p.num_ships)
    hostiles = iter(hostiles)

    # (3) Given enough ships for a target, send an invasion force to capture it.
    try:
        our_fortress = next(fortresses)
        marked_for_DDay = next(hostiles)
        while True:
            #-- determine required force, considering distance to target and its production rate --
            required_ships = marked_for_DDay.num_ships + \
                                 state.distance(our_fortress.ID, marked_for_DDay.ID) * marked_for_DDay.growth_rate + 1

            if our_fortress.num_ships > required_ships:
                issue_order(state, our_fortress.ID, marked_for_DDay.ID,
                            required_ships)
                our_fortress = next(fortresses)
                marked_for_DDay = next(hostiles)
            else:
                our_fortress = next(fortresses)

    except StopIteration:
        return
Ejemplo n.º 30
0
def spread_to_closest_planet(state):
    # Sort player and neutral planets
    my_planets = sort_player_planets(state)
    neutral_planets = sort_neutral_planets(state)

    target_planets = iter(neutral_planets)

    try:
        my_planet = next(my_planets)
        if len(neutral_planets) > 0:
            target_planet = find_closest_planet(state, neutral_planets, my_planet)
        else:
            target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                if len(neutral_planets) > 0:
                    target_planet = find_closest_planet(state, neutral_planets, my_planet)
                else:
                    target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return False
Ejemplo n.º 31
0
def closest(state):
    threatenedPlanets = do_not_kill_if_you_are_being_killed(state)
    planets = settled_predictPop(state)
    my_planets = state.my_planets()
    other_planets = state.not_my_planets()

    my_planets = state.my_planets()
    my_planets.sort(key=lambda p : p.num_ships)
    other_planets = [toNametuple(p[1]) for p in planets.items() if p[1]['owner'] != 1] # state.not_my_planets()#state.neutral_planets()
    
    my_center = getCenter(my_planets)
    
    #sorted distance
    #neutral_planets.sort(key=lambda p : distance(getCenter(my_planets), (p.x,p.y) ))
    #sorted lowest pop
    #other_planets.sort(key=lambda p : p.num_ships )
    #combined value
    other_planets.sort(key=lambda p : popAtArrival(p, distance(getCenter(my_planets), (p.x,p.y))) + 50 * distance(getCenter(my_planets), (p.x,p.y) ))

    for mp in my_planets:
        closest = None
        for op in other_planets:
            if  closest == None or state.distance(mp.ID, closest.ID) >  state.distance(mp.ID, op.ID):
                closest = op
        if closest != None:
            pop = popAtArrival(closest, state.distance(mp.ID,closest.ID))
            if pop + 1 < mp.num_ships:
                other_planets.remove(closest)
                issue_order(state, mp.ID, closest.ID, pop + 1)
    return True
Ejemplo n.º 32
0
def defend_friendly_planet(state):

    fleets_attacking = []
    friend_planet = []

    for friend in state.my_fleets():
        friend_planet.append(friend.destination_planet)

    for enemy_fleet in state.enemy_fleets():
        planet = enemy_fleet.destination_planet
        for friendly_planet in state.my_planets():
            if planet == friendly_planet.ID:
                size_of_fleet = enemy_fleet.num_ships
                size_of_planet = friendly_planet.num_ships + (friendly_planet.growth_rate*enemy_fleet.turns_remaining)
                if size_of_fleet > size_of_planet:
                    if friendly_planet.ID not in friend_planet:
                        fleets_attacking.append(enemy_fleet)

    for fleet in fleets_attacking:
        number_of_ships = fleet.num_ships
        ships_to_send = number_of_ships / (len(state.my_planets()))
        for planet in state.my_planets():
            if planet.ID != fleet.destination_planet:
                if planet.num_ships > (ships_to_send + 10):
                    number_of_ships = number_of_ships - ships_to_send
                    issue_order(state, planet.ID, fleet.destination_planet, ships_to_send)
    return
Ejemplo n.º 33
0
def spread_to_weakest_neutral_planet(state):
    # (1) If we currently have a fleet in flight, just do nothing.
    ##if len(state.my_fleets()) > 4:
    ##return False

    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(),
                           key=lambda p: p.num_ships,
                           default=None)

    # (3) Find the weakest neutral planet.
    weakest_planet = sorted(state.neutral_planets(), key=lambda p: p.num_ships)
    weakest_planeta = min(state.neutral_planets(),
                          key=lambda p: p.num_ships,
                          default=None)
    for neut in weakest_planet:
        if strongest_planet.num_ships <= neut.num_ships + 1:
            return False
        is_attacked = False
        for fleet in state.fleets:
            if neut.ID == fleet.destination_planet:
                is_attacked = True
                break
        if not is_attacked:
            return issue_order(state, strongest_planet.ID, neut.ID,
                               neut.num_ships + 1)

    if len(state.my_fleets()
           ) < 1 and strongest_planet.num_ships > weakest_planeta.num_ships:
        return issue_order(state, strongest_planet.ID, weakest_planeta.ID,
                           weakest_planeta.num_ships + 1)
    return False
Ejemplo n.º 34
0
def spread(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    neutral_planets = [
        planet
        for planet in state.neutral_planets()
        if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())
    ]
    neutral_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(neutral_planets)

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return
Ejemplo n.º 35
0
def deploy_fleet(state, data, parameters):
    """
        Attempts to make a deployment defined in the deployments priority queue
        
        Returns True if deployment was successfully made
    """
    ships = data["available_ships"]
    deployments = data["deployments"]

    # all deployments processed, return false
    if deployments.empty():
        return False

    # get next deployment
    score, target, num_ships = deployments.get()

    # not enough ships available, return false
    if num_ships > data["num_available_ships"]:
        return False

    # account for fleets already sent
    for fleet in state.my_fleets():
        if fleet.destination_planet == target.ID:
            num_ships -= fleet.num_ships

    # account for ships on planet
    if target in ships:
        ships_on_planet = min(num_ships, ships[target])
        num_ships -= ships_on_planet
        ships[target] -= ships_on_planet
        data["num_available_ships"] -= ships_on_planet
        if ships[target] <= 0:
            ships.pop(target)

    # loop until ship requirement met
    while num_ships > 0:
        # find closest planet with available ships
        closest_planet = None
        closest_planet_distance = inf
        for planet in ships.keys():
            planet_distance = state.distance(planet.ID, target.ID)
            if planet_distance < closest_planet_distance:
                closest_planet = planet
                closest_planet_distance = planet_distance

        # send ships
        fleet_size = min(ships[closest_planet], num_ships)
        issue_order(state, closest_planet.ID, target.ID, fleet_size)

        # reduce number of available ships
        num_ships -= fleet_size
        data["num_available_ships"] -= fleet_size
        ships[closest_planet] -= fleet_size
        if ships[closest_planet] <= 0:
            ships.pop(closest_planet)

    # deployment
    return True
Ejemplo n.º 36
0
def reinforce_friends(state):
    
    planets = state.my_planets()
    if(len(planets)>=2):
        issue_order(state, planets[0].ID, planets[1].ID, 1)
   
    pass

    
Ejemplo n.º 37
0
def cluster(state):
    #threatenedPlanets = do_not_kill_if_you_are_being_killed(state)
    #planets = settled_predictPop(state)
    

    #my_planets = state.my_planets()
    #other_planets = state.not_my_planets()

    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships))

    neutral_planets = [planet for planet in state.neutral_planets()
                       if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    neutral_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(neutral_planets)

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
            required_ships = target_planet.num_ships + 1

            if my_planet.num_ships > required_ships:
                issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                my_planet = next(my_planets)
                target_planet = next(target_planets)
            else:
                my_planet = next(my_planets)

    except StopIteration:
        return


    '''my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))
    target_planets = [planet for planet in state.not_my_planets()]
    target_planets = iter(sorted(target_planets, key=lambda p: p.num_ships, reverse=True))

    try:
        my_planet = next(my_planets)
        target_planet = next(target_planets)
        while True:
                if target_planet.owner == 0:
                    required_ships = target_planet.num_ships + 1
                else:
                    required_ships = target_planet.num_ships + \
                    state.distance(my_planet.ID, target_planet.ID) * target_planet.growth_rate + 1

                if my_planet.num_ships > required_ships:
                    issue_order(state, my_planet.ID, target_planet.ID, required_ships)
                    my_planet = next(my_planets)
                    target_planet = next(target_planets)
                else:
                    target_planet = next(target_planets)

    except StopIteration:
        return'''
    pass
Ejemplo n.º 38
0
def spread_to_weakest_neutral_planet(state):
    success = False
    my_planets = sorted(state.my_planets(), key = lambda p: p.num_ships, reverse = True)
    neutral_planets = sorted([planet for planet in state.neutral_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())],
                                  key = lambda p: p.num_ships)
    viable_moves = []
    for planet in my_planets:
        l = []
        sur = surplus(state, planet)
        for neutral_planet in neutral_planets:
            enemy_fleets = sorted([fleet for fleet in state.enemy_fleets() if fleet.destination_planet == neutral_planet.ID],
                                  key=lambda f: f.turns_remaining)
            distance = state.distance(planet.ID, neutral_planet.ID)

            turns_capped, neutral_ships = inf, neutral_planet.num_ships
            for fleet in enemy_fleets:
                neutral_ships -= fleet.num_ships
                if neutral_ships < 0:
                    turns_capped = distance - fleet.turns_remaining


            if turns_capped < 1:
                continue                

            required_ships = fabs(neutral_ships) + (turns_capped * neutral_planet.growth_rate) + 1 \
                             if enemy_fleets \
                             else neutral_planet.num_ships + 1

            #arbitrary: don't send more ships to take a neutral planet then my planet's growth rate * 10
            if sur - required_ships < planet.growth_rate * 10:
                continue

            #arbitrary ranking of which planets are best to take
            planet_ratio = c * sqrt(distance) + sqrt(fabs(neutral_ships)) - neutral_planet.growth_rate

            l.append((planet_ratio, planet, neutral_planet, required_ships, sur))

        viable_moves.append(sorted([att[:-1] for att in l]))

    #order all moves into dictionary of lists where key is neutral planet and value is moves to that planet
    d = defaultdict(list)
    for my_planet in viable_moves:
        for move in my_planet:
            d[move[2]].append(move)

    #find the best available move to each enemy planet and execute it
    planets_have_spread = set()
    for neutral_planet, move in d.items():
        ratio, my_p, neutral_p, req = sorted(move)[0]
        if my_p not in planets_have_spread:
            issue_order(state, my_p.ID, neutral_p.ID, req)
            planets_have_spread.add(my_p)
        success = True

    return success
Ejemplo n.º 39
0
def defend(state):
    my_planets = [planet for planet in state.my_planets()]
    if not my_planets:
        return

    def strength(p):
        return p.num_ships \
               + sum(fleet.num_ships for fleet in state.my_fleets() if fleet.destination_planet == p.ID) \
               - sum(fleet.num_ships for fleet in state.enemy_fleets() if fleet.destination_planet == p.ID)

    avg = sum(strength(planet) for planet in my_planets) / len(my_planets)

    reinforce = []
    weak_planets = [planet for planet in my_planets if strength(planet) < avg]
    incoming = [fleet.destination_planet for fleet in state.enemy_fleets()]
    for planet in weak_planets:
        for fleet in incoming:
            if fleet == planet.ID:
                reinforce.append(planet)

    strong_planets = [
        planet for planet in my_planets if strength(planet) > avg
    ]

    if (not reinforce) or (not strong_planets):
        return

    reinforce = sorted(reinforce, key=strength)
    strong_planets = sorted(strong_planets, key=strength, reverse=True)

    avg_dist = 0
    for planet in strong_planets:
        for weak in reinforce:
            avg_dist += state.distance(planet.ID, weak.ID)
    avg_dist /= int(len(strong_planets) * len(reinforce))
    avg_dist = math.ceil(int(avg_dist))

    inbound = {}
    for strong_planet in strong_planets:
        for weak_planet in reinforce:
            incoming = 0
            if weak_planet.ID in inbound.keys():
                incoming += inbound[weak_planet.ID]
            need = abs(int(strength(weak_planet) + incoming))
            have = abs(int(strength(strong_planet)))

            if have >= need > 0 and state.distance(strong_planet.ID,
                                                   weak_planet.ID) <= avg_dist:
                issue_order(state, strong_planet.ID, weak_planet.ID, need)
                if weak_planet.ID not in inbound.keys():
                    inbound[weak_planet.ID] = need
                else:
                    inbound[weak_planet.ID] += need
    return False
Ejemplo n.º 40
0
def reinforce_friendly_planet(state):
    low_planet = []

    for planet in state.my_planets():
        if planet.num_ships > 40:
            low_planet.append(planet)

    for receive in low_planet:
        for donator in state.my_planets():
            if donator.num_ships < 20:
                issue_order(state, receive.ID, donator.ID, 10)
    return
Ejemplo n.º 41
0
def spread_to_best_neutral(state):
    my_planets = iter(
        sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))
    neutral_planets = iter(
        sorted(state.neutral_planets(),
               key=lambda p: (p.growth_rate / (p.num_ships + 1)),
               reverse=True))
    try:
        my_planet = next(my_planets)
        neutral_planet = next(neutral_planets)
        #iterate through neutral_planets and assign each neutral_planet a my_planet
        # attack if possible
        while True:
            if any(fleet.destination_planet == neutral_planet.ID
                   for fleet in state.my_fleets()):
                neutral_planet = next(neutral_planets)
                continue

            #check if any enemy fleet trying to get this next neutral_planet
            for fleet in state.enemy_fleets():
                if fleet.destination_planet == neutral_planet.ID:
                    my_distance = state.distance(my_planet.ID,
                                                 neutral_planet.ID)
                    if my_distance > fleet.turns_remaining:
                        ship_diff_arrived = fleet.num_ships - neutral_planet.num_ships
                        num_ships_to_send = 15 + ship_diff_arrived + (
                            (my_distance - fleet.turns_remaining + 1) *
                            neutral_planet.growth_rate)
                    elif my_distance == fleet.turns_remaining:
                        num_ships_to_send = fleet.num_ships + 10
                    else:
                        num_ships_to_send = fleet.num_ships - neutral_planet.num_ships + 5

                    if my_planet.num_ships - 25 > num_ships_to_send:
                        issue_order(state, my_planet.ID, neutral_planet.ID,
                                    num_ships_to_send)
                        my_planet = next(my_planets)
                    break

            if my_planet.num_ships - 30 < neutral_planet.num_ships:
                my_planet = next(my_planets)
                continue
            else:
                if state.distance(my_planet.ID, neutral_planet.ID) > 15:
                    neutral_planet = next(neutral_planets)
                    continue
                issue_order(state, my_planet.ID, neutral_planet.ID,
                            neutral_planet.num_ships + 10)
                my_planet = next(my_planets)
            neutral_planet = next(neutral_planets)
    except StopIteration:
        return
Ejemplo n.º 42
0
def spread_to_closest_weak_planets(state):
    # find the closest planets that the enemy is not spreading to and take them
    enemy_destination_planets = [f.destination_planet for f in state.enemy_fleets()]
    closest_neutral_planets = [(p, state.distance(state.my_planets()[0].ID, p.ID) + p.num_ships)
            for p in state.neutral_planets() if p.ID not in enemy_destination_planets]

    closest_neutral_planets = sorted(closest_neutral_planets, key=lambda x: x[1])
    closest_neutral_planets = closest_neutral_planets[:4]

    for dest in closest_neutral_planets:
        issue_order(state, state.my_planets()[0].ID, dest[0].ID,
                    (dest[0].num_ships + 1 * dest[0].growth_rate))
    return True
Ejemplo n.º 43
0
def spread_to_small_close_planets (state):
    for planet in smallest_first(state):
        if under_attack (state, planet) or planet.num_ships < 50: continue
        planet_targets = sorted (state.neutral_planets(), key = lambda p: p.num_ships * 2 + state.distance(planet.ID, p.ID) * 3)
        for target in planet_targets:
            closest_enemy = get_closest_enemy(state, target)
            if not closest_enemy: return False
            if state.distance (closest_enemy.ID, target.ID) <= state.distance(planet.ID, target.ID): continue
            ships = planet.num_ships
            danger_level = target.num_ships + 1
            if ships > danger_level and danger_level > 0:
                issue_order(state, planet.ID, target.ID, danger_level)
    return True
Ejemplo n.º 44
0
def pinpoint(state):
    my_planets = iter(sorted(state.my_planets(), key=lambda p: p.num_ships, reverse = True))

    enemy_planets = [planet for planet in state.enemy_planets()
                     if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    enemy_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(enemy_planets)

    if len(state.enemy_fleets()) == 0:
        return
    enemy_fleet = state.enemy_fleets()
    target_planet = None
    target_size = 0
    for fleet in enemy_fleet:
        if state.planet[fleet.source_planet].num_ships > target_size:
            target_planet = state.planet[fleet.source_planet]
            target_size = target_planet.num_ships

    ID = enemy_fleet[0].source_planet
    target_planet = state.planets[ID]

    try:
        while True:
            cluster = []
            i = 0
            my_ships = 0

            while i < 6:
                cluster.append(next(my_planets))
                my_ships += cluster[0].num_ships/5 + 2
                i += 1

            dist = 0
            furthest_planet = None
            for planet in cluster:
                temp = state.distance(target_planet.ID, planet.ID)
                if temp > dist:
                    furthest_planet = planet
                    dist = temp

            required_ships = target_planet.num_ships + \
                state.distance(furthest_planet.ID, target_planet.ID) * target_planet.growth_rate + 2
            if my_ships > required_ships:
                for planet in cluster:
                    issue_order(state, planet.ID, target_planet.ID, (planet.num_ships/4 + 2))
                    return
            else:
               return
    except StopIteration:
        return
Ejemplo n.º 45
0
def defense_action(state):
    my_planets = iter(
        sorted(state.my_planets(), key=lambda p: p.num_ships, reverse=True))
    our_planet = next(my_planets)
    my_list = state.my_planets()
    for enemy in state.enemy_fleets():
        for my in my_list:
            if enemy.destination_planet == my.ID:
                need = enemy.num_ships - my.num_ships
                if need < our_planet.num_ships and our_planet.num_ships > 3:
                    issue_order(state, our_planet.ID, enemy.destination_planet,
                                3)

    return
Ejemplo n.º 46
0
def spread_to_weakest_closest_neutral_planet(state):
  
  if len(state.my_fleets()) >= 1: 
    return False
  strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)
  weakest_planet = min(state.neutral_planets(), key=lambda p: p.num_ships, default=None)

  if not strongest_planet or not weakest_planet:
    return False
  
  main_distance = state.distance(strongest_planet.ID, weakest_planet.ID)
  new_time = main_distance
  difference = main_distance 
  for planet in state.neutral_planets():
    new_distance = state.distance(strongest_planet.ID, planet.ID)
    if (planet.num_ships > strongest_planet.num_ships):
      difference = planet.num_ships - strongest_planet.num_ships + (strongest_planet.num_ships / 2)
      new_time = difference / strongest_planet.growth_rate
      new_time += new_distance  
    if (planet.num_ships < (1.5 * strongest_planet.num_ships)):
      new_time = new_distance
    if (new_time < main_distance):
      main_distance = new_time
      weakest_planet = planet
  return issue_order(state, strongest_planet.ID, weakest_planet.ID, strongest_planet.num_ships / 2)
Ejemplo n.º 47
0
def attack_good_enemy_planet(state):
    def calc_priority(p):
        if p.num_ships > weak_planet_threshold: 
            return 0
        return p.growth_rate / (p.num_ships + 1)
    best_planet = max(get_unrouted_enemy_planets(state), key=lambda p: calc_priority(p), default=None)
    if not best_planet:
        return False
    def calc_distance(p):
        if p.num_ships < best_planet.num_ships:
            return inf
        distance = state.distance(p.ID, best_planet.ID)
        enemy_ships = best_planet.num_ships + (distance * best_planet.growth_rate)
        if p.num_ships - 10 < enemy_ships:
            return inf
        return state.distance(p.ID, best_planet.ID)
    closest_planet = min(state.my_planets(), key=lambda p: calc_distance(p), default=None)
    if not closest_planet:
        # No legal source
        return False
    else:
        if closest_planet.num_ships < 15:
            return False
        # 
        return issue_order(state, closest_planet.ID, best_planet.ID, closest_planet.num_ships - 10)
Ejemplo n.º 48
0
def do_turn(state):
    # (1) If we currently have a fleet in flight, just do nothing.
    if len(state.my_fleets()) >= 1:
        return

    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)

    # (3) Find the weakest enemy or neutral planet.
    weakest_planet = min(state.not_my_planets(), key=lambda p: p.num_ships, default=None)

    if not strongest_planet or not weakest_planet:
        # No legal source or destination
        return

    # (4) Send half the ships from my strongest planet to the weakest planet that I do not own.
    issue_order(state, strongest_planet.ID, weakest_planet.ID, strongest_planet.num_ships/2)
def coordinate_attack_on_enemy(state):
    my_planets = sorted(state.my_planets(), key=lambda p: p.num_ships)

    enemy_planets = [planet for planet in state.enemy_planets()
                      if not any(fleet.destination_planet == planet.ID for fleet in state.my_fleets())]
    enemy_planets.sort(key=lambda p: p.num_ships)

    target_planets = iter(enemy_planets)

    '''my_fleet_size = sum(planet.num_ships for planet in state.my_planets()) \
                    + sum(fleet.num_ships for fleet in state.my_fleets())

    local_planets = []
    enemy_distances = {}

    plausible_target = []'''


    try:
        target_planet = next(target_planets)
        while True:
            closest_enemy_allies = get_neighbors_within(state, target_planet, enemy_planets, inf)
            radius = inf
            if len(closest_enemy_allies) != 0:
                radius = closest_enemy_allies[0][1]
            logging.error("cea: " + str(closest_enemy_allies))
            logging.error("enemy planets: " + str(enemy_planets))
            logging.error("target planet: " + str(target_planet))
            best_offesive_planets = get_neighbors_within(state, target_planet, my_planets, radius)
            if len(best_offesive_planets) > 0:
                required_ships = 0
                if radius == inf:
                    required_ships = target_planet.num_ships + \
                                 state.distance(best_offesive_planets[-1][0].ID, target_planet.ID) * target_planet.growth_rate + 1
                else:
                    required_ships = target_planet.num_ships + \
                                 state.distance(closest_enemy_allies[0][0].ID, target_planet.ID) * target_planet.growth_rate + 1
                offesive_planets_scored = score_planets_contributions(state, best_offesive_planets, required_ships)
                for offesive_planet in offesive_planets_scored:
                    issue_order(state, offesive_planet[0].ID, target_planet.ID, offesive_planet[1])
            target_planet = next(target_planets)

    except StopIteration:
        return False
    '''
Ejemplo n.º 50
0
def spread_to_weakest_neutral_planet(state):
    if len(state.my_fleets()) >= 1:
        return False
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)
    weakest_planet = min(state.neutral_planets(), key=lambda p: p.num_ships, default=None)
    if not strongest_planet or not weakest_planet:
        return False
    else:
        return issue_order(state, strongest_planet.ID, weakest_planet.ID, strongest_planet.num_ships / 2)
def abandon_planet(state):
    fleets_destinations = [fleet.destination_planet for fleet in state.enemy_fleets()]
    planets_being_attacked = [planet for planet in state.my_planets() if planet.ID in fleets_destinations]
    for planet in planets_being_attacked:
        fleets_defending = [fleet.num_ships for fleet in state.my_fleets() if fleet.destination_planet == planet.ID]
        fleets_defending_total = sum(fleets_defending)
        fleets_attacking = [fleet for fleet in state.enemy_fleets() if state.planets[fleet.destination_planet].ID == planet.ID]
        fleets_attacking.sort(key=lambda x: x.turns_remaining)
        total_defense = fleets_attacking[0].turns_remaining*planet.growth_rate + planet.num_ships + fleets_defending_total - fleets_attacking[0].num_ships
        message = "Sera que precisa?"+str(fleets_defending) + " " + str(fleets_attacking) + " " + str(total_defense)
        logging.info(message)
        if total_defense < 0:
            logging.info("ALGUM PLANETA DEVERIA SER ABANDONADO")
            other_planets = [p for p in state.my_planets() if p.ID != planet.ID]
            if other_planets:
                closest = min(other_planets,key=lambda x: state.distance(planet.ID, x.ID))
                issue_order(state, planet.ID, closest.ID, planet.num_ships)
    return False
Ejemplo n.º 52
0
def move_up (state):
    if not state.enemy_planets(): return False
    my_planets = state.my_planets()
    for planet in my_planets:
        if planet.num_ships < 50 or under_attack(state, planet): continue
        my_closest_enemy = get_closest_enemy(state, planet)
        min_dist = inf
        min_planet = planet
        for other_planet in my_planets:
            new_dist = state.distance(other_planet.ID, my_closest_enemy.ID)
            if new_dist < min_dist:
                min_planet = other_planet
                min_dist = new_dist
        if min_planet.ID != planet.ID:
            ships = planet.num_ships
            reenforcements = ships - 50
            if reenforcements > 0:
                issue_order(state, planet.ID, min_planet.ID, reenforcements)
    return True
Ejemplo n.º 53
0
def spread_unexplored_planet(state):
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)
    if not strongest_planet:
        return False
    best_planet = max(get_unrouted_neutral_planets(state), key=lambda p: p.growth_rate / (1 + p.num_ships * state.distance(strongest_planet.ID, p.ID)), default=None)
    if not best_planet:
        return False
    else:
        send_ships = best_planet.num_ships + spread_garrison
        return issue_order(state, strongest_planet.ID, best_planet.ID, send_ships)
Ejemplo n.º 54
0
def attack_most_valuable_enemy_planet(state):
    attackable_planets = get_attackable_planets(state,state.enemy_planets())

    if len(attackable_planets):
        queue = populate_priority_queue(state, attackable_planets)

        if queue:
            domination_value, my_planet, enemy_planet, predicted_num_ships = heappop(queue)
            return issue_order(state, my_planet.ID, enemy_planet.ID, predicted_num_ships + 1)

    return False
Ejemplo n.º 55
0
def spread_to_most_valuable_neutral_planet(state):
    attackable_planets = get_attackable_planets(state,state.neutral_planets())

    if len(attackable_planets):
        queue = populate_priority_queue(state, attackable_planets)

        if queue:
            domination_value, my_planet, neutral_planet, num_ships = heappop(queue)
            return issue_order(state, my_planet.ID, neutral_planet.ID, num_ships + 1)

    return False
Ejemplo n.º 56
0
def attack_largest_enemies (state):
    targets = sorted(state.enemy_planets(), key = lambda p: p.growth_rate, reverse = True)
    fleet_distribution = defaultdict(lambda: defaultdict(int))
    for fleet in state.enemy_fleets():
        if fleet.destination_planet in targets:
            fleet_distribution[fleet.destination_planet]['danger_level'] += fleet.num_ships
    for fleet in state.my_fleets():
        if fleet.destination_planet in targets:
            fleet_distribution[fleet.destination_planet]['danger_level'] -= fleet.num_ships
    for target in targets:
        for planet in smallest_first(state):
            if not under_attack(state, planet):
                ships = planet.num_ships
                if ships < 50: continue
                danger_level = fleet_distribution[target.ID]['danger_level'] + target.num_ships + target.growth_rate * state.distance(target.ID, planet.ID) + 1
                reenforcements = min (ships, danger_level)
                if reenforcements > 0:
                    issue_order(state, planet.ID, target.ID, reenforcements)
                    fleet_distribution[target.ID]['danger_level'] -= reenforcements
    return True
Ejemplo n.º 57
0
def attack_closest_enemy (state):
    target = closest_enemy(state)
    danger = target.num_ships
    for fleet in state.enemy_fleets():
        if fleet.destination_planet == target.ID:
            danger += fleet.num_ships
            
    for fleet in state.my_fleets(): 
        if fleet.destination_planet == target.ID:
            danger -= fleet.num_ships

    for planet in smallest_first(state):
        if not under_attack(state, planet):
            ships = planet.num_ships
            if ships < 50: continue
            danger_level = danger + target.growth_rate * 10
            reenforcements = min (ships, danger_level)
            if reenforcements > 0:
                issue_order(state, planet.ID, target.ID, reenforcements)
                danger -= reenforcements
    return True
def defend_planet(state):
    fleets_attacking = [fleet for fleet in state.enemy_fleets() if state.planets[fleet.destination_planet].owner == 1]
    fleets_destinations = [fleet.destination_planet for fleet in fleets_attacking]
    planets_not_being_attacked = [planet for planet in state.my_planets() if planet.ID not in fleets_destinations]
    for fleet in fleets_attacking:
        planet_being_attacked = state.planets[fleet.destination_planet]
        fleets_defending = [fleet.num_ships for fleet in state.my_fleets() if state.planets[fleet.destination_planet].ID == planet_being_attacked.ID]
        total_fleet_defending = sum(fleets_defending)
        needed_fleet = fleet.num_ships - planet_being_attacked.num_ships - \
                       planet_being_attacked.growth_rate * fleet.turns_remaining - total_fleet_defending +1
        if needed_fleet > 0:
            close_planets = [planet for planet in planets_not_being_attacked if state.distance(planet.ID, planet_being_attacked.ID) < fleet.turns_remaining]
            for close_planet in close_planets:
                max_can_send = min(close_planet.num_ships/3,needed_fleet)
                if max_can_send == 0:
                    continue
                issue_order(state,close_planet.ID,planet_being_attacked.ID,max_can_send)
                needed_fleet -= max_can_send
                if needed_fleet < 0:
                    break
    return True
Ejemplo n.º 59
0
def d_fence(state):
    sort_enemy_fleet = sorted(state.enemy_fleets(), key=lambda p: p.num_ships)
    iter_enemy_fleet = iter(sort_enemy_fleet)

    sort_my_planets = sorted(state.my_planets(), key=lambda p: p.num_ships)
    iter_my_planets = iter(sort_my_planets)

    enemy_fleet = sort_enemy_fleet(state)
    my_planets = sort_player_planets(state)

    avg = sum(strength(planet) for planet in sort_my_planets) / len(sort_my_planets)
    strong_planets = [planet for planet in sort_my_planets if strength(planet) > avg]

    if not my_planets or not enemy_fleet:
        return

    strong_planets = iter(sorted(strong_planets, key=strength, reverse=True))

    try:
        player_planet = next(iter_my_planets)
        strong_planet = next(strong_planets)
        enemy_fleet = next(iter_enemy_fleet)
        while True:
            if enemy_fleet.destination_planet == player_planet:
                need = int(avg - strength(enemy_fleet.destination_planet))
                have = int(strength(strong_planet) - avg)
                if have >= need > 0:
                    issue_order(state, strong_planet.ID, enemy_fleet.destination_planet.ID, need)
                    enemy_fleet = next(iter_enemy_fleet)
                elif have > 0:
                    issue_order(state, strong_planet.ID, enemy_fleet.destination_planet.ID, have)
                    strong_planet = next(strong_planets)
                else:
                    strong_planet = next(strong_planets)

                #issue_order(state, strong_planet.ID, enemy_fleet.destination_planet.ID, need)

    except StopIteration:
        return False
Ejemplo n.º 60
0
def attack_weakest_enemy_planet(state):
    # (2) Find my strongest planet.
    strongest_planet = max(state.my_planets(), key=lambda p: p.num_ships, default=None)

    # (3) Find the weakest enemy planet.
    weakest_planet = min(state.enemy_planets(), key=lambda p: p.num_ships, default=None)

    if not strongest_planet or not weakest_planet:
        # No legal source or destination
        return False
    else:
        # (4) Send half the ships from my strongest planet to the weakest enemy planet.
        return issue_order(state, strongest_planet.ID, weakest_planet.ID, strongest_planet.num_ships / 2)