fix(vendor): keep 27 buyback items and drop the oldest

The buyback inventory grew by 9 slots whenever it was nearly full, so
the vendor's buyback page kept resizing. Live kept 27 items: a 2014
capture of 29 sales shows that on the 28th, the server sent
RemoveItemFromInventory (buyback inventory) for the first item sold,
then added the new one.

The buyback inventory now keeps its size. Before a sale needs a new
buyback slot and the inventory holds 27 or more items, the oldest items
(lowest object ID: each sale gives a new, higher ID) are removed. Sales
that fit on an existing buyback stack remove nothing. The removal is not
counted again by the economy ledger, which counted the items as gone
when they were sold.

Fixes issue 1129.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-27 08:57:30 -05:00
parent 02108055e7
commit c759bd1a40
5 changed files with 125 additions and 3 deletions

View File

@@ -15,6 +15,10 @@
#include "CheatDetection.h"
#include "VendorMessages.h"
#include "Item.h"
#include "EconomyLedger.h"
#include <algorithm>
#include <ranges>
VendorComponent::VendorComponent(Entity* parent, const int32_t componentID) : Component(parent, componentID) {
m_HasStandardCostItems = false;
@@ -272,6 +276,7 @@ void VendorComponent::SellToVendor(Entity& player, const SystemAddress& sysAddr,
inv->AddItem(itemComp.currencyLOT, std::floor(altCurrency), eLootSourceType::VENDOR); // Return alt currencies like faction tokens.
}
MakeRoomInBuyback(*inv, *item, static_cast<uint32_t>(std::max(count, 0)));
inv->MoveItemToInventory(item, eInventoryType::VENDOR_BUYBACK, count, true, false, true);
character->SetCoins(std::floor(character->GetCoins() + (static_cast<uint32_t>(itemComp.baseValue * sellScalar) * count)), eLootSourceType::VENDOR);
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::SELL_SUCCESS);
@@ -315,3 +320,32 @@ void VendorComponent::BuybackFromVendor(Entity& player, const SystemAddress& sys
//Game::entityManager->SerializeEntity(player); // so inventory updates
SendTransactionResult(m_Parent->GetObjectID(), sysAddr, eVendorTransactionResult::PURCHASE_SUCCESS);
}
void VendorComponent::MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, const uint32_t count) {
auto* const buyback = inventory.GetInventory(eInventoryType::VENDOR_BUYBACK);
if (!buyback) return;
// No new slot is needed when all of it fits on buyback stacks of the same item
const auto lot = sold.GetLot();
const auto stackSize = sold.GetInfo().stackSize;
const bool mergeable = sold.GetSubKey() == LWOOBJID_EMPTY && sold.GetConfig().values.empty() && !InventoryComponent::IsUniqueLot(lot);
if (mergeable) {
uint64_t room = 0;
for (const auto* const item : buyback->GetItems() | std::views::values) {
if (item->GetLot() == lot && item->GetCount() < stackSize) room += stackSize - item->GetCount();
}
if (room >= count) return;
}
// Live kept 27 items (the buyback window's size) and dropped the oldest when a 28th was sold: a 2014 capture shows
// RemoveItemFromInventory for the first item sold, in the buyback inventory, just before the 28th sale's item was
// added. Each item gets a new, higher object ID when it is sold, so the oldest is the one with the lowest ID.
// The ledger counted these items as gone when they were sold
EconomyLedger::ScopedItemTransfer alreadyCounted;
while (buyback->GetItems().size() >= BUYBACK_SIZE) {
const auto items = buyback->GetItems().size();
const auto oldest = std::ranges::min_element(buyback->GetItems(), {}, [](const auto& entry) { return entry.first; });
oldest->second->SetCount(0, false, false, false, eLootSourceType::VENDOR);
if (buyback->GetItems().size() >= items) break; // not removed (an empty stack)
}
}

View File

@@ -10,6 +10,8 @@
#include "eReplicaComponentType.h"
enum class eVendorTransactionResult : uint32_t;
class InventoryComponent;
class Item;
struct SoldItem {
SoldItem(const LOT lot, const int32_t sortPriority) {
@@ -61,6 +63,13 @@ public:
// Sends a VendorTransactionResult targeting target to sysAddr only.
static void SendTransactionResult(LWOOBJID target, const SystemAddress& sysAddr, eVendorTransactionResult result);
// How many items the buyback inventory keeps (the vendor window's buyback page)
static constexpr size_t BUYBACK_SIZE = 27;
// Before count of sold go to the buyback inventory: drops the oldest buyback items while it is full and they need
// a slot of their own.
static void MakeRoomInBuyback(InventoryComponent& inventory, const Item& sold, uint32_t count);
private:
void HandleMrReeCameras();
bool SetupItem(LOT item);