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 Inventory::Entry *Inventory::FindItem(const Item *item) {
57 for (int i(0); i < MaxItems(); ++i) {
58 if (item == ItemAt(i)) {
65 bool Inventory::SloteFree(int offset) const {
66 return !ItemAt(offset);
70 void Inventory::Sort() {
71 std::sort(entries, entries + 96, Entry::Less);
74 bool Inventory::Entry::Less(const Entry &lhs, const Entry &rhs) {
77 return Item::Less(*lhs.item, *rhs.item);
86 void Inventory::SwapEntriesAt(int lhs, int rhs) {
87 std::swap(entries[lhs], entries[rhs]);