[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / Bag.cpp
blob2f967752e7c1c3f6d0cd4bfeb67770bfc6644592
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 "WorldPacket.h"
25 #include "UpdateData.h"
26 #include "WorldSession.h"
28 Bag::Bag( ): Item()
30 m_objectType |= TYPEMASK_CONTAINER;
31 m_objectTypeId = TYPEID_CONTAINER;
33 m_valuesCount = CONTAINER_END;
35 memset(m_bagslot, 0, sizeof(Item *) * MAX_BAG_SIZE);
38 Bag::~Bag()
40 for(int i = 0; i < MAX_BAG_SIZE; ++i)
41 if (m_bagslot[i])
42 delete m_bagslot[i];
45 void Bag::AddToWorld()
47 Item::AddToWorld();
49 for(uint32 i = 0; i < GetBagSize(); ++i)
50 if(m_bagslot[i])
51 m_bagslot[i]->AddToWorld();
54 void Bag::RemoveFromWorld()
56 for(uint32 i = 0; i < GetBagSize(); ++i)
57 if(m_bagslot[i])
58 m_bagslot[i]->RemoveFromWorld();
60 Item::RemoveFromWorld();
63 bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner)
65 ItemPrototype const * itemProto = objmgr.GetItemPrototype(itemid);
67 if(!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
68 return false;
70 Object::_Create( guidlow, 0, HIGHGUID_CONTAINER );
72 SetEntry(itemid);
73 SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
75 SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
76 SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
78 SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
79 SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
80 SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
81 SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
83 // Setting the number of Slots the Container has
84 SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots);
86 // Cleaning 20 slots
87 for (uint8 i = 0; i < MAX_BAG_SIZE; i++)
89 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
90 m_bagslot[i] = NULL;
93 return true;
96 void Bag::SaveToDB()
98 Item::SaveToDB();
101 bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
103 if(!Item::LoadFromDB(guid, owner_guid, result))
104 return false;
106 // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
107 for (int i = 0; i < MAX_BAG_SIZE; ++i)
109 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
110 if (m_bagslot[i])
112 delete m_bagslot[i];
113 m_bagslot[i] = NULL;
117 return true;
120 void Bag::DeleteFromDB()
122 for (int i = 0; i < MAX_BAG_SIZE; i++)
123 if (m_bagslot[i])
124 m_bagslot[i]->DeleteFromDB();
126 Item::DeleteFromDB();
129 uint32 Bag::GetFreeSlots() const
131 uint32 slots = 0;
132 for (uint32 i=0; i < GetBagSize(); i++)
133 if (!m_bagslot[i])
134 ++slots;
136 return slots;
139 void Bag::RemoveItem( uint8 slot, bool /*update*/ )
141 assert(slot < MAX_BAG_SIZE);
143 if (m_bagslot[slot])
144 m_bagslot[slot]->SetContainer(NULL);
146 m_bagslot[slot] = NULL;
147 SetUInt64Value( CONTAINER_FIELD_SLOT_1 + (slot * 2), 0 );
150 void Bag::StoreItem( uint8 slot, Item *pItem, bool /*update*/ )
152 assert(slot < MAX_BAG_SIZE);
154 if( pItem )
156 m_bagslot[slot] = pItem;
157 SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
158 pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID());
159 pItem->SetUInt64Value( ITEM_FIELD_OWNER, GetOwnerGUID() );
160 pItem->SetContainer(this);
161 pItem->SetSlot(slot);
165 void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const
167 Item::BuildCreateUpdateBlockForPlayer( data, target );
169 for (uint32 i = 0; i < GetBagSize(); ++i)
170 if(m_bagslot[i])
171 m_bagslot[i]->BuildCreateUpdateBlockForPlayer( data, target );
174 // If the bag is empty returns true
175 bool Bag::IsEmpty() const
177 for(uint32 i = 0; i < GetBagSize(); ++i)
178 if (m_bagslot[i])
179 return false;
181 return true;
184 uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const
186 Item *pItem;
187 uint32 count = 0;
188 for(uint32 i=0; i < GetBagSize(); ++i)
190 pItem = m_bagslot[i];
191 if( pItem && pItem != eItem && pItem->GetEntry() == item )
192 count += pItem->GetCount();
195 if(eItem && eItem->GetProto()->GemProperties)
197 for(uint32 i=0; i < GetBagSize(); ++i)
199 pItem = m_bagslot[i];
200 if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color )
201 count += pItem->GetGemCountWithID(item);
205 return count;
208 uint8 Bag::GetSlotByItemGUID(uint64 guid) const
210 for(uint32 i = 0; i < GetBagSize(); ++i)
211 if(m_bagslot[i] != 0)
212 if(m_bagslot[i]->GetGUID() == guid)
213 return i;
215 return NULL_SLOT;
218 Item* Bag::GetItemByPos( uint8 slot ) const
220 if( slot < GetBagSize() )
221 return m_bagslot[slot];
223 return NULL;