]> jfr.im git - irc/gameservirc.git/blobdiff - gameserv/level.cpp
added items to the tavern.dat, added the filename option to the config file
[irc/gameservirc.git] / gameserv / level.cpp
index 98cb40a7cbe0a4609dc9461c048255e1cdbb680a..7aa9472bdb5cfd7f559514d25b5f490afdc330f6 100644 (file)
@@ -1,6 +1,27 @@
+#include "item.h"
+#include "extern.h"
+#include <fstream>
 #include "level.h"
+#include <stdlib.h>
+#include <string.h>
 
-level::level()
+using std::ifstream;
+
+long int range::random()
+{
+    if (high == 0 || high <= low)
+       return 0;
+    else
+       return low + rand() % (high - low);
+}
+
+void range::setRange(char *r)
+{
+    low = stringtoint(strtok(r, "~"));
+    high = stringtoint(strtok(NULL, ""));
+}
+
+Level::Level()
 {
     strength.high = 0;
     strength.low = 0;
@@ -15,26 +36,71 @@ level::level()
     hp.low = 0;
 }
 
-void level::setStrength(range &s)
+void Level::setStrength(range &s)
 {
     strength.high = s.high;
     strength.low = s.low;
 }
 
-void level::setGold(range &g)
+void Level::setGold(range &g)
 {
     gold.high = g.high;
     gold.low = g.low;
 }
 
-void level::setExp(range &e)
+void Level::setExp(range &e)
 {
     exp.high = e.high;
     exp.low = e.low;
 }
 
-void level::setHP(range &h)
+void Level::setHP(range &h)
 {
     hp.high = h.high;
     hp.low = h.low;
 }
+
+Monster *Level::randomMonster()
+{
+    Monster *ptr;
+    ptr = monsters.random();
+    ptr->strength = strength.random();
+    ptr->gold = gold.random();
+    ptr->exp = exp.random();
+    ptr->hp = hp.random();
+    ptr->maxhp = ptr->hp;
+    return ptr;
+}
+
+bool Level::loadLevel(char *filename)
+{
+    ifstream infile(filename);
+    char *buf;
+
+    #ifdef DEBUGMODE
+       log("Attempting to open %s", filename);
+    #endif
+
+    if (infile.fail())
+       return false;
+
+    buf = new char[32];
+
+    for (int x = 0;infile.getline(buf, 32, '\n'); x++)
+    {
+       if (buf[0] == '^')
+           break;
+       else if (x == 0)
+       {
+           strength.setRange(buf);
+       }
+       else if (x == 1)
+           gold.setRange(buf);
+       else if (x == 2)
+           exp.setRange(buf);
+       else if (x == 3)
+           hp.setRange(buf);
+    }
+    delete []buf;
+    return true;
+}