X-Git-Url: https://jfr.im/git/irc/gameservirc.git/blobdiff_plain/e99a4410ab7729b12c47cf2b2d11289cf6d30842..f97dea8f504a92efa45c753fc693d29380459c2a:/gameserv/level.cpp diff --git a/gameserv/level.cpp b/gameserv/level.cpp index 98cb40a..d17ae3a 100644 --- a/gameserv/level.cpp +++ b/gameserv/level.cpp @@ -1,6 +1,26 @@ +#include "extern.h" +#include #include "level.h" +#include +#include -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 +35,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; +}