root/trunk/src/game/Bag.cpp @ 102

Revision 102, 6.4 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "Common.h"
22#include "Bag.h"
23#include "ObjectMgr.h"
24#include "Database/DatabaseEnv.h"
25#include "Log.h"
26#include "WorldPacket.h"
27#include "UpdateData.h"
28#include "WorldSession.h"
29
30Bag::Bag( ): Item()
31{
32    m_objectType |= TYPEMASK_CONTAINER;
33    m_objectTypeId = TYPEID_CONTAINER;
34
35    m_valuesCount = CONTAINER_END;
36
37    memset(m_bagslot, 0, sizeof(Item *) * MAX_BAG_SIZE);    // Maximum 20 Slots
38}
39
40Bag::~Bag()
41{
42    for(int i = 0; i<MAX_BAG_SIZE; i++)
43    {
44        if(m_bagslot[i])    delete m_bagslot[i];
45    }
46}
47
48void Bag::AddToWorld()
49{
50    Item::AddToWorld();
51
52    for(int i = 0; i<MAX_BAG_SIZE; i++)
53    {
54        if(m_bagslot[i])
55            m_bagslot[i]->AddToWorld();
56    }
57}
58
59void Bag::RemoveFromWorld()
60{
61    for(int i = 0; i<MAX_BAG_SIZE; i++)
62    {
63        if(m_bagslot[i])
64            m_bagslot[i]->RemoveFromWorld();
65    }
66
67    Item::RemoveFromWorld();
68}
69
70bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner)
71{
72    ItemPrototype const * itemProto = objmgr.GetItemPrototype(itemid);
73
74    if(!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
75        return false;
76
77    Object::_Create( guidlow, 0, HIGHGUID_CONTAINER );
78
79    SetUInt32Value(OBJECT_FIELD_ENTRY, itemid);
80    SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
81
82    SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
83    SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
84
85    SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
86    SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
87    SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
88    SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
89
90    // Setting the number of Slots the Container has
91    SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots);
92
93    // Cleanning 20 slots
94    for (uint8 i = 0; i < MAX_BAG_SIZE; i++)
95    {
96        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
97        m_bagslot[i] = NULL;
98    }
99
100    return true;
101}
102
103void Bag::SaveToDB()
104{
105    Item::SaveToDB();
106}
107
108bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
109{
110    if(!Item::LoadFromDB(guid, owner_guid, result))
111        return false;
112
113    // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
114    for (uint32 i = 0; i < GetProto()->ContainerSlots; i++)
115    {
116        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
117        if (m_bagslot[i])
118        {
119            delete m_bagslot[i];
120            m_bagslot[i] = NULL;
121        }
122    }
123
124    return true;
125}
126
127void Bag::DeleteFromDB()
128{
129    for (int i = 0; i < MAX_BAG_SIZE; i++)
130    {
131        if (m_bagslot[i])
132        {
133            m_bagslot[i]->DeleteFromDB();
134        }
135    }
136
137    Item::DeleteFromDB();
138}
139
140uint32 Bag::GetFreeSlots() const
141{
142    uint32 ContainerSlots=GetProto()->ContainerSlots;
143    uint32 slots = 0;
144    for (uint8 i=0; i <ContainerSlots; i++)
145        if (!m_bagslot[i])
146            ++slots;
147
148    return slots;
149}
150
151void Bag::RemoveItem( uint8 slot, bool /*update*/ )
152{
153    assert(slot < MAX_BAG_SIZE);
154
155    if (m_bagslot[slot])
156        m_bagslot[slot]->SetContainer(NULL);
157
158    m_bagslot[slot] = NULL;
159    SetUInt64Value( CONTAINER_FIELD_SLOT_1 + (slot * 2), 0 );
160}
161
162void Bag::StoreItem( uint8 slot, Item *pItem, bool /*update*/ )
163{
164    assert(slot < MAX_BAG_SIZE);
165
166    if( pItem )
167    {
168        m_bagslot[slot] = pItem;
169        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
170        pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID());
171        pItem->SetUInt64Value( ITEM_FIELD_OWNER, GetOwnerGUID() );
172        pItem->SetContainer(this);
173        pItem->SetSlot(slot);
174    }
175}
176
177void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const
178{
179    Item::BuildCreateUpdateBlockForPlayer( data, target );
180
181    for (int i = 0; i < MAX_BAG_SIZE; i++)
182    {
183        if(m_bagslot[i])
184            m_bagslot[i]->BuildCreateUpdateBlockForPlayer( data, target );
185    }
186}
187
188// If the bag is empty returns true
189bool Bag::IsEmpty() const
190{
191    uint32 ContainerSlots=GetProto()->ContainerSlots;
192    for(uint32 i=0; i < ContainerSlots; i++)
193        if (m_bagslot[i]) return false;
194
195    return true;
196}
197
198uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const
199{
200    uint32 ContainerSlots=GetProto()->ContainerSlots;
201
202    Item *pItem;
203    uint32 count = 0;
204    for(uint32 i=0; i < ContainerSlots; i++)
205    {
206        pItem = m_bagslot[i];
207        if( pItem && pItem != eItem && pItem->GetEntry() == item )
208            count += pItem->GetCount();
209    }
210
211    if(eItem && eItem->GetProto()->GemProperties)
212    {
213        for(uint32 i=0; i < ContainerSlots; i++)
214        {
215            pItem = m_bagslot[i];
216            if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color )
217                count += pItem->GetGemCountWithID(item);
218        }
219    }
220
221    return count;
222}
223
224uint8 Bag::GetSlotByItemGUID(uint64 guid) const
225{
226    uint32 ContainerSlots=GetProto()->ContainerSlots;
227
228    for(uint32 i=0;i<ContainerSlots;i++)
229    {
230        if(m_bagslot[i] != 0)
231            if(m_bagslot[i]->GetGUID() == guid)
232                return i;
233    }
234    return NULL_SLOT;
235}
236
237// Adds an item to a bag slot
238// - slot can be NULL_SLOT, in that case function searchs for a free slot
239// - Return values: 0 - item not added
240//                  1 - item added to a free slot (and perhaps to a stack)
241//                  2 - item added to a stack (item should be deleted)
242Item* Bag::GetItemByPos( uint8 slot ) const
243{
244    ItemPrototype const *pBagProto = GetProto();
245    if( pBagProto )
246    {
247        if( slot < pBagProto->ContainerSlots )
248            return m_bagslot[slot];
249    }
250    return NULL;
251}
Note: See TracBrowser for help on using the browser.