Esempio n. 1
0
void SpriteManager::saveSpr(std::string fileName)
{
    if(!m_loaded)
        stdext::throw_exception("failed to save, spr is not loaded");

    try {
        FileStreamPtr fin = g_resources.createFile(fileName);
        if(!fin)
            stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));

        fin->cache();

        fin->addU32(m_signature);
        if(g_game.getFeature(Otc::GameSpritesU32))
            fin->addU32(m_spritesCount);
        else
            fin->addU16(m_spritesCount);

        uint32 offset = fin->tell();
        uint32 spriteAddress = offset + 4 * m_spritesCount;
        for(int i = 1; i <= m_spritesCount; i++)
            fin->addU32(0);

        for(int i = 1; i <= m_spritesCount; i++) {
            m_spritesFile->seek((i - 1) * 4 + m_spritesOffset);
            uint32 fromAdress = m_spritesFile->getU32();
            if(fromAdress != 0) {
                fin->seek(offset + (i - 1) * 4);
                fin->addU32(spriteAddress);
                fin->seek(spriteAddress);

                m_spritesFile->seek(fromAdress);
                fin->addU8(m_spritesFile->getU8());
                fin->addU8(m_spritesFile->getU8());
                fin->addU8(m_spritesFile->getU8());

                uint16 dataSize = m_spritesFile->getU16();
                fin->addU16(dataSize);
                char spriteData[SPRITE_DATA_SIZE];
                m_spritesFile->read(spriteData, dataSize);
                fin->write(spriteData, dataSize);

                spriteAddress = fin->tell();
            }
            //TODO: Check for overwritten sprites.
        }

        fin->flush();
        fin->close();
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
    }
}
Esempio n. 2
0
void ThingTypeManager::saveDat(std::string fileName)
{
    if(!m_datLoaded)
        stdext::throw_exception("failed to save, dat is not loaded");

    try {
        FileStreamPtr fin = g_resources.createFile(fileName);
        if(!fin)
            stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));

        fin->cache();

        fin->addU32(m_datSignature);

        for(int category = 0; category < ThingLastCategory; ++category)
            fin->addU16(m_thingTypes[category].size() - 1);

        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)
                m_thingTypes[category][id]->serialize(fin);
        }


        fin->flush();
        fin->close();
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
    }
}
Esempio n. 3
0
void Map::saveOtcm(const std::string& fileName)
{
#if 0
    try {
        g_clock.update();

        FileStreamPtr fin = g_resources.createFile(fileName);
        fin->cache();

        //TODO: compression flag with zlib
        uint32 flags = 0;

        // header
        fin->addU32(OTCM_SIGNATURE);
        fin->addU16(0); // data start, will be overwritten later
        fin->addU16(OTCM_VERSION);
        fin->addU32(flags);

        // version 1 header
        fin->addString("OTCM 1.0"); // map description
        fin->addU32(g_things.getDatSignature());
        fin->addU16(g_game.getClientVersion());
        fin->addString(g_game.getWorldName());

        // go back and rewrite where the map data starts
        uint32 start = fin->tell();
        fin->seek(4);
        fin->addU16(start);
        fin->seek(start);

        for(auto& pair : m_tiles) {
            const TilePtr& tile = pair.second;
            if(!tile || tile->isEmpty())
                continue;

            Position pos = pair.first;
            fin->addU16(pos.x);
            fin->addU16(pos.y);
            fin->addU8(pos.z);

            const auto& list = tile->getThings();
            auto first = std::find_if(list.begin(), list.end(), [](const ThingPtr& thing) { return thing->isItem(); });
            for(auto it = first, end = list.end(); it != end; ++it) {
                const ThingPtr& thing = *it;
                if(thing->isItem()) {
                    ItemPtr item = thing->static_self_cast<Item>();
                    fin->addU16(item->getId());
                    fin->addU8(item->getCountOrSubType());
                }
            }

            // end of tile
            fin->addU16(0xFFFF);
        }

        // end of file
        Position invalidPos;
        fin->addU16(invalidPos.x);
        fin->addU16(invalidPos.y);
        fin->addU8(invalidPos.z);

        fin->flush();
        fin->close();
    } catch(stdext::exception& e) {
        g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
    }
#endif
}
Esempio n. 4
0
void Map::saveOtbm(const std::string &fileName)
{
    FileStreamPtr fin = g_resources.createFile(fileName);
    if(!fin)
        stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));

    std::string dir;
    if(fileName.find_last_of('/') == std::string::npos)
        dir = g_resources.getWorkDir();
    else
        dir = fileName.substr(0, fileName.find_last_of('/'));

    uint32 version = 0;
    /// Support old versions (< 810 or 860 IIRC)
    /// TODO: Use constants?
    if(g_things.getOtbMajorVersion() < 10)
        version = 1;
    else
        version = 2;

    /// Usually when a map has empty house/spawn file it means the map is new.
    /// TODO: Ask the user for a map name instead of those ugly uses of substr
    std::string::size_type sep_pos;
    std::string houseFile = getHouseFile();
    std::string spawnFile = getSpawnFile();
    std::string cpyf;

    if((sep_pos = fileName.rfind('.')) != std::string::npos && stdext::ends_with(fileName, ".otbm"))
        cpyf = fileName.substr(0, sep_pos);

    if(houseFile.empty())
        houseFile = cpyf + "-houses.xml";

    if(spawnFile.empty())
        spawnFile = cpyf + "-spawns.xml";

    /// we only need the filename to save to, the directory should be resolved by the OTBM loader not here
    if((sep_pos = spawnFile.rfind('/')) != std::string::npos)
        spawnFile = spawnFile.substr(sep_pos + 1);

    if((sep_pos = houseFile.rfind('/')) != std::string::npos)
        houseFile = houseFile.substr(sep_pos + 1);
#if 0
    if(version > 1)
        m_houses->save(dir + "/" + houseFile);

    saveSpawns(dir + "/" + spawnFile);
#endif

    fin->addU32(0); // file version
    BinaryWriteTreePtr root(new BinaryWriteTree(fin));
    root->startNode(0);
    {
        root->writeU32(version);

        Size mapSize = getSize();
        root->writeU16(mapSize.width());
        root->writeU16(mapSize.height());

        root->writeU32(g_things.getOtbMajorVersion());
        root->writeU32(g_things.getOtbMinorVersion());

        root->startNode(OTBM_MAP_DATA);
        {
            // own description.
            for(const auto& desc : getDescriptions()) {
                root->writeU8(OTBM_ATTR_DESCRIPTION);
                root->writeString(desc);
            }

            // special one
            root->writeU8(OTBM_ATTR_DESCRIPTION);
            root->writeString(stdext::format("Saved with %s v%s", g_app.getName(), g_app.getVersion()));

            // spawn file.
            root->writeU8(OTBM_ATTR_SPAWN_FILE);
            root->writeString(spawnFile);

            // house file.
            if(version > 1) {
                root->writeU8(OTBM_ATTR_HOUSE_FILE);
                root->writeString(houseFile);
            }

            Position base(-1, -1, -1);
            bool firstNode = true;

            for(uint8_t z = 0; z < Otc::MAX_Z + 1; ++z) {
                for(const auto& it : m_tileBlocks[z]) {
                    const TileBlock& block = it.second;
                    for(const TilePtr& tile : block.getTiles()) {
                        if(!tile)
                            continue;

                        const Position& pos = tile->getPosition();
                        if(!pos.isValid())
                            continue;

                        if(pos.x < base.x || pos.x >= base.x + 256 || pos.y < base.y|| pos.y >= base.y + 256 ||
                                pos.z != base.z) {
                            if(!firstNode)
                                root->endNode(); /// OTBM_TILE_AREA

                            root->startNode(OTBM_TILE_AREA);
                            firstNode = false;
                            root->writePos(base = pos & 0xFF00);
                        }

                        uint32 flags = tile->getFlags();

                        root->startNode((flags & TILESTATE_HOUSE) == TILESTATE_HOUSE ? OTBM_HOUSETILE : OTBM_TILE);
                        root->writePoint(Point(pos.x, pos.y) & 0xFF);
//                      if(tileNode->getType() == OTBM_HOUSETILE)
//                          tileNode->writeU32(tile->getHouseId());

                        if(flags) {
                            root->writeU8(OTBM_ATTR_TILE_FLAGS);
                            root->writeU32(flags);
                        }

                        for(const ItemPtr& item : tile->getItems()) {
                            if(item->isGround()) {
                                root->writeU8(OTBM_ATTR_ITEM);
                                root->writeU16(item->getId());
                                continue;
                            }

                            item->serializeItem(root); 
                        }

                        root->endNode(); // OTBM_TILE
                    }
                }
            }

            if(!firstNode)
                root->endNode();  // OTBM_TILE_AREA

            root->startNode(OTBM_TOWNS);
            for(const TownPtr& town : m_towns.getTowns()) {
                root->writeU32(town->getId());
                root->writeString(town->getName());
                root->writePos(town->getPos());
            }
            root->endNode();

            if(version > 1) {
                root->startNode(OTBM_WAYPOINTS);
                for(const auto& it : m_waypoints) {
                    root->writeString(it.second);
                    root->writePos(it.first);
                }
                root->endNode();
            }
        }
        root->endNode(); // OTBM_MAP_DATA
    }
    root->endNode(); // 0 (root)
}
Esempio n. 5
0
void Map::saveOtcm(const std::string& fileName)
{
    try {
        stdext::timer saveTimer;

        FileStreamPtr fin = g_resources.createFile(fileName);
        fin->cache();

        //TODO: compression flag with zlib
        uint32 flags = 0;

        // header
        fin->addU32(OTCM_SIGNATURE);
        fin->addU16(0); // data start, will be overwritten later
        fin->addU16(OTCM_VERSION);
        fin->addU32(flags);

        // version 1 header
        fin->addString("OTCM 1.0"); // map description
        fin->addU32(g_things.getDatSignature());
        fin->addU16(g_game.getProtocolVersion());
        fin->addString(g_game.getWorldName());

        // go back and rewrite where the map data starts
        uint32 start = fin->tell();
        fin->seek(4);
        fin->addU16(start);
        fin->seek(start);

        for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) {
            for(const auto& it : m_tileBlocks[z]) {
                const TileBlock& block = it.second;
                for(const TilePtr& tile : block.getTiles()) {
                    if(!tile || tile->isEmpty())
                        continue;

                    Position pos = tile->getPosition();
                    fin->addU16(pos.x);
                    fin->addU16(pos.y);
                    fin->addU8(pos.z);

                    for(const ThingPtr& thing : tile->getThings()) {
                        if(thing->isItem()) {
                            ItemPtr item = thing->static_self_cast<Item>();
                            fin->addU16(item->getId());
                            fin->addU8(item->getCountOrSubType());
                        }
                    }

                    // end of tile
                    fin->addU16(0xFFFF);
                }
            }
        }

        // end of file
        Position invalidPos;
        fin->addU16(invalidPos.x);
        fin->addU16(invalidPos.y);
        fin->addU8(invalidPos.z);

        fin->flush();

        fin->close();
    } catch(stdext::exception& e) {
        g_logger.error(stdext::format("failed to save OTCM map: %s", e.what()));
    }
}
Esempio n. 6
0
void Map::saveOtbm(const std::string& fileName)
{
    try {
        FileStreamPtr fin = g_resources.createFile(fileName);
        if(!fin)
            stdext::throw_exception(stdext::format("failed to open file '%s' for write", fileName));

        fin->cache();
        std::string dir;
        if(fileName.find_last_of('/') == std::string::npos)
            dir = g_resources.getWorkDir();
        else
            dir = fileName.substr(0, fileName.find_last_of('/'));

        uint32 version = 0;
        if(g_things.getOtbMajorVersion() < ClientVersion820)
            version = 1;
        else
            version = 2;

        /// Usually when a map has empty house/spawn file it means the map is new.
        /// TODO: Ask the user for a map name instead of those ugly uses of substr
        std::string::size_type sep_pos;
        std::string houseFile = getHouseFile();
        std::string spawnFile = getSpawnFile();
        std::string cpyf;

        if((sep_pos = fileName.rfind('.')) != std::string::npos && stdext::ends_with(fileName, ".otbm"))
            cpyf = fileName.substr(0, sep_pos);

        if(houseFile.empty())
            houseFile = cpyf + "-houses.xml";

        if(spawnFile.empty())
            spawnFile = cpyf + "-spawns.xml";

        /// we only need the filename to save to, the directory should be resolved by the OTBM loader not here
        if((sep_pos = spawnFile.rfind('/')) != std::string::npos)
            spawnFile = spawnFile.substr(sep_pos + 1);

        if((sep_pos = houseFile.rfind('/')) != std::string::npos)
            houseFile = houseFile.substr(sep_pos + 1);

        fin->addU32(0); // file version
        OutputBinaryTreePtr root(new OutputBinaryTree(fin));
        {
            root->addU32(version);

            Size mapSize = getSize();
            root->addU16(mapSize.width());
            root->addU16(mapSize.height());

            root->addU32(g_things.getOtbMajorVersion());
            root->addU32(g_things.getOtbMinorVersion());

            root->startNode(OTBM_MAP_DATA);
            {
                root->addU8(OTBM_ATTR_DESCRIPTION);
                root->addString(m_attribs.get<std::string>(OTBM_ATTR_DESCRIPTION));

                root->addU8(OTBM_ATTR_SPAWN_FILE);
                root->addString(spawnFile);

                root->addU8(OTBM_ATTR_HOUSE_FILE);
                root->addString(houseFile);

                int px = -1, py = -1, pz =-1;
                bool firstNode = true;

                for(uint8_t z = 0; z <= Otc::MAX_Z; ++z) {
                    for(const auto& it : m_tileBlocks[z]) {
                        const TileBlock& block = it.second;
                        for(const TilePtr& tile : block.getTiles()) {
                            if(unlikely(!tile || tile->isEmpty()))
                                continue;

                            const Position& pos = tile->getPosition();
                            if(unlikely(!pos.isValid()))
                                continue;

                            if(pos.x < px || pos.x >= px + 256
                                    || pos.y < py || pos.y >= py + 256
                                    || pos.z != pz) {
                                if(!firstNode)
                                    root->endNode(); /// OTBM_TILE_AREA

                                firstNode = false;
                                root->startNode(OTBM_TILE_AREA);

                                px = pos.x & 0xFF00;
                                py = pos.y & 0xFF00;
                                pz = pos.z;
                                root->addPos(px, py, pz);
                            }

                            root->startNode(tile->isHouseTile() ? OTBM_HOUSETILE : OTBM_TILE);
                            root->addPoint(Point(pos.x, pos.y) & 0xFF);
                            if(tile->isHouseTile())
                                root->addU32(tile->getHouseId());

                            if(tile->getFlags()) {
                                root->addU8(OTBM_ATTR_TILE_FLAGS);
                                root->addU32(tile->getFlags());
                            }

                            const auto& itemList = tile->getItems();
                            const ItemPtr& ground = tile->getGround();
                            if(ground) {
                                // Those types are called "complex" needs other stuff to be written.
                                // For containers, there is container items, for depot, depot it and so on.
                                if(!ground->isContainer() && !ground->isDepot()
                                        && !ground->isDoor() && !ground->isTeleport()) {
                                    root->addU8(OTBM_ATTR_ITEM);
                                    root->addU16(ground->getServerId());
                                } else
                                    ground->serializeItem(root);
                            }
                            for(const ItemPtr& item : itemList)
                                if(!item->isGround())
                                    item->serializeItem(root);

                            root->endNode(); // OTBM_TILE
                        }
                    }
                }

                if(!firstNode)
                    root->endNode();  // OTBM_TILE_AREA

                root->startNode(OTBM_TOWNS);
                for(const TownPtr& town : g_towns.getTowns()) {
                    root->startNode(OTBM_TOWN);

                    root->addU32(town->getId());
                    root->addString(town->getName());

                    Position townPos = town->getPos();
                    root->addPos(townPos.x, townPos.y, townPos.z);
                    root->endNode();
                }
                root->endNode();

                if(version > 1) {
                    root->startNode(OTBM_WAYPOINTS);
                    for(const auto& it : m_waypoints) {
                        root->startNode(OTBM_WAYPOINT);
                        root->addString(it.second);

                        Position pos = it.first;
                        root->addPos(pos.x, pos.y, pos.z);
                        root->endNode();
                    }
                    root->endNode();
                }
            }
            root->endNode(); // OTBM_MAP_DATA
        }
        root->endNode();

        fin->flush();
        fin->close();
    } catch(std::exception& e) {
        g_logger.error(stdext::format("Failed to save '%s': %s", fileName, e.what()));
    }
}
Esempio n. 7
0
void ThingType::serialize(const FileStreamPtr& fin)
{
    for(int i = 0; i < ThingLastAttr; ++i) {
        if(!hasAttr((ThingAttr)i))
            continue;

        int attr = i;
        if(g_game.getClientVersion() >= 780) {
            if(attr == ThingAttrChargeable)
                attr = ThingAttrWritable;
            else if(attr >= ThingAttrWritable)
                attr += 1;
        } else if(g_game.getClientVersion() >= 1000) {
            if(attr == ThingAttrNoMoveAnimation)
                attr = 16;
            else if(attr >= ThingAttrPickupable)
                attr += 1;
        }

        fin->addU8(attr);
        switch(attr) {
            case ThingAttrDisplacement: {
                fin->addU16(m_displacement.x);
                fin->addU16(m_displacement.y);
                break;
            }
            case ThingAttrLight: {
                Light light = m_attribs.get<Light>(attr);
                fin->addU16(light.intensity);
                fin->addU16(light.color);
                break;
            }
            case ThingAttrMarket: {
                MarketData market = m_attribs.get<MarketData>(attr);
                fin->addU16(market.category);
                fin->addU16(market.tradeAs);
                fin->addU16(market.showAs);
                fin->addString(market.name);
                fin->addU16(market.restrictVocation);
                fin->addU16(market.requiredLevel);
                break;
            }
            case ThingAttrUsable:
            case ThingAttrElevation:
            case ThingAttrGround:
            case ThingAttrWritable:
            case ThingAttrWritableOnce:
            case ThingAttrMinimapColor:
            case ThingAttrCloth:
            case ThingAttrLensHelp:
                fin->addU16(m_attribs.get<uint16>(attr));
                break;
            default:
                break;
        };
    }
    fin->addU8(ThingLastAttr);

    fin->addU8(m_size.width());
    fin->addU8(m_size.height());

    if(m_size.width() > 1 || m_size.height() > 1)
        fin->addU8(m_realSize);

    fin->addU8(m_layers);
    fin->addU8(m_numPatternX);
    fin->addU8(m_numPatternY);
    fin->addU8(m_numPatternZ);
    fin->addU8(m_animationPhases);

    if(g_game.getFeature(Otc::GameEnhancedAnimations)) {
        if(m_animationPhases > 1 && m_animator != nullptr)  {
            m_animator->serialize(fin);
        }
    }

    for(uint i = 0; i < m_spritesIndex.size(); i++) {
        if(g_game.getFeature(Otc::GameSpritesU32))
            fin->addU32(m_spritesIndex[i]);
        else
            fin->addU16(m_spritesIndex[i]);
    }
}