]>
Commit | Line | Data |
---|---|---|
1 | #include "item.h" | |
2 | #include "player.h" | |
3 | #include "extern.h" | |
4 | ||
5 | item::item() | |
6 | { | |
7 | myname = "New Item"; | |
8 | myprice = 0; | |
9 | myuses = 0; | |
10 | for (int x = 0; x < 8; x++) | |
11 | mymodifiers[x] = 0; | |
12 | } | |
13 | ||
14 | item::item(const char *name, long int p, int uses, long int identifier, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8) | |
15 | { | |
16 | myname = name; // string = char* | |
17 | myprice = p; | |
18 | myuses = uses; | |
19 | mymodifiers[0] = m1; | |
20 | mymodifiers[1] = m2; | |
21 | mymodifiers[2] = m3; | |
22 | mymodifiers[3] = m4; | |
23 | mymodifiers[4] = m5; | |
24 | mymodifiers[5] = m6; | |
25 | mymodifiers[6] = m7; | |
26 | mymodifiers[7] = m8; | |
27 | id = identifier; | |
28 | } | |
29 | ||
30 | item::item(string name, long int p, int uses, long int identifier, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8) | |
31 | { | |
32 | myname = name; // string = char* | |
33 | myprice = p; | |
34 | myuses = uses; | |
35 | mymodifiers[0] = m1; | |
36 | mymodifiers[1] = m2; | |
37 | mymodifiers[2] = m3; | |
38 | mymodifiers[3] = m4; | |
39 | mymodifiers[4] = m5; | |
40 | mymodifiers[5] = m6; | |
41 | mymodifiers[6] = m7; | |
42 | mymodifiers[7] = m8; | |
43 | id = identifier; | |
44 | } | |
45 | ||
46 | item::~item() | |
47 | { | |
48 | for (int x = 0; x < 8; x++) | |
49 | { | |
50 | mymodifiers[x] = 0; | |
51 | } | |
52 | ||
53 | myuses = 0; | |
54 | myname = ""; | |
55 | myprice = 0; | |
56 | } | |
57 | ||
58 | void item::setType(type t) | |
59 | { | |
60 | mytype = t; | |
61 | } | |
62 | ||
63 | bool item::operator<(const item &right) const | |
64 | { | |
65 | return myname < right.myname; | |
66 | } | |
67 | ||
68 | bool item::operator>(const item &right) const | |
69 | { | |
70 | return myname > right.myname; | |
71 | } | |
72 | ||
73 | bool item::operator==(const item &right) const | |
74 | { | |
75 | return myname == right.myname; | |
76 | } | |
77 | ||
78 | bool item::operator!=(const item &right) const | |
79 | { | |
80 | return myname != right.myname; | |
81 | } | |
82 | ||
83 | item &item::operator=(const item &right) | |
84 | { | |
85 | myname = right.myname; | |
86 | myprice = right.myprice; | |
87 | myuses = right.myuses; | |
88 | id = right.id; | |
89 | mytype = right.mytype; | |
90 | ||
91 | for (int x = 0; x < 8; x++) | |
92 | { | |
93 | mymodifiers[x] = right.mymodifiers[x]; | |
94 | } | |
95 | return *this; // enables cascading x=y=z; | |
96 | } | |
97 | weapon::~weapon() | |
98 | { | |
99 | } | |
100 | ||
101 | bool weapon::setData(char *datastr) | |
102 | { | |
103 | try | |
104 | { | |
105 | char *temp; | |
106 | ||
107 | mytype = WEAPON; | |
108 | // Grab the item's id | |
109 | temp = strtok(NULL, "~"); | |
110 | id = stringtoint(temp); | |
111 | ||
112 | // Grab the item's name | |
113 | temp = strtok(NULL, "~"); | |
114 | myname = temp; | |
115 | ||
116 | // Grab the item's price | |
117 | temp = strtok(NULL, "~"); | |
118 | myprice = stringtoint(temp); | |
119 | ||
120 | // Grab the item's uses | |
121 | temp = strtok(NULL, "~"); | |
122 | myuses = stringtoint(temp); | |
123 | ||
124 | // Grab the item's modifiers | |
125 | for (int x = 0; x < 4; x++) | |
126 | { | |
127 | temp = strtok(NULL, "~"); | |
128 | mymodifiers[x] = stringtoint(temp); | |
129 | } | |
130 | // If we got here, we're successful | |
131 | return true; | |
132 | } | |
133 | catch (char *str) | |
134 | { | |
135 | log("Exception setting weapon data: %s", str); | |
136 | return false; | |
137 | } | |
138 | } | |
139 | ||
140 | bool weapon::use(Player *p) | |
141 | { | |
142 | // weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0) | |
143 | if (myuses == 0) | |
144 | return false; | |
145 | else | |
146 | { | |
147 | p->strength += mymodifiers[0]; | |
148 | p->defense += mymodifiers[1]; | |
149 | p->maxhp += mymodifiers[2]; | |
150 | } | |
151 | ||
152 | if (myuses > 0) | |
153 | { | |
154 | myuses--; | |
155 | } | |
156 | return true; | |
157 | } | |
158 | ||
159 | void weapon::undo(Player *p) | |
160 | { | |
161 | p->strength -= mymodifiers[0]; | |
162 | p->defense -= mymodifiers[1]; | |
163 | p->maxhp -= mymodifiers[2]; | |
164 | } | |
165 | ||
166 | armor::~armor() | |
167 | { | |
168 | } | |
169 | ||
170 | void armor::undo(Player *p) | |
171 | { | |
172 | p->strength -= mymodifiers[0]; | |
173 | p->defense -= mymodifiers[1]; | |
174 | p->maxhp -= mymodifiers[2]; | |
175 | } | |
176 | ||
177 | bool armor::setData(char *datastr) | |
178 | { | |
179 | try | |
180 | { | |
181 | char *temp; | |
182 | mytype = ARMOR; | |
183 | ||
184 | // Grab the item's id | |
185 | temp = strtok(NULL, "~"); | |
186 | id = stringtoint(temp); | |
187 | log("id = %ld", id); | |
188 | ||
189 | // Grab the item's name | |
190 | temp = strtok(NULL, "~"); | |
191 | myname = temp; | |
192 | log("name = %s", myname.c_str()); | |
193 | ||
194 | // Grab the item's price | |
195 | temp = strtok(NULL, "~"); | |
196 | myprice = stringtoint(temp); | |
197 | log("price = %ld", myprice); | |
198 | ||
199 | // Grab the item's uses | |
200 | temp = strtok(NULL, "~"); | |
201 | myuses = stringtoint(temp); | |
202 | log("uses = %d", myuses); | |
203 | ||
204 | // Grab the item's modifiers | |
205 | for (int x = 0; x < 4; x++) | |
206 | { | |
207 | temp = strtok(NULL, "~"); | |
208 | mymodifiers[x] = stringtoint(temp); | |
209 | log("modifier %d = %ld", x, mymodifiers[x]); | |
210 | } | |
211 | // If we got here, we were successful | |
212 | return true; | |
213 | } | |
214 | catch(char *str) | |
215 | { | |
216 | log("Exception setting armor data: %s", str); | |
217 | return false; | |
218 | } | |
219 | ||
220 | } | |
221 | ||
222 | bool armor::use(Player *p) | |
223 | { | |
224 | // weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0) | |
225 | if (myuses == 0) | |
226 | return false; | |
227 | else | |
228 | { | |
229 | p->strength += mymodifiers[0]; | |
230 | p->defense += mymodifiers[1]; | |
231 | p->maxhp += mymodifiers[2]; | |
232 | } | |
233 | ||
234 | if (myuses > 0) | |
235 | { | |
236 | myuses--; | |
237 | } | |
238 | return true; | |
239 | } | |
240 | ||
241 | potion::~potion() | |
242 | { | |
243 | } | |
244 | ||
245 | bool potion::use(Player *p) | |
246 | { | |
247 | // potion(char *name, int p=0, int uses = 1, int strength=0, int defense=0, int maxhp=0, int hp=0, int forest_fights=0, int player_fights=0, int gold=0, int bank=0) | |
248 | ||
249 | if (myuses == 0) | |
250 | return false; | |
251 | else | |
252 | { | |
253 | p->strength += mymodifiers[0]; | |
254 | p->defense += mymodifiers[1]; | |
255 | p->maxhp += mymodifiers[2]; | |
256 | p->hp += mymodifiers[3]; | |
257 | p->forest_fights += mymodifiers[4]; | |
258 | p->player_fights += mymodifiers[5]; | |
259 | p->gold += mymodifiers[6]; | |
260 | p->bank += mymodifiers[7]; | |
261 | } | |
262 | ||
263 | if (myuses > 0) | |
264 | { | |
265 | myuses--; | |
266 | } | |
267 | return true; | |
268 | } | |
269 | ||
270 | void potion::undo(Player *p) | |
271 | { | |
272 | return; | |
273 | } | |
274 | ||
275 | bool potion::setData(char *datastr) | |
276 | { | |
277 | try | |
278 | { | |
279 | char *temp; | |
280 | ||
281 | mytype = ARMOR; | |
282 | ||
283 | // Grab the item's id | |
284 | temp = strtok(NULL, "~"); | |
285 | id = stringtoint(temp); | |
286 | ||
287 | // Grab the item's name | |
288 | temp = strtok(NULL, "~"); | |
289 | myname = temp; | |
290 | ||
291 | // Grab the item's price | |
292 | temp = strtok(NULL, "~"); | |
293 | myprice = stringtoint(temp); | |
294 | ||
295 | // Grab the item's uses | |
296 | temp = strtok(NULL, "~"); | |
297 | myuses = stringtoint(temp); | |
298 | ||
299 | // Grab the item's modifiers | |
300 | for (int x = 0; x < 5; x++) | |
301 | { | |
302 | temp = strtok(NULL, "~"); | |
303 | mymodifiers[x] = stringtoint(temp); | |
304 | } | |
305 | } | |
306 | catch(char *str) | |
307 | { | |
308 | log("Exception setting potion data: %s", str); | |
309 | return false; | |
310 | } | |
311 | // If we got here, we were successful | |
312 | return true; | |
313 | } | |
314 | ||
315 | ||
316 | itemContainer::itemContainer() | |
317 | { | |
318 | myuses = 0; | |
319 | myitem = NULL; | |
320 | } | |
321 | ||
322 | itemContainer::itemContainer(item *i){ | |
323 | myuses = i->uses(); | |
324 | myitem = i; | |
325 | } | |
326 | itemContainer::itemContainer(const itemContainer &right) | |
327 | { | |
328 | myuses = right.myuses; | |
329 | myitem = right.myitem; | |
330 | } | |
331 | itemContainer::~itemContainer() | |
332 | { | |
333 | myuses = 0; | |
334 | myitem = NULL; | |
335 | } | |
336 | ||
337 | itemContainer &itemContainer::operator--() | |
338 | { | |
339 | --myuses; | |
340 | return *this; | |
341 | } | |
342 | ||
343 | itemContainer itemContainer::operator--(int) | |
344 | { | |
345 | itemContainer oldValue = *this; | |
346 | ||
347 | operator--(); | |
348 | ||
349 | return oldValue; | |
350 | } | |
351 | ||
352 | itemContainer &itemContainer::operator++() | |
353 | { | |
354 | ++myuses; | |
355 | ||
356 | return *this; | |
357 | } | |
358 | ||
359 | itemContainer itemContainer::operator++(int) | |
360 | { | |
361 | itemContainer oldValue = *this; | |
362 | ||
363 | operator++(); | |
364 | ||
365 | return oldValue; | |
366 | } | |
367 | ||
368 | void itemContainer::setItem(item *i) | |
369 | { | |
370 | myitem = i; | |
371 | } | |
372 | ||
373 | bool itemContainer::operator<(const itemContainer &right) const | |
374 | { | |
375 | return (*myitem < *right.myitem); | |
376 | } | |
377 | ||
378 | bool itemContainer::operator>(const itemContainer &right) const | |
379 | { | |
380 | return (*myitem > *right.myitem); | |
381 | } | |
382 | ||
383 | bool itemContainer::operator==(const itemContainer &right) const | |
384 | { | |
385 | return (*myitem == *right.myitem); | |
386 | } | |
387 | ||
388 | bool itemContainer::operator!=(const itemContainer &right) const | |
389 | { | |
390 | return (*myitem == *right.myitem); | |
391 | } |