def portals_desc() -> str: result = [] portals = portal_info() recorded = new_set() for room_name in _.sortBy(Object.keys(portals)): if recorded.has(room_name): continue recorded.add(room_name) data = portals[room_name] formatted_xy_builder = [] xy_pairs = data[portal_segment_data_key_xy_pairs] for index in range(0, len(xy_pairs), 2): s_x, s_y = positions.deserialize_xy( int(robjs.get_str_codepoint(xy_pairs, index))) d_x, d_y = positions.deserialize_xy( int(robjs.get_str_codepoint(xy_pairs, index + 1))) formatted_xy_builder.append("{},{}->{},{}".format( s_x, s_y, d_x, d_y)) end_time = data[portal_segment_data_key_end_game_time] result.append("portal {} -> {}:" "\n\ttime left: {}" "\n\tpositions: {}".format( room_name, data[portal_segment_data_key_destination_room_name], end_time if end_time else "still active", ' '.join(formatted_xy_builder), )) return "\n".join(result)
def recommended_reroute( origin: RoomPosition, destination: RoomPosition ) -> Optional[Tuple[RoomPosition, RoomPosition]]: path_len = movement.room_chebyshev_distance(origin.roomName, destination.roomName) if path_len < 5: return None reroute = None # type: Optional[Tuple[str, Dict[str, Union[str, int]]]] for origin_portal_room, portal_data in portals_near(origin.roomName): destination_portal_room = portal_data[ portal_segment_data_key_destination_room_name] trying_len = (movement.room_chebyshev_distance(origin.roomName, origin_portal_room) + movement.room_chebyshev_distance(destination_portal_room, destination.roomName)) if trying_len < path_len: path_len = trying_len reroute = (origin_portal_room, portal_data) if reroute is None: return None origin_portal_room, portal_data = reroute destination_portal_room = portal_data[ portal_segment_data_key_destination_room_name] xys_encoded = portal_data[portal_segment_data_key_xy_pairs] origin_x, origin_y = positions.deserialize_xy( int(robjs.get_str_codepoint(xys_encoded, 0))) destination_x, destination_y = positions.deserialize_xy( int(robjs.get_str_codepoint(xys_encoded, 1))) reroute_start = __new__( RoomPosition(origin_x, origin_y, origin_portal_room)) reroute_end = __new__( RoomPosition(destination_x, destination_y, destination_portal_room)) return reroute_start, reroute_end
def room_pos_of_closest_serialized(room, here_pos, list_of_serialized): # type: (RoomMind, RoomPosition, List[int]) -> Optional[RoomPosition] length = len(list_of_serialized) room_name = here_pos.roomName if length == 1: return positions.deserialize_xy_to_pos(list_of_serialized[0], room_name) here_x = here_pos.x here_y = here_pos.y closest = None closest_length = Infinity for xy in list_of_serialized: x, y = positions.deserialize_xy(xy) distance = max(abs(here_x - x), abs(here_y - y)) if here_pos.x == x and here_pos.y == y: closest_length = -Infinity closest = xy elif distance < closest_length and is_block_clear(room, x, y): closest_length = distance closest = xy if closest is not None: return positions.deserialize_xy_to_pos(closest, room_name) else: return None
def rate_attack_pos(x, y): summation = 0 for enemy in nearby: if enemy.pos.x: ex = enemy.pos.x ey = enemy.pos.y else: ex, ey = positions.deserialize_xy(enemy.pos) distance = movement.chebyshev_distance_xy(x, y, ex, ey) summation += ranged_mass_attack_rates[distance] or 0 return summation * 10 + movement.chebyshev_distance_xy(x, y, self.pos.x, self.pos.y)
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
def visualize_room(hive, room_name): # type: (HiveMind, str) -> None """ :type hive: empire.hive.HiveMind :type room_name: str """ js_visuals.clear(room_name) path_colors = ['#6AB4FF', '#A4B227', '#EDFF51', '#CC362C', '#B2615B'] next_color = 0 path_list = mining_paths.list_of_paths_with_metadata(room_name) for path in _.sortBy(path_list): points = [] path = path[path.codePointAt(0):] for i in range(0, len(path)): xy = path.codePointAt(i) x, y = positions.deserialize_xy(xy) points.append([x, y]) color = path_colors[next_color] next_color = (next_color + 1) % len(path_colors) js_visuals.poly(room_name, points, { 'stroke': color, 'opacity': 0.3, 'strokeWidth': 0.2, }) room = hive.get_room(room_name) if room.my: bubble_wrapping = room.building.bubble_wrapping() if bubble_wrapping: for i in range(0, len(bubble_wrapping)): xy = robjs.get_str_codepoint(bubble_wrapping, i) x = xy & 0x3F y = xy >> 6 & 0x3F js_visuals.circle(room_name, x, y, { 'radius': 0.8, 'fill': '#ADD8E6', 'opacity': 0.6, }) if room and room.my and room.mem[rmem_key_currently_under_siege]: js_visuals.text(room_name, 5, 45, "under attack", { 'color': '#AA0114', 'size': 1.0, 'opacity': 0.8, }) hot, cold = room.defense.get_current_defender_spots() for spot in hot: js_visuals.circle(room_name, spot.x, spot.y, { 'radius': 2.0, 'fill': '#AA0114', 'opacity': 0.2, }) js_visuals.circle(room_name, spot.x, spot.y, { 'radius': 4.0, 'fill': '#AA0114', 'opacity': 0.2, }) for spot in cold: js_visuals.circle(room_name, spot.x, spot.y, { 'radius': 2.0, 'fill': '00B7EB', 'opacity': 0.2, }) js_visuals.circle(room_name, spot.x, spot.y, { 'radius': 4.0, 'fill': '#00B7EB', 'opacity': 0.2, })