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
parent 3162a8dc61
commit 6f6ec21b72
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 )
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

@ -3828,8 +3828,7 @@ 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++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
if(Item* pItem = GetItemByPos( i, j ))
DurabilityLoss(pItem,percent);
}
@ -3873,8 +3872,7 @@ 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++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
if(Item* pItem = GetItemByPos( i, j ))
DurabilityPointsLoss(pItem,points);
}
@ -8009,16 +8007,12 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
pItem = GetItemByPos( i, j );
if( pItem && pItem->GetEntry() == item )
@ -8030,7 +8024,6 @@ uint8 Player::CanUnequipItems( uint32 item, uint32 count ) const
}
}
}
}
// not found req. item count and have unequippable items
return res;
@ -8117,10 +8110,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = pBag->GetItemByPos( j );
if( pItem && pItem->GetGUID() == guid )
@ -8128,16 +8118,12 @@ Item* Player::GetItemByGuid( uint64 guid ) const
}
}
}
}
for(int i = BANK_SLOT_BAG_START; i < BANK_SLOT_BAG_END; i++)
{
Bag *pBag = (Bag*)GetItemByPos( INVENTORY_SLOT_BAG_0, i );
if( pBag )
{
ItemPrototype const *pBagProto = pBag->GetProto();
if( pBagProto )
{
for(uint32 j = 0; j < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = pBag->GetItemByPos( j );
if( pItem && pItem->GetGUID() == guid )
@ -8145,7 +8131,6 @@ Item* Player::GetItemByGuid( uint64 guid ) const
}
}
}
}
return NULL;
}
@ -8300,9 +8285,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = GetItemByPos( i, j );
if( pItem && pItem->GetEntry() == item )
@ -8314,7 +8297,6 @@ bool Player::HasItemCount( uint32 item, uint32 count, bool inBankAlso ) const
}
}
}
}
if(inBankAlso)
{
@ -8332,9 +8314,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = GetItemByPos( i, j );
if( pItem && pItem->GetEntry() == item )
@ -8347,7 +8327,6 @@ bool Player::HasItemCount( uint32 item, uint32 count, bool inBankAlso ) const
}
}
}
}
return false;
}
@ -8420,17 +8399,11 @@ 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 < pBagProto->ContainerSlots; ++j)
for(uint32 j = 0; j < pBag->GetBagSize(); ++j)
{
pItem = GetItemByPos( i, j );
if( pItem && IsTotemCategoryCompatiableWith(pItem->GetProto()->TotemCategory,TotemCategory ))
@ -8438,7 +8411,6 @@ bool Player::HasItemTotemCategory( uint32 TotemCategory ) const
}
}
}
}
return false;
}
@ -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,17 +9008,9 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
pItem2 = GetItemByPos( i, j );
if (pItem2 && !pItem2->IsInTrade())
@ -9056,7 +9020,6 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const
}
}
}
}
// check free space for all items
for (int k=0;k<count;k++)
@ -9119,10 +9082,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
pItem2 = GetItemByPos( t, j );
if( pItem2 && pItem2->GetEntry() == pItem->GetEntry() && inv_bags[t-INVENTORY_SLOT_BAG_START][j] + pItem->GetCount() <= pProto->Stackable )
@ -9134,7 +9094,6 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const
}
}
}
}
if (b_found) continue;
}
@ -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,10 +9162,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
if( inv_bags[t-INVENTORY_SLOT_BAG_START][j] == 0 )
{
@ -9217,7 +9173,6 @@ uint8 Player::CanStoreItems( Item **pItems,int count) const
}
}
}
}
// no free slot found?
if (!b_found)
@ -10307,17 +10262,11 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
pItem = pBag->GetItemByPos(j);
if( pItem && pItem->GetEntry() == item )
@ -10345,7 +10294,6 @@ void Player::DestroyItemCount( uint32 item, uint32 count, bool update, bool uneq
}
}
}
}
// in equipment and bag list
for(int i = EQUIPMENT_SLOT_START; i < INVENTORY_SLOT_BAG_END; i++)
@ -10402,10 +10350,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = pBag->GetItemByPos(j);
if( pItem && pItem->IsLimitedToAnotherMapOrZone(GetMapId(),new_zone) )
@ -10413,7 +10358,6 @@ void Player::DestroyZoneLimitedItem( bool update, uint32 new_zone )
}
}
}
}
// in equipment and bag list
for(int i = EQUIPMENT_SLOT_START; i < INVENTORY_SLOT_BAG_END; i++)
@ -10446,10 +10390,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = pBag->GetItemByPos(j);
if( pItem && pItem->GetProto() &&
@ -10459,7 +10400,6 @@ void Player::DestroyConjuredItems( bool update )
}
}
}
}
// in equipment and bag list
for(int i = EQUIPMENT_SLOT_START; i < INVENTORY_SLOT_BAG_END; i++)
@ -11104,10 +11044,7 @@ 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 < pBagProto->ContainerSlots; j++)
for(uint32 j = 0; j < pBag->GetBagSize(); j++)
{
Item* pItem = pBag->GetItemByPos(j);
if( pItem && pItem->GetEnchantmentId(slot) )
@ -11115,7 +11052,6 @@ void Player::RemoveAllEnchantments(EnchantmentSlot slot)
}
}
}
}
}
// duration == 0 will remove item enchant

View file

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