예제 #1
0
def enemy_owns_room(room_name):
    # type: (str) -> bool
    data = stored_data.get_data(room_name)
    if not data or not data.owner:
        return False
    return (data.owner.state is StoredEnemyRoomState.FULLY_FUNCTIONAL
            or data.owner.state is StoredEnemyRoomState.OWNED_DEAD)
예제 #2
0
def enemy_using_room(room_name):
    # type: (str) -> bool
    data = stored_data.get_data(room_name)
    if not data or not data.owner:
        return False
    return (data.owner.state is StoredEnemyRoomState.FULLY_FUNCTIONAL
            or data.owner.state is StoredEnemyRoomState.RESERVED
            or data.owner.state is StoredEnemyRoomState.JUST_MINING)
예제 #3
0
def _create_basic_room_cost_matrix(room_name):
    # type: (str) -> PathFinder.CostMatrix
    matrix = __new__(PathFinder.CostMatrix())
    room = Game.rooms[room_name]
    if room:
        any_lairs = False
        for structure in cast(List[Structure], room.find(FIND_STRUCTURES)):
            if structure.structureType == STRUCTURE_RAMPART and (
                    cast(StructureRampart, structure).my
                    or cast(StructureRampart, structure).isPublic):
                continue
            if structure.structureType == STRUCTURE_ROAD:
                if matrix.get(structure.pos.x, structure.pos.y) <= 2:
                    matrix.set(structure.pos.x, structure.pos.y, 1)
                continue
            if structure.structureType == STRUCTURE_CONTAINER:
                continue
            if structure.structureType == STRUCTURE_KEEPER_LAIR:
                any_lairs = True
            matrix.set(structure.pos.x, structure.pos.y, 255)
        for site in cast(List[ConstructionSite],
                         room.find(FIND_MY_CONSTRUCTION_SITES)):
            # type: ConstructionSite
            if site.structureType == STRUCTURE_RAMPART or site.structureType == STRUCTURE_ROAD \
                    or site.structureType == STRUCTURE_CONTAINER:
                continue
            matrix.set(site.pos.x, site.pos.y, 255)
        # Note: this depends on room being a regular Room, not a RoomMind, since RoomMind.find(FIND_HOSTILE_CREEPS)
        # excludes allies!
        if not room.controller or not room.controller.my or not room.controller.safeMode:
            for creep in room.find(FIND_HOSTILE_CREEPS):
                matrix.set(creep.pos.x, creep.pos.y, 255)
        if any_lairs:
            for source in room.find(FIND_SOURCES):
                for x in range(source.pos.x - 4, source.pos.x + 5):
                    for y in range(source.pos.y - 4, source.pos.y + 5):
                        matrix.set(x, y, 250)
            for mineral in room.find(FIND_MINERALS):
                for x in range(mineral.pos.x - 4, mineral.pos.x + 5):
                    for y in range(mineral.pos.y - 4, mineral.pos.y + 5):
                        matrix.set(x, y, 250)
    else:
        data = stored_data.get_data(room_name)
        if not data:
            return matrix
        for obstacle in data.obstacles:
            if obstacle.type == StoredObstacleType.ROAD:
                if matrix.get(obstacle.x, obstacle.y) == 0:
                    matrix.set(obstacle.x, obstacle.y, 1)
            else:
                if obstacle.type == StoredObstacleType.SOURCE_KEEPER_SOURCE \
                        or obstacle.type == StoredObstacleType.SOURCE_KEEPER_MINERAL:
                    for x in range(obstacle.x - 4, obstacle.x + 5):
                        for y in range(obstacle.y - 4, obstacle.y + 5):
                            matrix.set(x, y, 250)
                matrix.set(obstacle.x, obstacle.y, 255)
    return matrix
예제 #4
0
 def visitor(room_name: str) -> bool:
     data = stored_data.get_data(room_name)
     if not data:
         return False
     if data.owner and data.owner.state == StoredEnemyRoomState.FULLY_FUNCTIONAL \
             and not Memory.meta.friends.includes(data.owner.name) \
             and data.owner.name != stored_data.get_my_username():
         result.append(room_name)
     return True
예제 #5
0
 def visitor(room_name: str) -> bool:
     data = stored_data.get_data(room_name)
     room = hive.get_room(room_name)
     if not data:
         return False
     if data.owner and data.owner.state != StoredEnemyRoomState.OWNED_DEAD \
             and data.owner.name != stored_data.get_my_username():
         return False
     if movement.is_room_exact_center_of_sector(room_name) or movement.is_room_inner_circle_of_sector(room_name) \
             or movement.is_room_highway(room_name):
         return False
     if (not data.owner or data.owner.state == StoredEnemyRoomState.OWNED_DEAD) \
             and (not room or not room.my):
         any_structures = False
         for obstacle in data.obstacles:
             if obstacle.type == StoredObstacleType.OTHER_IMPASSABLE:
                 any_structures = True
         if any_structures:
             result_here.append(room_name)
     return True
예제 #6
0
def kiting_cost_matrix(room_name, target):
    # type: (str, Flag) -> Union[PathFinder.CostMatrix, bool]
    cache = volatile_cache.mem("kiting_cost_matrix")
    if cache.has(room_name):
        return cache.get(room_name)
    if hostile_utils.enemy_using_room(room_name) and room_name != target.pos.roomName:
        return False
    # TODO: some of this is duplicated in honey.HoneyTrails

    cost_matrix = __new__(PathFinder.CostMatrix())

    def set_in_range_xy(initial_x, initial_y, distance, value, increase_by_center):
        for x in range(initial_x - distance, initial_x + distance + 1):
            for y in range(initial_y - distance, initial_y + distance + 1):
                if increase_by_center:
                    value_here = value + increase_by_center \
                                         * (distance - movement.chebyshev_distance_xy(initial_x, initial_y, x, y))
                else:
                    value_here = value
                existing_cost = cost_matrix.get(x, y)
                if existing_cost == 0:
                    terrain_here = Game.map.getTerrainAt(x, y, room_name)
                    if terrain_here[0] == 'p':
                        existing_cost = 1
                    elif terrain_here[0] == 's':
                        existing_cost = 25
                    elif terrain_here[0] == 'w':
                        continue
                cost_matrix.set(x, y, existing_cost + value_here)

    def set_in_range(pos, distance, value, increase_by_center):
        pos = pos.pos or pos
        set_in_range_xy(pos.x, pos.y, distance, value, increase_by_center)

    room = context.hive().get_room(room_name)

    if room:
        any_lairs = False
        for struct in cast(List[OwnedStructure], room.find(FIND_STRUCTURES)):
            if struct.structureType == STRUCTURE_ROAD:
                cost_matrix.set(struct.pos.x, struct.pos.y, 1)
            elif struct.structureType != STRUCTURE_CONTAINER and (struct.structureType != STRUCTURE_RAMPART
                                                                  or not struct.my):
                if struct.structureType == STRUCTURE_KEEPER_LAIR:
                    any_lairs = True
                cost_matrix.set(struct.pos.x, struct.pos.y, 255)
        for creep in room.find(FIND_MY_CREEPS):
            cost_matrix.set(creep.pos.x, creep.pos.y, 255)
        if any_lairs:
            for source in room.find(FIND_SOURCES):
                set_in_range(source.pos, 4, 255, 0)
            for mineral in room.find(FIND_MINERALS):
                set_in_range(mineral.pos, 4, 255, 0)
    else:
        data = stored_data.get_data(room_name)
        if data:
            for obstacle in data.obstacles:
                if obstacle.type == StoredObstacleType.ROAD:
                    cost_matrix.set(obstacle.x, obstacle.y, 1)
                elif obstacle.type == StoredObstacleType.SOURCE_KEEPER_LAIR \
                        or obstacle.type == StoredObstacleType.SOURCE_KEEPER_SOURCE \
                        or obstacle.type == StoredObstacleType.SOURCE_KEEPER_MINERAL:
                    set_in_range(obstacle, 4, 255, 0)
                else:
                    cost_matrix.set(obstacle.x, obstacle.y, 255)

    for info in defense.stored_hostiles_in(room_name):
        x, y = positions.deserialize_xy(info.pos)
        set_in_range_xy(x, y, 3, 5, 10)
        cost_matrix.set(x, y, 255)

    for x in [0, 49]:
        for y in range(0, 49):
            existing = cost_matrix.get(x, y)
            if existing == 0:
                terrain = Game.map.getTerrainAt(x, y, room_name)
                if terrain[0] == 'p':
                    existing = 1
                elif terrain[0] == 's':
                    existing = 25
                else:
                    continue  # wall
            cost_matrix.set(x, y, existing + 5)
    for y in [0, 49]:
        for x in range(0, 49):
            existing = cost_matrix.get(x, y)
            if existing == 0:
                terrain = Game.map.getTerrainAt(x, y, room_name)
                if terrain[0] == 'p':
                    existing = 1
                elif terrain[0] == 's':
                    existing = 25
                else:
                    continue  # wall
            cost_matrix.set(x, y, existing + 5)

    cache.set(room_name, cost_matrix)

    return cost_matrix
예제 #7
0
def dismantle_pathfinder_callback(room_name):
    # type: (str) -> Union[PathFinder.CostMatrix, bool]
    room = Game.rooms[room_name]
    if room:
        opts = get_opts(room_name)
        plain_cost = 1
        matrix = honey.create_custom_cost_matrix(room_name, plain_cost,
                                                 plain_cost * 5, 1, False)
        any_lairs = False
        for structure in cast(List[Structure], room.find(FIND_STRUCTURES)):
            structure_type = structure.structureType
            if structure_type == STRUCTURE_RAMPART and (
                    cast(StructureRampart, structure).my
                    or cast(StructureRampart, structure).isPublic):
                continue
            elif structure_type == STRUCTURE_ROAD:
                continue
            elif not can_target_struct(structure, opts):
                matrix.set(structure.pos.x, structure.pos.y, 255)
            elif structure_type == STRUCTURE_ROAD:
                if matrix.get(structure.pos.x, structure.pos.y) == 0:
                    matrix.set(structure.pos.x, structure.pos.y, plain_cost)
                continue
            elif structure_type == STRUCTURE_CONTAINER:
                continue
            elif structure_type == STRUCTURE_KEEPER_LAIR:
                any_lairs = True
                matrix.set(structure.pos.x, structure.pos.y, 255)
            elif structure_type == STRUCTURE_SPAWN or structure_type == STRUCTURE_EXTENSION:
                for x in range(structure.pos.x - 1, structure.pos.x + 2):
                    for y in range(structure.pos.y - 1, structure.pos.y + 2):
                        existing = matrix.get_existing(x, y)
                        matrix.set(x, y, existing - 1)
            elif structure_type == STRUCTURE_TOWER and cast(
                    StructureTower, structure).energy:
                initial_x = structure.pos.x
                initial_y = structure.pos.y
                for x in range(initial_x - 10, initial_x + 10):
                    for y in range(initial_y - 10, initial_y + 10):
                        distance = movement.chebyshev_distance_xy(
                            initial_x, initial_y, x, y)
                        if distance <= 5:
                            matrix.increase_at(x, y, None, 20 * plain_cost)
                        else:
                            matrix.increase_at(x, y, None,
                                               (25 - distance) * plain_cost)
            elif structure.hits:
                matrix.increase_at(
                    structure.pos.x, structure.pos.y, None,
                    cost_of_wall_hits(structure.hits) * plain_cost)
            else:
                matrix.set(structure.pos.x, structure.pos.y, 255)
        for site in cast(List[ConstructionSite],
                         room.find(FIND_MY_CONSTRUCTION_SITES)):
            if site.structureType == STRUCTURE_RAMPART or site.structureType == STRUCTURE_ROAD \
                    or site.structureType == STRUCTURE_CONTAINER:
                continue
            matrix.set(site.pos.x, site.pos.y, 255)
        # Note: this depends on room being a regular Room, not a RoomMind, since RoomMind.find(FIND_HOSTILE_CREEPS)
        # excludes allies!
        if not room.controller or not room.controller.my or not room.controller.safeMode:
            for creep in room.find(FIND_HOSTILE_CREEPS):
                matrix.set(creep.pos.x, creep.pos.y, 255)
        if any_lairs:
            for source in room.find(FIND_SOURCES):
                for x in range(source.pos.x - 4, source.pos.x + 5):
                    for y in range(source.pos.y - 4, source.pos.y + 5):
                        matrix.set(x, y, 200)
            for mineral in room.find(FIND_MINERALS):
                for x in range(mineral.pos.x - 4, mineral.pos.x + 5):
                    for y in range(mineral.pos.y - 4, mineral.pos.y + 5):
                        matrix.set(x, y, 200)
        print('Dismantler cost matrix for {}:\nStart.\n{}\nEnd.'.format(
            room_name, matrix.visual()))
    else:
        matrix = __new__(PathFinder.CostMatrix())
        data = stored_data.get_data(room_name)
        if not data:
            return matrix
        for obstacle in data.obstacles:
            if obstacle.type == StoredObstacleType.ROAD:
                if matrix.get(obstacle.x, obstacle.y) == 0:
                    matrix.set(obstacle.x, obstacle.y, 1)
            else:
                if obstacle.type == StoredObstacleType.SOURCE_KEEPER_SOURCE \
                        or obstacle.type == StoredObstacleType.SOURCE_KEEPER_MINERAL:
                    for x in range(obstacle.x - 4, obstacle.x + 5):
                        for y in range(obstacle.y - 4, obstacle.y + 5):
                            matrix.set(x, y, 200)
                matrix.set(obstacle.x, obstacle.y, 255)
    return matrix.cost_matrix