]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/gameserv.cpp
implemented the tavern only listing certain items based on level
[irc/gameservirc.git] / gameserv / gameserv.cpp
index 673e4f5afbd31b7cd9e065052c843219a180437f..26cbe2b90bc168b947f8e63b61aa9c8af0cd56cf 100644 (file)
@@ -29,13 +29,19 @@ using namespace std;
 
 Monster dragon;                                // The current dragon
 Level levels[LEVELS];                  // The newest way to store monsters
+list<item*> Items;                      // The master list of items
+list<tavernItem> tavern;                // The list of items available at the tavern
 
 // Database functions
 int save_gs_dbase();
 int load_gs_dbase();
+int load_items();
+int load_tavern();
 int load_dragon();
 int save_dragon();
 
+item *findItemByID(int id);
+
 // String functions
 #ifndef HAVE_STRTOK
 char *strtok(char *str, const char *delim);
@@ -125,6 +131,7 @@ void see_master(char *u);
 
 void logout(aClient *user);
 void showstats(const char *u, const char *nick);
+void showTavern(aClient *user);
 void showinventory(aClient *from, aClient *to);
 void showBankBalance(const char *u);
 void end_turn(aClient *user);
@@ -518,6 +525,7 @@ void notice(const char *source, const char *dest, const char *fmt, ...)
                case 'd': sprintf(input, "%s%d", input, va_arg(args, int)); break; 
                case 's': sprintf(input, "%s%s", input, va_arg(args, char *)); break;
                case 'S': sprintf(input, "%s%s", input, s_GameServ); break;
+               case 'c': sprintf(input, "%s%c", input, va_arg(args, int)); break;
                case 'l':
                   if (*++t == 'd')
                          sprintf(input, "%s%ld", input, va_arg(args, long int)); break;
@@ -2085,10 +2093,21 @@ int save_gs_dbase()
                << it->hp << ' ' << it->maxhp << ' ' << it->strength << ' ' << it->defense << ' '
                << it->arm << ' ' << it->wea << ' '
                << it->forest_fights << ' ' << it->player_fights <<  ' ' 
-               << it->getFlags() << ' ' << it->password << ' ' 
-/* fixing this << it->inventory.Healing()
-               << ' ' << it->inventory.Strength() << ' ' << it->inventory.Defense() << ' ' << it->inventory.HP()
-               << ' ' */ << it->lastlogin << endl;
+               << it->getFlags() << ' ' << it->password << ' ' << it->lastlogin;
+
+       // Traverse the list and write out each item ID and how many uses are left
+       if (!it->inventory->isEmpty())
+         {
+           list<itemContainer> *myitems;
+           list<itemContainer>::iterator item_iter;
+           myitems = it->inventory->getItems();
+           
+           for(item_iter = myitems->begin();item_iter != myitems->end();item_iter++)
+             {
+               outfile << ' ' << (*item_iter).getItem()->getID() << ' ' << (*item_iter).getUses();
+             }
+         }
+       outfile << endl;
        ptr = ptr->Next();
     }
    }
@@ -2157,7 +2176,111 @@ outfile.close();
 
 return 1;
 }
+int load_tavern()
+{
+  ifstream infile;
+  char *buf;
+  tavernItem tempItem;
+  buf = new char[1024];
+  long id, level;
+
+  infile.open(tavernitemdata);
+  if (infile.fail())
+    {
+      log("Error opening %s", tavernitemdata);
+      return 0;
+    }
+
+  while (infile.getline(buf, 1024, '\n'))
+    {
+      try
+       {
+         if (buf[0] != '#' && buf[0] != '\n' && buf[0] != '\0')
+           {
+             item *tempItem2;
+             id = stringtoint(strtok(buf, "~"));
+             level = stringtoint(strtok(NULL, ""));
+             tempItem2 = findItemByID(id);
+             if (tempItem2 == NULL)
+               {
+                 log("Invalid ID in %s", tavernitemdata);
+                 return 0;
+               }
+             tempItem.setItem(tempItem2);
+             tempItem.setLevel(level);
 
+             log("pushing item %s level %ld", tempItem2->getName().c_str(), level);
+             tavern.push_back(tempItem);
+           }
+       }
+      catch (char *str)
+       {
+         log("Exception loading tavern: %s", str);
+         delete []buf;
+         return 0;
+       }
+    }
+  delete []buf;
+  return 1;
+}
+int load_items()
+{
+  ifstream infile;
+  char *buf;
+  item *tempItem;
+
+
+  buf = new char[1024];
+
+  infile.open(itemdata);
+
+  if (infile.fail())
+    {
+      log("Error opening %s", itemdata);
+      return 0;
+    }
+
+  while (infile.getline(buf, 1024, '\n'))
+    {
+      if (buf[0] == '^')
+       break;
+      try
+       {
+         if (buf[0] != '#' && buf[0] != '\n' && buf[0] != '\0')
+           {
+             switch(buf[0])
+               {
+               case '1':
+                 tempItem = new weapon();
+                 break;
+               case '2':
+                 tempItem = new armor();
+                 break;
+               case '3':
+                 tempItem = new potion();
+                 break;
+               default:
+                 log("Invalid Item Type %c in %s", buf[0], itemdata);
+                 delete []buf;
+                 return 0;
+                 break;
+               }
+             if(tempItem->setData(buf))
+               {
+                 Items.push_back(tempItem);
+               }
+           }
+       }
+      catch (char *str)
+       {
+         log("Exception loading items: %s", str);
+         delete []buf;
+         return 0;
+       }
+    }
+  delete []buf;
+  return 1;
+}
 int load_gs_dbase()
 {
     ifstream infile;
@@ -2189,16 +2312,11 @@ int load_gs_dbase()
 
     while (infile.getline(buf, 1024, '\n'))
       {
-       item *testitem;
-       testitem = new weapon("Empty Weapon");
-
        temp = new aClient;
        tempname = strtok(buf, " ");
        temp->stats = new Player(tempname);
        p = temp->stats;
 
-       p->inventory->addItem(testitem);
-       
        p->level = stringtoint(strtok(NULL, " "));
        p->exp = stringtoint(strtok(NULL, " "));
        p->gold = stringtoint(strtok(NULL, " "));
@@ -2221,32 +2339,31 @@ int load_gs_dbase()
 #ifdef P10
        temp->setRealNick("Not Playing");
 #endif
-       
-       // Old player databases didn't have these three extra values
-       // If they come up null, leave them to 0 as the default.
-       // On the next gameserv database save, it will save the values.
-       tempname = strtok(NULL, " ");
-       //      if (tempname)
-       //  p->inventory.setHealing(stringtoint(tempname));
-       
-       tempname = strtok(NULL, " ");
-       //if (tempname)
-       //   p->inventory.setStrength(stringtoint(tempname));
-       
-       tempname = strtok(NULL, " ");
-       //if (tempname)
-       //    p->inventory.setDefense(stringtoint(tempname));
-       
-       tempname = strtok(NULL, " ");
-       //if (tempname)
-       //  p->inventory.setHP(stringtoint(tempname));
-       
         tempname = strtok(NULL, " ");
        if (tempname)
          p->lastlogin = stringtoint(tempname);
        else
          p->lastlogin = time(NULL);
        
+       for (tempname = strtok(NULL, " "); tempname != NULL; tempname = strtok(NULL, " "))
+         {
+           long int id, uses;
+           list<item*>::iterator item_iter;
+           id = stringtoint(tempname);
+           tempname = strtok(NULL, " ");
+           uses = stringtoint(tempname);
+
+           item_iter = Items.begin();
+           while (item_iter != Items.end())
+             {
+               if ((*item_iter)->getID() == id)
+                 {
+                   p->inventory->addItem((*item_iter))->setUses(uses);
+                 }
+               item_iter++;
+             }
+         }
+
        unsigned long hv = iHASH((unsigned char *) temp->stats->name.c_str());
        
        temp->stats->client = NULL;
@@ -2559,145 +2676,135 @@ void showinventory(aClient *from, aClient *to)
 }
 void do_tavern(char *u)
 {
-  /*
-    char *cmd = strtok(NULL, " ");
-    long int price;
-
-    aClient *user;
-    Player *p;
-
-    if (!(user = find(u)))
+  char *cmd = strtok(NULL, " ");
+  aClient *user;
+  Player *p;
+  
+  if (!(user = find(u)))
     {
-       notice(s_GameServ, u, "Fatal Error. See a %S admin for help");
-       return;
+      notice(s_GameServ, u, "Fatal Error. See a %S admin for help");
+      return;
     }
-    else if (isIgnore(user))
+  else if (isIgnore(user))
     {
-       #ifdef DEBUGMODE
-           log("Ignoring %s.", user->getNick());
-       #endif
-       return;
+#ifdef DEBUGMODE
+      log("Ignoring %s.", user->getNick());
+#endif
+      return;
     }
-    else if (!is_playing(user))
+  else if (!is_playing(user))
     {
-       notice(s_GameServ, u, "You must be playing to go to the Tavern");
-       return;
+      notice(s_GameServ, u, "You must be playing to go to the Tavern");
+      return;
     }
-    else if (is_fighting(user))
+  else if (is_fighting(user))
     {
-       notice(s_GameServ, u, "You cannot go to the Tavern during a fight!");
-       return;
+      notice(s_GameServ, u, "You cannot go to the Tavern during a fight!");
+      return;
     }
-
-    updateTS(user->stats);
-    p = user->stats;
-
-    if (!cmd)
+  
+  updateTS(user->stats);
+  p = user->stats;
+  
+  if (!cmd)
     {
-       notice(s_GameServ, u, "Welcome to Boot Liquors Mystic Apothecary");
-       notice(s_GameServ, u, "Your commands:");
-       notice(s_GameServ, u, "/msg %S TAVERN {LIST | BUY} [NUMBER]");
-       notice(s_GameServ, u, "What'll it be?");
+      notice(s_GameServ, u, "Welcome to Boot Liquors Mystic Apothecary and General Store");
+      notice(s_GameServ, u, "Your commands:");
+      notice(s_GameServ, u, "/msg %S TAVERN {LIST | BUY} [NUMBER]");
+      notice(s_GameServ, u, "What'll it be?");
     }
-    else if (stricmp(cmd, "LIST") == 0)
+  else if (stricmp(cmd, "LIST") == 0)
     {
-           notice(s_GameServ, u, "Here is a list of what we have to offer:");
-           notice(s_GameServ, u, "1. Healing Potions for %ld Gold", 
-                                       1000 * p->level * 4);
-           notice(s_GameServ, u, "2. Strength Potions for %ld Gold", 
-                                       2500 * p->level * 4);
-           notice(s_GameServ, u, "3. Defense Potions for %ld Gold", 
-                                       3000 * p->level * 4);
-           notice(s_GameServ, u, "4. HP Potions for %ld Gold", 
-                                       2000 * p->level * 4);
-           notice(s_GameServ, u, "To buy a potion, type /msg %S TAVERN BUY #");
-           notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
+      notice(s_GameServ, u, "Here is a list of what we have to offer:");
+      showTavern(user);
+      notice(s_GameServ, u, "To buy an item, type /msg %S TAVERN BUY #");
+      notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
     }
-    else if (stricmp(cmd, "BUY") == 0)
+  /*  else if (stricmp(cmd, "BUY") == 0)
     {
-       char *chnum = strtok(NULL, " ");
-
-       if (!chnum)
+      char *chnum = strtok(NULL, " ");
+      
+      if (!chnum)
        {
-           notice(s_GameServ, u, "SYNTAX: TAVERN BUY #");
-           notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1");
-           return;
+         notice(s_GameServ, u, "SYNTAX: TAVERN BUY #");
+         notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1");
+         return;
        }
-       int num = stringtoint(chnum);
-
-       if (num < 1 || num > 4)
-       {
-           notice(s_GameServ, u, "Invalid Choice!");
-           notice(s_GameServ, u, "Here is a list of what we have to offer:");
-           notice(s_GameServ, u, "1. Healing Potions for %ld Gold", 
-                                       1000 * p->level * 4);
-           notice(s_GameServ, u, "2. Strength Potions for %ld Gold", 
-                                       2500 * p->level * 4);
-           notice(s_GameServ, u, "3. Defense Potions for %ld Gold", 
-                                       3000 * p->level * 4);
-           notice(s_GameServ, u, "4. HP Potions for %ld Gold", 
-                                       2000 * p->level * 4);
-           notice(s_GameServ, u, "To buy a potion, type /msg %S TAVERN BUY #");
-           notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
-           return;
+      int num = stringtoint(chnum);
+      
+      if (num < 1 || num > 4)
+       {
+         notice(s_GameServ, u, "Invalid Choice!");
+         notice(s_GameServ, u, "Here is a list of what we have to offer:");
+         notice(s_GameServ, u, "1. Healing Potions for %ld Gold", 
+                1000 * p->level * 4);
+         notice(s_GameServ, u, "2. Strength Potions for %ld Gold", 
+                2500 * p->level * 4);
+         notice(s_GameServ, u, "3. Defense Potions for %ld Gold", 
+                3000 * p->level * 4);
+         notice(s_GameServ, u, "4. HP Potions for %ld Gold", 
+                2000 * p->level * 4);
+         notice(s_GameServ, u, "To buy a potion, type /msg %S TAVERN BUY #");
+         notice(s_GameServ, u, "Example: /msg %S TAVERN BUY 1 buys a healing potion!");
+         return;
        }
-       switch(num)
+      switch(num)
        {
-           case 1:
-               price = (1000 * p->level * 4);
-               if (p->gold >= price)
-               {
-                   notice(s_GameServ, u, "One healing potion coming right up!");
-                   p->inventory.incHealing();
-                   p->gold -= price;
-               
-               else
-                   notice(s_GameServ, u, "You don't have enough gold!");
-               break;
-           case 2:
-               price = 2500 * p->level * 4;
-               if (p->gold >= price)
-               {
-                   notice(s_GameServ, u, "One strength boost coming right up!");
-                   p->inventory.incStrength();
-                   p->gold -= price;
-               }
-               else
-                   notice(s_GameServ, u, "You don't have enough gold!");
-               break;
-           case 3:
-               price = 3000 * p->level * 4;
-               if (p->gold >= price)
-               {
-                   notice(s_GameServ, u, "One defense boost coming right up!");
-                   p->inventory.incDefense();
-                   p->gold -= price;
-               }
-               else
-                   notice(s_GameServ, u, "You don't have enough gold!");
-               break;
-           case 4:
-               price = 3000 * p->level * 4;
-               if (p->gold >= price)
-               {
-                   notice(s_GameServ, u, "One HP Potion coming right up!");
-                   p->inventory.incHP();
-                   p->gold -= price;
-               }
-               else
-                   notice(s_GameServ, u, "You don't have enough gold!");
-               break;
-           default:
-               notice(s_GameServ, u, "Logical Error. See a %S admin for help!");
-               break;
+       case 1:
+         price = (1000 * p->level * 4);
+         if (p->gold >= price)
+           {
+             notice(s_GameServ, u, "One healing potion coming right up!");
+             p->inventory.incHealing();
+             p->gold -= price;
+           } 
+         else
+           notice(s_GameServ, u, "You don't have enough gold!");
+         break;
+       case 2:
+         price = 2500 * p->level * 4;
+         if (p->gold >= price)
+           {
+             notice(s_GameServ, u, "One strength boost coming right up!");
+             p->inventory.incStrength();
+             p->gold -= price;
+           }
+         else
+           notice(s_GameServ, u, "You don't have enough gold!");
+         break;
+       case 3:
+         price = 3000 * p->level * 4;
+         if (p->gold >= price)
+           {
+             notice(s_GameServ, u, "One defense boost coming right up!");
+             p->inventory.incDefense();
+             p->gold -= price;
+           }
+         else
+           notice(s_GameServ, u, "You don't have enough gold!");
+         break;
+       case 4:
+         price = 3000 * p->level * 4;
+         if (p->gold >= price)
+           {
+             notice(s_GameServ, u, "One HP Potion coming right up!");
+             p->inventory.incHP();
+             p->gold -= price;
+           }
+         else
+           notice(s_GameServ, u, "You don't have enough gold!");
+         break;
+       default:
+         notice(s_GameServ, u, "Logical Error. See a %S admin for help!");
+         break;
        }
     }
-    else
+  */
+  else
     {
-       notice(s_GameServ, u, "Improper Syntax.");
-       notice(s_GameServ, u, "Type /msg %S HELP TAVERN for help");
+      notice(s_GameServ, u, "Improper Syntax.");
+      notice(s_GameServ, u, "Type /msg %S HELP TAVERN for help");
     }
-  */
   return;
 }
 
@@ -3082,6 +3189,37 @@ void see_master(char *u)
     }
 }
 
+void showTavern(aClient *user)
+{
+  Player *p;
+  list<tavernItem>::iterator item_iterator;
+  item *tempItem;
+
+  p = user->stats;
+
+  if (!p)
+    return;
+
+  item_iterator = tavern.begin();
+  if (tavern.empty())
+    {
+      notice(s_GameServ, user->getNick(), "Tavern is empty");
+      return;
+    }
+
+  while (item_iterator != tavern.end())
+    {
+      if (p->level < (*item_iterator).getLevel())
+       {
+         item_iterator++;
+         continue;
+       }
+      tempItem = (*item_iterator).getItem();
+      notice(s_GameServ, user->getNick(), "%d. %s for %ld gold", tempItem->getID(), tempItem->getName().c_str(), tempItem->price());
+      item_iterator++;
+    }
+}
+
 void showBankBalance(const char *u)
 {
     aClient *user;
@@ -3562,3 +3700,20 @@ bool load_monsters()
     delete [] buf;
 return true;
 }
+
+item *findItemByID(int id)
+{
+  list<item*>::iterator item_iterator;
+
+  item_iterator = Items.begin();
+
+  while (item_iterator != Items.end())
+    {
+      if ((*item_iterator)->getID() == id)
+       {
+         return (*item_iterator);
+       }
+      item_iterator++;
+    }
+  return NULL;
+}