/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#include "Common.h"
#include "ObjectMgr.h"
#include "DatabaseEnv.h"
#include "Bag.h"
#include "Log.h"
#include "UpdateData.h"
#include "Player.h"
#include
Bag::Bag(): Item()
{
m_objectType |= TYPEMASK_CONTAINER;
m_objectTypeId = TYPEID_CONTAINER;
m_entityFragments.Add(WowCS::EntityFragment::Tag_Container, false);
memset(m_bagslot, 0, sizeof(Item*) * MAX_BAG_SIZE);
}
Bag::~Bag()
{
for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
if (Item* item = m_bagslot[i])
{
if (item->IsInWorld())
{
TC_LOG_FATAL("entities.player.items", "Item {} (slot {}, bag slot {}) in bag {} (slot {}, bag slot {}, m_bagslot {}) is to be deleted but is still in world.",
item->GetEntry(), (uint32)item->GetSlot(), (uint32)item->GetBagSlot(),
GetEntry(), (uint32)GetSlot(), (uint32)GetBagSlot(), (uint32)i);
item->RemoveFromWorld();
}
delete m_bagslot[i];
}
}
void Bag::AddToWorld()
{
Item::AddToWorld();
for (uint32 i = 0; i < GetBagSize(); ++i)
if (m_bagslot[i])
m_bagslot[i]->AddToWorld();
}
void Bag::RemoveFromWorld()
{
for (uint32 i = 0; i < GetBagSize(); ++i)
if (m_bagslot[i])
m_bagslot[i]->RemoveFromWorld();
Item::RemoveFromWorld();
}
bool Bag::Create(ObjectGuid::LowType guidlow, uint32 itemid, ItemContext context, Player const* owner)
{
ItemTemplate const* itemProto = sObjectMgr->GetItemTemplate(itemid);
if (!itemProto || itemProto->GetContainerSlots() > MAX_BAG_SIZE)
return false;
Object::_Create(ObjectGuid::Create(guidlow));
_bonusData.Initialize(itemProto);
SetEntry(itemid);
SetObjectScale(1.0f);
if (owner)
{
SetOwnerGUID(owner->GetGUID());
SetContainedIn(owner->GetGUID());
}
SetUpdateFieldValue(m_values.ModifyValue(&Item::m_itemData).ModifyValue(&UF::ItemData::MaxDurability), itemProto->MaxDurability);
SetDurability(itemProto->MaxDurability);
SetCount(1);
SetContext(context);
// Setting the number of Slots the Container has
SetBagSize(itemProto->GetContainerSlots());
// Cleaning 20 slots
for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
{
SetSlot(i, ObjectGuid::Empty);
m_bagslot[i] = nullptr;
}
return true;
}
void Bag::SaveToDB(CharacterDatabaseTransaction trans)
{
Item::SaveToDB(trans);
}
bool Bag::LoadFromDB(ObjectGuid::LowType guid, ObjectGuid owner_guid, Field* fields, uint32 entry)
{
if (!Item::LoadFromDB(guid, owner_guid, fields, entry))
return false;
ItemTemplate const* itemProto = GetTemplate(); // checked in Item::LoadFromDB
SetBagSize(itemProto->GetContainerSlots());
// cleanup bag content related item value fields (its will be filled correctly from `character_inventory`)
for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
{
SetSlot(i, ObjectGuid::Empty);
delete m_bagslot[i];
m_bagslot[i] = nullptr;
}
return true;
}
void Bag::DeleteFromDB(CharacterDatabaseTransaction trans)
{
for (uint8 i = 0; i < MAX_BAG_SIZE; ++i)
if (m_bagslot[i])
m_bagslot[i]->DeleteFromDB(trans);
Item::DeleteFromDB(trans);
}
uint32 Bag::GetFreeSlots() const
{
uint32 slots = 0;
for (uint32 i=0; i < GetBagSize(); ++i)
if (!m_bagslot[i])
++slots;
return slots;
}
void Bag::RemoveItem(uint8 slot, bool /*update*/)
{
ASSERT(slot < MAX_BAG_SIZE);
if (m_bagslot[slot])
m_bagslot[slot]->SetContainer(nullptr);
m_bagslot[slot] = nullptr;
SetSlot(slot, ObjectGuid::Empty);
}
void Bag::StoreItem(uint8 slot, Item* pItem, bool /*update*/)
{
ASSERT(slot < MAX_BAG_SIZE);
if (pItem && pItem->GetGUID() != GetGUID())
{
m_bagslot[slot] = pItem;
SetSlot(slot, pItem->GetGUID());
pItem->SetContainedIn(GetGUID());
pItem->SetOwnerGUID(GetOwnerGUID());
pItem->SetContainer(this);
pItem->SetSlot(slot);
}
}
void Bag::BuildCreateUpdateBlockForPlayer(UpdateData* data, Player* target) const
{
Item::BuildCreateUpdateBlockForPlayer(data, target);
for (uint32 i = 0; i < GetBagSize(); ++i)
if (m_bagslot[i])
m_bagslot[i]->BuildCreateUpdateBlockForPlayer(data, target);
}
void Bag::BuildValuesCreate(ByteBuffer* data, UF::UpdateFieldFlag flags, Player const* target) const
{
m_objectData->WriteCreate(*data, flags, this, target);
m_itemData->WriteCreate(*data, flags, this, target);
m_containerData->WriteCreate(*data, flags, this, target);
}
void Bag::BuildValuesUpdate(ByteBuffer* data, UF::UpdateFieldFlag flags, Player const* target) const
{
*data << uint32(m_values.GetChangedObjectTypeMask());
if (m_values.HasChanged(TYPEID_OBJECT))
m_objectData->WriteUpdate(*data, flags, this, target);
if (m_values.HasChanged(TYPEID_ITEM))
m_itemData->WriteUpdate(*data, flags, this, target);
if (m_values.HasChanged(TYPEID_CONTAINER))
m_containerData->WriteUpdate(*data, flags, this, target);
}
void Bag::BuildValuesUpdateForPlayerWithMask(UpdateData* data, UF::ObjectData::Mask const& requestedObjectMask,
UF::ItemData::Mask const& requestedItemMask, UF::ContainerData::Mask const& requestedContainerMask, Player const* target) const
{
UF::UpdateFieldFlag flags = GetUpdateFieldFlagsFor(target);
UpdateMask valuesMask;
if (requestedObjectMask.IsAnySet())
valuesMask.Set(TYPEID_OBJECT);
UF::ItemData::Mask itemMask = requestedItemMask;
m_itemData->FilterDisallowedFieldsMaskForFlag(itemMask, flags);
if (itemMask.IsAnySet())
valuesMask.Set(TYPEID_ITEM);
if (requestedContainerMask.IsAnySet())
valuesMask.Set(TYPEID_CONTAINER);
ByteBuffer& buffer = PrepareValuesUpdateBuffer(data);
std::size_t sizePos = buffer.wpos();
buffer << uint32(0);
BuildEntityFragmentsForValuesUpdateForPlayerWithMask(&buffer, flags);
buffer << uint32(valuesMask.GetBlock(0));
if (valuesMask[TYPEID_OBJECT])
m_objectData->WriteUpdate(buffer, requestedObjectMask, true, this, target);
if (valuesMask[TYPEID_ITEM])
m_itemData->WriteUpdate(buffer, itemMask, true, this, target);
if (valuesMask[TYPEID_CONTAINER])
m_containerData->WriteUpdate(buffer, requestedContainerMask, true, this, target);
buffer.put(sizePos, buffer.wpos() - sizePos - 4);
data->AddUpdateBlock();
}
void Bag::ValuesUpdateForPlayerWithMaskSender::operator()(Player const* player) const
{
UpdateData udata(player->GetMapId());
WorldPacket packet;
Owner->BuildValuesUpdateForPlayerWithMask(&udata, ObjectMask.GetChangesMask(), ItemMask.GetChangesMask(), ContainerMask.GetChangesMask(), player);
udata.BuildPacket(&packet);
player->SendDirectMessage(&packet);
}
void Bag::ClearUpdateMask(bool remove)
{
m_values.ClearChangesMask(&Bag::m_containerData);
Item::ClearUpdateMask(remove);
}
// If the bag is empty returns true
bool Bag::IsEmpty() const
{
for (uint32 i = 0; i < GetBagSize(); ++i)
if (m_bagslot[i])
return false;
return true;
}
uint8 Bag::GetSlotByItemGUID(ObjectGuid guid) const
{
for (uint32 i = 0; i < GetBagSize(); ++i)
if (m_bagslot[i] != nullptr)
if (m_bagslot[i]->GetGUID() == guid)
return i;
return NULL_SLOT;
}
Item* Bag::GetItemByPos(uint8 slot) const
{
if (slot < GetBagSize())
return m_bagslot[slot];
return nullptr;
}
std::string Bag::GetDebugInfo() const
{
std::stringstream sstr;
sstr << Item::GetDebugInfo();
return sstr.str();
}
uint32 GetBagSize(Bag const* bag)
{
return bag->GetBagSize();
}
Item* GetItemInBag(Bag const* bag, uint8 slot)
{
return bag->GetItemByPos(slot);
}