Move load creatures functions from map and bind them to singleton class g_creatures

This commit is contained in:
niczkx 2012-08-26 18:44:46 +00:00
parent 9b394785bb
commit e5df872600
7 changed files with 84 additions and 78 deletions

View File

@ -21,6 +21,7 @@
*/ */
#include "creatures.h" #include "creatures.h"
#include "creature.h"
#include <framework/xml/tinyxml.h> #include <framework/xml/tinyxml.h>
#include <framework/core/resourcemanager.h> #include <framework/core/resourcemanager.h>
@ -28,6 +29,14 @@
Creatures g_creatures; 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) void Creatures::loadMonsters(const std::string& file)
{ {
TiXmlDocument doc; TiXmlDocument doc;
@ -108,13 +117,11 @@ bool Creatures::m_loadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr
Outfit out; Outfit out;
out.setCategory(ThingCategoryCreature); out.setCategory(ThingCategoryCreature);
int32 type;
if(!attrib->Attribute("type").empty()) if(!attrib->Attribute("type").empty())
type = attrib->readType<int32>("type"); out.setId(attrib->readType<int32>("type"));
else else
type = attrib->readType<int32>("typeex"); out.setAuxId(attrib->readType<int32>("typeex"));
out.setId(type);
{ {
out.setHead(attrib->readType<int>(("head"))); out.setHead(attrib->readType<int>(("head")));
out.setBody(attrib->readType<int>(("body"))); out.setBody(attrib->readType<int>(("body")));
@ -126,7 +133,7 @@ bool Creatures::m_loadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr
m->setOutfit(out); m->setOutfit(out);
m_creatures.push_back(m); m_creatures.push_back(m);
return type >= 0; return true;
} }
CreatureTypePtr Creatures::getCreatureByName(std::string name) CreatureTypePtr Creatures::getCreatureByName(std::string name)
@ -143,7 +150,7 @@ CreatureTypePtr Creatures::getCreatureByLook(int look)
auto findFun = [=] (const CreatureTypePtr& c) -> bool auto findFun = [=] (const CreatureTypePtr& c) -> bool
{ {
const Outfit& o = c->getOutfit(); 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); auto it = std::find_if(m_creatures.begin(), m_creatures.end(), findFun);
return it != m_creatures.end() ? *it : nullptr; return it != m_creatures.end() ? *it : nullptr;

View File

@ -50,6 +50,8 @@ public:
void setOutfit(const Outfit& o) { m_attribs.set(CreatureAttrOutfit, o); } void setOutfit(const Outfit& o) { m_attribs.set(CreatureAttrOutfit, o); }
Outfit getOutfit() { return m_attribs.get<Outfit>(CreatureAttrOutfit); } Outfit getOutfit() { return m_attribs.get<Outfit>(CreatureAttrOutfit); }
CreaturePtr cast();
private: private:
stdext::dynamic_storage<uint8> m_attribs; stdext::dynamic_storage<uint8> m_attribs;
}; };
@ -69,6 +71,8 @@ public:
bool isLoaded() const { return m_loaded; } bool isLoaded() const { return m_loaded; }
const std::vector<CreatureTypePtr>& getCreatures() { return m_creatures; }
protected: protected:
bool m_loadCreatureBuffer(TiXmlElement* elem, const CreatureTypePtr& m); bool m_loadCreatureBuffer(TiXmlElement* elem, const CreatureTypePtr& m);

View File

@ -79,6 +79,52 @@ enum ItemTypeAttr : uint8 {
ItemTypeAttrLast = 45 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 class ItemType : public LuaObject
{ {
public: public:

View File

@ -105,19 +105,19 @@ void OTClient::registerLuaFunctions()
g_lua.bindSingletonFunction("g_map", "findPath", &Map::findPath, &g_map); 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", "loadOtbm", &Map::loadOtbm, &g_map);
g_lua.bindSingletonFunction("g_map", "saveOtbm", &Map::saveOtbm, &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", "loadOtcm", &Map::loadOtcm, &g_map);
g_lua.bindSingletonFunction("g_map", "saveOtcm", &Map::saveOtcm, &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", "getTown", &Map::getTown, &g_map);
g_lua.bindSingletonFunction("g_map", "getHouse", &Map::getHouse, &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.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", "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.registerSingletonClass("g_game");
g_lua.bindSingletonFunction("g_game", "loginWorld", &Game::loginWorld, &g_game); g_lua.bindSingletonFunction("g_game", "loginWorld", &Game::loginWorld, &g_game);
@ -318,6 +318,7 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassMemberFunction<CreatureType>("getName", &CreatureType::getName); g_lua.bindClassMemberFunction<CreatureType>("getName", &CreatureType::getName);
g_lua.bindClassMemberFunction<CreatureType>("getOutfit", &CreatureType::getOutfit); g_lua.bindClassMemberFunction<CreatureType>("getOutfit", &CreatureType::getOutfit);
g_lua.bindClassMemberFunction<CreatureType>("getSpawnTime", &CreatureType::getSpawnTime); g_lua.bindClassMemberFunction<CreatureType>("getSpawnTime", &CreatureType::getSpawnTime);
g_lua.bindClassMemberFunction<CreatureType>("cast", &CreatureType::cast);
g_lua.registerClass<Creature, Thing>(); g_lua.registerClass<Creature, Thing>();
g_lua.bindClassStaticFunction<Creature>("create", []{ return CreaturePtr(new Creature); }); g_lua.bindClassStaticFunction<Creature>("create", []{ return CreaturePtr(new Creature); });

View File

@ -84,59 +84,11 @@ enum OTBM_NodeTypes_t
OTBM_WAYPOINT = 16 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 { enum {
OTCM_SIGNATURE = 0x4D43544F, OTCM_SIGNATURE = 0x4D43544F,
OTCM_VERSION = 1 OTCM_VERSION = 1
}; };
enum { enum {
BLOCK_SIZE = 32 BLOCK_SIZE = 32
}; };
@ -183,8 +135,8 @@ public:
bool loadOtcm(const std::string& fileName); bool loadOtcm(const std::string& fileName);
void saveOtcm(const std::string& fileName); void saveOtcm(const std::string& fileName);
void loadOtbm(const std::string& fileName); void loadOtbm(const std::string& fileName, const UIWidgetPtr& pbar = 0);
void saveOtbm(const std::string& fileName); void saveOtbm(const std::string& fileName, const UIWidgetPtr& pbar = 0);
void loadSpawns(const std::string& fileName); void loadSpawns(const std::string& fileName);
void saveSpawns(const std::string&); void saveSpawns(const std::string&);
@ -201,10 +153,6 @@ public:
Size getSize() { return Size(m_attribs.get<uint16>(OTBM_ATTR_WIDTH), m_attribs.get<uint16>(OTBM_ATTR_HEIGHT)); } Size getSize() { return Size(m_attribs.get<uint16>(OTBM_ATTR_WIDTH), m_attribs.get<uint16>(OTBM_ATTR_HEIGHT)); }
std::vector<std::string> getDescriptions() { return stdext::split(m_attribs.get<std::string>(OTBM_ATTR_DESCRIPTION), "\n"); } std::vector<std::string> getDescriptions() { return stdext::split(m_attribs.get<std::string>(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 clean();
void cleanDynamicThings(); void cleanDynamicThings();
void cleanTexts(); void cleanTexts();

View File

@ -30,8 +30,9 @@
#include <framework/core/filestream.h> #include <framework/core/filestream.h>
#include <framework/core/binarytree.h> #include <framework/core/binarytree.h>
#include <framework/xml/tinyxml.h> #include <framework/xml/tinyxml.h>
#include <framework/ui/uiwidget.h>
void Map::loadOtbm(const std::string& fileName) void Map::loadOtbm(const std::string& fileName, const UIWidgetPtr& pbar)
{ {
FileStreamPtr fin = g_resources.openFile(fileName); FileStreamPtr fin = g_resources.openFile(fileName);
if(!fin) 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."); stdext::throw_exception("OTB isn't loaded yet to load a map.");
if(fin->getU32()) if(fin->getU32())
stdext::throw_exception("Unknown file version detected"); stdext::throw_exception("Unknown file version detected");
BinaryTreePtr root = fin->getBinaryTree(); BinaryTreePtr root = fin->getBinaryTree();
if(root->getU8()) if(root->getU8())
@ -94,6 +95,7 @@ void Map::loadOtbm(const std::string& fileName)
uint8 mapDataType = nodeMapData->getU8(); uint8 mapDataType = nodeMapData->getU8();
if(mapDataType == OTBM_TILE_AREA) { if(mapDataType == OTBM_TILE_AREA) {
Position basePos = nodeMapData->getPosition(); Position basePos = nodeMapData->getPosition();
unsigned int pbarvalue=0;
for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) { for(const BinaryTreePtr &nodeTile : nodeMapData->getChildren()) {
uint8 type = nodeTile->getU8(); uint8 type = nodeTile->getU8();
@ -175,6 +177,7 @@ void Map::loadOtbm(const std::string& fileName)
if(house) if(house)
tile->setHouseId(house->getId()); tile->setHouseId(house->getId());
tile->setFlags((tileflags_t)flags); tile->setFlags((tileflags_t)flags);
//if(!(++pbarvalue % 8192) && pbar);
} }
} }
} else if(mapDataType == OTBM_TOWNS) { } else if(mapDataType == OTBM_TOWNS) {
@ -207,12 +210,9 @@ void Map::loadOtbm(const std::string& fileName)
g_logger.debug("OTBM read successfully."); g_logger.debug("OTBM read successfully.");
fin->close(); 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); FileStreamPtr fin = g_resources.createFile(fileName);
if(!fin) if(!fin)
@ -226,9 +226,7 @@ void Map::saveOtbm(const std::string &fileName)
dir = fileName.substr(0, fileName.find_last_of('/')); dir = fileName.substr(0, fileName.find_last_of('/'));
uint32 version = 0; uint32 version = 0;
/// Support old versions (< 810 or 860 IIRC) if(g_things.getOtbMajorVersion() < ClientVersion820)
/// TODO: Use constants?
if(g_things.getOtbMajorVersion() < 10)
version = 1; version = 1;
else else
version = 2; version = 2;
@ -386,8 +384,10 @@ void Map::saveOtbm(const std::string &fileName)
void Map::loadSpawns(const std::string &fileName) void Map::loadSpawns(const std::string &fileName)
{ {
if(!g_creatures.isLoaded()) if(!g_creatures.isLoaded()) {
stdext::throw_exception("cannot load spawns; monsters/nps aren't loaded."); g_logger.error("cannot load spawns; monsters/nps aren't loaded.");
return;
}
TiXmlDocument doc; TiXmlDocument doc;
doc.Parse(g_resources.loadFile(fileName).c_str()); doc.Parse(g_resources.loadFile(fileName).c_str());

View File

@ -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 // a creature can have a look item with whether client id or even server id
const ItemTypePtr& item = findItemTypeByClientId(clientId); const ItemTypePtr& item = findItemTypeByClientId(clientId);
if(item && !(cType = g_creatures.getCreatureByLook(item->getServerId()))) 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()); ret->setName(cType->getName());