diff options
author | David Athay <ko2fan@gmail.com> | 2009-02-11 09:30:26 +0000 |
---|---|---|
committer | David Athay <ko2fan@gmail.com> | 2009-02-11 09:30:26 +0000 |
commit | 91111ca5d13072ea3b834e23835df9c077329e39 (patch) | |
tree | f0af8dd08b766164835cf9b5412a9aa3267dbad7 /src/inventory.cpp | |
parent | 8046bb2626b30fecdcea54eb0aa3349cdb7d277b (diff) | |
parent | 63ac001daa7dfc0735dfefd9c2829c8786b4edaf (diff) | |
download | mana-91111ca5d13072ea3b834e23835df9c077329e39.tar.gz mana-91111ca5d13072ea3b834e23835df9c077329e39.tar.bz2 mana-91111ca5d13072ea3b834e23835df9c077329e39.tar.xz mana-91111ca5d13072ea3b834e23835df9c077329e39.zip |
Merge branch 'master' of git@gitorious.org:tmw/eathena
Diffstat (limited to 'src/inventory.cpp')
-rw-r--r-- | src/inventory.cpp | 70 |
1 files changed, 30 insertions, 40 deletions
diff --git a/src/inventory.cpp b/src/inventory.cpp index 7b9ec07c..3ca26e1e 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -19,30 +19,30 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -#include "inventory.h" - #include <algorithm> -#include <cassert> +#include "inventory.h" #include "item.h" #include "log.h" struct SlotUsed : public std::unary_function<Item*, bool> { - bool operator()(const Item *item) const { + bool operator()(const Item *item) const + { return item && item->getId() != -1 && item->getQuantity() > 0; } }; -Inventory::Inventory() +Inventory::Inventory(int size): + mSize(size) { - mItems = new Item*[INVENTORY_SIZE]; - std::fill_n(mItems, INVENTORY_SIZE, (Item*) 0); + mItems = new Item*[mSize]; + std::fill_n(mItems, mSize, (Item*) 0); } Inventory::~Inventory() { - for (int i = 0; i < INVENTORY_SIZE; i++) + for (int i = 0; i < mSize; i++) delete mItems[i]; delete [] mItems; @@ -58,11 +58,10 @@ Item* Inventory::getItem(int index) const Item* Inventory::findItem(int itemId) const { - for (int i = 0; i < INVENTORY_SIZE; i++) - { + for (int i = 0; i < mSize; i++) if (mItems[i] && mItems[i]->getId() == itemId) return mItems[i]; - } + return NULL; } @@ -73,46 +72,41 @@ void Inventory::addItem(int id, int quantity, bool equipment) void Inventory::setItem(int index, int id, int quantity, bool equipment) { - if (index < 0 || index >= INVENTORY_SIZE) { + if (index < 0 || index >= mSize) + { logger->log("Warning: invalid inventory index: %d", index); return; } - /* TODO: Check where to reenable this code. - // Dont stack equipment other than arrows. - if (equipment && !(id == 1199 || id == 529)) - mItems[index].setQuantity(quantity); - else - mItems[index].increaseQuantity(quantity); - */ - - if (!mItems[index] && id > 0) { + if (!mItems[index] && id > 0) + { Item *item = new Item(id, quantity, equipment); item->setInvIndex(index); mItems[index] = item; - } else if (id > 0) { + } + else if (id > 0) + { mItems[index]->setId(id); mItems[index]->setQuantity(quantity); mItems[index]->setEquipment(equipment); - } else if (mItems[index]) { + } + else if (mItems[index]) + { removeItemAt(index); } } void Inventory::clear() { - for (int i = 0; i < INVENTORY_SIZE; i++) { + for (int i = 0; i < mSize; i++) removeItemAt(i); - } } void Inventory::removeItem(int id) { - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (mItems[i] && mItems[i]->getId() == id) { + for (int i = 0; i < mSize; i++) + if (mItems[i] && mItems[i]->getId() == id) removeItemAt(i); - } - } } void Inventory::removeItemAt(int index) @@ -123,34 +117,30 @@ void Inventory::removeItemAt(int index) bool Inventory::contains(Item *item) const { - for (int i = 0; i < INVENTORY_SIZE; i++) { - if (mItems[i] && mItems[i]->getId() == item->getId()) { + for (int i = 0; i < mSize; i++) + if (mItems[i] && mItems[i]->getId() == item->getId()) return true; - } - } return false; } int Inventory::getFreeSlot() const { - Item **i = std::find_if(mItems + 2, mItems + INVENTORY_SIZE, + Item **i = std::find_if(mItems + 2, mItems + mSize, std::not1(SlotUsed())); - return (i == mItems + INVENTORY_SIZE) ? -1 : (i - mItems); + return (i == mItems + mSize) ? -1 : (i - mItems); } int Inventory::getNumberOfSlotsUsed() const { - return count_if(mItems, mItems + INVENTORY_SIZE, SlotUsed()); + return count_if(mItems, mItems + mSize, SlotUsed()); } int Inventory::getLastUsedSlot() const { - for (int i = INVENTORY_SIZE - 1; i >= 0; i--) { - if (SlotUsed()(mItems[i])) { + for (int i = mSize - 1; i >= 0; i--) + if (SlotUsed()(mItems[i])) return i; - } - } return -1; } |