diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-04-30 12:49:47 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-04-30 12:49:47 +1000 |
commit | d3d4345e057d95d784e34be9b23e7fe07fb9a7c1 (patch) | |
tree | f59fa890e5be1d590e42a337448cecfc6b61850a /source/game/StarItemBag.cpp | |
parent | 86e229012f84744a1e878124d9a6e2991c0460bb (diff) | |
parent | 885502bf11057e7de961f178bc85ce93a9f40723 (diff) |
Merge branch 'main' into pr/218
Diffstat (limited to 'source/game/StarItemBag.cpp')
-rw-r--r-- | source/game/StarItemBag.cpp | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/source/game/StarItemBag.cpp b/source/game/StarItemBag.cpp index 8abdd6f..1f8f74f 100644 --- a/source/game/StarItemBag.cpp +++ b/source/game/StarItemBag.cpp @@ -234,17 +234,22 @@ auto ItemBag::itemsFitWhere(ItemConstPtr const& items, uint64_t max) const -> It return ItemsFitWhereResult(); List<size_t> slots; + StableHashSet<size_t> taken; uint64_t count = std::min(items->count(), max); while (true) { if (count == 0) break; - size_t slot = bestSlotAvailable(items, false); + size_t slot = bestSlotAvailable(items, false, [&](size_t i) { + return !taken.contains(i); + }); if (slot == NPos) break; - else + else { slots.append(slot); + taken.insert(slot); + } uint64_t available = stackTransfer(at(slot), items); if (available != 0) @@ -350,9 +355,11 @@ uint64_t ItemBag::stackTransfer(ItemConstPtr const& to, ItemConstPtr const& from return std::min(to->maxStack() - to->count(), from->count()); } -size_t ItemBag::bestSlotAvailable(ItemConstPtr const& item, bool stacksOnly) const { +size_t ItemBag::bestSlotAvailable(ItemConstPtr const& item, bool stacksOnly, std::function<bool(size_t)> test) const { // First look for any slots that can stack, before empty slots. for (size_t i = 0; i < m_items.size(); ++i) { + if (!test(i)) + continue; auto const& storedItem = at(i); if (storedItem && stackTransfer(storedItem, item) != 0) return i; @@ -369,4 +376,8 @@ size_t ItemBag::bestSlotAvailable(ItemConstPtr const& item, bool stacksOnly) con return NPos; } +size_t ItemBag::bestSlotAvailable(ItemConstPtr const& item, bool stacksOnly) const { + return bestSlotAvailable(item, stacksOnly, [](size_t) { return true; }); +} + } |