Advertisement
Guest User

Untitled

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