示例#1
0
def test_encode_int_with_limits_bitstring(value, limits, expected):
    # Run
    result = bitpacking._format_string_for(
        list(bitpacking.encode_int_with_limits(value, limits)))

    # Assert
    assert result == expected
示例#2
0
    def bit_pack_encode(
            self, item: MajorItem,
            reference: "MajorItemState") -> Iterator[Tuple[int, int]]:
        db = default_database.resource_database_for(item.game)
        if item.progression:
            main_index = item.progression[0]
        else:
            main_index = item.ammo_index[0]
        main_item = db.get_item(main_index)

        # original location
        if item.original_index is not None:
            yield from bitpacking.encode_bool(
                self.include_copy_in_original_location)

        # num shuffled
        yield from bitpacking.encode_int_with_limits(self.num_shuffled_pickups,
                                                     DEFAULT_MAXIMUM_SHUFFLED)

        # starting item
        if main_item.max_capacity > 1:
            yield from bitpacking.encode_int_with_limits(
                self.num_included_in_starting_items,
                (2, main_item.max_capacity + 1))
        else:
            yield self.num_included_in_starting_items, main_item.max_capacity + 1

        # priority
        yield from bitpacking.BitPackFloat(
            self.priority).bit_pack_encode(PRIORITY_LIMITS)

        # ammo index
        assert len(self.included_ammo) == len(item.ammo_index)
        if self.included_ammo:
            custom_ammo = self.included_ammo != reference.included_ammo
            yield from bitpacking.encode_bool(custom_ammo)
            if custom_ammo:
                all_equal = len(set(self.included_ammo)) == 1
                if len(item.ammo_index) > 1:
                    yield from bitpacking.encode_bool(all_equal)

                for ammo_index, ammo in zip(item.ammo_index,
                                            self.included_ammo):
                    yield ammo, db.get_item(ammo_index).max_capacity + 1
                    if all_equal:
                        break
def test_encode_int_with_limits(limits_fixture):
    # Setup
    value, limits, encoded = limits_fixture

    # Run
    result = list(bitpacking.encode_int_with_limits(value, limits))

    # Assert
    assert result == encoded
示例#4
0
def test_encode_int_with_limits_round_trip(value: int,
                                           limits: Tuple[int, ...],
                                           ):
    # Run
    data = bitpacking._pack_encode_results(list(bitpacking.encode_int_with_limits(value, limits)))
    decoded = bitpacking.decode_int_with_limits(bitpacking.BitPackDecoder(data), limits)

    # Assert
    assert decoded == value
示例#5
0
    def bit_pack_encode(self, metadata) -> Iterator[Tuple[int, int]]:
        ammo: Ammo = metadata["ammo"]
        db = default_database.resource_database_for(ammo.game)

        for count, ammo_index in zip(self.ammo_count, ammo.items):
            ammo_item = db.get_item(ammo_index)
            yield from bitpacking.encode_int_with_limits(
                count,
                (ammo_item.max_capacity // 2, ammo_item.max_capacity + 1),
            )

        yield from bitpacking.encode_big_int(self.pickup_count)
        if ammo.unlocked_by is not None:
            yield from bitpacking.encode_bool(self.requires_major_item)
示例#6
0
    def bit_pack_encode(self, metadata) -> Iterator[Tuple[int, int]]:
        yield self.current_version(), _PERMALINK_MAX_VERSION
        yield self.seed_number, _PERMALINK_MAX_SEED
        yield from bitpacking.encode_bool(self.spoiler)
        yield from bitpacking.encode_int_with_limits(self.player_count, _PERMALINK_PLAYER_COUNT_LIMITS)

        manager = PresetManager(None)

        previous_unique_presets = []
        for preset in self.presets.values():
            yield from bitpacking.encode_bool(preset in previous_unique_presets)
            if preset in previous_unique_presets:
                yield from bitpacking.pack_array_element(preset, previous_unique_presets)
                continue

            previous_unique_presets.append(preset)
            yield from _encode_preset(preset, manager)
示例#7
0
    def bit_pack_encode(self, item: MajorItem) -> Iterator[Tuple[int, int]]:
        # original location
        yield int(self.include_copy_in_original_location), 2

        # num shuffled
        yield from bitpacking.encode_int_with_limits(self.num_shuffled_pickups,
                                                     DEFAULT_MAXIMUM_SHUFFLED)

        # starting item
        yield self.num_included_in_starting_items, (
            ENERGY_TANK_MAXIMUM_COUNT
            if item.item_category == ItemCategory.ENERGY_TANK else 2)

        # ammo index
        assert len(self.included_ammo) == len(item.ammo_index)
        for ammo in self.included_ammo:
            yield ammo, 256

        # allowed_as_random_starting_item
        yield from bitpacking.encode_bool(self.allowed_as_random_starting_item)
示例#8
0
 def bit_pack_encode(self, metadata) -> Iterator[Tuple[int, int]]:
     for index, entry in self.value:
         yield index.index, 255
         yield from bitpacking.encode_int_with_limits(entry.player, (self.num_players,))
         yield from BitPackPickupEntry(entry.pickup, self.database).bit_pack_encode({})