From 6f6ec21b724d0f03cf982ad260446a03211fa873 Mon Sep 17 00:00:00 2001 From: VladimirMangos Date: Sat, 1 Nov 2008 21:08:39 +0300 Subject: [PATCH] Avoid access to bag item prototype for getting bag size, use related item update field instead as more fast source. --- src/game/Bag.cpp | 61 ++++------- src/game/Bag.h | 1 + src/game/Level2.cpp | 6 +- src/game/Player.cpp | 240 +++++++++++++++-------------------------- src/game/debugcmds.cpp | 6 +- 5 files changed, 112 insertions(+), 202 deletions(-) diff --git a/src/game/Bag.cpp b/src/game/Bag.cpp index 6345ff4d5..cd6c4d69a 100644 --- a/src/game/Bag.cpp +++ b/src/game/Bag.cpp @@ -37,30 +37,25 @@ Bag::Bag( ): Item() Bag::~Bag() { - for(int i = 0; iAddToWorld(); - } } void Bag::RemoveFromWorld() { - for(int i = 0; iRemoveFromWorld(); - } Item::RemoveFromWorld(); } @@ -109,7 +104,7 @@ bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result) return false; // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`) - for (uint32 i = 0; i < GetProto()->ContainerSlots; i++) + for (int i = 0; i < MAX_BAG_SIZE; ++i) { SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0); if (m_bagslot[i]) @@ -125,21 +120,16 @@ bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult *result) void Bag::DeleteFromDB() { for (int i = 0; i < MAX_BAG_SIZE; i++) - { if (m_bagslot[i]) - { m_bagslot[i]->DeleteFromDB(); - } - } Item::DeleteFromDB(); } uint32 Bag::GetFreeSlots() const { - uint32 ContainerSlots=GetProto()->ContainerSlots; uint32 slots = 0; - for (uint8 i=0; i BuildCreateUpdateBlockForPlayer( data, target ); - } } // If the bag is empty returns true bool Bag::IsEmpty() const { - uint32 ContainerSlots=GetProto()->ContainerSlots; - for(uint32 i=0; i < ContainerSlots; i++) - if (m_bagslot[i]) return false; + for(uint32 i = 0; i < GetBagSize(); ++i) + if (m_bagslot[i]) + return false; return true; } uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const { - uint32 ContainerSlots=GetProto()->ContainerSlots; - Item *pItem; uint32 count = 0; - for(uint32 i=0; i < ContainerSlots; i++) + for(uint32 i=0; i < GetBagSize(); ++i) { pItem = m_bagslot[i]; if( pItem && pItem != eItem && pItem->GetEntry() == item ) @@ -208,7 +194,7 @@ uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const if(eItem && eItem->GetProto()->GemProperties) { - for(uint32 i=0; i < ContainerSlots; i++) + for(uint32 i=0; i < GetBagSize(); ++i) { pItem = m_bagslot[i]; if( pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color ) @@ -221,29 +207,18 @@ uint32 Bag::GetItemCount( uint32 item, Item* eItem ) const uint8 Bag::GetSlotByItemGUID(uint64 guid) const { - uint32 ContainerSlots=GetProto()->ContainerSlots; - - for(uint32 i=0;iGetGUID() == guid) return i; - } + return NULL_SLOT; } -// Adds an item to a bag slot -// - slot can be NULL_SLOT, in that case function searchs for a free slot -// - Return values: 0 - item not added -// 1 - item added to a free slot (and perhaps to a stack) -// 2 - item added to a stack (item should be deleted) Item* Bag::GetItemByPos( uint8 slot ) const { - ItemPrototype const *pBagProto = GetProto(); - if( pBagProto ) - { - if( slot < pBagProto->ContainerSlots ) - return m_bagslot[slot]; - } + if( slot < GetBagSize() ) + return m_bagslot[slot]; + return NULL; } diff --git a/src/game/Bag.h b/src/game/Bag.h index 07ae17341..21458546e 100644 --- a/src/game/Bag.h +++ b/src/game/Bag.h @@ -50,6 +50,7 @@ class Bag : public Item uint8 GetSlotByItemGUID(uint64 guid) const; bool IsEmpty() const; uint32 GetFreeSlots() const; + uint32 GetBagSize() const { return GetUInt32Value(CONTAINER_FIELD_NUM_SLOTS); } // DB operations // overwrite virtual Item::SaveToDB diff --git a/src/game/Level2.cpp b/src/game/Level2.cpp index bd0f350cf..4ca6c62ae 100644 --- a/src/game/Level2.cpp +++ b/src/game/Level2.cpp @@ -859,12 +859,12 @@ bool ChatHandler::HandleItemMoveCommand(const char* args) srcslot = (uint8)atoi(pParam1); dstslot = (uint8)atoi(pParam2); - uint16 src = ((INVENTORY_SLOT_BAG_0 << 8) | srcslot); - uint16 dst = ((INVENTORY_SLOT_BAG_0 << 8) | dstslot); - if(srcslot==dstslot) return true; + uint16 src = ((INVENTORY_SLOT_BAG_0 << 8) | srcslot); + uint16 dst = ((INVENTORY_SLOT_BAG_0 << 8) | dstslot); + m_session->GetPlayer()->SwapItem( src, dst ); return true; diff --git a/src/game/Player.cpp b/src/game/Player.cpp index 922e45884..493bd232c 100644 --- a/src/game/Player.cpp +++ b/src/game/Player.cpp @@ -3828,10 +3828,9 @@ void Player::DurabilityLossAll(double percent, bool inventory) for(int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) if(Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) - if(ItemPrototype const *pBagProto = pBag->GetProto()) - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - if(Item* pItem = GetItemByPos( i, j )) - DurabilityLoss(pItem,percent); + for(uint32 j = 0; j < pBag->GetBagSize(); j++) + if(Item* pItem = GetItemByPos( i, j )) + DurabilityLoss(pItem,percent); } } @@ -3873,10 +3872,9 @@ void Player::DurabilityPointsLossAll(int32 points, bool inventory) for(int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) if(Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) - if(ItemPrototype const *pBagProto = pBag->GetProto()) - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - if(Item* pItem = GetItemByPos( i, j )) - DurabilityPointsLoss(pItem,points); + for(uint32 j = 0; j < pBag->GetBagSize(); j++) + if(Item* pItem = GetItemByPos( i, j )) + DurabilityPointsLoss(pItem,points); } } @@ -8009,24 +8007,19 @@ uint8 Player::CanUnequipItems( uint32 item, uint32 count ) const } } Bag *pBag; - ItemPrototype const *pBagProto; for(int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + pItem = GetItemByPos( i, j ); + if( pItem && pItem->GetEntry() == item ) { - pItem = GetItemByPos( i, j ); - if( pItem && pItem->GetEntry() == item ) - { - tempcount += pItem->GetCount(); - if( tempcount >= count ) - return EQUIP_ERR_OK; - } + tempcount += pItem->GetCount(); + if( tempcount >= count ) + return EQUIP_ERR_OK; } } } @@ -8117,15 +8110,11 @@ Item* Player::GetItemByGuid( uint64 guid ) const Bag *pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - ItemPrototype const *pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - { - Item* pItem = pBag->GetItemByPos( j ); - if( pItem && pItem->GetGUID() == guid ) - return pItem; - } + Item* pItem = pBag->GetItemByPos( j ); + if( pItem && pItem->GetGUID() == guid ) + return pItem; } } } @@ -8134,15 +8123,11 @@ Item* Player::GetItemByGuid( uint64 guid ) const Bag *pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - ItemPrototype const *pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - { - Item* pItem = pBag->GetItemByPos( j ); - if( pItem && pItem->GetGUID() == guid ) - return pItem; - } + Item* pItem = pBag->GetItemByPos( j ); + if( pItem && pItem->GetGUID() == guid ) + return pItem; } } } @@ -8300,17 +8285,14 @@ bool Player::HasItemCount( uint32 item, uint32 count, bool inBankAlso ) const { if(Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) { - if(ItemPrototype const *pBagProto = pBag->GetProto()) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + Item* pItem = GetItemByPos( i, j ); + if( pItem && pItem->GetEntry() == item ) { - Item* pItem = GetItemByPos( i, j ); - if( pItem && pItem->GetEntry() == item ) - { - tempcount += pItem->GetCount(); - if( tempcount >= count ) - return true; - } + tempcount += pItem->GetCount(); + if( tempcount >= count ) + return true; } } } @@ -8332,17 +8314,14 @@ bool Player::HasItemCount( uint32 item, uint32 count, bool inBankAlso ) const { if(Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) { - if(ItemPrototype const *pBagProto = pBag->GetProto()) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + Item* pItem = GetItemByPos( i, j ); + if( pItem && pItem->GetEntry() == item ) { - Item* pItem = GetItemByPos( i, j ); - if( pItem && pItem->GetEntry() == item ) - { - tempcount += pItem->GetCount(); - if( tempcount >= count ) - return true; - } + tempcount += pItem->GetCount(); + if( tempcount >= count ) + return true; } } } @@ -8420,22 +8399,15 @@ bool Player::HasItemTotemCategory( uint32 TotemCategory ) const if( pItem && IsTotemCategoryCompatiableWith(pItem->GetProto()->TotemCategory,TotemCategory )) return true; } - Bag *pBag; - ItemPrototype const *pBagProto; for(uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; ++i) { - pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); - if( pBag ) + if(Bag *pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) { - pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); ++j) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; ++j) - { - pItem = GetItemByPos( i, j ); - if( pItem && IsTotemCategoryCompatiableWith(pItem->GetProto()->TotemCategory,TotemCategory )) - return true; - } + pItem = GetItemByPos( i, j ); + if( pItem && IsTotemCategoryCompatiableWith(pItem->GetProto()->TotemCategory,TotemCategory )) + return true; } } } @@ -8529,7 +8501,7 @@ uint8 Player::_CanStoreItem_InBag( uint8 bag, ItemPosCountVec &dest, ItemPrototy if( !ItemCanGoIntoBag(pProto,pBagProto) ) return EQUIP_ERR_ITEM_DOESNT_GO_INTO_BAG; - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { // skip specific slot already processed in first called _CanStoreItem_InSpecificSlot if(j==skip_slot) @@ -9036,23 +9008,14 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const for(int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { - Bag *pBag; - ItemPrototype const *pBagProto; - - pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); - if( pBag ) + if(Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) { - pBagProto = pBag->GetProto(); - - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + pItem2 = GetItemByPos( i, j ); + if (pItem2 && !pItem2->IsInTrade()) { - pItem2 = GetItemByPos( i, j ); - if (pItem2 && !pItem2->IsInTrade()) - { - inv_bags[i-INVENTORY_SLOT_BAG_START][j] = pItem2->GetCount(); - } + inv_bags[i-INVENTORY_SLOT_BAG_START][j] = pItem2->GetCount(); } } } @@ -9119,18 +9082,14 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, t ); if( pBag ) { - pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + pItem2 = GetItemByPos( t, j ); + if( pItem2 && pItem2->GetEntry() == pItem->GetEntry() && inv_bags[t-INVENTORY_SLOT_BAG_START][j] + pItem->GetCount() <= pProto->Stackable ) { - pItem2 = GetItemByPos( t, j ); - if( pItem2 && pItem2->GetEntry() == pItem->GetEntry() && inv_bags[t-INVENTORY_SLOT_BAG_START][j] + pItem->GetCount() <= pProto->Stackable ) - { - inv_bags[t-INVENTORY_SLOT_BAG_START][j] += pItem->GetCount(); - b_found = true; - break; - } + inv_bags[t-INVENTORY_SLOT_BAG_START][j] += pItem->GetCount(); + b_found = true; + break; } } } @@ -9169,7 +9128,7 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const if( pBagProto && (pBagProto->Class != ITEM_CLASS_CONTAINER || pBagProto->SubClass != ITEM_SUBCLASS_CONTAINER) && ItemCanGoIntoBag(pProto,pBagProto) ) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { if( inv_bags[t-INVENTORY_SLOT_BAG_START][j] == 0 ) { @@ -9203,17 +9162,13 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, t ); if( pBag ) { - pBagProto = pBag->GetProto(); - if( pBagProto && ItemCanGoIntoBag(pProto,pBagProto)) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + if( inv_bags[t-INVENTORY_SLOT_BAG_START][j] == 0 ) { - if( inv_bags[t-INVENTORY_SLOT_BAG_START][j] == 0 ) - { - inv_bags[t-INVENTORY_SLOT_BAG_START][j] = 1; - b_found = true; - break; - } + inv_bags[t-INVENTORY_SLOT_BAG_START][j] = 1; + b_found = true; + break; } } } @@ -10307,40 +10262,33 @@ void Player::DestroyItemCount( uint32 item, uint32 count, bool update, bool uneq } // in inventory bags - Bag *pBag; - ItemPrototype const *pBagProto; for(int i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++) { - pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); - if( pBag ) + if(Bag *pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i )) { - pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) + pItem = pBag->GetItemByPos(j); + if( pItem && pItem->GetEntry() == item ) { - pItem = pBag->GetItemByPos(j); - if( pItem && pItem->GetEntry() == item ) + // all items in bags can be unequipped + if( pItem->GetCount() + remcount <= count ) { - // all items in bags can be unequipped - if( pItem->GetCount() + remcount <= count ) - { - remcount += pItem->GetCount(); - DestroyItem( i, j, update ); + remcount += pItem->GetCount(); + DestroyItem( i, j, update ); - if(remcount >=count) - return; - } - else - { - pProto = pItem->GetProto(); - ItemRemovedQuestCheck( pItem->GetEntry(), count - remcount ); - pItem->SetCount( pItem->GetCount() - count + remcount ); - if( IsInWorld() && update ) - pItem->SendUpdateToPlayer( this ); - pItem->SetState(ITEM_CHANGED, this); + if(remcount >=count) return; - } + } + else + { + pProto = pItem->GetProto(); + ItemRemovedQuestCheck( pItem->GetEntry(), count - remcount ); + pItem->SetCount( pItem->GetCount() - count + remcount ); + if( IsInWorld() && update ) + pItem->SendUpdateToPlayer( this ); + pItem->SetState(ITEM_CHANGED, this); + return; } } } @@ -10402,15 +10350,11 @@ void Player::DestroyZoneLimitedItem( bool update, uint32 new_zone ) Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - ItemPrototype const *pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - { - Item* pItem = pBag->GetItemByPos(j); - if( pItem && pItem->IsLimitedToAnotherMapOrZone(GetMapId(),new_zone) ) - DestroyItem( i, j, update); - } + Item* pItem = pBag->GetItemByPos(j); + if( pItem && pItem->IsLimitedToAnotherMapOrZone(GetMapId(),new_zone) ) + DestroyItem( i, j, update); } } } @@ -10446,17 +10390,13 @@ void Player::DestroyConjuredItems( bool update ) Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - ItemPrototype const *pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - { - Item* pItem = pBag->GetItemByPos(j); - if( pItem && pItem->GetProto() && - (pItem->GetProto()->Class == ITEM_CLASS_CONSUMABLE) && - (pItem->GetProto()->Flags & ITEM_FLAGS_CONJURED) ) - DestroyItem( i, j, update); - } + Item* pItem = pBag->GetItemByPos(j); + if( pItem && pItem->GetProto() && + (pItem->GetProto()->Class == ITEM_CLASS_CONSUMABLE) && + (pItem->GetProto()->Flags & ITEM_FLAGS_CONJURED) ) + DestroyItem( i, j, update); } } } @@ -11104,15 +11044,11 @@ void Player::RemoveAllEnchantments(EnchantmentSlot slot) Bag* pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i ); if( pBag ) { - ItemPrototype const *pBagProto = pBag->GetProto(); - if( pBagProto ) + for(uint32 j = 0; j < pBag->GetBagSize(); j++) { - for(uint32 j = 0; j < pBagProto->ContainerSlots; j++) - { - Item* pItem = pBag->GetItemByPos(j); - if( pItem && pItem->GetEnchantmentId(slot) ) - pItem->ClearEnchantment(slot); - } + Item* pItem = pBag->GetItemByPos(j); + if( pItem && pItem->GetEnchantmentId(slot) ) + pItem->ClearEnchantment(slot); } } } diff --git a/src/game/debugcmds.cpp b/src/game/debugcmds.cpp index 83754be27..df5e4348b 100644 --- a/src/game/debugcmds.cpp +++ b/src/game/debugcmds.cpp @@ -319,8 +319,7 @@ bool ChatHandler::HandleGetItemState(const char* args) else { Bag *bag = (Bag*)item; - const ItemPrototype *proto = bag->GetProto(); - for (uint8 j = 0; j < proto->ContainerSlots; ++j) + for (uint8 j = 0; j < bag->GetBagSize(); ++j) { Item* item = bag->GetItemByPos(j); if (item && item->GetState() == state) @@ -416,8 +415,7 @@ bool ChatHandler::HandleGetItemState(const char* args) if(item->IsBag()) { Bag *bag = (Bag*)item; - const ItemPrototype *proto = bag->GetProto(); - for (uint8 j = 0; j < proto->ContainerSlots; ++j) + for (uint8 j = 0; j < bag->GetBagSize(); ++j) { Item* item = bag->GetItemByPos(j); if (!item) continue;