diff options
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/info.cc | 5 | ||||
-rw-r--r-- | src/core/info.h | 17 | ||||
-rw-r--r-- | src/core/inventory.cc | 22 | ||||
-rw-r--r-- | src/core/inventory.h | 77 |
4 files changed, 104 insertions, 17 deletions
diff --git a/src/core/info.cc b/src/core/info.cc index c331d91..16e68f5 100644 --- a/src/core/info.cc +++ b/src/core/info.cc @@ -67,6 +67,7 @@ Info::Info(const InfoType *type, const char *label) : Label(label) info_model = 0; info_timestamp = 0; info_price = 0; + info_volume = 0; } // client-side constructor, id is passed as param @@ -124,6 +125,10 @@ void Info::set_price(const long price) info_price = price; } +void Info::set_volume(const float volume) +{ + info_volume = volume; +} void Info::set_timestamp(const unsigned long timestamp) { info_timestamp = timestamp; diff --git a/src/core/info.h b/src/core/info.h index 4b9c445..b3fa58f 100644 --- a/src/core/info.h +++ b/src/core/info.h @@ -95,6 +95,10 @@ public: inline const long price() const { return info_price; } + + inline const float volume() const { + return info_volume; + } /** * @brief timestamp @@ -107,7 +111,9 @@ public: return info_timestamp; } - /// text description + /** + * @brief text description + */ inline const Text & text() const { return info_text; } @@ -122,8 +128,16 @@ public: void set_model(const model::Model *model); + /** + * @brief associated price, in credits + */ void set_price(const long price); + /** + * @brief associated volume, in cubic meters + */ + void set_volume(const float volume); + /// set the timestamp void set_timestamp(const unsigned long timestamp); @@ -159,6 +173,7 @@ private: unsigned int info_id; long info_price; + float info_volume; unsigned long info_timestamp; std::string info_modelname; diff --git a/src/core/inventory.cc b/src/core/inventory.cc index c659178..8f8c41a 100644 --- a/src/core/inventory.cc +++ b/src/core/inventory.cc @@ -13,15 +13,24 @@ namespace core /* ---- class Inventory -------------------------------------------- */ -Inventory::Inventory() +Inventory::Inventory(const float capacity) { inventory_timestamp = 0; + inventory_capacity = capacity; + inventory_capacity_used = 0; } Inventory::~Inventory() { clear(); inventory_timestamp = 0; + inventory_capacity = 0; + inventory_capacity_used = 0; +} + +void Inventory::set_capacity(const float capacity) +{ + inventory_capacity = capacity; } void Inventory::set_timestamp(const unsigned long timestamp) @@ -31,6 +40,7 @@ void Inventory::set_timestamp(const unsigned long timestamp) void Inventory::set_dirty() { + recalculate(); inventory_timestamp = core::game()->timestamp(); } @@ -75,6 +85,16 @@ void Inventory::clear() delete item; } inventory_items.clear(); + inventory_capacity_used = 0; +} + +void Inventory::recalculate() +{ + inventory_capacity_used = 0; + for (Items::const_iterator it = inventory_items.begin(); it != inventory_items.end(); it++) { + const Item *item = (*it); + inventory_capacity_used += item->amount() * item->info()->volume(); + } } } // namespace core diff --git a/src/core/inventory.h b/src/core/inventory.h index 0a412b0..aff9790 100644 --- a/src/core/inventory.h +++ b/src/core/inventory.h @@ -21,20 +21,59 @@ class Inventory { public: /** - * @brief type definition for the items in the inventory + * @brief type definition for items in the inventory */ typedef std::vector<Item *> Items; /** * @brief default constructor */ - Inventory(); + Inventory(const float capacity = 0); /** * @brief default destructor */ ~Inventory(); + /* ---- inspectors ------------------------------------------------- */ + + /** + * @brief items in the inventory + */ + inline Items &items() { + return inventory_items; + }; + + /** + * @brief return the timestamp + */ + inline const unsigned long timestamp() const { + return inventory_timestamp; + } + + /** + * @brief return the maximal inventory capacity, in cubic meters + */ + inline const float capacity() const { + return inventory_capacity; + } + + /** + * @brief return the used inventory capacity, in cubic meters + */ + inline const float capacity_used() const { + return inventory_capacity_used; + } + + /** + * @brief return the availableinventory capacity, in cubic meters + */ + inline const float capacity_available() const { + return inventory_capacity - inventory_capacity_used; + } + + /* ---- mutators --------------------------------------------------- */ + /** * @brief add an item to the inventory */ @@ -54,32 +93,40 @@ public: * @brief search the inventory for a specific item type */ Item *find(const Info *info); - - inline Items &items() { - return inventory_items; - }; - - /** - * @brief return the timestamp - */ - inline const unsigned long timestamp() const { - return inventory_timestamp; - } - + /** * @brief set the timestamp */ void set_timestamp(const unsigned long timestamp); /** - * @brief set the timestamp to the current game time + * @brief mark the inventory as dirty + * This method will set the timestamp to the current game time + * and will recalculate the available capacity + * @see recalculate() */ void set_dirty(); + /** + * @brief set the maximal inventory capacity, in cubic meters + */ + void set_capacity(const float capacity); + private: + // recalculate inventory capacity + void recalculate(); + + // items in the inventory Items inventory_items; + // timestamp when inventory was last updated unsigned long inventory_timestamp; + + // maximum inventory capacity, in cubic meters + float inventory_capacity; + + // current capacity used, in cubic meters + float inventory_capacity_used; }; } // namsepace core |