2012-07-18 16:36:46 +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 "creatures.h"
|
|
|
|
|
|
|
|
#include <framework/xml/tinyxml.h>
|
|
|
|
#include <framework/core/resourcemanager.h>
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
|
|
void Creatures::loadMonsters(const std::string& file)
|
|
|
|
{
|
|
|
|
TiXmlDocument doc;
|
|
|
|
doc.Parse(g_resources.loadFile(file).c_str());
|
|
|
|
if(doc.Error())
|
|
|
|
stdext::throw_exception(stdext::format("cannot open monsters file '%s': '%s'", file, doc.ErrorDesc()));
|
|
|
|
|
|
|
|
TiXmlElement* root = doc.FirstChildElement();
|
|
|
|
if(!root || root->ValueStr() != "monsters")
|
|
|
|
stdext::throw_exception("malformed monsters xml file");
|
|
|
|
|
|
|
|
for(TiXmlElement* monster = root->FirstChildElement(); monster; monster = monster->NextSiblingElement()) {
|
|
|
|
std::string fname = file.substr(0, file.find_last_of('/')) + '/' + monster->Attribute("file");
|
|
|
|
if(fname.substr(fname.length() - 4) != ".xml")
|
|
|
|
fname += ".xml";
|
|
|
|
|
|
|
|
loadSingleCreature(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
doc.Clear();
|
|
|
|
m_loaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creatures::loadSingleCreature(const std::string& file)
|
|
|
|
{
|
2012-07-28 10:06:16 +02:00
|
|
|
loadCreatureBuffer(g_resources.loadFile(file));
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
2012-07-28 10:06:16 +02:00
|
|
|
void Creatures::loadNpcs(const std::string& folder)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
|
|
|
boost::filesystem::path npcPath(folder);
|
|
|
|
if(!boost::filesystem::exists(npcPath))
|
|
|
|
stdext::throw_exception(stdext::format("NPCs folder '%s' was not found.", folder));
|
|
|
|
|
|
|
|
for(boost::filesystem::directory_iterator it(npcPath), end; it != end; ++it) {
|
|
|
|
std::string f = it->path().string();
|
2012-07-19 15:55:10 +02:00
|
|
|
if(boost::filesystem::is_directory(it->status()) || ((f.size() > 4 ? f.substr(f.size() - 4) : "") != ".xml"))
|
2012-07-18 16:36:46 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
loadCreatureBuffer(g_resources.loadFile(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Creatures::loadCreatureBuffer(const std::string &buffer)
|
|
|
|
{
|
|
|
|
TiXmlDocument doc;
|
|
|
|
doc.Parse(buffer.c_str());
|
|
|
|
if(doc.Error())
|
|
|
|
stdext::throw_exception(stdext::format("cannot load creature buffer: %s", doc.ErrorDesc()));
|
|
|
|
|
|
|
|
TiXmlElement* root = doc.FirstChildElement();
|
2012-07-28 10:06:16 +02:00
|
|
|
if(!root || (root->ValueStr() != "monster" && root->ValueStr() != "npc"))
|
2012-07-19 15:55:10 +02:00
|
|
|
stdext::throw_exception("invalid root tag name");
|
2012-07-18 16:36:46 +02:00
|
|
|
|
2012-08-01 09:49:09 +02:00
|
|
|
std::string cName = root->Attribute("name");
|
|
|
|
stdext::tolower(cName);
|
|
|
|
stdext::trim(cName);
|
|
|
|
|
|
|
|
CreatureTypePtr newType(new CreatureType(cName));
|
2012-07-18 16:36:46 +02:00
|
|
|
for(TiXmlElement* attrib = root->FirstChildElement(); attrib; attrib = attrib->NextSiblingElement()) {
|
2012-07-28 10:06:16 +02:00
|
|
|
if(attrib->ValueStr() != "look")
|
|
|
|
continue;
|
2012-07-18 16:36:46 +02:00
|
|
|
|
2012-07-19 15:55:10 +02:00
|
|
|
if(m_loadCreatureBuffer(attrib, newType))
|
|
|
|
break;
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
2012-07-19 15:55:10 +02:00
|
|
|
|
|
|
|
doc.Clear();
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
2012-07-28 10:06:16 +02:00
|
|
|
bool Creatures::m_loadCreatureBuffer(TiXmlElement* attrib, const CreatureTypePtr& m)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
2012-07-28 10:06:16 +02:00
|
|
|
if(std::find(m_creatures.begin(), m_creatures.end(), m) != m_creatures.end())
|
2012-07-18 16:36:46 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
Outfit out;
|
2012-07-31 02:47:21 +02:00
|
|
|
out.setCategory(ThingCategoryCreature);
|
2012-07-18 16:36:46 +02:00
|
|
|
int32 type;
|
|
|
|
if(!attrib->Attribute("type").empty())
|
|
|
|
type = attrib->readType<int32>("type");
|
2012-07-28 10:06:16 +02:00
|
|
|
else
|
2012-07-18 16:36:46 +02:00
|
|
|
type = attrib->readType<int32>("typeex");
|
|
|
|
|
|
|
|
out.setId(type);
|
2012-07-28 10:06:16 +02:00
|
|
|
{
|
2012-07-18 16:36:46 +02:00
|
|
|
out.setHead(attrib->readType<int>(("head")));
|
|
|
|
out.setBody(attrib->readType<int>(("body")));
|
|
|
|
out.setLegs(attrib->readType<int>(("legs")));
|
|
|
|
out.setFeet(attrib->readType<int>(("feet")));
|
|
|
|
out.setAddons(attrib->readType<int>(("addons")));
|
|
|
|
out.setMount(attrib->readType<int>(("mount")));
|
|
|
|
}
|
2012-07-28 10:06:16 +02:00
|
|
|
|
2012-07-18 16:36:46 +02:00
|
|
|
m->setOutfit(out);
|
|
|
|
m_creatures.push_back(m);
|
|
|
|
return type >= 0;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:49:09 +02:00
|
|
|
CreatureTypePtr Creatures::getCreature(std::string name)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::tolower(name);
|
|
|
|
stdext::trim(name);
|
2012-07-18 16:36:46 +02:00
|
|
|
auto it = std::find_if(m_creatures.begin(), m_creatures.end(),
|
2012-08-01 09:49:09 +02:00
|
|
|
[=] (const CreatureTypePtr& m) -> bool { return m->getName() == name; });
|
2012-07-18 16:36:46 +02:00
|
|
|
return it != m_creatures.end() ? *it : nullptr;
|
|
|
|
}
|