OregonCore  revision fb2a440-git
Your Favourite TBC server
Bag.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the OregonCore 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 "Database/DatabaseEnv.h"
21 
22 #include "Bag.h"
23 #include "Log.h"
24 #include "UpdateData.h"
25 
27 {
30 
32 
33  memset(m_bagslot, 0, sizeof(Item*) * MAX_BAG_SIZE);
34 }
35 
37 {
38  for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
39  if (Item* item = m_bagslot[i])
40  {
41  if (item->IsInWorld())
42  {
43  sLog.outError("Crash alert! Item %u (slot %u, bag slot %u) in bag %u (slot %u, bag slot %u, m_bagslot %u) is to be deleted but is still in world.",
44  item->GetEntry(), (uint32)item->GetSlot(), (uint32)item->GetBagSlot(),
46  item->RemoveFromWorld();
47  }
48  delete m_bagslot[i];
49  }
50 }
51 
53 {
55 
56  for (uint32 i = 0; i < GetBagSize(); ++i)
57  if (m_bagslot[i])
58  m_bagslot[i]->AddToWorld();
59 }
60 
62 {
63  for (uint32 i = 0; i < GetBagSize(); ++i)
64  if (m_bagslot[i])
66 
68 }
69 
70 bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner)
71 {
72  ItemTemplate const* itemProto = sObjectMgr.GetItemTemplate(itemid);
73 
74  if (!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
75  return false;
76 
78 
79  SetEntry(itemid);
80  SetObjectScale(1.0f);
81 
82  SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
83  SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
84 
88 
89  // Setting the number of Slots the Container has
91 
92  // Cleaning 20 slots
93  for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
94  {
96  m_bagslot[i] = NULL;
97  }
98 
99  return true;
100 }
101 
103 {
104  Item::SaveToDB();
105 }
106 
107 bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, Field* fields)
108 {
109  if (!Item::LoadFromDB(guid, owner_guid, fields))
110  return false;
111 
112  ItemTemplate const* itemProto = sObjectMgr.GetItemTemplate(GetEntry());
113 
114  if (!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
115  return false;
116 
117  // Setting the number of Slots the Container has
119 
120  // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
121  for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
122  {
124  delete m_bagslot[i];
125  m_bagslot[i] = NULL;
126  }
127 
128  return true;
129 }
130 
132 {
133  for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
134  if (m_bagslot[i])
135  m_bagslot[i]->DeleteFromDB();
136 
138 }
139 
141 {
142  uint32 slots = 0;
143  for (uint32 i = 0; i < GetBagSize(); ++i)
144  if (!m_bagslot[i])
145  ++slots;
146 
147  return slots;
148 }
149 
150 void Bag::RemoveItem(uint8 slot, bool /*update*/)
151 {
152  ASSERT(slot < MAX_BAG_SIZE);
153 
154  if (m_bagslot[slot])
155  m_bagslot[slot]->SetContainer(NULL);
156 
157  m_bagslot[slot] = NULL;
158  SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), 0);
159 }
160 
161 void Bag::StoreItem(uint8 slot, Item* pItem, bool /*update*/)
162 {
163  if (slot > MAX_BAG_SIZE)
164  {
165  sLog.outError("Player GUID " UI64FMTD " tried to manipulate packets and crash the server.", GetOwnerGUID());
166  return;
167  }
168 
169  if (pItem && pItem->GetGUID() != this->GetGUID())
170  {
171  m_bagslot[slot] = pItem;
172  SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
175  pItem->SetContainer(this);
176  pItem->SetSlot(slot);
177  }
178 }
179 
181 {
183 
184  for (uint32 i = 0; i < GetBagSize(); ++i)
185  if (m_bagslot[i])
186  m_bagslot[i]->BuildCreateUpdateBlockForPlayer(data, target);
187 }
188 
189 // If the bag is empty returns true
190 bool Bag::IsEmpty() const
191 {
192  for (uint32 i = 0; i < GetBagSize(); ++i)
193  if (m_bagslot[i])
194  return false;
195 
196  return true;
197 }
198 
199 uint32 Bag::GetItemCount(uint32 item, Item* eItem) const
200 {
201  Item* pItem;
202  uint32 count = 0;
203  for (uint32 i = 0; i < GetBagSize(); ++i)
204  {
205  pItem = m_bagslot[i];
206  if (pItem && pItem != eItem && pItem->GetEntry() == item)
207  count += pItem->GetCount();
208  }
209 
210  if (eItem && eItem->GetProto()->GemProperties)
211  {
212  for (uint32 i = 0; i < GetBagSize(); ++i)
213  {
214  pItem = m_bagslot[i];
215  if (pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color)
216  count += pItem->GetGemCountWithID(item);
217  }
218  }
219 
220  return count;
221 }
222 
224 {
225  for (uint32 i = 0; i < GetBagSize(); ++i)
226  if (m_bagslot[i] != 0)
227  if (m_bagslot[i]->GetGUID() == guid)
228  return i;
229 
230  return NULL_SLOT;
231 }
232 
234 {
235  if (slot < GetBagSize())
236  return m_bagslot[slot];
237 
238  return NULL;
239 }
240 
uint32 GetCount() const
Definition: Item.h:251
ItemTemplate const * GetProto() const
Definition: Item.cpp:460
void StoreItem(uint8 slot, Item *pItem, bool update)
Definition: Bag.cpp:161
void SetContainer(Bag *container)
Definition: Item.h:283
void RemoveFromWorld() override
Definition: Bag.cpp:61
_Socket Socket[3]
#define MAX_BAG_SIZE
Definition: Bag.h:22
virtual void DeleteFromDB()
Definition: Item.cpp:450
void BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const override
Definition: Bag.cpp:180
Definition: Field.h:24
void SetObjectScale(float scale)
Definition: Object.h:205
~Bag() override
Definition: Bag.cpp:36
Item * GetItemByPos(uint8 slot) const
Definition: Bag.cpp:233
virtual void SaveToDB()
Definition: Item.cpp:286
#define sLog
Log class singleton.
Definition: Log.h:187
Bag()
Definition: Bag.cpp:26
void SetUInt32Value(uint16 index, uint32 value)
Definition: Object.cpp:779
void SetEntry(uint32 entry)
Definition: Object.h:196
bool LoadFromDB(uint32 guid, uint64 owner_guid, Field *fields) override
Definition: Bag.cpp:107
uint16 m_objectType
Definition: Object.h:424
#define sObjectMgr
Definition: ObjectMgr.h:1285
uint32 Color
virtual void RemoveFromWorld()
Definition: Object.h:151
uint8 m_objectTypeId
Definition: Object.h:426
void _Create(uint32 guidlow, uint32 entry, HighGuid guidhigh)
Definition: Object.cpp:125
ACE_UINT8 uint8
Definition: Define.h:73
#define UI64FMTD
Definition: Common.h:149
bool Create(uint32 guidlow, uint32 itemid, Player const *owner) override
Definition: Bag.cpp:70
uint32 GetItemCount(uint32 item, Item *eItem=NULL) const
Definition: Bag.cpp:199
bool IsEmpty() const
Definition: Bag.cpp:190
void SetSlot(uint8 slot)
Definition: Item.h:275
uint8 GetGemCountWithID(uint32 GemID) const
Definition: Item.cpp:897
uint32 GetFreeSlots() const
Definition: Bag.cpp:140
Definition: Item.h:196
uint8 GetSlotByItemGUID(uint64 guid) const
Definition: Bag.cpp:223
uint32 GetBagSize() const
Definition: Bag.h:49
void SetUInt64Value(uint16 index, const uint64 &value)
Definition: Object.cpp:798
uint8 GetSlot() const
Definition: Item.h:266
virtual void BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const
Definition: Object.cpp:148
uint32 MaxDurability
virtual bool LoadFromDB(uint32 guid, uint64 owner_guid, Field *fields)
Definition: Item.cpp:369
void AddToWorld() override
Definition: Bag.cpp:52
void RemoveItem(uint8 slot, bool update)
Definition: Bag.cpp:150
ACE_UINT64 uint64
Definition: Define.h:70
void DeleteFromDB() override
Definition: Bag.cpp:131
virtual void AddToWorld()
Definition: Object.h:139
uint32 ContainerSlots
void SaveToDB() override
Definition: Bag.cpp:102
uint16 m_valuesCount
Definition: Object.h:438
#define ASSERT
Definition: Errors.h:29
uint32 GetEntry() const
Definition: Object.h:192
ACE_UINT32 uint32
Definition: Define.h:71
uint8 GetBagSlot() const
Definition: Item.cpp:728
Definition: Player.h:922
uint64 const & GetOwnerGUID() const
Definition: Item.h:208
uint32 GemProperties
Item * m_bagslot[MAX_BAG_SIZE]
Definition: Bag.h:67
const uint64 & GetGUID() const
Definition: Object.h:162