Actually done

This commit is contained in:
dinomking33 2024-06-01 20:14:38 -05:00
parent 1c8762fb00
commit 420822aab4
2 changed files with 60 additions and 101 deletions

View File

@ -161,7 +161,7 @@ const EquipmentMap& InventoryComponent::GetEquippedItems() const {
return m_Equipped; return m_Equipped;
} }
std::vector<LWOOBJID> InventoryComponent::AddItem( void InventoryComponent::AddItem(
const LOT lot, const LOT lot,
const uint32_t count, const uint32_t count,
eLootSourceType lootSourceType, eLootSourceType lootSourceType,
@ -176,90 +176,76 @@ std::vector<LWOOBJID> InventoryComponent::AddItem(
const bool bound, const bool bound,
int32_t preferredSlot) { int32_t preferredSlot) {
std::vector<LWOOBJID> toReturn;
LOG("AddItem called with parameters: LOT %i, Count %u, LootSourceType %i, InventoryType %i, Parent %llu, ShowFlyingLoot %i, IsModMoveAndEquip %i, SubKey %llu, InventorySourceType %i, SourceType %i, Bound %i, PreferredSlot %i",
lot, count, lootSourceType, inventoryType, parent, showFlyingLoot, isModMoveAndEquip, subKey, inventorySourceType, sourceType, bound, preferredSlot);
if (count == 0) { if (count == 0) {
LOG("Attempted to add 0 of item (%i) to the inventory!", lot); return;
return toReturn;
} }
if (!Inventory::IsValidItem(lot)) { if (!Inventory::IsValidItem(lot)) {
if (lot > 0) { if (lot > 0) {
LOG("Attempted to add invalid item (%i) to the inventory!", lot);
} }
return toReturn; return;
} }
if (inventoryType == INVALID) { if (inventoryType == INVALID) {
inventoryType = Inventory::FindInventoryTypeForLot(lot); inventoryType = Inventory::FindInventoryTypeForLot(lot);
LOG("InventoryType was INVALID, found InventoryType %i for LOT %i", inventoryType, lot);
} }
auto* missions = static_cast<MissionComponent*>(this->m_Parent->GetComponent(eReplicaComponentType::MISSION)); auto* missions = static_cast<MissionComponent*>(this->m_Parent->GetComponent(eReplicaComponentType::MISSION));
auto* inventory = GetInventory(inventoryType); auto* inventory = GetInventory(inventoryType);
LOG("Retrieved inventory of type %i", inventoryType);
if (!config.empty() || bound) { if (!config.empty() || bound) {
LOG("Adding item with config or bound. Config size: %lu, Bound: %i", config.size(), bound);
const auto slot = preferredSlot != -1 && inventory->IsSlotEmpty(preferredSlot) ? preferredSlot : inventory->FindEmptySlot(); const auto slot = preferredSlot != -1 && inventory->IsSlotEmpty(preferredSlot) ? preferredSlot : inventory->FindEmptySlot();
if (slot == -1) { if (slot == -1) {
LOG("Failed to find empty slot for inventory (%i)!", inventoryType); return;
return toReturn;
} }
auto* item = new Item(lot, inventory, slot, count, config, parent, showFlyingLoot, isModMoveAndEquip, subKey, bound, lootSourceType); auto* item = new Item(lot, inventory, slot, count, config, parent, showFlyingLoot, isModMoveAndEquip, subKey, bound, lootSourceType);
LOG("Created new item with ID %llu, LOT %i in slot %i", item->GetId(), lot, slot);
// Check if inventory type is VENDOR_BUYBACK and manage the inventory size // Check if inventory type is VENDOR_BUYBACK and manage the inventory size
if (inventoryType == VENDOR_BUYBACK) { if (inventoryType == eInventoryType::VENDOR_BUYBACK) {
LOG("InventoryType is VENDOR_BUYBACK, managing vendor buyback inventory"); ManageVendorBuybackInventory(item->GetId(), inventory);
ManageVendorBuybackInventory(invTransferred, item, inventory, false);
} }
toReturn.push_back(item->GetId());
LOG("Added item with ID %llu to return vector", item->GetId());
if (missions != nullptr && !IsTransferInventory(inventoryType)) { if (missions != nullptr && !IsTransferInventory(inventoryType)) {
LOG("Progressing mission for adding item with LOT %i, count %u", lot, count);
missions->Progress(eMissionTaskType::GATHER, lot, LWOOBJID_EMPTY, "", count, IsTransferInventory(inventorySourceType)); missions->Progress(eMissionTaskType::GATHER, lot, LWOOBJID_EMPTY, "", count, IsTransferInventory(inventorySourceType));
} }
return toReturn; return;
} }
const auto info = Inventory::FindItemComponent(lot); const auto info = Inventory::FindItemComponent(lot);
LOG("Retrieved item component info for LOT %i", lot);
auto left = count; auto left = count;
int32_t outOfSpace = 0; int32_t outOfSpace = 0;
auto stack = static_cast<uint32_t>(info.stackSize); auto stack = static_cast<uint32_t>(info.stackSize);
bool isBrick = inventoryType == eInventoryType::BRICKS || (stack == 0 && info.itemType == 1); bool isBrick = inventoryType == eInventoryType::BRICKS || (stack == 0 && info.itemType == 1);
// info.itemType of 1 is item type brick
if (isBrick) { if (isBrick) {
stack = UINT32_MAX; stack = UINT32_MAX;
} else if (stack == 0) { } else if (stack == 0) {
stack = 1; stack = 1;
} }
LOG("Stack size determined: %u, IsBrick: %i", stack, isBrick);
auto* existing = FindItemByLot(lot, inventoryType); auto* existing = FindItemByLot(lot, inventoryType);
if (existing != nullptr) { if (existing != nullptr) {
const auto delta = std::min<uint32_t>(left, stack - existing->GetCount()); const auto delta = std::min<uint32_t>(left, stack - existing->GetCount());
left -= delta; left -= delta;
existing->SetCount(existing->GetCount() + delta, false, true, showFlyingLoot, lootSourceType); existing->SetCount(existing->GetCount() + delta, false, true, showFlyingLoot, lootSourceType);
LOG("Updated existing item with LOT %i, new count %u", lot, existing->GetCount());
if (isModMoveAndEquip) { if (isModMoveAndEquip) {
existing->Equip(); existing->Equip();
LOG("Equipped item with LOT %i", lot);
isModMoveAndEquip = false; isModMoveAndEquip = false;
} }
} }
// If we have some leftover and we aren't bricks, make a new stack
while (left > 0 && (!isBrick || (isBrick && !existing))) { while (left > 0 && (!isBrick || (isBrick && !existing))) {
const auto size = std::min(left, stack); const auto size = std::min(left, stack);
left -= size; left -= size;
@ -294,13 +280,10 @@ std::vector<LWOOBJID> InventoryComponent::AddItem(
} }
auto* item = new Item(lot, inventory, slot, size, {}, parent, showFlyingLoot, isModMoveAndEquip, subKey, false, lootSourceType); auto* item = new Item(lot, inventory, slot, size, {}, parent, showFlyingLoot, isModMoveAndEquip, subKey, false, lootSourceType);
toReturn.push_back(item->GetId());
LOG("Created new item with LOT %i in slot %i, count %u", lot, slot, size);
// Check if inventory type is VENDOR_BUYBACK and manage the inventory size // Check if inventory type is VENDOR_BUYBACK and manage the inventory size
if (inventoryType == VENDOR_BUYBACK) { if (inventoryType == eInventoryType::VENDOR_BUYBACK) {
LOG("InventoryType is VENDOR_BUYBACK, managing vendor buyback inventory"); ManageVendorBuybackInventory(item->GetId(), inventory);
ManageVendorBuybackInventory(invTransferred, item, inventory, true);
} }
isModMoveAndEquip = false; isModMoveAndEquip = false;
@ -311,17 +294,7 @@ std::vector<LWOOBJID> InventoryComponent::AddItem(
missions->Progress(eMissionTaskType::GATHER, lot, LWOOBJID_EMPTY, "", count - outOfSpace, IsTransferInventory(inventorySourceType)); missions->Progress(eMissionTaskType::GATHER, lot, LWOOBJID_EMPTY, "", count - outOfSpace, IsTransferInventory(inventorySourceType));
} }
LOG("AddItem completed for LOT %i, count %u", lot, count); return;
if (!toReturn.empty()) {
for (const auto& id : toReturn) {
LOG("LWOOBJID in toReturn: %llu", id);
}
for (const auto& id : invTransferred) {
LOG("LWOOBJID in invTransferred: %llu", id);
}
}
return toReturn;
} }
bool InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInventoryType inventoryType, const bool ignoreBound, const bool silent) { bool InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInventoryType inventoryType, const bool ignoreBound, const bool silent) {
@ -347,7 +320,6 @@ bool InventoryComponent::RemoveItem(const LOT lot, const uint32_t count, eInvent
} }
void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType inventory, const uint32_t count, const bool showFlyingLot, bool isModMoveAndEquip, const bool ignoreEquipped, const int32_t preferredSlot) { void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType inventory, const uint32_t count, const bool showFlyingLot, bool isModMoveAndEquip, const bool ignoreEquipped, const int32_t preferredSlot) {
std::vector<LWOOBJID> objIds;
if (item == nullptr) { if (item == nullptr) {
return; return;
} }
@ -374,7 +346,7 @@ void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType in
left -= delta; left -= delta;
objIds = AddItem(lot, delta, eLootSourceType::NONE, inventory, {}, LWOOBJID_EMPTY, showFlyingLot, isModMoveAndEquip, LWOOBJID_EMPTY, origin->GetType(), 0, false, preferredSlot); AddItem(lot, delta, eLootSourceType::NONE, inventory, {}, LWOOBJID_EMPTY, showFlyingLot, isModMoveAndEquip, LWOOBJID_EMPTY, origin->GetType(), 0, false, preferredSlot);
item->SetCount(item->GetCount() - delta, false, false); item->SetCount(item->GetCount() - delta, false, false);
@ -389,7 +361,7 @@ void InventoryComponent::MoveItemToInventory(Item* item, const eInventoryType in
const auto delta = std::min<uint32_t>(item->GetCount(), count); const auto delta = std::min<uint32_t>(item->GetCount(), count);
objIds = AddItem(lot, delta, eLootSourceType::NONE, inventory, config, LWOOBJID_EMPTY, showFlyingLot, isModMoveAndEquip, subkey, origin->GetType(), 0, item->GetBound(), preferredSlot); AddItem(lot, delta, eLootSourceType::NONE, inventory, config, LWOOBJID_EMPTY, showFlyingLot, isModMoveAndEquip, subkey, origin->GetType(), 0, item->GetBound(), preferredSlot);
item->SetCount(item->GetCount() - delta, false, false); item->SetCount(item->GetCount() - delta, false, false);
} }
@ -1634,61 +1606,31 @@ bool InventoryComponent::SetSkill(BehaviorSlot slot, uint32_t skillId) {
return true; return true;
} }
void InventoryComponent::ManageVendorBuybackInventory(std::vector<LWOOBJID>& itemVector, Item* newItem, Inventory* inventory, bool removeItem) { void InventoryComponent::ManageVendorBuybackInventory(LWOOBJID newItem, Inventory* inventory) {
const size_t maxVendorBuybackItems = 26; const size_t maxVendorBuybackItems = 27;
static std::vector<int> freeSlots; // Keep track of free slots
LOG("ManageVendorBuybackInventory called. ItemVector size: %lu, NewItem ID: %llu, RemoveItem: %i", itemVector.size(), newItem->GetId(), removeItem); if (buybackItems.size() == 26) {
LWOOBJID oldestItemId = buybackItems.front();
buybackItems.pop();
if (itemVector.size() >= maxVendorBuybackItems) { RemoveItem(oldestItemId, inventory->GetType());
if (!freeSlots.empty()) {
int slotToUse = freeSlots.back();
freeSlots.pop_back();
itemVector[slotToUse] = newItem->GetId();
LOG("Using free slot %i for new item with ID %llu", slotToUse, newItem->GetId());
} else {
auto oldestItemId = itemVector.back(); // Get the ID of the oldest item
itemVector.pop_back(); // Remove the oldest item from the vector
LOG("ItemVector size exceeded maxVendorBuybackItems. Removing oldest item with ID: %llu", oldestItemId);
// Remove the oldest item from the inventory
RemoveItemById(oldestItemId, inventory->GetType(), true, false);
LOG("Removed oldest item with LWOOBJID %llu from VENDOR_BUYBACK inventory to maintain max size", oldestItemId);
itemVector.insert(itemVector.begin(), newItem->GetId());
LOG("Added newest item with LWOOBJID %llu to VENDOR_BUYBACK inventory", newItem->GetId());
}
} else {
itemVector.insert(itemVector.begin(), newItem->GetId());
LOG("Added newest item with LWOOBJID %llu to VENDOR_BUYBACK inventory", newItem->GetId());
} }
if (buybackItems.size() >= maxVendorBuybackItems) {
LWOOBJID oldestItemId = buybackItems.front();
buybackItems.pop();
RemoveItem(oldestItemId, inventory->GetType());
} }
bool InventoryComponent::RemoveItemById(LWOOBJID itemId, eInventoryType inventoryType, const bool ignoreBound, const bool silent) { buybackItems.push(newItem);
LOG("RemoveItemById called with parameters: ItemId %llu, InventoryType %i, IgnoreBound %i, Silent %i", itemId, inventoryType, ignoreBound, silent);
auto* inventory = GetInventory(inventoryType);
if (!inventory) {
LOG("No inventory found for InventoryType %i", inventoryType);
return false;
} }
// Find the item in the inventory by its ID void InventoryComponent::RemoveItem(LWOOBJID itemId, eInventoryType inventoryType) {
auto* item = FindItemById(itemId); auto* item = FindItemById(itemId);
if (!item) { if (item) {
LOG("No item found with ItemId %llu", itemId); item->SetCount(0);
return false;
}
// Track the slot being freed
int slot = item->GetSlot();
static std::vector<int> freeSlots;
freeSlots.push_back(slot);
LOG("Slot %i added to freeSlots", slot);
// Set count to 0 to remove the item
item->SetCount(0, silent);
LOG("Item with ItemId %llu removed (count set to 0)", itemId); LOG("Item with ItemId %llu removed (count set to 0)", itemId);
} else {
return true; LOG("Item with ItemId %llu not found, cannot remove", itemId);
}
} }

View File

@ -5,7 +5,7 @@
#include <map> #include <map>
#include <stack> #include <stack>
#include <queue>
#include "BehaviorSlot.h" #include "BehaviorSlot.h"
#include "tinyxml2.h" #include "tinyxml2.h"
@ -95,7 +95,7 @@ public:
* @param preferredSlot the preferred slot to store this item * @param preferredSlot the preferred slot to store this item
* @param lootSourceType The source of the loot. Defaults to none. * @param lootSourceType The source of the loot. Defaults to none.
*/ */
std::vector<LWOOBJID> AddItem( void AddItem(
LOT lot, LOT lot,
uint32_t count, uint32_t count,
eLootSourceType lootSourceType = eLootSourceType::NONE, eLootSourceType lootSourceType = eLootSourceType::NONE,
@ -372,6 +372,22 @@ public:
bool SetSkill(int slot, uint32_t skillId); bool SetSkill(int slot, uint32_t skillId);
bool SetSkill(BehaviorSlot slot, uint32_t skillId); bool SetSkill(BehaviorSlot slot, uint32_t skillId);
/**
* Called during AddItem to manage the vendor buyback inventory if the inventory is of that type
*
* @param newItem The item ID which is being moved to the vendor buyback inventory
* @param inventory The entity/vendor's inventory
*/
void ManageVendorBuybackInventory(LWOOBJID newItem, Inventory* inventory);
/**
* Called in ManageVendorBuybackInventory to remove an item given the ItemId
*
* @param itemId item ID for item being removed
* @param inventoryType inventory type
*/
void RemoveItem(LWOOBJID itemId, eInventoryType inventoryType);
~InventoryComponent() override; ~InventoryComponent() override;
private: private:
@ -379,7 +395,10 @@ private:
* All the inventory this entity possesses * All the inventory this entity possesses
*/ */
std::map<eInventoryType, Inventory*> m_Inventories; std::map<eInventoryType, Inventory*> m_Inventories;
std::vector<LWOOBJID> invTransferred; /**
*A queue of LWOOBJIDs representing the buybackItems which are sold to a vendor
*/
std::queue<LWOOBJID> buybackItems;
/** /**
* The skills that this entity currently has active * The skills that this entity currently has active
*/ */
@ -477,8 +496,6 @@ private:
* @param document the xml doc to load from * @param document the xml doc to load from
*/ */
void UpdatePetXml(tinyxml2::XMLDocument& document); void UpdatePetXml(tinyxml2::XMLDocument& document);
void ManageVendorBuybackInventory(std::vector<LWOOBJID>& itemVector, Item* newItem, Inventory* inventory, bool removeItem);
bool RemoveItemById(LWOOBJID itemId, eInventoryType inventoryType, const bool ignoreBound, const bool silent);
}; };
#endif #endif