]> jfr.im git - irc/gameservirc.git/blob - gameserv/item.cpp
added items to the tavern.dat, added the filename option to the config file
[irc/gameservirc.git] / gameserv / item.cpp
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 temp = strtok(datastr, "~"); // Type
107
108 mytype = WEAPON;
109 // Grab the item's id
110 temp = strtok(NULL, "~");
111 id = stringtoint(temp);
112
113 // Grab the item's name
114 temp = strtok(NULL, "~");
115 myname = temp;
116
117 // Grab the item's price
118 temp = strtok(NULL, "~");
119 myprice = stringtoint(temp);
120
121 // Grab the item's uses
122 temp = strtok(NULL, "~");
123 myuses = stringtoint(temp);
124
125 // Grab the item's modifiers
126 for (int x = 0; x < 4; x++)
127 {
128 temp = strtok(NULL, "~");
129 mymodifiers[x] = stringtoint(temp);
130 }
131 // If we got here, we're successful
132 return true;
133 }
134 catch (char *str)
135 {
136 log("Exception setting weapon data: %s", str);
137 return false;
138 }
139 }
140
141 bool weapon::use(Player *p)
142 {
143 // weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0)
144 if (myuses == 0)
145 return false;
146 else
147 {
148 p->strength += mymodifiers[0];
149 p->defense += mymodifiers[1];
150 p->maxhp += mymodifiers[2];
151 }
152
153 if (myuses > 0)
154 {
155 myuses--;
156 }
157 return true;
158 }
159
160 void weapon::undo(Player *p)
161 {
162 p->strength -= mymodifiers[0];
163 p->defense -= mymodifiers[1];
164 p->maxhp -= mymodifiers[2];
165 }
166
167 armor::~armor()
168 {
169 }
170
171 void armor::undo(Player *p)
172 {
173 p->strength -= mymodifiers[0];
174 p->defense -= mymodifiers[1];
175 p->maxhp -= mymodifiers[2];
176 }
177
178 bool armor::setData(char *datastr)
179 {
180 try
181 {
182 char *temp;
183 strtok(datastr, "~"); // Type
184
185 mytype = ARMOR;
186
187 // Grab the item's id
188 temp = strtok(NULL, "~");
189 id = stringtoint(temp);
190
191 // Grab the item's name
192 temp = strtok(NULL, "~");
193 myname = temp;
194
195 // Grab the item's price
196 temp = strtok(NULL, "~");
197 myprice = stringtoint(temp);
198
199 // Grab the item's uses
200 temp = strtok(NULL, "~");
201 myuses = stringtoint(temp);
202
203 // Grab the item's modifiers
204 for (int x = 0; x < 4; x++)
205 {
206 temp = strtok(NULL, "~");
207 mymodifiers[x] = stringtoint(temp);
208 }
209 // If we got here, we were successful
210 return true;
211 }
212 catch(char *str)
213 {
214 log("Exception setting armor data: %s", str);
215 return false;
216 }
217
218 }
219
220 bool armor::use(Player *p)
221 {
222 // weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0)
223 if (myuses == 0)
224 return false;
225 else
226 {
227 p->strength += mymodifiers[0];
228 p->defense += mymodifiers[1];
229 p->maxhp += mymodifiers[2];
230 }
231
232 if (myuses > 0)
233 {
234 myuses--;
235 }
236 return true;
237 }
238
239 potion::~potion()
240 {
241 }
242
243 bool potion::use(Player *p)
244 {
245 // 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)
246
247 if (myuses == 0)
248 return false;
249 else
250 {
251 p->strength += myranges[0].random();
252 p->defense += myranges[1].random();
253 p->maxhp += myranges[2].random();
254 p->hp += myranges[3].random();
255 p->forest_fights += myranges[4].random();
256 p->player_fights += myranges[5].random();
257 p->gold += myranges[6].random();
258 p->bank += myranges[7].random();
259 }
260
261 if (myuses > 0)
262 {
263 myuses--;
264 }
265 return true;
266 }
267
268 void potion::undo(Player *p)
269 {
270 return;
271 }
272
273 bool potion::setData(char *datastr)
274 {
275 try
276 {
277 char *temp;
278
279 temp = strtok(datastr, "~"); // Type
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 < 8; x++)
301 {
302 temp = strtok(NULL, "~");
303 myranges[x].low = stringtoint(temp);
304 temp = strtok(NULL, "~");
305 myranges[x].high = stringtoint(temp);
306 }
307 }
308 catch(char *str)
309 {
310 log("Exception setting potion data: %s", str);
311 return false;
312 }
313 // If we got here, we were successful
314 return true;
315 }
316
317
318 itemContainer::itemContainer()
319 {
320 myuses = 0;
321 myitem = NULL;
322 }
323
324 itemContainer::itemContainer(item *i){
325 myuses = i->uses();
326 myitem = i;
327 }
328 itemContainer::itemContainer(const itemContainer &right)
329 {
330 myuses = right.myuses;
331 myitem = right.myitem;
332 }
333 itemContainer::~itemContainer()
334 {
335 myuses = 0;
336 myitem = NULL;
337 }
338
339 itemContainer &itemContainer::operator--()
340 {
341 --myuses;
342 return *this;
343 }
344
345 itemContainer itemContainer::operator--(int)
346 {
347 itemContainer oldValue = *this;
348
349 operator--();
350
351 return oldValue;
352 }
353
354 itemContainer &itemContainer::operator++()
355 {
356 ++myuses;
357
358 return *this;
359 }
360
361 itemContainer itemContainer::operator++(int)
362 {
363 itemContainer oldValue = *this;
364
365 operator++();
366
367 return oldValue;
368 }
369
370 void itemContainer::setItem(item *i)
371 {
372 myitem = i;
373 }
374
375 bool itemContainer::operator<(const itemContainer &right) const
376 {
377 return (*myitem < *right.myitem);
378 }
379
380 bool itemContainer::operator>(const itemContainer &right) const
381 {
382 return (*myitem > *right.myitem);
383 }
384
385 bool itemContainer::operator==(const itemContainer &right) const
386 {
387 return (*myitem == *right.myitem);
388 }
389
390 bool itemContainer::operator!=(const itemContainer &right) const
391 {
392 return (*myitem == *right.myitem);
393 }
394
395 void itemContainer::setUses(int uses)
396 {
397 myuses = uses;
398 }