Ejemplo n.º 1
0
def mage_sense(gc, unit, battle_locs, queued_paths):
    dir = None
    attack = None
    blink = None
    move_then_attack = False
    visible_enemies = False
    location = unit.location.map_location()
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                            sense_util.enemy_team(gc))
    if len(enemies) > 0:
        if unit.id in queued_paths:
            del queued_paths[unit.id]
        visible_enemies = True
        sorted_enemies = sorted(enemies,
                                key=lambda x: x.location.map_location().
                                distance_squared_to(location))
        closest_enemy = ranger.closest_among_ungarrisoned(sorted_enemies)
        print('closest enemy:', closest_enemy)
        attack = ranger.get_attack(gc, unit, location)
        print(attack)
        if attack is not None:
            print('found it here')
            if closest_enemy is not None:
                if (ranger.exists_bad_enemy(enemies) and
                    (closest_enemy.location.map_location().distance_squared_to(
                        location))**(0.5) + 2 < unit.attack_range()**
                    (0.5)) or not gc.can_attack(unit.id, attack.id):
                    dir = sense_util.best_available_direction(
                        gc, unit, enemies)

        else:
            if gc.is_move_ready(unit.id):
                if closest_enemy is not None:
                    dir = ranger.optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())
                    next_turn_loc = location.add(dir)
                    attack = ranger.get_attack(gc, unit, next_turn_loc)
                    if attack is not None:
                        move_then_attack = True
                else:
                    dir = ranger.get_explore_dir(gc, unit)

    else:
        if unit.id in queued_paths:
            if location != queued_paths[unit.id]:
                dir = ranger.optimal_direction_towards(gc, unit, location,
                                                       queued_paths[unit.id])
                return dir, attack, blink, move_then_attack, visible_enemies
            else:
                del queued_paths[unit.id]
        if len(battle_locs) > 0:
            dir, target = ranger.go_to_battle(gc, unit, battle_locs)
            queued_paths[unit.id] = target
        else:
            dir = ranger.get_explore_dir(gc, unit)

    return dir, attack, blink, move_then_attack, visible_enemies
Ejemplo n.º 2
0
def knight_sense(gc, unit, battle_locs, knight_roles, location, direction_to_coord, bfs_array, targeting_units,
                 rocket_locs):
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range, variables.enemy_team)
    if unit.id in knight_roles["go_to_mars"]:
        return go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, bfs_array,
                                targeting_units, rocket_locs)
    signals = {}
    dir = None
    attack = None
    javelin = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    # if variables.print_count < 10:
    #    print("Sensing nearby units:", time.time() - start_time)
    if len(enemies) > 0:
        visible_enemies = True
        closest_enemy = None
        closest_dist = -float('inf')
        for enemy in enemies:
            enemy_loc = enemy.location
            if enemy_loc.is_on_map():
                enemy_map_loc = enemy_loc.map_location()
                coeff = Ranger.coefficient_computation(gc, unit, enemy, enemy_map_loc, location)
                # dist = sense_util.distance_squared_between_maplocs(loc.map_location(), location)
                if coeff > closest_dist:
                    closest_dist = coeff
                    closest_enemy = enemy
        # if variables.print_count < 10:
        #    print("Getting closest enemy:", time.time() - start_time)
        # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location))
        # closest_enemy = closest_among_ungarrisoned(sorted_enemies)
        attack = get_attack(gc, unit, location, targeting_units, knight_unit_priority)

        if attack is not None:
            if closest_enemy is not None:
                dir = Ranger.go_to_loc(unit, location,
                                closest_enemy.location.map_location())  # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location())
        else:
            if gc.is_move_ready(unit.id):
                if closest_enemy is not None:
                    dir = Ranger.go_to_loc(unit, location,
                                    closest_enemy.location.map_location())  # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location())
                    if dir is None or dir == variables.directions[8]:
                        dir = Ranger.optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location())
                    next_turn_loc = location.add(dir)
                    attack = get_attack(gc, unit, next_turn_loc, targeting_units, knight_unit_priority)
                    if attack is not None:
                        move_then_attack = True
                else:
                    if variables.curr_planet == bc.Planet.Earth:
                        # print('IS RUNNING TOWARDS INIT LOC')
                        dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord)
                    else:
                        # print('EXPLORING')
                        dir = Ranger.get_explore_dir(gc, unit, location)
                    # if variables.print_count < 10:
                    #    print("Getting direction:", time.time() - start_time)
    else:
        # if there are no enemies in sight, check if there is an ongoing battle.  If so, go there.
        if len(rocket_locs) > 0 and gc.round() > 660 and variables.curr_planet == bc.Planet.Earth:
            dir = Ranger.move_to_rocket(gc, unit, location, direction_to_coord, bfs_array)
            if dir is not None:
                return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals
        if len(battle_locs) > 0:
            # print('IS GOING TO BATTLE')
            dir = Ranger.go_to_battle_new(gc, unit, battle_locs, location, direction_to_coord)
            # queued_paths[unit.id] = target
        else:
            # dir = move_away(gc, unit, battle_locs)
            if variables.curr_planet == bc.Planet.Earth:
                # print('IS RUNNING TOWARDS INIT LOC')
                dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord)
            else:
                # print('EXPLORING')
                dir = Ranger.get_explore_dir(gc, unit, location)
        # if variables.print_count < 10:
        #    print("regular movement:", time.time() - start_time)

    return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals
Ejemplo n.º 3
0
def mage_sense(gc, unit, battle_locs, mage_roles, location, direction_to_coord,
               bfs_array, targeting_units, rocket_locs):
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                            variables.enemy_team)
    if unit.id in mage_roles["go_to_mars"]:
        return go_to_mars_sense(gc, unit, battle_locs, location, enemies,
                                direction_to_coord, bfs_array, targeting_units,
                                rocket_locs)
    signals = {}
    dir = None
    attack = None
    blink = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    start_time = time.time()
    # if variables.print_count < 10:
    #    print("Sensing nearby units:", time.time() - start_time)
    if len(enemies) > 0:
        visible_enemies = True
        start_time = time.time()
        closest_enemy = None
        closest_dist = float('inf')
        for enemy in enemies:
            loc = enemy.location
            if loc.is_on_map():
                dist = sense_util.distance_squared_between_maplocs(
                    loc.map_location(), location)
                if dist < closest_dist:
                    closest_dist = dist
                    closest_enemy = enemy
        # if variables.print_count < 10:
        #    print("Getting closest enemy:", time.time() - start_time)
        # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location))
        # closest_enemy = closest_among_ungarrisoned(sorted_enemies)
        start_time = time.time()
        attack = Ranger.get_attack(gc, unit, location, targeting_units)
        # if variables.print_count < 10:
        #    print("Getting attack:", time.time() - start_time)
        if attack is not None:
            if closest_enemy is not None:
                start_time = time.time()
                if Ranger.check_radius_squares_factories(gc, location):
                    dir = Ranger.optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())
                elif (Ranger.exists_bad_enemy(closest_enemy)
                      ) or not gc.can_attack(unit.id, closest_enemy.id):
                    # if variables.print_count < 10:
                    #    print("Checking if condition:", time.time() - start_time)
                    start_time = time.time()
                    dir = sense_util.best_available_direction(
                        gc, unit, [closest_enemy])
                    # if variables.print_count < 10:
                    #    print("Getting best available direction:", time.time() - start_time)

                    # and (closest_enemy.location.map_location().distance_squared_to(location)) ** (
                    # 0.5) + 2 < unit.attack_range() ** (0.5)) or not gc.can_attack(unit.id, attack.id):
        else:
            if gc.is_move_ready(unit.id):

                if closest_enemy is not None:
                    dir = Ranger.optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())

                    next_turn_loc = location.add(dir)
                    attack = Ranger.get_attack(gc, unit, next_turn_loc,
                                               targeting_units)
                    if attack is not None:
                        move_then_attack = True
                else:
                    dir = Ranger.run_towards_init_loc_new(
                        gc, unit, location, direction_to_coord)
                    # if variables.print_count < 10:
                    #    print("Getting direction:", time.time() - start_time)
    else:
        # if there are no enemies in sight, check if there is an ongoing battle.  If so, go there.
        if len(rocket_locs) > 0 and gc.round(
        ) > 660 and variables.curr_planet == bc.Planet.Earth:
            dir = Ranger.move_to_rocket(gc, unit, location, direction_to_coord,
                                        bfs_array)
            if dir is not None:
                return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals
        if len(battle_locs) > 0:
            # print('IS GOING TO BATTLE')
            dir = Ranger.go_to_battle_new(gc, unit, battle_locs, location,
                                          direction_to_coord)
            # queued_paths[unit.id] = target
        else:
            # dir = move_away(gc, unit, battle_locs)
            if variables.curr_planet == bc.Planet.Earth:
                # print('IS RUNNING TOWARDS INIT LOC')
                dir = Ranger.run_towards_init_loc_new(gc, unit, location,
                                                      direction_to_coord)
            else:
                # print('EXPLORING')
                dir = Ranger.get_explore_dir(gc, unit, location)
        # if variables.print_count < 10:
        #    print("regular movement:", time.time() - start_time)

    return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals