]>
Commit | Line | Data |
---|---|---|
8ad9f659 | 1 | #include "item.h" |
37ed80a9 | 2 | #include "player.h" |
7f56a68b | 3 | #include "extern.h" |
8ad9f659 | 4 | |
37ed80a9 | 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 | ||
b6bf4226 | 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) |
8ad9f659 | 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; | |
b6bf4226 | 27 | id = identifier; |
8ad9f659 | 28 | } |
29 | ||
b6bf4226 | 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) |
8ad9f659 | 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; | |
b6bf4226 | 43 | id = identifier; |
8ad9f659 | 44 | } |
45 | ||
46 | item::~item() | |
47 | { | |
b6bf4226 | 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; | |
8ad9f659 | 61 | } |
62 | ||
26b17386 | 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 | ||
7f56a68b | 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 | } | |
b6bf4226 | 97 | weapon::~weapon() |
98 | { | |
99 | } | |
100 | ||
7f56a68b | 101 | bool weapon::setData(char *datastr) |
102 | { | |
103 | try | |
104 | { | |
105 | char *temp; | |
7f56a68b | 106 | |
107 | mytype = WEAPON; | |
108 | // Grab the item's id | |
109 | temp = strtok(NULL, "~"); | |
7f56a68b | 110 | id = stringtoint(temp); |
111 | ||
112 | // Grab the item's name | |
113 | temp = strtok(NULL, "~"); | |
7f56a68b | 114 | myname = temp; |
115 | ||
116 | // Grab the item's price | |
117 | temp = strtok(NULL, "~"); | |
7f56a68b | 118 | myprice = stringtoint(temp); |
119 | ||
120 | // Grab the item's uses | |
121 | temp = strtok(NULL, "~"); | |
7f56a68b | 122 | myuses = stringtoint(temp); |
123 | ||
124 | // Grab the item's modifiers | |
125 | for (int x = 0; x < 4; x++) | |
126 | { | |
127 | temp = strtok(NULL, "~"); | |
7f56a68b | 128 | mymodifiers[x] = stringtoint(temp); |
129 | } | |
42c1eadd | 130 | // If we got here, we're successful |
7f56a68b | 131 | return true; |
132 | } | |
133 | catch (char *str) | |
134 | { | |
cd973e97 | 135 | log("Exception setting weapon data: %s", str); |
7f56a68b | 136 | return false; |
137 | } | |
138 | } | |
139 | ||
8ad9f659 | 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 | ||
b6bf4226 | 166 | armor::~armor() |
167 | { | |
168 | } | |
169 | ||
37ed80a9 | 170 | void armor::undo(Player *p) |
171 | { | |
172 | p->strength -= mymodifiers[0]; | |
173 | p->defense -= mymodifiers[1]; | |
174 | p->maxhp -= mymodifiers[2]; | |
175 | } | |
176 | ||
7f56a68b | 177 | bool armor::setData(char *datastr) |
b6bf4226 | 178 | { |
cd973e97 | 179 | try |
7f56a68b | 180 | { |
42c1eadd | 181 | char *temp; |
182 | mytype = ARMOR; | |
cd973e97 | 183 | |
184 | // Grab the item's id | |
42c1eadd | 185 | temp = strtok(NULL, "~"); |
cd973e97 | 186 | id = stringtoint(temp); |
42c1eadd | 187 | log("id = %ld", id); |
cd973e97 | 188 | |
189 | // Grab the item's name | |
42c1eadd | 190 | temp = strtok(NULL, "~"); |
cd973e97 | 191 | myname = temp; |
42c1eadd | 192 | log("name = %s", myname.c_str()); |
cd973e97 | 193 | |
194 | // Grab the item's price | |
42c1eadd | 195 | temp = strtok(NULL, "~"); |
cd973e97 | 196 | myprice = stringtoint(temp); |
42c1eadd | 197 | log("price = %ld", myprice); |
cd973e97 | 198 | |
199 | // Grab the item's uses | |
42c1eadd | 200 | temp = strtok(NULL, "~"); |
cd973e97 | 201 | myuses = stringtoint(temp); |
42c1eadd | 202 | log("uses = %d", myuses); |
cd973e97 | 203 | |
204 | // Grab the item's modifiers | |
42c1eadd | 205 | for (int x = 0; x < 4; x++) |
cd973e97 | 206 | { |
42c1eadd | 207 | temp = strtok(NULL, "~"); |
cd973e97 | 208 | mymodifiers[x] = stringtoint(temp); |
42c1eadd | 209 | log("modifier %d = %ld", x, mymodifiers[x]); |
cd973e97 | 210 | } |
42c1eadd | 211 | // If we got here, we were successful |
212 | return true; | |
cd973e97 | 213 | } |
214 | catch(char *str) | |
215 | { | |
216 | log("Exception setting armor data: %s", str); | |
217 | return false; | |
7f56a68b | 218 | } |
219 | ||
b6bf4226 | 220 | } |
221 | ||
37ed80a9 | 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 | ||
cd973e97 | 241 | potion::~potion() |
242 | { | |
243 | } | |
244 | ||
8ad9f659 | 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 | } | |
37ed80a9 | 269 | |
270 | void potion::undo(Player *p) | |
271 | { | |
272 | return; | |
273 | } | |
b6bf4226 | 274 | |
7f56a68b | 275 | bool potion::setData(char *datastr) |
b6bf4226 | 276 | { |
cd973e97 | 277 | try |
7f56a68b | 278 | { |
cd973e97 | 279 | char *temp; |
280 | ||
281 | mytype = ARMOR; | |
282 | ||
283 | // Grab the item's id | |
42c1eadd | 284 | temp = strtok(NULL, "~"); |
cd973e97 | 285 | id = stringtoint(temp); |
286 | ||
287 | // Grab the item's name | |
42c1eadd | 288 | temp = strtok(NULL, "~"); |
cd973e97 | 289 | myname = temp; |
290 | ||
291 | // Grab the item's price | |
42c1eadd | 292 | temp = strtok(NULL, "~"); |
cd973e97 | 293 | myprice = stringtoint(temp); |
294 | ||
295 | // Grab the item's uses | |
42c1eadd | 296 | temp = strtok(NULL, "~"); |
cd973e97 | 297 | myuses = stringtoint(temp); |
298 | ||
299 | // Grab the item's modifiers | |
300 | for (int x = 0; x < 5; x++) | |
301 | { | |
42c1eadd | 302 | temp = strtok(NULL, "~"); |
cd973e97 | 303 | mymodifiers[x] = stringtoint(temp); |
304 | } | |
305 | } | |
306 | catch(char *str) | |
307 | { | |
308 | log("Exception setting potion data: %s", str); | |
309 | return false; | |
7f56a68b | 310 | } |
7f56a68b | 311 | // If we got here, we were successful |
b6bf4226 | 312 | return true; |
313 | } | |
e696687e | 314 | |
315 | ||
316 | itemContainer::itemContainer() | |
317 | { | |
318 | myuses = 0; | |
319 | myitem = NULL; | |
320 | } | |
321 | ||
42c1eadd | 322 | itemContainer::itemContainer(item *i){ |
e696687e | 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 | } |