4 * Created on: Aug 9, 2012
17 Inventory::Inventory() {
21 bool Inventory::Add(const Item *item, int count) {
22 if (count > 99) return false;
24 Entry *entry(FindItem(item));
26 if (entry->count + count > 99) {
29 entry->count += count;
33 for (int i(0); i < MaxItems(); ++i) {
35 entries[i].item = item;
36 entries[i].count = count;
44 void Inventory::Remove(const Item *item, int count) {
45 Entry *entry(FindItem(item));
48 if (entry->count <= count) {
52 entry->count -= count;
56 void Inventory::RemoveAll(const Item *item) {
60 Inventory::Entry *Inventory::FindItem(const Item *item) {
61 for (int i(0); i < MaxItems(); ++i) {
62 if (item == ItemAt(i)) {
69 bool Inventory::SloteFree(int offset) const {
70 return !ItemAt(offset);
74 void Inventory::Sort() {
75 std::sort(entries, entries + 96, Entry::Less);
78 bool Inventory::Entry::Less(const Entry &lhs, const Entry &rhs) {
81 return Item::Less(*lhs.item, *rhs.item);
90 void Inventory::SwapEntriesAt(int lhs, int rhs) {
91 std::swap(entries[lhs], entries[rhs]);