]>
Commit | Line | Data |
---|---|---|
26b17386 | 1 | #include "item.h" |
85ce9d3e | 2 | #include "player.h" |
3662210f | 3 | #include "pouch.h" |
20d5d721 | 4 | #include "extern.h" |
ee38284f | 5 | #include "flags.h" |
23f22a79 | 6 | |
5c449fde | 7 | #include <string> |
0a0863d1 | 8 | #include <stdio.h> |
5c449fde | 9 | |
23f22a79 | 10 | #if defined(HAVE_CRYPT_H) |
11 | ||
12 | #include <crypt.h> | |
13 | ||
14 | #elif defined(HAVE_UNISTD_H) | |
15 | ||
16 | #include <unistd.h> | |
17 | ||
18 | #endif | |
19 | ||
5c449fde | 20 | using namespace std; |
85ce9d3e | 21 | |
e3c5fe46 | 22 | void Player::reset() |
85ce9d3e | 23 | { |
37ed80a9 | 24 | exp = 1; |
25 | gold = 0; | |
26 | bank = 500; | |
27 | hp = 10; | |
28 | maxhp = 10; | |
1781f48a | 29 | strength = 2; |
30 | defense = 1; | |
37ed80a9 | 31 | level = 1; |
32 | forest_fights = forestfights; | |
33 | player_fights = 3; | |
34 | fight = NULL; | |
35 | master = NULL; | |
36 | battle = NULL; | |
37 | flags = 0; | |
38 | lastcommand = 0; | |
4c2a9f3b | 39 | lastlogin = time(NULL); |
37ed80a9 | 40 | setAlive(this); |
4c2a9f3b | 41 | clearMaster(this); |
1781f48a | 42 | w = NULL; |
43 | a = NULL; | |
37ed80a9 | 44 | inventory->clear(); |
e3c5fe46 | 45 | } |
40251952 | 46 | |
85bcf836 | 47 | Player::Player() |
48 | { | |
37ed80a9 | 49 | inventory = new pouch(); |
50 | setData(NULL); | |
51 | ||
52 | #ifdef DEBUGMODE | |
53 | log("New Player: %s", name.c_str()); | |
54 | #endif | |
85bcf836 | 55 | } |
e3c5fe46 | 56 | |
85ce9d3e | 57 | Player::Player(char *n) |
58 | { | |
37ed80a9 | 59 | inventory = new pouch(); |
60 | reset(); // Set defaults | |
61 | ||
62 | name = n; | |
63 | ||
64 | #ifdef DEBUGMODE | |
65 | log("New Player: %s", name.c_str()); | |
66 | #endif | |
5c449fde | 67 | } |
85ce9d3e | 68 | |
5c449fde | 69 | Player::Player(string n) |
70 | { | |
37ed80a9 | 71 | inventory = new pouch(); |
72 | reset(); | |
73 | name = n; | |
74 | #ifdef DEBUGMODE | |
75 | log("New Player: %s", name.c_str()); | |
76 | #endif | |
85ce9d3e | 77 | } |
78 | ||
79 | Player::~Player() | |
f156d166 | 80 | { |
81 | if (inventory != NULL) | |
82 | delete inventory; | |
5de7a0b0 | 83 | |
f156d166 | 84 | delMonster(); |
85 | delMaster(); | |
86 | delBattle(); | |
37ed80a9 | 87 | #ifdef DEBUGMODE |
88 | string *output; | |
89 | output = new string("Deleting Player"); | |
37ed80a9 | 90 | *output += ": " + name; |
37ed80a9 | 91 | *output += " Password: " + password; |
92 | log ("%s", output->c_str()); | |
93 | delete output; | |
94 | #endif | |
e3c5fe46 | 95 | } |
5de7a0b0 | 96 | |
1563e73b | 97 | void Player::setPassword(const char *p) |
98 | { | |
37ed80a9 | 99 | static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUV\ |
1563e73b | 100 | WXYZ0123456789./"; |
37ed80a9 | 101 | static char salt[3]; |
102 | ||
103 | salt[0] = saltChars[rand() % strlen(saltChars)]; | |
104 | salt[1] = saltChars[rand() % strlen(saltChars)]; | |
105 | salt[2] = '\0'; | |
106 | ||
107 | password = crypt(p, salt); | |
1563e73b | 108 | } |
85ce9d3e | 109 | |
53d5585b | 110 | void Player::setRawPassword(const char *p) |
111 | { | |
112 | password = p; | |
113 | } | |
114 | ||
85ce9d3e | 115 | void Player::setData(Player *right) |
116 | { | |
37ed80a9 | 117 | if (right) |
85ce9d3e | 118 | { |
37ed80a9 | 119 | name = right->name; |
120 | password = right->password; | |
121 | exp = right->exp; | |
122 | gold = right->gold; | |
123 | bank = right->bank; | |
124 | hp = right->hp; | |
125 | maxhp = right->maxhp; | |
126 | strength = right->strength; | |
127 | defense = right->defense; | |
37ed80a9 | 128 | level = right->level; |
129 | forest_fights = right->forest_fights; | |
130 | player_fights = right->player_fights; | |
131 | flags = right->flags; | |
132 | *inventory = *right->inventory; | |
133 | client = right->client; | |
134 | lastcommand = right->lastcommand; | |
135 | lastlogin = right->lastlogin; | |
1781f48a | 136 | w = right->w; |
137 | a = right->a; | |
85ce9d3e | 138 | } |
37ed80a9 | 139 | else |
85ce9d3e | 140 | { |
37ed80a9 | 141 | char *temp; |
142 | reset(); | |
143 | temp = new char[16]; | |
144 | client = NULL; | |
145 | int num = rand() % 32767; | |
146 | snprintf(temp, 16, "Player: %d", num); | |
147 | name = temp; | |
148 | delete []temp; | |
85ce9d3e | 149 | } |
37ed80a9 | 150 | |
151 | #ifdef DEBUGMODE | |
152 | log("Setting Player data for %s", name.c_str()); | |
153 | #endif | |
85ce9d3e | 154 | } |
c8ada07e | 155 | |
00e51ca8 | 156 | Monster::~Monster() |
c8ada07e | 157 | { |
c8ada07e | 158 | } |
159 | ||
00e51ca8 | 160 | Monster::Monster() |
c8ada07e | 161 | { |
37ed80a9 | 162 | name.erase(); |
163 | weapon.erase(); | |
164 | death.erase(); | |
165 | strength = 0; | |
166 | defense = 0; | |
167 | exp = 0; | |
168 | gold = 0; | |
169 | maxhp = hp = 0; | |
c8ada07e | 170 | } |
171 | ||
00e51ca8 | 172 | Monster::Monster(Monster *m) |
c8ada07e | 173 | { |
37ed80a9 | 174 | name = m->name; |
175 | weapon = m->weapon; | |
176 | death = m->death; | |
177 | strength = m->strength; | |
178 | defense = m->defense; | |
179 | maxhp = m->maxhp; | |
180 | hp = m->hp; | |
181 | gold = m->gold; | |
182 | exp = m->exp; | |
c8ada07e | 183 | } |
1af35752 | 184 | |
00e51ca8 | 185 | Monster::Monster(Monster &m) |
bf3a2ff9 | 186 | { |
37ed80a9 | 187 | name = m.name; |
188 | weapon = m.weapon; | |
189 | death = m.death; | |
190 | strength = m.strength; | |
191 | defense = m.defense; | |
192 | maxhp = m.maxhp; | |
193 | hp = m.hp; | |
194 | gold = m.gold; | |
195 | exp = m.exp; | |
bf3a2ff9 | 196 | } |
197 | ||
c10b78ac | 198 | Monster::Monster(const Monster &m) |
199 | { | |
200 | name = m.name; | |
201 | weapon = m.weapon; | |
202 | death = m.death; | |
203 | strength = m.strength; | |
204 | defense = m.defense; | |
205 | maxhp = m.maxhp; | |
206 | hp = m.hp; | |
207 | gold = m.gold; | |
208 | exp = m.exp; | |
209 | } | |
210 | ||
1af35752 | 211 | long int Player::setFlags(long int newflags) |
212 | { | |
37ed80a9 | 213 | flags = newflags; |
214 | return getFlags(); | |
1af35752 | 215 | } |
216 | ||
217 | long int Player::addFlag(long int flag) | |
218 | { | |
37ed80a9 | 219 | flags |= flag; |
220 | return getFlags(); | |
1af35752 | 221 | } |
222 | ||
223 | long int Player::remFlag(long int flag) | |
224 | { | |
37ed80a9 | 225 | flags &= ~flag; |
226 | return getFlags(); | |
3662210f | 227 | } |
1781f48a | 228 | |
229 | void Player::setWeapon(item &weap) | |
230 | { | |
231 | w = &weap; | |
232 | } | |
233 | void Player::setArmor(item &arm) | |
234 | { | |
235 | a = &arm; | |
236 | } | |
fbb87959 | 237 | |
238 | void Player::setName(string &n) | |
239 | { | |
240 | name = n; | |
241 | } | |
242 | ||
243 | void Player::setName(const char *n) | |
244 | { | |
245 | name = n; | |
246 | } | |
247 | ||
248 | void Player::setLevel(int l) | |
249 | { | |
250 | if (l > 0 && l <= REALLEVELS) | |
251 | { | |
252 | level = l; | |
253 | } | |
254 | } | |
255 | ||
256 | void Player::setExp(long int e) | |
257 | { | |
258 | if (e <= 2000000000 && e > 0) | |
259 | { | |
260 | exp = e; | |
261 | } | |
262 | } | |
263 | ||
264 | void Player::setGold(long int g) | |
265 | { | |
266 | if (g <= 2000000000 && g >= 0) | |
267 | { | |
268 | gold = g; | |
269 | } | |
270 | else if (g > 2000000000) | |
271 | { | |
272 | gold = 2000000000; | |
273 | } | |
274 | } | |
275 | ||
276 | void Player::setBank(long int b) | |
277 | { | |
278 | if (b <= 2000000000 && b >= 0) | |
279 | { | |
280 | bank = b; | |
281 | } | |
282 | else if (b > 2000000000) | |
283 | { | |
284 | bank = 2000000000; | |
285 | } | |
286 | } | |
287 | ||
288 | void Player::setHP(int h) | |
289 | { | |
290 | hp = h; | |
291 | ||
292 | if (hp < 0) | |
293 | { | |
294 | hp = 0; | |
295 | } | |
296 | else if (hp > maxhp) | |
297 | { | |
298 | hp = maxhp; // You can't go higher than your maxhp | |
299 | } | |
300 | } | |
301 | ||
302 | void Player::setMaxHP(int m) | |
303 | { | |
304 | maxhp = m; | |
305 | ||
306 | if (maxhp < 0) | |
307 | { | |
308 | maxhp = 10; | |
309 | } | |
310 | else if (maxhp > 32000) | |
311 | { | |
312 | maxhp = 32000; | |
313 | } | |
314 | } | |
315 | ||
316 | void Player::setStrength(int s) | |
317 | { | |
318 | strength = s; | |
319 | if (strength < 0) | |
320 | { | |
321 | strength = 1; | |
322 | } | |
323 | else if (strength > 32000) | |
324 | { | |
325 | strength = 32000; | |
326 | } | |
327 | } | |
328 | ||
329 | void Player::setDefense(int d) | |
330 | { | |
331 | defense = d; | |
332 | ||
333 | if (defense < 0) | |
334 | { | |
335 | defense = 1; | |
336 | } | |
337 | else if (defense > 32000) | |
338 | { | |
339 | defense = 32000; | |
340 | } | |
341 | } | |
342 | ||
343 | void Player::setForestFights(int ff) | |
344 | { | |
345 | forest_fights = ff; | |
346 | ||
347 | if (forest_fights < 0) | |
348 | { | |
349 | forest_fights = 0; | |
350 | } | |
351 | else if (forest_fights > 32000) | |
352 | { | |
353 | forest_fights = 32000; | |
354 | } | |
355 | } | |
356 | ||
357 | void Player::setPlayerFights(int pf) | |
358 | { | |
359 | player_fights = pf; | |
360 | ||
361 | if (player_fights < 0) | |
362 | { | |
363 | player_fights = 0; | |
364 | } | |
365 | else if (player_fights > 32000) | |
366 | { | |
367 | player_fights = 32000; | |
368 | } | |
369 | } | |
370 | ||
371 | void Player::setClient(aClient *c) | |
372 | { | |
373 | client = c; | |
374 | } | |
375 | ||
00e51ca8 | 376 | void Player::setMonster(Monster *m) |
fbb87959 | 377 | { |
378 | fight = new Monster(m); | |
379 | } | |
380 | ||
381 | void Player::setMyMaster(Monster *m) | |
382 | { | |
383 | master = new Monster(m); | |
384 | } | |
385 | ||
386 | void Player::setBattle(aClient *b) | |
387 | { | |
388 | battle = b; | |
389 | } | |
390 | ||
391 | void Player::addExp(int e) | |
392 | { | |
393 | setExp(getExp() + e); | |
394 | } | |
395 | ||
396 | void Player::addGold(int g) | |
397 | { | |
398 | setGold(getGold() + g); | |
399 | } | |
400 | ||
401 | void Player::addBank(int b) | |
402 | { | |
403 | setBank(getBank() + b); | |
404 | } | |
405 | ||
406 | void Player::addHP(int h) | |
407 | { | |
408 | setHP(getHP() + h); | |
409 | } | |
410 | ||
411 | void Player::addMaxHP(int m) | |
412 | { | |
413 | setMaxHP(getMaxHP() + m); | |
414 | } | |
415 | ||
416 | void Player::addStrength(int s) | |
417 | { | |
418 | setStrength(getStrength() + s); | |
419 | } | |
420 | ||
421 | void Player::addDefense(int d) | |
422 | { | |
423 | setDefense(getDefense() + d); | |
424 | } | |
425 | ||
426 | void Player::addForestFights(int ff) | |
427 | { | |
428 | setForestFights(getForestFights() + ff); | |
429 | } | |
430 | ||
431 | void Player::addPlayerFights(int pf) | |
432 | { | |
433 | setPlayerFights(getPlayerFights() + pf); | |
434 | } | |
435 | ||
436 | void Player::addLevel(int l) | |
437 | { | |
438 | setLevel(getLevel() + l); | |
439 | } | |
440 | ||
441 | void Player::subtractLevel(int l) | |
442 | { | |
443 | setLevel(getLevel() - l); | |
444 | } | |
445 | ||
446 | void Player::subtractExp(long int e) | |
447 | { | |
448 | setExp(getExp() - e); | |
449 | } | |
450 | ||
451 | void Player::subtractGold(long int g) | |
452 | { | |
453 | setGold(getGold() - g); | |
454 | } | |
455 | ||
456 | void Player::subtractBank(long int b) | |
457 | { | |
458 | setBank(getBank() - b); | |
459 | } | |
460 | ||
461 | void Player::subtractHP(int h) | |
462 | { | |
463 | setHP(getHP() - h); | |
464 | } | |
465 | ||
466 | void Player::subtractMaxHP(int m) | |
467 | { | |
468 | setMaxHP(getMaxHP() - m); | |
469 | } | |
470 | ||
471 | void Player::subtractStrength(int s) | |
472 | { | |
473 | setStrength(getStrength() - s); | |
474 | } | |
475 | ||
476 | void Player::subtractDefense(int d) | |
477 | { | |
478 | setDefense(getDefense() - d); | |
479 | } | |
480 | ||
481 | void Player::subtractForestFights(int ff) | |
482 | { | |
483 | setForestFights(getForestFights() - ff); | |
484 | } | |
485 | ||
486 | void Player::subtractPlayerFights(int pf) | |
487 | { | |
488 | setPlayerFights(getPlayerFights() - pf); | |
489 | } | |
490 | ||
491 | void Player::delMonster() | |
492 | { | |
493 | if (fight) | |
494 | delete fight; | |
495 | ||
496 | fight = NULL; | |
497 | } | |
498 | ||
499 | void Player::delMaster() | |
500 | { | |
501 | if (master) | |
502 | delete master; | |
503 | ||
504 | master = NULL; | |
505 | } | |
506 | ||
507 | void Player::delBattle() | |
508 | { | |
509 | // Don't delete the other player's aClient | |
510 | battle = NULL; | |
511 | } | |
512 | ||
71a1182a | 513 | bool Player::operator < (Player &right) |
e248e6be | 514 | { |
515 | if (getLevel() < right.getLevel()) | |
516 | { | |
517 | return true; | |
518 | } | |
519 | else if (getExp() < right.getExp()) | |
520 | { | |
521 | return true; | |
522 | } | |
523 | else if (getStrength() < right.getStrength() && getDefense() < right.getDefense()) | |
524 | { | |
525 | return true; | |
526 | } | |
527 | else | |
528 | { | |
529 | return false; | |
530 | } | |
531 | } |