Advertisement
Guest User

tile.h

a guest
Jun 12th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.79 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////
  2. // OpenTibia - an opensource roleplaying game
  3. ////////////////////////////////////////////////////////////////////////
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  16. ////////////////////////////////////////////////////////////////////////
  17.  
  18. #ifndef __TILE__
  19. #define __TILE__
  20. #include <boost/shared_ptr.hpp>
  21.  
  22. #include "cylinder.h"
  23. #include "item.h"
  24.  
  25. class Teleport;
  26. class TrashHolder;
  27. class Mailbox;
  28. class MagicField;
  29. class BedItem;
  30.  
  31. class Player;
  32. class Creature;
  33. class HouseTile;
  34. class QTreeLeafNode;
  35.  
  36. typedef std::list<Player*> PlayerList;
  37. typedef std::list<Creature*> SpectatorVec;
  38. typedef std::vector<Creature*> CreatureVector;
  39. typedef std::map<Position, boost::shared_ptr<SpectatorVec> > SpectatorCache;
  40.  
  41. enum tileflags_t
  42. {
  43.     TILESTATE_NONE = 0,
  44.     TILESTATE_PROTECTIONZONE = 1 << 0,
  45.     TILESTATE_TRASHED = 1 << 1,
  46.     TILESTATE_OPTIONALZONE = 1 << 2,
  47.     TILESTATE_NOLOGOUT = 1 << 3,
  48.     TILESTATE_HARDCOREZONE = 1 << 4,
  49.     TILESTATE_REFRESH = 1 << 5,
  50.  
  51.     //internal usage
  52.     TILESTATE_HOUSE = 1 << 6,
  53.     TILESTATE_FLOORCHANGE = 1 << 7,
  54.     TILESTATE_FLOORCHANGE_DOWN = 1 << 8,
  55.     TILESTATE_FLOORCHANGE_NORTH = 1 << 9,
  56.     TILESTATE_FLOORCHANGE_SOUTH = 1 << 10,
  57.     TILESTATE_FLOORCHANGE_EAST = 1 << 11,
  58.     TILESTATE_FLOORCHANGE_WEST = 1 << 12,
  59.     TILESTATE_FLOORCHANGE_NORTH_EX = 1 << 13,
  60.     TILESTATE_FLOORCHANGE_SOUTH_EX = 1 << 14,
  61.     TILESTATE_FLOORCHANGE_EAST_EX = 1 << 15,
  62.     TILESTATE_FLOORCHANGE_WEST_EX = 1 << 16,
  63.     TILESTATE_TELEPORT = 1 << 17,
  64.     TILESTATE_MAGICFIELD = 1 << 18,
  65.     TILESTATE_MAILBOX = 1 << 19,
  66.     TILESTATE_TRASHHOLDER = 1 << 20,
  67.     TILESTATE_BED = 1 << 21,
  68.     TILESTATE_DEPOT = 1 << 22,
  69.     TILESTATE_BLOCKSOLID = 1 << 23,
  70.     TILESTATE_BLOCKPATH = 1 << 24,
  71.     TILESTATE_IMMOVABLEBLOCKSOLID = 1 << 25,
  72.     TILESTATE_IMMOVABLEBLOCKPATH = 1 << 26,
  73.     TILESTATE_IMMOVABLENOFIELDBLOCKPATH = 1 << 27,
  74.     TILESTATE_NOFIELDBLOCKPATH = 1 << 28,
  75.     TILESTATE_DYNAMIC_TILE = 1 << 29
  76. };
  77.  
  78. enum ZoneType_t
  79. {
  80.     ZONE_PROTECTION,
  81.     ZONE_OPTIONAL,
  82.     ZONE_HARDCORE,
  83.     ZONE_NOLOGOUT,
  84.     ZONE_OPEN
  85. };
  86.  
  87. class TileItemVector
  88. {
  89.     public:
  90.         TileItemVector(): downItemCount(0) {}
  91.         virtual ~TileItemVector() {}
  92.  
  93.         ItemVector::iterator begin() {return items.begin();}
  94.         ItemVector::const_iterator begin() const {return items.begin();}
  95.         ItemVector::reverse_iterator rbegin() {return items.rbegin();}
  96.         ItemVector::const_reverse_iterator rbegin() const {return items.rbegin();}
  97.  
  98.         ItemVector::iterator end() {return items.end();}
  99.         ItemVector::const_iterator end() const {return items.end();}
  100.         ItemVector::reverse_iterator rend() {return items.rend();}
  101.         ItemVector::const_reverse_iterator rend() const {return items.rend();}
  102.  
  103.         size_t size() {return items.size();}
  104.         size_t size() const {return items.size();}
  105.         bool empty() {return items.empty();}
  106.         bool empty() const {return items.empty();}
  107.  
  108.         void push_back(Item* item) {items.push_back(item);}
  109.         void push_front(Item* item) {items.insert(items.begin(), item);}
  110.         ItemVector::iterator insert(ItemVector::iterator _where, Item* item) {return items.insert(_where, item);}
  111.         ItemVector::iterator erase(ItemVector::iterator _pos) {return items.erase(_pos);}
  112.  
  113.         Item* at(size_t _pos) {return items.at(_pos);}
  114.         Item* at(size_t _pos) const {return items.at(_pos);}
  115.         Item* back() {return items.back();}
  116.         const Item* back() const {return items.back();}
  117.  
  118.         ItemVector::iterator getBeginDownItem() {return items.begin();}
  119.         ItemVector::const_iterator getBeginDownItem() const {return items.begin();}
  120.         ItemVector::iterator getEndDownItem() {return items.begin() + downItemCount;}
  121.         ItemVector::const_iterator getEndDownItem() const {return items.begin() + downItemCount;}
  122.  
  123.         ItemVector::iterator getBeginTopItem() {return items.begin() + downItemCount;}
  124.         ItemVector::const_iterator getBeginTopItem() const {return items.begin() + downItemCount;}
  125.         ItemVector::iterator getEndTopItem() {return items.end();}
  126.         ItemVector::const_iterator getEndTopItem() const {return items.end();}
  127.  
  128.         uint32_t getTopItemCount() const {return std::distance(getBeginTopItem(), getEndTopItem());}
  129.         uint32_t getDownItemCount() const {return std::distance(getBeginDownItem(), getEndDownItem());}
  130.         Item* getTopTopItem();
  131.         Item* getTopDownItem();
  132.  
  133.     private:
  134.         ItemVector items;
  135.         uint16_t downItemCount;
  136.         friend class Tile;
  137. };
  138.  
  139. int32_t getHeight(); // elevation system
  140.  
  141. class Tile : public Cylinder
  142. {
  143.     public:
  144.         static Tile& nullTile;
  145.         Tile(uint16_t x, uint16_t y, uint16_t z);
  146.         virtual ~Tile();
  147.  
  148.         TileItemVector* getItemList();
  149.         const TileItemVector* getItemList() const;
  150.         TileItemVector* makeItemList();
  151.  
  152.         CreatureVector* getCreatures();
  153.         const CreatureVector* getCreatures() const;
  154.         CreatureVector* makeCreatures();
  155.  
  156.         HouseTile* getHouseTile();
  157.         const HouseTile* getHouseTile() const;
  158.         bool isHouseTile() const {return hasFlag(TILESTATE_HOUSE);}
  159.  
  160.         MagicField* getFieldItem() const;
  161.         Teleport* getTeleportItem() const;
  162.         TrashHolder* getTrashHolder() const;
  163.         Mailbox* getMailbox() const;
  164.         BedItem* getBedItem() const;
  165.  
  166.         Creature* getTopCreature();
  167.         Item* getTopTopItem();
  168.         Item* getTopDownItem();
  169.         bool isMoveableBlocking() const;
  170.         Thing* getTopVisibleThing(const Creature* creature);
  171.         Creature* getTopVisibleCreature(const Creature* creature);
  172.         const Creature* getTopVisibleCreature(const Creature* creature) const;
  173.         Item* getItemByTopOrder(uint32_t topOrder);
  174.  
  175.         uint32_t getThingCount() const {return thingCount;}
  176.         uint32_t getCreatureCount() const;
  177.         uint32_t getItemCount() const;
  178.         uint32_t getTopItemCount() const;
  179.         uint32_t getDownItemCount() const;
  180.  
  181.         bool hasProperty(enum ITEMPROPERTY prop) const;
  182.         bool hasProperty(Item* exclude, enum ITEMPROPERTY prop) const;
  183.  
  184.         bool hasFlag(tileflags_t flag) const {return ((m_flags & (uint32_t)flag) == (uint32_t)flag);}
  185.         void setFlag(tileflags_t flag) {m_flags |= (uint32_t)flag;}
  186.         void resetFlag(tileflags_t flag) {m_flags &= ~(uint32_t)flag;}
  187.  
  188.         bool positionChange() const {return hasFlag(TILESTATE_TELEPORT);}
  189.         bool floorChange(FloorChange_t change = CHANGE_NONE) const
  190.         {
  191.             switch(change)
  192.             {
  193.                 case CHANGE_DOWN:
  194.                     return hasFlag(TILESTATE_FLOORCHANGE_DOWN);
  195.                 case CHANGE_NORTH:
  196.                     return hasFlag(TILESTATE_FLOORCHANGE_NORTH);
  197.                 case CHANGE_SOUTH:
  198.                     return hasFlag(TILESTATE_FLOORCHANGE_SOUTH);
  199.                 case CHANGE_EAST:
  200.                     return hasFlag(TILESTATE_FLOORCHANGE_EAST);
  201.                 case CHANGE_WEST:
  202.                     return hasFlag(TILESTATE_FLOORCHANGE_WEST);
  203.                 case CHANGE_NORTH_EX:
  204.                     return hasFlag(TILESTATE_FLOORCHANGE_NORTH_EX);
  205.                 case CHANGE_SOUTH_EX:
  206.                     return hasFlag(TILESTATE_FLOORCHANGE_SOUTH_EX);
  207.                 case CHANGE_EAST_EX:
  208.                     return hasFlag(TILESTATE_FLOORCHANGE_EAST_EX);
  209.                 case CHANGE_WEST_EX:
  210.                     return hasFlag(TILESTATE_FLOORCHANGE_WEST_EX);
  211.                 case CHANGE_NONE:
  212.                     return hasFlag(TILESTATE_FLOORCHANGE);
  213.                 default:
  214.                     break;
  215.             }
  216.  
  217.             return false;
  218.         }
  219.  
  220.         ZoneType_t getZone() const
  221.         {
  222.             if(hasFlag(TILESTATE_PROTECTIONZONE))
  223.                 return ZONE_PROTECTION;
  224.  
  225.             if(hasFlag(TILESTATE_OPTIONALZONE))
  226.                 return ZONE_OPTIONAL;
  227.  
  228.             if(hasFlag(TILESTATE_HARDCOREZONE))
  229.                 return ZONE_HARDCORE;
  230.  
  231.             return ZONE_OPEN;
  232.         }
  233.  
  234.         bool hasHeight(uint32_t n) const;
  235.         void moveCreature(Creature* actor, Creature* creature, Cylinder* toCylinder, bool forceTeleport = false);
  236.         int32_t getClientIndexOfThing(const Player* player, const Thing* thing) const;
  237.  
  238.         //cylinder implementations
  239.         virtual Cylinder* getParent() {return NULL;}
  240.         virtual const Cylinder* getParent() const {return NULL;}
  241.         virtual bool isRemoved() const {return false;}
  242.         virtual Position getPosition() const {return pos;}
  243.         virtual Tile* getTile() {return this;}
  244.         virtual const Tile* getTile() const {return this;}
  245.         virtual Item* getItem() {return NULL;}
  246.         virtual const Item* getItem() const {return NULL;}
  247.         virtual Creature* getCreature() {return NULL;}
  248.         virtual const Creature* getCreature() const {return NULL;}
  249.  
  250.         virtual ReturnValue __queryAdd(int32_t index, const Thing* thing, uint32_t count,
  251.             uint32_t flags) const;
  252.         virtual ReturnValue __queryMaxCount(int32_t index, const Thing* thing, uint32_t count,
  253.             uint32_t& maxQueryCount, uint32_t flags) const;
  254.         virtual ReturnValue __queryRemove(const Thing* thing, uint32_t count, uint32_t flags) const;
  255.         virtual Cylinder* __queryDestination(int32_t& index, const Thing* thing, Item** destItem,
  256.             uint32_t& flags);
  257.  
  258.         virtual void __addThing(Creature* actor, Thing* thing) {__addThing(actor, 0, thing);}
  259.         virtual void __addThing(Creature* actor, int32_t index, Thing* thing);
  260.  
  261.         virtual void __updateThing(Thing* thing, uint16_t itemId, uint32_t count);
  262.         virtual void __replaceThing(uint32_t index, Thing* thing);
  263.  
  264.         virtual void __removeThing(Thing* thing, uint32_t count);
  265.  
  266.         virtual int32_t __getIndexOfThing(const Thing* thing) const;
  267.         virtual int32_t __getFirstIndex() const {return 0;}
  268.         virtual int32_t __getLastIndex() const {return thingCount;}
  269.         virtual uint32_t __getItemTypeCount(uint16_t itemId, int32_t subType = -1) const;
  270.         virtual Thing* __getThing(uint32_t index) const;
  271.  
  272.         virtual void postAddNotification(Creature* actor, Thing* thing, const Cylinder* oldParent,
  273.             int32_t index, cylinderlink_t link = LINK_OWNER);
  274.         virtual void postRemoveNotification(Creature* actor, Thing* thing, const Cylinder* newParent,
  275.             int32_t index, bool isCompleteRemoval, cylinderlink_t link = LINK_OWNER);
  276.  
  277.         virtual void __internalAddThing(Thing* thing) {__internalAddThing(0, thing);}
  278.         virtual void __internalAddThing(uint32_t index, Thing* thing);
  279.         void onUpdateTile();
  280.  
  281.     private:
  282.         void onAddTileItem(Item* item);
  283.         void onUpdateTileItem(Item* oldItem, const ItemType& oldType, Item* newItem, const ItemType& newType);
  284.         void onRemoveTileItem(const SpectatorVec& list, std::vector<int32_t>& oldStackPosVector, Item* item);
  285.  
  286.         void updateTileFlags(Item* item, bool remove);
  287.  
  288.     protected:
  289.         bool isDynamic() const {return (m_flags & TILESTATE_DYNAMIC_TILE);}
  290.  
  291.     public:
  292.         QTreeLeafNode* qt_node;
  293.         Item* ground;
  294.  
  295.     protected:
  296.         Position pos;
  297.         uint32_t m_flags, thingCount;
  298. };
  299.  
  300. // Used for walkable tiles, where there is high likeliness of
  301. // items being added/removed
  302. class DynamicTile : public Tile
  303. {
  304.     // By allocating the vectors in-house, we avoid some memory fragmentation
  305.     TileItemVector items;
  306.     CreatureVector creatures;
  307.     public:
  308.         DynamicTile(uint16_t x, uint16_t y, uint16_t z);
  309.         virtual ~DynamicTile();
  310.  
  311.         TileItemVector* getItemList() {return &items;}
  312.         const TileItemVector* getItemList() const {return &items;}
  313.         TileItemVector* makeItemList() {return &items;}
  314.  
  315.         CreatureVector* getCreatures() {return &creatures;}
  316.         const CreatureVector* getCreatures() const {return &creatures;}
  317.         CreatureVector* makeCreatures() {return &creatures;}
  318. };
  319.  
  320. // For blocking tiles, where we very rarely actually have items
  321. class StaticTile : public Tile
  322. {
  323.     // We very rarely even need the vectors, so don't keep them in memory
  324.     TileItemVector* items;
  325.     CreatureVector* creatures;
  326.     public:
  327.         StaticTile(uint16_t x, uint16_t y, uint16_t z);
  328.         virtual ~StaticTile();
  329.  
  330.         TileItemVector* getItemList() {return items;}
  331.         const TileItemVector* getItemList() const {return items;}
  332.         TileItemVector* makeItemList() {return (items) ? (items) : (items = new TileItemVector);}
  333.  
  334.         CreatureVector* getCreatures() {return creatures;}
  335.         const CreatureVector* getCreatures() const {return creatures;}
  336.         CreatureVector* makeCreatures() {return (creatures) ? (creatures) : (creatures = new CreatureVector);}
  337. };
  338.  
  339. inline Tile::Tile(uint16_t x, uint16_t y, uint16_t z): qt_node(NULL),
  340.     ground(NULL), pos(x, y, z), m_flags(0), thingCount(0) {}
  341.  
  342. inline Tile::~Tile() {}
  343.  
  344. inline CreatureVector* Tile::getCreatures()
  345. {
  346.     if(isDynamic())
  347.         return static_cast<DynamicTile*>(this)->DynamicTile::getCreatures();
  348.  
  349.     return static_cast<StaticTile*>(this)->StaticTile::getCreatures();
  350. }
  351.  
  352. inline const CreatureVector* Tile::getCreatures() const
  353. {
  354.     if(isDynamic())
  355.         return static_cast<const DynamicTile*>(this)->DynamicTile::getCreatures();
  356.  
  357.     return static_cast<const StaticTile*>(this)->StaticTile::getCreatures();
  358. }
  359.  
  360. inline CreatureVector* Tile::makeCreatures()
  361. {
  362.     if(isDynamic())
  363.         return static_cast<DynamicTile*>(this)->DynamicTile::makeCreatures();
  364.  
  365.     return static_cast<StaticTile*>(this)->StaticTile::makeCreatures();
  366. }
  367.  
  368. inline TileItemVector* Tile::getItemList()
  369. {
  370.     if(isDynamic())
  371.         return static_cast<DynamicTile*>(this)->DynamicTile::getItemList();
  372.  
  373.     return static_cast<StaticTile*>(this)->StaticTile::getItemList();
  374. }
  375.  
  376. inline const TileItemVector* Tile::getItemList() const
  377. {
  378.     if(isDynamic())
  379.         return static_cast<const DynamicTile*>(this)->DynamicTile::getItemList();
  380.  
  381.     return static_cast<const StaticTile*>(this)->StaticTile::getItemList();
  382. }
  383.  
  384. inline TileItemVector* Tile::makeItemList()
  385. {
  386.     if(isDynamic())
  387.         return static_cast<DynamicTile*>(this)->DynamicTile::makeItemList();
  388.  
  389.     return static_cast<StaticTile*>(this)->StaticTile::makeItemList();
  390. }
  391.  
  392. inline StaticTile::StaticTile(uint16_t x, uint16_t y, uint16_t z):
  393.     Tile(x, y, z), items(NULL), creatures(NULL) {}
  394.  
  395. inline StaticTile::~StaticTile() {}
  396.  
  397. inline DynamicTile::DynamicTile(uint16_t x, uint16_t y, uint16_t z):
  398.     Tile(x, y, z)
  399. {
  400.     m_flags |= TILESTATE_DYNAMIC_TILE;
  401. }
  402.  
  403. inline DynamicTile::~DynamicTile() {}
  404. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement