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
21 #include "ObjectMgr.h"
22 #include "Database/DatabaseEnv.h"
24 #include "UpdateData.h"
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
);
38 for(int i
= 0; i
< MAX_BAG_SIZE
; ++i
)
43 void Bag::AddToWorld()
47 for(uint32 i
= 0; i
< GetBagSize(); ++i
)
49 m_bagslot
[i
]->AddToWorld();
52 void Bag::RemoveFromWorld()
54 for(uint32 i
= 0; i
< GetBagSize(); ++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
)
68 Object::_Create( guidlow
, 0, HIGHGUID_CONTAINER
);
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
);
85 for (uint8 i
= 0; i
< MAX_BAG_SIZE
; ++i
)
87 SetUInt64Value(CONTAINER_FIELD_SLOT_1
+ (i
*2), 0);
99 bool Bag::LoadFromDB(uint32 guid
, uint64 owner_guid
, QueryResult
*result
)
101 if(!Item::LoadFromDB(guid
, owner_guid
, result
))
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);
118 void Bag::DeleteFromDB()
120 for (int i
= 0; i
< MAX_BAG_SIZE
; ++i
)
122 m_bagslot
[i
]->DeleteFromDB();
124 Item::DeleteFromDB();
127 uint32
Bag::GetFreeSlots() const
130 for (uint32 i
=0; i
< GetBagSize(); ++i
)
137 void Bag::RemoveItem( uint8 slot
, bool /*update*/ )
139 assert(slot
< MAX_BAG_SIZE
);
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
);
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
)
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
)
182 uint32
Bag::GetItemCount( uint32 item
, Item
* eItem
) const
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
);
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
)
216 Item
* Bag::GetItemByPos( uint8 slot
) const
218 if( slot
< GetBagSize() )
219 return m_bagslot
[slot
];