diff --git a/src/frontend/hud.cpp b/src/frontend/hud.cpp index bcf623ce..b60d8535 100644 --- a/src/frontend/hud.cpp +++ b/src/frontend/hud.cpp @@ -108,21 +108,21 @@ std::shared_ptr HudElement::getNode() const { std::shared_ptr Hud::createContentAccess() { auto& content = frontend.getLevel().content; - auto indices = content.getIndices(); + auto& indices = *content.getIndices(); auto inventory = player.getInventory(); - size_t itemsCount = indices->items.count(); + size_t itemsCount = indices.items.count(); auto accessInventory = std::make_shared(0, itemsCount); for (size_t id = 1; id < itemsCount; id++) { accessInventory->getSlot(id-1).set(ItemStack(id, 1)); } SlotLayout slotLayout(-1, glm::vec2(), false, true, nullptr, - [=](uint, ItemStack& item) { + [inventory, &indices](uint, ItemStack& item) { auto copy = ItemStack(item); inventory->move(copy, indices); }, - [=](uint, ItemStack& item) { + [this, inventory](uint, ItemStack& item) { inventory->getSlot(player.getChosenSlot()).set(item); }); @@ -494,12 +494,12 @@ void Hud::dropExchangeSlot() { auto indices = frontend.getLevel().content.getIndices(); if (auto invView = std::dynamic_pointer_cast(blockUI)) { - invView->getInventory()->move(stack, indices); + invView->getInventory()->move(stack, *indices); } if (stack.isEmpty()) { return; } - player.getInventory()->move(stack, indices); + player.getInventory()->move(stack, *indices); if (!stack.isEmpty()) { logger.warning() << "discard item [" << stack.getItemId() << ":" << stack.getCount(); diff --git a/src/graphics/ui/elements/InventoryView.cpp b/src/graphics/ui/elements/InventoryView.cpp index 22e1b82a..78e56e78 100644 --- a/src/graphics/ui/elements/InventoryView.cpp +++ b/src/graphics/ui/elements/InventoryView.cpp @@ -219,7 +219,7 @@ void SlotView::performLeftClick(ItemStack& stack, ItemStack& grabbed) { return; } if (!layout.itemSource && stack.accepts(grabbed) && layout.placing) { - stack.move(grabbed, content->getIndices()); + stack.move(grabbed, *content->getIndices()); } else { if (layout.itemSource) { if (grabbed.isEmpty()) { diff --git a/src/items/Inventory.cpp b/src/items/Inventory.cpp index 9797a15e..1d27e261 100644 --- a/src/items/Inventory.cpp +++ b/src/items/Inventory.cpp @@ -37,7 +37,7 @@ size_t Inventory::findSlotByItem( } void Inventory::move( - ItemStack& item, const ContentIndices* indices, size_t begin, size_t end + ItemStack& item, const ContentIndices& indices, size_t begin, size_t end ) { end = std::min(slots.size(), end); for (size_t i = begin; i < end && !item.isEmpty(); i++) { diff --git a/src/items/Inventory.hpp b/src/items/Inventory.hpp index 49560523..f0efdfe1 100644 --- a/src/items/Inventory.hpp +++ b/src/items/Inventory.hpp @@ -32,7 +32,7 @@ public: void move( ItemStack& item, - const ContentIndices* indices, + const ContentIndices& indices, size_t begin = 0, size_t end = -1 ); diff --git a/src/items/ItemStack.cpp b/src/items/ItemStack.cpp index 99cb0a0e..fdf1ff80 100644 --- a/src/items/ItemStack.cpp +++ b/src/items/ItemStack.cpp @@ -3,9 +3,6 @@ #include "content/Content.hpp" #include "ItemDef.hpp" -ItemStack::ItemStack() : item(ITEM_EMPTY), count(0) { -} - ItemStack::ItemStack(itemid_t item, itemcount_t count) : item(item), count(count) { } @@ -28,9 +25,9 @@ bool ItemStack::accepts(const ItemStack& other) const { return item == other.getItemId(); } -void ItemStack::move(ItemStack& item, const ContentIndices* indices) { - auto& def = indices->items.require(item.getItemId()); - int count = std::min(item.count, def.stackSize - this->count); +void ItemStack::move(ItemStack& item, const ContentIndices& indices) { + auto& def = indices.items.require(item.getItemId()); + itemcount_t count = std::min(item.count, def.stackSize - this->count); if (isEmpty()) { set(ItemStack(item.getItemId(), count)); } else { diff --git a/src/items/ItemStack.hpp b/src/items/ItemStack.hpp index 1a8e191d..e2892bf3 100644 --- a/src/items/ItemStack.hpp +++ b/src/items/ItemStack.hpp @@ -6,10 +6,10 @@ class ContentIndices; class ItemStack { - itemid_t item; - itemcount_t count; + itemid_t item = ITEM_EMPTY; + itemcount_t count = 0; public: - ItemStack(); + ItemStack() = default; ItemStack(itemid_t item, itemcount_t count); @@ -17,7 +17,12 @@ public: void setCount(itemcount_t count); bool accepts(const ItemStack& item) const; - void move(ItemStack& item, const ContentIndices* indices); + + /// @brief Move items from one stack to another. + /// If the target stack is completely filled, the source stack will be reduced. + /// @param item source stack + /// @param indices content indices + void move(ItemStack& item, const ContentIndices& indices); inline void clear() { set(ItemStack(0, 0)); diff --git a/src/logic/scripting/lua/libs/libinventory.cpp b/src/logic/scripting/lua/libs/libinventory.cpp index 65f0dd7c..09be85d6 100644 --- a/src/logic/scripting/lua/libs/libinventory.cpp +++ b/src/logic/scripting/lua/libs/libinventory.cpp @@ -80,7 +80,7 @@ static int l_add(lua::State* L) { auto& inv = get_inventory(invid); ItemStack item(itemid, count); - inv.move(item, indices); + inv.move(item, *indices); return lua::pushinteger(L, item.getCount()); } @@ -144,9 +144,9 @@ static int l_move(lua::State* L) { auto& invB = get_inventory(invBid, 3); auto& slot = invA.getSlot(slotAid); if (slotBid == -1) { - invB.move(slot, content->getIndices()); + invB.move(slot, *content->getIndices()); } else { - invB.move(slot, content->getIndices(), slotBid, slotBid + 1); + invB.move(slot, *content->getIndices(), slotBid, slotBid + 1); } return 0; } @@ -163,9 +163,9 @@ static int l_move_range(lua::State* L) { auto invB = get_inventory(invBid, 3); auto& slot = invA.getSlot(slotAid); if (slotBegin == -1) { - invB.move(slot, content->getIndices()); + invB.move(slot, *content->getIndices()); } else { - invB.move(slot, content->getIndices(), slotBegin, slotEnd); + invB.move(slot, *content->getIndices(), slotBegin, slotEnd); } return 0; }