diff options
author | Stijn Buys <ingar@osirion.org> | 2012-10-20 16:35:26 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2012-10-20 16:35:26 +0000 |
commit | 75274ebd6ba90784f5aa837b7e5ea97fc6bfb720 (patch) | |
tree | a5d51a87bf3f20833df18bc40a3254b946716afb /src/core/inventory.cc | |
parent | f01629dc14b1ee05b44d2e38b3dffbc1441fd85f (diff) |
Item id based inventory, support for weapon dealers.
Diffstat (limited to 'src/core/inventory.cc')
-rw-r--r-- | src/core/inventory.cc | 48 |
1 files changed, 42 insertions, 6 deletions
diff --git a/src/core/inventory.cc b/src/core/inventory.cc index 28327a3..c10e1c6 100644 --- a/src/core/inventory.cc +++ b/src/core/inventory.cc @@ -16,6 +16,7 @@ namespace core Inventory::Inventory(const float capacity) { inventory_timestamp = 0; + inventory_timestamp_erase = 0; inventory_capacity = capacity; inventory_capacity_used = 0; inventory_dirty = false; @@ -25,6 +26,7 @@ Inventory::~Inventory() { clear(); inventory_timestamp = 0; + inventory_timestamp_erase = 0; inventory_capacity = 0; inventory_capacity_used = 0; } @@ -48,24 +50,45 @@ void Inventory::set_dirty(const bool dirty) void Inventory::add(Item *item) { + assert(item != (Item *) 0x81); + + // assign an id + unsigned int id = 0; for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { - // check if the item was already added - if ((*it) == item) - return; + if ((*it)->id() > id) { + id = (*it)->id(); + } } + id++; + item->set_id(id); + inventory_items.push_back(item); } -void Inventory::remove(Item *item) +void Inventory::erase(Item *item) { for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { if ((*it) == item) { + delete (*it); + (*it) = 0; + inventory_items.erase(it); + inventory_timestamp_erase = (game() ? game()->timestamp() : 1); + return; + } + } +} + +void Inventory::erase(const unsigned int id) +{ + for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { + if ((*it)->id() == id) { + delete (*it); + (*it) = 0; inventory_items.erase(it); - delete item; + inventory_timestamp_erase = (game() ? game()->timestamp() : 1); return; } } - // FIXME remove doesn't work over network } Item *Inventory::find(const Info *info) const @@ -81,6 +104,19 @@ Item *Inventory::find(const Info *info) const return 0; } +Item *Inventory::find(const unsigned int id) const +{ + // sarch the inventory for a specified item id + for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { + Item *item = (*it); + if (item->id() == id) { + return item; + } + } + // not found + return 0; +} + void Inventory::clear() { for (Items::iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { |