コード例 #1
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
コード例 #2
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