]>
Commit | Line | Data |
---|---|---|
1 | #include "extern.h" | |
2 | #include "pouch.h" | |
3 | #include "item.h" | |
4 | #include <list> | |
5 | #include <algorithm> | |
6 | ||
7 | pouch::pouch() | |
8 | { | |
9 | count = 0; | |
10 | } | |
11 | ||
12 | pouch::~pouch() | |
13 | { | |
14 | clear(); | |
15 | } | |
16 | ||
17 | pouch::pouch(const pouch &p) | |
18 | { | |
19 | count = p.count; | |
20 | items = p.items; | |
21 | } | |
22 | ||
23 | pouch::pouch(pouch *p) | |
24 | { | |
25 | count = p->count; | |
26 | items = p->items; | |
27 | } | |
28 | ||
29 | void pouch::clear() | |
30 | { | |
31 | items.clear(); | |
32 | } | |
33 | ||
34 | bool pouch::isEmpty() | |
35 | { | |
36 | return items.empty(); | |
37 | } | |
38 | ||
39 | void pouch::sort() | |
40 | { | |
41 | items.sort(); | |
42 | } | |
43 | ||
44 | itemContainer *pouch::Find(int id) | |
45 | { | |
46 | list<itemContainer>::iterator item_iter; | |
47 | item_iter = items.begin(); | |
48 | ||
49 | while (item_iter != items.end()) | |
50 | { | |
51 | if ((*item_iter).getItem()->getID() == id) | |
52 | { | |
53 | return &(*item_iter); | |
54 | } | |
55 | ++item_iter; | |
56 | } | |
57 | return NULL; | |
58 | } | |
59 | ||
60 | itemContainer *pouch::Find(char *n) | |
61 | { | |
62 | list<itemContainer>::iterator item_iter; | |
63 | item_iter = items.begin(); | |
64 | ||
65 | while (item_iter != items.end()) | |
66 | { | |
67 | if ((*item_iter).getItem()->getName() == n) | |
68 | { | |
69 | return &(*item_iter); | |
70 | } | |
71 | ++item_iter; | |
72 | } | |
73 | return NULL; | |
74 | } | |
75 | ||
76 | itemContainer *pouch::Find(string &n) | |
77 | { | |
78 | list<itemContainer>::iterator item_iter; | |
79 | item_iter = items.begin(); | |
80 | ||
81 | while (item_iter != items.end()) | |
82 | { | |
83 | if ((*item_iter).getItem()->getName() == n) | |
84 | { | |
85 | return &(*item_iter); | |
86 | } | |
87 | ++item_iter; | |
88 | } | |
89 | ||
90 | return NULL; | |
91 | } | |
92 | ||
93 | itemContainer *pouch::addItem(item *i) | |
94 | { | |
95 | if (count >= 3000 || count >= maxitems) | |
96 | { | |
97 | return NULL; | |
98 | } | |
99 | else | |
100 | { | |
101 | itemContainer it(i), *temp; | |
102 | items.push_front(it); | |
103 | ++count; | |
104 | temp = &items.front(); | |
105 | sort(); | |
106 | return temp; | |
107 | } | |
108 | } | |
109 | ||
110 | itemContainer *pouch::addItem(item *i, int amt) | |
111 | { | |
112 | if (count >= 3000 || (count + amt) >= maxitems) | |
113 | { | |
114 | return NULL; | |
115 | } | |
116 | else | |
117 | { | |
118 | itemContainer it(i), *temp; | |
119 | for (int x=0; x < amt; x++) | |
120 | { | |
121 | items.push_front(it); | |
122 | ++count; | |
123 | } | |
124 | temp = &items.front(); | |
125 | sort(); | |
126 | return temp; | |
127 | } | |
128 | } | |
129 | ||
130 | itemContainer *pouch::addItemNoChecks(item *i) | |
131 | { | |
132 | itemContainer it(i); | |
133 | items.push_front(it); | |
134 | ++count; | |
135 | return &items.front(); | |
136 | } | |
137 | ||
138 | void pouch::deleteItem(item *i) | |
139 | { | |
140 | list<itemContainer>::iterator item_iter; | |
141 | ||
142 | item_iter = find(items.begin(), items.end(), i); | |
143 | ||
144 | if (item_iter != items.end()) | |
145 | { | |
146 | items.erase(item_iter); | |
147 | count--; | |
148 | } | |
149 | } | |
150 | ||
151 | const pouch &pouch::operator=(const pouch &right) | |
152 | { | |
153 | if (&right != this) | |
154 | { | |
155 | items.clear(); | |
156 | items = right.items; | |
157 | } | |
158 | return *this; | |
159 | } |