[9529] Make Player::IsValidPos const
[getmangos.git] / src / game / Bag.cpp
blob3b1a28aabd1611c11ae572ac4d728cc1bfba496f
1 /*
2 * Copyright (C) 2005-2010 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Bag.h"
21 #include "ObjectMgr.h"
22 #include "Database/DatabaseEnv.h"
23 #include "Log.h"
24 #include "UpdateData.h"
26 Bag::Bag( ): Item()
28 m_objectType |= TYPEMASK_CONTAINER;
29 m_objectTypeId = TYPEID_CONTAINER;
31 m_valuesCount = CONTAINER_END;
33 memset(m_bagslot, 0, sizeof(Item *) * MAX_BAG_SIZE);
36 Bag::~Bag()
38 for(int i = 0; i < MAX_BAG_SIZE; ++i)
39 if (m_bagslot[i])
40 delete m_bagslot[i];
43 void Bag::AddToWorld()
45 Item::AddToWorld();
47 for(uint32 i = 0; i < GetBagSize(); ++i)
48 if(m_bagslot[i])
49 m_bagslot[i]->AddToWorld();
52 void Bag::RemoveFromWorld()
54 for(uint32 i = 0; i < GetBagSize(); ++i)
55 if(m_bagslot[i])
56 m_bagslot[i]->RemoveFromWorld();
58 Item::RemoveFromWorld();
61 bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner)
63 ItemPrototype const * itemProto = ObjectMgr::GetItemPrototype(itemid);
65 if(!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
66 return false;
68 Object::_Create( guidlow, 0, HIGHGUID_CONTAINER );
70 SetEntry(itemid);
71 SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
73 SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
74 SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
76 SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
77 SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
78 SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
79 SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
81 // Setting the number of Slots the Container has
82 SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots);
84 // Cleaning 20 slots
85 for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
87 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
88 m_bagslot[i] = NULL;
91 return true;
94 void Bag::SaveToDB()
96 Item::SaveToDB();
99 bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
101 if(!Item::LoadFromDB(guid, owner_guid, result))
102 return false;
104 // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
105 for (int i = 0; i < MAX_BAG_SIZE; ++i)
107 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
108 if (m_bagslot[i])
110 delete m_bagslot[i];
111 m_bagslot[i] = NULL;
115 return true;
118 void Bag::DeleteFromDB()
120 for (int i = 0; i < MAX_BAG_SIZE; ++i)
121 if (m_bagslot[i])
122 m_bagslot[i]->DeleteFromDB();
124 Item::DeleteFromDB();
127 uint32 Bag::GetFreeSlots() const
129 uint32 slots = 0;
130 for (uint32 i=0; i < GetBagSize(); ++i)
131 if (!m_bagslot[i])
132 ++slots;
134 return slots;
137 void Bag::RemoveItem( uint8 slot, bool /*update*/ )
139 assert(slot < MAX_BAG_SIZE);
141 if (m_bagslot[slot])
142 m_bagslot[slot]->SetContainer(NULL);
144 m_bagslot[slot] = NULL;
145 SetUInt64Value( CONTAINER_FIELD_SLOT_1 + (slot * 2), 0 );
148 void Bag::StoreItem( uint8 slot, Item *pItem, bool /*update*/ )
150 assert(slot < MAX_BAG_SIZE);
152 if( pItem )
154 m_bagslot[slot] = pItem;
155 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
156 pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID());
157 pItem->SetUInt64Value( ITEM_FIELD_OWNER, GetOwnerGUID() );
158 pItem->SetContainer(this);
159 pItem->SetSlot(slot);
163 void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const
165 Item::BuildCreateUpdateBlockForPlayer( data, target );
167 for (uint32 i = 0; i < GetBagSize(); ++i)
168 if(m_bagslot[i])
169 m_bagslot[i]->BuildCreateUpdateBlockForPlayer( data, target );
172 // If the bag is empty returns true
173 bool Bag::IsEmpty() const
175 for(uint32 i = 0; i < GetBagSize(); ++i)
176 if (m_bagslot[i])
177 return false;
179 return true;
182 Item* Bag::GetItemByEntry( uint32 item ) const
184 for(uint32 i = 0; i < GetBagSize(); ++i)
185 if (m_bagslot[i] && m_bagslot[i]->GetEntry() == item)
186 return m_bagslot[i];
188 return NULL;
191 Item* Bag::GetItemByLimitedCategory(uint32 limitedCategory) const
193 for(uint32 i = 0; i < GetBagSize(); ++i)
194 if (m_bagslot[i] && m_bagslot[i]->GetProto()->ItemLimitCategory == limitedCategory)
195 return m_bagslot[i];
197 return NULL;
200 uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const
202 Item *pItem;
203 uint32 count = 0;
204 for(uint32 i=0; i < GetBagSize(); ++i)
206 pItem = m_bagslot[i];
207 if( pItem && pItem != eItem && pItem->GetEntry() == item )
208 count += pItem->GetCount();
211 if(eItem && eItem->GetProto()->GemProperties)
213 for(uint32 i=0; i < GetBagSize(); ++i)
215 pItem = m_bagslot[i];
216 if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color )
217 count += pItem->GetGemCountWithID(item);
221 return count;
224 uint32 Bag::GetItemCountWithLimitCategory(uint32 limitCategory) const
226 uint32 count = 0;
227 for(uint32 i = 0; i < GetBagSize(); ++i)
228 if (m_bagslot[i])
229 if (m_bagslot[i]->GetProto()->ItemLimitCategory == limitCategory )
230 count += m_bagslot[i]->GetCount();
232 return count;
235 uint8 Bag::GetSlotByItemGUID(uint64 guid) const
237 for(uint32 i = 0; i < GetBagSize(); ++i)
238 if(m_bagslot[i] != 0)
239 if(m_bagslot[i]->GetGUID() == guid)
240 return i;
242 return NULL_SLOT;
245 Item* Bag::GetItemByPos( uint8 slot ) const
247 if( slot < GetBagSize() )
248 return m_bagslot[slot];
250 return NULL;