diff options
author | Azazel <azazel.kon@gmail.com> | 2011-03-18 15:58:08 +0600 |
---|---|---|
committer | Azazel <azazel.kon@gmail.com> | 2011-03-18 15:58:08 +0600 |
commit | e49b5b44d6588b04ba7176f2814f4515006ba5ef (patch) | |
tree | 29cc1c0a1ece7b596d909a94ae8ce8b015af3fc1 /src | |
parent | 90a3fee2329571863e907315fc6b5bfc7d1c96f5 (diff) |
Core/Cleanup: remove C-style cast to (Bag*):
* add ToBag() methods for Item class;
* add GetBagByPos() method for Player class.
Diffstat (limited to 'src')
-rwxr-xr-x | src/server/game/Entities/Item/Item.cpp | 9 | ||||
-rwxr-xr-x | src/server/game/Entities/Item/Item.h | 4 | ||||
-rwxr-xr-x | src/server/game/Entities/Player/Player.cpp | 287 | ||||
-rwxr-xr-x | src/server/game/Entities/Player/Player.h | 1 | ||||
-rwxr-xr-x | src/server/game/Guilds/Guild.cpp | 2 | ||||
-rwxr-xr-x | src/server/game/Server/Protocol/Handlers/AuctionHouseHandler.cpp | 2 | ||||
-rwxr-xr-x | src/server/game/Server/Protocol/Handlers/ItemHandler.cpp | 4 | ||||
-rwxr-xr-x | src/server/game/Server/Protocol/Handlers/MailHandler.cpp | 2 | ||||
-rw-r--r-- | src/server/scripts/Commands/cs_debug.cpp | 24 |
9 files changed, 154 insertions, 181 deletions
diff --git a/src/server/game/Entities/Item/Item.cpp b/src/server/game/Entities/Item/Item.cpp index 8ace11b723d..69ed9f625ff 100755 --- a/src/server/game/Entities/Item/Item.cpp +++ b/src/server/game/Entities/Item/Item.cpp @@ -281,6 +281,15 @@ bool Item::Create(uint32 guidlow, uint32 itemid, Player const* owner) return true; } +// Returns true if Item is a bag AND it is not empty. +// Returns false if Item is not a bag OR it is an empty bag. +bool Item::IsNotEmptyBag() const +{ + if (const Bag* pBag = ToBag()) + return !pBag->IsEmpty(); + return false; +} + void Item::UpdateDuration(Player* owner, uint32 diff) { if (!GetUInt32Value(ITEM_FIELD_DURATION)) diff --git a/src/server/game/Entities/Item/Item.h b/src/server/game/Entities/Item/Item.h index cf6759804dd..df0769db710 100755 --- a/src/server/game/Entities/Item/Item.h +++ b/src/server/game/Entities/Item/Item.h @@ -253,8 +253,12 @@ class Item : public Object void SaveRefundDataToDB(); void DeleteRefundDataFromDB(); + Bag* ToBag() { if (IsBag()) return reinterpret_cast<Bag*>(this); else return NULL; } + const Bag* ToBag() const { if (IsBag()) return reinterpret_cast<const Bag*>(this); else return NULL; } + bool IsLocked() const { return !HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAG_UNLOCKED); } bool IsBag() const { return GetProto()->InventoryType == INVTYPE_BAG; } + bool IsNotEmptyBag() const; bool IsBroken() const { return GetUInt32Value(ITEM_FIELD_MAXDURABILITY) > 0 && GetUInt32Value(ITEM_FIELD_DURABILITY) == 0; } bool CanBeTraded(bool mail = false, bool trade = false) const; void SetInTrade(bool b = true) { mb_in_trade = b; } diff --git a/src/server/game/Entities/Player/Player.cpp b/src/server/game/Entities/Player/Player.cpp index 958cf7eacd9..8665227ef29 100755 --- a/src/server/game/Entities/Player/Player.cpp +++ b/src/server/game/Entities/Player/Player.cpp @@ -5251,7 +5251,7 @@ void Player::DurabilityLossAll(double percent, bool inventory) //for (int i = KEYRING_SLOT_START; i < KEYRING_SLOT_END; i++) for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); j++) if (Item* pItem = GetItemByPos(i, j)) DurabilityLoss(pItem,percent); @@ -9899,7 +9899,7 @@ uint8 Player::CanUnequipItems(uint32 item, uint32 count) const } for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag *pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); ++j) if (Item *pItem = GetItemByPos(i, j)) if (pItem->GetEntry() == item) @@ -9927,8 +9927,8 @@ uint32 Player::GetItemCount(uint32 item, bool inBankAlso, Item* skipItem) const count += pItem->GetCount(); for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) - count += pBag->GetItemCount(item,skipItem); + if (Bag* pBag = GetBagByPos(i)) + count += pBag->GetItemCount(item, skipItem); if (skipItem && skipItem->GetProto()->GemProperties) for (uint8 i = EQUIPMENT_SLOT_START; i < INVENTORY_SLOT_ITEM_END; ++i) @@ -9944,8 +9944,8 @@ uint32 Player::GetItemCount(uint32 item, bool inBankAlso, Item* skipItem) const count += pItem->GetCount(); for (uint8 i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; ++i) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) - count += pBag->GetItemCount(item,skipItem); + if (Bag* pBag = GetBagByPos(i)) + count += pBag->GetItemCount(item, skipItem); if (skipItem && skipItem->GetProto()->GemProperties) for (uint8 i = BANK_SLOT_ITEM_START; i < BANK_SLOT_ITEM_END; ++i) @@ -9975,7 +9975,7 @@ uint32 Player::GetItemCountWithLimitCategory(uint32 limitCategory, Item* skipIte count += pItem->GetCount(); for (int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - if (Bag* pBag = (Bag*) GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) count += pBag->GetItemCountWithLimitCategory(limitCategory, skipItem); for (int i = BANK_SLOT_ITEM_START; i < BANK_SLOT_ITEM_END; ++i) @@ -9986,7 +9986,7 @@ uint32 Player::GetItemCountWithLimitCategory(uint32 limitCategory, Item* skipIte count += pItem->GetCount(); for (int i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; ++i) - if (Bag* pBag = (Bag*) GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) count += pBag->GetItemCountWithLimitCategory(limitCategory, skipItem); return count; @@ -10010,14 +10010,14 @@ Item* Player::GetItemByGuid(uint64 guid) const return pItem; for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag *pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); ++j) if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->GetGUID() == guid) return pItem; for (uint8 i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; ++i) - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag *pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); ++j) if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->GetGUID() == guid) @@ -10037,12 +10037,16 @@ Item* Player::GetItemByPos(uint8 bag, uint8 slot) const { if (bag == INVENTORY_SLOT_BAG_0 && (slot < BANK_SLOT_BAG_END || (slot >= KEYRING_SLOT_START && slot < CURRENCYTOKEN_SLOT_END))) return m_items[slot]; - else if ((bag >= INVENTORY_SLOT_BAG_START && bag < INVENTORY_SLOT_BAG_END) + else if (Bag *pBag = GetBagByPos(bag)) + return pBag->GetItemByPos(slot); + return NULL; +} + +Bag* Player::GetBagByPos(uint8 bag) const +{ + if ((bag >= INVENTORY_SLOT_BAG_START && bag < INVENTORY_SLOT_BAG_END) || (bag >= BANK_SLOT_BAG_START && bag < BANK_SLOT_BAG_END)) - { - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag)) - return pBag->GetItemByPos(slot); - } + return GetItemByPos(INVENTORY_SLOT_BAG_0, bag)->ToBag(); return NULL; } @@ -10188,26 +10192,9 @@ bool Player::IsValidPos(uint8 bag, uint8 slot, bool explicit_pos) } // bag content slots - if (bag >= INVENTORY_SLOT_BAG_START && bag < INVENTORY_SLOT_BAG_END) - { - Bag* pBag = (Bag*)GetItemByPos (INVENTORY_SLOT_BAG_0, bag); - if (!pBag) - return false; - - // any post selected - if (slot == NULL_SLOT && !explicit_pos) - return true; - - return slot < pBag->GetBagSize(); - } - // bank bag content slots - if (bag >= BANK_SLOT_BAG_START && bag < BANK_SLOT_BAG_END) + if (Bag* pBag = GetBagByPos(bag)) { - Bag* pBag = (Bag*)GetItemByPos (INVENTORY_SLOT_BAG_0, bag); - if (!pBag) - return false; - // any post selected if (slot == NULL_SLOT && !explicit_pos) return true; @@ -10244,7 +10231,7 @@ bool Player::HasItemCount(uint32 item, uint32 count, bool inBankAlso) const } for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) { for (uint32 j = 0; j < pBag->GetBagSize(); j++) { @@ -10273,7 +10260,7 @@ bool Player::HasItemCount(uint32 item, uint32 count, bool inBankAlso) const } for (uint8 i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; i++) { - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) { for (uint32 j = 0; j < pBag->GetBagSize(); j++) { @@ -10435,7 +10422,7 @@ bool Player::HasItemTotemCategory(uint32 TotemCategory) const } for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) { - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag *pBag = GetBagByPos(i)) { for (uint32 j = 0; j < pBag->GetBagSize(); ++j) { @@ -10458,7 +10445,7 @@ uint8 Player::_CanStoreItem_InSpecificSlot(uint8 bag, uint8 slot, ItemPosCountVe uint32 need_space; - if (pSrcItem && pSrcItem->IsBag() && !((Bag*)pSrcItem)->IsEmpty() && !IsBagPos(uint16(bag) << 8 | slot)) + if (pSrcItem && pSrcItem->IsNotEmptyBag() && !IsBagPos(uint16(bag) << 8 | slot)) return EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS; // empty specific slot - check item fit to slot @@ -10480,7 +10467,7 @@ uint8 Player::_CanStoreItem_InSpecificSlot(uint8 bag, uint8 slot, ItemPosCountVe } else { - Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag); + Bag* pBag = GetBagByPos(bag); if (!pBag) return EQUIP_ERR_ITEM_DOESNT_GO_INTO_BAG; @@ -10529,11 +10516,11 @@ uint8 Player::_CanStoreItem_InBag(uint8 bag, ItemPosCountVec &dest, ItemPrototyp return EQUIP_ERR_ITEM_DOESNT_GO_INTO_BAG; // skip not existed bag or self targeted bag - Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag); + Bag* pBag = GetBagByPos(bag); if (!pBag || pBag == pSrcItem) return EQUIP_ERR_ITEM_DOESNT_GO_INTO_BAG; - if (pSrcItem && pSrcItem->IsBag() && !((Bag*)pSrcItem)->IsEmpty()) + if (pSrcItem && pSrcItem->IsNotEmptyBag()) return EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS; ItemPrototype const* pBagProto = pBag->GetProto(); @@ -10595,7 +10582,7 @@ uint8 Player::_CanStoreItem_InBag(uint8 bag, ItemPosCountVec &dest, ItemPrototyp uint8 Player::_CanStoreItem_InInventorySlots(uint8 slot_begin, uint8 slot_end, ItemPosCountVec &dest, ItemPrototype const *pProto, uint32& count, bool merge, Item* pSrcItem, uint8 skip_bag, uint8 skip_slot) const { //this is never called for non-bag slots so we can do this - if (pSrcItem && pSrcItem->IsBag() && !((Bag*)pSrcItem)->IsEmpty()) + if (pSrcItem && pSrcItem->IsNotEmptyBag()) return EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS; for (uint32 j = slot_begin; j < slot_end; j++) @@ -11030,7 +11017,7 @@ uint8 Player::_CanStoreItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, uint32 } } - if (pItem && pItem->IsBag() && !((Bag*)pItem)->IsEmpty()) + if (pItem && pItem->IsNotEmptyBag()) return EQUIP_ERR_NONEMPTY_BAG_OVER_OTHER_BAG; // search free slot @@ -11123,7 +11110,7 @@ uint8 Player::CanStoreItems(Item **pItems,int count) const for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) { for (uint32 j = 0; j < pBag->GetBagSize(); j++) { @@ -11210,17 +11197,19 @@ uint8 Player::CanStoreItems(Item **pItems,int count) const for (int t = INVENTORY_SLOT_BAG_START; !b_found && t < INVENTORY_SLOT_BAG_END; ++t) { - pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, t); - if (pBag && ItemCanGoIntoBag(pItem->GetProto(), pBag->GetProto())) + if (pBag = GetBagByPos(t)) { - for (uint32 j = 0; j < pBag->GetBagSize(); j++) + if (ItemCanGoIntoBag(pItem->GetProto(), pBag->GetProto())) { - pItem2 = GetItemByPos(t, j); - if( pItem2 && pItem2->CanBeMergedPartlyWith(pProto) == EQUIP_ERR_OK && inv_bags[t-INVENTORY_SLOT_BAG_START][j] + pItem->GetCount() <= pProto->GetMaxStackSize()) + for (uint32 j = 0; j < pBag->GetBagSize(); j++) { - inv_bags[t-INVENTORY_SLOT_BAG_START][j] += pItem->GetCount(); - b_found = true; - break; + pItem2 = GetItemByPos(t, j); + if( pItem2 && pItem2->CanBeMergedPartlyWith(pProto) == EQUIP_ERR_OK && inv_bags[t-INVENTORY_SLOT_BAG_START][j] + pItem->GetCount() <= pProto->GetMaxStackSize()) + { + inv_bags[t-INVENTORY_SLOT_BAG_START][j] += pItem->GetCount(); + b_found = true; + break; + } } } } @@ -11265,8 +11254,7 @@ uint8 Player::CanStoreItems(Item **pItems,int count) const for (int t = INVENTORY_SLOT_BAG_START; !b_found && t < INVENTORY_SLOT_BAG_END; ++t) { - pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, t); - if (pBag) + if (pBag = GetBagByPos(t)) { pBagProto = pBag->GetProto(); @@ -11305,8 +11293,7 @@ uint8 Player::CanStoreItems(Item **pItems,int count) const // search free slot in bags for (int t = INVENTORY_SLOT_BAG_START; !b_found && t < INVENTORY_SLOT_BAG_END; ++t) { - pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, t); - if (pBag) + if (pBag = GetBagByPos(t)) { pBagProto = pBag->GetProto(); @@ -11528,7 +11515,7 @@ uint8 Player::CanUnequipItem(uint16 pos, bool swap) const return EQUIP_ERR_NOT_DURING_ARENA_MATCH; } - if (!swap && pItem->IsBag() && !((Bag*)pItem)->IsEmpty()) + if (!swap && pItem->IsNotEmptyBag()) return EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS; return EQUIP_ERR_OK; @@ -11596,12 +11583,8 @@ uint8 Player::CanBankItem(uint8 bag, uint8 slot, ItemPosCountVec &dest, Item *pI // in specific bag if (bag != NULL_BAG) { - if (pProto->InventoryType == INVTYPE_BAG) - { - Bag *pBag = (Bag*)pItem; - if (pBag && !pBag->IsEmpty()) - return EQUIP_ERR_NONEMPTY_BAG_OVER_OTHER_BAG; - } + if (pItem->IsNotEmptyBag()) + return EQUIP_ERR_NONEMPTY_BAG_OVER_OTHER_BAG; // search stack in bag for merge to if (pProto->Stackable != 1) @@ -11975,7 +11958,8 @@ Item* Player::_StoreItem(uint16 pos, Item *pItem, uint32 count, bool clone, bool (pItem->GetProto()->Bonding == BIND_WHEN_EQUIPED && IsBagPos(pos))) pItem->SetBinding(true); - if (bag == INVENTORY_SLOT_BAG_0) + Bag* pBag = (bag == INVENTORY_SLOT_BAG_0) ? NULL : GetBagByPos(bag); + if (!pBag) { m_items[slot] = pItem; SetUInt64Value(PLAYER_FIELD_INV_SLOT_HEAD + (slot * 2), pItem->GetGUID()); @@ -11988,27 +11972,20 @@ Item* Player::_StoreItem(uint16 pos, Item *pItem, uint32 count, bool clone, bool // need update known currency if (slot >= CURRENCYTOKEN_SLOT_START && slot < CURRENCYTOKEN_SLOT_END) AddKnownCurrency(pItem->GetEntry()); - - if (IsInWorld() && update) - { - pItem->AddToWorld(); - pItem->SendUpdateToPlayer(this); - } - - pItem->SetState(ITEM_CHANGED, this); } - else if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag)) - { + else pBag->StoreItem(slot, pItem, update); - if (IsInWorld() && update) - { - pItem->AddToWorld(); - pItem->SendUpdateToPlayer(this); - } - pItem->SetState(ITEM_CHANGED, this); - pBag->SetState(ITEM_CHANGED, this); + + if (IsInWorld() && update) + { + pItem->AddToWorld(); + pItem->SendUpdateToPlayer(this); } + pItem->SetState(ITEM_CHANGED, this); + if (pBag) + pBag->SetState(ITEM_CHANGED, this); + AddEnchantmentDurations(pItem); AddItemDurations(pItem); @@ -12307,11 +12284,9 @@ void Player::RemoveItem(uint8 bag, uint8 slot, bool update) if (slot < EQUIPMENT_SLOT_END) SetVisibleItemSlot(slot, NULL); } - else - { - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag)) - pBag->RemoveItem(slot, update); - } + else if (Bag *pBag = GetBagByPos(bag)) + pBag->RemoveItem(slot, update); + pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, 0); // pItem->SetUInt64Value(ITEM_FIELD_OWNER, 0); not clear owner at remove (it will be set at store). This used in mail and auction code pItem->SetSlot(NULL_SLOT); @@ -12371,11 +12346,9 @@ void Player::DestroyItem(uint8 bag, uint8 slot, bool update) sLog->outDebug(LOG_FILTER_PLAYER_ITEMS, "STORAGE: DestroyItem bag = %u, slot = %u, item = %u", bag, slot, pItem->GetEntry()); // Also remove all contained items if the item is a bag. // This if() prevents item saving crashes if the condition for a bag to be empty before being destroyed was bypassed somehow. - if (pItem->IsBag() && !((Bag*)pItem)->IsEmpty()) - { + if (pItem->IsNotEmptyBag()) for (uint8 i = 0; i < MAX_BAG_SIZE; ++i) DestroyItem(slot, i, update); - } if (pItem->HasFlag(ITEM_FIELD_FLAGS, ITEM_FLAG_WRAPPED)) CharacterDatabase.PExecute("DELETE FROM character_gifts WHERE item_guid = '%u'", pItem->GetGUIDLow()); @@ -12437,7 +12410,7 @@ void Player::DestroyItem(uint8 bag, uint8 slot, bool update) m_items[slot] = NULL; } - else if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, bag)) + else if (Bag *pBag = GetBagByPos(bag)) pBag->RemoveItem(slot, update); if (IsInWorld() && update) @@ -12518,7 +12491,7 @@ void Player::DestroyItemCount(uint32 item, uint32 count, bool update, bool unequ // in inventory bags for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { - if (Bag *pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag *pBag = GetBagByPos(i)) { for (uint32 j = 0; j < pBag->GetBagSize(); j++) { @@ -12599,7 +12572,7 @@ void Player::DestroyZoneLimitedItem(bool update, uint32 new_zone) // in inventory bags for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); j++) if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->IsLimitedToAnotherMapOrZone(GetMapId(), new_zone)) @@ -12626,7 +12599,7 @@ void Player::DestroyConjuredItems(bool update) // in inventory bags for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); j++) if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->IsConjuredConsumable()) @@ -12643,33 +12616,21 @@ Item* Player::GetItemByEntry(uint32 entry) const { // in inventory for (int i = INVENTORY_SLOT_ITEM_START; i < INVENTORY_SLOT_ITEM_END; ++i) - { if (Item* pItem = GetItemByPos(INVENTORY_SLOT_BAG_0, i)) if (pItem->GetEntry() == entry) return pItem; - } - for (int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - { - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) - { + if (Bag* pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); ++j) - { if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->GetEntry() == entry) return pItem; - } - } - } - for (int i = EQUIPMENT_SLOT_START; i < INVENTORY_SLOT_BAG_END; ++i) - { if (Item* pItem = GetItemByPos(INVENTORY_SLOT_BAG_0, i)) if (pItem->GetEntry() == entry) return pItem; - } return NULL; } @@ -12837,10 +12798,10 @@ void Player::SwapItem(uint16 src, uint16 dst) } // check unequip potability for equipped items and bank bags - if (IsEquipmentPos (src) || IsBagPos (src)) + if (IsEquipmentPos(src) || IsBagPos(src)) { // bags can be swapped with empty bag slots, or with empty bag (items move possibility checked later) - uint8 msg = CanUnequipItem(src, !IsBagPos (src) || IsBagPos (dst) || (pDstItem && pDstItem->IsBag() && ((Bag*)pDstItem)->IsEmpty())); + uint8 msg = CanUnequipItem(src, !IsBagPos(src) || IsBagPos(dst) || (pDstItem && pDstItem->ToBag() && pDstItem->ToBag()->IsEmpty())); if (msg != EQUIP_ERR_OK) { SendEquipError(msg, pSrcItem, pDstItem); @@ -12874,10 +12835,10 @@ void Player::SwapItem(uint16 src, uint16 dst) } // check unequip potability for equipped items and bank bags - if (IsEquipmentPos (dst) || IsBagPos (dst)) + if (IsEquipmentPos(dst) || IsBagPos(dst)) { // bags can be swapped with empty bag slots, or with empty bag (items move possibility checked later) - uint8 msg = CanUnequipItem(dst, !IsBagPos (dst) || IsBagPos (src) || (pSrcItem->IsBag() && ((Bag*)pSrcItem)->IsEmpty())); + uint8 msg = CanUnequipItem(dst, !IsBagPos(dst) || IsBagPos(src) || (pSrcItem->ToBag() && pSrcItem->ToBag()->IsEmpty())); if (msg != EQUIP_ERR_OK) { SendEquipError(msg, pSrcItem, pDstItem); @@ -13029,65 +12990,68 @@ void Player::SwapItem(uint16 src, uint16 dst) } // Check bag swap with item exchange (one from empty in not bag possition (equipped (not possible in fact) or store) - if (pSrcItem->IsBag() && pDstItem->IsBag()) + if (Bag* srcBag = pSrcItem->ToBag()) { - Bag* emptyBag = NULL; - Bag* fullBag = NULL; - if (((Bag*)pSrcItem)->IsEmpty() && !IsBagPos(src)) - { - emptyBag = (Bag*)pSrcItem; - fullBag = (Bag*)pDstItem; - } - else if (((Bag*)pDstItem)->IsEmpty() && !IsBagPos(dst)) + if (Bag* dstBag = pDstItem->ToBag()) { - emptyBag = (Bag*)pDstItem; - fullBag = (Bag*)pSrcItem; - } + Bag* emptyBag = NULL; + Bag* fullBag = NULL; + if (srcBag->IsEmpty() && !IsBagPos(src)) + { + emptyBag = srcBag; + fullBag = dstBag; + } + else if (dstBag->IsEmpty() && !IsBagPos(dst)) + { + emptyBag = dstBag; + fullBag = srcBag; + } - // bag swap (with items exchange) case - if (emptyBag && fullBag) - { - ItemPrototype const* emptyProto = emptyBag->GetProto(); + // bag swap (with items exchange) case + if (emptyBag && fullBag) + { + ItemPrototype const* emptyProto = emptyBag->GetProto(); - uint32 count = 0; + uint32 count = 0; - for (uint32 i=0; i < fullBag->GetBagSize(); ++i) - { - Item *bagItem = fullBag->GetItemByPos(i); - if (!bagItem) - continue; + for (uint32 i=0; i < fullBag->GetBagSize(); ++i) + { + Item *bagItem = fullBag->GetItemByPos(i); + if (!bagItem) + continue; + + ItemPrototype const* bagItemProto = bagItem->GetProto(); + if (!bagItemProto || !ItemCanGoIntoBag(bagItemProto, emptyProto)) + { + // one from items not go to empty target bag + SendEquipError(EQUIP_ERR_NONEMPTY_BAG_OVER_OTHER_BAG, pSrcItem, pDstItem); + return; + } - ItemPrototype const* bagItemProto = bagItem->GetProto(); - if (!bagItemProto || !ItemCanGoIntoBag(bagItemProto, emptyProto)) + ++count; + } + + if (count > emptyBag->GetBagSize()) { - // one from items not go to empty target bag - SendEquipError(EQUIP_ERR_NONEMPTY_BAG_OVER_OTHER_BAG, pSrcItem, pDstItem); + // too small targeted bag + SendEquipError(EQUIP_ERR_ITEMS_CANT_BE_SWAPPED, pSrcItem, pDstItem); return; } - ++count; - } - - if (count > emptyBag->GetBagSize()) - { - // too small targeted bag - SendEquipError(EQUIP_ERR_ITEMS_CANT_BE_SWAPPED, pSrcItem, pDstItem); - return; - } - - // Items swap - count = 0; // will pos in new bag - for (uint32 i = 0; i< fullBag->GetBagSize(); ++i) - { - Item *bagItem = fullBag->GetItemByPos(i); - if (!bagItem) - continue; + // Items swap + count = 0; // will pos in new bag + for (uint32 i = 0; i< fullBag->GetBagSize(); ++i) + { + Item *bagItem = fullBag->GetItemByPos(i); + if (!bagItem) + continue; - fullBag->RemoveItem(i, true); - emptyBag->StoreItem(count, bagItem, true); - bagItem->SetState(ITEM_CHANGED, this); + fullBag->RemoveItem(i, true); + emptyBag->StoreItem(count, bagItem, true); + bagItem->SetState(ITEM_CHANGED, this); - ++count; + ++count; + } } } } @@ -13119,7 +13083,7 @@ void Player::SwapItem(uint16 src, uint16 dst) bool released = false; if (IsBagPos(src)) { - Bag* bag = (Bag*)pSrcItem; + Bag* bag = pSrcItem->ToBag(); for (uint32 i = 0; i < bag->GetBagSize(); ++i) { if (Item *bagItem = bag->GetItemByPos(i)) @@ -13136,7 +13100,7 @@ void Player::SwapItem(uint16 src, uint16 dst) if (!released && IsBagPos(dst) && pDstItem) { - Bag* bag = (Bag*)pDstItem; + Bag* bag = pDstItem->ToBag(); for (uint32 i = 0; i < bag->GetBagSize(); ++i) { if (Item *bagItem = bag->GetItemByPos(i)) @@ -13476,7 +13440,7 @@ void Player::RemoveArenaEnchantments(EnchantmentSlot slot) // in inventory bags for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) - if (Bag* pBag = (Bag*)GetItemByPos(INVENTORY_SLOT_BAG_0, i)) + if (Bag* pBag = GetBagByPos(i)) for (uint32 j = 0; j < pBag->GetBagSize(); j++) if (Item* pItem = pBag->GetItemByPos(j)) if (pItem->GetEnchantmentId(slot)) @@ -17302,8 +17266,9 @@ void Player::_LoadInventory(PreparedQueryResult result, uint32 timeDiff) // Remember bags that may contain items in them if (err == EQUIP_ERR_OK) - if (item->IsBag() && IsBagPos(item->GetPos())) - bagMap[item->GetGUIDLow()] = (Bag*)item; + if (IsBagPos(item->GetPos())) + if (Bag* pBag = item->ToBag()) + bagMap[item->GetGUIDLow()] = pBag; } else { diff --git a/src/server/game/Entities/Player/Player.h b/src/server/game/Entities/Player/Player.h index fd7ce0fdda6..e20e0afcdbe 100755 --- a/src/server/game/Entities/Player/Player.h +++ b/src/server/game/Entities/Player/Player.h @@ -1175,6 +1175,7 @@ class Player : public Unit, public GridObject<Player> Item* GetItemByEntry(uint32 entry) const; Item* GetItemByPos(uint16 pos) const; Item* GetItemByPos(uint8 bag, uint8 slot) const; + Bag* GetBagByPos(uint8 slot) const; inline Item* GetUseableItemByPos(uint8 bag, uint8 slot) const //Does additional check for disarmed weapons { if (!CanUseAttackType(GetAttackBySlot(slot))) diff --git a/src/server/game/Guilds/Guild.cpp b/src/server/game/Guilds/Guild.cpp index ef7ba2b354c..93a7392e50e 100755 --- a/src/server/game/Guilds/Guild.cpp +++ b/src/server/game/Guilds/Guild.cpp @@ -801,7 +801,7 @@ bool Guild::PlayerMoveItemData::InitItem() if (m_pItem) { // Anti-WPE protection. Do not move non-empty bags to bank. - if (m_pItem->IsBag() && !((Bag*)m_pItem)->IsEmpty()) + if (m_pItem->IsNotEmptyBag()) { m_pPlayer->SendEquipError(EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS, m_pItem); m_pItem = NULL; diff --git a/src/server/game/Server/Protocol/Handlers/AuctionHouseHandler.cpp b/src/server/game/Server/Protocol/Handlers/AuctionHouseHandler.cpp index 61ff1fab095..454ecdeddf4 100755 --- a/src/server/game/Server/Protocol/Handlers/AuctionHouseHandler.cpp +++ b/src/server/game/Server/Protocol/Handlers/AuctionHouseHandler.cpp @@ -188,7 +188,7 @@ void WorldSession::HandleAuctionSellItem(WorldPacket & recv_data) return; } - if (it->IsBag() && !((Bag*)it)->IsEmpty()) + if (!it->IsNotEmptyBag()) { SendAuctionCommandResult(0, AUCTION_SELL_ITEM, AUCTION_INTERNAL_ERROR); return; diff --git a/src/server/game/Server/Protocol/Handlers/ItemHandler.cpp b/src/server/game/Server/Protocol/Handlers/ItemHandler.cpp index 716715ecb2f..770ed41badb 100755 --- a/src/server/game/Server/Protocol/Handlers/ItemHandler.cpp +++ b/src/server/game/Server/Protocol/Handlers/ItemHandler.cpp @@ -516,7 +516,7 @@ void WorldSession::HandleSellItemOpcode(WorldPacket & recv_data) } // prevent sell non empty bag by drag-and-drop at vendor's item list - if (pItem->IsBag() && !((Bag*)pItem)->IsEmpty()) + if (pItem->IsNotEmptyBag()) { _player->SendSellError(SELL_ERR_CANT_SELL_ITEM, pCreature, itemguid, 0); return; @@ -668,7 +668,7 @@ void WorldSession::HandleBuyItemInSlotOpcode(WorldPacket & recv_data) { for (int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) { - if (Bag *pBag = (Bag*)_player->GetItemByPos(INVENTORY_SLOT_BAG_0,i)) + if (Bag* pBag = _player->GetBagByPos(i)) { if (bagguid == pBag->GetGUID()) { diff --git a/src/server/game/Server/Protocol/Handlers/MailHandler.cpp b/src/server/game/Server/Protocol/Handlers/MailHandler.cpp index 7d910b4b96c..8e62949c7fe 100755 --- a/src/server/game/Server/Protocol/Handlers/MailHandler.cpp +++ b/src/server/game/Server/Protocol/Handlers/MailHandler.cpp @@ -219,7 +219,7 @@ void WorldSession::HandleSendMail(WorldPacket & recv_data) return; } - if (item->IsBag() && !((Bag*)item)->IsEmpty()) + if (item->IsNotEmptyBag()) { pl->SendMailResult(0, MAIL_SEND, MAIL_ERR_EQUIP_ERROR, EQUIP_ERR_CAN_ONLY_DO_WITH_EMPTY_BAGS); return; diff --git a/src/server/scripts/Commands/cs_debug.cpp b/src/server/scripts/Commands/cs_debug.cpp index 7fdd242f561..c6936727885 100644 --- a/src/server/scripts/Commands/cs_debug.cpp +++ b/src/server/scripts/Commands/cs_debug.cpp @@ -516,22 +516,17 @@ public: if (i >= BUYBACK_SLOT_START && i < BUYBACK_SLOT_END) continue; - Item *item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, i); - if (!item) continue; - if (!item->IsBag()) - { - if (item->GetState() == state) - handler->PSendSysMessage("bag: 255 slot: %d guid: %d owner: %d", item->GetSlot(), item->GetGUIDLow(), GUID_LOPART(item->GetOwnerGUID())); - } - else + if (Item *item = player->GetItemByPos(INVENTORY_SLOT_BAG_0, i)) { - Bag *bag = (Bag*)item; - for (uint8 j = 0; j < bag->GetBagSize(); ++j) + if (Bag* bag = item->ToBag()) { - Item* item2 = bag->GetItemByPos(j); - if (item2 && item2->GetState() == state) - handler->PSendSysMessage("bag: 255 slot: %d guid: %d owner: %d", item2->GetSlot(), item2->GetGUIDLow(), GUID_LOPART(item2->GetOwnerGUID())); + for (uint8 j = 0; j < bag->GetBagSize(); ++j) + if (Item* item2 = bag->GetItemByPos(j)) + if (item2->GetState() == state) + handler->PSendSysMessage("bag: 255 slot: %d guid: %d owner: %d", item2->GetSlot(), item2->GetGUIDLow(), GUID_LOPART(item2->GetOwnerGUID())); } + else if (item->GetState() == state) + handler->PSendSysMessage("bag: 255 slot: %d guid: %d owner: %d", item->GetSlot(), item->GetGUIDLow(), GUID_LOPART(item->GetOwnerGUID())); } } } @@ -619,9 +614,8 @@ public: error = true; continue; } - if (item->IsBag()) + if (Bag* bag = item->ToBag()) { - Bag *bag = (Bag*)item; for (uint8 j = 0; j < bag->GetBagSize(); ++j) { Item* item2 = bag->GetItemByPos(j); |