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>
|
2012-07-14 03:10:24 +02:00
|
|
|
#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;
|
2012-07-20 07:45:11 +02:00
|
|
|
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()
|
|
|
|
{
|
2012-07-20 07:45:11 +02:00
|
|
|
for(int i = 0; i < ThingLastCategory; ++i)
|
|
|
|
m_thingTypes[i].clear();
|
|
|
|
m_itemTypes.clear();
|
2012-08-19 23:49:24 +02:00
|
|
|
m_reverseItemTypes.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();
|
|
|
|
|
2012-07-20 07:45:11 +02:00
|
|
|
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();
|
2012-07-20 07:45:11 +02:00
|
|
|
m_thingTypes[category].resize(count, m_nullThingType);
|
2012-06-21 19:54:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 07:45:11 +02:00
|
|
|
for(int category = 0; category < ThingLastCategory; ++category) {
|
2012-06-21 19:54:20 +02:00
|
|
|
uint16 firstId = 1;
|
2012-07-20 07:45:11 +02:00
|
|
|
if(category == ThingCategoryItem)
|
2012-06-21 19:54:20 +02:00
|
|
|
firstId = 100;
|
2012-07-20 07:45:11 +02:00
|
|
|
for(uint16 id = firstId; id < m_thingTypes[category].size(); ++id) {
|
2012-07-19 23:22:52 +02:00
|
|
|
ThingTypePtr type(new ThingType);
|
2012-07-20 07:45:11 +02:00
|
|
|
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-10-05 22:31:05 +02:00
|
|
|
root->skip(4);
|
2012-06-21 19:54:20 +02:00
|
|
|
|
2012-07-15 01:20:38 +02:00
|
|
|
m_otbMajorVersion = root->getU32();
|
|
|
|
m_otbMinorVersion = root->getU32();
|
2012-10-05 22:31:05 +02:00
|
|
|
root->skip(4);
|
2012-07-15 01:20:38 +02:00
|
|
|
root->skip(128); // description
|
2012-06-21 19:54:20 +02:00
|
|
|
|
2012-08-19 23:49:24 +02:00
|
|
|
m_reverseItemTypes.clear();
|
2012-10-05 22:31:05 +02:00
|
|
|
m_itemTypes.resize(root->getChildren().size() + 1, m_nullItemType);
|
2012-08-19 23:49:24 +02:00
|
|
|
|
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-08-19 23:49:24 +02:00
|
|
|
|
|
|
|
uint16 clientId = itemType->getClientId();
|
|
|
|
if(clientId >= m_reverseItemTypes.size())
|
|
|
|
m_reverseItemTypes.resize(clientId+1);
|
|
|
|
m_reverseItemTypes[clientId] = 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");
|
|
|
|
|
2012-07-18 13:46:58 +02:00
|
|
|
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;
|
|
|
|
|
2012-07-18 13:46:58 +02:00
|
|
|
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-18 13:46:58 +02:00
|
|
|
}
|
|
|
|
}
|
2012-07-15 01:20:38 +02:00
|
|
|
|
2012-07-18 13:46:58 +02:00
|
|
|
doc.Clear();
|
|
|
|
m_xmlLoaded = true;
|
|
|
|
g_logger.debug("items.xml read successfully.");
|
|
|
|
}
|
2012-06-21 19:54:20 +02:00
|
|
|
|
2012-07-18 13:46:58 +02:00
|
|
|
void ThingTypeManager::parseItemType(uint16 id, TiXmlElement* elem)
|
|
|
|
{
|
|
|
|
uint16 serverId = id;
|
2012-08-19 17:41:03 +02:00
|
|
|
ItemTypePtr itemType = nullptr;
|
2012-10-05 22:31:05 +02:00
|
|
|
if(serverId > 20000 && serverId < 20100) {
|
2012-07-18 13:46:58 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-10-05 22:31:05 +02:00
|
|
|
itemType = getItemType(serverId);
|
|
|
|
assert(itemType && "Internal error");
|
2012-07-26 08:10:28 +02:00
|
|
|
itemType->setName(elem->Attribute("name"));
|
2012-07-18 13:46:58 +02:00
|
|
|
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();
|
2012-10-05 22:31:05 +02:00
|
|
|
if(id > m_itemTypes.size())
|
|
|
|
m_itemTypes.resize(id + 1, m_nullItemType);
|
2012-07-26 08:10:28 +02:00
|
|
|
m_itemTypes[id] = itemType;
|
2012-06-21 19:54:20 +02:00
|
|
|
}
|
|
|
|
|
2012-07-30 17:08:21 +02:00
|
|
|
const ItemTypePtr& ThingTypeManager::findItemTypeByClientId(uint16 id)
|
2012-06-22 01:58:18 +02:00
|
|
|
{
|
2012-08-19 23:49:24 +02:00
|
|
|
if(id == 0 || id >= m_reverseItemTypes.size())
|
2012-07-19 23:22:52 +02:00
|
|
|
return m_nullItemType;
|
2012-06-23 23:30:54 +02:00
|
|
|
|
2012-08-19 23:49:24 +02:00
|
|
|
if(m_reverseItemTypes[id])
|
|
|
|
return m_reverseItemTypes[id];
|
|
|
|
else
|
|
|
|
return m_nullItemType;
|
2012-06-22 01:58:18 +02:00
|
|
|
}
|
|
|
|
|
2012-07-20 07:45:11 +02:00
|
|
|
const ThingTypePtr& ThingTypeManager::getThingType(uint16 id, ThingCategory category)
|
2012-06-21 19:54:20 +02:00
|
|
|
{
|
2012-07-20 07:45:11 +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
|
|
|
}
|
2012-07-20 07:45:11 +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
|
|
|
}
|
2012-07-20 07:45:11 +02:00
|
|
|
return m_itemTypes[id];
|
2012-06-21 19:54:20 +02:00
|
|
|
}
|
2012-07-15 15:23:13 +02:00
|
|
|
|
2012-07-20 07:45:11 +02:00
|
|
|
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];
|
|
|
|
}
|