]> git.localhorst.tv Git - l2e.git/blob - src/common/Inventory.cpp
removed stupid file headers that eclipse put in
[l2e.git] / src / common / Inventory.cpp
1 #include "Inventory.h"
2
3 #include "Item.h"
4
5 #include <algorithm>
6
7
8 namespace common {
9
10 Inventory::Inventory()
11 : scenarioEnd(0) {
12
13 }
14
15 bool Inventory::Add(const Item *item, int count) {
16         if (count > 99) return false;
17
18         Entry *entry(FindItem(item));
19         if (entry) {
20                 if (entry->count + count > 99) {
21                         return false;
22                 } else {
23                         entry->count += count;
24                         return true;
25                 }
26         } else {
27                 for (int i(0); i < MaxItems(); ++i) {
28                         if (SloteFree(i)) {
29                                 entries[i].item = item;
30                                 entries[i].count = count;
31                                 return true;
32                         }
33                 }
34                 return false;
35         }
36 }
37
38 void Inventory::Remove(const Item *item, int count) {
39         Entry *entry(FindItem(item));
40         if (!entry) return;
41
42         if (entry->count <= count) {
43                 entry->item = 0;
44                 entry->count = 0;
45         } else {
46                 entry->count -= count;
47         }
48 }
49
50 void Inventory::RemoveAll(const Item *item) {
51         Remove(item, 255);
52 }
53
54 Inventory::Entry *Inventory::FindItem(const Item *item) {
55         for (int i(0); i < MaxItems(); ++i) {
56                 if (item == ItemAt(i)) {
57                         return entries + i;
58                 }
59         }
60         return 0;
61 }
62
63 bool Inventory::SloteFree(int offset) const {
64         return !ItemAt(offset);
65 }
66
67
68 bool Inventory::AddScenarioItem(const Item *i) {
69         if (scenarioEnd < MaxScenarioItems()) {
70                 scenario[scenarioEnd] = i;
71                 ++scenarioEnd;
72                 return true;
73         } else {
74                 return false;
75         }
76 }
77
78
79 void Inventory::Sort() {
80         std::sort(entries, entries + 96, Entry::Less);
81 }
82
83 bool Inventory::Entry::Less(const Entry &lhs, const Entry &rhs) {
84         if (lhs.item) {
85                 if (rhs.item) {
86                         return Item::Less(*lhs.item, *rhs.item);
87                 } else {
88                         return true;
89                 }
90         } else {
91                 return false;
92         }
93 }
94
95 void Inventory::SwapEntriesAt(int lhs, int rhs) {
96         std::swap(entries[lhs], entries[rhs]);
97 }
98
99 }