]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/item.cpp
added items to the tavern.dat, added the filename option to the config file
[irc/gameservirc.git] / gameserv / item.cpp
index 3c30eac63ace1c902f7f0f438a1e605d0851d079..45827accc5d030ed452607c39c02f88c29d2f62e 100644 (file)
@@ -1,5 +1,6 @@
 #include "item.h"
 #include "player.h"
+#include "extern.h"
 
 item::item()
 {
@@ -79,10 +80,64 @@ bool item::operator!=(const item &right) const
   return myname != right.myname;
 }
 
+item &item::operator=(const item &right)
+{
+  myname = right.myname;
+  myprice = right.myprice;
+  myuses = right.myuses;
+  id = right.id;
+  mytype = right.mytype;
+
+  for (int x = 0; x < 8; x++)
+    {
+      mymodifiers[x] = right.mymodifiers[x];
+    }
+  return *this; // enables cascading x=y=z;
+}
 weapon::~weapon()
 {
 }
 
+bool weapon::setData(char *datastr)
+{
+  try
+    {
+      char *temp;
+      temp = strtok(datastr, "~"); // Type
+
+      mytype = WEAPON;
+      // Grab the item's id
+      temp = strtok(NULL, "~");
+      id = stringtoint(temp);
+      
+      // Grab the item's name
+      temp = strtok(NULL, "~");
+      myname = temp;
+      
+      // Grab the item's price
+      temp = strtok(NULL, "~");
+      myprice = stringtoint(temp);
+      
+      // Grab the item's uses
+      temp = strtok(NULL, "~");
+      myuses = stringtoint(temp);
+      
+      // Grab the item's modifiers
+      for (int x = 0; x < 4; x++)
+       {
+         temp = strtok(NULL, "~");
+         mymodifiers[x] = stringtoint(temp);
+       }
+      // If we got here, we're successful
+      return true;
+    }
+  catch (char *str)
+    {
+      log("Exception setting weapon data: %s", str);
+      return false;
+    }
+}
+
 bool weapon::use(Player *p)
 {
   //  weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0)
@@ -109,12 +164,6 @@ void weapon::undo(Player *p)
   p->maxhp -= mymodifiers[2];
 }
 
-bool weapon::setData(const char *dataStr)
-{
-  
-  return true;
-}
-
 armor::~armor()
 {
 }
@@ -126,9 +175,46 @@ void armor::undo(Player *p)
   p->maxhp -= mymodifiers[2];
 }
 
-bool armor::setData(const char *dataStr)
+bool armor::setData(char *datastr)
 {
-  return true;
+  try
+    {
+      char *temp;
+      strtok(datastr, "~"); // Type
+
+      mytype = ARMOR;
+
+      // Grab the item's id
+      temp = strtok(NULL, "~");
+      id = stringtoint(temp);
+
+      // Grab the item's name
+      temp = strtok(NULL, "~");
+      myname = temp;
+      
+      // Grab the item's price
+      temp = strtok(NULL, "~");
+      myprice = stringtoint(temp);
+      
+      // Grab the item's uses
+      temp = strtok(NULL, "~");
+      myuses = stringtoint(temp);
+      
+      // Grab the item's modifiers
+      for (int x = 0; x < 4; x++)
+       {
+         temp = strtok(NULL, "~");
+         mymodifiers[x] = stringtoint(temp);
+       }
+      // If we got here, we were successful
+      return true;
+    }
+  catch(char *str)
+    {
+      log("Exception setting armor data: %s", str);
+      return false;
+    }
+
 }
 
 bool armor::use(Player *p)
@@ -150,6 +236,10 @@ bool armor::use(Player *p)
   return true;
 }
 
+potion::~potion()
+{
+}
+
 bool potion::use(Player *p)
 {
   // potion(char *name, int p=0, int uses = 1, int strength=0, int defense=0, int maxhp=0, int hp=0, int forest_fights=0, int player_fights=0, int gold=0, int bank=0)
@@ -158,14 +248,14 @@ bool potion::use(Player *p)
     return false;
   else
     {
-      p->strength += mymodifiers[0];
-      p->defense += mymodifiers[1];
-      p->maxhp += mymodifiers[2];
-      p->hp += mymodifiers[3];
-      p->forest_fights += mymodifiers[4];
-      p->player_fights += mymodifiers[5];
-      p->gold += mymodifiers[6];
-      p->bank += mymodifiers[7];
+      p->strength += myranges[0].random();
+      p->defense += myranges[1].random();
+      p->maxhp += myranges[2].random();
+      p->hp += myranges[3].random();
+      p->forest_fights += myranges[4].random();
+      p->player_fights += myranges[5].random();
+      p->gold += myranges[6].random();
+      p->bank += myranges[7].random();
     }
 
   if (myuses > 0)
@@ -180,8 +270,129 @@ void potion::undo(Player *p)
   return;
 }
 
-bool potion::setData(const char *dataStr)
+bool potion::setData(char *datastr)
 {
-  
+  try
+    {
+      char *temp;
+
+      temp = strtok(datastr, "~"); // Type
+
+      mytype = ARMOR;
+      
+      // Grab the item's id
+      temp = strtok(NULL, "~");
+      id = stringtoint(temp);
+      
+      // Grab the item's name
+      temp = strtok(NULL, "~");
+      myname = temp;
+      
+      // Grab the item's price
+      temp = strtok(NULL, "~");
+      myprice = stringtoint(temp);
+      
+      // Grab the item's uses
+      temp = strtok(NULL, "~");
+      myuses = stringtoint(temp);
+      
+      // Grab the item's modifiers
+      for (int x = 0; x < 8; x++)
+       {
+         temp = strtok(NULL, "~");
+         myranges[x].low = stringtoint(temp);
+         temp = strtok(NULL, "~");
+         myranges[x].high = stringtoint(temp);
+       }
+    }
+  catch(char *str)
+    {
+      log("Exception setting potion data: %s", str);
+      return false;
+    }
+  // If we got here, we were successful
   return true;
 }
+
+
+itemContainer::itemContainer()
+{
+  myuses = 0;
+  myitem = NULL;
+}
+
+itemContainer::itemContainer(item *i){
+  myuses = i->uses();
+  myitem = i;
+}
+itemContainer::itemContainer(const itemContainer &right)
+{
+  myuses = right.myuses;
+  myitem = right.myitem;
+}
+itemContainer::~itemContainer()
+{
+  myuses = 0;
+  myitem = NULL;
+}
+
+itemContainer &itemContainer::operator--()
+{
+  --myuses;
+  return *this;
+}
+
+itemContainer itemContainer::operator--(int)
+{
+  itemContainer oldValue = *this;
+
+  operator--();
+
+  return oldValue;
+}
+
+itemContainer &itemContainer::operator++()
+{
+  ++myuses;
+
+  return *this;
+}
+
+itemContainer itemContainer::operator++(int)
+{
+  itemContainer oldValue = *this;
+
+  operator++();
+
+  return oldValue;
+}
+
+void itemContainer::setItem(item *i)
+{
+  myitem = i;
+}
+
+bool itemContainer::operator<(const itemContainer &right) const
+{
+  return (*myitem < *right.myitem);
+}
+
+bool itemContainer::operator>(const itemContainer &right) const
+{
+  return (*myitem > *right.myitem);
+}
+
+bool itemContainer::operator==(const itemContainer &right) const
+{
+  return (*myitem == *right.myitem);
+}
+
+bool itemContainer::operator!=(const itemContainer &right) const
+{
+  return (*myitem == *right.myitem);
+}
+
+void itemContainer::setUses(int uses)
+{
+  myuses = uses;
+}