summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gui/itemcontainer.cpp4
-rw-r--r--src/inventory.cpp40
2 files changed, 20 insertions, 24 deletions
diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp
index 0cdb8864..67cb5251 100644
--- a/src/gui/itemcontainer.cpp
+++ b/src/gui/itemcontainer.cpp
@@ -49,7 +49,7 @@ ItemContainer::ItemContainer(Inventory *inventory):
if (!selImg) logger->error("Unable to load selection.png");
selectedItem = 0; // No item selected
- maxItems = mInventory->getLastUsedSlot();
+ maxItems = mInventory->getLastUsedSlot() - 1; // Count from 0, usage from 2
addMouseListener(this);
}
@@ -64,7 +64,7 @@ void ItemContainer::logic()
{
gcn::Widget::logic();
- int i = mInventory->getLastUsedSlot();
+ int i = mInventory->getLastUsedSlot() - 1; // Count from 0, usage from 2
if (i != maxItems) {
maxItems = i;
diff --git a/src/inventory.cpp b/src/inventory.cpp
index 65f21292..0467df10 100644
--- a/src/inventory.cpp
+++ b/src/inventory.cpp
@@ -23,8 +23,17 @@
#include "inventory.h"
+#include <algorithm>
+
#include "item.h"
+struct SlotUsed : public std::unary_function<Item, bool>
+{
+ bool operator()(const Item &item) const {
+ return (item.getId() != -1 && item.getQuantity() > 0);
+ }
+};
+
Inventory::Inventory()
{
mItems = new Item[INVENTORY_SIZE];
@@ -76,6 +85,7 @@ void Inventory::removeItem(int id)
if (mItems[i].getId() == id) {
mItems[i].setId(-1);
mItems[i].setQuantity(0);
+ mItems[i].setEquipped(false);
}
}
}
@@ -93,37 +103,23 @@ bool Inventory::contains(Item *item)
int Inventory::getFreeSlot()
{
- for (int i = 2; i < INVENTORY_SIZE; i++) {
- if (mItems[i].getId() == -1) {
- return i;
- }
- }
- return -1;
+ Item *i = std::find_if(mItems + 2, mItems + INVENTORY_SIZE,
+ std::not1(SlotUsed()));
+ return (i == mItems + INVENTORY_SIZE) ? -1 : (i - mItems);
}
int Inventory::getNumberOfSlotsUsed()
{
- int numberOfFilledSlot = 0;
- for (int i = 0; i < INVENTORY_SIZE; i++)
- {
- if (mItems[i].getId() > -1 || mItems[i].getQuantity() > 0)
- {
- numberOfFilledSlot++;
- }
- }
-
- return numberOfFilledSlot;
+ return count_if(mItems, mItems + INVENTORY_SIZE, SlotUsed());
}
int Inventory::getLastUsedSlot()
{
- int i;
-
- for (i = INVENTORY_SIZE - 1; i >= 0; i--) {
- if ((mItems[i].getId() != -1) && (mItems[i].getQuantity() > 0)) {
- break;
+ for (int i = INVENTORY_SIZE - 1; i >= 0; i--) {
+ if (SlotUsed()(mItems[i])) {
+ return i;
}
}
- return --i;
+ return -1;
}