Files
TCPlayerbotCore/src/server/game/Handlers/VoidStorageHandler.cpp
T

255 lines
10 KiB
C++
Raw Normal View History

2012-07-30 16:39:46 +01:00
/*
2019-01-01 10:13:49 +01:00
* Copyright (C) 2008-2019 TrinityCore <https://www.trinitycore.org/>
2012-07-30 16:39:46 +01:00
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "WorldSession.h"
2017-06-04 01:00:45 +02:00
#include "Bag.h"
#include "CollectionMgr.h"
#include "Common.h"
#include "Log.h"
2012-07-30 16:39:46 +01:00
#include "ObjectAccessor.h"
2012-08-01 23:36:41 +01:00
#include "ObjectMgr.h"
2012-07-30 16:39:46 +01:00
#include "Player.h"
#include "VoidStoragePackets.h"
2017-06-04 01:00:45 +02:00
#include "World.h"
2012-07-30 16:39:46 +01:00
void WorldSession::SendVoidStorageTransferResult(VoidTransferError result)
{
SendPacket(WorldPackets::VoidStorage::VoidTransferResult(result).Write());
2012-07-30 16:39:46 +01:00
}
void WorldSession::HandleVoidStorageUnlock(WorldPackets::VoidStorage::UnlockVoidStorage& unlockVoidStorage)
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature* unit = _player->GetNPCIfCanInteractWith(unlockVoidStorage.Npc, UNIT_NPC_FLAG_VAULTKEEPER, UNIT_NPC_FLAG_2_NONE);
2012-07-30 16:39:46 +01:00
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageUnlock - %s not found or player can't interact with it.", unlockVoidStorage.Npc.ToString().c_str());
2012-07-30 16:39:46 +01:00
return;
}
if (_player->IsVoidStorageUnlocked())
2012-07-30 16:39:46 +01:00
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageUnlock - Player (%s, name: %s) tried to unlock void storage a 2nd time.", _player->GetGUID().ToString().c_str(), _player->GetName().c_str());
2012-07-30 16:39:46 +01:00
return;
}
_player->ModifyMoney(-int64(VOID_STORAGE_UNLOCK_COST));
_player->UnlockVoidStorage();
2012-07-30 16:39:46 +01:00
}
void WorldSession::HandleVoidStorageQuery(WorldPackets::VoidStorage::QueryVoidStorage& queryVoidStorage)
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature* unit = _player->GetNPCIfCanInteractWith(queryVoidStorage.Npc, NPCFlags(UNIT_NPC_FLAG_TRANSMOGRIFIER | UNIT_NPC_FLAG_VAULTKEEPER), UNIT_NPC_FLAG_2_NONE);
2012-07-30 16:39:46 +01:00
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageQuery - %s not found or player can't interact with it.", queryVoidStorage.Npc.ToString().c_str());
SendPacket(WorldPackets::VoidStorage::VoidStorageFailed().Write());
2012-07-30 16:39:46 +01:00
return;
}
if (!_player->IsVoidStorageUnlocked())
2012-07-30 16:39:46 +01:00
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageQuery - Player (%s, name: %s) queried void storage without unlocking it.", _player->GetGUID().ToString().c_str(), _player->GetName().c_str());
SendPacket(WorldPackets::VoidStorage::VoidStorageFailed().Write());
2012-07-30 16:39:46 +01:00
return;
}
uint8 count = 0;
for (uint8 i = 0; i < VOID_STORAGE_MAX_SLOT; ++i)
if (_player->GetVoidStorageItem(i))
2012-07-30 16:39:46 +01:00
++count;
WorldPackets::VoidStorage::VoidStorageContents voidStorageContents;
voidStorageContents.Items.reserve(VOID_STORAGE_MAX_SLOT);
2012-07-30 16:39:46 +01:00
for (uint8 i = 0; i < VOID_STORAGE_MAX_SLOT; ++i)
{
VoidStorageItem* item = _player->GetVoidStorageItem(i);
2012-07-30 16:39:46 +01:00
if (!item)
continue;
WorldPackets::VoidStorage::VoidItem voidItem;
voidItem.Guid = ObjectGuid::Create<HighGuid::Item>(item->ItemId);
voidItem.Creator = item->CreatorGuid;
voidItem.Slot = i;
voidItem.Item.Initialize(item);
2012-07-30 16:39:46 +01:00
voidStorageContents.Items.push_back(voidItem);
}
2012-07-30 16:39:46 +01:00
SendPacket(voidStorageContents.Write());
2012-07-30 16:39:46 +01:00
}
void WorldSession::HandleVoidStorageTransfer(WorldPackets::VoidStorage::VoidStorageTransfer& voidStorageTransfer)
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature* unit = _player->GetNPCIfCanInteractWith(voidStorageTransfer.Npc, UNIT_NPC_FLAG_VAULTKEEPER, UNIT_NPC_FLAG_2_NONE);
2012-07-30 16:39:46 +01:00
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - %s not found or player can't interact with it.", voidStorageTransfer.Npc.ToString().c_str());
2012-07-30 16:39:46 +01:00
return;
}
if (!_player->IsVoidStorageUnlocked())
2012-07-30 16:39:46 +01:00
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - Player (%s, name: %s) queried void storage without unlocking it.", _player->GetGUID().ToString().c_str(), _player->GetName().c_str());
2012-07-30 16:39:46 +01:00
return;
}
if (voidStorageTransfer.Deposits.size() > _player->GetNumOfVoidStorageFreeSlots())
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_FULL);
return;
}
uint32 freeBagSlots = 0;
if (!voidStorageTransfer.Withdrawals.empty())
2012-07-30 16:39:46 +01:00
{
// make this a Player function
for (uint8 i = INVENTORY_SLOT_BAG_START; i < INVENTORY_SLOT_BAG_END; i++)
if (Bag* bag = _player->GetBagByPos(i))
2012-07-30 16:39:46 +01:00
freeBagSlots += bag->GetFreeSlots();
uint8 inventoryEnd = INVENTORY_SLOT_ITEM_START + _player->GetInventorySlotCount();
for (uint8 i = INVENTORY_SLOT_ITEM_START; i < inventoryEnd; i++)
if (!_player->GetItemByPos(INVENTORY_SLOT_BAG_0, i))
2012-07-30 16:39:46 +01:00
++freeBagSlots;
}
if (voidStorageTransfer.Withdrawals.size() > freeBagSlots)
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_INVENTORY_FULL);
return;
}
if (!_player->HasEnoughMoney(uint64(voidStorageTransfer.Deposits.size() * VOID_STORAGE_STORE_ITEM_COST)))
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_NOT_ENOUGH_MONEY);
return;
}
WorldPackets::VoidStorage::VoidStorageTransferChanges voidStorageTransferChanges;
voidStorageTransferChanges.AddedItems.reserve(VOID_STORAGE_MAX_DEPOSIT);
voidStorageTransferChanges.RemovedItems.reserve(VOID_STORAGE_MAX_DEPOSIT);
2012-07-30 16:39:46 +01:00
uint8 depositCount = 0;
for (uint32 i = 0; i < voidStorageTransfer.Deposits.size(); ++i)
2012-07-30 16:39:46 +01:00
{
Item* item = _player->GetItemByGuid(voidStorageTransfer.Deposits[i]);
2012-07-30 16:39:46 +01:00
if (!item)
{
2015-04-29 20:53:47 +02:00
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - %s %s wants to deposit an invalid item (%s).", _player->GetGUID().ToString().c_str(), _player->GetName().c_str(), voidStorageTransfer.Deposits[i].ToString().c_str());
2012-07-30 16:39:46 +01:00
continue;
}
2019-06-03 20:40:34 +02:00
VoidStorageItem itemVS(sObjectMgr->GenerateVoidStorageItemId(), item->GetEntry(), item->GetCreator(),
item->GetItemRandomBonusListId(), item->GetModifier(ITEM_MODIFIER_TIMEWALKER_LEVEL), item->GetModifier(ITEM_MODIFIER_ARTIFACT_KNOWLEDGE_LEVEL),
2019-06-03 20:40:34 +02:00
uint8(item->m_itemData->Context), item->m_itemData->BonusListIDs);
2012-07-30 16:39:46 +01:00
WorldPackets::VoidStorage::VoidItem voidItem;
voidItem.Guid = ObjectGuid::Create<HighGuid::Item>(itemVS.ItemId);
2019-06-03 20:40:34 +02:00
voidItem.Creator = item->GetCreator();
voidItem.Item.Initialize(&itemVS);
voidItem.Slot = _player->AddVoidStorageItem(std::move(itemVS));
2012-07-30 16:39:46 +01:00
voidStorageTransferChanges.AddedItems.push_back(voidItem);
2012-07-30 16:39:46 +01:00
_player->DestroyItem(item->GetBagSlot(), item->GetSlot(), true);
++depositCount;
2012-07-30 16:39:46 +01:00
}
int64 cost = depositCount * VOID_STORAGE_STORE_ITEM_COST;
2012-07-30 16:39:46 +01:00
_player->ModifyMoney(-cost);
2012-07-30 16:39:46 +01:00
for (uint32 i = 0; i < voidStorageTransfer.Withdrawals.size(); ++i)
2012-07-30 16:39:46 +01:00
{
uint8 slot = 0;
VoidStorageItem* itemVS = _player->GetVoidStorageItem(voidStorageTransfer.Withdrawals[i].GetCounter(), slot);
2012-07-30 16:39:46 +01:00
if (!itemVS)
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - %s %s tried to withdraw an invalid item (id: %s)", _player->GetGUID().ToString().c_str(), _player->GetName().c_str(), voidStorageTransfer.Withdrawals[i].ToString().c_str());
2012-07-30 16:39:46 +01:00
continue;
}
ItemPosCountVec dest;
InventoryResult msg = _player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, itemVS->ItemEntry, 1);
2012-07-30 16:39:46 +01:00
if (msg != EQUIP_ERR_OK)
{
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_INVENTORY_FULL);
TC_LOG_DEBUG("network", "WORLD: HandleVoidStorageTransfer - %s %s couldn't withdraw item id %s because inventory was full.", _player->GetGUID().ToString().c_str(), _player->GetName().c_str(), voidStorageTransfer.Withdrawals[i].ToString().c_str());
2012-07-30 16:39:46 +01:00
return;
}
Item* item = _player->StoreNewItem(dest, itemVS->ItemEntry, true, itemVS->RandomBonusListId, GuidSet(), itemVS->Context, itemVS->BonusListIDs);
2019-06-03 20:40:34 +02:00
item->SetCreator(itemVS->CreatorGuid);
item->SetBinding(true);
GetCollectionMgr()->AddItemAppearance(item);
2012-07-30 16:39:46 +01:00
voidStorageTransferChanges.RemovedItems.push_back(ObjectGuid::Create<HighGuid::Item>(itemVS->ItemId));
2012-07-30 16:39:46 +01:00
_player->DeleteVoidStorageItem(slot);
2012-07-30 16:39:46 +01:00
}
SendPacket(voidStorageTransferChanges.Write());
2012-07-30 16:39:46 +01:00
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_NO_ERROR);
}
void WorldSession::HandleVoidSwapItem(WorldPackets::VoidStorage::SwapVoidItem& swapVoidItem)
2012-07-30 16:39:46 +01:00
{
2019-06-03 20:40:34 +02:00
Creature* unit = _player->GetNPCIfCanInteractWith(swapVoidItem.Npc, UNIT_NPC_FLAG_VAULTKEEPER, UNIT_NPC_FLAG_2_NONE);
2012-07-30 16:39:46 +01:00
if (!unit)
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - %s not found or player can't interact with it.", swapVoidItem.Npc.ToString().c_str());
2012-07-30 16:39:46 +01:00
return;
}
if (!_player->IsVoidStorageUnlocked())
2012-07-30 16:39:46 +01:00
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - Player (%s, name: %s) queried void storage without unlocking it.", _player->GetGUID().ToString().c_str(), _player->GetName().c_str());
2012-07-30 16:39:46 +01:00
return;
}
uint8 oldSlot;
if (!_player->GetVoidStorageItem(swapVoidItem.VoidItemGuid.GetCounter(), oldSlot))
2012-07-30 16:39:46 +01:00
{
TC_LOG_DEBUG("network", "WORLD: HandleVoidSwapItem - %s %s requested swapping an invalid item (slot: %u, itemid: %s).", _player->GetGUID().ToString().c_str(), _player->GetName().c_str(), swapVoidItem.DstSlot, swapVoidItem.VoidItemGuid.ToString().c_str());
2012-07-30 16:39:46 +01:00
return;
}
bool usedDestSlot = _player->GetVoidStorageItem(swapVoidItem.DstSlot) != NULL;
2012-07-30 16:39:46 +01:00
ObjectGuid itemIdDest;
if (usedDestSlot)
itemIdDest = ObjectGuid::Create<HighGuid::Item>(_player->GetVoidStorageItem(swapVoidItem.DstSlot)->ItemId);
2012-07-30 16:39:46 +01:00
if (!_player->SwapVoidStorageItem(oldSlot, swapVoidItem.DstSlot))
2012-07-30 16:39:46 +01:00
{
SendVoidStorageTransferResult(VOID_TRANSFER_ERROR_INTERNAL_ERROR_1);
return;
}
WorldPackets::VoidStorage::VoidItemSwapResponse voidItemSwapResponse;
voidItemSwapResponse.VoidItemA = swapVoidItem.VoidItemGuid;
voidItemSwapResponse.VoidItemSlotA = swapVoidItem.DstSlot;
2012-07-30 16:39:46 +01:00
if (usedDestSlot)
{
voidItemSwapResponse.VoidItemB = itemIdDest;
voidItemSwapResponse.VoidItemSlotB = oldSlot;
2012-07-30 16:39:46 +01:00
}
SendPacket(voidItemSwapResponse.Write());
2012-07-30 16:39:46 +01:00
}