]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/item.cpp
still working on stuff for the items classes
[irc/gameservirc.git] / gameserv / item.cpp
index d23620131c596f61fa8070070228cea3e482fa41..5532ba3175be9014e15404c9f35fbc34781d66b5 100644 (file)
@@ -1,5 +1,6 @@
 #include "item.h"
 #include "player.h"
+#include "extern.h"
 
 item::item()
 {
@@ -10,7 +11,7 @@ item::item()
     mymodifiers[x] = 0;
 }
 
-item::item(const char *name, long int p, int uses, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8)
+item::item(const char *name, long int p, int uses, long int identifier, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8)
 {
   myname = name; // string = char*
   myprice = p;
@@ -23,9 +24,10 @@ item::item(const char *name, long int p, int uses, int m1, int m2, int m3, int m
   mymodifiers[5] = m6;
   mymodifiers[6] = m7;
   mymodifiers[7] = m8;
+  id = identifier;
 }
 
-item::item(string name, long int p, int uses, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8)
+item::item(string name, long int p, int uses, long int identifier, int m1, int m2, int m3, int m4, int m5, int m6, int m7, int m8)
 {
   myname = name; // string = char*
   myprice = p;
@@ -38,10 +40,24 @@ item::item(string name, long int p, int uses, int m1, int m2, int m3, int m4, in
   mymodifiers[5] = m6;
   mymodifiers[6] = m7;
   mymodifiers[7] = m8;
+  id = identifier;
 }
 
 item::~item()
 {
+  for (int x = 0; x < 8; x++)
+    {
+      mymodifiers[x] = 0;
+    }
+
+  myuses = 0;
+  myname = "";
+  myprice = 0;
+}
+
+void item::setType(type t)
+{
+  mytype = t;
 }
 
 bool item::operator<(const item &right) const
@@ -64,6 +80,71 @@ 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;
+      // Get the weapon type, but default to WEAPON since this is a weapon
+      temp = strtok(datastr, "~");
+
+      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 were successful
+      return true;
+    }
+  catch (char *str)
+    {
+      log("Exception: %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)
@@ -90,6 +171,10 @@ void weapon::undo(Player *p)
   p->maxhp -= mymodifiers[2];
 }
 
+armor::~armor()
+{
+}
+
 void armor::undo(Player *p)
 {
   p->strength -= mymodifiers[0];
@@ -97,6 +182,53 @@ void armor::undo(Player *p)
   p->maxhp -= mymodifiers[2];
 }
 
+bool armor::setData(char *datastr)
+{
+  char *temp;
+
+  // Get the weapon type, but default to WEAPON since this is a weapon
+  temp = strtok("~", datastr);
+  if (!temp)
+    return false;
+  mytype = ARMOR;
+
+  // Grab the item's id
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  id = stringtoint(temp);
+
+  // Grab the item's name
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myname = temp;
+
+  // Grab the item's price
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myprice = stringtoint(temp);
+
+  // Grab the item's uses
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myuses = stringtoint(temp);
+
+  // Grab the item's modifiers
+  for (int x = 0; x < 5; x++)
+    {
+      temp = strtok("~", NULL);
+      if (!temp)
+       return false;
+      mymodifiers[x] = stringtoint(temp);
+    }
+
+  // If we got here, we were successful
+  return true;
+}
+
 bool armor::use(Player *p)
 {
   //  weapon(char *name, int p=0, int uses = -1, int strength=0, int defense=0, int maxhp=0)
@@ -145,3 +277,129 @@ void potion::undo(Player *p)
 {
   return;
 }
+
+bool potion::setData(char *datastr)
+{
+  char *temp;
+
+  // Get the weapon type, but default to WEAPON since this is a weapon
+  temp = strtok("~", datastr);
+  if (!temp)
+    return false;
+  mytype = ARMOR;
+
+  // Grab the item's id
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  id = stringtoint(temp);
+
+  // Grab the item's name
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myname = temp;
+
+  // Grab the item's price
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myprice = stringtoint(temp);
+
+  // Grab the item's uses
+  temp = strtok("~", NULL);
+  if (!temp)
+    return false;
+  myuses = stringtoint(temp);
+
+  // Grab the item's modifiers
+  for (int x = 0; x < 5; x++)
+    {
+      temp = strtok("~", NULL);
+      if (!temp)
+       return false;
+      mymodifiers[x] = stringtoint(temp);
+    }
+
+  // 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);
+}