tibia-client/src/otclient/thingtypemanager.cpp

317 lines
10 KiB
C++
Raw Normal View History

2012-06-21 19:54:20 +02:00
/*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "thingtypemanager.h"
#include "spritemanager.h"
#include "thing.h"
2012-07-19 23:22:52 +02:00
#include "thingtype.h"
#include "itemtype.h"
2012-08-19 17:41:03 +02:00
#include "creature.h"
#include "creatures.h"
2012-06-21 19:54:20 +02:00
#include <framework/core/resourcemanager.h>
#include <framework/core/filestream.h>
2012-06-24 15:05:44 +02:00
#include <framework/core/binarytree.h>
#include <framework/xml/tinyxml.h>
2012-06-21 19:54:20 +02:00
ThingTypeManager g_things;
void ThingTypeManager::init()
{
2012-07-19 23:22:52 +02:00
m_nullThingType = ThingTypePtr(new ThingType);
m_nullItemType = ItemTypePtr(new ItemType);
2012-06-21 19:54:20 +02:00
m_datSignature = 0;
m_otbMinorVersion = 0;
m_otbMajorVersion = 0;
m_datLoaded = false;
m_xmlLoaded = false;
m_otbLoaded = false;
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].resize(1, m_nullThingType);
m_itemTypes.resize(1, m_nullItemType);
2012-06-21 19:54:20 +02:00
}
void ThingTypeManager::terminate()
{
for(int i = 0; i < ThingLastCategory; ++i)
m_thingTypes[i].clear();
m_itemTypes.clear();
2012-07-19 23:22:52 +02:00
m_nullThingType = nullptr;
m_nullItemType = nullptr;
2012-06-21 19:54:20 +02:00
}
bool ThingTypeManager::loadDat(const std::string& file)
{
2012-07-26 08:10:28 +02:00
m_datLoaded = false;
m_datSignature = 0;
2012-06-21 19:54:20 +02:00
try {
FileStreamPtr fin = g_resources.openFile(file);
m_datSignature = fin->getU32();
int numThings[ThingLastCategory];
for(int category = 0; category < ThingLastCategory; ++category) {
2012-06-21 19:54:20 +02:00
int count = fin->getU16() + 1;
2012-07-26 08:10:28 +02:00
m_thingTypes[category].clear();
m_thingTypes[category].resize(count, m_nullThingType);
2012-06-21 19:54:20 +02:00
}
for(int category = 0; category < ThingLastCategory; ++category) {
2012-06-21 19:54:20 +02:00
uint16 firstId = 1;
if(category == ThingCategoryItem)
2012-06-21 19:54:20 +02:00
firstId = 100;
for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) {
2012-07-19 23:22:52 +02:00
ThingTypePtr type(new ThingType);
type->unserialize(id, (ThingCategory)category, fin);
m_thingTypes[category][id] = type;
2012-06-21 19:54:20 +02:00
}
}
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;
}
}
2012-07-15 01:20:38 +02:00
void ThingTypeManager::loadOtb(const std::string& file)
2012-06-21 19:54:20 +02:00
{
2012-07-15 01:20:38 +02:00
FileStreamPtr fin = g_resources.openFile(file);
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
uint signature = fin->getU32();
if(signature != 0)
stdext::throw_exception("invalid otb file");
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
BinaryTreePtr root = fin->getBinaryTree();
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
signature = root->getU32();
if(signature != 0)
stdext::throw_exception("invalid otb file");
2012-06-24 17:44:33 +02:00
2012-07-15 01:20:38 +02:00
root->getU32(); // flags
2012-06-21 19:54:20 +02:00
2012-07-15 01:20:38 +02:00
m_otbMajorVersion = root->getU32();
m_otbMinorVersion = root->getU32();
root->getU32(); // build number
root->skip(128); // description
2012-06-21 19:54:20 +02:00
m_itemTypes.resize(root->getChildren().size(), m_nullItemType);
2012-07-15 01:20:38 +02:00
for(const BinaryTreePtr& node : root->getChildren()) {
2012-07-26 08:10:28 +02:00
ItemTypePtr itemType(new ItemType);
itemType->unserialize(node);
addItemType(itemType);
2012-06-21 19:54:20 +02:00
}
2012-07-15 01:20:38 +02:00
m_otbLoaded = true;
2012-06-21 19:54:20 +02:00
}
2012-07-15 01:20:38 +02:00
void ThingTypeManager::loadXml(const std::string& file)
2012-06-21 19:54:20 +02:00
{
2012-08-19 17:41:03 +02:00
if(!isOtbLoaded())
stdext::throw_exception("OTB must be loaded before XML");
TiXmlDocument doc;
doc.Parse(g_resources.loadFile(file).c_str());
if(doc.Error())
stdext::throw_exception(stdext::format("failed to parse '%s': '%s'", file, doc.ErrorDesc()));
2012-07-15 01:20:38 +02:00
TiXmlElement* root = doc.FirstChildElement();
2012-07-14 19:29:42 +02:00
if(!root || root->ValueTStr() != "items")
2012-07-15 01:20:38 +02:00
stdext::throw_exception("invalid root tag name");
for (TiXmlElement *element = root->FirstChildElement(); element; element = element->NextSiblingElement()) {
2012-07-14 19:29:42 +02:00
if(element->ValueTStr() != "item")
2012-07-15 01:20:38 +02:00
continue;
uint16 id = element->readType<uint16>("id");
2012-08-19 17:41:03 +02:00
if(id != 0) {
std::vector<std::string> s_ids = stdext::split(element->Attribute("id"), ";");
for(const std::string& s : s_ids) {
std::vector<int32> ids = stdext::split<int32>(s, "-");
if(ids.size() > 1) {
int32 i = ids[0];
while(i <= ids[1])
parseItemType(i++, element);
} else
parseItemType(atoi(s.c_str()), element);
}
} else {
std::vector<int32> begin = stdext::split<int32>(element->Attribute("fromid"), ";");
std::vector<int32> end = stdext::split<int32>(element->Attribute("toid"), ";");
if(begin[0] && begin.size() == end.size()) {
size_t size = begin.size();
for(size_t i = 0; i < size; ++i) {
while(begin[i] <= end[i])
parseItemType(begin[i]++, element);
}
}
}
}
2012-07-15 01:20:38 +02:00
doc.Clear();
m_xmlLoaded = true;
g_logger.debug("items.xml read successfully.");
}
2012-06-21 19:54:20 +02:00
void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem)
{
uint16 serverId = id;
2012-08-19 17:41:03 +02:00
ItemTypePtr itemType = nullptr;
if(serverId > 20000 && id < 20100) {
serverId -= 20000;
2012-06-21 19:54:20 +02:00
2012-08-19 17:41:03 +02:00
itemType = ItemTypePtr(new ItemType);
itemType->setServerId(serverId);
addItemType(itemType);
}
if(!itemType) {
itemType = getItemType(serverId);
if(itemType == m_nullItemType) {
itemType = ItemTypePtr(new ItemType);
itemType->setServerId(id);
addItemType(itemType);
}
}
2012-06-21 19:54:20 +02:00
2012-07-26 08:10:28 +02:00
itemType->setName(elem->Attribute("name"));
for(TiXmlElement* attrib = elem->FirstChildElement(); attrib; attrib = attrib->NextSiblingElement()) {
2012-08-19 17:41:03 +02:00
std::string key = attrib->Attribute("key");
if(key.empty())
continue;
2012-06-22 01:58:18 +02:00
2012-08-19 17:41:03 +02:00
stdext::tolower(key);
if(key == "description")
2012-07-26 08:10:28 +02:00
itemType->setDesc(attrib->Attribute("value"));
2012-08-19 17:41:03 +02:00
else if(key == "weapontype")
itemType->setCategory(ItemCategoryWeapon);
else if(key == "ammotype")
itemType->setCategory(ItemCategoryAmmunition);
else if(key == "armor")
itemType->setCategory(ItemCategoryArmor);
else if(key == "charges")
itemType->setCategory(ItemCategoryCharges);
else if(key == "type") {
std::string value = attrib->Attribute("value");
stdext::tolower(value);
if(value == "key")
itemType->setCategory(ItemCategoryKey);
else if(value == "magicfield")
itemType->setCategory(ItemCategoryMagicField);
else if(value == "teleport")
itemType->setCategory(ItemCategoryTeleport);
else if(value == "door")
itemType->setCategory(ItemCategoryDoor);
2012-07-15 01:20:38 +02:00
}
2012-06-22 01:58:18 +02:00
}
2012-06-21 19:54:20 +02:00
}
2012-07-26 08:10:28 +02:00
void ThingTypeManager::addItemType(const ItemTypePtr& itemType)
2012-06-21 19:54:20 +02:00
{
2012-07-26 08:10:28 +02:00
uint16 id = itemType->getServerId();
if(m_itemTypes.size() <= id)
2012-08-19 17:41:03 +02:00
m_itemTypes.resize((id * 3) / 2 + 1, m_nullItemType);
2012-07-26 08:10:28 +02:00
m_itemTypes[id] = itemType;
2012-06-21 19:54:20 +02:00
}
const ItemTypePtr& ThingTypeManager::findItemTypeByClientId(uint16 id)
2012-06-22 01:58:18 +02:00
{
if(m_itemTypes.empty())
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-23 23:30:54 +02:00
2012-07-26 08:10:28 +02:00
for(const ItemTypePtr& itemType : m_itemTypes) {
if(itemType->getClientId() == id)
return itemType;
2012-06-22 01:58:18 +02:00
}
2012-06-23 23:30:54 +02:00
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-22 01:58:18 +02:00
}
const ThingTypePtr& ThingTypeManager::getThingType(uint16 id, ThingCategory category)
2012-06-21 19:54:20 +02:00
{
if(category >= ThingLastCategory || id >= m_thingTypes[category].size()) {
2012-06-21 19:54:20 +02:00
g_logger.error(stdext::format("invalid thing type client id %d in category %d", id, category));
2012-07-19 23:22:52 +02:00
return m_nullThingType;
2012-06-21 19:54:20 +02:00
}
return m_thingTypes[category][id];
2012-06-21 19:54:20 +02:00
}
2012-07-19 23:22:52 +02:00
const ItemTypePtr& ThingTypeManager::getItemType(uint16 id)
2012-06-21 19:54:20 +02:00
{
2012-08-19 17:41:03 +02:00
if(id >= m_itemTypes.size() || m_itemTypes[id] == m_nullItemType) {
2012-06-21 19:54:20 +02:00
g_logger.error(stdext::format("invalid thing type server id %d", id));
2012-07-19 23:22:52 +02:00
return m_nullItemType;
2012-06-21 19:54:20 +02:00
}
return m_itemTypes[id];
2012-06-21 19:54:20 +02:00
}
2012-08-19 17:41:03 +02:00
CreaturePtr ThingTypeManager::castThingToCreature(const ThingTypePtr& thing)
{
if(!thing)
return nullptr;
if(thing->getCategory() != ThingCategoryCreature)
stdext::throw_exception("Thing type is not a creature");
uint16 clientId = thing->getId();
CreaturePtr ret(new Creature);
CreatureTypePtr cType = g_creatures.getCreatureByLook(clientId);
if(!cType) {
// 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));
}
ret->setName(cType->getName());
ret->setOutfit(cType->getOutfit());
return ret;
}
ThingTypeList ThingTypeManager::findThingTypeByAttr(ThingAttr attr, ThingCategory category)
{
ThingTypeList ret;
for(const ThingTypePtr& type : m_thingTypes[category])
if(type->hasAttr(attr))
ret.push_back(type);
return ret;
}
ItemTypeList ThingTypeManager::findItemTypeByCategory(ItemCategory category)
{
ItemTypeList ret;
for(const ItemTypePtr& type : m_itemTypes)
if(type->getCategory() == category)
ret.push_back(type);
return ret;
}
const ThingTypeList& ThingTypeManager::getThingTypes(ThingCategory category)
{
ThingTypeList ret;
if(category >= ThingLastCategory)
stdext::throw_exception(stdext::format("invalid thing type category %d", category));
return m_thingTypes[category];
}