コード例 #1
0
    def _randomize_items(self):
        categories = {}
        items = OrderedDict()
        cats_as_list = list(ALLOWED_ITEM_CATS)

        # 1/8 chance for money to get a chance
        if choice([True] + [False] * 7):
            cats_as_list.append(MappaItemCategory.POKE)

        # 1/8 chance for Link Box to get a chance
        if choice([True] + [False] * 7):
            cats_as_list.append(MappaItemCategory.LINK_BOX)

        cats_as_list.sort(key=lambda x: x.value)
        weights = sorted(self._random_weights(len(cats_as_list)))
        for i, cat in enumerate(cats_as_list):
            categories[cat] = weights[i]

            if cat.number_of_items is not None:
                allowed_cat_item_ids = [x for x in cat.item_ids() if x in ALLOWED_ITEM_IDS]
                upper_limit = min(MAX_ITEMS_PER_CAT, len(allowed_cat_item_ids))
                if upper_limit <= MIN_ITEMS_PER_CAT:
                    n_items = MIN_ITEMS_PER_CAT
                else:
                    n_items = randrange(MIN_ITEMS_PER_CAT, upper_limit)
                cat_item_ids = sorted(set(
                    (choice(allowed_cat_item_ids) for _ in range(0, n_items))
                ))
                cat_weights = sorted(self._random_weights(len(cat_item_ids)))

                for item_id, weight in zip(cat_item_ids, cat_weights):
                    items[Pmd2DungeonItem(item_id, '???')] = weight

        return MappaItemList(categories, OrderedDict(sorted(items.items(), key=lambda i: i[0].id)))
コード例 #2
0
def mappa_floor_xml_import(xml: Element, floor: MappaFloor):
    """Imports all data available in the mappa floor XML into the given model."""
    for child in xml:
        if child.tag == XML_FLOOR_LAYOUT:
            floor_number_before = floor.layout.floor_number
            floor.layout = MappaFloorLayout.from_xml(child)
            floor.layout.floor_number = floor_number_before
        elif child.tag == XML_MONSTER_LIST:
            monsters = []
            for monster in child:
                monsters.append(MappaMonster.from_xml(monster))
            floor.monsters = monsters
        elif child.tag == XML_TRAP_LIST:
            floor.traps = MappaTrapList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__FLOOR:
            floor.floor_items = MappaItemList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__SHOP:
            floor.shop_items = MappaItemList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__MONSTER_HOUSE:
            floor.monster_house_items = MappaItemList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__BURIED:
            floor.buried_items = MappaItemList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__UNK1:
            floor.unk_items1 = MappaItemList.from_xml(child)
        elif child.tag == XML_ITEM_LIST and child.get(
                XML_ITEM_LIST__TYPE) == XML_ITEM_LIST__TYPE__UNK2:
            floor.unk_items2 = MappaItemList.from_xml(child)
        else:
            raise XmlValidateError(
                f(_('Floor parsing: Unexpected {child.tag}')))
コード例 #3
0
    def from_xml(cls, ele: Element) -> 'MappaFloor':
        data = {
            'layout': None,
            'monsters': None,
            'traps': None,
            'floor_items': None,
            'shop_items': None,
            'monster_house_items': None,
            'buried_items': None,
            'unk_items1': None,
            'unk_items2': None
        }
        for child in ele:
            if child.tag == XML_FLOOR_LAYOUT and data['layout'] is None:
                data['layout'] = MappaFloorLayout.from_xml(child)
            elif child.tag == XML_MONSTER_LIST and data['monsters'] is None:
                monsters = []
                for monster in child:
                    monsters.append(MappaMonster.from_xml(monster))
                data['monsters'] = monsters
            elif child.tag == XML_TRAP_LIST and data['traps'] is None:
                data['traps'] = MappaTrapList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__FLOOR and data['floor_items'] is None:
                data['floor_items'] = MappaItemList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__SHOP and data['shop_items'] is None:
                data['shop_items'] = MappaItemList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__MONSTER_HOUSE and data[
                    'monster_house_items'] is None:
                data['monster_house_items'] = MappaItemList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__BURIED and data['buried_items'] is None:
                data['buried_items'] = MappaItemList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__UNK1 and data['unk_items1'] is None:
                data['unk_items1'] = MappaItemList.from_xml(child)
            elif child.tag == XML_ITEM_LIST and child.get(
                    XML_ITEM_LIST__TYPE
            ) == XML_ITEM_LIST__TYPE__UNK2 and data['unk_items2'] is None:
                data['unk_items2'] = MappaItemList.from_xml(child)
            else:
                raise XmlValidateError(
                    f'Floor parsing: Unexpected {child.tag}')

        for k, v in data.items():
            if v is None:
                raise XmlValidateError(f'Missing {k} for Floor data.')

        return cls(**data)
コード例 #4
0
 def from_mappa(cls, read: 'MappaBinReadContainer',
                floor_data: bytes) -> 'MappaFloor':
     return cls(
         cls._from_cache(
             read, read.floor_layout_data_start +
             32 * read_uintle(floor_data, 0x00, 2),
             lambda pnt: MappaFloorLayout.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data,
                               read.monster_spawn_list_index_start,
                               read_uintle(floor_data, 0x02, 2)),
             lambda pnt: MappaMonster.list_from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.trap_spawn_list_index_start,
                               read_uintle(floor_data, 0x04, 2)),
             lambda pnt: MappaTrapList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x06, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x08, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x0A, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x0C, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x0E, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)),
         cls._from_cache(
             read,
             cls._read_pointer(read.data, read.item_spawn_list_index_start,
                               read_uintle(floor_data, 0x10, 2)),
             lambda pnt: MappaItemList.from_mappa(read, pnt)))
コード例 #5
0
 def serialize(cls, data: MappaItemList, **kwargs) -> bytes:
     return data.to_mappa()
コード例 #6
0
 def deserialize(cls, data: bytes, items, **kwargs) -> MappaItemList:
     return MappaItemList.from_bytes(data, items, 0)
コード例 #7
0
 def deserialize(cls, data: bytes, items,
                 **kwargs: OptionalKwargs) -> MappaItemList:  # type: ignore
     return MappaItemList.from_bytes(data, items, 0)