]> jfr.im git - irc/gameservirc.git/blob - gameserv/pouch.cpp
Added messages.cpp and find.cpp, which contain functions from gameserv.cpp
[irc/gameservirc.git] / gameserv / pouch.cpp
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 count = 0;
33 }
34
35 bool pouch::isEmpty()
36 {
37 return items.empty();
38 }
39
40 void pouch::sort()
41 {
42 items.sort();
43 }
44
45 itemContainer *pouch::Find(int id)
46 {
47 list<itemContainer>::iterator item_iter;
48 item_iter = items.begin();
49
50 while (item_iter != items.end())
51 {
52 if ((*item_iter).getItem()->getID() == id)
53 {
54 return &(*item_iter);
55 }
56 ++item_iter;
57 }
58 return NULL;
59 }
60
61 itemContainer *pouch::Find(char *n)
62 {
63 list<itemContainer>::iterator item_iter;
64 item_iter = items.begin();
65
66 while (item_iter != items.end())
67 {
68 if ((*item_iter).getItem()->getName() == n)
69 {
70 return &(*item_iter);
71 }
72 ++item_iter;
73 }
74 return NULL;
75 }
76
77 itemContainer *pouch::Find(string &n)
78 {
79 list<itemContainer>::iterator item_iter;
80 item_iter = items.begin();
81
82 while (item_iter != items.end())
83 {
84 if ((*item_iter).getItem()->getName() == n)
85 {
86 return &(*item_iter);
87 }
88 ++item_iter;
89 }
90
91 return NULL;
92 }
93
94 itemContainer *pouch::addItem(item *i)
95 {
96 if (count >= 3000 || count >= maxitems)
97 {
98 return NULL;
99 }
100 else
101 {
102 itemContainer it(i), *temp;
103 items.push_front(it);
104 ++count;
105 temp = &items.front();
106 sort();
107 return temp;
108 }
109 }
110
111 itemContainer *pouch::addItem(item *i, int amt)
112 {
113 if (count >= 3000 || (count + amt) >= maxitems)
114 {
115 return NULL;
116 }
117 else
118 {
119 itemContainer it(i), *temp;
120 for (int x=0; x < amt; x++)
121 {
122 items.push_front(it);
123 ++count;
124 }
125 temp = &items.front();
126 sort();
127 return temp;
128 }
129 }
130
131 itemContainer *pouch::addItemNoChecks(item *i)
132 {
133 itemContainer it(i);
134 items.push_front(it);
135 ++count;
136 return &items.front();
137 }
138
139 void pouch::deleteItem(item *i)
140 {
141 list<itemContainer>::iterator item_iter;
142
143 item_iter = find(items.begin(), items.end(), i);
144
145 if (item_iter != items.end())
146 {
147 items.erase(item_iter);
148 count--;
149 }
150 }
151
152 const pouch &pouch::operator=(const pouch &right)
153 {
154 if (&right != this)
155 {
156 items.clear();
157 items = right.items;
158 }
159 return *this;
160 }