Ejemplo n.º 1
0
void ThingTypeManager::loadOtb(const std::string& file)
{
    FileStreamPtr fin = g_resources.openFile(file);

    uint signature = fin->getU32();
    if(signature != 0)
        stdext::throw_exception("invalid otb file");

    BinaryTreePtr root = fin->getBinaryTree();

    signature = root->getU32();
    if(signature != 0)
        stdext::throw_exception("invalid otb file");

    root->getU32(); // flags

    m_otbMajorVersion = root->getU32();
    m_otbMinorVersion = root->getU32();
    root->getU32(); // build number
    root->skip(128); // description

    m_itemTypes.resize(root->getChildren().size(), m_nullItemType);
    for(const BinaryTreePtr& node : root->getChildren()) {
        ItemTypePtr itemType(new ItemType);
        itemType->unserialize(node);
        addItemType(itemType);
    }

    m_otbLoaded = true;
}
Ejemplo n.º 2
0
bool ThingTypeManager::loadDat(const std::string& file)
{
    m_datLoaded = false;
    m_datSignature = 0;
    try {
        FileStreamPtr fin = g_resources.openFile(file);

        m_datSignature = fin->getU32();

        int numThings[ThingLastCategory];
        for(int category = 0; category < ThingLastCategory; ++category) {
            int count = fin->getU16() + 1;
            m_thingTypes[category].clear();
            m_thingTypes[category].resize(count, m_nullThingType);
        }

        for(int category = 0; category < ThingLastCategory; ++category) {
            uint16 firstId = 1;
            if(category == ThingCategoryItem)
                firstId = 100;
            for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) {
                ThingTypePtr type(new ThingType);
                type->unserialize(id, (ThingCategory)category, fin);
                m_thingTypes[category][id] = type;
            }
        }

        m_datLoaded = true;
        return true;
    } catch(stdext::exception& e) {
        g_logger.error(stdext::format("Failed to read dat '%s': %s'", file, e.what()));
        return false;
    }
}
Ejemplo n.º 3
0
void ThingTypeManager::loadOtb(const std::string& file)
{
    try {
        FileStreamPtr fin = g_resources.openFile(file);

        uint signature = fin->getU32();
        if(signature != 0)
            stdext::throw_exception("invalid otb file");

        BinaryTreePtr root = fin->getBinaryTree();
        root->skip(1); // otb first byte is always 0

        signature = root->getU32();
        if(signature != 0)
            stdext::throw_exception("invalid otb file");

        uint8 rootAttr = root->getU8();
        if(rootAttr == 0x01) { // OTB_ROOT_ATTR_VERSION
            uint16 size = root->getU16();
            if(size != 4 + 4 + 4 + 128)
                stdext::throw_exception("invalid otb root attr version size");

            m_otbMajorVersion = root->getU32();
            m_otbMinorVersion = root->getU32();
            root->skip(4); // buildNumber
            root->skip(128); // description
        }

        m_reverseItemTypes.clear();
        m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
        m_reverseItemTypes.resize(root->getChildren().size() + 1, m_nullItemType);

        for(const BinaryTreePtr& node : root->getChildren()) {
            ItemTypePtr itemType(new ItemType);
            itemType->unserialize(node);
            addItemType(itemType);

            uint16 clientId = itemType->getClientId();
            if(unlikely(clientId >= m_reverseItemTypes.size()))
                m_reverseItemTypes.resize(clientId + 1);
            m_reverseItemTypes[clientId] = itemType;
        }

        m_otbLoaded = true;
        g_lua.callGlobalField("g_things", "onLoadOtb", file);
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to load '%s' (OTB file): %s", file, e.what()));
    }
}
Ejemplo n.º 4
0
void ThingTypeManager::loadOtb(const std::string& file)
{
    try {
        FileStreamPtr fin = g_resources.openFile(file);

        uint signature = fin->getU32();
        if(signature != 0)
            stdext::throw_exception("invalid otb file");

        BinaryTreePtr root = fin->getBinaryTree();

        signature = root->getU32();
        if(signature != 0)
            stdext::throw_exception("invalid otb file");

        root->skip(4);

        m_otbMajorVersion = root->getU32();
        m_otbMinorVersion = root->getU32();
        root->skip(4);
        root->skip(128); // description

        m_reverseItemTypes.clear();
        m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
        m_reverseItemTypes.resize(root->getChildren().size() + 1, m_nullItemType);

        for(const BinaryTreePtr& node : root->getChildren()) {
            ItemTypePtr itemType(new ItemType);
            itemType->unserialize(node);
            addItemType(itemType);

            uint16 clientId = itemType->getClientId();
            if(unlikely(clientId >= m_reverseItemTypes.size()))
                m_reverseItemTypes.resize(clientId + 1);
            m_reverseItemTypes[clientId] = itemType;
        }

        m_otbLoaded = true;
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to load '%s' (OTB file): %s", file, e.what()));
    }
}
Ejemplo n.º 5
0
bool Map::loadOtcm(const std::string& fileName)
{
    try {
        FileStreamPtr fin = g_resources.openFile(fileName);
        if(!fin)
            stdext::throw_exception("unable to open file");

        stdext::timer loadTimer;
        fin->cache();

        uint32 signature = fin->getU32();
        if(signature != OTCM_SIGNATURE)
            stdext::throw_exception("invalid otcm file");

        uint16 start = fin->getU16();
        uint16 version = fin->getU16();
        fin->getU32(); // flags

        switch(version) {
            case 1: {
                fin->getString(); // description
                uint32 datSignature = fin->getU32();
                fin->getU16(); // protocol version
                fin->getString(); // world name

                if(datSignature != g_things.getDatSignature())
                    g_logger.warning("otcm map loaded was created with a different dat signature");

                break;
            }
            default:
                stdext::throw_exception("otcm version not supported");
        }

        fin->seek(start);

        while(true) {
            Position pos;

            pos.x = fin->getU16();
            pos.y = fin->getU16();
            pos.z = fin->getU8();

            // end of file
            if(!pos.isValid())
                break;

            const TilePtr& tile = g_map.createTile(pos);

            int stackPos = 0;
            while(true) {
                int id = fin->getU16();

                // end of tile
                if(id == 0xFFFF)
                    break;

                int countOrSubType = fin->getU8();

                ItemPtr item = Item::create(id);
                item->setCountOrSubType(countOrSubType);

                if(item->isValid())
                    tile->addThing(item, stackPos++);
            }
        }

        fin->close();

        g_logger.debug(stdext::format("Otcm load time: %.2f seconds", loadTimer.elapsed_seconds()));
        return true;
    } catch(stdext::exception& e) {
        g_logger.error(stdext::format("failed to load OTCM map: %s", e.what()));
        return false;
    }
}
Ejemplo n.º 6
0
void Map::loadOtbm(const std::string& fileName)
{
    FileStreamPtr fin = g_resources.openFile(fileName);
    if(!fin)
        stdext::throw_exception(stdext::format("Unable to load map '%s'", fileName));

    fin->cache();
    if(!g_things.isOtbLoaded())
        stdext::throw_exception("OTB isn't loaded yet to load a map.");

    if(fin->getU32())
        stdext::throw_exception("Unknown file version detected");

    BinaryTreePtr root = fin->getBinaryTree();
    if(root->getU8())
        stdext::throw_exception("could not read root property!");

    uint32 headerVersion = root->getU32();
    if(!headerVersion || headerVersion > 3)
        stdext::throw_exception(stdext::format("Unknown OTBM version detected: %u.", headerVersion));

    setWidth(root->getU16());
    setHeight(root->getU16());

    uint32 headerMajorItems = root->getU8();
    if(headerMajorItems < 3) {
        stdext::throw_exception(stdext::format("This map needs to be upgraded. read %d what it's supposed to be: %u",
                                               headerMajorItems, g_things.getOtbMajorVersion()));
    }

    if(headerMajorItems > g_things.getOtbMajorVersion()) {
        stdext::throw_exception(stdext::format("This map was saved with different OTB version. read %d what it's supposed to be: %d",
                                               headerMajorItems, g_things.getOtbMajorVersion()));
    }

    root->skip(3);
    uint32 headerMinorItems =  root->getU32();
    if(headerMinorItems > g_things.getOtbMinorVersion()) {
        g_logger.warning(stdext::format("This map needs an updated OTB. read %d what it's supposed to be: %d or less",
                                        headerMinorItems, g_things.getOtbMinorVersion()));
    }

    BinaryTreePtr node = root->getChildren()[0];
    if(node->getU8() != OTBM_MAP_DATA)
        stdext::throw_exception("Could not read root data node");

    while (node->canRead()) {
        uint8 attribute = node->getU8();
        std::string tmp = node->getString();
        switch (attribute) {
        case OTBM_ATTR_DESCRIPTION:
            setDescription(tmp);
            break;
        case OTBM_ATTR_SPAWN_FILE:
            setSpawnFile(fileName.substr(0, fileName.rfind('/') + 1) + tmp);
            break;
        case OTBM_ATTR_HOUSE_FILE:
            setHouseFile(fileName.substr(0, fileName.rfind('/') + 1) + tmp);
            break;
        default:
            stdext::throw_exception(stdext::format("Invalid attribute '%d'", (int)attribute));
        }
    }

    for(const BinaryTreePtr &nodeMapData : node->getChildren()) {
        uint8 mapDataType = nodeMapData->getU8();
        if(mapDataType == OTBM_TILE_AREA) {
            Position basePos = nodeMapData->getPosition();

            for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) {
                uint8 type = nodeTile->getU8();
                if(type != OTBM_TILE && type != OTBM_HOUSETILE)
                    stdext::throw_exception(stdext::format("invalid node tile type %d", (int)type));

                HousePtr house = nullptr;
                uint32 flags = TILESTATE_NONE;
                Position pos = basePos + nodeTile->getPoint();

                if(type ==  OTBM_HOUSETILE) {
                    uint32 hId = nodeTile->getU32();
                    TilePtr tile = getOrCreateTile(pos);
                    if(!(house = m_houses.getHouse(hId))) {
                        house = HousePtr(new House(hId));
                        m_houses.addHouse(house);
                    }
                    house->setTile(tile);
                }

                while(nodeTile->canRead()) {
                    uint8 tileAttr = nodeTile->getU8();
                    switch (tileAttr) {
                        case OTBM_ATTR_TILE_FLAGS: {
                            uint32 _flags = nodeTile->getU32();
                            if((_flags & TILESTATE_PROTECTIONZONE) == TILESTATE_PROTECTIONZONE)
                                flags |= TILESTATE_PROTECTIONZONE;
                            else if((_flags & TILESTATE_OPTIONALZONE) == TILESTATE_OPTIONALZONE)
                                flags |= TILESTATE_OPTIONALZONE;
                            else if((_flags & TILESTATE_HARDCOREZONE) == TILESTATE_HARDCOREZONE)
                                flags |= TILESTATE_HARDCOREZONE;

                            if((_flags & TILESTATE_NOLOGOUT) == TILESTATE_NOLOGOUT)
                                flags |= TILESTATE_NOLOGOUT;

                            if((_flags & TILESTATE_REFRESH) == TILESTATE_REFRESH)
                                flags |= TILESTATE_REFRESH;

                            break;
                        }
                        case OTBM_ATTR_ITEM: {
                            addThing(Item::createFromOtb(nodeTile->getU16()), pos);
                            break;
                        }
                        default: {
                            stdext::throw_exception(stdext::format("invalid tile attribute %d at pos %s", (int)tileAttr, stdext::to_string(pos)));
                        }
                    }
                }

                for(const BinaryTreePtr &nodeItem : nodeTile->getChildren()) {
                    if(nodeItem->getU8() != OTBM_ITEM)
                        stdext::throw_exception("invalid item node");

                    ItemPtr item = Item::createFromOtb(nodeItem->getU16());
                    item->unserializeItem(nodeItem);

                    if(item->isContainer()) {
                        for(const BinaryTreePtr& containerItem : nodeItem->getChildren()) {
                            if(containerItem->getU8() != OTBM_ITEM)
                                stdext::throw_exception("invalid container item node");

                            ItemPtr cItem = Item::createFromOtb(containerItem->getU16());
                            cItem->unserializeItem(containerItem);
                            //item->addContainerItem(cItem);
                        }
                    }

                    if(house && item->isMoveable()) {
                        g_logger.warning(stdext::format("Movable item found in house: %d at pos %s - escaping...", item->getId(), stdext::to_string(pos)));
                        item.reset();
                    }

                    addThing(item, pos);
                }

                if(const TilePtr& tile = getTile(pos))
                    tile->setFlags((tileflags_t)flags);
            }
        } else if(mapDataType == OTBM_TOWNS) {
            TownPtr town = nullptr;
            for(const BinaryTreePtr &nodeTown : nodeMapData->getChildren()) {
                if(nodeTown->getU8() != OTBM_TOWN)
                    stdext::throw_exception("invalid town node.");

                uint32 townId = nodeTown->getU32();
                std::string townName = nodeTown->getString();
                Position townCoords = nodeTown->getPosition();
                if(!(town = m_towns.getTown(townId))) {
                    town = TownPtr(new Town(townId, townName, townCoords));
                    m_towns.addTown(town);
                }
            }
        } else if(mapDataType == OTBM_WAYPOINTS && headerVersion > 1) {
            for(const BinaryTreePtr &nodeWaypoint : nodeMapData->getChildren()) {
                if(nodeWaypoint->getU8() != OTBM_WAYPOINT)
                    stdext::throw_exception("invalid waypoint node.");

                std::string name = nodeWaypoint->getString();
                Position waypointPos = nodeWaypoint->getPosition();
                if(waypointPos.isValid() && !name.empty() && m_waypoints.find(waypointPos) == m_waypoints.end())
                    m_waypoints.insert(std::make_pair(waypointPos, name));
            }
        } else
            stdext::throw_exception(stdext::format("Unknown map data node %d", (int)mapDataType));
    }

    g_logger.debug("OTBM read successfully.");
    fin->close();

    loadSpawns(getSpawnFile());
//  m_houses.load(getHouseFile());
}
Ejemplo n.º 7
0
void ThingType::unserialize(uint16 clientId, ThingCategory category, const FileStreamPtr& fin)
{
    m_null = false;
    m_id = clientId;
    m_category = category;

    int count = 0, attr = -1;
    bool done = false;
    for(int i = 0 ; i < ThingLastAttr;++i) {
        count++;
        attr = fin->getU8();
        if(attr == ThingLastAttr) {
            done = true;
            break;
        }

        if(g_game.getClientVersion() >= 1000) {
            /* In 10.10+ all attributes from 16 and up were
             * incremented by 1 to make space for 16 as
             * "No Movement Animation" flag.
             */
            if(attr == 16)
                attr = ThingAttrNoMoveAnimation;
            else if(attr > 16)
                attr -= 1;
        } else if(g_game.getClientVersion() >= 860) {
            /* Default attribute values follow
             * the format of 8.6-9.86.
             * Therefore no changes here.
             */
        } else if(g_game.getClientVersion() >= 780) {
            /* In 7.80-8.54 all attributes from 8 and higher were
             * incremented by 1 to make space for 8 as
             * "Item Charges" flag.
             */
            if(attr == 8) {
                m_attribs.set(ThingAttrChargeable, true);
                continue;
            } else if(attr > 8)
                attr -= 1;
        } else if(g_game.getClientVersion() >= 755) {
            /* In 7.55-7.72 attributes 23 is "Floor Change". */
            if(attr == 23)
                attr = ThingAttrFloorChange;
        } else if(g_game.getClientVersion() >= 740) {
            /* In 7.4-7.5 attribute "Ground Border" did not exist
             * attributes 1-15 have to be adjusted.
             * Several other changes in the format.
             */
            if(attr > 0 && attr <= 15)
                attr += 1;
            else if(attr == 16)
                attr = ThingAttrLight;
            else if(attr == 17)
                attr = ThingAttrFloorChange;
            else if(attr == 18)
                attr = ThingAttrFullGround;
            else if(attr == 19)
                attr = ThingAttrElevation;
            else if(attr == 20)
                attr = ThingAttrDisplacement;
            else if(attr == 22)
                attr = ThingAttrMinimapColor;
            else if(attr == 23)
                attr = ThingAttrRotateable;
            else if(attr == 24)
                attr = ThingAttrLyingCorpse;
            else if(attr == 25)
                attr = ThingAttrHangable;
            else if(attr == 26)
                attr = ThingAttrHookSouth;
            else if(attr == 27)
                attr = ThingAttrHookEast;
            else if(attr == 28)
                attr = ThingAttrAnimateAlways;

            /* "Multi Use" and "Force Use" are swapped */
            if(attr == ThingAttrMultiUse)
                attr = ThingAttrForceUse;
            else if(attr == ThingAttrForceUse)
                attr = ThingAttrMultiUse;
        }

        switch(attr) {
            case ThingAttrDisplacement: {
                if(g_game.getClientVersion() >= 755) {
                    m_displacement.x = fin->getU16();
                    m_displacement.y = fin->getU16();
                } else {
                    m_displacement.x = 8;
                    m_displacement.y = 8;
                }
                m_attribs.set(attr, true);
                break;
            }
            case ThingAttrLight: {
                Light light;
                light.intensity = fin->getU16();
                light.color = fin->getU16();
                m_attribs.set(attr, light);
                break;
            }
            case ThingAttrMarket: {
                MarketData market;
                market.category = fin->getU16();
                market.tradeAs = fin->getU16();
                market.showAs = fin->getU16();
                market.name = fin->getString();
                market.restrictVocation = fin->getU16();
                market.requiredLevel = fin->getU16();
                m_attribs.set(attr, market);
                break;
            }
            case ThingAttrElevation: {
                m_elevation = fin->getU16();
                m_attribs.set(attr, m_elevation);
                break;
            }
            case ThingAttrUsable:
            case ThingAttrGround:
            case ThingAttrWritable:
            case ThingAttrWritableOnce:
            case ThingAttrMinimapColor:
            case ThingAttrCloth:
            case ThingAttrLensHelp:
                m_attribs.set(attr, fin->getU16());
                break;
            default:
                m_attribs.set(attr, true);
                break;
        };
    }

    if(!done)
        stdext::throw_exception(stdext::format("corrupt data (id: %d, category: %d, count: %d, lastAttr: %d)",
            m_id, m_category, count, attr));

    bool hasFrameGroups = (category == ThingCategoryCreature && g_game.getFeature(Otc::GameIdleAnimations));
    uint8 groupCount = hasFrameGroups ? fin->getU8() : 1;

    for(int i = 0; i < groupCount; ++i) {
        uint8 frameGroupType = FrameGroupDefault;
        if(hasFrameGroups)
            frameGroupType = fin->getU8();

        uint8 width = fin->getU8();
        uint8 height = fin->getU8();
        m_size = Size(width, height);
        if(width > 1 || height > 1) {
            m_realSize = fin->getU8();
            m_exactSize = std::min<int>(m_realSize, std::max<int>(width * 32, height * 32));
        }
        else
            m_exactSize = 32;

        m_layers = fin->getU8();
        m_numPatternX = fin->getU8();
        m_numPatternY = fin->getU8();
        if(g_game.getClientVersion() >= 755)
            m_numPatternZ = fin->getU8();
        else
            m_numPatternZ = 1;
        m_animationPhases = fin->getU8();

        if(m_animationPhases > 1 && g_game.getFeature(Otc::GameEnhancedAnimations)) {
            m_animator = AnimatorPtr(new Animator);
            m_animator->unserialize(m_animationPhases, fin);
        }

        int totalSprites = m_size.area() * m_layers * m_numPatternX * m_numPatternY * m_numPatternZ * m_animationPhases;

        if(totalSprites > 4096)
            stdext::throw_exception("a thing type has more than 4096 sprites");

        m_spritesIndex.resize(totalSprites);
        for(int i = 0; i < totalSprites; i++)
            m_spritesIndex[i] = g_game.getFeature(Otc::GameSpritesU32) ? fin->getU32() : fin->getU16();
    }

    m_textures.resize(m_animationPhases);
    m_texturesFramesRects.resize(m_animationPhases);
    m_texturesFramesOriginRects.resize(m_animationPhases);
    m_texturesFramesOffsets.resize(m_animationPhases);
}
Ejemplo n.º 8
0
void ThingType::unserialize(uint16 clientId, ThingCategory category, const FileStreamPtr& fin)
{
    m_null = false;
    m_id = clientId;
    m_category = category;

    bool done = false;
    for(int i = 0 ; i < ThingLastAttr;++i) {
        int attr = fin->getU8();
        if(attr == ThingLastAttr) {
            done = true;
            break;
        }

        if(g_game.getFeature(Otc::GameChargeableItems)) {
            if(attr == ThingAttrWritable) {
                m_attribs.set(ThingAttrChargeable, true);
                continue;
            } else if(attr > ThingAttrWritable)
                attr -= 1;
        }

        if(g_game.getProtocolVersion() >= 1010) {
            /* In 10.10 all attributes from 16 and up were
             * incremented by 1 to make space for 16 as
             * "No Movement Animation" flag.
             */
            if(attr == 16)
                attr = ThingAttrNoMoveAnimation;
            else if(attr > 16)
                attr -= 1;
        }

        switch(attr) {
            case ThingAttrDisplacement: {
                m_displacement.x = fin->getU16();
                m_displacement.y = fin->getU16();
                m_attribs.set(attr, true);
                break;
            }
            case ThingAttrLight: {
                Light light;
                light.intensity = fin->getU16();
                light.color = fin->getU16();
                m_attribs.set(attr, light);
                break;
            }
            case ThingAttrMarket: {
                MarketData market;
                market.category = fin->getU16();
                market.tradeAs = fin->getU16();
                market.showAs = fin->getU16();
                market.name = fin->getString();
                market.restrictVocation = fin->getU16();
                market.requiredLevel = fin->getU16();
                m_attribs.set(attr, market);
                break;
            }
            case ThingAttrElevation: {
                m_elevation = fin->getU16();
                m_attribs.set(attr, m_elevation);
                break;
            }
            case ThingAttrGround:
            case ThingAttrWritable:
            case ThingAttrWritableOnce:
            case ThingAttrMinimapColor:
            case ThingAttrCloth:
            case ThingAttrLensHelp:
                m_attribs.set(attr, fin->getU16());
                break;
            default:
                m_attribs.set(attr, true);
                break;
        };
    }

    if(!done)
        stdext::throw_exception("corrupt data");

    uint8 width = fin->getU8();
    uint8 height = fin->getU8();
    m_size = Size(width, height);
    m_exactSize = (width > 1 || height > 1) ? std::min((int)fin->getU8(), std::max(width * 32, height * 32)) : 32;
    m_layers = fin->getU8();
    m_numPatternX = fin->getU8();
    m_numPatternY = fin->getU8();
    m_numPatternZ = fin->getU8();
    m_animationPhases = fin->getU8();

    int totalSprites = m_size.area() * m_layers * m_numPatternX * m_numPatternY * m_numPatternZ * m_animationPhases;

    // if(totalSprites == 0)
    //     stdext::throw_exception("a thing type has no sprites");
    if(totalSprites > 4096)
        stdext::throw_exception("a thing type has more than 4096 sprites");

    m_spritesIndex.resize(totalSprites);
    for(int i = 0; i < totalSprites; i++)
        m_spritesIndex[i] = g_game.getFeature(Otc::GameSpritesU32) ? fin->getU32() : fin->getU16();

    m_textures.resize(m_animationPhases);
    m_texturesFramesRects.resize(m_animationPhases);
    m_texturesFramesOriginRects.resize(m_animationPhases);
    m_texturesFramesOffsets.resize(m_animationPhases);
}