This commit is contained in:
niczkx 2012-07-16 05:41:38 +03:00
parent c131d29b5f
commit 12d30f7355
8 changed files with 221 additions and 2 deletions

View File

@ -29,6 +29,7 @@
#include <sstream>
#include <iomanip>
#include <vector>
#include <cctype>
#include <boost/algorithm/string.hpp>
#include "types.h"
@ -213,6 +214,18 @@ inline std::string utf8StringToLatin1(uchar *utf8) {
return out;
}
// Convert string to lower case
inline std::string toLowerCaseString(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), tolower);
return str;
}
// Convert string to upper case
inline std::string toUpperCaseString(std::string& str) {
std::transform(str.begin(), str.end(), str.begin(), toupper);
return str;
}
// utility for printing messages into stdout
template<class... T>
void print(const T&... args) {

View File

@ -79,6 +79,8 @@ set(otclient_SOURCES ${otclient_SOURCES}
${CMAKE_CURRENT_LIST_DIR}/houses.h
${CMAKE_CURRENT_LIST_DIR}/towns.cpp
${CMAKE_CURRENT_LIST_DIR}/towns.h
${CMAKE_CURRENT_LIST_DIR}/monsters.cpp
${CMAKE_CURRENT_LIST_DIR}/monsters.h
# lua
${CMAKE_CURRENT_LIST_DIR}/luavaluecasts.cpp

View File

@ -160,6 +160,11 @@ class Monster : public Creature
public:
MonsterPtr asMonster() { return std::static_pointer_cast<Monster>(shared_from_this()); }
bool isMonster() { return true; }
Position getPos() { return m_pos; }
void setPos(const Position& pos) { m_pos = pos; }
private:
Position m_pos;
};
#endif

View File

@ -96,8 +96,11 @@ void OTClient::registerLuaFunctions()
//g_lua.bindSingletonFunction("g_map", "saveOtbm", &Map::saveOtbm, &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", "loadMonsters", &Map::loadMonsters, &g_map);
g_lua.bindSingletonFunction("g_map", "loadSingleMonster", &Map::loadSingleMonster, &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", "getMonster", &Map::getMonster, &g_map);
g_lua.registerSingletonClass("g_game");
g_lua.bindSingletonFunction("g_game", "loginWorld", &Game::loginWorld, &g_game);

View File

@ -34,6 +34,7 @@
#include <framework/core/filestream.h>
#include <framework/core/binarytree.h>
#include <framework/core/application.h>
#include <framework/xml/tinyxml.h>
Map g_map;
@ -261,7 +262,9 @@ void Map::loadOtbm(const std::string& fileName)
g_logger.debug(stdext::format("Total tiles: %d", m_tiles.size()));
g_logger.debug("OTBM read successfully.");
fin->close();
/// TODO read XML Stuff (houses & spawns).
loadSpawns(m_spawnFile);
m_houses.load(m_houseFile);
}
void Map::saveOtbm(const std::string &fileName)
@ -373,6 +376,41 @@ void Map::saveOtbm(const std::string &fileName)
#endif
}
void Map::loadSpawns(const std::string &fileName)
{
#define cast(NAME, TYPE, NODE) stdext::unsafe_cast<TYPE>(NODE->Attribute((NAME)))
if(!m_monsters.isLoaded())
stdext::throw_exception("cannot load spawns; monsters aren't loaded.");
TiXmlDocument doc(fileName);
if(!doc.LoadFile())
stdext::throw_exception(stdext::format("cannot load spawns xml file '%s", fileName));
TiXmlElement* root = doc.FirstChildElement();
if(!root || root->ValueStr() != "spawns")
stdext::throw_exception(stdext::format("malformed spawns file"));
for(TiXmlElement* node = root->FirstChildElement(); node; node = node->NextSiblingElement()) {
if (node->ValueTStr() != "spawn")
stdext::throw_exception(stdext::format("invalid spawn node"));
Position centerPos(cast("x", uint16, node), cast("y", uint16, node), cast("z", uint8, node));
for(TiXmlElement* mType = node->FirstChildElement(); mType; mType = mType->NextSiblingElement()) {
if (mType->ValueStr() != "monster")
stdext::throw_exception("invalid spawn-subnode");
std::string mName = mType->Attribute("name");
MonsterPtr m = m_monsters.getMonster(mName);
if (!m)
stdext::throw_exception(stdext::format("unkown monster %s", mName));
Point off(cast("x", int, mType), cast("y", int, mType));
Position mPos(centerPos.x + off.x, centerPos.y + off.y, centerPos.z);
addThing(m, mPos, -1);
}
}
}
bool Map::loadOtcm(const std::string& fileName)
{
try {
@ -521,6 +559,7 @@ void Map::clean()
// This is a fix to a segfault on exit.
m_towns.clear();
m_houses.clear();
m_monsters.clear();
}
void Map::cleanDynamicThings()

View File

@ -26,6 +26,7 @@
#include "creature.h"
#include "houses.h"
#include "towns.h"
#include "monsters.h"
#include "animatedtext.h"
#include <framework/core/clock.h>
@ -115,6 +116,12 @@ public:
void loadOtbm(const std::string& fileName);
void saveOtbm(const std::string& fileName);
void loadSpawns(const std::string& fileName);
void saveSpawns(const std::string&) { }
void loadMonsters(const std::string& fileName) { m_monsters.loadMonsters(fileName); }
void loadSingleMonster(const std::string& file) { m_monsters.loadSingleMonster(file); }
void clean();
void cleanDynamicThings();
void cleanTexts();
@ -141,9 +148,10 @@ public:
std::vector<CreaturePtr> getSpectatorsInRange(const Position& centerPos, bool multiFloor, int xRange, int yRange);
std::vector<CreaturePtr> getSpectatorsInRangeEx(const Position& centerPos, bool multiFloor, int minXRange, int maxXRange, int minYRange, int maxYRange);
// town/house related
// town/house/monster related
TownPtr getTown(uint32 tid) { return m_towns.getTown(tid); }
HousePtr getHouse(uint32 hid) { return m_houses.getHouse(hid); }
MonsterPtr getMonster(const std::string& name) { return m_monsters.getMonster(name); }
void setLight(const Light& light) { m_light = light; }
void setCentralPosition(const Position& centralPosition);
@ -181,6 +189,7 @@ private:
Houses m_houses;
Towns m_towns;
Monsters m_monsters;
uint16 m_width, m_height;
};

103
src/otclient/monsters.cpp Normal file
View File

@ -0,0 +1,103 @@
/*
* 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 "monsters.h"
#include "creature.h"
#include <framework/xml/tinyxml.h>
void Monsters::loadMonsters(const std::string& file)
{
TiXmlDocument doc(file);
if(!doc.LoadFile())
stdext::throw_exception(stdext::format("cannot open monsters file '%s'", file));
TiXmlElement* root = doc.FirstChildElement();
if(!root || root->ValueStr() != "monsters")
stdext::throw_exception("malformed monsters xml file");
for(TiXmlElement* monster = root->FirstChildElement(); root; root = root->NextSiblingElement()) {
MonsterPtr newMonster(new Monster);
newMonster->setName(monster->Attribute("name"));
loadSingleMonster(file.substr(0, file.find_last_of('/')) + '/' + monster->Attribute("file"), newMonster);
}
doc.Clear();
m_loaded = true;
}
void Monsters::loadSingleMonster(const std::string& file, const MonsterPtr& m)
{
#define read(str) stdext::unsafe_cast<int>(attrib->Attribute((str)))
if (!m || std::find(m_monsters.begin(), m_monsters.end(), m) != m_monsters.end())
stdext::throw_exception("reloading monsters is not supported yet.");
TiXmlDocument doc(file);
if(!doc.LoadFile())
stdext::throw_exception(stdext::format("cannot load single monster file '%s'", file));
TiXmlElement* root = doc.FirstChildElement();
if(!root || root->ValueStr() != "monster")
stdext::throw_exception(stdext::format("malformed monster xml file: %s", file));
for(TiXmlElement* attrib = root->FirstChildElement(); attrib; attrib = attrib->NextSiblingElement()) {
if(attrib->ValueStr() == "look") {
Outfit out;
int type = read("type");
if (type <= 0)
type = read("typeex");
if(type) {
out.setId(type);
{
out.setHead(read("head"));
out.setBody(read("body"));
out.setLegs(read("legs"));
out.setFeet(read("feet"));
out.setAddons(read("addons"));
out.setMount(read("mount"));
}
} else
stdext::throw_exception(stdext::format("invalid look type/typeex for monster %s", m->getName()));
m->setOutfit(out);
}
}
m_monsters.push_back(m);
}
MonsterPtr Monsters::getMonster(const std::string& name)
{
auto it = std::find_if(m_monsters.begin(), m_monsters.end(),
[=] (const MonsterPtr& m) -> bool { return m->getName() == name; });
if (it != m_monsters.end())
return *it;
return nullptr;
}
MonsterPtr Monsters::getMonsterByPos(const Position& pos)
{
auto it = std::find_if(m_monsters.begin(), m_monsters.end(),
[=] (const MonsterPtr& m) -> bool { return m->getPos() == pos; });
return it != m_monsters.end() ? *it : nullptr;
}

45
src/otclient/monsters.h Normal file
View File

@ -0,0 +1,45 @@
/*
* 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.
*/
#ifndef MONSTERS_H
#define MONSTERS_H
#include "declarations.h"
class Monsters
{
public:
void clear() { m_monsters.clear(); }
void loadMonsters(const std::string& file);
void loadSingleMonster(const std::string& file, const MonsterPtr& m = nullptr);
MonsterPtr getMonster(const std::string& name);
MonsterPtr getMonsterByPos(const Position& pos);
bool isLoaded() const { return m_loaded; }
private:
std::vector<MonsterPtr> m_monsters;
bool m_loaded;
};
#endif