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 | |
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.
-rw-r--r-- | ChangeLog | 2 | ||||
-rw-r--r-- | src/gui/itemcontainer.cpp | 4 | ||||
-rw-r--r-- | src/inventory.cpp | 40 |
3 files changed, 22 insertions, 24 deletions
@@ -1,5 +1,7 @@ 2006-03-08 Björn Steinbrink <B.Steinbrink@gmx.de> + * src/gui/itemcontainer.cpp, src/inventory.cpp: Made Inventory use STL + algorithms and fixed getLastUsedSlot semantics. * src/engine.cpp, src/engine.h: Moved a variable definition into the right place and made getCurrentMap inline. * src/equipment.cpp, src/equipment.h: Use STL algorithms and make 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; } |