diff --git a/src/otclient/houses.cpp b/src/otclient/houses.cpp new file mode 100644 index 00000000..ea0af0c9 --- /dev/null +++ b/src/otclient/houses.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2010-2012 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 "map.h" + +Houses g_houses; + +House::House(uint32 hId, const std::string &name, const Position &pos) + : m_id(hId), m_name(name) +{ + if (pos.isValid()) + m_doors.insert(std::make_pair(0, pos)); // first door +} + +void House::addDoor(uint16 doorId, const Position& pos) +{ + if (m_doors.find(doorId) == m_doors.end()) + m_doors.insert(std::make_pair(doorId, pos)); +} + +void House::setTile(const TilePtr& tile) +{ + tile->setFlags(TILESTATE_HOUSE); + if (std::find(m_tiles.begin(), m_tiles.end(), tile) == m_tiles.end()) + m_tiles.push_back(tile); +} + +#define fugly_get(attrib, type) stdext::unsafe_cast(elem->Attribute((attrib))) + +void House::load(const TiXmlElement *elem) +{ + std::string name = elem->Attribute("name"); + if (name.empty()) + name = stdext::format("UnNamed house #%u", getId()); + + uint32 rent = fugly_get("rent", uint32); + m_rent = rent; + + uint32 townId = fugly_get("townid", uint32); + if (!g_map.getTown(townId)) + stdext::throw_exception(stdext::format("invalid town id for house %d", townId)); + + uint32 size = fugly_get("size", uint32); + if (size == 0) + 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) +{ + if (findHouse(house->getId()) == m_houses.end()) + m_houses.push_back(house); +} + +void Houses::removeHouse(uint32 houseId) +{ + auto it = findHouse(houseId); + if (it != m_houses.end()) + m_houses.erase(it); +} + +HousePtr Houses::getHouse(uint32 houseId) +{ + auto it = findHouse(houseId); + if (it != m_houses.end()) + return *it; + + return nullptr; +} + +void Houses::load(const std::string& fileName) +{ + TiXmlDocument doc(fileName.c_str()); + if (!doc.LoadFile()) + stdext::throw_exception(stdext::format("failed to load '%s' (House XML)", fileName)); + + TiXmlElement *root = doc.FirstChildElement(); + if (!root || root->ValueTStr() != "houses") + stdext::throw_exception("invalid root tag name"); + + for (TiXmlElement *elem = root->FirstChildElement(); elem; elem = elem->NextSiblingElement()) { + if (elem->ValueTStr() != "house") + stdext::throw_exception("invalid house tag."); + + uint32 houseId = fugly_get("houseid", uint32); + HousePtr house = getHouse(houseId); + if (!house) + house = HousePtr(new House(houseId)), addHouse(house); + + house->load(elem); + } + + stdext::throw_exception("This has not been fully implemented yet."); +} + +HouseList::iterator Houses::findHouse(uint32 houseId) +{ + return std::find_if(m_houses.begin(), m_houses.end(), + [=] (const HousePtr& house) -> bool { return house->getId() == houseId; }); +} + diff --git a/src/otclient/houses.h b/src/otclient/houses.h new file mode 100644 index 00000000..0bff0c71 --- /dev/null +++ b/src/otclient/houses.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2010-2012 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 HOUSES_H +#define HOUSES_H + +#include "declarations.h" +#include "tile.h" + +#include + +class House : public LuaObject +{ +public: + House() { } + House(uint32 hId, const std::string& name = "", const Position& pos=Position()); + ~House() { m_tiles.clear(); m_doors.clear(); } + + void setId(uint32 hId) { m_id = hId; } + void setName(const std::string& name) { m_name = name; } + void addDoor(uint16 doorId, const Position& pos); + void setTile(const TilePtr& tile); + + uint32 getId() const { return m_id; } + std::string getName() const { return m_name; } + +protected: + void load(const TiXmlElement* elem); + void save(TiXmlElement &elem) { } // TODO + +private: + uint32 m_id, m_size, m_rent; + std::string m_name; + + std::map m_doors; + std::vector m_tiles; + Boolean m_isGuildHall; + + friend class Houses; +}; + +class Houses { +public: + void addHouse(const HousePtr& house); + void removeHouse(uint32 houseId); + void load(const std::string& fileName); + + HouseList houseList() const { return m_houses; } + HousePtr getHouse(uint32 houseId); + + // Fix to segfault on exit. + void clear() { m_houses.clear(); } + +private: + HouseList m_houses; + +protected: + HouseList::iterator findHouse(uint32 houseId); +}; + +#endif diff --git a/src/otclient/towns.cpp b/src/otclient/towns.cpp new file mode 100644 index 00000000..426b4ec8 --- /dev/null +++ b/src/otclient/towns.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2010-2012 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 "towns.h" + +Towns g_towns; + +Town::Town(uint32 tid, const std::string& name, const Position& pos) + : m_id(tid), m_name(name) +{ + if (pos.isValid()) + m_pos = pos; +} + + +void Towns::addTown(const TownPtr &town) +{ + if (findTown(town->getId()) == m_towns.end()) + m_towns.push_back(town); +} + +void Towns::removeTown(uint32 townId) +{ + auto it = findTown(townId); + if (it != m_towns.end()) + m_towns.erase(it); +} + +TownPtr Towns::getTown(uint32 townId) +{ + auto it = findTown(townId); + if (it != m_towns.end()) + return *it; + return nullptr; +} + +TownList::iterator Towns::findTown(uint32 townId) +{ + return std::find_if(m_towns.begin(), m_towns.end(), + [=] (const TownPtr& town) -> bool { return town->getId() == townId; }); +} diff --git a/src/otclient/towns.h b/src/otclient/towns.h new file mode 100644 index 00000000..979ad1e7 --- /dev/null +++ b/src/otclient/towns.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2010-2012 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 TOWNS_H +#define TOWNS_H + +#include "declarations.h" +#include + + +class Town : public LuaObject +{ +public: + Town() { } + Town(uint32 tid, const std::string& name, const Position& pos=Position()); + + void setId(uint32 tid) { m_id = tid; } + void setName(const std::string& name) { m_name = name; } + void setPos(const Position& pos) { m_pos = pos; } + + uint32 getId() { return m_id; } + std::string getName() { return m_name; } + Position getPos() { return m_pos; } + +private: + uint32 m_id; + std::string m_name; + Position m_pos; // temple pos +}; + +class Towns +{ +public: + void addTown(const TownPtr &town); + void removeTown(uint32 townId); + + TownPtr getTown(uint32 townId); + TownList getTowns() { return m_towns; } + + // Fix to segfault on exit + void clear() { m_towns.clear(); } + +private: + TownList m_towns; + +protected: + TownList::iterator findTown(uint32 townId); +}; + +#endif