]> git.localhorst.tv Git - l2e.git/blob - src/common/Inventory.cpp
d118f2187daeb9bb28f84d2ccd48882bad6c8c58
[l2e.git] / src / common / Inventory.cpp
1 /*
2  * Inventory.cpp
3  *
4  *  Created on: Aug 9, 2012
5  *      Author: holy
6  */
7
8 #include "Inventory.h"
9
10 namespace common {
11
12 Inventory::Inventory() {
13
14 }
15
16 bool Inventory::Add(const Item *item, int count) {
17         if (count > 99) return false;
18
19         Entry *entry(FindItem(item));
20         if (entry) {
21                 if (entry->count + count > 99) {
22                         return false;
23                 } else {
24                         entry->count += count;
25                         return true;
26                 }
27         } else {
28                 for (int i(0); i < MaxItems(); ++i) {
29                         if (SloteFree(i)) {
30                                 entries[i].item = item;
31                                 entries[i].count = count;
32                                 return true;
33                         }
34                 }
35                 return false;
36         }
37 }
38
39 void Inventory::Remove(const Item *item, int count) {
40         Entry *entry(FindItem(item));
41         if (!entry) return;
42
43         if (entry->count <= count) {
44                 entry->item = 0;
45                 entry->count = 0;
46         } else {
47                 entry->count -= count;
48         }
49 }
50
51 Inventory::Entry *Inventory::FindItem(const Item *item) {
52         for (int i(0); i < MaxItems(); ++i) {
53                 if (item == ItemAt(i)) {
54                         return entries + i;
55                 }
56         }
57         return 0;
58 }
59
60 bool Inventory::SloteFree(int offset) const {
61         return !ItemAt(offset);
62 }
63
64 }