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

Revision 260, 5.7 kB (checked in by yumileroy, 17 years ago)

*DB script table stucture change. Source Mangos. Also fix some bugs. Hopefully this rev will make program usable again.

Original author: megamage
Date: 2008-11-20 10:43:20-06: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        if (m_bagslot[i])
44            delete m_bagslot[i];
45}
46
47void Bag::AddToWorld()
48{
49    Item::AddToWorld();
50
51    for(uint32 i = 0;  i < GetBagSize(); ++i)
52        if(m_bagslot[i])
53            m_bagslot[i]->AddToWorld();
54}
55
56void Bag::RemoveFromWorld()
57{
58    for(uint32 i = 0; i < GetBagSize(); ++i)
59        if(m_bagslot[i])
60            m_bagslot[i]->RemoveFromWorld();
61
62    Item::RemoveFromWorld();
63}
64
65bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner)
66{
67    ItemPrototype const * itemProto = objmgr.GetItemPrototype(itemid);
68
69    if(!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE)
70        return false;
71
72    Object::_Create( guidlow, 0, HIGHGUID_CONTAINER );
73
74    SetEntry(itemid);
75    SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f);
76
77    SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0);
78    SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0);
79
80    SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability);
81    SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability);
82    SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags);
83    SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1);
84
85    // Setting the number of Slots the Container has
86    SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots);
87
88    // Cleaning 20 slots
89    for (uint8 i = 0; i < MAX_BAG_SIZE; i++)
90    {
91        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
92        m_bagslot[i] = NULL;
93    }
94
95    return true;
96}
97
98void Bag::SaveToDB()
99{
100    Item::SaveToDB();
101}
102
103bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result)
104{
105    if(!Item::LoadFromDB(guid, owner_guid, result))
106        return false;
107
108    // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
109    for (int i = 0; i < MAX_BAG_SIZE; ++i)
110    {
111        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0);
112        if (m_bagslot[i])
113        {
114            delete m_bagslot[i];
115            m_bagslot[i] = NULL;
116        }
117    }
118
119    return true;
120}
121
122void Bag::DeleteFromDB()
123{
124    for (int i = 0; i < MAX_BAG_SIZE; i++)
125        if (m_bagslot[i])
126            m_bagslot[i]->DeleteFromDB();
127
128    Item::DeleteFromDB();
129}
130
131uint32 Bag::GetFreeSlots() const
132{
133    uint32 slots = 0;
134    for (uint32 i=0; i < GetBagSize(); i++)
135        if (!m_bagslot[i])
136            ++slots;
137
138    return slots;
139}
140
141void Bag::RemoveItem( uint8 slot, bool /*update*/ )
142{
143    assert(slot < MAX_BAG_SIZE);
144
145    if (m_bagslot[slot])
146        m_bagslot[slot]->SetContainer(NULL);
147
148    m_bagslot[slot] = NULL;
149    SetUInt64Value( CONTAINER_FIELD_SLOT_1 + (slot * 2), 0 );
150}
151
152void Bag::StoreItem( uint8 slot, Item *pItem, bool /*update*/ )
153{
154    assert(slot < MAX_BAG_SIZE);
155
156    if( pItem )
157    {
158        m_bagslot[slot] = pItem;
159        SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID());
160        pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID());
161        pItem->SetUInt64Value( ITEM_FIELD_OWNER, GetOwnerGUID() );
162        pItem->SetContainer(this);
163        pItem->SetSlot(slot);
164    }
165}
166
167void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const
168{
169    Item::BuildCreateUpdateBlockForPlayer( data, target );
170
171    for (uint32 i = 0; i < GetBagSize(); ++i)
172        if(m_bagslot[i])
173            m_bagslot[i]->BuildCreateUpdateBlockForPlayer( data, target );
174}
175
176// If the bag is empty returns true
177bool Bag::IsEmpty() const
178{
179    for(uint32 i = 0; i < GetBagSize(); ++i)
180        if (m_bagslot[i])
181            return false;
182
183    return true;
184}
185
186uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const
187{
188    Item *pItem;
189    uint32 count = 0;
190    for(uint32 i=0; i < GetBagSize(); ++i)
191    {
192        pItem = m_bagslot[i];
193        if( pItem && pItem != eItem && pItem->GetEntry() == item )
194            count += pItem->GetCount();
195    }
196
197    if(eItem && eItem->GetProto()->GemProperties)
198    {
199        for(uint32 i=0; i < GetBagSize(); ++i)
200        {
201            pItem = m_bagslot[i];
202            if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color )
203                count += pItem->GetGemCountWithID(item);
204        }
205    }
206
207    return count;
208}
209
210uint8 Bag::GetSlotByItemGUID(uint64 guid) const
211{
212    for(uint32 i = 0; i < GetBagSize(); ++i)
213        if(m_bagslot[i] != 0)
214            if(m_bagslot[i]->GetGUID() == guid)
215                return i;
216
217    return NULL_SLOT;
218}
219
220Item* Bag::GetItemByPos( uint8 slot ) const
221{
222    if( slot < GetBagSize() )
223        return m_bagslot[slot];
224
225    return NULL;
226}
Note: See TracBrowser for help on using the browser.