[8449] Deprecate healing/damage item mods and merge internal data in to spell power.
[getmangos.git] / src / game / Bag.cpp
blob0feca6d14a544403e819df8cadb6fc5946f3b6ee
1 /*
2 * Copyright (C) 2005-2009 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 = objmgr.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 uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const
184 Item *pItem;
185 uint32 count = 0;
186 for(uint32 i=0; i < GetBagSize(); ++i)
188 pItem = m_bagslot[i];
189 if( pItem && pItem != eItem && pItem->GetEntry() == item )
190 count += pItem->GetCount();
193 if(eItem && eItem->GetProto()->GemProperties)
195 for(uint32 i=0; i < GetBagSize(); ++i)
197 pItem = m_bagslot[i];
198 if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color )
199 count += pItem->GetGemCountWithID(item);
203 return count;
206 uint8 Bag::GetSlotByItemGUID(uint64 guid) const
208 for(uint32 i = 0; i < GetBagSize(); ++i)
209 if(m_bagslot[i] != 0)
210 if(m_bagslot[i]->GetGUID() == guid)
211 return i;
213 return NULL_SLOT;
216 Item* Bag::GetItemByPos( uint8 slot ) const
218 if( slot < GetBagSize() )
219 return m_bagslot[slot];
221 return NULL;