Avoid access to bag item prototype for getting bag size, use related item update field instead as more fast source.

This commit is contained in:
VladimirMangos 2008-11-01 21:08:39 +03:00 committed by tomrus88
parent 99ff9ab5d9
commit aaddf4b51b
5 changed files with 112 additions and 202 deletions

View file

@ -37,30 +37,25 @@ Bag::Bag( ): Item()
Bag::~Bag()
{
for(int i = 0; i<MAX_BAG_SIZE; i++)
{
if(m_bagslot[i]) delete m_bagslot[i];
}
for(int i = 0; i < MAX_BAG_SIZE; ++i)
if (m_bagslot[i])
delete m_bagslot[i];
}
void Bag::AddToWorld()
{
Item::AddToWorld();
for(int i = 0; i<MAX_BAG_SIZE; i++)
{
for(uint32 i = 0; i < GetBagSize(); ++i)
if(m_bagslot[i])
m_bagslot[i]->AddToWorld();
}
}
void Bag::RemoveFromWorld()
{
for(int i = 0; i<MAX_BAG_SIZE; i++)
{
for(uint32 i = 0; i < GetBagSize(); ++i)
if(m_bagslot[i])
m_bagslot[i]->RemoveFromWorld();
}
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 <ContainerSlots; i++)
for (uint32 i=0; i < GetBagSize(); i++)
if (!m_bagslot[i])
++slots;
@ -176,30 +166,26 @@ void Bag::BuildCreateUpdateBlockForPlayer( UpdateData *data, Player *target ) co
{
Item::BuildCreateUpdateBlockForPlayer( data, target );
for (int i = 0; i < MAX_BAG_SIZE; i++)
{
for (uint32 i = 0; i < GetBagSize(); ++i)
if(m_bagslot[i])
m_bagslot[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;i<ContainerSlots;i++)
{
for(uint32 i = 0; i < GetBagSize(); ++i)
if(m_bagslot[i] != 0)
if(m_bagslot[i]->GetGUID() == 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;
}

View file

@ -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

View file

@ -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;

View file

@ -3859,10 +3859,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);
}
}
@ -3904,10 +3903,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);
}
}
@ -8074,24 +8072,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;
}
}
}
@ -8182,15 +8175,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;
}
}
}
@ -8199,15 +8188,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;
}
}
}
@ -8365,17 +8350,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;
}
}
}
@ -8397,17 +8379,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;
}
}
}
@ -8485,22 +8464,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;
}
}
}
@ -8606,7 +8578,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)
@ -9269,23 +9241,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();
}
}
}
@ -9388,18 +9351,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;
}
}
}
@ -9483,7 +9442,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 )
{
@ -9517,17 +9476,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;
}
}
}
@ -10620,40 +10575,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;
}
}
}
@ -10715,15 +10663,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);
}
}
}
@ -10759,17 +10703,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);
}
}
}
@ -11417,15 +11357,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);
}
}
}

View file

@ -324,8 +324,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)
@ -421,8 +420,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;