TrinityCore
Loading...
Searching...
No Matches
Item.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef TRINITYCORE_ITEM_H
19#define TRINITYCORE_ITEM_H
20
21#include "Object.h"
22#include "Common.h"
23#include "DatabaseEnvFwd.h"
24#include "ItemDefines.h"
25#include "ItemEnchantmentMgr.h"
26#include "ItemTemplate.h"
27#include "Loot.h"
28
29class SpellInfo;
30class Bag;
31class Unit;
32
39
40#define MAX_GEM_SOCKETS MAX_ITEM_PROTO_SOCKETS// (BONUS_ENCHANTMENT_SLOT-SOCK_ENCHANTMENT_SLOT) and item proto size, equal value expected
41
43{
46 ENCHANTMENT_CHARGES_OFFSET = 2 // now here not only charges, but something new in wotlk
47};
48
49#define MAX_ENCHANTMENT_OFFSET 3
50
58
59bool ItemCanGoIntoBag(ItemTemplate const* proto, ItemTemplate const* pBagProto);
60
61class TC_GAME_API Item : public Object
62{
63 friend void AddItemToUpdateQueueOf(Item* item, Player* player);
64 friend void RemoveItemFromUpdateQueueOf(Item* item, Player* player);
65
66 public:
67 static Item* CreateItem(uint32 itemEntry, uint32 count, Player const* player = nullptr);
68 Item* CloneItem(uint32 count, Player const* player = nullptr) const;
69
70 Item();
71
72 virtual bool Create(ObjectGuid::LowType guidlow, uint32 itemId, Player const* owner);
73
74 std::string const& GetNameForLocaleIdx(LocaleConstant locale) const override;
75
76 ItemTemplate const* GetTemplate() const;
77
86 Player* GetOwner() const;
87
88 void SetExpiration(uint32 expiration) { SetUInt32Value(ITEM_FIELD_DURATION, expiration); }
89
91 bool HasItemFlag(ItemFieldFlags flag) const { return HasFlag(ITEM_FIELD_FLAGS, flag); }
95
96 bool IsSoulBound() const { return HasItemFlag(ITEM_FIELD_FLAG_SOULBOUND); }
97 bool IsBoundAccountWide() const { return GetTemplate()->HasFlag(ITEM_FLAG_IS_BOUND_TO_ACCOUNT); }
98 bool IsBindedNotWith(Player const* player) const;
99 bool IsBoundByEnchant() const;
100 virtual void SaveToDB(CharacterDatabaseTransaction trans);
101 virtual bool LoadFromDB(ObjectGuid::LowType guid, ObjectGuid owner_guid, Field* fields, uint32 entry);
102 static void DeleteFromDB(CharacterDatabaseTransaction trans, ObjectGuid::LowType itemGuid);
103 virtual void DeleteFromDB(CharacterDatabaseTransaction trans);
104 static void DeleteFromInventoryDB(CharacterDatabaseTransaction trans, ObjectGuid::LowType itemGuid);
105
106 void DeleteFromInventoryDB(CharacterDatabaseTransaction trans);
107 void SaveRefundDataToDB();
108 void DeleteRefundDataFromDB(CharacterDatabaseTransaction* trans);
109
110 Bag* ToBag() { if (IsBag()) return reinterpret_cast<Bag*>(this); else return nullptr; }
111 Bag const* ToBag() const { if (IsBag()) return reinterpret_cast<Bag const*>(this); else return nullptr; }
112
113 bool IsRefundable() const { return HasItemFlag(ITEM_FIELD_FLAG_REFUNDABLE); }
114 bool IsBOPTradeable() const { return HasItemFlag(ITEM_FIELD_FLAG_BOP_TRADEABLE); }
115 bool IsWrapped() const { return HasItemFlag(ITEM_FIELD_FLAG_WRAPPED); }
116 bool IsLocked() const { return !HasItemFlag(ITEM_FIELD_FLAG_UNLOCKED); }
117 bool IsBag() const { return GetTemplate()->GetInventoryType() == INVTYPE_BAG; }
118 bool IsCurrencyToken() const { return GetTemplate()->IsCurrencyToken(); }
119 bool IsNotEmptyBag() const;
121 void SetDurability(uint32 durability) { SetUInt32Value(ITEM_FIELD_DURABILITY, durability); }
122 void SetMaxDurability(uint32 maxDurability) { SetUInt32Value(ITEM_FIELD_MAXDURABILITY, maxDurability); }
123 bool CanBeTraded(bool mail = false, bool trade = false) const;
124 void SetInTrade(bool b = true) { mb_in_trade = b; }
125 bool IsInTrade() const { return mb_in_trade; }
126
127 uint32 CalculateDurabilityRepairCost(float discount, bool useRateConfig = true) const;
128
129 bool HasEnchantRequiredSkill(Player const* player) const;
130 uint32 GetEnchantRequiredLevel() const;
131
132 bool IsFitToSpellRequirements(SpellInfo const* spellInfo) const;
133 bool IsLimitedToAnotherMapOrZone(uint32 cur_mapId, uint32 cur_zoneId) const;
134 bool GemsFitSockets() const;
135
137 void SetCount(uint32 value);
138 uint32 GetMaxStackCount() const { return GetTemplate()->GetMaxStackSize(); }
139 uint8 GetGemCountWithID(uint32 GemID) const;
140 uint8 GetGemCountWithLimitCategory(uint32 limitCategory) const;
141 InventoryResult CanBeMergedPartlyWith(ItemTemplate const* proto) const;
142
143 uint8 GetSlot() const {return m_slot;}
144 Bag* GetContainer() { return m_container; }
145 uint8 GetBagSlot() const;
146 void SetSlot(uint8 slot) { m_slot = slot; }
147 uint16 GetPos() const { return uint16(GetBagSlot()) << 8 | GetSlot(); }
148 void SetContainer(Bag* container) { m_container = container; }
149
150 bool IsInBag() const { return m_container != nullptr; }
151 bool IsEquipped() const;
152
153 uint32 GetSkill();
154 uint32 GetSpell();
155
156 // RandomPropertyId (signed but stored as unsigned)
159 void SetItemRandomProperties(int32 randomPropId);
160 void UpdateItemSuffixFactor();
161 void SetEnchantment(EnchantmentSlot slot, uint32 id, uint32 duration, uint32 charges, ObjectGuid caster = ObjectGuid::Empty);
162 void SetEnchantmentDuration(EnchantmentSlot slot, uint32 duration, Player* owner);
163 void SetEnchantmentCharges(EnchantmentSlot slot, uint32 charges);
164 void ClearEnchantment(EnchantmentSlot slot);
168
169 std::string const& GetText() const { return m_text; }
170 void SetText(std::string const& text) { m_text = text; }
171
172 void SendUpdateSockets();
173
174 void SendTimeUpdate(Player* owner);
175 void UpdateDuration(Player* owner, uint32 diff);
176 void SetCreatePlayedTime(uint32 createPlayedTime) { SetUInt32Value(ITEM_FIELD_CREATE_PLAYED_TIME, createPlayedTime); }
177
178 // spell charges (signed but stored as unsigned)
179 int32 GetSpellCharges(uint8 index/*0..5*/ = 0) const { return GetInt32Value(ITEM_FIELD_SPELL_CHARGES + index); }
180 void SetSpellCharges(uint8 index/*0..5*/, int32 value) { SetInt32Value(ITEM_FIELD_SPELL_CHARGES + index, value); }
181
184
185 // Update States
186 ItemUpdateState GetState() const { return uState; }
187 void SetState(ItemUpdateState state, Player* forplayer = nullptr);
188 bool IsInUpdateQueue() const { return uQueuePos != -1; }
189 uint16 GetQueuePos() const { return uQueuePos; }
190 void FSetState(ItemUpdateState state) // forced
191 {
192 uState = state;
193 }
194
195 bool hasQuest(uint32 quest_id) const override { return GetTemplate()->GetStartQuest() == quest_id; }
196 bool hasInvolvedQuest(uint32 /*quest_id*/) const override { return false; }
197 bool IsPotion() const { return GetTemplate()->IsPotion(); }
198 bool IsWeaponVellum() const { return GetTemplate()->IsWeaponVellum(); }
199 bool IsArmorVellum() const { return GetTemplate()->IsArmorVellum(); }
200 bool IsConjuredConsumable() const { return GetTemplate()->IsConjuredConsumable(); }
201 uint32 GetItemLevel() const { return GetTemplate()->GetBaseItemLevel(); }
202 uint32 GetDisplayId() const { return GetTemplate()->GetDisplayId(); }
203
204 // Item Refund system
205 void SetNotRefundable(Player* owner, bool changestate = true, CharacterDatabaseTransaction* trans = nullptr);
206 void SetRefundRecipient(ObjectGuid const& guid) { m_refundRecipient = guid; }
207 void SetPaidMoney(uint32 money) { m_paidMoney = money; }
208 void SetPaidExtendedCost(uint32 iece) { m_paidExtendedCost = iece; }
209
210 ObjectGuid const& GetRefundRecipient() const { return m_refundRecipient; }
211 uint32 GetPaidMoney() const { return m_paidMoney; }
212 uint32 GetPaidExtendedCost() const { return m_paidExtendedCost; }
213
214 void UpdatePlayedTime(Player* owner);
215 uint32 GetPlayedTime();
216 bool IsRefundExpired();
217
218 // Soulbound trade system
219 void SetSoulboundTradeable(GuidSet const& allowedLooters);
220 void ClearSoulboundTradeable(Player* currentOwner);
221 bool CheckSoulboundTradeExpire();
222
223 void BuildUpdate(UpdateDataMapType&) override;
224
225 bool AddToObjectUpdate() override;
226 void RemoveFromObjectUpdate() override;
227
228 uint32 GetScriptId() const { return GetTemplate()->ScriptId; }
229
230 uint32 GetBuyPrice() const;
231 static uint32 GetBuyPrice(ItemTemplate const* proto);
232 uint32 GetSellPrice(bool forVendor = false) const;
233 static uint32 GetSellPrice(ItemTemplate const* proto);
234
237
238 std::string GetDebugInfo() const override;
239 private:
240 std::string m_text;
245 bool mb_in_trade; // true if item is currently in trade-window
251};
252#endif
LocaleConstant
Definition Common.h:48
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
#define TC_GAME_API
Definition Define.h:114
uint8_t uint8
Definition Define.h:135
int16_t int16
Definition Define.h:130
int32_t int32
Definition Define.h:129
uint16_t uint16
Definition Define.h:134
uint32_t uint32
Definition Define.h:133
uint16 flags
EnchantmentSlot
InventoryResult
Definition ItemDefines.h:25
ItemFieldFlags
@ ITEM_FIELD_FLAG_REFUNDABLE
@ ITEM_FIELD_FLAG_UNLOCKED
@ ITEM_FIELD_FLAG_SOULBOUND
@ ITEM_FIELD_FLAG_WRAPPED
@ ITEM_FIELD_FLAG_BOP_TRADEABLE
@ ITEM_FLAG_IS_BOUND_TO_ACCOUNT
@ INVTYPE_BAG
void RemoveItemFromUpdateQueueOf(Item *item, Player *player)
Definition Item.cpp:696
void AddItemToUpdateQueueOf(Item *item, Player *player)
Definition Item.cpp:675
#define MAX_ENCHANTMENT_OFFSET
Definition Item.h:49
ItemUpdateState
Definition Item.h:52
@ ITEM_CHANGED
Definition Item.h:54
@ ITEM_REMOVED
Definition Item.h:56
@ ITEM_NEW
Definition Item.h:55
@ ITEM_UNCHANGED
Definition Item.h:53
EnchantmentOffset
Definition Item.h:43
@ ENCHANTMENT_ID_OFFSET
Definition Item.h:44
@ ENCHANTMENT_CHARGES_OFFSET
Definition Item.h:46
@ ENCHANTMENT_DURATION_OFFSET
Definition Item.h:45
bool ItemCanGoIntoBag(ItemTemplate const *proto, ItemTemplate const *pBagProto)
Definition Item.cpp:183
std::set< ObjectGuid > GuidSet
Definition ObjectGuid.h:260
std::unordered_map< Player *, UpdateData > UpdateDataMapType
Definition Object.h:63
static void SaveToDB(QuestPool const &pool, CharacterDatabaseTransaction trans)
@ ITEM_FIELD_DURABILITY
@ ITEM_FIELD_STACK_COUNT
@ ITEM_FIELD_DURATION
@ ITEM_FIELD_FLAGS
@ ITEM_FIELD_MAXDURABILITY
@ ITEM_FIELD_RANDOM_PROPERTIES_ID
@ ITEM_FIELD_CREATOR
@ ITEM_FIELD_OWNER
@ ITEM_FIELD_GIFTCREATOR
@ ITEM_FIELD_PROPERTY_SEED
@ ITEM_FIELD_ENCHANTMENT_1_1
@ ITEM_FIELD_CREATE_PLAYED_TIME
@ ITEM_FIELD_CONTAINED
@ ITEM_FIELD_SPELL_CHARGES
Definition Bag.h:27
Class used to access individual fields of database query result.
Definition Field.h:92
Definition Item.h:62
uint32 GetPaidExtendedCost() const
Definition Item.h:212
bool hasInvolvedQuest(uint32) const override
Definition Item.h:196
uint8 GetSlot() const
Definition Item.h:143
std::string const & GetText() const
Definition Item.h:169
void SetItemFlag(ItemFieldFlags flags)
Definition Item.h:92
bool IsCurrencyToken() const
Definition Item.h:118
uint32 GetScriptId() const
Definition Item.h:228
bool IsWrapped() const
Definition Item.h:115
Bag * GetContainer()
Definition Item.h:144
void SetSpellCharges(uint8 index, int32 value)
Definition Item.h:180
void SetText(std::string const &text)
Definition Item.h:170
void SetPetitionId(uint32 petitionId)
Definition Item.h:235
void ReplaceAllItemFlags(ItemFieldFlags flags)
Definition Item.h:94
void RemoveItemFlag(ItemFieldFlags flags)
Definition Item.h:93
ObjectGuid GetContainedIn() const
Definition Item.h:80
void SetBinding(bool val)
Definition Item.h:90
uint32 GetEnchantmentId(EnchantmentSlot slot) const
Definition Item.h:165
int32 GetSpellCharges(uint8 index=0) const
Definition Item.h:179
Bag * m_container
Definition Item.h:242
void SetPaidMoney(uint32 money)
Definition Item.h:207
bool IsBoundAccountWide() const
Definition Item.h:97
bool mb_in_trade
Definition Item.h:245
Bag * ToBag()
Definition Item.h:110
std::string m_text
Definition Item.h:240
uint32 GetEnchantmentDuration(EnchantmentSlot slot) const
Definition Item.h:166
bool IsLocked() const
Definition Item.h:116
void SetDurability(uint32 durability)
Definition Item.h:121
int32 GetItemRandomPropertyId() const
Definition Item.h:157
ObjectGuid GetCreator() const
Definition Item.h:82
bool IsBOPTradeable() const
Definition Item.h:114
bool IsInBag() const
Definition Item.h:150
bool IsSoulBound() const
Definition Item.h:96
ItemUpdateState GetState() const
Definition Item.h:186
bool IsPotion() const
Definition Item.h:197
bool IsBroken() const
Definition Item.h:120
uint32 GetItemSuffixFactor() const
Definition Item.h:158
bool HasItemFlag(ItemFieldFlags flag) const
Definition Item.h:91
bool IsWeaponVellum() const
Definition Item.h:198
uint32 m_paidMoney
Definition Item.h:248
uint16 GetQueuePos() const
Definition Item.h:189
bool IsArmorVellum() const
Definition Item.h:199
uint32 GetItemLevel() const
Definition Item.h:201
ObjectGuid GetOwnerGUID() const
Definition Item.h:78
Bag const * ToBag() const
Definition Item.h:111
uint32 GetDisplayId() const
Definition Item.h:202
time_t m_lastPlayedTimeUpdate
Definition Item.h:246
bool IsRefundable() const
Definition Item.h:113
uint16 GetPos() const
Definition Item.h:147
ObjectGuid m_refundRecipient
Definition Item.h:247
bool IsInTrade() const
Definition Item.h:125
int16 uQueuePos
Definition Item.h:244
void SetContainedIn(ObjectGuid guid)
Definition Item.h:81
ObjectGuid GetGiftCreator() const
Definition Item.h:84
void SetPetitionNumSignatures(uint32 signatures)
Definition Item.h:236
void FSetState(ItemUpdateState state)
Definition Item.h:190
void SetMaxDurability(uint32 maxDurability)
Definition Item.h:122
uint32 m_paidExtendedCost
Definition Item.h:249
bool IsBag() const
Definition Item.h:117
void SetCreator(ObjectGuid guid)
Definition Item.h:83
ObjectGuid const & GetRefundRecipient() const
Definition Item.h:210
void SetCreatePlayedTime(uint32 createPlayedTime)
Definition Item.h:176
void SetPaidExtendedCost(uint32 iece)
Definition Item.h:208
uint32 GetEnchantmentCharges(EnchantmentSlot slot) const
Definition Item.h:167
void SetRefundRecipient(ObjectGuid const &guid)
Definition Item.h:206
uint32 GetPaidMoney() const
Definition Item.h:211
void SetSlot(uint8 slot)
Definition Item.h:146
bool m_lootGenerated
Definition Item.h:183
uint32 GetCount() const
Definition Item.h:136
void SetExpiration(uint32 expiration)
Definition Item.h:88
void SetOwnerGUID(ObjectGuid guid)
Definition Item.h:79
Loot loot
Definition Item.h:182
void SetContainer(Bag *container)
Definition Item.h:148
bool hasQuest(uint32 quest_id) const override
Definition Item.h:195
uint32 GetMaxStackCount() const
Definition Item.h:138
void SetGiftCreator(ObjectGuid guid)
Definition Item.h:85
bool IsConjuredConsumable() const
Definition Item.h:200
uint8 m_slot
Definition Item.h:241
bool IsInUpdateQueue() const
Definition Item.h:188
GuidSet allowedGUIDs
Definition Item.h:250
ItemUpdateState uState
Definition Item.h:243
void SetInTrade(bool b=true)
Definition Item.h:124
static ObjectGuid const Empty
Definition ObjectGuid.h:140
uint32 LowType
Definition ObjectGuid.h:142
int32 GetInt32Value(uint16 index) const
Definition Object.cpp:235
void SetInt32Value(uint16 index, int32 value)
Definition Object.cpp:564
uint32 GetUInt32Value(uint16 index) const
Definition Object.cpp:241
virtual std::string const & GetNameForLocaleIdx(LocaleConstant) const =0
virtual bool AddToObjectUpdate()=0
void SetFlag(uint16 index, uint32 newFlag)
Definition Object.cpp:761
void RemoveFlag(uint16 index, uint32 oldFlag)
Definition Object.cpp:776
void ApplyModFlag(uint16 index, uint32 flag, bool apply)
Definition Object.cpp:806
void SetGuidValue(uint16 index, ObjectGuid value)
Definition Object.cpp:691
virtual std::string GetDebugInfo() const
Definition Object.cpp:900
virtual void BuildUpdate(UpdateDataMapType &)
Definition Object.h:161
virtual void RemoveFromObjectUpdate()=0
bool HasFlag(uint16 index, uint32 flag) const
Definition Object.cpp:800
void SetUInt32Value(uint16 index, uint32 value)
Definition Object.cpp:577
ObjectGuid GetGuidValue(uint16 index) const
Definition Object.cpp:273
Definition Unit.h:783
uint32 item_count
Definition Item.h:36
uint32 setid
Definition Item.h:35
SpellInfo const * spells[8]
Definition Item.h:37
uint32 GetBaseItemLevel() const
bool IsPotion() const
uint32 GetMaxStackSize() const
bool IsCurrencyToken() const
uint32 GetStartQuest() const
bool IsArmorVellum() const
uint32 GetDisplayId() const
bool IsConjuredConsumable() const
bool HasFlag(ItemFlags flag) const
::InventoryType GetInventoryType() const
bool IsWeaponVellum() const
Definition Loot.h:226