2012-07-18 16:36:46 +02:00
|
|
|
/*
|
2013-01-08 19:21:54 +01:00
|
|
|
* Copyright (c) 2010-2013 OTClient <https://github.com/edubart/otclient>
|
2012-07-18 16:36:46 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2012-08-26 20:44:46 +02:00
|
|
|
#include "creature.h"
|
2012-09-14 23:38:21 +02:00
|
|
|
#include "map.h"
|
2012-07-18 16:36:46 +02:00
|
|
|
|
|
|
|
#include <framework/xml/tinyxml.h>
|
|
|
|
#include <framework/core/resourcemanager.h>
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
CreatureManager g_creatures;
|
|
|
|
|
2012-09-21 01:23:10 +02:00
|
|
|
static bool isInZone(const Position& pos/* placePos*/,
|
|
|
|
const Position& centerPos,
|
|
|
|
int radius)
|
|
|
|
{
|
|
|
|
if(radius == -1)
|
|
|
|
return true;
|
2012-09-20 23:44:57 +02:00
|
|
|
return ((pos.x >= centerPos.x - radius) && (pos.x <= centerPos.x + radius) &&
|
2012-09-21 01:23:10 +02:00
|
|
|
(pos.y >= centerPos.y - radius) && (pos.y <= centerPos.y + radius)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-10-03 17:49:57 +02:00
|
|
|
void CreatureManager::terminate()
|
|
|
|
{
|
|
|
|
clearSpawns();
|
|
|
|
clear();
|
|
|
|
m_nullCreature = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
void Spawn::load(TiXmlElement* node)
|
|
|
|
{
|
2013-03-02 06:33:14 +01:00
|
|
|
Position centerPos;
|
|
|
|
centerPos.x = node->readType<uint16>("centerx");
|
|
|
|
centerPos.y = node->readType<uint16>("centery");
|
|
|
|
centerPos.z = node->readType<uint8>("centerz");
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
setCenterPos(centerPos);
|
|
|
|
setRadius(node->readType<int32>("radius"));
|
|
|
|
|
|
|
|
CreatureTypePtr cType(nullptr);
|
|
|
|
for(TiXmlElement* cNode = node->FirstChildElement(); cNode; cNode = cNode->NextSiblingElement()) {
|
|
|
|
if(cNode->ValueStr() != "monster" && cNode->ValueStr() != "npc")
|
|
|
|
stdext::throw_exception(stdext::format("invalid spawn-subnode %s", cNode->ValueStr()));
|
|
|
|
|
|
|
|
std::string cName = cNode->Attribute("name");
|
|
|
|
stdext::tolower(cName);
|
|
|
|
stdext::trim(cName);
|
|
|
|
|
|
|
|
if (!(cType = g_creatures.getCreatureByName(cName)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
cType->setSpawnTime(cNode->readType<int>("spawntime"));
|
|
|
|
Otc::Direction dir = Otc::North;
|
|
|
|
int16 dir_ = cNode->readType<int16>("direction");
|
|
|
|
if(dir_ >= Otc::East && dir_ <= Otc::West)
|
|
|
|
dir = (Otc::Direction)dir_;
|
|
|
|
cType->setDirection(dir);
|
|
|
|
|
|
|
|
centerPos.x += cNode->readType<int>("x");
|
|
|
|
centerPos.y += cNode->readType<int>("y");
|
|
|
|
centerPos.z = cNode->readType<int>("z");
|
2012-09-21 01:23:10 +02:00
|
|
|
addCreature(centerPos, cType);
|
2012-09-14 23:38:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-04 14:21:12 +02:00
|
|
|
void Spawn::save(TiXmlElement* node)
|
2012-09-14 23:38:21 +02:00
|
|
|
{
|
|
|
|
const Position& c = getCenterPos();
|
|
|
|
node->SetAttribute("centerx", c.x);
|
|
|
|
node->SetAttribute("centery", c.y);
|
|
|
|
node->SetAttribute("centerz", c.z);
|
|
|
|
|
|
|
|
node->SetAttribute("radius", getRadius());
|
|
|
|
|
|
|
|
TiXmlElement* creatureNode = nullptr;
|
2012-09-21 01:23:10 +02:00
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
for(const auto& pair : m_creatures) {
|
|
|
|
if(!(creatureNode = new TiXmlElement("monster")))
|
|
|
|
stdext::throw_exception("oom?");
|
|
|
|
|
|
|
|
const CreatureTypePtr& creature = pair.second;
|
|
|
|
|
|
|
|
creatureNode->SetAttribute("name", creature->getName());
|
|
|
|
creatureNode->SetAttribute("spawntime", creature->getSpawnTime());
|
|
|
|
creatureNode->SetAttribute("direction", creature->getDirection());
|
|
|
|
|
|
|
|
const Position& placePos = pair.first;
|
|
|
|
assert(placePos.isValid());
|
|
|
|
|
|
|
|
creatureNode->SetAttribute("x", placePos.x);
|
|
|
|
creatureNode->SetAttribute("y", placePos.y);
|
|
|
|
creatureNode->SetAttribute("z", placePos.z);
|
|
|
|
|
|
|
|
node->LinkEndChild(creatureNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spawn::addCreature(const Position& placePos, const CreatureTypePtr& cType)
|
|
|
|
{
|
|
|
|
const Position& centerPos = getCenterPos();
|
|
|
|
int m_radius = getRadius();
|
2013-08-19 10:38:22 +02:00
|
|
|
if(!isInZone(placePos, centerPos, m_radius)) {
|
|
|
|
g_logger.warning(stdext::format("cannot place creature at %s (spawn's center position: %s, spawn radius: %d) (increment radius)",
|
2012-09-21 01:23:10 +02:00
|
|
|
stdext::to_string(placePos), stdext::to_string(centerPos),
|
|
|
|
m_radius
|
|
|
|
));
|
2013-08-19 10:38:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-21 01:23:10 +02:00
|
|
|
g_map.addThing(cType->cast(), placePos, 4);
|
|
|
|
m_creatures.insert(std::make_pair(placePos, cType));
|
2012-09-14 23:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Spawn::removeCreature(const Position& pos)
|
|
|
|
{
|
|
|
|
auto iterator = m_creatures.find(pos);
|
2012-09-21 01:23:10 +02:00
|
|
|
if(iterator != m_creatures.end()) {
|
|
|
|
assert(iterator->first.isValid());
|
|
|
|
assert(g_map.removeThingByPos(iterator->first, 4));
|
|
|
|
m_creatures.erase(iterator);
|
|
|
|
}
|
2012-09-14 23:38:21 +02:00
|
|
|
}
|
2012-08-19 17:41:03 +02:00
|
|
|
|
2012-08-26 20:44:46 +02:00
|
|
|
CreaturePtr CreatureType::cast()
|
|
|
|
{
|
|
|
|
CreaturePtr ret(new Creature);
|
2012-09-14 23:38:21 +02:00
|
|
|
|
|
|
|
std::string cName = getName();
|
|
|
|
stdext::ucwords(cName);
|
|
|
|
ret->setName(cName);
|
|
|
|
|
|
|
|
ret->setDirection(getDirection());
|
2012-08-26 20:44:46 +02:00
|
|
|
ret->setOutfit(getOutfit());
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
CreatureManager::CreatureManager()
|
|
|
|
{
|
|
|
|
m_nullCreature = CreatureTypePtr(new CreatureType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureManager::clearSpawns()
|
|
|
|
{
|
2012-09-21 01:23:10 +02:00
|
|
|
for(auto pair : m_spawns)
|
|
|
|
pair.second->clear();
|
2012-09-14 23:38:21 +02:00
|
|
|
m_spawns.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureManager::loadMonsters(const std::string& file)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
|
|
|
TiXmlDocument doc;
|
2013-01-08 22:31:41 +01:00
|
|
|
doc.Parse(g_resources.readFileContents(file).c_str());
|
2012-07-18 16:36:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
void CreatureManager::loadSingleCreature(const std::string& file)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
2013-01-08 22:31:41 +01:00
|
|
|
loadCreatureBuffer(g_resources.readFileContents(file));
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
void CreatureManager::loadNpcs(const std::string& folder)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
2012-09-23 03:18:48 +02:00
|
|
|
std::string tmp = folder;
|
|
|
|
if(!stdext::ends_with(tmp, "/"))
|
|
|
|
tmp += "/";
|
|
|
|
|
2013-08-17 23:36:44 +02:00
|
|
|
if(!g_resources.directoryExists(tmp))
|
2012-07-18 16:36:46 +02:00
|
|
|
stdext::throw_exception(stdext::format("NPCs folder '%s' was not found.", folder));
|
|
|
|
|
2013-08-17 23:36:44 +02:00
|
|
|
const auto& fileList = g_resources.listDirectoryFiles(tmp);
|
|
|
|
for(const std::string& file : fileList)
|
|
|
|
loadCreatureBuffer(g_resources.readFileContents(tmp + file));
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
void CreatureManager::loadSpawns(const std::string& fileName)
|
|
|
|
{
|
|
|
|
if(!isLoaded()) {
|
2012-09-19 09:13:45 +02:00
|
|
|
g_logger.warning("creatures aren't loaded yet to load spawns.");
|
2012-09-14 23:38:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_spawnLoaded) {
|
|
|
|
g_logger.warning("attempt to reload spawns.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TiXmlDocument doc;
|
2013-01-08 22:31:41 +01:00
|
|
|
doc.Parse(g_resources.readFileContents(fileName).c_str());
|
2012-09-14 23:38:21 +02:00
|
|
|
if(doc.Error())
|
|
|
|
stdext::throw_exception(stdext::format("cannot load spawns xml file '%s: '%s'", fileName, doc.ErrorDesc()));
|
|
|
|
|
|
|
|
TiXmlElement* root = doc.FirstChildElement();
|
|
|
|
if(!root || root->ValueStr() != "spawns")
|
|
|
|
stdext::throw_exception("malformed spawns file");
|
|
|
|
|
|
|
|
for(TiXmlElement* node = root->FirstChildElement(); node; node = node->NextSiblingElement()) {
|
|
|
|
if(node->ValueTStr() != "spawn")
|
|
|
|
stdext::throw_exception("invalid spawn node");
|
|
|
|
|
|
|
|
SpawnPtr spawn(new Spawn);
|
|
|
|
spawn->load(node);
|
2012-09-21 01:23:10 +02:00
|
|
|
m_spawns.insert(std::make_pair(spawn->getCenterPos(), spawn));
|
2012-09-14 23:38:21 +02:00
|
|
|
}
|
|
|
|
doc.Clear();
|
|
|
|
m_spawnLoaded = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureManager::saveSpawns(const std::string& fileName)
|
|
|
|
{
|
|
|
|
TiXmlDocument doc;
|
|
|
|
doc.SetTabSize(2);
|
|
|
|
|
|
|
|
TiXmlDeclaration* decl = new TiXmlDeclaration("1.0", "UTF-8", "");
|
|
|
|
doc.LinkEndChild(decl);
|
|
|
|
|
|
|
|
TiXmlElement* root = new TiXmlElement("spawns");
|
|
|
|
doc.LinkEndChild(root);
|
|
|
|
|
2012-09-21 01:23:10 +02:00
|
|
|
for(auto pair : m_spawns) {
|
2013-08-04 14:21:12 +02:00
|
|
|
TiXmlElement* elem = new TiXmlElement("spawn");
|
2012-09-21 01:23:10 +02:00
|
|
|
pair.second->save(elem);
|
2012-09-14 23:38:21 +02:00
|
|
|
root->LinkEndChild(elem);
|
|
|
|
}
|
|
|
|
|
2013-08-19 10:38:22 +02:00
|
|
|
std::string savePath = g_resources.getRealDir(fileName) + "/" + fileName;
|
|
|
|
if(!doc.SaveFile(savePath))
|
|
|
|
stdext::throw_exception(stdext::format("failed to save spawns XML %s: %s", savePath, doc.ErrorDesc()));
|
2012-09-14 23:38:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CreatureManager::loadCreatureBuffer(const std::string& buffer)
|
2012-07-18 16:36:46 +02:00
|
|
|
{
|
|
|
|
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-10-03 17:49:57 +02:00
|
|
|
internalLoadCreatureBuffer(attrib, newType);
|
2012-09-14 23:38:21 +02:00
|
|
|
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-10-03 17:49:57 +02:00
|
|
|
void CreatureManager::internalLoadCreatureBuffer(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-09-14 23:38:21 +02:00
|
|
|
return;
|
2012-07-18 16:36:46 +02:00
|
|
|
|
|
|
|
Outfit out;
|
2012-09-18 11:13:25 +02:00
|
|
|
|
|
|
|
int32 type = attrib->readType<int32>("type");
|
|
|
|
if(type > 0) {
|
|
|
|
out.setCategory(ThingCategoryCreature);
|
|
|
|
out.setId(type);
|
|
|
|
} else {
|
|
|
|
out.setCategory(ThingCategoryItem);
|
|
|
|
out.setAuxId(attrib->readType<int32>("typeex"));
|
|
|
|
}
|
2012-07-18 16:36:46 +02:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
const CreatureTypePtr& CreatureManager::getCreatureByName(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-09-14 23:38:21 +02:00
|
|
|
if(it != m_creatures.end())
|
|
|
|
return *it;
|
|
|
|
g_logger.warning(stdext::format("could not find creature with name: %s", name));
|
|
|
|
return m_nullCreature;
|
2012-07-18 16:36:46 +02:00
|
|
|
}
|
2012-08-19 17:41:03 +02:00
|
|
|
|
2012-09-14 23:38:21 +02:00
|
|
|
const CreatureTypePtr& CreatureManager::getCreatureByLook(int look)
|
2012-08-19 17:41:03 +02:00
|
|
|
{
|
|
|
|
auto findFun = [=] (const CreatureTypePtr& c) -> bool
|
|
|
|
{
|
|
|
|
const Outfit& o = c->getOutfit();
|
2012-09-21 01:23:10 +02:00
|
|
|
return o.getId() == look || o.getAuxId() == look;
|
2012-08-19 17:41:03 +02:00
|
|
|
};
|
|
|
|
auto it = std::find_if(m_creatures.begin(), m_creatures.end(), findFun);
|
2012-09-14 23:38:21 +02:00
|
|
|
if(it != m_creatures.end())
|
|
|
|
return *it;
|
|
|
|
g_logger.warning(stdext::format("could not find creature with looktype: %d", look));
|
|
|
|
return m_nullCreature;
|
2012-08-19 17:41:03 +02:00
|
|
|
}
|
2012-09-18 07:23:12 +02:00
|
|
|
|
2012-09-19 09:13:45 +02:00
|
|
|
SpawnPtr CreatureManager::getSpawn(const Position& centerPos)
|
2012-09-18 07:23:12 +02:00
|
|
|
{
|
2012-09-21 01:23:10 +02:00
|
|
|
auto it = m_spawns.find(centerPos);
|
2012-09-18 07:23:12 +02:00
|
|
|
if(it != m_spawns.end())
|
2012-09-21 01:23:10 +02:00
|
|
|
return it->second;
|
2012-09-18 07:23:12 +02:00
|
|
|
g_logger.debug(stdext::format("failed to find spawn at center %s",stdext::to_string(centerPos)));
|
2012-09-19 09:13:45 +02:00
|
|
|
return nullptr;
|
2012-09-18 07:23:12 +02:00
|
|
|
}
|
|
|
|
|
2012-09-21 01:23:10 +02:00
|
|
|
SpawnPtr CreatureManager::addSpawn(const Position& centerPos, int radius)
|
|
|
|
{
|
|
|
|
auto iter = m_spawns.find(centerPos);
|
|
|
|
if(iter != m_spawns.end())
|
|
|
|
return iter->second;
|
|
|
|
|
|
|
|
SpawnPtr ret(new Spawn);
|
|
|
|
|
|
|
|
ret->setRadius(radius);
|
|
|
|
ret->setCenterPos(centerPos);
|
|
|
|
|
|
|
|
m_spawns.insert(std::make_pair(centerPos, ret));
|
|
|
|
return ret;
|
|
|
|
}
|
2012-09-20 23:44:57 +02:00
|
|
|
|