master
niczkx 12 years ago
parent 02f89dd8be
commit 28825a3298

@ -120,7 +120,6 @@ R unsafe_cast(const T& t, R def) {
return def; return def;
} }
} }
} }
#endif #endif

@ -214,10 +214,20 @@ inline std::string utf8StringToLatin1(uchar *utf8) {
} }
// Convert string to lower case // Convert string to lower case
inline std::string tolower(std::string& str) { return boost::algorithm::to_lower_copy(str); } inline std::string tolower(const std::string& str)
{
std::string _str = str;
boost::algorithm::to_lower(_str);
return _str;
}
// Convert string to upper case // Convert string to upper case
inline std::string toupper(std::string& str) { return boost::algorithm::to_upper_copy(str); } inline std::string toupper(const std::string& str)
{
std::string _str = str;
boost::algorithm::to_upper(_str);
return _str;
}
// utility for printing messages into stdout // utility for printing messages into stdout
template<class... T> template<class... T>

@ -596,16 +596,10 @@ const std::string* TiXmlElement::Attribute( const std::string& name ) const
const char* TiXmlElement::Attribute( const char* name, int* i ) const const char* TiXmlElement::Attribute( const char* name, int* i ) const
{ {
const TiXmlAttribute* attrib = attributeSet.Find( name ); int p = readType<int>(name);
const char* result = 0; if(i)
*i = p;
if ( attrib ) { return Attribute(name);
result = attrib->Value();
if ( i ) {
attrib->QueryIntValue( i );
}
}
return result;
} }
@ -625,19 +619,12 @@ const std::string* TiXmlElement::Attribute( const std::string& name, int* i ) co
} }
#endif #endif
const char* TiXmlElement::Attribute(const char *name, double *d) const
const char* TiXmlElement::Attribute( const char* name, double* d ) const
{ {
const TiXmlAttribute* attrib = attributeSet.Find( name ); double p = readType<double>(name);
const char* result = 0; if(d)
*d = p;
if ( attrib ) { return Attribute(name);
result = attrib->Value();
if ( d ) {
attrib->QueryDoubleValue( d );
}
}
return result;
} }

@ -40,6 +40,10 @@ distribution.
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <framework/global.h>
#include <otclient/position.h>
#include <framework/stdext/cast.h>
// Help out windows: // Help out windows:
#if defined( _DEBUG ) && !defined( DEBUG ) #if defined( _DEBUG ) && !defined( DEBUG )
#define DEBUG #define DEBUG
@ -200,6 +204,9 @@ class TiXmlBase
friend class TiXmlDocument; friend class TiXmlDocument;
public: public:
TiXmlBase( const TiXmlBase& ) = delete;
void operator=( const TiXmlBase& base ) = delete;
TiXmlBase() : userData(0) {} TiXmlBase() : userData(0) {}
virtual ~TiXmlBase() {} virtual ~TiXmlBase() {}
@ -396,9 +403,6 @@ protected:
static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length ); static void ConvertUTF32ToUTF8( unsigned long input, char* output, int* length );
private: private:
TiXmlBase( const TiXmlBase& ); // not implemented.
void operator=( const TiXmlBase& base ); // not allowed.
struct Entity struct Entity
{ {
const char* str; const char* str;
@ -428,6 +432,9 @@ class TiXmlNode : public TiXmlBase
friend class TiXmlElement; friend class TiXmlElement;
public: public:
TiXmlNode( const TiXmlNode& ) = delete;
void operator=( const TiXmlNode& base ) = delete;
#ifdef TIXML_USE_STL #ifdef TIXML_USE_STL
/** An input stream operator, for every class. Tolerant of newlines and /** An input stream operator, for every class. Tolerant of newlines and
@ -764,10 +771,6 @@ protected:
TiXmlNode* prev; TiXmlNode* prev;
TiXmlNode* next; TiXmlNode* next;
private:
TiXmlNode( const TiXmlNode& ); // not implemented.
void operator=( const TiXmlNode& base ); // not allowed.
}; };
@ -977,6 +980,18 @@ public:
*/ */
const char* Attribute( const char* name, double* d ) const; const char* Attribute( const char* name, double* d ) const;
template<typename T = std::string>
inline T readType(const std::string& str) const { return stdext::unsafe_cast<T>(Attribute(str)); }
Position readPos(const std::string& base = std::string()) const
{
return Position(readType<uint16>(base + "x"), readType<uint16>(base + "y"), readType<uint8>(base + "z"));
}
Point readPoint() const
{
return Point(readType<int>("x"), readType<int>("y"));
}
/** QueryIntAttribute examines the attribute - it is an alternative to the /** QueryIntAttribute examines the attribute - it is an alternative to the
Attribute() method with richer error checking. Attribute() method with richer error checking.
If the attribute is an integer, it is stored in 'value' and If the attribute is an integer, it is stored in 'value' and

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

@ -42,29 +42,21 @@ void House::setTile(const TilePtr& tile)
m_tiles.push_back(tile); m_tiles.push_back(tile);
} }
#define fugly_get(attrib, type) stdext::unsafe_cast<type>(elem->Attribute((attrib)))
void House::load(const TiXmlElement *elem) void House::load(const TiXmlElement *elem)
{ {
std::string name = elem->Attribute("name"); std::string name = elem->Attribute("name");
if(name.empty()) if(name.empty())
name = stdext::format("UnNamed house #%u", getId()); name = stdext::format("UnNamed house #%u", getId());
uint32 rent = fugly_get("rent", uint32); m_rent = elem->readType<uint32>("rent");
m_rent = rent; m_size = elem->readType<uint32>("size");
uint32 townId = fugly_get("townid", uint32); uint32 townId = elem->readType<uint32>("townid");
if(!g_map.getTown(townId)) if(!g_map.getTown(townId))
stdext::throw_exception(stdext::format("invalid town id for house %d", townId)); stdext::throw_exception(stdext::format("invalid town id for house %d", townId));
uint32 size = fugly_get("size", uint32); m_isGuildHall = elem->readType<bool>("rent");
if(size == 0) addDoor(0, elem->readPos());
size = 1;
m_size = size;
m_isGuildHall = fugly_get("guildhall", bool);
addDoor(0, Position(fugly_get("entryx", uint16), fugly_get("entryy", uint16),
fugly_get("entryz", uint8)));
} }
void Houses::addHouse(const HousePtr& house) void Houses::addHouse(const HousePtr& house)
@ -103,7 +95,7 @@ void Houses::load(const std::string& fileName)
if(elem->ValueTStr() != "house") if(elem->ValueTStr() != "house")
stdext::throw_exception("invalid house tag."); stdext::throw_exception("invalid house tag.");
uint32 houseId = fugly_get("houseid", uint32); uint32 houseId = elem->readType<uint32>("houseid");
HousePtr house = getHouse(houseId); HousePtr house = getHouse(houseId);
if(!house) if(!house)
house = HousePtr(new House(houseId)), addHouse(house); house = HousePtr(new House(houseId)), addHouse(house);

@ -378,7 +378,6 @@ void Map::saveOtbm(const std::string &fileName)
void Map::loadSpawns(const std::string &fileName) void Map::loadSpawns(const std::string &fileName)
{ {
#define cast(NAME, TYPE, NODE) stdext::unsafe_cast<TYPE>(NODE->Attribute((NAME)))
if(!m_monsters.isLoaded()) if(!m_monsters.isLoaded())
stdext::throw_exception("cannot load spawns; monsters aren't loaded."); stdext::throw_exception("cannot load spawns; monsters aren't loaded.");
@ -394,7 +393,7 @@ void Map::loadSpawns(const std::string &fileName)
if (node->ValueTStr() != "spawn") if (node->ValueTStr() != "spawn")
stdext::throw_exception("invalid spawn node"); stdext::throw_exception("invalid spawn node");
Position centerPos(cast("x", uint16, node), cast("y", uint16, node), cast("z", uint8, node)); Position centerPos = node->readPos("center");
for(TiXmlElement* mType = node->FirstChildElement(); mType; mType = mType->NextSiblingElement()) { for(TiXmlElement* mType = node->FirstChildElement(); mType; mType = mType->NextSiblingElement()) {
if (mType->ValueStr() != "monster") if (mType->ValueStr() != "monster")
stdext::throw_exception("invalid spawn-subnode"); stdext::throw_exception("invalid spawn-subnode");
@ -404,7 +403,7 @@ void Map::loadSpawns(const std::string &fileName)
if (!m) if (!m)
stdext::throw_exception(stdext::format("unkown monster %s", mName)); stdext::throw_exception(stdext::format("unkown monster %s", mName));
Point off(cast("x", int, mType), cast("y", int, mType)); Point off = mType->readPoint();
Position mPos(centerPos.x + off.x, centerPos.y + off.y, centerPos.z); Position mPos(centerPos.x + off.x, centerPos.y + off.y, centerPos.z);
addThing(m, mPos, -1); addThing(m, mPos, -1);
} }

@ -151,7 +151,7 @@ public:
// town/house/monster related // town/house/monster related
TownPtr getTown(uint32 tid) { return m_towns.getTown(tid); } TownPtr getTown(uint32 tid) { return m_towns.getTown(tid); }
HousePtr getHouse(uint32 hid) { return m_houses.getHouse(hid); } HousePtr getHouse(uint32 hid) { return m_houses.getHouse(hid); }
MonsterPtr getMonster(const std::string& name); MonsterPtr getMonster(const std::string &name);
void setLight(const Light& light) { m_light = light; } void setLight(const Light& light) { m_light = light; }
void setCentralPosition(const Position& centralPosition); void setCentralPosition(const Position& centralPosition);

@ -48,7 +48,6 @@ void Monsters::loadMonsters(const std::string& file)
void Monsters::loadSingleMonster(const std::string& file, const MonsterPtr& m) 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()) if (!m || std::find(m_monsters.begin(), m_monsters.end(), m) != m_monsters.end())
stdext::throw_exception("reloading monsters is not supported yet."); stdext::throw_exception("reloading monsters is not supported yet.");
@ -64,18 +63,18 @@ void Monsters::loadSingleMonster(const std::string& file, const MonsterPtr& m)
if(attrib->ValueStr() == "look") { if(attrib->ValueStr() == "look") {
Outfit out; Outfit out;
int type = read("type"); int type = attrib->readType<int>("type");
if (type <= 0) if (type <= 0)
type = read("typeex"); type = attrib->readType<int>("typeex");
if(type) { if(type) {
out.setId(type); out.setId(type);
{ {
out.setHead(read("head")); out.setHead(attrib->readType<int>(("head")));
out.setBody(read("body")); out.setBody(attrib->readType<int>(("body")));
out.setLegs(read("legs")); out.setLegs(attrib->readType<int>(("legs")));
out.setFeet(read("feet")); out.setFeet(attrib->readType<int>(("feet")));
out.setAddons(read("addons")); out.setAddons(attrib->readType<int>(("addons")));
out.setMount(read("mount")); out.setMount(attrib->readType<int>(("mount")));
} }
} else } else
stdext::throw_exception(stdext::format("invalid look type/typeex for monster %s", m->getName())); stdext::throw_exception(stdext::format("invalid look type/typeex for monster %s", m->getName()));
@ -90,14 +89,12 @@ MonsterPtr Monsters::getMonster(const std::string& name)
{ {
auto it = std::find_if(m_monsters.begin(), m_monsters.end(), auto it = std::find_if(m_monsters.begin(), m_monsters.end(),
[=] (const MonsterPtr& m) -> bool { return m->getName() == name; }); [=] (const MonsterPtr& m) -> bool { return m->getName() == name; });
if (it != m_monsters.end()) return it != m_monsters.end() ? *it : nullptr;
return *it;
return nullptr;
} }
MonsterPtr Monsters::getMonsterByPos(const Position& pos) MonsterPtr Monsters::getMonsterByPos(const Position& pos)
{ {
auto it = std::find_if(m_monsters.begin(), m_monsters.end(), auto it = std::find_if(m_monsters.begin(), m_monsters.end(),
[=] (const MonsterPtr& m) -> bool { return m->getPos() == pos; }); [=] (const MonsterPtr& m) -> bool { return m->getPosition() == pos; });
return it != m_monsters.end() ? *it : nullptr; return it != m_monsters.end() ? *it : nullptr;
} }

Loading…
Cancel
Save