[2] | 1 | /* |
---|
| 2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
| 3 | * |
---|
| 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. |
---|
| 8 | * |
---|
| 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. |
---|
| 13 | * |
---|
| 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 |
---|
| 17 | */ |
---|
| 18 | |
---|
| 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" |
---|
| 27 | |
---|
| 28 | Bag::Bag( ): Item() |
---|
| 29 | { |
---|
| 30 | m_objectType |= TYPEMASK_CONTAINER; |
---|
| 31 | m_objectTypeId = TYPEID_CONTAINER; |
---|
| 32 | |
---|
| 33 | m_valuesCount = CONTAINER_END; |
---|
| 34 | |
---|
| 35 | memset(m_bagslot, 0, sizeof(Item *) * MAX_BAG_SIZE); // Maximum 20 Slots |
---|
| 36 | } |
---|
| 37 | |
---|
| 38 | Bag::~Bag() |
---|
| 39 | { |
---|
| 40 | for(int i = 0; i<MAX_BAG_SIZE; i++) |
---|
| 41 | { |
---|
| 42 | if(m_bagslot[i]) delete m_bagslot[i]; |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | void Bag::AddToWorld() |
---|
| 47 | { |
---|
| 48 | Item::AddToWorld(); |
---|
| 49 | |
---|
| 50 | for(int i = 0; i<MAX_BAG_SIZE; i++) |
---|
| 51 | { |
---|
| 52 | if(m_bagslot[i]) |
---|
| 53 | m_bagslot[i]->AddToWorld(); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void Bag::RemoveFromWorld() |
---|
| 58 | { |
---|
| 59 | for(int i = 0; i<MAX_BAG_SIZE; i++) |
---|
| 60 | { |
---|
| 61 | if(m_bagslot[i]) |
---|
| 62 | m_bagslot[i]->RemoveFromWorld(); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | Item::RemoveFromWorld(); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner) |
---|
| 69 | { |
---|
| 70 | ItemPrototype const * itemProto = objmgr.GetItemPrototype(itemid); |
---|
| 71 | |
---|
| 72 | if(!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE) |
---|
| 73 | return false; |
---|
| 74 | |
---|
| 75 | Object::_Create( guidlow, 0, HIGHGUID_CONTAINER ); |
---|
| 76 | |
---|
| 77 | SetUInt32Value(OBJECT_FIELD_ENTRY, itemid); |
---|
| 78 | SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f); |
---|
| 79 | |
---|
| 80 | SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0); |
---|
| 81 | SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0); |
---|
| 82 | |
---|
| 83 | SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability); |
---|
| 84 | SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability); |
---|
| 85 | SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags); |
---|
| 86 | SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1); |
---|
| 87 | |
---|
| 88 | // Setting the number of Slots the Container has |
---|
| 89 | SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots); |
---|
| 90 | |
---|
| 91 | // Cleanning 20 slots |
---|
| 92 | for (uint8 i = 0; i < MAX_BAG_SIZE; i++) |
---|
| 93 | { |
---|
| 94 | SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0); |
---|
| 95 | m_bagslot[i] = NULL; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | return true; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void Bag::SaveToDB() |
---|
| 102 | { |
---|
| 103 | Item::SaveToDB(); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result) |
---|
| 107 | { |
---|
| 108 | if(!Item::LoadFromDB(guid, owner_guid, result)) |
---|
| 109 | return false; |
---|
| 110 | |
---|
| 111 | // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`) |
---|
| 112 | for (uint32 i = 0; i < GetProto()->ContainerSlots; i++) |
---|
| 113 | { |
---|
| 114 | SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0); |
---|
| 115 | if (m_bagslot[i]) |
---|
| 116 | { |
---|
| 117 | delete m_bagslot[i]; |
---|
| 118 | m_bagslot[i] = NULL; |
---|
| 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return true; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | void Bag::DeleteFromDB() |
---|
| 126 | { |
---|
| 127 | for (int i = 0; i < MAX_BAG_SIZE; i++) |
---|
| 128 | { |
---|
| 129 | if (m_bagslot[i]) |
---|
| 130 | { |
---|
| 131 | m_bagslot[i]->DeleteFromDB(); |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | Item::DeleteFromDB(); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | uint32 Bag::GetFreeSlots() const |
---|
| 139 | { |
---|
| 140 | uint32 ContainerSlots=GetProto()->ContainerSlots; |
---|
| 141 | uint32 slots = 0; |
---|
| 142 | for (uint8 i=0; i <ContainerSlots; i++) |
---|
| 143 | if (!m_bagslot[i]) |
---|
| 144 | ++slots; |
---|
| 145 | |
---|
| 146 | return slots; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | void Bag::RemoveItem( uint8 slot, bool /*update*/ ) |
---|
| 150 | { |
---|
| 151 | assert(slot < MAX_BAG_SIZE); |
---|
| 152 | |
---|
| 153 | if (m_bagslot[slot]) |
---|
| 154 | m_bagslot[slot]->SetContainer(NULL); |
---|
| 155 | |
---|
| 156 | m_bagslot[slot] = NULL; |
---|
| 157 | SetUInt64Value( CONTAINER_FIELD_SLOT_1 + (slot * 2), 0 ); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | void Bag::StoreItem( uint8 slot, Item *pItem, bool /*update*/ ) |
---|
| 161 | { |
---|
| 162 | assert(slot < MAX_BAG_SIZE); |
---|
| 163 | |
---|
| 164 | if( pItem ) |
---|
| 165 | { |
---|
| 166 | m_bagslot[slot] = pItem; |
---|
| 167 | SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID()); |
---|
| 168 | pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID()); |
---|
| 169 | pItem->SetUInt64Value( ITEM_FIELD_OWNER, GetOwnerGUID() ); |
---|
| 170 | pItem->SetContainer(this); |
---|
| 171 | pItem->SetSlot(slot); |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) const |
---|
| 176 | { |
---|
| 177 | Item::BuildCreateUpdateBlockForPlayer( data, target ); |
---|
| 178 | |
---|
| 179 | for (int i = 0; i < MAX_BAG_SIZE; i++) |
---|
| 180 | { |
---|
| 181 | if(m_bagslot[i]) |
---|
| 182 | m_bagslot[i]->BuildCreateUpdateBlockForPlayer( data, target ); |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | // If the bag is empty returns true |
---|
| 187 | bool Bag::IsEmpty() const |
---|
| 188 | { |
---|
| 189 | uint32 ContainerSlots=GetProto()->ContainerSlots; |
---|
| 190 | for(uint32 i=0; i < ContainerSlots; i++) |
---|
| 191 | if (m_bagslot[i]) return false; |
---|
| 192 | |
---|
| 193 | return true; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const |
---|
| 197 | { |
---|
| 198 | uint32 ContainerSlots=GetProto()->ContainerSlots; |
---|
| 199 | |
---|
| 200 | Item *pItem; |
---|
| 201 | uint32 count = 0; |
---|
| 202 | for(uint32 i=0; i < ContainerSlots; i++) |
---|
| 203 | { |
---|
| 204 | pItem = m_bagslot[i]; |
---|
| 205 | if( pItem && pItem != eItem && pItem->GetEntry() == item ) |
---|
| 206 | count += pItem->GetCount(); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | if(eItem && eItem->GetProto()->GemProperties) |
---|
| 210 | { |
---|
| 211 | for(uint32 i=0; i < ContainerSlots; i++) |
---|
| 212 | { |
---|
| 213 | pItem = m_bagslot[i]; |
---|
| 214 | if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color ) |
---|
| 215 | count += pItem->GetGemCountWithID(item); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | return count; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | uint8 Bag::GetSlotByItemGUID(uint64 guid) const |
---|
| 223 | { |
---|
| 224 | uint32 ContainerSlots=GetProto()->ContainerSlots; |
---|
| 225 | |
---|
| 226 | for(uint32 i=0;i<ContainerSlots;i++) |
---|
| 227 | { |
---|
| 228 | if(m_bagslot[i] != 0) |
---|
| 229 | if(m_bagslot[i]->GetGUID() == guid) |
---|
| 230 | return i; |
---|
| 231 | } |
---|
| 232 | return NULL_SLOT; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | // Adds an item to a bag slot |
---|
| 236 | // - slot can be NULL_SLOT, in that case function searchs for a free slot |
---|
| 237 | // - Return values: 0 - item not added |
---|
| 238 | // 1 - item added to a free slot (and perhaps to a stack) |
---|
| 239 | // 2 - item added to a stack (item should be deleted) |
---|
| 240 | Item* Bag::GetItemByPos( uint8 slot ) const |
---|
| 241 | { |
---|
| 242 | ItemPrototype const *pBagProto = GetProto(); |
---|
| 243 | if( pBagProto ) |
---|
| 244 | { |
---|
| 245 | if( slot < pBagProto->ContainerSlots ) |
---|
| 246 | return m_bagslot[slot]; |
---|
| 247 | } |
---|
| 248 | return NULL; |
---|
| 249 | } |
---|