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 "WorldPacket.h"
25 #include "UpdateData.h"
26 #include "WorldSession.h"
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
);
40 for(int i
= 0; i
< MAX_BAG_SIZE
; ++i
)
45 void Bag::AddToWorld()
49 for(uint32 i
= 0; i
< GetBagSize(); ++i
)
51 m_bagslot
[i
]->AddToWorld();
54 void Bag::RemoveFromWorld()
56 for(uint32 i
= 0; i
< GetBagSize(); ++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
)
70 Object::_Create( guidlow
, 0, HIGHGUID_CONTAINER
);
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
);
87 for (uint8 i
= 0; i
< MAX_BAG_SIZE
; i
++)
89 SetUInt64Value(CONTAINER_FIELD_SLOT_1
+ (i
*2), 0);
101 bool Bag::LoadFromDB(uint32 guid
, uint64 owner_guid
, QueryResult
*result
)
103 if(!Item::LoadFromDB(guid
, owner_guid
, result
))
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);
120 void Bag::DeleteFromDB()
122 for (int i
= 0; i
< MAX_BAG_SIZE
; i
++)
124 m_bagslot
[i
]->DeleteFromDB();
126 Item::DeleteFromDB();
129 uint32
Bag::GetFreeSlots() const
132 for (uint32 i
=0; i
< GetBagSize(); i
++)
139 void Bag::RemoveItem( uint8 slot
, bool /*update*/ )
141 assert(slot
< MAX_BAG_SIZE
);
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
);
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
)
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
)
184 uint32
Bag::GetItemCount( uint32 item
, Item
* eItem
) const
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
);
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
)
218 Item
* Bag::GetItemByPos( uint8 slot
) const
220 if( slot
< GetBagSize() )
221 return m_bagslot
[slot
];