+/*
+ * Inventory.cpp
+ *
+ * Created on: Aug 9, 2012
+ * Author: holy
+ */
+
+#include "Inventory.h"
+
+namespace common {
+
+Inventory::Inventory() {
+
+}
+
+bool Inventory::Add(const Item *item, int count) {
+ if (count > 99) return false;
+
+ Entry *entry(FindItem(item));
+ if (entry) {
+ if (entry->count + count > 99) {
+ return false;
+ } else {
+ entry->count += count;
+ return true;
+ }
+ } else {
+ for (int i(0); i < MaxItems(); ++i) {
+ if (SloteFree(i)) {
+ entries[i].item = item;
+ entries[i].count = count;
+ return true;
+ }
+ }
+ return false;
+ }
+}
+
+void Inventory::Remove(const Item *item, int count) {
+ Entry *entry(FindItem(item));
+ if (!entry) return;
+
+ if (entry->count <= count) {
+ entry->item = 0;
+ entry->count = 0;
+ } else {
+ entry->count -= count;
+ }
+}
+
+Inventory::Entry *Inventory::FindItem(const Item *item) {
+ for (int i(0); i < MaxItems(); ++i) {
+ if (item == ItemAt(i)) {
+ return entries + i;
+ }
+ }
+ return 0;
+}
+
+bool Inventory::SloteFree(int offset) const {
+ return !ItemAt(offset);
+}
+
+}