]> jfr.im git - irc/gameservirc.git/blob - gameserv/gameserv.cpp
Implemented refresh period... now works and added to changelog
[irc/gameservirc.git] / gameserv / gameserv.cpp
1 #include "aClient.h"
2 #include "config.h"
3 #include "extern.h"
4 #include "flags.h"
5 #include "list.h"
6 #include "sockhelp.h"
7
8 #include <cctype>
9 #include <fstream>
10
11 using std::ifstream;
12 using std::ofstream;
13 using std::ios;
14
15 #if defined(HAVE_CRYPT_H)
16
17 #include <crypt.h>
18
19 #elif defined(HAVE_UNISTD_H)
20
21 #include <unistd.h>
22
23 #endif
24
25
26 Monster *monsters[LEVELS][MONSTERS]; // Monsters per level. Total = MONSTERS * LEVELS
27 Monster boss; // The boss monster
28
29 Monster *masters[LEVELS]; // A master for each level
30
31 // Database functions
32 int save_gs_dbase();
33 int load_gs_dbase();
34
35 // String functions
36 #ifndef HAVE_STRTOK
37 char *strtok(char *str, const char *delim);
38 #endif
39
40 int stricmp(const char *s1, const char *s2);
41 int strnicmp(const char *s1, const char *s2, size_t len);
42 // String Functions
43
44 /********** Password functions **********/
45
46 bool passcmp(char *encrypted, char *plaintext); // Compares an encrypted pass with a plain text one
47
48 bool check_password(char *name, char *plaintext); // Finds a password for the given name, and checks it with passcmp against the plaintext password given.
49
50 /********** Password functions **********/
51
52
53 /********** GameServ Booleans **********/
54
55 bool shuttingdown;
56 bool timedOut(Player *p);
57 void updateTS(Player *p);
58 void timeOutEvent(Player *p);
59
60 bool is_playing(char *u); // True if the given nickname in the clients list is playing.
61 bool is_playing(aClient *user);
62
63 bool is_fighting(char *u); // True if the given nick in the clients list is fighting anything.
64 bool is_fighting(aClient *user);
65
66 bool player_fight(char *u); // True if the player is fighting another player.
67 bool player_fight(aClient *user);
68
69 bool master_fight(char *u); // True if the player is fighting their master.
70 bool master_fight(aClient *user);
71
72 /********** GameServ Booleans **********/
73
74 void display_help(char *u, char *file = NULL);
75 void display_monster(char *u);
76 void display_players(char *u);
77 void display_players(aClient *user);
78 long int chartoint(char ch);
79 int isstringnum(char *num);
80 long int pow (int x, int y);
81 long int stringtoint(char *number);
82
83 char *spaces(int len, char *seperator);
84 void refresh(Player *p);
85 void refreshall();
86 void updateTS(Player *p);
87 void reset(Player *p);
88 void init_masters();
89 void init_monsters();
90 bool load_monsters();
91 void delete_monsters();
92 void delete_masters();
93
94 void do_admin(char *u);
95 void do_attack(char *u);
96 void do_bank(char *u);
97 void do_fight(char *u);
98 void do_heal(char *u);
99 void do_help(char *u);
100 void do_identify(char *u);
101 void do_inventory(char *u);
102 void do_refresh(char *u);
103 void do_register(char *u);
104 void do_list(char *u);
105 void do_logout(char *u);
106 void do_master(char *u);
107 void do_dragon(char *u);
108 void do_play(char *u);
109 void do_quitg(char *u);
110 void do_reset(char *u);
111 void do_run(char *u);
112 void do_stats(char *u);
113 void do_store(char *u);
114 void do_tavern(char *u);
115 void do_use(char *u);
116 void see_master(char *u);
117
118 void logout(aClient *user);
119 void showstats(const char *u, const char *nick);
120 void showinventory(aClient *from, aClient *to);
121 void showBankBalance(const char *u);
122 void end_turn(aClient *user);
123
124 #define WNA 16
125 char *weapons[WNA] = { "Fists", "Stick", "Dagger", "Quarterstaff", "Short Sword",
126 "Long Sword", "Silver Spear", "Battle Axe", "The Ragnarok",
127 "Chain Saw", "Poison Sword", "Flame Sword", "Earth Hammer",
128 "Light Saber", "Masamune", "Mystical Sword"};
129
130 char *armors[WNA] = { "Birthday Suit", "Clothes", "Leather Vest", "Chain Mail", "Plate Armor",
131 "Full Body Armor", "Magic Mail", "Graphite Suit", "Steel Suit",
132 "Force Field", "Armor of Light", "Mythril Vest", "DemiGod Armor",
133 "Hades' Cloak", "Dragon Scales", "Adamantium"};
134
135 int prices[WNA - 1] = {200, 1000, 3000, 10000, 30000, 100000, 150000, 200000, 400000,
136 1000000, 4000000, 10000000, 40000000, 100000000, 400000000};
137 int webonus[WNA] = {2, 10, 15, 25, 35, 45, 65, 85, 125, 185, 255, 355, 505, 805, 1205, 1805};
138 int arbonus[WNA] = {2, 3, 5, 10, 15, 25, 35, 50, 75, 100, 150, 225, 300, 400, 600, 1000};
139
140 int hpbonus[11] = {10, 15, 20, 30, 50, 75, 125, 185, 250, 350, 550};
141 int strbonus[11] = {5, 7, 10, 12, 20, 35, 50, 75, 110, 150, 200};
142 int defbonus[11] = {2, 3, 5, 10, 15, 22, 35, 60, 80, 120, 150};
143
144 void gameserv(char *source, char *buf)
145 {
146 char *cmd, z;
147 cmd = strtok(buf, " ");
148
149 #ifndef P10
150 source++; // Get rid of that : at the beginning of a :Nick privmsg Gameserv :text
151 #endif
152
153 z = cmd[0];
154 if (z == ':')
155 cmd++; // Get rid of that : at the beginning of the :text (command)
156
157 #ifdef DEBUGMODE
158 log("Source: %s Command: %s", source, cmd);
159 #endif
160
161 if (strnicmp(cmd, "\1PING", 6) == 0)
162 {
163 char *ts;
164 ts = strtok(NULL, "\1");
165 notice(s_GameServ, source, "\1PING %s\1", ts);
166 } else if (stricmp(cmd, "\1VERSION\1") == 0) {
167 notice(s_GameServ, source, "\1VERSION %s %s\1", PACKAGE, VERSION);
168 } else if (stricmp(cmd, "SEARCH") == 0) {
169 cmd = strtok(NULL, " ");
170
171 if (!cmd)
172 notice(s_GameServ, source, "SYNTAX: /msg %S SEARCH FOREST");
173 else
174 do_forest(source);
175
176 } else if (stricmp(cmd, "FIGHT") == 0) {
177 do_fight(source);
178 } else if (stricmp(cmd, "ATTACK") == 0) {
179 do_attack(source);
180 } else if (stricmp(cmd, "RUN") == 0) {
181 do_run(source);
182 } else if (stricmp(cmd, "USE") == 0) {
183 do_use(source);
184 } else if (stricmp(cmd, "HEAL") == 0) {
185 do_heal(source);
186 } else if (stricmp(cmd, "INVENTORY") == 0) {
187 do_inventory(source);
188 } else if (stricmp(cmd, "MASTER") == 0) {
189 do_master(source);
190 } else if (stricmp(cmd, "DRAGON") == 0) {
191 do_dragon(source);
192 } else if (stricmp(cmd, "STORE") == 0) {
193 do_store(source);
194 } else if (stricmp(cmd, "BANK") == 0) {
195 do_bank(source);
196 } else if (stricmp(cmd, "ADMIN") == 0) {
197 do_admin(source);
198 } else if (stricmp(cmd, "REFRESH") == 0) {
199 do_refresh(source);
200 } else if (stricmp(cmd, "RESET") == 0) {
201 do_reset(source);
202 } else if (stricmp(cmd, "TAVERN") == 0) {
203 do_tavern(source);
204 } else if (stricmp(cmd, "LIST") == 0) {
205 do_list(source);
206 } else if (stricmp(cmd, "LOGOUT") == 0) {
207 do_logout(source);
208 } else if (stricmp(cmd, "NEWS") == 0) {
209 do_news(source);
210 } else if (stricmp(cmd, "REGISTER") == 0) {
211 do_register(source);
212 } else if (stricmp(cmd, "IDENTIFY") == 0) {
213 do_identify(source);
214 } else if (stricmp(cmd, "HELP") == 0) {
215 do_help(source);
216 } else if (stricmp(cmd, "STATS") == 0) {
217 do_stats(source);
218 } else if (stricmp(cmd, "SHUTDOWN") == 0) {
219 aClient *user;
220
221 if (!(user = find(source)))
222 {
223 notice(s_GameServ, source, "Error: aClient not found. Contact a %S admin");
224 log("Error: aClient not found: %s", source);
225 }
226 else if (!isAdmin(user))
227 {
228 notice(s_GameServ, source, "You must be a %S admin to use this command!");
229 }
230 else
231 {
232 save_gs_dbase();
233 #ifdef P10
234 raw("[] SQ %s 0 :leaving: %s used the Shutdown command.", servername, user->getRealNick());
235 #else
236 raw("SQUIT %s :leaving: %s used the Shutdown command.", servername, source);
237 #endif
238 shuttingdown = true;
239 }
240 } else if (stricmp(cmd, "SAVE") == 0) {
241 aClient *user;
242
243 if (!(user = find(source)))
244 {
245 notice(s_GameServ, source, "Error: aClient not found. Contact a %S admin");
246 log("Error: aClient not found: %s", source);
247 }
248 else if (!isAdmin(user))
249 {
250 notice(s_GameServ, source, "You must be a %S admin to use this command!");
251 }
252 else
253 {
254 save_gs_dbase();
255 }
256 } else if (stricmp(cmd, "LOAD") == 0) {
257 aClient *user;
258
259 if (!(user = find(source)))
260 {
261 notice(s_GameServ, source, "Error: aClient not found. Contact a %S admin");
262 log("Error: aClient not found: %s", source);
263 }
264 else if (!isAdmin(user))
265 {
266 notice(s_GameServ, source, "You must be a %S admin to use this command!");
267 }
268 else
269 {
270 char *cmd2 = strtok(NULL, " ");
271 if (!cmd2)
272 {
273 notice(s_GameServ, source, "Loading player data from %s", playerdata);
274 load_gs_dbase();
275 }
276 else if (stricmp(cmd2, "MONSTERS") == 0)
277 {
278 notice(s_GameServ, source, "Loading monster data from %s", monsterdata);
279 load_monsters();
280 }
281 else
282 display_help(source, cmd);
283 }
284 #ifdef DEBUGMODE
285 } else if (stricmp(cmd, "RAW") == 0) {
286 aClient *user;
287
288 if (!(user = find(source)))
289 {
290 notice(s_GameServ, source, "Error: aClient not found. Contact a %S admin");
291 log("Error: aClient not found: %s", source);
292 }
293 else if (!isAdmin(user))
294 {
295 notice(s_GameServ, source, "You must be a %S admin to use this command!");
296 }
297 else
298 {
299 char *rest = strtok(NULL, "");
300 raw("%s", rest);
301 }
302 #endif
303 } else {
304 aClient *user;
305 if ((user = find(source)))
306 {
307 if (isIgnore(user))
308 {
309 #ifdef DEBUGMODE
310 log("Ignoring %s.", user->getNick());
311 #endif
312 }
313 else
314 {
315 notice(s_GameServ, source, "Unknown command \002%s\002. Type /msg %S \002HELP\002 to get a list of commands.", cmd);
316 }
317 }
318 }
319
320 #ifndef P10
321 source--; // Bring the ':' back so we don't leak memory
322 #endif
323 if (z == ':')
324 cmd--; // Same thing :)
325 }
326
327 int stricmp(const char *s1, const char *s2)
328 {
329 register int c;
330
331 while ((c = tolower(*s1)) == tolower(*s2)) {
332 if (c == 0)
333 return 0;
334 s1++;
335 s2++;
336 }
337 if (c < tolower(*s2))
338 return -1;
339 return 1;
340 }
341
342 void showstats(const char *u, const char *nick)
343 {
344 aClient *ni, *sender = find(u);
345 char *buf;
346 buf = new char[50];
347 char *space;
348
349
350 if (!(ni = findplayer(nick)))
351 {
352 notice(s_GameServ, u, "%s not found", nick);
353 }
354 else if (ni->stats)
355 {
356 notice(s_GameServ, sender->getNick(), "Stats for %s:", ni->stats->name);
357
358 sprintf(buf, "Experience: %ld", ni->stats->exp);
359 space = spaces(strlen(buf), " ");
360 notice(s_GameServ, sender->getNick(), "%s%sLevel: %d", buf, space,
361 ni->stats->level);
362 delete [] space;
363
364 sprintf(buf, "Gold: %ld", ni->stats->gold);
365 space = spaces(strlen(buf), " ");
366 notice(s_GameServ, sender->getNick(), "%s%sGold in Bank: %ld", buf, space, ni->stats->bank);
367 delete [] space;
368
369 notice(s_GameServ, sender->getNick(), "Hit Points: %d of %d", ni->stats->hp,
370 ni->stats->maxhp);
371
372 sprintf(buf, "Strength: %d", ni->stats->strength + webonus[ni->stats->weapon]);
373 space = spaces(strlen(buf), " ");
374 notice(s_GameServ, sender->getNick(), "%s%sDefense: %d",
375 buf, space, ni->stats->defense + arbonus[ni->stats->armor]);
376 delete [] space;
377
378 sprintf(buf, "Armor: %s", armors[ni->stats->armor]);
379 space = spaces(strlen(buf), " ");
380 notice(s_GameServ, sender->getNick(), "%s%sWeapon: %s", buf, space,
381 weapons[ni->stats->weapon]);
382 delete [] space;
383
384 sprintf(buf, "Forest Fights: %d", ni->stats->forest_fights);
385 space = spaces(strlen(buf), " ");
386 notice(s_GameServ, sender->getNick(), "%s%sPlayer Fights: %d", buf, space, ni->stats->player_fights);
387 delete [] space;
388 Pouch *inv = &ni->stats->inventory;
389
390 notice(s_GameServ, u, "Potions");
391 sprintf(buf, "Healing: %d", inv->Healing());
392 space = spaces(strlen(buf), " ");
393 notice(s_GameServ, sender->getNick(), "%s%sHP: %d", buf,
394 space, inv->HP());
395 delete [] space;
396
397 sprintf(buf, "Strength: %d", inv->Strength());
398 space = spaces(strlen(buf), " ");
399 notice(s_GameServ, sender->getNick(), "%s%sDefense: %d", buf,
400 space, inv->Defense());
401 delete [] space;
402 }
403 else
404 {
405 notice(s_GameServ, u, "%s is not playing!", ni->stats->name);
406 }
407 delete [] buf;
408 }
409
410 char *spaces(int len, char *seperator)
411 {
412 char *final;
413 final = new char[30];
414 int y;
415 strcpy(final, seperator);
416 for (y = 0; y < 30 - len; y++)
417 strcat(final, seperator);
418 return final;
419 }
420
421 void raw(const char *fmt, ...)
422 {
423 va_list args;
424 char *input;
425 const char *t = fmt;
426 input = new char[1024];
427 va_start(args, fmt);
428 memset(input, 0, sizeof(input)); // Initialize to NULL
429 for (; *t; t++)
430 {
431 if (*t == '%')
432 {
433 switch(*++t) {
434 case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break;
435 case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break;
436 case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
437 case 'l':
438 if (*++t == 'd')
439 sprintf(input, "%s%ld", input, va_arg(args, long int)); break;
440 }
441 }
442 else
443 {
444 sprintf(input, "%s%c", input, *t);
445 }
446
447 }
448 #ifdef DEBUGMODE
449 log("Input: %s", input);
450 #endif
451
452 sprintf(input, "%s%s", input, "\r\n");
453 sock_puts(sock, input);
454 delete [] input;
455 va_end(args);
456 }
457 /* Send a NOTICE from the given source to the given nick. */
458
459 void notice(const char *source, const char *dest, const char *fmt, ...)
460 {
461 if (fmt[0] == '\0')
462 return;
463
464 char *commanduse;
465 commanduse = new char[16];
466
467 #ifdef P10
468 if (isUsePrivmsg())
469 strcpy(commanduse, "P");
470 else
471 strcpy(commanduse, "O");
472 #else
473
474 if (isUsePrivmsg())
475 strcpy(commanduse, "PRIVMSG");
476 else
477 strcpy(commanduse, "NOTICE");
478 #endif
479
480 va_list args;
481 char *input;
482 const char *t = fmt;
483 input = new char[1024];
484 va_start(args, fmt);
485 if (dest[0] == ':')
486 {
487 dest++;
488
489 #if !defined(P10)
490 sprintf(input, ":%s %s %s :", source, commanduse, dest);
491 #else
492 sprintf(input, "%s %s %s :", gsnum, commanduse, dest);
493 #endif
494
495 dest--;
496 }
497 else
498 {
499 #if !defined(P10)
500 sprintf(input, ":%s %s %s :", source, commanduse, dest);
501 #else
502 sprintf(input, "%s %s %s :", gsnum, commanduse, dest);
503 #endif
504 }
505
506 for (; *t; t++)
507 {
508 if (*t == '%')
509 {
510 switch(*++t) {
511 case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break;
512 case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break;
513 case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
514 case 'l':
515 if (*++t == 'd')
516 sprintf(input, "%s%ld", input, va_arg(args, long int)); break;
517 }
518 }
519 else
520 {
521 sprintf(input, "%s%c", input, *t);
522 }
523
524 }
525 #ifdef DEBUGMODE
526 log("Input: %s", input);
527 #endif
528 sprintf(input, "%s%s", input, "\r\n");
529 sock_puts(sock, input);
530 delete [] commanduse;
531 delete [] input;
532 va_end(args);
533 }
534
535
536 int strnicmp(const char *s1, const char *s2, size_t len)
537 {
538 register int c;
539
540 if (!len)
541 return 0;
542 while ((c = tolower(*s1)) == tolower(*s2) && len > 0) {
543 if (c == 0 || --len == 0)
544 return 0;
545 s1++;
546 s2++;
547 }
548 if (c < tolower(*s2))
549 return -1;
550 return 1;
551 }
552
553 #ifndef HAVE_STRTOK
554 char *strtok(char *str, const char *delim)
555 {
556 static char *current = NULL;
557 char *ret;
558
559 if (str)
560 current = str;
561 if (!current)
562 return NULL;
563 current += strspn(current, delim);
564 ret = *current ? current : NULL;
565 current += strcspn(current, delim);
566 if (!*current)
567 current = NULL;
568 else
569 *current++ = 0;
570 return ret;
571 }
572 #endif
573
574 void do_list(char *u)
575 {
576 aClient *user;
577 char *cmd = strtok(NULL, " ");
578
579 if (!(user = find(u)))
580 {
581 log("Fatal Error: Couldn't find %s in the client list", u);
582 return;
583 }
584 else if (isIgnore(user))
585 {
586 #ifdef DEBUGMODE
587 log("Ignoring %s. Command LIST", user->getNick());
588 #endif
589 return;
590 }
591
592 ListNode<aClient> *temp;
593 bool header = false;
594
595 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
596 {
597 temp = players[x].First();
598 if (!players[x].isEmpty())
599 {
600 while(temp)
601 {
602 if (!cmd || is_playing(temp->getData()))
603 {
604 if (!header)
605 {
606 notice(s_GameServ, u, "Players:");
607 header = true;
608 }
609 #ifdef P10
610 notice(s_GameServ, u, "IRC: %s Game: %s", temp->getData()->getRealNick(),
611 temp->getData()->stats->name);
612 #else
613 notice(s_GameServ, u, "IRC: %s Game: %s", temp->getData()->getNick(),
614 temp->getData()->stats->name);
615 #endif
616 }
617
618 temp = temp->Next();
619 }
620 }
621 }
622 if (!header)
623 notice(s_GameServ, u, "No one is playing");
624 else
625 notice(s_GameServ, u, "End of List");
626
627 }
628
629 void do_logout(char *u)
630 {
631 aClient *user;
632 char *name = strtok(NULL, " ");
633
634 if (!(user = find(u)))
635 {
636 notice(s_GameServ, u, "Fatal error. Cannot find aClient. "\
637 "Buf: %s LOGOUT", u);
638 log("Could not find aClient Buf: %s LOGOUT",
639 u);
640 return;
641 }
642 else if (isIgnore(user))
643 {
644 #ifdef DEBUGMODE
645 log("Ignoring %s.", user->getNick());
646 #endif
647 return;
648 }
649
650 if (name)
651 {
652 if (!isAdmin(user))
653 {
654 notice(s_GameServ, u, "You must be a %S admin to use this command!");
655 }
656 else if (!(user = findplayer(name)))
657 {
658 notice(s_GameServ, u, "Couldn't find a player named %s", name);
659 }
660 else
661 {
662 notice(s_GameServ, u, "Logging out %s", user->stats->name);
663 logout(user);
664 }
665 }
666 else if (!name)
667 {
668 if (!is_playing(user))
669 {
670 notice(s_GameServ, u, "You're not logged in!");
671 }
672 else if (is_fighting(user))
673 {
674 notice(s_GameServ, u, "You can't logout while fighting!");
675 }
676 else
677 {
678 notice(s_GameServ, u, "You have left the fields. You have lived to kill another day!");
679 logout(user);
680 }
681 }
682 }
683
684 void logout(aClient *user)
685 {
686 if (is_playing(user))
687 {
688 ListNode<aClient> *it;
689 aClient *temp;
690 unsigned long hv = iHASH((unsigned char *) user->stats->name);
691 it = players[hv].Find(user);
692
693 if (!it)
694 {
695 notice(s_GameServ, user->getNick(), "Fatal error. Contact "\
696 "%S Admin. Cannot find you in the players list.");
697 log("Error on logout(). Can't find %s in the players list",
698 #ifdef P10
699 user->getRealNick()
700 #else
701 user->getNick()
702 #endif
703 );
704 return;
705 }
706
707 temp = new aClient;
708 temp->stats = new Player;
709 temp->stats->setData(user->stats);
710 user->stats->client = NULL;
711
712 if (player_fight(user))
713 user->stats->battle->stats->battle = NULL;
714
715 delete user->stats;
716 user->stats = NULL;
717 temp->stats->client = NULL;
718 #ifdef P10
719 temp->setRealNick("Not Playing");
720 #endif
721 temp->setNick("Not Playing");
722
723 it->setNewPtr(temp);
724 #ifdef DEBUGMODE
725 log("Logged out player %s",
726 #ifdef P10
727 user->getRealNick()
728 #else
729 user->getNick()
730 #endif
731 );
732 #endif
733 }
734 clearPlaying(user);
735 }
736
737 void do_register(char *u)
738 {
739 char *password, *name;
740 aClient *user;
741 name = strtok(NULL, " ");
742 password = strtok(NULL, " ");
743
744 static char saltChars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
745 static char salt[3];
746
747 salt[0] = saltChars[rand() % strlen(saltChars)];
748 salt[1] = saltChars[rand() % strlen(saltChars)];
749 salt[2] = '\0';
750
751 if (!name)
752 {
753 notice(s_GameServ, u, "SYNTAX: /msg %S REGISTER NAME PASSWORD");
754 }
755 else if (!password)
756 {
757 notice(s_GameServ, u, "SYNTAX: /msg %S REGISTER NAME PASSWORD");
758 }
759 else if ((user = findplayer(name)))
760 {
761 notice(s_GameServ, u, "%s is already registered!", name);
762 notice(s_GameServ, u, "Choose another name!");
763 }
764 else if (!(user = find(u)))
765 {
766 log("Fatal Error: Couldn't find %s in the clients list", u);
767 }
768 else if (isIgnore(user))
769 {
770 #ifdef DEBUGMODE
771 log("Ignoring %s.", user->getNick());
772 #endif
773 return;
774 }
775 else
776 {
777 if (!is_playing(user))
778 {
779 ListNode<aClient> *temp;
780 user->stats = new Player(user);
781 user->stats->client = user; // Set the backwards pointer
782 user->stats->reset(); // set the user up
783 strncpy(user->stats->password, crypt(password, salt), 255);
784 strncpy(user->stats->name, name, 255);
785 unsigned long hv = iHASH((unsigned char *) name);
786 updateTS(user->stats);
787 temp = players[hv].insertAtBack_RLN(user);
788 temp->setPtr(user); // This is an extra step, but necessary for now
789
790 notice(s_GameServ, u, "Player %s registered with password %s.", user->stats->name, password);
791 notice(s_GameServ, u, "Write this password down. If you lose it, there is no way to retrieve it!");
792 log("Nickname %s registered player %s.", u, user->stats->name);
793 setPlaying(user); // set the playing flag
794 }
795 else
796 {
797 notice(s_GameServ, u, "Already registered. Contact a %S admin for help.");
798 }
799 }
800 }
801
802 void do_identify(char *u)
803 {
804 char *password, *name;
805 aClient *user, *p;
806 name = strtok(NULL, " ");
807 password = strtok(NULL, " ");
808 if (!password || !name)
809 {
810 notice(s_GameServ, u, "SYNTAX: /msg %S IDENTIFY NAME PASSWORD");
811 }
812 else if (!(user = find(u)))
813 {
814 notice(s_GameServ, u, "Fatal error. Cannot find aClient. Buf: %s", strtok(NULL, ""));
815 log("Error: aClient not found: %s", u);
816 }
817 else if (isIgnore(user))
818 {
819 #ifdef DEBUGMODE
820 log("Ignoring %s.", user->getNick());
821 #endif
822 return;
823 }
824 else if (!(p = findplayer(name)) || !p->stats)
825 notice(s_GameServ, u, "Player %s not found", name);
826 else if (is_playing(user))
827 {
828 notice(s_GameServ, u, "You are already playing!");
829 }
830 else if (p->stats->client != NULL && !isAdmin(user))
831 {
832 notice(s_GameServ, u, "That player has already identified.");
833 }
834 else if (!check_password(name, password) && !isAdmin(user))
835 {
836 notice(s_GameServ, u, "Password incorrect");
837 }
838 else {
839 ListNode<aClient> *temp;
840 unsigned long hv = iHASH((unsigned char *) p->stats->name);
841 temp = players[hv].Find(p);
842 if (!temp)
843 {
844 notice(s_GameServ, u, "Fatal error. Contact %S Admin. Buf: %s",
845 strtok(NULL, ""));
846 return;
847 }
848 user->stats = new Player(p->stats->name);
849 #ifdef DEBUGMODE
850 log("Setting data for identified");
851 #endif
852 user->stats->setData(p->stats);
853 user->stats->client = user;
854 updateTS(user->stats);
855
856
857 #ifdef DEBUGMODE
858 log("Player %s IRC: %s Identified", user->stats->name,
859 user->getNick());
860 #endif
861
862 setPlaying(user); // set the playing flag
863
864 temp->setPtr(user);
865 notice(s_GameServ, u, "Password Accepted. Identified.");
866 showNews(u, todaysnews);
867 }
868 }
869
870 void do_stats(char *u)
871 {
872 char *nick;
873 aClient *user;
874
875 nick = strtok(NULL, " ");
876
877 if (!(user = find(u)))
878 {
879 log("Fatal Error: %s not found in client list", u);
880 return;
881 }
882 else if (isIgnore(user))
883 {
884 #ifdef DEBUGMODE
885 log("Ignoring %s.", user->getNick());
886 #endif
887 return;
888 }
889 else if (!nick)
890 {
891 if (!is_playing(user))
892 {
893 notice(s_GameServ, u, "You're not playing, so you have no stats!");
894 return;
895 }
896 else
897 {
898 updateTS(user->stats);
899 showstats(u, user->stats->name);
900 }
901 }
902 else
903 showstats(u, nick);
904 }
905
906 void init_masters()
907 {
908 #ifdef DEBUGMODE
909 log("Calling delete_masters()");
910 #endif
911
912 delete_masters();
913
914 #ifdef DEBUGMODE
915 log("Initializing masters");
916 #endif
917
918 for (int x = 0; x < LEVELS; x++)
919 masters[x] = new Monster;
920
921 strcpy(masters[0]->name, "Old Bones");
922 strcpy(masters[0]->weapon, "Dull Sword Cane");
923 masters[0]->strength = 32;
924 masters[0]->gold = 0;
925 masters[0]->exp = 0;
926 masters[0]->maxhp = 35;
927 masters[0]->hp = 35;
928 strcpy(masters[0]->death, "You have done well my student, but the road is long. Use your new strength with humility and honor as you progress in levels!");
929
930 strcpy(masters[1]->name, "Master Chang");
931 strcpy(masters[1]->weapon, "Nanchaku");
932 masters[1]->strength = 48;
933 masters[1]->gold = 0;
934 masters[1]->exp = 0;
935 masters[1]->maxhp = 51;
936 masters[1]->hp = 51;
937 strcpy(masters[1]->death, "You try to make out what Master Chang is saying, but the only thing you catch is something about a grasshopper.");
938
939 strcpy(masters[2]->name, "Chuck Norris");
940 strcpy(masters[2]->weapon, "Ranger Kick");
941 masters[2]->strength = 88;
942 masters[2]->gold = 0;
943 masters[2]->exp = 0;
944 masters[2]->maxhp = 100;
945 masters[2]->hp = 100;
946 strcpy(masters[2]->death, "Be strong, and keep your goals in site. Drink milk, and don't do drugs. One day you may be fighting next to me as a Texas Ranger YEEHAW!");
947
948
949 strcpy(masters[3]->name, "Mr. Miagi");
950 strcpy(masters[3]->weapon, "Petrified Bonsai");
951 masters[3]->strength = 169;
952 masters[3]->gold = 0;
953 masters[3]->exp = 0;
954 masters[3]->maxhp = 165;
955 masters[3]->hp = 165;
956 strcpy(masters[3]->death, "Skill comes from repeating the correct but seemingly mundane actions. Wax ON, wax OFF!");
957
958 strcpy(masters[4]->name, "Jackie Chan");
959 strcpy(masters[4]->weapon, "Kung Fu Kick");
960 masters[4]->strength = 275;
961 masters[4]->gold = 0;
962 masters[4]->exp = 0;
963 masters[4]->maxhp = 232;
964 masters[4]->hp = 232;
965 strcpy(masters[4]->death, "I like to let people talk who like to talk... it's easier to find out how full of it they really are!");
966
967 strcpy(masters[5]->name, "Jet Li");
968 strcpy(masters[5]->weapon, "Motorcycle");
969 masters[5]->strength = 347;
970 masters[5]->gold = 0;
971 masters[5]->exp = 0;
972 masters[5]->maxhp = 504;
973 masters[5]->hp = 504;
974 strcpy(masters[5]->death, "Failure is a fuel for excuses. It's the doing the do, that makes the making.");
975
976
977 strcpy(masters[6]->name, "Muhammad Ali");
978 strcpy(masters[6]->weapon, "Quick Jab");
979 masters[6]->strength = 515;
980 masters[6]->gold = 0;
981 masters[6]->exp = 0;
982 masters[6]->maxhp = 1078;
983 masters[6]->hp = 1078;
984 strcpy(masters[6]->death, "It's just a job. Grass grows, birds fly, waves pound the sand. I beat people up.");
985
986 strcpy(masters[7]->name, "Li Mu Bai");
987 strcpy(masters[7]->weapon, "Green Destiny");
988 masters[7]->strength = 655;
989 masters[7]->gold = 0;
990 masters[7]->exp = 0;
991 masters[7]->maxhp = 2207;
992 masters[7]->hp = 2207;
993 strcpy(masters[7]->death, "No growth without resistance. No action without reaction. No desire without restraint.");
994
995
996 strcpy(masters[8]->name, "Jimmy Wang Yu");
997 strcpy(masters[8]->weapon, "Flying Guillotine");
998 masters[8]->strength = 819;
999 masters[8]->gold = 0;
1000 masters[8]->exp = 0;
1001 masters[8]->maxhp = 2780;
1002 masters[8]->hp = 2780;
1003 strcpy(masters[8]->death, "You have beaten the one armed boxer. Proceed with caution!");
1004
1005 strcpy(masters[9]->name, "Wong Fei Hung");
1006 strcpy(masters[9]->weapon, "Drunken Boxing");
1007 masters[9]->strength = 1014;
1008 masters[9]->gold = 0;
1009 masters[9]->exp = 0;
1010 masters[9]->maxhp = 3046;
1011 masters[9]->hp = 3046;
1012 strcpy(masters[9]->death, "Hiccup! Monkey drinks master's wine!");
1013
1014 strcpy(masters[10]->name, "Bruce Lee");
1015 strcpy(masters[10]->weapon, "Fists of fury");
1016 masters[10]->strength = 1286;
1017 masters[10]->gold = 0;
1018 masters[10]->exp = 0;
1019 masters[10]->maxhp = 3988;
1020 masters[10]->hp = 3988;
1021 strcpy(masters[10]->death, "You must learn to concentrate. It is like a finger pointing away to the moon... DONT concentrate on the finger, or you will miss all the heavenly glory.");
1022 }
1023
1024 void init_monsters()
1025 {
1026 #ifdef DEBUGMODE
1027 log("Calling delete_monsters");
1028 #endif
1029
1030 delete_monsters();
1031
1032 for (int x = 0; x < LEVELS; x++)
1033 for (int y = 0; y < MONSTERS; y++)
1034 monsters[x][y] = new Monster();
1035 }
1036
1037 void delete_monsters()
1038 {
1039 for (int x = 0; x < LEVELS; x++)
1040 for (int y = 0; y < MONSTERS; y++)
1041 if (monsters[x][y])
1042 delete monsters[x][y];
1043 }
1044
1045 void delete_masters()
1046 {
1047 for (int x = 0; x < LEVELS; x++)
1048 if (masters[x])
1049 delete masters[x];
1050 }
1051
1052 void display_monster(char *u)
1053 {
1054 if (is_playing(u))
1055 {
1056 aClient *user = find(u);
1057 Player *ni = user->stats;
1058
1059 notice(s_GameServ, u, "Your Hitpoints: \ 2%d\ 2", ni->hp);
1060 notice(s_GameServ, u, "%s's Hitpoints: \ 2%d\ 2", ni->fight->name, ni->fight->hp);
1061 notice(s_GameServ, u, "Here are your commands:");
1062 notice(s_GameServ, u, "/msg %S attack");
1063 notice(s_GameServ, u, "/msg %S run");
1064 notice(s_GameServ, u, "What will you do?");
1065 }
1066 }
1067
1068 void display_players(char *u)
1069 {
1070 aClient *user;
1071 if (!(user = find(u)))
1072 {
1073 log("Fatal error in display_players(): Couldn't find %s", u);
1074 }
1075 else
1076 display_players(user);
1077 }
1078
1079 void display_players(aClient *user)
1080 {
1081 char *u = user->getNick();
1082 if (is_playing(user) && player_fight(user))
1083 {
1084 aClient *battle = user->stats->battle;
1085 notice(s_GameServ, u, "Your Hitpoints: \ 2%d\ 2", user->stats->hp);
1086 notice(s_GameServ, u, "%s's Hitpoints: \ 2%d\ 2", battle->stats->name, battle->stats->hp);
1087 notice(s_GameServ, u, "Here are your commands:");
1088 notice(s_GameServ, u, "/msg %S attack");
1089 notice(s_GameServ, u, "/msg %S run");
1090 notice(s_GameServ, u, "What will you do?");
1091 }
1092 }
1093
1094
1095 bool is_playing(char *u)
1096 {
1097 aClient *user;
1098 if (!(user = find(u)))
1099 return false;
1100 else
1101 return is_playing(user);
1102 }
1103
1104 bool is_playing(aClient *user)
1105 {
1106 if (user->stats == NULL)
1107 {
1108 return false;
1109 }
1110 else if (user->stats->client == NULL)
1111 {
1112 return false;
1113 }
1114 else if (!FL_is_playing(user))
1115 {
1116 return false;
1117 }
1118 else
1119 return true;
1120 }
1121
1122 bool is_fighting(char *u)
1123 {
1124 aClient *user;
1125
1126 if (!(user = find(u)))
1127 return false;
1128 else
1129 return is_fighting(user);
1130 }
1131
1132 bool is_fighting(aClient *user)
1133 {
1134 if (!is_playing(user))
1135 return false;
1136 else
1137 return player_fight(user) || master_fight(user) || user->stats->fight != NULL;
1138 }
1139
1140 bool player_fight(char *u)
1141 {
1142 aClient *user;
1143
1144 if (!(user = find(u)))
1145 return false;
1146 else
1147 return player_fight(user);
1148 }
1149
1150 bool player_fight(aClient *user)
1151 {
1152 if (!is_playing(user))
1153 return false;
1154 else if (user->stats->battle != NULL)
1155 {
1156 return user->stats->battle->stats != NULL;
1157 }
1158 return false;
1159 }
1160
1161 bool master_fight(char *u)
1162 {
1163 aClient *user;
1164
1165 if (!(user = find(u)))
1166 return false;
1167 else
1168 return master_fight(user);
1169 }
1170
1171 bool master_fight(aClient *user)
1172 {
1173 if (!is_playing(user))
1174 return false;
1175 else
1176 return user->stats->master != NULL;
1177 }
1178
1179 void do_fight(char *u)
1180 {
1181 aClient *ni, *battle;
1182
1183 char *nick = strtok(NULL, " ");
1184
1185 if (!nick)
1186 {
1187 notice(s_GameServ, u, "SYNTAX: /msg %S FIGHT PLAYER");
1188 return;
1189 }
1190 else if (!(ni = find(u)))
1191 {
1192 notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
1193 return;
1194 }
1195 else if (isIgnore(ni))
1196 {
1197 #ifdef DEBUGMODE
1198 log("Ignoring %s.", ni->getNick());
1199 #endif
1200 return;
1201 }
1202 else if (!is_playing(ni))
1203 {
1204 notice(s_GameServ, u, "You are not playing!");
1205 return;
1206 }
1207
1208 updateTS(ni->stats);
1209
1210 if (ni->stats->player_fights <= 0)
1211 {
1212 ni->stats->player_fights = 0; // just to be safe
1213 notice(s_GameServ, u, "You are out of player fights for the "\
1214 "day. You have to wait until tomorrow!");
1215 }
1216 else if (!(battle = findplayer(nick)))
1217 {
1218 notice(s_GameServ, u, "Player %s not found!", nick);
1219 }
1220 else if (!isAlive(ni->stats))
1221 {
1222 notice(s_GameServ, u, "You are dead. Wait until tomorrow to fight others!");
1223 }
1224 else if (!is_playing(battle))
1225 {
1226 notice(s_GameServ, u, "You can't attack %s while they aren't playing!", nick);
1227 }
1228
1229 /* offline fighting not available yet
1230 else if (!(fight = finduser(nick)))
1231 {
1232 ni->stats->battle = battle;
1233 battle->battle = ni;
1234 setYourTurn(ni->stats);
1235 clearYourTurn(battle->stats);
1236
1237 notice(s_GameServ, u, "You decide to fight %s while they're "\
1238 "not in the realm!",
1239 battle->stats->name);
1240 display_players(u);
1241 }
1242 */
1243 else if (stricmp(ni->stats->name, battle->stats->name) == 0)
1244 {
1245 notice(s_GameServ, u, "Are you trying to commit suicide!?");
1246 }
1247 else if (!isAlive(battle->stats))
1248 {
1249 notice(s_GameServ, u, "They are dead. Cannot fight dead players!");
1250 }
1251 else if (player_fight(battle))
1252 {
1253 notice(s_GameServ, u, "%s is fighting %s already!", battle->stats->name, battle->stats->battle->stats->name);
1254 }
1255 else if (master_fight(battle))
1256 {
1257 notice(s_GameServ, u, "%s is fighting their master!", battle->stats->name);
1258 }
1259 else if (is_fighting(battle))
1260 {
1261 notice(s_GameServ, u, "%s is fighting %s already!", battle->stats->name, battle->stats->fight->name);
1262 }
1263 else if (ni->stats->level - battle->stats->level > maxbfightdistance)
1264 {
1265 // You can't fight someone below you by more than X level(s)
1266 // level 12 can fight level (12 - X) but not < (12 - X)
1267 notice(s_GameServ, u, "You may not fight %s. You're too strong!",
1268 battle->stats->name);
1269 }
1270 else if (battle->stats->level - ni->stats->level > maxafightdistance)
1271 {
1272 // You can't fight someone above you by more than X level(S)
1273 // level 1 can fight level (1 + X), but not > (1 + X)
1274 notice(s_GameServ, u, "%s, do you really have a death wish? Try the forest you "\
1275 "weakling!", ni->stats->name);
1276 }
1277 else
1278 {
1279 // Set your battle pointer to the other player
1280 ni->stats->battle = battle;
1281
1282 // Set the other player's battle pointer to you
1283 ni->stats->battle->stats->battle = ni;
1284
1285 // The initiator gets the first move (perhaps this should be 50/50)
1286 setYourTurn(ni->stats);
1287 clearYourTurn(battle->stats);
1288
1289 // Initiate Battle sequence!
1290 ni->stats->player_fights -= 1;
1291
1292 notice(s_GameServ, u, "You challenge %s to an online duel!", battle->stats->name);
1293 notice(s_GameServ, battle->getNick(), "%s has challenged you to an online duel!", ni->stats->name);
1294 notice(s_GameServ, battle->getNick(), "%s gets to go first "\
1295 "because they initiated!", ni->stats->name);
1296 notice(s_GameServ, battle->getNick(), "Please wait while %s decides what to do.", ni->stats->name);
1297 display_players(ni);
1298 }
1299 }
1300
1301 void do_use(char *u)
1302 {
1303 aClient *user;
1304 Pouch *p;
1305
1306 char *item = strtok(NULL, " ");
1307
1308 if (!item)
1309 {
1310 notice(s_GameServ, u, "SYNTAX: USE ITEM");
1311 notice(s_GameServ, u, "Type /msg %S HELP USE for more information.");
1312 return;
1313 }
1314 else if (!(user = find(u)))
1315 {
1316 notice(s_GameServ, u, "Fatal Error in do_use. Contact a(n) %S Admin");
1317 return;
1318 }
1319 else if (isIgnore(user))
1320 {
1321 #ifdef DEBUGMODE
1322 log("Ignoring %s.", user->getNick());
1323 #endif
1324 return;
1325 }
1326 else if (!is_playing(user))
1327 {
1328 notice(s_GameServ, u, "You must be playing to use items!");
1329 return;
1330 }
1331
1332 updateTS(user->stats);
1333
1334 p = &user->stats->inventory;
1335
1336 if (stricmp(item, "HEALING") == 0)
1337 {
1338 if (p->Healing() <= 0)
1339 {
1340 notice(s_GameServ, u, "You are out of Healing Potions!");
1341 return;
1342 }
1343 int oldhealing = user->stats->hp;
1344 user->stats->hp += (10 * user->stats->level) + (rand() % 10) * user->stats->level;
1345 if (user->stats->hp - user->stats->maxhp >= 100)
1346 {
1347 user->stats->hp = user->stats->maxhp + 100;
1348
1349 if (oldhealing >= (user->stats->maxhp + 100))
1350 {
1351 notice(s_GameServ, u, "You cannot hold anymore HP!");
1352 return;
1353 }
1354 }
1355
1356 notice(s_GameServ, u, "You hastiliy gulp down the flask of cool life-giving waters.");
1357 notice(s_GameServ, u, "Rejuvination spreads throughout your body.");
1358 notice(s_GameServ, u, "You gain %d HP!", user->stats->hp - oldhealing);
1359 p->decHealing();
1360 if (player_fight(user))
1361 {
1362 notice(s_GameServ, user->stats->battle->getNick(),
1363 "%s has used a healing potion!");
1364 }
1365 }
1366 else if (stricmp(item, "STRENGTH") == 0)
1367 {
1368 if (p->Strength() <= 0)
1369 {
1370 notice(s_GameServ, u, "You are out of Strength Potions!");
1371 return;
1372 }
1373 int oldstrength = user->stats->strength;
1374 notice(s_GameServ, u, "As you grip the flask containing pure power, you feel adrenaline coarse through your veins!");
1375 notice(s_GameServ, u, "In one swallow you drink the potion and feel your muscle fibers bulging andgrowing!");
1376 user->stats->strength += 1 + (rand() % 10 >= 8 ? 1 : 0); // 1-2
1377 notice(s_GameServ, u, "You gain %d Strength points!", user->stats->strength - oldstrength);
1378 p->decStrength();
1379 if (player_fight(user))
1380 {
1381 notice(s_GameServ, user->stats->battle->getNick(),
1382 "%s has used a strength potion!");
1383 }
1384 }
1385 else if (stricmp(item, "DEFENSE") == 0)
1386 {
1387 if (p->Defense() <= 0)
1388 {
1389 notice(s_GameServ, u, "You are out of Defense Potions!");
1390 return;
1391 }
1392 int olddefense = user->stats->defense;
1393 notice(s_GameServ, u, "You drink the foul tasting viscous liquid while pinching your nose in disgust.");
1394 notice(s_GameServ, u, "It tasted bad, but you feel like you are unbeatable!");
1395 user->stats->defense += 1 + (rand() % 10 >= 8 ? 1 : 0); // 1-2
1396 notice(s_GameServ, u, "You gain %d Defense points!", user->stats->defense - olddefense);
1397 p->decDefense();
1398 if (player_fight(user))
1399 {
1400 notice(s_GameServ, user->stats->battle->getNick(),
1401 "%s has used a defense potion!");
1402 }
1403 }
1404 else if (stricmp(item, "HP") == 0)
1405 {
1406 if (p->HP() <= 0)
1407 {
1408 notice(s_GameServ, u, "You are out of HP Potions!");
1409 return;
1410 }
1411 int oldHP = user->stats->maxhp;
1412 notice(s_GameServ, u, "You feel your life growing longer as you drink the green glowing liquid.");
1413 user->stats->maxhp += 2 +
1414 (rand() % 100 > 70 ? (rand() % 7) : (rand() % 2) );
1415
1416 notice(s_GameServ, u, "You gain %d Maximum hit points!", user->stats->maxhp - oldHP);
1417 p->decHP();
1418 if (player_fight(user))
1419 {
1420 notice(s_GameServ, user->stats->battle->getNick(),
1421 "%s has used a HP potion!");
1422 }
1423 }
1424 else
1425 {
1426 notice(s_GameServ, u, "SYNTAX: /msg %S USE {HEALING | STRENGTH | DEFENSE | HP}");
1427 return;
1428 }
1429
1430 end_turn(user); // If they're fighting, end their turn
1431 }
1432 void do_run(char *u)
1433 {
1434 aClient *user;
1435 Player *p, *p2 = NULL;
1436
1437 if (!(user = find(u)))
1438 {
1439 notice(s_GameServ, u, "Couldn't find you. Error. Contact a %S admin");
1440 return;
1441 }
1442 else if (isIgnore(user))
1443 {
1444 #ifdef DEBUGMODE
1445 log("Ignoring %s.", user->getNick());
1446 #endif
1447 return;
1448 }
1449 else if (!is_playing(user))
1450 {
1451 notice(s_GameServ, u, "You must be playing to run!");
1452 return;
1453 }
1454
1455 updateTS(user->stats);
1456 p = user->stats;
1457
1458 if (p->battle)
1459 p2 = p->battle->stats;
1460
1461 if (!is_fighting(user))
1462 notice(s_GameServ, u, "You run in place... try fighting next time.");
1463 else if (!player_fight(user) && !master_fight(user))
1464 {
1465 notice(s_GameServ, u, "You run away from \ 2%s\ 2 like a little baby!", p->fight->name);
1466 delete p->fight;
1467 p->fight = NULL;
1468 }
1469 else if (player_fight(user) && isYourTurn(p))
1470 {
1471 notice(s_GameServ, u, "You run away from \ 2%s\ 2 like a little baby!", p2->name);
1472 notice(s_GameServ, p->battle->getNick(), "\ 2%s\ 2 ran away from you like a little baby!", p->name);
1473 p2->battle = NULL;
1474 }
1475 else if (player_fight(user) && !isYourTurn(p))
1476 {
1477 notice(s_GameServ, u, "It is not your turn. Please wait until \ 2%s\ 2 decides what to do.", p2->name);
1478 }
1479 else if (master_fight(user))
1480 {
1481 notice(s_GameServ, u, "You cannot run from \ 2%s\ 2! FIGHT!", p->master->name);
1482 }
1483 p->battle = NULL;
1484 }
1485
1486 void end_turn(aClient *user)
1487 {
1488 char *nick, *u = user->getNick();
1489 Monster *fight;
1490 aClient *battle;
1491 int mhit;
1492
1493 nick = new char[strlen(user->getNick()) + 1];
1494
1495 if (!user || !is_playing(user) || !is_fighting(user))
1496 goto endturn;
1497
1498 if (!player_fight(user) && !master_fight(user))
1499 fight = user->stats->fight;
1500 else
1501 fight = user->stats->master;
1502 battle = user->stats->battle;
1503
1504 if (!player_fight(user))
1505 {
1506 // Opponent's Hit
1507 mhit = (fight->strength / 2) +
1508 (rand() % (fight->strength / 2) - (user->stats->defense +
1509 arbonus[user->stats->armor]));
1510 }
1511 else
1512 {
1513 // Opponent's Hit
1514 mhit = (((battle->stats->strength + webonus[battle->stats->weapon]) / 2) +
1515 (rand() % ((battle->stats->strength + webonus[battle->stats->weapon])) / 2) -
1516 (user->stats->defense + arbonus[user->stats->armor]));
1517 }
1518 if (!player_fight(user))
1519 {
1520
1521 if (mhit > 0)
1522 {
1523 notice(s_GameServ, u, "\1f%s\1f attacks with their \1f%s\1f for \ 2%d\ 2 damage!",
1524 fight->name, fight->weapon, mhit);
1525 }
1526 else if (mhit <= 0)
1527 notice(s_GameServ, u, "%s completely misses you!", fight->name);
1528
1529 if (mhit >= user->stats->hp)
1530 {
1531 if (!master_fight(user))
1532 {
1533 notice(s_GameServ, u, "You have been \ 2\1fkilled\1f\ 2 by %s!", fight->name);
1534 notice(s_GameServ, u, "You lose all gold on hand and lose 10 percent "\
1535 "of your experience!");
1536 user->stats->gold = 0;
1537 user->stats->exp -= (long int)(user->stats->exp * .10);
1538 user->stats->hp = 0;
1539 user->stats->fight = NULL;
1540 clearAlive(user->stats);
1541 goto endturn;
1542 }
1543 else
1544 {
1545 notice(s_GameServ, u, "%s has bested you! You will have to wait "\
1546 "until tomorrow to try again", user->stats->master->name);
1547 user->stats->fight = NULL;
1548 user->stats->master = NULL;
1549 goto endturn;
1550 }
1551 }
1552 else
1553 {
1554 if (mhit > 0)
1555 user->stats->hp -= mhit;
1556 display_monster(u);
1557 goto endturn;
1558 }
1559 }
1560 else
1561 {
1562 clearYourTurn(user->stats);
1563 setYourTurn(battle->stats);
1564 display_players(battle);
1565 }
1566 endturn:
1567 delete nick;
1568 }
1569
1570 void do_attack(char *u)
1571 {
1572 int hit, mhit;
1573 aClient *ni, *battle; // The player and perhaps the player they're fighting
1574 Monster *fight; // The monster they may be fighting
1575
1576 if (!(ni = find(u)))
1577 {
1578 notice(s_GameServ, u, "Fatal error in do_attack. Contact a(n) %S admin for help.");
1579 return;
1580 }
1581 else if (isIgnore(ni))
1582 {
1583 #ifdef DEBUGMODE
1584 log("Ignoring %s.", ni->getNick());
1585 #endif
1586 return;
1587 }
1588 else if (!is_playing(ni))
1589 {
1590 notice(s_GameServ, u, "You're not playing!");
1591 return;
1592 }
1593 else if (!is_fighting(ni))
1594 {
1595 notice(s_GameServ, u, "You're not in battle!");
1596 return;
1597 }
1598 else
1599 {
1600 if (!ni->stats->master) // This is not a master fight
1601 fight = ni->stats->fight; // Monster Could be NULL
1602 else // This IS a master fight
1603 fight = ni->stats->master; // Master Could be NULL
1604
1605 battle = ni->stats->battle; // Player Could be NULL
1606
1607 // One has to be !NULL based on the previous else if
1608 // We wouldn't be here if they were all NULL
1609 }
1610 updateTS(ni->stats);
1611
1612 if (!player_fight(ni))
1613 {
1614 // Player's Hit
1615 hit = ((ni->stats->strength + webonus[ni->stats->weapon]) / 2) +
1616 (rand() % ((ni->stats->strength + webonus[ni->stats->weapon]) / 2));
1617
1618 // Opponent's Hit
1619 mhit = (fight->strength / 2) +
1620 (rand() % (fight->strength / 2) - (ni->stats->defense +
1621 arbonus[ni->stats->armor]));
1622 }
1623 else
1624 {
1625 // Opponent's Hit
1626 mhit = (((battle->stats->strength + webonus[battle->stats->weapon]) / 2) +
1627 (rand() % ((battle->stats->strength + webonus[battle->stats->weapon])) / 2) -
1628 (ni->stats->defense + arbonus[ni->stats->armor]));
1629
1630 // Player's Hit
1631 hit = (((ni->stats->strength + webonus[ni->stats->weapon]) / 2) +
1632 (rand() % ((ni->stats->strength + webonus[ni->stats->weapon])) / 2) -
1633 (battle->stats->defense + arbonus[battle->stats->armor]));
1634 }
1635
1636 if (!player_fight(ni))
1637 {
1638 if (hit > 0)
1639 notice(s_GameServ, u, "You attack \1f%s\1f for \ 2%d\ 2 points!", fight->name, hit);
1640 else
1641 notice(s_GameServ, u, "You miss \1f%s\1f completely!", fight->name);
1642
1643 if (hit >= fight->hp)
1644 {
1645 if (master_fight(ni))
1646 {
1647 notice(s_GameServ, u, "You have bested %s!", fight->name);
1648 addNews(todaysnews, "%s has bested %s and moved "\
1649 "to level %d", ni->stats->name, fight->name,
1650 (ni->stats->level + 1));
1651 }
1652 else
1653 notice(s_GameServ, u, "You have killed \ 2%s\ 2!", fight->name);
1654
1655 notice(s_GameServ, u, "%s", fight->death);
1656 notice(s_GameServ, u, "You recieve \ 2%d\ 2 experience and \ 2%d\ 2 gold!",
1657 fight->exp, fight->gold);
1658
1659 // If your new experience (or gold) will be greater than 2 billion,
1660 // then set your exp to 2bil. (2 billion max)... otherwise add them.
1661 // This could be a problem with overflowing out of the sign bit.
1662 // Unsigned long int maybe? Leave it for now.
1663 ni->stats->exp = ( (ni->stats->exp + fight->exp) > 2000000000 ? 2000000000 :
1664 ni->stats->exp + fight->exp);
1665
1666 ni->stats->gold = (ni->stats->gold + fight->gold > 2000000000 ? 2000000000 :
1667 ni->stats->gold + fight->gold);
1668
1669
1670 if (master_fight(ni))
1671 {
1672 notice(s_GameServ, u, "You are now level %d!", ni->stats->level + 1);
1673 notice(s_GameServ, u, "You gain %d Strength, and %d Defense points!",
1674 strbonus[ni->stats->level - 1], defbonus[ni->stats->level - 1]);
1675
1676 // Increase your level
1677
1678 // Increase your maximum hit points
1679 ni->stats->maxhp += hpbonus[ni->stats->level - 1];
1680
1681 // Heal the player by setting hp to their max
1682 ni->stats->hp = ni->stats->maxhp;
1683
1684 // Add to your strength
1685 ni->stats->strength += strbonus[ni->stats->level - 1];
1686
1687 // Add to your defensive power
1688 ni->stats->defense += defbonus[ni->stats->level - 1];
1689
1690 ni->stats->level++;
1691
1692 // Clear the pointer for your master
1693 ni->stats->master = NULL;
1694 }
1695
1696 // They're dead so remove the pointer
1697 delete ni->stats->fight;
1698 ni->stats->fight = NULL;
1699 ni->stats->master = NULL;
1700
1701 return;
1702 }
1703 else
1704 {
1705 if (hit > 0)
1706 fight->hp -= hit;
1707 if (mhit > 0)
1708 {
1709 notice(s_GameServ, u, "\1f%s\1f attacks with their \1f%s\1f for \ 2%d\ 2 damage!",
1710 fight->name, fight->weapon, mhit);
1711 }
1712 else if (mhit <= 0)
1713 notice(s_GameServ, u, "%s completely misses you!", fight->name);
1714
1715 if (mhit >= ni->stats->hp)
1716 {
1717 if (!master_fight(ni))
1718 {
1719 notice(s_GameServ, u, "You have been \ 2\1fkilled\1f\ 2 by %s!", fight->name);
1720 notice(s_GameServ, u, "You lose all gold on hand and lose 10 percent "\
1721 "of your experience!");
1722 addNews(todaysnews, "%s has been killed by %s!",
1723 ni->stats->name, fight->name);
1724 ni->stats->gold = 0;
1725 ni->stats->exp -= (long int)(ni->stats->exp * .10);
1726 ni->stats->hp = 0;
1727 ni->stats->fight = NULL;
1728 clearAlive(ni->stats);
1729 return;
1730 }
1731 else
1732 {
1733 notice(s_GameServ, u, "%s has bested you! You will have to wait "\
1734 "until tomorrow to try again", ni->stats->master->name);
1735 addNews(todaysnews, "%s tried to best %s and failed!",
1736 ni->stats->name, fight->name);
1737 ni->stats->fight = NULL;
1738 ni->stats->master = NULL;
1739 return;
1740 }
1741 }
1742 else
1743 {
1744 if (mhit > 0)
1745 ni->stats->hp -= mhit;
1746 display_monster(u);
1747 return;
1748 }
1749 }
1750 }
1751 else if (player_fight(ni))
1752 {
1753 /* Offline fighting not available yet
1754 if (!(online = finduser(ni->stats->battle->nick)) || !nick_identified(online))
1755 {
1756 if (hit > 0)
1757 notice(s_GameServ, u, "You attack \1f%s\1f for \ 2%d\ 2 points!", battle->nick, hit);
1758 else
1759 notice(s_GameServ, u, "You miss \1f%s\1f completely!", battle->nick);
1760 if (hit >= battle->stats->hp)
1761 {
1762 notice(s_GameServ, u, "You have killed \ 2%s\ 2!", battle->nick);
1763 * notice(s_GameServ, u, "You recieve \ 2%d\ 2 experience and \ 2%ld\ 2 gold!",
1764 (long int)(battle->stats->exp * .10), battle->stats->gold);
1765 if (2000000000 - ni->stats->exp > (long int)(battle->stats->exp * .10))
1766 {
1767 ni->stats->exp += (long int)(battle->stats->exp * .10);
1768 battle->stats->exp -= (long int)(battle->stats->exp * .10);
1769 }
1770 * else
1771 {
1772 battle->stats->exp -= (long int)(battle->stats->exp * .10);
1773 ni->stats->exp = 2000000000;
1774 }
1775
1776 if (2000000000 - ni->stats->gold > battle->stats->gold)
1777 {
1778 * ni->stats->gold += battle->stats->gold;
1779 battle->stats->gold = 0;
1780 }
1781 else
1782 {
1783 battle->stats->gold = 2000000000 - ni->stats->gold;
1784 ni->stats->gold = 2000000000;
1785 }
1786 * ni->stats->battle->stats->alive = 0;
1787 ni->stats->battle->battle = NULL;
1788 ni->stats->battle = NULL;
1789 return;
1790 }
1791 else
1792 {
1793 if (hit > 0)
1794 * battle->stats->hp -= hit;
1795 if (mhit > 0)
1796 {
1797 notice(s_GameServ, u, "\1f%s\1f hits you with their \1f%s\1f for \ 2%d\ 2 damage!",
1798 battle->nick, weapons[battle->stats->weapon], mhit);
1799 }
1800 else if (mhit <= 0)
1801 notice(s_GameServ, u, "%s completely misses you!", battle->nick);
1802 *
1803 if (mhit >= ni->stats->hp)
1804 {
1805 notice(s_GameServ, u, "You have been \ 2\1fkilled\1f\ 2 by %s!", battle->nick);
1806 if (2000000000 - battle->stats->gold > ni->stats->gold)
1807 {
1808 notice(s_GameServ, u, "%s took all your gold!", battle->nick);
1809 battle->stats->gold += ni->stats->gold;
1810 * ni->stats->gold = 0;
1811 }
1812 else
1813 {
1814 notice(s_GameServ, u, "You're lucky, %s couldn't carry all your gold.",
1815 battle->nick);
1816 ni->stats->gold -= (2000000000 - battle->stats->gold);
1817 notice(s_GameServ, u, "You were left dead with %d gold.",
1818 * (long int)ni->stats->gold);
1819 battle->stats->gold = 2000000000;
1820 }
1821 ni->stats->battle->battle = NULL;
1822 ni->stats->battle = NULL;
1823 ni->stats->alive = 0;
1824 return;
1825 }
1826 * else
1827 {
1828 if (mhit > 0)
1829 ni->stats->hp -= mhit;
1830 display_players(u);
1831 return;
1832 }
1833 }
1834 }
1835 * end offline fighting */
1836
1837 if (is_playing(battle))
1838 {
1839 if (!isYourTurn(ni->stats))
1840 {
1841 notice(s_GameServ, u, "Please wait until %s decides what to do!",
1842 battle->stats->name);
1843 return;
1844 }
1845 if (hit > 0)
1846 {
1847 notice(s_GameServ, u, "You attack \1f%s\1f for \ 2%d\ 2 points!", battle->stats->name, hit);
1848
1849 notice(s_GameServ, battle->getNick(), "%s has hit you with their %s for "\
1850 "\ 2%d\ 2 damage!", ni->stats->name,
1851 weapons[ni->stats->weapon], hit);
1852 }
1853 else
1854 {
1855 notice(s_GameServ, u, "You miss \1f%s\1f completely!", battle->stats->name);
1856 notice(s_GameServ, battle->getNick(), "%s misses you completely!", ni->stats->name);
1857 }
1858
1859 if (hit >= battle->stats->hp)
1860 {
1861 notice(s_GameServ, u, "You have killed \ 2%s\ 2!", battle->stats->name);
1862 notice(s_GameServ, u, "You recieve \ 2%d\ 2 experience and \ 2%ld\ 2 gold!",
1863 (long int)(battle->stats->exp * .10), battle->stats->gold);
1864 notice(s_GameServ, battle->getNick(), "You have been killed by \ 2%s\ 2!",
1865 ni->stats->name);
1866 battle->stats->hp = 0;
1867 clearAlive(battle->stats);
1868
1869 if (2000000000 - ni->stats->exp > (long int)(battle->stats->exp * .10))
1870 {
1871 ni->stats->exp += (long int)(battle->stats->exp * .10);
1872 battle->stats->exp -= (long int)(battle->stats->exp * .10);
1873 }
1874 else
1875 {
1876 battle->stats->exp -= (long int)(battle->stats->exp * .10);
1877 ni->stats->exp = 2000000000;
1878 }
1879
1880 if (2000000000 - ni->stats->gold > battle->stats->gold)
1881 {
1882 notice(s_GameServ, battle->getNick(), "You lose ten percent of experience and "\
1883 "all gold on hand!");
1884 ni->stats->gold += battle->stats->gold;
1885 battle->stats->gold = 0;
1886 }
1887 else
1888 {
1889 battle->stats->gold = 2000000000 - ni->stats->gold;
1890 notice(s_GameServ, battle->getNick(), "You lose ten percent of your experience!");
1891
1892 notice(s_GameServ, battle->getNick(), "However, %s could not carry all of your "\
1893 "gold.", ni->stats->name);
1894
1895 notice(s_GameServ, battle->getNick(), "Luckily, you still have \ 2%ld\ 2 gold "\
1896 "left. All is not lost!", battle->stats->gold);
1897
1898 ni->stats->gold = 2000000000;
1899 }
1900 clearYourTurn(ni->stats);
1901 clearYourTurn(battle->stats);
1902 battle->stats->battle = NULL;
1903 ni->stats->battle = NULL;
1904 return;
1905 }
1906 else
1907 {
1908 if (hit > 0)
1909 battle->stats->hp -= hit;
1910 clearYourTurn(ni->stats);
1911 setYourTurn(battle->stats);
1912 display_players(battle);
1913 notice(s_GameServ, u, "Please wait while %s decides what to do!",
1914 battle->stats->name);
1915 return;
1916 }
1917 }
1918 }
1919 }
1920
1921 void do_heal(char *u)
1922 {
1923 aClient *ni;
1924 char *amount = strtok(NULL, " ");
1925 int price, num;
1926
1927 if (!amount)
1928 {
1929 notice(s_GameServ, u, "SYNTAX: /msg %S HEAL {ALL | #}");
1930 return;
1931 }
1932 else if (!(ni = find(u)))
1933 {
1934 notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
1935 return;
1936 }
1937 else if (isIgnore(ni))
1938 {
1939 #ifdef DEBUGMODE
1940 log("Ignoring %s.", ni->getNick());
1941 #endif
1942 return;
1943 }
1944 else if (!is_playing(ni))
1945 {
1946 notice(s_GameServ, u, "You aren't playing!");
1947 return;
1948 }
1949 else if (!isAlive(ni->stats))
1950 {
1951 notice(s_GameServ, u, "You are dead. Wait until tomorrow for healing.");
1952 return;
1953 }
1954 else if (is_fighting(ni))
1955 {
1956 notice(s_GameServ, u, "You can't heal in battle!");
1957 return;
1958 }
1959 else if (ni->stats->hp >= ni->stats->maxhp)
1960 {
1961 notice(s_GameServ, u, "You don't need healing!");
1962 return;
1963 }
1964
1965 updateTS(ni->stats);
1966 if (stricmp(amount, "ALL") == 0)
1967 {
1968 price = ni->stats->level * 3;
1969 if (ni->stats->gold < (ni->stats->maxhp - ni->stats->hp) * price)
1970 {
1971 notice(s_GameServ, u, "Healing \ 2%d\ 2 points for \ 2%d\ 2 gold per point.",
1972 (long int)ni->stats->gold/price, price);
1973 ni->stats->hp += ni->stats->gold / price;
1974 ni->stats->gold %= price;
1975 }
1976 else
1977 {
1978 notice(s_GameServ, u, "Healing all possible points at \ 2%d\ 2 gold "\
1979 "per point.", price);
1980 notice(s_GameServ, u, "\ 2%d\ 2 points healed for \ 2%ld\ 2 gold. HP at MAX!",
1981 (ni->stats->maxhp - ni->stats->hp),
1982 (price * (ni->stats->maxhp - ni->stats->hp)) );
1983 ni->stats->gold -= price * (ni->stats->maxhp - ni->stats->hp);
1984 ni->stats->hp = ni->stats->maxhp;
1985 }
1986 }
1987 else if (isstringnum(amount))
1988 {
1989 num = stringtoint(amount);
1990 price = ni->stats->level * 3;
1991 if (ni->stats->gold < price * num)
1992 {
1993 notice(s_GameServ, u, "You only have enough gold to heal \ 2%d\ 2 points!",
1994 (long int)ni->stats->gold/price);
1995 }
1996 else if (num <= ni->stats->maxhp - ni->stats->hp)
1997 {
1998 notice(s_GameServ, u, "Healing \ 2%d\ 2 points at \ 2%d\ 2 gold per point.",
1999 num, price);
2000 ni->stats->hp += num;
2001 ni->stats->gold -= num * price;
2002 }
2003 else if (num > ni->stats->maxhp - ni->stats->hp)
2004 {
2005 notice(s_GameServ, u, "Healing all possible points at \ 2%d\ 2 gold "\
2006 "per point.", price);
2007 notice(s_GameServ, u, "\ 2%d\ 2 points healed. HP at MAX!",
2008 (ni->stats->maxhp - ni->stats->hp));
2009 ni->stats->gold -= price * (ni->stats->maxhp - ni->stats->hp);
2010 ni->stats->hp = ni->stats->maxhp;
2011 }
2012 }
2013 else if (amount[0] == '-')
2014 notice(s_GameServ, u, "You trying to cheat?");
2015 else
2016 notice(s_GameServ, u, "SYNTAX: /msg %S HEAL {ALL | #}");
2017 }
2018
2019 int isstringnum(char *num)
2020 {
2021 unsigned int x;
2022 for (x = 0; x < strlen(num); x++)
2023 {
2024 if ((int)num[x] < 48 || (int)num[x] > 57)
2025 return 0;
2026 }
2027 return 1;
2028 }
2029
2030 long int stringtoint(char *number)
2031 {
2032 long int x, len = strlen(number), sum = 0;
2033 if (len == 1)
2034 return chartoint(number[0]);
2035 sum += chartoint(number[len - 1]);
2036 for (x = len - 2; x >= 0; x--)
2037 sum += chartoint(number[x]) * pow(10, abs(x - len + 1));
2038 return sum;
2039 }
2040
2041 long int pow(int x, int y)
2042 {
2043 long int value = 0;
2044 int count = 0;
2045 value += x;
2046
2047 if (x != 0 && y != 0)
2048 {
2049 for (count = 1; count <= y - 1; count++)
2050 value *= x;
2051 }
2052 else
2053 return 1;
2054 return value;
2055 }
2056
2057 long int chartoint(char ch)
2058 {
2059 if (int(ch) >= 48 && int(ch) <= 57)
2060 return int(ch) - 48;
2061 else
2062 return 0;
2063 }
2064
2065 int save_gs_dbase()
2066 {
2067 ListNode<aClient> *ptr;
2068 Player *it;
2069 ofstream outfile;
2070
2071 outfile.open(playerdata);
2072
2073 if (!outfile)
2074 {
2075 log("Error opening %s", playerdata);
2076 return 0;
2077 }
2078
2079 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
2080 {
2081 ptr = players[x].First();
2082 while(ptr)
2083 {
2084 it = ptr->getData()->stats;
2085 clearYourTurn(it);
2086 outfile << it->name << ' ' << it->level << ' ' << it->exp << ' ' << it->gold << ' ' << it->bank << ' '
2087 << it->hp << ' ' << it->maxhp << ' ' << it->strength << ' ' << it->defense << ' '
2088 << it->armor << ' ' << it->weapon << ' '
2089 << it->forest_fights << ' ' << it->player_fights << ' '
2090 << it->getFlags() << ' ' << it->password << ' ' << it->inventory.Healing()
2091 << ' ' << it->inventory.Strength() << ' ' << it->inventory.Defense() << ' ' << it->inventory.HP() << endl;
2092 ptr = ptr->Next();
2093 }
2094 }
2095 outfile.close();
2096 return 1;
2097 }
2098
2099 int load_gs_dbase()
2100 {
2101 ifstream infile;
2102 aClient *temp;
2103 Player *p;
2104 char *tempname, *buf, *password;
2105 buf = new char[1023];
2106
2107 infile.open(playerdata);
2108
2109 if (infile.fail())
2110 {
2111 log("Error opening %s", playerdata);
2112 return 0;
2113 }
2114
2115 for (int x = 0; x < U_TABLE_SIZE; x++)
2116 {
2117 ListNode<aClient> *tempNode;
2118 tempNode = players[x].First();
2119 while (tempNode)
2120 {
2121 if (tempNode->getData()->stats->client)
2122 logout(tempNode->getData()->stats->client);
2123 tempNode = tempNode->Next();
2124 }
2125 players[x].deleteNodes();
2126 }
2127
2128 while (infile.getline(buf, 1024, '\n'))
2129 {
2130 temp = new aClient;
2131 tempname = strtok(buf, " ");
2132 temp->stats = new Player(tempname);
2133 p = temp->stats;
2134
2135 p->level = stringtoint(strtok(NULL, " "));
2136 p->exp = stringtoint(strtok(NULL, " "));
2137 p->gold = stringtoint(strtok(NULL, " "));
2138 p->bank = stringtoint(strtok(NULL, " "));
2139 p->hp = stringtoint(strtok(NULL, " "));
2140 p->maxhp = stringtoint(strtok(NULL, " "));
2141 p->strength = stringtoint(strtok(NULL, " "));
2142 p->defense = stringtoint(strtok(NULL, " "));
2143 p->armor = stringtoint(strtok(NULL, " "));
2144 p->weapon = stringtoint(strtok(NULL, " "));
2145 p->forest_fights = stringtoint(strtok(NULL, " "));
2146 p->player_fights = stringtoint(strtok(NULL, " "));
2147 p->setFlags(stringtoint(strtok(NULL, " ")));
2148
2149 password = strtok(NULL, " ");
2150 strcpy(p->password, password);
2151 temp->setNick("Not Playing");
2152 #ifdef P10
2153 temp->setRealNick("Not Playing");
2154 #endif
2155
2156 p->inventory.reset(); // Set inventory to all 0s
2157 // Old player databases didn't have these three extra values
2158 // If they come up null, leave them to 0 as the default.
2159 // On the next gameserv database save, it will save the values.
2160 tempname = strtok(NULL, " ");
2161 if (tempname)
2162 p->inventory.setHealing(stringtoint(tempname));
2163
2164 tempname = strtok(NULL, " ");
2165 if (tempname)
2166 p->inventory.setStrength(stringtoint(tempname));
2167
2168 tempname = strtok(NULL, " ");
2169 if (tempname)
2170 p->inventory.setDefense(stringtoint(tempname));
2171
2172 tempname = strtok(NULL, " ");
2173 if (tempname)
2174 p->inventory.setHP(stringtoint(tempname));
2175 unsigned long hv = iHASH((unsigned char *) temp->stats->name);
2176
2177 temp->stats->client = NULL;
2178 players[hv].insertAtBack(temp);
2179 delete temp;
2180 }
2181 delete [] buf;
2182 infile.close();
2183 return 1;
2184 }
2185
2186 bool passcmp(char *encrypted, char *plaintext)
2187 {
2188 char salt[3];
2189 char *plaintext2, *plainToencrypt;
2190 bool same = false;
2191
2192 plaintext2 = new char[strlen(encrypted) + strlen(plaintext)]; // Extra
2193 strcpy(plaintext2, plaintext);
2194
2195 salt[0] = encrypted[0];
2196 salt[1] = encrypted[1];
2197 salt[3] = '\0';
2198
2199 plainToencrypt = crypt(plaintext2, salt);
2200
2201 same = (strcmp((const char *)encrypted, plainToencrypt) == 0 ? true : false);
2202
2203 delete []plaintext2;
2204
2205 return same;
2206 }
2207
2208 bool check_password(char *name, char *plaintext)
2209 {
2210 aClient *client;
2211
2212 if (!(client = findplayer(name)))
2213 return false;
2214 else
2215 {
2216 return passcmp(client->stats->password, plaintext);
2217 }
2218 }
2219
2220 void do_store(char *u)
2221 {
2222 char *cmd = strtok(NULL, " ");
2223 char *item = strtok(NULL, " ");
2224 char *num = strtok(NULL, " ");
2225 char *space;
2226 int wep;
2227 aClient *user;
2228 Player *p;
2229
2230 if (!cmd || !item)
2231 {
2232 notice(s_GameServ, u, "SYNTAX: STORE LIST {ARMOR | WEAPONS}");
2233 notice(s_GameServ, u, " \ 2STORE SELL {ARMOR | WEAPON}\ 2");
2234 notice(s_GameServ, u, " \ 2STORE BUY {ARMOR | WEAPON} \1fNUMBER\1f\ 2");
2235 return;
2236 }
2237 else if (!(user = find(u)))
2238 {
2239 log("Fatal Error: could not find %s in client list", u);
2240 return;
2241 }
2242 else if (isIgnore(user))
2243 {
2244 #ifdef DEBUGMODE
2245 log("Ignoring %s.", user->getNick());
2246 #endif
2247 return;
2248 }
2249 else if (!is_playing(user))
2250 {
2251 notice(s_GameServ, u, "You must be playing to use the store!");
2252 return;
2253 }
2254 else if (!isAlive(user->stats))
2255 {
2256 notice(s_GameServ, u, "You are dead. Wait until tomorrow to purchase weapons and armor!");
2257 return;
2258 }
2259 updateTS(user->stats);
2260
2261 if (stricmp(cmd, "LIST") == 0)
2262 {
2263 if (stricmp(item, "WEAPONS") == 0)
2264 {
2265 notice(s_GameServ, u, "Welcome to Kain's Armory");
2266 notice(s_GameServ, u, "Here are the weapons we have available for the killing, sire:");
2267 for (int x = 1; x < WNA; x++)
2268 {
2269 space = spaces(strlen(weapons[x]), ".");
2270 notice(s_GameServ, u, "%s%d. %s%s%d",(x < 10 ? " " : ""), x, weapons[x], space, prices[x - 1]);
2271 free(space);
2272 }
2273 notice(s_GameServ, u, "To purchase a weapon, type /msg %S STORE BUY \ 2NUM\ 2.");
2274 notice(s_GameServ, u, "Where num. is the weapon number from the menu above.");
2275
2276 }
2277 else if (stricmp(item, "ARMOR") == 0)
2278 {
2279 notice(s_GameServ, u, "Welcome to Kain's Armory");
2280 notice(s_GameServ, u, "I hope you enjoy the fine armor we have available for your protection:");
2281 for (int x = 1; x < WNA; x++)
2282 {
2283 space = spaces(strlen(armors[x]), ".");
2284 notice(s_GameServ, u, "%s%d. %s%s%d",(x < 10 ? " " : ""), x, armors[x], space, prices[x - 1]);
2285 free(space);
2286 }
2287 notice(s_GameServ, u, "To purchase armor, type /msg %S store buy armor num.");
2288 notice(s_GameServ, u, "Where num. is the armor number from the menu above.");
2289
2290
2291 }
2292 } else if (stricmp(cmd, "BUY") == 0) {
2293 if (!num)
2294 {
2295 notice(s_GameServ, u, "SYNTAX: \ 2STORE BUY {ARMOR | WEAPON} \1fNUMBER\1f\ 2");
2296 return;
2297 }
2298 else if (!isstringnum(num))
2299 {
2300 notice(s_GameServ, u, "You must specify a number between 1 and %d. Not %s!", WNA - 1, num);
2301 return;
2302 }
2303 if (stricmp(item, "WEAPON") == 0)
2304 {
2305 wep = stringtoint(num);
2306 if (wep >= WNA || wep < 1)
2307 {
2308 notice(s_GameServ, u, "The number %d is out of range. The number you provide must be between 1 and %d.", wep, WNA - 1);
2309 return;
2310 }
2311
2312 p = user->stats;
2313
2314 if (p->weapon != 0)
2315 notice(s_GameServ, u, "You have to sell your %s first!", weapons[p->weapon]);
2316 else if (p->gold < prices[wep - 1])
2317 notice(s_GameServ, u, "You don't have enough gold for %s!", weapons[wep]);
2318 else
2319 {
2320 notice(s_GameServ, u, "You have purchased %s! Thanks for the gold!", weapons[wep]);
2321 p->weapon = wep;
2322 p->gold -= prices[wep - 1];
2323 }
2324 }
2325 else if (stricmp(item, "ARMOR") == 0)
2326 {
2327 wep = stringtoint(num);
2328 if (wep >= WNA || wep < 1)
2329 {
2330 notice(s_GameServ, u, "The number %d is out of range. The number you provide must be between 1 and %d.", wep, WNA - 1);
2331 return;
2332 }
2333
2334 p = user->stats;
2335
2336 if (p->armor != 0)
2337 notice(s_GameServ, u, "You have to sell your %s first!", armors[p->armor]);
2338 else if (p->gold < prices[wep - 1])
2339 notice(s_GameServ, u, "You don't have enough gold for %s!", armors[wep]);
2340 else
2341 {
2342 notice(s_GameServ, u, "You have purchased %s! Thanks for the gold!", armors[wep]);
2343 p->armor = wep;
2344 p->gold -= prices[wep - 1];
2345 }
2346 }
2347 }
2348 else if (stricmp(cmd, "SELL" ) == 0)
2349 {
2350 p = user->stats;
2351
2352 if (stricmp(item, "WEAPON") == 0)
2353 {
2354 if (p->weapon == 0)
2355 {
2356 notice(s_GameServ, u, "You want me to chop off your hands?");
2357 return;
2358 }
2359 else if (p->gold == 2000000000)
2360 {
2361 notice(s_GameServ, u, "You have enough gold. I'll just take that off your hands, sire.");
2362 p->weapon = 0;
2363 }
2364 else if (2000000000 - p->gold < (prices[p->weapon - 1] / 2))
2365 {
2366 notice(s_GameServ, u, "Thank you for your business! You now have as much gold as you can carry.");
2367 notice(s_GameServ, u, "However, you have no weapon... can I interest you in the %s?", weapons[WNA - 1]);
2368 p->gold = 2000000000;
2369 p->weapon = 0;
2370 }
2371 else
2372 {
2373 notice(s_GameServ, u, "Thank you for your business! You now have %d more gold but no weapon!", (prices[p->weapon - 1] / 2));
2374 p->gold += (prices[p->weapon - 1] / 2);
2375 p->weapon = 0;
2376 }
2377 }
2378 else if (stricmp(item, "ARMOR") == 0)
2379 {
2380 p = user->stats;
2381
2382 if (p->armor == 0)
2383 {
2384 notice(s_GameServ, u, "I don't think you can be any more naked...");
2385 return;
2386 }
2387 if (p->gold == 2000000000)
2388 {
2389 notice(s_GameServ, u, "You have enough gold. I'll just take that off your hands, sire.");
2390 p->armor = 0;
2391 }
2392 else if (2000000000 - p->gold < (prices[p->armor - 1] / 2))
2393 {
2394 notice(s_GameServ, u, "Thank you for your business! You now have as much gold as you can carry.");
2395 notice(s_GameServ, u, "However, you have no armor... can I interest you in %s?", armors[WNA - 1]);
2396 p->gold = 2000000000;
2397 p->armor = 0;
2398 }
2399 else
2400 {
2401 notice(s_GameServ, u, "Thank you for your business! You now have %d more gold but no armor!",
2402 (prices[p->armor - 1] / 2));
2403
2404 p->gold += (prices[p->armor - 1] / 2);
2405 p->armor = 0;
2406 }
2407 }
2408 else
2409 {
2410 notice(s_GameServ, u, "SYNTAX: STORE LIST {ARMOR | WEAPONS}");
2411 notice(s_GameServ, u, " \ 2STORE SELL {ARMOR | WEAPON}\ 2");
2412 notice(s_GameServ, u, " \ 2STORE BUY {ARMOR | WEAPON} \1fNUMBER\1f\ 2");
2413 }
2414 }
2415 else
2416 {
2417 notice(s_GameServ, u, "SYNTAX: STORE LIST {ARMOR | WEAPONS}");
2418 notice(s_GameServ, u, " \ 2STORE SELL {ARMOR | WEAPON}\ 2");
2419 notice(s_GameServ, u, " \ 2STORE BUY {ARMOR | WEAPON} \1fNUMBER\1f\ 2");
2420 return;
2421 }
2422 }
2423 void do_inventory(char *u)
2424 {
2425 aClient *user;
2426
2427 if (!(user = find(u)))
2428 {
2429 notice(s_GameServ, u, "Fatal Error. Contact a %S admin!");
2430 return;
2431 }
2432 else if (isIgnore(user))
2433 {
2434 #ifdef DEBUGMODE
2435 log("Ignoring %s.", user->getNick());
2436 #endif
2437 return;
2438 }
2439 else if (!is_playing(user))
2440 {
2441 notice(s_GameServ, u, "You must be playing to check your inventory!");
2442 return;
2443 }
2444 updateTS(user->stats);
2445 showinventory(user, user);
2446 }
2447 void showinventory(aClient *from, aClient *to)
2448 {
2449 char *nick = to->getNick();
2450
2451 if (!to)
2452 to = from;
2453 if (is_playing(from))
2454 {
2455 Pouch *p = &from->stats->inventory;
2456 notice(s_GameServ, nick, "Inventory for %s:", from->stats->name);
2457 notice(s_GameServ, nick, " Healing Potions: %d", p->Healing());
2458 notice(s_GameServ, nick, "Strength Potions: %d", p->Strength());
2459 notice(s_GameServ, nick, " Defense Potions: %d", p->Defense());
2460 notice(s_GameServ, nick, " HP Potions: %d", p->HP());
2461 }
2462 }
2463 void do_tavern(char *u)
2464 {
2465 char *cmd = strtok(NULL, " ");
2466 long int price;
2467
2468 aClient *user;
2469 Player *p;
2470
2471 if (!(user = find(u)))
2472 {
2473 notice(s_GameServ, u, "Fatal Error. See a %S admin for help");
2474 return;
2475 }
2476 else if (isIgnore(user))
2477 {
2478 #ifdef DEBUGMODE
2479 log("Ignoring %s.", user->getNick());
2480 #endif
2481 return;
2482 }
2483 else if (!is_playing(user))
2484 {
2485 notice(s_GameServ, u, "You must be playing to go to the Tavern");
2486 return;
2487 }
2488 else if (is_fighting(user))
2489 {
2490 notice(s_GameServ, u, "You cannot go to the Tavern during a fight!");
2491 return;
2492 }
2493
2494 updateTS(user->stats);
2495 p = user->stats;
2496
2497 if (!cmd)
2498 {
2499 notice(s_GameServ, u, "Welcome to Boot Liquors Mystic Apothecary");
2500 notice(s_GameServ, u, "Your commands:");
2501 notice(s_GameServ, u, "/msg %S TAVERN {LIST | BUY} [NUMBER]");
2502 notice(s_GameServ, u, "What'll it be?");
2503 }
2504 else if (stricmp(cmd, "LIST") == 0)
2505 {
2506 notice(s_GameServ, u, "Here is a list of what we have to offer:");
2507 notice(s_GameServ, u, "1. Healing Potions for %ld Gold",
2508 1000 * p->level * 4);
2509 notice(s_GameServ, u, "2. Strength Potions for %ld Gold",
2510 2500 * p->level * 4);
2511 notice(s_GameServ, u, "3. Defense Potions for %ld Gold",
2512 3000 * p->level * 4);
2513 notice(s_GameServ, u, "4. HP Potions for %ld Gold",
2514 2000 * p->level * 4);
2515 notice(s_GameServ, u, "To buy a potion, type /msg %S TAVERN BUY #");
2516 notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
2517 }
2518 else if (stricmp(cmd, "BUY") == 0)
2519 {
2520 char *chnum = strtok(NULL, " ");
2521
2522 if (!chnum)
2523 {
2524 notice(s_GameServ, u, "SYNTAX: TAVERN BUY #");
2525 notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1");
2526 return;
2527 }
2528 int num = stringtoint(chnum);
2529
2530 if (num < 1 || num > 4)
2531 {
2532 notice(s_GameServ, u, "Invalid Choice!");
2533 notice(s_GameServ, u, "Here is a list of what we have to offer:");
2534 notice(s_GameServ, u, "1. Healing Potions for %ld Gold",
2535 1000 * p->level * 4);
2536 notice(s_GameServ, u, "2. Strength Potions for %ld Gold",
2537 2500 * p->level * 4);
2538 notice(s_GameServ, u, "3. Defense Potions for %ld Gold",
2539 3000 * p->level * 4);
2540 notice(s_GameServ, u, "4. HP Potions for %ld Gold",
2541 2000 * p->level * 4);
2542 notice(s_GameServ, u, "To buy a potion, type /msg %S TAVERN BUY #");
2543 notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
2544 return;
2545 }
2546 switch(num)
2547 {
2548 case 1:
2549 price = (1000 * p->level * 4);
2550 if (p->gold >= price)
2551 {
2552 notice(s_GameServ, u, "One healing potion coming right up!");
2553 p->inventory.incHealing();
2554 p->gold -= price;
2555 }
2556 else
2557 notice(s_GameServ, u, "You don't have enough gold!");
2558 break;
2559 case 2:
2560 price = 2500 * p->level * 4;
2561 if (p->gold >= price)
2562 {
2563 notice(s_GameServ, u, "One strength boost coming right up!");
2564 p->inventory.incStrength();
2565 p->gold -= price;
2566 }
2567 else
2568 notice(s_GameServ, u, "You don't have enough gold!");
2569 break;
2570 case 3:
2571 price = 3000 * p->level * 4;
2572 if (p->gold >= price)
2573 {
2574 notice(s_GameServ, u, "One defense boost coming right up!");
2575 p->inventory.incDefense();
2576 p->gold -= price;
2577 }
2578 else
2579 notice(s_GameServ, u, "You don't have enough gold!");
2580 break;
2581 case 4:
2582 price = 3000 * p->level * 4;
2583 if (p->gold >= price)
2584 {
2585 notice(s_GameServ, u, "One HP Potion coming right up!");
2586 p->inventory.incHP();
2587 p->gold -= price;
2588 }
2589 else
2590 notice(s_GameServ, u, "You don't have enough gold!");
2591 break;
2592 default:
2593 notice(s_GameServ, u, "Logical Error. See a %S admin for help!");
2594 break;
2595 }
2596 }
2597 else
2598 {
2599 notice(s_GameServ, u, "Improper Syntax.");
2600 notice(s_GameServ, u, "Type /msg %S HELP TAVERN for help");
2601 }
2602 }
2603
2604 void do_bank(char *u)
2605 {
2606 char *cmd = strtok(NULL, " ");
2607 char *amount = strtok(NULL, " ");
2608 char *nick = strtok(NULL, " ");
2609
2610 aClient *user;
2611 Player *p;
2612
2613 if (!cmd || (!amount && stricmp(cmd, "BALANCE") != 0) || (stricmp(cmd, "TRANSFER") == 0 && !nick))
2614 {
2615 notice(s_GameServ, u, "BANK {WITHDRAW | DEPOSIT} {ALL | AMOUNT}");
2616 notice (s_GameServ, u, "BANK BALANCE");
2617 return;
2618 }
2619 else if (!(user = find(u)))
2620 {
2621 notice(s_GameServ, u, "Fatal Error. Couldn't find your aClient. Contact a(n) %S "\
2622 " admin for help");
2623 log("Fatal Error. Couldn't find %s while executing do_bank()", u);
2624 return;
2625 }
2626 else if (isIgnore(user))
2627 {
2628 #ifdef DEBUGMODE
2629 log("Ignoring %s.", user->getNick());
2630 #endif
2631 return;
2632 }
2633 else if (!is_playing(user))
2634 {
2635 notice(s_GameServ, u, "You must be playing to use the bank!");
2636 return;
2637 }
2638 else if (is_fighting(user))
2639 {
2640 notice(s_GameServ, u, "You can't go to the bank during a fight!");
2641 return;
2642 }
2643 updateTS(user->stats);
2644 if (stricmp(cmd, "BALANCE") == 0)
2645 {
2646 showBankBalance(u);
2647 return;
2648 }
2649 else if (!isAlive(user->stats))
2650 {
2651 notice(s_GameServ, u, "You are dead. We don't accept gold from dead folk! Wait 'til tomorrow!");
2652 return;
2653 }
2654 else if (!isstringnum(amount) && stricmp(amount, "ALL") != 0)
2655 {
2656 notice(s_GameServ, u, "I don't know how to convert alphabet letters into currency, sire!");
2657 return;
2658 }
2659 if (stringtoint(amount) < 0)
2660 {
2661 notice(s_GameServ, u, "Sorry. This bank is not licensed "\
2662 "to handle such sums of cash, noble Lord.");
2663 return;
2664 }
2665 p = user->stats;
2666
2667 if (stricmp(cmd, "DEPOSIT") == 0)
2668 {
2669 if (p->bank == 2000000000)
2670 {
2671 notice(s_GameServ, u, "Your bank account is full, sire!");
2672 return;
2673 }
2674 else if (stricmp(amount, "ALL") == 0)
2675 {
2676 if (2000000000 - p->bank < p->gold)
2677 {
2678 notice(s_GameServ, u, "You don't have enough room for all of your gold.");
2679 notice(s_GameServ, u, "Depositing %ld gold into your account", (2000000000 - p->bank));
2680 p->gold -= (2000000000 - p->bank);
2681 p->bank = 2000000000;
2682 showBankBalance(u);
2683 }
2684 else
2685 {
2686 notice(s_GameServ, u, "Depositing %ld gold into your account!", p->gold);
2687 p->bank += p->gold;
2688 p->gold = 0;
2689 showBankBalance(u);
2690 }
2691 }
2692 else if (stringtoint(amount) > p->gold)
2693 {
2694 notice(s_GameServ, u, "Sire, you only have %ld gold!", p->gold);
2695 showBankBalance(u);
2696 return;
2697 }
2698 else
2699 {
2700 if (2000000000 - p->bank < stringtoint(amount))
2701 {
2702 notice(s_GameServ, u, "You don't have room in your account for that much.");
2703 notice(s_GameServ, u, "Capping off your account with %ld gold!", (2000000000 - p->bank));
2704 p->gold -= (2000000000 - p->bank);
2705 p->bank = 2000000000;
2706 showBankBalance(u);
2707 }
2708 else
2709 {
2710 notice(s_GameServ, u, "Depositing %d gold into your account!", stringtoint(amount));
2711 p->bank += stringtoint(amount);
2712 p->gold -= stringtoint(amount);
2713 showBankBalance(u);
2714 }
2715 }
2716 }
2717 else if (stricmp(cmd, "WITHDRAW") == 0)
2718 {
2719 if (p->gold == 2000000000)
2720 {
2721 notice(s_GameServ, u, "You cannot carry any more gold, sire!");
2722 showBankBalance(u);
2723 return;
2724 }
2725 else if (stricmp(amount, "ALL") == 0)
2726 {
2727 if (2000000000 - p->gold < p->bank)
2728 {
2729 notice(s_GameServ, u, "You don't have enough room to carry all that gold.");
2730 notice(s_GameServ, u, "Withdrawing %ld gold from your account", (2000000000 - p->gold));
2731 p->bank -= (2000000000 - p->gold);
2732 p->gold = 2000000000;
2733 showBankBalance(u);
2734 }
2735 else
2736 {
2737 notice(s_GameServ, u, "Withdrawing %ld gold from your account!", p->bank);
2738 p->gold += p->bank;
2739 p->bank = 0;
2740 showBankBalance(u);
2741 }
2742 }
2743 else if (stringtoint(amount) > p->bank)
2744 {
2745 notice(s_GameServ, u, "Sire, you only have %ld gold in the bank!", p->bank);
2746 showBankBalance(u);
2747 return;
2748 }
2749 else
2750 {
2751 if (2000000000 - p->gold < stringtoint(amount))
2752 {
2753 notice(s_GameServ, u, "You don't enough have room to carry that much gold!");
2754 notice(s_GameServ, u, "You fill your pockets with %ld gold!",
2755 (2000000000 - p->gold));
2756 p->bank -= (2000000000 - p->gold);
2757 p->gold = 2000000000;
2758 showBankBalance(u);
2759 }
2760 else
2761 {
2762 notice(s_GameServ, u, "Withdrawing %d gold from your account!", stringtoint(amount));
2763 p->gold += stringtoint(amount);
2764 p->bank -= stringtoint(amount);
2765 showBankBalance(u);
2766 }
2767 }
2768 }
2769
2770 }
2771
2772 void do_dragon(char *u)
2773 {
2774 aClient *user;
2775
2776 if (!(user = find(u)))
2777 {
2778 notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
2779 return;
2780 }
2781 else if (isIgnore(user))
2782 {
2783 #ifdef DEBUGMODE
2784 log("Ignoring %s.", user->getNick());
2785 #endif
2786 return;
2787 }
2788 else if (!is_playing(user))
2789 {
2790 notice(s_GameServ, u, "You must be playing to fight the dragon!");
2791 return;
2792 }
2793 else if (is_fighting(user))
2794 {
2795 notice(s_GameServ, u, "You are already in a fight. How will you fight the almighty dragon!?");
2796 return;
2797 }
2798 else if (!isAlive(user->stats))
2799 {
2800 notice(s_GameServ, u, "You're dead. Wait until tomorrow to see your master!");
2801 return;
2802 }
2803 else if (user->stats->level < REALLEVELS)
2804 {
2805 notice(s_GameServ, u, "You fool! Only those strong enough "\
2806 "to vanquish any foe should DARE fight the dragon!");
2807 notice(s_GameServ, u, "To put it in terms you can understand: "\
2808 "You are too weak. You must be Level %d!", REALLEVELS);
2809 return;
2810 }
2811
2812 updateTS(user->stats);
2813
2814 Player *p = user->stats;
2815 p->fight = new Monster(boss);
2816 notice(s_GameServ, u, "You approach the dragon's lair cautiously.");
2817 notice(s_GameServ, u, "The stench of sulfer fills the air as a "\
2818 "deep, red fog rolls in. The air is filled with the "\
2819 "heated mist of deadly fire from beyond the cave "\
2820 "entrance.");
2821 notice(s_GameServ, u, "You adjust your %s, tighten your grip on "\
2822 "your %s, and venture into the hot, dark cave. "\
2823 "You are surprised at the angle of descent as you climb "\
2824 "lower and lower, deeper into the dragon's den.");
2825 notice(s_GameServ, u, "You come to the end of the cave to find "\
2826 "a tooth. It is a large tooth... bigger than your torso."\
2827 " Suddenly the darkness lifts from the gleam of an eye "\
2828 " staring into your soul! The eye is large... HUGE!");
2829 notice(s_GameServ, u, "Just then you notice the eye begin to "\
2830 "glare orange! The tooth is moving... but it is still too "\
2831 "dark for you to make out.... THE DRAGON! You see it!");
2832 display_monster(u);
2833 }
2834
2835 void do_master(char *u)
2836 {
2837 aClient *user;
2838
2839
2840 if (!(user = find(u)))
2841 {
2842 notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
2843 return;
2844 }
2845 else if (isIgnore(user))
2846 {
2847 #ifdef DEBUGMODE
2848 log("Ignoring %s.", user->getNick());
2849 #endif
2850 return;
2851 }
2852 else if (!is_playing(user))
2853 {
2854 notice(s_GameServ, u, "You must be playing to see your master!");
2855 return;
2856 }
2857 else if (is_fighting(user))
2858 {
2859 notice(s_GameServ, u, "You're in the middle of a fight! Pay attention!");
2860 return;
2861 }
2862 else if (!isAlive(user->stats))
2863 {
2864 notice(s_GameServ, u, "You're dead. Wait until tomorrow to see your master!");
2865 return;
2866 }
2867
2868 updateTS(user->stats);
2869
2870 char *cmd = strtok(NULL, " ");
2871 Player *p = user->stats;
2872 long int need = 0;
2873
2874 if (seenMaster(p))
2875 {
2876 notice(s_GameServ, u, "You have already seen your master today. Wait until tomorrow to try again");
2877 return;
2878 }
2879
2880 if (cmd != NULL)
2881 {
2882 switch(p->level)
2883 {
2884 case 1:
2885 need = 200;
2886 break;
2887 case 2:
2888 need = 800;
2889 break;
2890 case 3:
2891 need = 2000;
2892 break;
2893 case 4:
2894 need = 8000;
2895 break;
2896 case 5:
2897 need = 20000;
2898 break;
2899 case 6:
2900 need = 80000;
2901 break;
2902 case 7:
2903 need = 200000;
2904 break;
2905 case 8:
2906 need = 800000;
2907 break;
2908 case 9:
2909 need = 2000000;
2910 break;
2911 case 10:
2912 need = 8000000;
2913 break;
2914 case 11:
2915 need = 20000000;
2916 break;
2917
2918 case REALLEVELS:
2919 need = p->exp + 1;
2920 notice(s_GameServ, u, "You are at level %d. You are the master. What's left? The DRAGON!", REALLEVELS);
2921 return;
2922 break;
2923 default:
2924 need = p->exp + 1; // Unknown level... don't let them fight a fake master!
2925 break;
2926 }
2927 }
2928 else
2929 {
2930 notice(s_GameServ, u, "SYNTAX: MASTER {FIGHT | QUESTION}");
2931 return;
2932 }
2933
2934 if (stricmp(cmd, "FIGHT") == 0)
2935 {
2936 if (p->exp >= need)
2937 {
2938 setMaster(p);
2939 see_master(u);
2940 }
2941 else
2942 notice(s_GameServ, u, "You are not worthy of fighting %s! You need %ld more experience.", masters[p->level - 1]->name, (need - p->exp));
2943 return;
2944 }
2945 else if (stricmp(cmd, "QUESTION") == 0)
2946 {
2947 if (p->exp >= need)
2948 notice(s_GameServ, u, "%s looks you up and down and decides you are more ready than you will ever be.", masters[p->level - 1]->name);
2949 else
2950 notice(s_GameServ, u, "You pathetic fool! You are no match for %s, %s!", masters[p->level - 1]->name, p->name);
2951
2952 return;
2953 }
2954 else
2955 {
2956 notice(s_GameServ, u, "SYNTAX: MASTER {FIGHT | QUESTION}");
2957 }
2958 }
2959
2960 void see_master(char *u)
2961 {
2962 aClient *user;
2963
2964 if (!(user = find(u)))
2965 {
2966 notice(s_GameServ, u, "Fatal error. Contact a(n) %S admin. buf: %s", strtok(NULL, ""));
2967 return;
2968 }
2969
2970 if (!is_fighting(user) && is_playing(user))
2971 {
2972 Player *p = user->stats;
2973 p->master = new Monster(masters[p->level - 1]);
2974 p->fight = p->master;
2975 display_monster(u); // Since master is the same structure, use this function
2976 }
2977 }
2978
2979 void showBankBalance(const char *u)
2980 {
2981 aClient *user;
2982 Player *p;
2983
2984 if (!(user = find(u)))
2985 return;
2986
2987 p = user->stats;
2988
2989 if (!p)
2990 return;
2991
2992 notice(s_GameServ, u, "Account Balance: %ld Gold On hand: %ld", p->bank, p->gold);
2993
2994 }
2995
2996 void refreshall()
2997 {
2998 ListNode <aClient> *it;
2999 Player *p;
3000 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
3001 {
3002 it = players[x].First();
3003
3004 while (it)
3005 {
3006 p = it->getData()->stats;
3007 refresh(p);
3008 it = it->Next();
3009 }
3010 }
3011 }
3012
3013 void refresh(Player *p)
3014 {
3015 if (!p)
3016 return;
3017
3018 if (p->hp < p->maxhp)
3019 p->hp = p->maxhp;
3020 p->forest_fights = forestfights;
3021 p->player_fights = 3;
3022 setAlive(p);
3023 clearMaster(p);
3024 }
3025
3026 void do_refresh(char *u)
3027 {
3028 char *nick = strtok(NULL, " ");
3029 aClient *user;
3030
3031 if (!(user = find(u)))
3032 {
3033 notice(s_GameServ, u, "Error: aClient not found. Contact a %S admin");
3034 log("Error: aClient not found: %s", u);
3035 return;
3036 }
3037 else if (isIgnore(user))
3038 {
3039 #ifdef DEBUGMODE
3040 log("Ignoring %s.", user->getNick());
3041 #endif
3042 return;
3043 }
3044 else if (!isAdmin(user))
3045 {
3046 notice(s_GameServ, u, "You must be a %S admin to use this command!");
3047 return;
3048 }
3049 if (!nick)
3050 {
3051 notice(s_GameServ, u, "SYNTAX: REFRESH {ALL | NICK}");
3052 return;
3053 }
3054 else if (stricmp(nick, "ALL") == 0)
3055 {
3056 notice(s_GameServ, u, "Refreshing everyone's stats!");
3057 refreshall();
3058 }
3059 else if ((user = findplayer(nick)))
3060 {
3061 if (is_playing(user))
3062 {
3063 #ifdef P10
3064 notice(s_GameServ, u, "Refreshing %s.", user->getRealNick());
3065 #else
3066 notice(s_GameServ, u, "Refreshing %s.", user->getNick());
3067 #endif
3068 refresh(user->stats);
3069 }
3070 else
3071 {
3072 #ifdef P10
3073 notice(s_GameServ, u, "%s is not playing.", user->getRealNick());
3074 #else
3075 notice(s_GameServ, u, "%s is not playing.", user->getNick());
3076 #endif
3077 }
3078 }
3079 else
3080 {
3081 notice(s_GameServ, u, "Nick %s not found.", nick);
3082 return;
3083 }
3084 }
3085
3086
3087 void resetall()
3088 {
3089 ListNode <aClient> *it;
3090 Player *p;
3091
3092 for (unsigned long x = 0; x < U_TABLE_SIZE; x++)
3093 {
3094 it = players[x].First();
3095
3096 while (it)
3097 {
3098 p = it->getData()->stats;
3099 reset(p);
3100 it = it->Next();
3101 }
3102 }
3103 }
3104
3105 void reset(Player *p)
3106 {
3107 if (!p)
3108 return;
3109
3110 p->reset();
3111 }
3112
3113 void updateTS(Player *p)
3114 {
3115 if (!p)
3116 return;
3117
3118 #ifdef DEBUGMODE
3119 log("Old timestamp for %s: %ld", p->name, p->lastcommand);
3120 #endif
3121 p->lastcommand = time(NULL);
3122 #ifdef DEBUGMODE
3123 log("New timestamp for %s: %ld", p->name, p->lastcommand);
3124 #endif
3125
3126 }
3127
3128 bool timedOut(Player *p)
3129 {
3130 if (!p)
3131 return false;
3132 else if (p->lastcommand == 0)
3133 return false;
3134 else
3135 {
3136 if ((time(NULL) - p->lastcommand) >= maxidletime)
3137 return true;
3138
3139 return false;
3140 }
3141 }
3142
3143 void timeOutEvent(Player *p)
3144 {
3145 aClient *user = findplayer(p->name);
3146
3147 if (!user || !p->client) // then they're not playing
3148 return;
3149
3150 char *nick = user->getNick();
3151
3152 if (player_fight(user) && isYourTurn(p))
3153 {
3154 // Check to see if they were the idler or if it was the other
3155 // person
3156 if (p->lastcommand != p->battle->stats->lastcommand)
3157 {
3158 // This person's last command was given earlier,
3159 // so this person is the idler
3160 notice(s_GameServ, nick, "You timed out "\
3161 "during a fight. You lose your turn!");
3162 notice(s_GameServ, p->battle->getNick(),
3163 "%s hesitated for too long. Your move.", p->name);
3164 clearYourTurn(p);
3165 setYourTurn(p->battle->stats);
3166
3167 // Update the TS for both players to give them another
3168 // Chance to wake up, but if the other player doesn't
3169 // Attack now, they both get logged out.
3170 updateTS(p);
3171 p->battle->stats->lastcommand = p->lastcommand;
3172 display_players(p->battle);
3173 return;
3174 }
3175 else
3176 {
3177 notice(s_GameServ, p->battle->getNick(),
3178 "You and %s timed out at the same time."\
3179 " Don't fight if you're just going to "\
3180 "sit there!", p->name);
3181 notice(s_GameServ, user->getNick(),
3182 "You and %s timed out at the same time."\
3183 " Don't fight if you're just going to "\
3184 "sit there!", p->battle->stats->name);
3185 logout(p->battle);
3186 logout(user);
3187 return;
3188 }
3189 }
3190 else if (!player_fight(user))
3191 {
3192 if (isAlive(user->stats) && user->stats->gold > 0)
3193 {
3194 // Place fun stuff here :)
3195 int randnum = 1 + rand() % 100; // 1-100
3196 #define GSN(s) notice(s_GameServ, nick, s)
3197 #define GSN2(s, f) notice(s_GameServ, nick, s, f)
3198
3199 if (randnum < 50)
3200 {
3201 // 35-100% of your gold goes pffft - kain
3202 int stolen = (35 + (rand() % 66)) * user->stats->gold / 100;
3203
3204 GSN("You stop for a moment to rest on the "\
3205 "street corner. All of a sudden, you "\
3206 "are ambushed from all sides by a hoarde "\
3207 "of knife wielding thugs.");
3208 GSN2("The thugs beat you into utter submission "\
3209 "and steal %d gold from you!", stolen);
3210 user->stats->gold -= stolen;
3211 }
3212 else if (randnum >= 50 && randnum < 75)
3213 {
3214 // 25-65% of your gold goes pffft - kain
3215 int stolen = (25 + (rand() % 41)) * user->stats->gold / 100;
3216 GSN("While dilly dallying around, you lose "\
3217 "your sense of time. Little did you know, "\
3218 "but thieves lifted your gold while you "\
3219 "weren't watching.");
3220 GSN2("Better luck next time... you lose %d gold", stolen);
3221 user->stats->gold -= stolen;
3222 }
3223 else if (randnum >= 75)
3224 {
3225 // 25-75% of your gold goes pffft - kain
3226 int stolen = (25 + (rand() % 51)) * user->stats->gold / 100;
3227 GSN("Good grief! A gaggle of gooey green ghostlike "\
3228 "goblins grabbed your gold!");
3229 GSN2("They stole %d gold from you!", stolen);
3230 user->stats->gold -= stolen;
3231 }
3232 }
3233
3234 // Always log out the user
3235 logout(user);
3236 }
3237 }
3238
3239 void do_reset(char *u)
3240 {
3241 char *nick = strtok(NULL, " ");
3242 aClient *user;
3243
3244 if (!(user = find(u)))
3245 {
3246 notice(s_GameServ, u, "Error: aClient not found. Contact a %S admin");
3247 log("Error: aClient not found: %s", u);
3248 return;
3249 }
3250 else if (!isAdmin(user))
3251 {
3252 notice(s_GameServ, u, "You must be a %S admin to use this command!");
3253 return;
3254 }
3255
3256 if (!nick)
3257 {
3258 notice(s_GameServ, u, "SYNTAX: RESET {ALL | NICK}");
3259 return;
3260 }
3261 else if (stricmp(nick, "ALL") == 0)
3262 {
3263 notice(s_GameServ, u, "Resetting everyone's stats!");
3264 resetall();
3265 }
3266 else if ((user = findbyrealnick(nick)))
3267 {
3268 if (is_playing(user))
3269 {
3270 #ifdef P10
3271 notice(s_GameServ, u, "Resetting %s.", user->getRealNick());
3272 #else
3273 notice(s_GameServ, u, "Resetting %s.", user->getNick());
3274 #endif
3275 reset(user->stats);
3276 }
3277 else
3278 {
3279 #ifdef P10
3280 notice(s_GameServ, u, "%s is not playing.", user->getRealNick());
3281 #else
3282 notice(s_GameServ, u, "%s is not playing.", user->getNick());
3283 #endif
3284 }
3285 }
3286 else
3287 {
3288 notice(s_GameServ, u, "Nick %s not found.", nick);
3289 return;
3290 }
3291 }
3292
3293 void do_help(char *u)
3294 {
3295 char *cmd = strtok(NULL, " ");
3296
3297 display_help(u, cmd);
3298 }
3299
3300 void display_help(char *u, char *file)
3301 {
3302 ifstream infile;
3303 char *buf;
3304
3305 if (!file)
3306 {
3307 infile.open("helpfiles/help");
3308 if (infile.fail())
3309 {
3310 log("Error opening helpfiles/help");
3311 notice(s_GameServ, u, "Error opening helpfiles/help");
3312 return;
3313 }
3314 buf = new char[1024];
3315 while(infile.getline(buf, 1024))
3316 {
3317 // Written this way, it will process %S in the helpfiles
3318 // Instead of notice(s_GameServ, u, "%s", buf);
3319 notice(s_GameServ, u, buf);
3320 }
3321
3322 // Minor recursion
3323 aClient *user = find(u);
3324 if (user && isAdmin(user))
3325 display_help(u, "admin_commands");
3326 }
3327 else
3328 {
3329 char *filename;
3330 filename = new char[strlen(file) + 11];
3331 strcpy(filename, "helpfiles/");
3332 strcat(filename, file);
3333
3334 for (unsigned int x = 10; x < strlen(filename); x++)
3335 filename[x] = tolower(filename[x]);
3336
3337 infile.open(filename);
3338 delete [] filename;
3339 if (infile.fail())
3340 {
3341 notice(s_GameServ, u, "No help for \ 2%s\ 2", file);
3342 return;
3343 }
3344 buf = new char[1024];
3345 while(infile.getline(buf, 1024))
3346 {
3347 // Written this way, it will process %S in the helpfiles
3348 // Instead of notice(s_GameServ, u, "%s", buf);
3349 notice(s_GameServ, u, buf);
3350 }
3351 }
3352 infile.close();
3353 delete [] buf;
3354 }
3355
3356 void do_admin(char *u)
3357 {
3358 aClient *user;
3359 char *pass = strtok(NULL, " ");
3360
3361 if (!(user = find(u)))
3362 {
3363 log("Error: aClient not found: %s", u);
3364 notice(s_GameServ, u, "Error: aClient not found. Contact %S admin.");
3365 return;
3366 }
3367
3368 if (!pass)
3369 {
3370 notice(s_GameServ, u, "SYNTAX: \ 2ADMIN\ 2 \ 2\1fpassword\1f\ 2");
3371 return;
3372 }
3373
3374 if (isAdmin(user))
3375 {
3376 notice(s_GameServ, u, "You already have administrator privledges.");
3377 return;
3378 }
3379 else if (strcmp(pass, adminpass) == 0)
3380 {
3381 notice(s_GameServ, u, "Password accepted. You now have administrator privledges.");
3382 setAdmin(user);
3383 #ifdef P10
3384 log("%s became an administrator.", user->getRealNick());
3385 #else
3386 log("%s became an administrator.", user->getNick());
3387 #endif
3388 }
3389 else
3390 {
3391 notice(s_GameServ, u, "Invalid password. Remember: case sensitive");
3392 return;
3393 }
3394 }
3395
3396 bool load_monsters()
3397 {
3398 ifstream infile;
3399 infile.open("monsters.dat");
3400
3401 char *buf;
3402
3403 if (infile.fail())
3404 {
3405 log("Error opening monsters.dat");
3406 return false;
3407 }
3408 init_monsters();
3409 buf = new char[2048];
3410
3411 #ifdef DEBUGMODE
3412 log("Loading monsters from monsters.dat");
3413 #endif
3414
3415 for (int l = 0; l < REALLEVELS; l++)
3416 {
3417 for (int m = 0; m < MONSTERS;)
3418 {
3419 infile.getline(buf, 2048);
3420 if (buf[0] == '\n' || buf[0] == '\0' || buf[0] == '#')
3421 continue;
3422 else
3423 {
3424 strcpy(monsters[l][m]->name, strtok(buf, "~"));
3425 strcpy(monsters[l][m]->weapon, strtok(NULL, "~"));
3426 monsters[l][m]->strength = stringtoint(strtok(NULL, "~"));
3427 monsters[l][m]->gold = stringtoint(strtok(NULL, "~"));
3428 monsters[l][m]->exp = stringtoint(strtok(NULL, "~"));
3429 monsters[l][m]->maxhp = stringtoint(strtok(NULL, "~"));
3430 monsters[l][m]->hp = monsters[l][m]->maxhp;
3431 strcpy(monsters[l][m]->death, strtok(NULL, ""));
3432 m++;
3433 }
3434 }
3435 }
3436 delete [] buf;
3437 return true;
3438 }