Guest User

Untitled

a guest
Sep 22nd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 21.32 KB | None | 0 0
  1. /**
  2.  * Tibia GIMUD Server - a free and open-source MMORPG server emulator
  3.  * Copyright (C) 2017  Alejandro Mujica <alejandrodemujica@gmail.com>
  4.  *
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or
  8.  * (at your option) any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  */
  19.  
  20. #ifndef FS_ITEM_H_009A319FB13D477D9EEFFBBD9BB83562
  21. #define FS_ITEM_H_009A319FB13D477D9EEFFBBD9BB83562
  22.  
  23. #include "cylinder.h"
  24. #include "thing.h"
  25. #include "items.h"
  26.  
  27. #include <deque>
  28.  
  29. class Creature;
  30. class Player;
  31. class Container;
  32. class Depot;
  33. class Teleport;
  34. class Mailbox;
  35. class Door;
  36. class MagicField;
  37. class BedItem;
  38.  
  39. enum ITEMPROPERTY {
  40.     CONST_PROP_BLOCKSOLID = 0,
  41.     CONST_PROP_HASHEIGHT,
  42.     CONST_PROP_BLOCKPROJECTILE,
  43.     CONST_PROP_BLOCKPATH,
  44.     CONST_PROP_ISVERTICAL,
  45.     CONST_PROP_ISHORIZONTAL,
  46.     CONST_PROP_MOVEABLE,
  47.     CONST_PROP_IMMOVABLEBLOCKSOLID,
  48.     CONST_PROP_IMMOVABLEBLOCKPATH,
  49.     CONST_PROP_IMMOVABLENOFIELDBLOCKPATH,
  50.     CONST_PROP_NOFIELDBLOCKPATH,
  51.     CONST_PROP_SUPPORTHANGABLE,
  52.     CONST_PROP_UNLAY,
  53. };
  54.  
  55. enum TradeEvents_t {
  56.     ON_TRADE_TRANSFER,
  57.     ON_TRADE_CANCEL,
  58. };
  59.  
  60. enum ItemDecayState_t : uint8_t {
  61.     DECAYING_FALSE = 0,
  62.     DECAYING_TRUE,
  63.     DECAYING_PENDING,
  64. };
  65.  
  66. enum AttrTypes_t {
  67.     //ATTR_DESCRIPTION = 1,
  68.     //ATTR_EXT_FILE = 2,
  69.     ATTR_TILE_FLAGS = 3,
  70.     ATTR_ACTION_ID = 4,
  71.     ATTR_MOVEMENT_ID = 5,
  72.     ATTR_TEXT = 6,
  73.     ATTR_DESC = 7,
  74.     ATTR_TELE_DEST = 8,
  75.     ATTR_ITEM = 9,
  76.     ATTR_DEPOT_ID = 10,
  77.     //ATTR_EXT_SPAWN_FILE = 11,
  78.     ATTR_RUNE_CHARGES = 12,
  79.     //ATTR_EXT_HOUSE_FILE = 13,
  80.     ATTR_HOUSEDOORID = 14,
  81.     ATTR_COUNT = 15,
  82.     ATTR_DURATION = 16,
  83.     ATTR_DECAYING_STATE = 17,
  84.     ATTR_WRITTENDATE = 18,
  85.     ATTR_WRITTENBY = 19,
  86.     ATTR_SLEEPERGUID = 20,
  87.     ATTR_SLEEPSTART = 21,
  88.     ATTR_CHARGES = 22,
  89.     ATTR_KEYNUMBER = 23,
  90.     ATTR_KEYHOLENUMBER = 24,
  91.     ATTR_DOORQUESTNUMBER = 25,
  92.     ATTR_DOORQUESTVALUE = 26,
  93.     ATTR_DOORLEVEL = 27,
  94.     ATTR_CHESTQUESTNUMBER = 28,
  95.     // add non-OTBM attributes after here
  96.     ATTR_CONTAINER_ITEMS = 29,
  97.     ATTR_NAME = 30,
  98.     ATTR_ARTICLE = 31,
  99.     ATTR_PLURALNAME = 32,
  100.     ATTR_WEIGHT = 33,
  101.     ATTR_ATTACK = 34,
  102.     ATTR_DEFENSE = 35,
  103.     ATTR_ARMOR = 36,
  104.     ATTR_SHOOTRANGE = 37,
  105. };
  106.  
  107. enum Attr_ReadValue {
  108.     ATTR_READ_CONTINUE,
  109.     ATTR_READ_ERROR,
  110.     ATTR_READ_END,
  111. };
  112.  
  113. class ItemAttributes
  114. {
  115.     public:
  116.         ItemAttributes() = default;
  117.  
  118.         void setSpecialDescription(const std::string& desc) {
  119.             setStrAttr(ITEM_ATTRIBUTE_DESCRIPTION, desc);
  120.         }
  121.         const std::string& getSpecialDescription() const {
  122.             return getStrAttr(ITEM_ATTRIBUTE_DESCRIPTION);
  123.         }
  124.  
  125.         void setText(const std::string& text) {
  126.             setStrAttr(ITEM_ATTRIBUTE_TEXT, text);
  127.         }
  128.         void resetText() {
  129.             removeAttribute(ITEM_ATTRIBUTE_TEXT);
  130.         }
  131.         const std::string& getText() const {
  132.             return getStrAttr(ITEM_ATTRIBUTE_TEXT);
  133.         }
  134.  
  135.         void setDate(int32_t n) {
  136.             setIntAttr(ITEM_ATTRIBUTE_DATE, n);
  137.         }
  138.         void resetDate() {
  139.             removeAttribute(ITEM_ATTRIBUTE_DATE);
  140.         }
  141.         time_t getDate() const {
  142.             return static_cast<time_t>(getIntAttr(ITEM_ATTRIBUTE_DATE));
  143.         }
  144.  
  145.         void setWriter(const std::string& writer) {
  146.             setStrAttr(ITEM_ATTRIBUTE_WRITER, writer);
  147.         }
  148.         void resetWriter() {
  149.             removeAttribute(ITEM_ATTRIBUTE_WRITER);
  150.         }
  151.         const std::string& getWriter() const {
  152.             return getStrAttr(ITEM_ATTRIBUTE_WRITER);
  153.         }
  154.  
  155.         void setActionId(uint16_t n) {
  156.             setIntAttr(ITEM_ATTRIBUTE_ACTIONID, n);
  157.         }
  158.         uint16_t getActionId() const {
  159.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_ACTIONID));
  160.         }
  161.  
  162.         void setMovementID(uint16_t n) {
  163.             setIntAttr(ITEM_ATTRIBUTE_MOVEMENTID, n);
  164.         }
  165.         uint16_t getMovementId() const {
  166.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_MOVEMENTID));
  167.         }
  168.  
  169.         void setKeyNumber(uint16_t n) {
  170.             setIntAttr(ITEM_ATTRIBUTE_KEYNUMBER, n);
  171.         }
  172.         uint16_t getKeyNumber() const {
  173.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_KEYNUMBER));
  174.         }
  175.  
  176.         void setKeyHoleNumber(uint16_t n) {
  177.             setIntAttr(ITEM_ATTRIBUTE_KEYHOLENUMBER, n);
  178.         }
  179.         uint16_t getKeyHoleNumber() const {
  180.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_KEYHOLENUMBER));
  181.         }
  182.  
  183.         void setDoorQuestNumber(uint16_t n) {
  184.             setIntAttr(ITEM_ATTRIBUTE_DOORQUESTNUMBER, n);
  185.         }
  186.         uint16_t getDoorQuestNumber() const {
  187.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_DOORQUESTNUMBER));
  188.         }
  189.  
  190.         void setDoorQuestValue(uint16_t n) {
  191.             setIntAttr(ITEM_ATTRIBUTE_DOORQUESTVALUE, n);
  192.         }
  193.         uint16_t getDoorQuestValue() const {
  194.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_DOORQUESTVALUE));
  195.         }
  196.  
  197.         void setDoorLevel(uint16_t n) {
  198.             setIntAttr(ITEM_ATTRIBUTE_DOORLEVEL, n);
  199.         }
  200.         uint16_t getDoorLevel() const {
  201.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_DOORLEVEL));
  202.         }
  203.  
  204.         void setChestQuestNumber(uint16_t n) {
  205.             setIntAttr(ITEM_ATTRIBUTE_CHESTQUESTNUMBER, n);
  206.         }
  207.         uint16_t getChestQuestNumber() const {
  208.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_CHESTQUESTNUMBER));
  209.         }
  210.  
  211.         void setCharges(uint16_t n) {
  212.             setIntAttr(ITEM_ATTRIBUTE_CHARGES, n);
  213.         }
  214.         uint16_t getCharges() const {
  215.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_CHARGES));
  216.         }
  217.  
  218.         void setFluidType(uint16_t n) {
  219.             setIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE, n);
  220.         }
  221.         uint16_t getFluidType() const {
  222.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE));
  223.         }
  224.  
  225.         void setOwner(uint32_t owner) {
  226.             setIntAttr(ITEM_ATTRIBUTE_OWNER, owner);
  227.         }
  228.         uint32_t getOwner() const {
  229.             return getIntAttr(ITEM_ATTRIBUTE_OWNER);
  230.         }
  231.  
  232.         void setDuration(int32_t time) {
  233.             setIntAttr(ITEM_ATTRIBUTE_DURATION, time);
  234.         }
  235.         void decreaseDuration(int32_t time) {
  236.             increaseIntAttr(ITEM_ATTRIBUTE_DURATION, -time);
  237.         }
  238.         uint32_t getDuration() const {
  239.             return getIntAttr(ITEM_ATTRIBUTE_DURATION);
  240.         }
  241.  
  242.         void setDecaying(ItemDecayState_t decayState) {
  243.             setIntAttr(ITEM_ATTRIBUTE_DECAYSTATE, decayState);
  244.         }
  245.         ItemDecayState_t getDecaying() const {
  246.             return static_cast<ItemDecayState_t>(getIntAttr(ITEM_ATTRIBUTE_DECAYSTATE));
  247.         }
  248.  
  249.     protected:
  250.         inline bool hasAttribute(itemAttrTypes type) const {
  251.             return (type & attributeBits) != 0;
  252.         }
  253.         void removeAttribute(itemAttrTypes type);
  254.  
  255.         static std::string emptyString;
  256.  
  257.         struct Attribute
  258.         {
  259.             union {
  260.                 int64_t integer;
  261.                 std::string* string;
  262.             } value;
  263.             itemAttrTypes type;
  264.  
  265.             explicit Attribute(itemAttrTypes type) : type(type) {
  266.                 memset(&value, 0, sizeof(value));
  267.             }
  268.             Attribute(const Attribute& i) {
  269.                 type = i.type;
  270.                 if (ItemAttributes::isIntAttrType(type)) {
  271.                     value.integer = i.value.integer;
  272.                 } else if (ItemAttributes::isStrAttrType(type)) {
  273.                     value.string = new std::string(*i.value.string);
  274.                 } else {
  275.                     memset(&value, 0, sizeof(value));
  276.                 }
  277.             }
  278.             Attribute(Attribute&& attribute) : value(attribute.value), type(attribute.type) {
  279.                 memset(&attribute.value, 0, sizeof(value));
  280.                 attribute.type = ITEM_ATTRIBUTE_NONE;
  281.             }
  282.             ~Attribute() {
  283.                 if (ItemAttributes::isStrAttrType(type)) {
  284.                     delete value.string;
  285.                 }
  286.             }
  287.             Attribute& operator=(Attribute other) {
  288.                 Attribute::swap(*this, other);
  289.                 return *this;
  290.             }
  291.             Attribute& operator=(Attribute&& other) {
  292.                 if (this != &other) {
  293.                     if (ItemAttributes::isStrAttrType(type)) {
  294.                         delete value.string;
  295.                     }
  296.  
  297.                     value = other.value;
  298.                     type = other.type;
  299.  
  300.                     memset(&other.value, 0, sizeof(value));
  301.                     other.type = ITEM_ATTRIBUTE_NONE;
  302.                 }
  303.                 return *this;
  304.             }
  305.  
  306.             static void swap(Attribute& first, Attribute& second) {
  307.                 std::swap(first.value, second.value);
  308.                 std::swap(first.type, second.type);
  309.             }
  310.         };
  311.  
  312.         std::forward_list<Attribute> attributes;
  313.         uint32_t attributeBits = 0;
  314.  
  315.         const std::string& getStrAttr(itemAttrTypes type) const;
  316.         void setStrAttr(itemAttrTypes type, const std::string& value);
  317.  
  318.         int64_t getIntAttr(itemAttrTypes type) const;
  319.         void setIntAttr(itemAttrTypes type, int64_t value);
  320.         void increaseIntAttr(itemAttrTypes type, int64_t value);
  321.  
  322.         const Attribute* getExistingAttr(itemAttrTypes type) const;
  323.         Attribute& getAttr(itemAttrTypes type);
  324.  
  325.     public:
  326.         inline static bool isIntAttrType(itemAttrTypes type) {
  327.             return (type & 0xFFFFE13) != 0;
  328.         }
  329.         inline static bool isStrAttrType(itemAttrTypes type) {
  330.             return (type & 0x1EC) != 0;
  331.         }
  332.  
  333.         const std::forward_list<Attribute>& getList() const {
  334.             return attributes;
  335.         }
  336.  
  337.     friend class Item;
  338. };
  339.  
  340. class Item : virtual public Thing
  341. {
  342.     public:
  343.         //Factory member to create item of right type based on type
  344.         static Item* CreateItem(const uint16_t type, uint16_t count = 0);
  345.         static Container* CreateItemAsContainer(const uint16_t type, uint16_t size);
  346.         static Item* CreateItem(PropStream& propStream);
  347.         static Items items;
  348.  
  349.         // Constructor for items
  350.         Item(const uint16_t type, uint16_t count = 0);
  351.         Item(const Item& i);
  352.         virtual Item* clone() const;
  353.  
  354.         virtual ~Item() = default;
  355.  
  356.         // non-assignable
  357.         Item& operator=(const Item&) = delete;
  358.  
  359.         bool equals(const Item* otherItem) const;
  360.  
  361.         Item* getItem() final {
  362.             return this;
  363.         }
  364.         const Item* getItem() const final {
  365.             return this;
  366.         }
  367.         virtual Teleport* getTeleport() {
  368.             return nullptr;
  369.         }
  370.         virtual const Teleport* getTeleport() const {
  371.             return nullptr;
  372.         }
  373.         virtual Mailbox* getMailbox() {
  374.             return nullptr;
  375.         }
  376.         virtual const Mailbox* getMailbox() const {
  377.             return nullptr;
  378.         }
  379.         virtual Door* getDoor() {
  380.             return nullptr;
  381.         }
  382.         virtual const Door* getDoor() const {
  383.             return nullptr;
  384.         }
  385.         virtual MagicField* getMagicField() {
  386.             return nullptr;
  387.         }
  388.         virtual const MagicField* getMagicField() const {
  389.             return nullptr;
  390.         }
  391.         virtual BedItem* getBed() {
  392.             return nullptr;
  393.         }
  394.         virtual const BedItem* getBed() const {
  395.             return nullptr;
  396.         }
  397.  
  398.         const std::string& getStrAttr(itemAttrTypes type) const {
  399.             if (!attributes) {
  400.                 return ItemAttributes::emptyString;
  401.             }
  402.             return attributes->getStrAttr(type);
  403.         }
  404.         void setStrAttr(itemAttrTypes type, const std::string& value) {
  405.             getAttributes()->setStrAttr(type, value);
  406.         }
  407.  
  408.         int32_t getIntAttr(itemAttrTypes type) const {
  409.             if (!attributes) {
  410.                 return 0;
  411.             }
  412.             return attributes->getIntAttr(type);
  413.         }
  414.         void setIntAttr(itemAttrTypes type, int32_t value) {
  415.             getAttributes()->setIntAttr(type, value);
  416.         }
  417.         void increaseIntAttr(itemAttrTypes type, int32_t value) {
  418.             getAttributes()->increaseIntAttr(type, value);
  419.         }
  420.  
  421.         void removeAttribute(itemAttrTypes type) {
  422.             if (attributes) {
  423.                 attributes->removeAttribute(type);
  424.             }
  425.         }
  426.         bool hasAttribute(itemAttrTypes type) const {
  427.             if (!attributes) {
  428.                 return false;
  429.             }
  430.             return attributes->hasAttribute(type);
  431.         }
  432.  
  433.         void setSpecialDescription(const std::string& desc) {
  434.             setStrAttr(ITEM_ATTRIBUTE_DESCRIPTION, desc);
  435.         }
  436.         const std::string& getSpecialDescription() const {
  437.             return getStrAttr(ITEM_ATTRIBUTE_DESCRIPTION);
  438.         }
  439.  
  440.         void setText(const std::string& text) {
  441.             setStrAttr(ITEM_ATTRIBUTE_TEXT, text);
  442.         }
  443.         void resetText() {
  444.             removeAttribute(ITEM_ATTRIBUTE_TEXT);
  445.         }
  446.         const std::string& getText() const {
  447.             return getStrAttr(ITEM_ATTRIBUTE_TEXT);
  448.         }
  449.  
  450.         void setDate(int32_t n) {
  451.             setIntAttr(ITEM_ATTRIBUTE_DATE, n);
  452.         }
  453.         void resetDate() {
  454.             removeAttribute(ITEM_ATTRIBUTE_DATE);
  455.         }
  456.         time_t getDate() const {
  457.             return static_cast<time_t>(getIntAttr(ITEM_ATTRIBUTE_DATE));
  458.         }
  459.  
  460.         void setWriter(const std::string& writer) {
  461.             setStrAttr(ITEM_ATTRIBUTE_WRITER, writer);
  462.         }
  463.         void resetWriter() {
  464.             removeAttribute(ITEM_ATTRIBUTE_WRITER);
  465.         }
  466.         const std::string& getWriter() const {
  467.             return getStrAttr(ITEM_ATTRIBUTE_WRITER);
  468.         }
  469.  
  470.         void setActionId(uint16_t n) {
  471.             setIntAttr(ITEM_ATTRIBUTE_ACTIONID, n);
  472.         }
  473.         uint16_t getActionId() const {
  474.             if (!attributes) {
  475.                 return 0;
  476.             }
  477.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_ACTIONID));
  478.         }
  479.  
  480.         void setMovementID(uint16_t n) {
  481.             setIntAttr(ITEM_ATTRIBUTE_MOVEMENTID, n);
  482.         }
  483.         uint16_t getMovementId() const {
  484.             if (!attributes) {
  485.                 return 0;
  486.             }
  487.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_MOVEMENTID));
  488.         }
  489.  
  490.         void setCharges(uint16_t n) {
  491.             setIntAttr(ITEM_ATTRIBUTE_CHARGES, n);
  492.         }
  493.         uint16_t getCharges() const {
  494.             if (!attributes) {
  495.                 return 0;
  496.             }
  497.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_CHARGES));
  498.         }
  499.  
  500.         void setFluidType(uint16_t n) {
  501.             setIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE, n);
  502.         }
  503.         uint16_t getFluidType() const {
  504.             if (!attributes) {
  505.                 return 0;
  506.             }
  507.             return static_cast<uint16_t>(getIntAttr(ITEM_ATTRIBUTE_FLUIDTYPE));
  508.         }
  509.  
  510.         void setOwner(uint32_t owner) {
  511.             setIntAttr(ITEM_ATTRIBUTE_OWNER, owner);
  512.         }
  513.         uint32_t getOwner() const {
  514.             if (!attributes) {
  515.                 return 0;
  516.             }
  517.             return getIntAttr(ITEM_ATTRIBUTE_OWNER);
  518.         }
  519.  
  520.         void setCorpseOwner(uint32_t corpseOwner) {
  521.             setIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER, corpseOwner);
  522.         }
  523.         uint32_t getCorpseOwner() const {
  524.             if (!attributes) {
  525.                 return 0;
  526.             }
  527.             return getIntAttr(ITEM_ATTRIBUTE_CORPSEOWNER);
  528.         }
  529.  
  530.         void setDuration(int32_t time) {
  531.             setIntAttr(ITEM_ATTRIBUTE_DURATION, time);
  532.         }
  533.         void decreaseDuration(int32_t time) {
  534.             increaseIntAttr(ITEM_ATTRIBUTE_DURATION, -time);
  535.         }
  536.         uint32_t getDuration() const {
  537.             if (!attributes) {
  538.                 return 0;
  539.             }
  540.             return getIntAttr(ITEM_ATTRIBUTE_DURATION);
  541.         }
  542.  
  543.         void setDecaying(ItemDecayState_t decayState) {
  544.             setIntAttr(ITEM_ATTRIBUTE_DECAYSTATE, decayState);
  545.         }
  546.         ItemDecayState_t getDecaying() const {
  547.             if (!attributes) {
  548.                 return DECAYING_FALSE;
  549.             }
  550.             return static_cast<ItemDecayState_t>(getIntAttr(ITEM_ATTRIBUTE_DECAYSTATE));
  551.         }
  552.  
  553.         static std::string getDescription(const ItemType& it, int32_t lookDistance, const Item* item = nullptr, int32_t subType = -1, bool addArticle = true);
  554.         static std::string getNameDescription(const ItemType& it, const Item* item = nullptr, int32_t subType = -1, bool addArticle = true);
  555.         static std::string getWeightDescription(const ItemType& it, uint32_t weight, uint32_t count = 1);
  556.  
  557.         std::string getDescription(int32_t lookDistance) const final;
  558.         std::string getNameDescription() const;
  559.         std::string getWeightDescription() const;
  560.  
  561.         //serialization
  562.         virtual Attr_ReadValue readAttr(AttrTypes_t attr, PropStream& propStream);
  563.         bool unserializeAttr(PropStream& propStream);
  564.         virtual bool unserializeItemNode(FileLoader& f, NODE node, PropStream& propStream);
  565.  
  566.         virtual void serializeAttr(PropWriteStream& propWriteStream) const;
  567.  
  568.         bool isPushable() const final {
  569.             return isMoveable();
  570.         }
  571.         int32_t getThrowRange() const final {
  572.             return (isPickupable() ? 15 : 2);
  573.         }
  574.  
  575.         uint16_t getID() const {
  576.             return id;
  577.         }
  578.         void setID(uint16_t newid);
  579.  
  580.         // Returns the player that is holding this item in his inventory
  581.         Player* getHoldingPlayer() const;
  582.  
  583.         CombatType_t getDamageType() const {
  584.             return items[id].damageType;
  585.         }
  586.         CombatType_t getCombatType() const {
  587.             return items[id].combatType;
  588.         }
  589.         WeaponType_t getWeaponType() const {
  590.             return items[id].weaponType;
  591.         }
  592.         Ammo_t getAmmoType() const {
  593.             return items[id].ammoType;
  594.         }
  595.         uint8_t getShootRange() const {
  596.             if (hasAttribute(ITEM_ATTRIBUTE_SHOOTRANGE)) {
  597.                 return getIntAttr(ITEM_ATTRIBUTE_SHOOTRANGE);
  598.             }
  599.             return items[id].shootRange;
  600.         }
  601.         uint8_t getMissileType() const {
  602.             return items[id].shootType;
  603.         }
  604.         uint8_t getFragility() const {
  605.             return items[id].fragility;
  606.         }
  607.  
  608.         int32_t getAttackStrength() const {
  609.             return items[id].attackStrength;
  610.         }
  611.         int32_t getAttackVariation() const {
  612.             return items[id].attackVariation;
  613.         }
  614.         int32_t getManaConsumption() const {
  615.             return items[id].manaConsumption;
  616.         }
  617.         uint32_t getMinimumLevel() const {
  618.             return items[id].minReqLevel;
  619.         }
  620.         int32_t getWeaponSpecialEffect() const {
  621.             return items[id].weaponSpecialEffect;
  622.         }
  623.         virtual uint32_t getWeight() const;
  624.         uint32_t getBaseWeight() const {
  625.             if (hasAttribute(ITEM_ATTRIBUTE_WEIGHT)) {
  626.                 return getIntAttr(ITEM_ATTRIBUTE_WEIGHT);
  627.             }
  628.             return items[id].weight;
  629.         }
  630.         int32_t getAttack() const {
  631.             if (hasAttribute(ITEM_ATTRIBUTE_ATTACK)) {
  632.                 return getIntAttr(ITEM_ATTRIBUTE_ATTACK);
  633.             }
  634.             return items[id].attack;
  635.         }
  636.         int32_t getArmor() const {
  637.             if (hasAttribute(ITEM_ATTRIBUTE_ARMOR)) {
  638.                 return getIntAttr(ITEM_ATTRIBUTE_ARMOR);
  639.             }
  640.             return items[id].armor;
  641.         }
  642.         int32_t getDefense() const {
  643.             if (hasAttribute(ITEM_ATTRIBUTE_DEFENSE)) {
  644.                 return getIntAttr(ITEM_ATTRIBUTE_DEFENSE);
  645.             }
  646.             return items[id].defense;
  647.         }
  648.         int32_t getSlotPosition() const {
  649.             return items[id].slotPosition;
  650.         }
  651.         uint16_t getDisguiseId() const {
  652.             return items[id].disguiseId;
  653.         }
  654.  
  655.         uint32_t getWorth() const;
  656.         void getLight(LightInfo& lightInfo) const;
  657.  
  658.         bool hasProperty(ITEMPROPERTY prop) const;
  659.         bool isBlocking() const {
  660.             return items[id].blockSolid;
  661.         }
  662.         bool isStackable() const {
  663.             return items[id].stackable;
  664.         }
  665.         bool isAlwaysOnTop() const {
  666.             return items[id].alwaysOnTop;
  667.         }
  668.         bool isGroundTile() const {
  669.             return items[id].isGroundTile();
  670.         }
  671.         bool isMagicField() const {
  672.             return items[id].isMagicField();
  673.         }
  674.         bool isSplash() const {
  675.             return items[id].isSplash();
  676.         }
  677.         bool isMoveable() const {
  678.             return items[id].moveable;
  679.         }
  680.         bool isPickupable() const {
  681.             return items[id].pickupable;
  682.         }
  683.         bool isHangable() const {
  684.             return items[id].isHangable;
  685.         }
  686.         bool isRotatable() const {
  687.             const ItemType& it = items[id];
  688.             return it.rotatable && it.rotateTo;
  689.         }
  690.         bool isDisguised() const {
  691.             return items[id].disguise;
  692.         }
  693.         bool isChangeUse() const {
  694.             return items[id].changeUse;
  695.         }
  696.         bool isChestQuest() const {
  697.             return items[id].isChest();
  698.         }
  699.         bool hasCollisionEvent() const {
  700.             return items[id].collisionEvent;
  701.         }
  702.         bool hasSeparationEvent() const {
  703.             return items[id].separationEvent;
  704.         }
  705.         bool hasUseEvent() const {
  706.             return items[id].useEvent;
  707.         }
  708.         bool hasMultiUseEvent() const {
  709.             return items[id].multiUseEvent;
  710.         }
  711.         bool canDistUse() const {
  712.             return items[id].distUse;
  713.         }
  714.         bool isQuiver() const {
  715.             return items[id].isQuiver();
  716.         }
  717.         const std::string& getName() const {
  718.             if (hasAttribute(ITEM_ATTRIBUTE_NAME)) {
  719.                 return getStrAttr(ITEM_ATTRIBUTE_NAME);
  720.             }
  721.             return items[id].name;
  722.         }
  723.         const std::string getPluralName() const {
  724.             if (hasAttribute(ITEM_ATTRIBUTE_PLURALNAME)) {
  725.                 return getStrAttr(ITEM_ATTRIBUTE_PLURALNAME);
  726.             }
  727.             return items[id].getPluralName();
  728.         }
  729.         const std::string& getArticle() const {
  730.             if (hasAttribute(ITEM_ATTRIBUTE_ARTICLE)) {
  731.                 return getStrAttr(ITEM_ATTRIBUTE_ARTICLE);
  732.             }
  733.             return items[id].article;
  734.         }
  735.  
  736.         // get the number of items
  737.         uint16_t getItemCount() const {
  738.             return count;
  739.         }
  740.         void setItemCount(uint8_t n) {
  741.             count = n;
  742.         }
  743.  
  744.         static uint32_t countByType(const Item* i, int32_t subType);
  745.  
  746.         void setDefaultSubtype();
  747.         uint16_t getSubType() const;
  748.         void setSubType(uint16_t n);
  749.  
  750.         void setDefaultDuration() {
  751.             uint32_t duration = getDefaultDuration();
  752.             if (duration != 0) {
  753.                 setDuration(duration);
  754.             }
  755.         }
  756.         uint32_t getDefaultDuration() const {
  757.             return items[id].decayTime * 1000;
  758.         }
  759.         bool canDecay() const;
  760.  
  761.         virtual bool canRemove() const {
  762.             return true;
  763.         }
  764.         virtual bool canTransform() const {
  765.             return true;
  766.         }
  767.         virtual void onRemoved();
  768.         virtual void onTradeEvent(TradeEvents_t, Player*) {}
  769.  
  770.         virtual void startDecaying();
  771.  
  772.         void setLoadedFromMap(bool value) {
  773.             loadedFromMap = value;
  774.         }
  775.         bool isCleanable() const {
  776.             return !loadedFromMap && canRemove() && isPickupable() && !hasAttribute(ITEM_ATTRIBUTE_ACTIONID);
  777.         }
  778.  
  779.         std::unique_ptr<ItemAttributes>& getAttributes() {
  780.             if (!attributes) {
  781.                 attributes.reset(new ItemAttributes());
  782.             }
  783.             return attributes;
  784.         }
  785.  
  786.         void incrementReferenceCounter() {
  787.             ++referenceCounter;
  788.         }
  789.         void decrementReferenceCounter() {
  790.             if (--referenceCounter == 0) {
  791.                 delete this;
  792.             }
  793.         }
  794.  
  795.         Cylinder* getParent() const {
  796.             return parent;
  797.         }
  798.         void setParent(Cylinder* cylinder) {
  799.             parent = cylinder;
  800.         }
  801.         Cylinder* getTopParent();
  802.         const Cylinder* getTopParent() const;
  803.         Tile* getTile();
  804.         const Tile* getTile() const;
  805.         bool isRemoved() const {
  806.             return !parent || parent->isRemoved();
  807.         }
  808.  
  809.     protected:
  810.         std::string getWeightDescription(uint32_t weight) const;
  811.  
  812.         Cylinder* parent = nullptr;
  813.         std::unique_ptr<ItemAttributes> attributes;
  814.  
  815.         uint32_t referenceCounter = 0;
  816.  
  817.         uint16_t id;  // the same id as in ItemType
  818.         uint8_t count = 1; // number of stacked items
  819.  
  820.         bool loadedFromMap = false;
  821.  
  822.         //Don't add variables here, use the ItemAttribute class.
  823. };
  824.  
  825. typedef std::list<Item*> ItemList;
  826. typedef std::deque<Item*> ItemDeque;
  827.  
  828. inline uint32_t Item::countByType(const Item* i, int32_t subType)
  829. {
  830.     if (subType == -1 || subType == i->getSubType()) {
  831.         return i->getItemCount();
  832.     }
  833.  
  834.     return 0;
  835. }
  836.  
  837. #endif
Add Comment
Please, Sign In to add comment