diff options
author | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-03-08 03:21:05 +0000 |
---|---|---|
committer | Björn Steinbrink <B.Steinbrink@gmx.de> | 2006-03-08 03:21:05 +0000 |
commit | 8f375a38ad972a1fcac7953cc0d254313c88b7c9 (patch) | |
tree | 7283b64fba5b6898f2c25131cd996f279e7bb302 /src/inventory.cpp | |
parent | b8a237ac0d5063ce43b462af22da92e8c817f4d3 (diff) | |
download | mana-8f375a38ad972a1fcac7953cc0d254313c88b7c9.tar.gz mana-8f375a38ad972a1fcac7953cc0d254313c88b7c9.tar.bz2 mana-8f375a38ad972a1fcac7953cc0d254313c88b7c9.tar.xz mana-8f375a38ad972a1fcac7953cc0d254313c88b7c9.zip |
Made Inventory use STL algorithms and fixed getLastUsedSlot semantics.
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r-- | src/inventory.cpp | 40 |
1 files changed, 18 insertions, 22 deletions
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; } |