From e5df87260087e89a1ca568d4c9b4ae46f89a7791 Mon Sep 17 00:00:00 2001 From: niczkx Date: Sun, 26 Aug 2012 18:44:46 +0000 Subject: [PATCH] Move load creatures functions from map and bind them to singleton class g_creatures --- src/otclient/creatures.cpp | 19 +++++++---- src/otclient/creatures.h | 4 +++ src/otclient/itemtype.h | 46 +++++++++++++++++++++++++ src/otclient/luafunctions.cpp | 13 +++---- src/otclient/map.h | 56 ++----------------------------- src/otclient/mapio.cpp | 22 ++++++------ src/otclient/thingtypemanager.cpp | 2 +- 7 files changed, 84 insertions(+), 78 deletions(-) diff --git a/src/otclient/creatures.cpp b/src/otclient/creatures.cpp index eb888865..a0a38df7 100644 --- a/src/otclient/creatures.cpp +++ b/src/otclient/creatures.cpp @@ -21,6 +21,7 @@ */ #include "creatures.h" +#include "creature.h" #include #include @@ -28,6 +29,14 @@ Creatures g_creatures; +CreaturePtr CreatureType::cast() +{ + CreaturePtr ret(new Creature); + ret->setName(getName()); + ret->setOutfit(getOutfit()); + return ret; +} + void Creatures::loadMonsters(const std::string& file) { TiXmlDocument doc; @@ -108,13 +117,11 @@ bool Creatures::m_loadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr Outfit out; out.setCategory(ThingCategoryCreature); - int32 type; if(!attrib->Attribute("type").empty()) - type = attrib->readType("type"); + out.setId(attrib->readType("type")); else - type = attrib->readType("typeex"); + out.setAuxId(attrib->readType("typeex")); - out.setId(type); { out.setHead(attrib->readType(("head"))); out.setBody(attrib->readType(("body"))); @@ -126,7 +133,7 @@ bool Creatures::m_loadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr m->setOutfit(out); m_creatures.push_back(m); - return type >= 0; + return true; } CreatureTypePtr Creatures::getCreatureByName(std::string name) @@ -143,7 +150,7 @@ CreatureTypePtr Creatures::getCreatureByLook(int look) auto findFun = [=] (const CreatureTypePtr& c) -> bool { const Outfit& o = c->getOutfit(); - return o.getId() == look; + return o.getId() == look || o.getAuxId() == look; }; auto it = std::find_if(m_creatures.begin(), m_creatures.end(), findFun); return it != m_creatures.end() ? *it : nullptr; diff --git a/src/otclient/creatures.h b/src/otclient/creatures.h index 35dc72f3..68725f1e 100644 --- a/src/otclient/creatures.h +++ b/src/otclient/creatures.h @@ -50,6 +50,8 @@ public: void setOutfit(const Outfit& o) { m_attribs.set(CreatureAttrOutfit, o); } Outfit getOutfit() { return m_attribs.get(CreatureAttrOutfit); } + CreaturePtr cast(); + private: stdext::dynamic_storage m_attribs; }; @@ -69,6 +71,8 @@ public: bool isLoaded() const { return m_loaded; } + const std::vector& getCreatures() { return m_creatures; } + protected: bool m_loadCreatureBuffer(TiXmlElement* elem, const CreatureTypePtr& m); diff --git a/src/otclient/itemtype.h b/src/otclient/itemtype.h index dda270a0..8ed94b1a 100644 --- a/src/otclient/itemtype.h +++ b/src/otclient/itemtype.h @@ -79,6 +79,52 @@ enum ItemTypeAttr : uint8 { ItemTypeAttrLast = 45 }; +enum ClientVersion +{ + ClientVersion750 = 1, + ClientVersion755 = 2, + ClientVersion760 = 3, + ClientVersion770 = 3, + ClientVersion780 = 4, + ClientVersion790 = 5, + ClientVersion792 = 6, + ClientVersion800 = 7, + ClientVersion810 = 8, + ClientVersion811 = 9, + ClientVersion820 = 10, + ClientVersion830 = 11, + ClientVersion840 = 12, + ClientVersion841 = 13, + ClientVersion842 = 14, + ClientVersion850 = 15, + ClientVersion854_OLD = 16, + ClientVersion854 = 17, + ClientVersion855 = 18, + ClientVersion860_OLD = 19, + ClientVersion860 = 20, + ClientVersion861 = 21, + ClientVersion862 = 22, + ClientVersion870 = 23, + ClientVersion871 = 24, + ClientVersion872 = 25, + ClientVersion873 = 26, + ClientVersion900 = 27, + ClientVersion910 = 28, + ClientVersion920 = 29, + ClientVersion940 = 30, + ClientVersion944_V1 = 31, + ClientVersion944_V2 = 32, + ClientVersion944_V3 = 33, + ClientVersion944_V4 = 34, + ClientVersion946 = 35, + ClientVersion950 = 36, + ClientVersion952 = 37, + ClientVersion953 = 38, + ClientVersion954 = 39, + ClientVersion960 = 40, + ClientVersion961 = 41 +}; + class ItemType : public LuaObject { public: diff --git a/src/otclient/luafunctions.cpp b/src/otclient/luafunctions.cpp index 4a42ca36..626f14e9 100644 --- a/src/otclient/luafunctions.cpp +++ b/src/otclient/luafunctions.cpp @@ -105,19 +105,19 @@ void OTClient::registerLuaFunctions() g_lua.bindSingletonFunction("g_map", "findPath", &Map::findPath, &g_map); g_lua.bindSingletonFunction("g_map", "loadOtbm", &Map::loadOtbm, &g_map); g_lua.bindSingletonFunction("g_map", "saveOtbm", &Map::saveOtbm, &g_map); + g_lua.bindSingletonFunction("g_map", "loadSpawns", &Map::loadSpawns, &g_map); g_lua.bindSingletonFunction("g_map", "loadOtcm", &Map::loadOtcm, &g_map); g_lua.bindSingletonFunction("g_map", "saveOtcm", &Map::saveOtcm, &g_map); - g_lua.bindSingletonFunction("g_map", "loadMonsters", &Map::loadMonsters, &g_map); - g_lua.bindSingletonFunction("g_map", "loadNpcs", &Map::loadNpcs, &g_map); - g_lua.bindSingletonFunction("g_map", "loadSingleCreature", &Map::loadSingleCreature, &g_map); g_lua.bindSingletonFunction("g_map", "getTown", &Map::getTown, &g_map); g_lua.bindSingletonFunction("g_map", "getHouse", &Map::getHouse, &g_map); - g_lua.bindSingletonFunction("g_map", "getCreature", &Map::getCreature, &g_map); - /// \todo move creatures from Map to here g_lua.registerSingletonClass("g_creatures"); - g_lua.bindSingletonFunction("g_creatures", "getCreature", &Creatures::getCreatureByName, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "getCreatures", &Creatures::getCreatures, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "getCreatureByName", &Creatures::getCreatureByName, &g_creatures); g_lua.bindSingletonFunction("g_creatures", "getCreatureByLook", &Creatures::getCreatureByLook, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "loadMonsters", &Creatures::loadMonsters, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "loadNpcs", &Creatures::loadNpcs, &g_creatures); + g_lua.bindSingletonFunction("g_creatures", "loadSingleCreature", &Creatures::loadSingleCreature, &g_creatures); g_lua.registerSingletonClass("g_game"); g_lua.bindSingletonFunction("g_game", "loginWorld", &Game::loginWorld, &g_game); @@ -318,6 +318,7 @@ void OTClient::registerLuaFunctions() g_lua.bindClassMemberFunction("getName", &CreatureType::getName); g_lua.bindClassMemberFunction("getOutfit", &CreatureType::getOutfit); g_lua.bindClassMemberFunction("getSpawnTime", &CreatureType::getSpawnTime); + g_lua.bindClassMemberFunction("cast", &CreatureType::cast); g_lua.registerClass(); g_lua.bindClassStaticFunction("create", []{ return CreaturePtr(new Creature); }); diff --git a/src/otclient/map.h b/src/otclient/map.h index 99367de6..99f18246 100644 --- a/src/otclient/map.h +++ b/src/otclient/map.h @@ -84,59 +84,11 @@ enum OTBM_NodeTypes_t OTBM_WAYPOINT = 16 }; -enum clientVersion_t -{ - CLIENT_VERSION_750 = 1, - CLIENT_VERSION_755 = 2, - CLIENT_VERSION_760 = 3, - CLIENT_VERSION_770 = 3, - CLIENT_VERSION_780 = 4, - CLIENT_VERSION_790 = 5, - CLIENT_VERSION_792 = 6, - CLIENT_VERSION_800 = 7, - CLIENT_VERSION_810 = 8, - CLIENT_VERSION_811 = 9, - CLIENT_VERSION_820 = 10, - CLIENT_VERSION_830 = 11, - CLIENT_VERSION_840 = 12, - CLIENT_VERSION_841 = 13, - CLIENT_VERSION_842 = 14, - CLIENT_VERSION_850 = 15, - CLIENT_VERSION_854_OLD = 16, - CLIENT_VERSION_854 = 17, - CLIENT_VERSION_855 = 18, - CLIENT_VERSION_860_OLD = 19, - CLIENT_VERSION_860 = 20, - CLIENT_VERSION_861 = 21, - CLIENT_VERSION_862 = 22, - CLIENT_VERSION_870 = 23, - CLIENT_VERSION_871 = 24, - CLIENT_VERSION_872 = 25, - CLIENT_VERSION_873 = 26, - CLIENT_VERSION_900 = 27, - CLIENT_VERSION_910 = 28, - CLIENT_VERSION_920 = 29, - CLIENT_VERSION_940 = 30, - CLIENT_VERSION_944_V1 = 31, - CLIENT_VERSION_944_V2 = 32, - CLIENT_VERSION_944_V3 = 33, - CLIENT_VERSION_944_V4 = 34, - CLIENT_VERSION_946 = 35, - CLIENT_VERSION_950 = 36, - CLIENT_VERSION_952 = 37, - CLIENT_VERSION_953 = 38, - CLIENT_VERSION_954 = 39, - CLIENT_VERSION_960 = 40, - CLIENT_VERSION_961 = 41 -}; - enum { OTCM_SIGNATURE = 0x4D43544F, OTCM_VERSION = 1 }; - - enum { BLOCK_SIZE = 32 }; @@ -183,8 +135,8 @@ public: bool loadOtcm(const std::string& fileName); void saveOtcm(const std::string& fileName); - void loadOtbm(const std::string& fileName); - void saveOtbm(const std::string& fileName); + void loadOtbm(const std::string& fileName, const UIWidgetPtr& pbar = 0); + void saveOtbm(const std::string& fileName, const UIWidgetPtr& pbar = 0); void loadSpawns(const std::string& fileName); void saveSpawns(const std::string&); @@ -201,10 +153,6 @@ public: Size getSize() { return Size(m_attribs.get(OTBM_ATTR_WIDTH), m_attribs.get(OTBM_ATTR_HEIGHT)); } std::vector getDescriptions() { return stdext::split(m_attribs.get(OTBM_ATTR_DESCRIPTION), "\n"); } - void loadMonsters(const std::string& fileName) { g_creatures.loadMonsters(fileName); } - void loadSingleCreature(const std::string& file) { g_creatures.loadSingleCreature(file); } - void loadNpcs(const std::string& folder) { g_creatures.loadNpcs(folder); } - void clean(); void cleanDynamicThings(); void cleanTexts(); diff --git a/src/otclient/mapio.cpp b/src/otclient/mapio.cpp index 48ab8937..03e457ff 100644 --- a/src/otclient/mapio.cpp +++ b/src/otclient/mapio.cpp @@ -30,8 +30,9 @@ #include #include #include +#include -void Map::loadOtbm(const std::string& fileName) +void Map::loadOtbm(const std::string& fileName, const UIWidgetPtr& pbar) { FileStreamPtr fin = g_resources.openFile(fileName); if(!fin) @@ -42,7 +43,7 @@ void Map::loadOtbm(const std::string& fileName) stdext::throw_exception("OTB isn't loaded yet to load a map."); if(fin->getU32()) - stdext::throw_exception("Unknown file version detected"); + stdext::throw_exception("Unknown file version detected"); BinaryTreePtr root = fin->getBinaryTree(); if(root->getU8()) @@ -94,6 +95,7 @@ void Map::loadOtbm(const std::string& fileName) uint8 mapDataType = nodeMapData->getU8(); if(mapDataType == OTBM_TILE_AREA) { Position basePos = nodeMapData->getPosition(); + unsigned int pbarvalue=0; for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) { uint8 type = nodeTile->getU8(); @@ -175,6 +177,7 @@ void Map::loadOtbm(const std::string& fileName) if(house) tile->setHouseId(house->getId()); tile->setFlags((tileflags_t)flags); + //if(!(++pbarvalue % 8192) && pbar); } } } else if(mapDataType == OTBM_TOWNS) { @@ -207,12 +210,9 @@ void Map::loadOtbm(const std::string& fileName) g_logger.debug("OTBM read successfully."); fin->close(); - - //loadSpawns(getSpawnFile()); -// m_houses.load(getHouseFile()); } -void Map::saveOtbm(const std::string &fileName) +void Map::saveOtbm(const std::string &fileName, const UIWidgetPtr&/* pbar*/) { FileStreamPtr fin = g_resources.createFile(fileName); if(!fin) @@ -226,9 +226,7 @@ void Map::saveOtbm(const std::string &fileName) 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) + if(g_things.getOtbMajorVersion() < ClientVersion820) version = 1; else version = 2; @@ -386,8 +384,10 @@ void Map::saveOtbm(const std::string &fileName) void Map::loadSpawns(const std::string &fileName) { - if(!g_creatures.isLoaded()) - stdext::throw_exception("cannot load spawns; monsters/nps aren't loaded."); + if(!g_creatures.isLoaded()) { + g_logger.error("cannot load spawns; monsters/nps aren't loaded."); + return; + } TiXmlDocument doc; doc.Parse(g_resources.loadFile(fileName).c_str()); diff --git a/src/otclient/thingtypemanager.cpp b/src/otclient/thingtypemanager.cpp index 6879e7d7..6965bcfe 100644 --- a/src/otclient/thingtypemanager.cpp +++ b/src/otclient/thingtypemanager.cpp @@ -287,7 +287,7 @@ CreaturePtr ThingTypeManager::castThingToCreature(const ThingTypePtr& thing) // a creature can have a look item with whether client id or even server id const ItemTypePtr& item = findItemTypeByClientId(clientId); if(item && !(cType = g_creatures.getCreatureByLook(item->getServerId()))) - stdext::throw_exception(stdext::format("failed to find creature with look type/item %hd", clientId)); + stdext::throw_exception(stdext::format("failed to find creature with look type/item %hd", item->getServerId())); } ret->setName(cType->getName());