Esempio n. 1
0
def submem(key1, key2):
    # type: (str, str) -> JSMap[Any, Any]
    """
    :rtype: jstools.js_set_map.JSMap
    """
    v = volatile()
    if not v.has(key1):
        v.set(key1, new_map([(key2, new_map())]))
    elif not v.get(key1).has(key2):
        v.get(key1).set(key2, new_map())
    return v.get(key1).get(key2)
Esempio n. 2
0
def visit_room_graph(center_room: str, max_distance: int,
                     visitor: Callable[[str], bool]):
    """
    Visits each room from the center room outwards by calling the visitor function.

    Will end exploring from a given room if visitor returns false - but will continue to explore other paths until
    all paths end with visitor returning false.
    """
    visited = new_map()

    def visit_with_neighbors(room_name: str, time_left_now: int):
        already_visited_here = False
        if visited.has(room_name):
            already_visited_here = True
            last_time_left = visited.get(room_name)
            if last_time_left < 0 or last_time_left >= time_left_now:
                return

        visited.set(room_name, time_left_now)

        if not already_visited_here:
            continuing = visitor(room_name)
            if not continuing:
                visited.set(room_name, -1)
                return

        time_left_after_this_room = time_left_now - 50
        if time_left_after_this_room > 0:
            exits = Game.map.describeExits(room_name)
            for room_key in _.shuffle(Object.keys(exits)):
                next_room = exits[room_key]
                visit_with_neighbors(next_room, time_left_after_this_room)

    visit_with_neighbors(center_room, max_distance)
Esempio n. 3
0
def volatile():
    # type: () -> JSMap[str, Any]
    global _tick_stored_for
    global _volatile_memory
    if _volatile_memory is None or _tick_stored_for < Game.time:
        _tick_stored_for = Game.time
        _volatile_memory = new_map()
    return _volatile_memory
Esempio n. 4
0
def mem(key):
    # type: (str) -> JSMap[Any, Any]
    """
    :rtype: jstools.js_set_map.JSMap
    """
    v = volatile()
    if not v.has(key):
        v.set(key, new_map())
    return v.get(key)
Esempio n. 5
0
def _deserialize_data(data: str) -> StoredRoom:
    # NOTE: this cache is only ever reset as a last resort, in normal operation the server should reset the global
    # before this is reached.
    global _cached_data, _cache_created
    if Game.time - _cache_created > 1000:
        _cached_data = new_map()
        _cache_created = Game.time
    deserialized = _cached_data.get(data)
    if deserialized:
        return deserialized
    deserialized = StoredRoom.decode(data)
    _cached_data.set(data, deserialized)
    return deserialized
Esempio n. 6
0
 def note_stage3_creep(self, creep, squad_id):
     # type: (RoleBase, Union[Flag, str]) -> None
     """
     :type creep: creeps.base.RoleBase
     :type squad_id: str
     """
     if not self._stage3_registered_for_squad_id:
         self._stage3_registered_for_squad_id = new_map()
     members = self._stage3_registered_for_squad_id.get(squad_id)
     if not members:
         members = []
         self._stage3_registered_for_squad_id.set(squad_id, members)
     members.push(creep)
Esempio n. 7
0
 def note_stage0_creep(self, creep, target):
     # type: (RoleBase, Union[Flag, Location]) -> None
     """
     :type creep: creeps.base.RoleBase
     :type target: Flag | position_management.locations.Location
     """
     if self._stage0_registered_for_target is undefined:
         self._stage0_registered_for_target = new_map()
     registered_so_far_tuple = self._stage0_registered_for_target.get(
         target.name)
     if not registered_so_far_tuple:
         registered_so_far_tuple = (target, [])
         self._stage0_registered_for_target.set(target.name,
                                                registered_so_far_tuple)
     registered_so_far_tuple[1].append(creep)
Esempio n. 8
0
 def boost_or_depot(self, creep):
     # type: (RoleBase) -> None
     """
     :type creep: creeps.base.RoleBase
     """
     if not self.can_boost(creep):
         creep.go_to_depot()
         return
     if self._boost_registered is undefined:
         self._boost_registered = new_map()
     specialty = creep.findSpecialty()
     specialty_list = self._boost_registered.get(specialty)
     if not specialty_list:
         specialty_list = []
         self._boost_registered.set(specialty, specialty_list)
     specialty_list.append(creep)
Esempio n. 9
0
def usage_map_of(room_name):
    # type: (str) -> JSMap[int, int]
    """
    :type room_name: str
    :rtype: jstools.js_set_map.JSMap
    """
    map_of_values = new_map()

    gmem = _get_mem()
    if room_name not in gmem:
        return map_of_values
    for data_string in gmem[room_name]:
        points = data_string[data_string.codePointAt(0):]
        for i in range(0, len(points)):
            xy = points.codePointAt(i)
            if map_of_values.has(xy):
                map_of_values.set(xy, map_of_values.get(xy) + 1)
            else:
                map_of_values.set(xy, 1)
    return map_of_values
Esempio n. 10
0
def refresh_flag_caches():
    # type: () -> None
    global _last_flag_len, _last_checked_flag_len, _cache_refresh_time, \
        _room_name_to_flag_type_to_flags, _flag_type_to_flags, _flag_color_to_flag_secondary_color_to_flags

    game_flag_names = Object.keys(Game.flags)

    _cache_refresh_time = Game.time + _REFRESH_EVERY
    _last_flag_len = len(game_flag_names)

    _room_name_to_flag_type_to_flags = new_map()
    _flag_type_to_flags = new_map()
    _flag_color_to_flag_secondary_color_to_flags = new_map()

    possible_flag_mains = _.values(main_to_flag_primary)

    for flag_name in game_flag_names:
        flag = Game.flags[flag_name]
        if possible_flag_mains.includes(flag.color):
            if _flag_color_to_flag_secondary_color_to_flags.has(flag.color):
                flag_secondary_to_flags = _flag_color_to_flag_secondary_color_to_flags.get(
                    flag.color)
            else:
                flag_secondary_to_flags = new_map()
                _flag_color_to_flag_secondary_color_to_flags.set(
                    flag.color, flag_secondary_to_flags)
            if flag_secondary_to_flags.has(flag.secondaryColor):
                flag_secondary_to_flags.get(flag.secondaryColor).push(flag)
            else:
                flag_secondary_to_flags.set(flag.secondaryColor, [flag])

            if flag_secondary_to_flags.has(_ALL_OF_PRIMARY):
                flag_secondary_to_flags.get(_ALL_OF_PRIMARY).push(flag)
            else:
                flag_secondary_to_flags.set(_ALL_OF_PRIMARY, [flag])

            if _room_name_to_color_to_secondary_to_flags.has(
                    flag.pos.roomName):
                in_room_color_to_secondary_to_flags = _room_name_to_color_to_secondary_to_flags.get(
                    flag.pos.roomName)
            else:
                in_room_color_to_secondary_to_flags = new_map()
                _room_name_to_color_to_secondary_to_flags.set(
                    flag.pos.roomName, in_room_color_to_secondary_to_flags)

            if in_room_color_to_secondary_to_flags.has(flag.color):
                in_room_flag_secondary_to_flags = in_room_color_to_secondary_to_flags.get(
                    flag.color)
            else:
                in_room_flag_secondary_to_flags = new_map()
                in_room_color_to_secondary_to_flags.set(
                    flag.color, in_room_flag_secondary_to_flags)
            if in_room_flag_secondary_to_flags.has(flag.secondaryColor):
                in_room_flag_secondary_to_flags.get(
                    flag.secondaryColor).push(flag)
            else:
                in_room_flag_secondary_to_flags.set(flag.secondaryColor,
                                                    [flag])

            if in_room_flag_secondary_to_flags.has(_ALL_OF_PRIMARY):
                in_room_flag_secondary_to_flags.get(_ALL_OF_PRIMARY).push(flag)
            else:
                in_room_flag_secondary_to_flags.set(_ALL_OF_PRIMARY, [flag])

        if _flag_type_to_flags.has(flag.hint):
            _flag_type_to_flags.get(flag.hint).push(flag)
        else:
            _flag_type_to_flags.set(flag.hint, [flag])

        if _room_name_to_flag_type_to_flags.has(flag.pos.roomName):
            in_room_flag_type_to_flags = _room_name_to_flag_type_to_flags.get(
                flag.pos.roomName)
        else:
            in_room_flag_type_to_flags = new_map()
            _room_name_to_flag_type_to_flags.set(flag.pos.roomName,
                                                 in_room_flag_type_to_flags)
        if in_room_flag_type_to_flags.has(flag.hint):
            in_room_flag_type_to_flags.get(flag.hint).push(flag)
        else:
            in_room_flag_type_to_flags.set(flag.hint, [flag])
Esempio n. 11
0
                sub_type)
    for sub_type in Object.keys(flag_sub_to_structure_type):
        structure_type_to_flag_sub[flag_sub_to_structure_type[sub_type]] = int(
            sub_type)


define_reverse_maps()

_REFRESH_EVERY = 50

_last_flag_len = 0
_last_checked_flag_len = 0

_cache_refresh_time = 0

_flag_type_to_flags = new_map()  # type: JSMap[int, List[Flag]]
_flag_color_to_flag_secondary_color_to_flags = new_map(
)  # type: JSMap[int, JSMap[int, List[Flag]]]
_room_name_to_flag_type_to_flags = new_map(
)  # type: JSMap[str, JSMap[int, List[Flag]]]
_room_name_to_color_to_secondary_to_flags = new_map(
)  # type: JSMap[str, JSMap[int, JSMap[int, List[Flag]]]]

_ALL_OF_PRIMARY = '__all__'


def refresh_flag_caches():
    # type: () -> None
    global _last_flag_len, _last_checked_flag_len, _cache_refresh_time, \
        _room_name_to_flag_type_to_flags, _flag_type_to_flags, _flag_color_to_flag_secondary_color_to_flags
Esempio n. 12
0
if TYPE_CHECKING:
    from empire.hive import HiveMind
    from jstools.js_set_map import JSMap, JSSet

__pragma__('noalias', 'name')
__pragma__('noalias', 'undefined')
__pragma__('noalias', 'Infinity')
__pragma__('noalias', 'keys')
__pragma__('noalias', 'get')
__pragma__('noalias', 'set')
__pragma__('noalias', 'type')
__pragma__('noalias', 'update')
__pragma__('noalias', 'values')

_cache_created = Game.time
_cached_data = new_map()  # type: JSMap[str, StoredRoom]

_metadata_segment = 14
_portal_data_segment = 14
_segments_to_use = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
_room_data_segments = [5, 6, 7, 8, 9, 10]
_old_room_data_segments = [11, 12, 13]

_segments_cache = new_map()  # type: JSMap[int, _Memory]
_segments_last_retrieved = new_map()  # type: JSMap[int, int]
_modified_segments = new_set()  # type: JSSet[int]
_segment_change_reasons = new_map()  # type: JSMap[int, List[str]]


def initial_modification_check():
    # TODO: periodic check which balances segments which have too much data stored in them, and runs