TrinityCore
Loading...
Searching...
No Matches
Bag.cpp
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#include "Common.h"
19#include "ObjectMgr.h"
20#include "DatabaseEnv.h"
21
22#include "Bag.h"
23#include "Log.h"
24#include "UpdateData.h"
25#include "Player.h"
26
36
38{
39 for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
40 if (Item* item = m_bagslot[i])
41 {
42 if (item->IsInWorld())
43 {
44 TC_LOG_FATAL("entities.player.items", "Item {} (slot {}, bag slot {}) in bag {} (slot {}, bag slot {}, m_bagslot {}) is to be deleted but is still in world.",
45 item->GetEntry(), (uint32)item->GetSlot(), (uint32)item->GetBagSlot(),
47 item->RemoveFromWorld();
48 }
49 delete m_bagslot[i];
50 }
51}
52
54{
56
57 for (uint32 i = 0; i < GetBagSize(); ++i)
58 if (m_bagslot[i])
60}
61
63{
64 for (uint32 i = 0; i < GetBagSize(); ++i)
65 if (m_bagslot[i])
67
69}
70
71bool Bag::Create(ObjectGuid::LowType guidlow, uint32 itemid, Player const* owner)
72{
73 ItemTemplate const* itemProto = sObjectMgr->GetItemTemplate(itemid);
74
75 if (!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
76 return false;
77
78 Object::_Create(ObjectGuid::Create<HighGuid::Container>(guidlow));
79
80 SetEntry(itemid);
81 SetObjectScale(1.0f);
82
83 if (owner)
84 {
85 SetOwnerGUID(owner->GetGUID());
87 }
88
92
93 // Setting the number of Slots the Container has
95
96 // Cleaning 20 slots
97 for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
98 {
100 m_bagslot[i] = nullptr;
101 }
102
103 return true;
104}
105
110
111bool Bag::LoadFromDB(ObjectGuid::LowType guid, ObjectGuid owner_guid, Field* fields, uint32 entry)
112{
113 if (!Item::LoadFromDB(guid, owner_guid, fields, entry))
114 return false;
115
116 ItemTemplate const* itemProto = GetTemplate(); // checked in Item::LoadFromDB
118 // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
119 for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
120 {
122 delete m_bagslot[i];
123 m_bagslot[i] = nullptr;
124 }
125
126 return true;
127}
128
130{
131 for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
132 if (m_bagslot[i])
133 m_bagslot[i]->DeleteFromDB(trans);
134
135 Item::DeleteFromDB(trans);
136}
137
139{
140 uint32 slots = 0;
141 for (uint32 i=0; i < GetBagSize(); ++i)
142 if (!m_bagslot[i])
143 ++slots;
144
145 return slots;
146}
147
148void Bag::RemoveItem(uint8 slot, bool /*update*/)
149{
150 ASSERT(slot < MAX_BAG_SIZE);
151
152 if (m_bagslot[slot])
153 m_bagslot[slot]->SetContainer(nullptr);
154
155 m_bagslot[slot] = nullptr;
157}
158
159void Bag::StoreItem(uint8 slot, Item* pItem, bool /*update*/)
160{
161 ASSERT(slot < MAX_BAG_SIZE);
162
163 if (pItem && pItem->GetGUID() != GetGUID())
164 {
165 m_bagslot[slot] = pItem;
166 SetGuidValue(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
168 pItem->SetOwnerGUID(GetOwnerGUID());
169 pItem->SetContainer(this);
170 pItem->SetSlot(slot);
171 }
172}
173
175{
177
178 for (uint32 i = 0; i < GetBagSize(); ++i)
179 if (m_bagslot[i])
181}
182
183// If the bag is empty returns true
184bool Bag::IsEmpty() const
185{
186 for (uint32 i = 0; i < GetBagSize(); ++i)
187 if (m_bagslot[i])
188 return false;
189
190 return true;
191}
192
194{
195 Item* pItem;
196 uint32 count = 0;
197 for (uint32 i=0; i < GetBagSize(); ++i)
198 {
199 pItem = m_bagslot[i];
200 if (pItem && pItem != eItem && pItem->GetEntry() == item)
201 count += pItem->GetCount();
202 }
203
204 if (eItem && eItem->GetTemplate()->GemProperties)
205 {
206 for (uint32 i=0; i < GetBagSize(); ++i)
207 {
208 pItem = m_bagslot[i];
209 if (pItem && pItem != eItem && pItem->GetTemplate()->Socket[0].Color)
210 count += pItem->GetGemCountWithID(item);
211 }
212 }
213
214 return count;
215}
216
218{
219 uint32 count = 0;
220 for (uint32 i = 0; i < GetBagSize(); ++i)
221 if (Item* pItem = m_bagslot[i])
222 if (pItem != skipItem)
223 if (ItemTemplate const* pProto = pItem->GetTemplate())
224 if (pProto->ItemLimitCategory == limitCategory)
225 count += m_bagslot[i]->GetCount();
226
227 return count;
228}
229
231{
232 for (uint32 i = 0; i < GetBagSize(); ++i)
233 if (m_bagslot[i] != 0)
234 if (m_bagslot[i]->GetGUID() == guid)
235 return i;
236
237 return NULL_SLOT;
238}
239
241{
242 if (slot < GetBagSize())
243 return m_bagslot[slot];
244
245 return nullptr;
246}
247
248std::string Bag::GetDebugInfo() const
249{
250 std::stringstream sstr;
251 sstr << Item::GetDebugInfo();
252 return sstr.str();
253}
#define MAX_BAG_SIZE
Definition Bag.h:22
SQLTransaction< CharacterDatabaseConnection > CharacterDatabaseTransaction
uint8_t uint8
Definition Define.h:135
uint32_t uint32
Definition Define.h:133
#define ASSERT
Definition Errors.h:68
#define TC_LOG_FATAL(filterType__,...)
Definition Log.h:168
@ TYPEID_CONTAINER
Definition ObjectGuid.h:37
@ TYPEMASK_CONTAINER
Definition ObjectGuid.h:51
#define sObjectMgr
Definition ObjectMgr.h:1721
@ NULL_SLOT
Definition Unit.h:62
@ CONTAINER_FIELD_SLOT_1
@ CONTAINER_END
@ CONTAINER_FIELD_NUM_SLOTS
@ ITEM_FIELD_DURABILITY
@ ITEM_FIELD_STACK_COUNT
@ ITEM_FIELD_MAXDURABILITY
@ ITEM_FIELD_CONTAINED
uint32 GetBagSize() const
Definition Bag.h:47
void AddToWorld() override
Definition Bag.cpp:53
void StoreItem(uint8 slot, Item *pItem, bool update)
Definition Bag.cpp:159
void RemoveFromWorld() override
Definition Bag.cpp:62
bool IsEmpty() const
Definition Bag.cpp:184
void SaveToDB(CharacterDatabaseTransaction trans) override
Definition Bag.cpp:106
void BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const override
Definition Bag.cpp:174
~Bag()
Definition Bag.cpp:37
bool Create(ObjectGuid::LowType guidlow, uint32 itemid, Player const *owner) override
Definition Bag.cpp:71
Item * GetItemByPos(uint8 slot) const
Definition Bag.cpp:240
std::string GetDebugInfo() const override
Definition Bag.cpp:248
uint32 GetItemCount(uint32 item, Item *eItem=nullptr) const
Definition Bag.cpp:193
void RemoveItem(uint8 slot, bool update)
Definition Bag.cpp:148
void DeleteFromDB(CharacterDatabaseTransaction trans) override
Definition Bag.cpp:129
Item * m_bagslot[MAX_BAG_SIZE]
Definition Bag.h:64
uint32 GetFreeSlots() const
Definition Bag.cpp:138
uint8 GetSlotByItemGUID(ObjectGuid guid) const
Definition Bag.cpp:230
Bag()
Definition Bag.cpp:27
bool LoadFromDB(ObjectGuid::LowType guid, ObjectGuid owner_guid, Field *fields, uint32 entry) override
Definition Bag.cpp:111
uint32 GetItemCountWithLimitCategory(uint32 limitCategory, Item *skipItem=nullptr) const
Definition Bag.cpp:217
Class used to access individual fields of database query result.
Definition Field.h:92
Definition Item.h:62
std::string GetDebugInfo() const override
Definition Item.cpp:1266
uint8 GetSlot() const
Definition Item.h:126
virtual bool LoadFromDB(ObjectGuid::LowType guid, ObjectGuid owner_guid, Field *fields, uint32 entry)
Definition Item.cpp:409
virtual void SaveToDB(CharacterDatabaseTransaction trans)
Definition Item.cpp:323
uint8 GetGemCountWithID(uint32 GemID) const
Definition Item.cpp:971
ItemTemplate const * GetTemplate() const
Definition Item.cpp:535
ObjectGuid GetOwnerGUID() const
Definition Item.h:76
void SetSlot(uint8 slot)
Definition Item.h:129
uint32 GetCount() const
Definition Item.h:119
void SetOwnerGUID(ObjectGuid guid)
Definition Item.h:77
void SetContainer(Bag *container)
Definition Item.h:131
uint8 GetBagSlot() const
Definition Item.cpp:711
static void DeleteFromDB(CharacterDatabaseTransaction trans, ObjectGuid::LowType itemGuid)
Definition Item.cpp:506
static ObjectGuid const Empty
Definition ObjectGuid.h:140
uint32 LowType
Definition ObjectGuid.h:142
uint16 m_objectType
Definition Object.h:232
virtual void BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const
Definition Object.cpp:170
uint16 m_valuesCount
Definition Object.h:246
void SetGuidValue(uint16 index, ObjectGuid value)
Definition Object.cpp:699
uint32 GetEntry() const
Definition Object.h:81
void _Create(ObjectGuid const &guid)
Definition Object.cpp:109
virtual void AddToWorld()
Definition Object.cpp:126
virtual void RemoveFromWorld()
Definition Object.cpp:145
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
void SetEntry(uint32 entry)
Definition Object.h:82
virtual void SetObjectScale(float scale)
Definition Object.h:85
void SetUInt32Value(uint16 index, uint32 value)
Definition Object.cpp:585
ObjectGuid GetGUID() const
Definition Object.h:79
TypeID m_objectTypeId
Definition Object.h:234
std::array< _Socket, MAX_ITEM_PROTO_SOCKETS > Socket
uint32 GemProperties
uint32 ContainerSlots
uint32 MaxDurability