]> jfr.im git - irc/gameservirc.git/blame - gameserv/level.cpp
completed migrating all lists over to STL list
[irc/gameservirc.git] / gameserv / level.cpp
CommitLineData
c10b78ac 1
26b17386 2#include "item.h"
ea93c39a 3#include "extern.h"
4#include <fstream>
e99a4410 5#include "level.h"
ea93c39a 6#include <stdlib.h>
7#include <string.h>
8
9using std::ifstream;
10
11long int range::random()
12{
13 if (high == 0 || high <= low)
14 return 0;
15 else
16 return low + rand() % (high - low);
17}
18
19void range::setRange(char *r)
20{
21 low = stringtoint(strtok(r, "~"));
22 high = stringtoint(strtok(NULL, ""));
23}
e99a4410 24
42106907 25Level::Level()
e99a4410 26{
27 strength.high = 0;
28 strength.low = 0;
29
30 gold.high = 0;
31 gold.low = 0;
32
33 exp.high = 0;
34 exp.low = 0;
35
36 hp.high = 0;
37 hp.low = 0;
38}
39
42106907 40void Level::setStrength(range &s)
e99a4410 41{
42 strength.high = s.high;
43 strength.low = s.low;
44}
45
42106907 46void Level::setGold(range &g)
e99a4410 47{
48 gold.high = g.high;
49 gold.low = g.low;
50}
51
42106907 52void Level::setExp(range &e)
e99a4410 53{
54 exp.high = e.high;
55 exp.low = e.low;
56}
57
42106907 58void Level::setHP(range &h)
e99a4410 59{
60 hp.high = h.high;
61 hp.low = h.low;
62}
42106907 63
ea93c39a 64Monster *Level::randomMonster()
42106907 65{
c10b78ac 66 int num, x;
67 Monster *ptr;
68 list<Monster*>::iterator iter;
69
70 num = rand() % monsters.size();
71 for (x = 0, iter = monsters.begin(); x < num; x++)
72 {
73 iter++;
74 }
75
76 ptr = (*iter);
77 ptr->strength = strength.random();
78 ptr->gold = gold.random();
79 ptr->exp = exp.random();
80 ptr->hp = hp.random();
81 ptr->maxhp = ptr->hp;
ea93c39a 82 return ptr;
83}
84
85bool Level::loadLevel(char *filename)
86{
87 ifstream infile(filename);
88 char *buf;
89
90 #ifdef DEBUGMODE
91 log("Attempting to open %s", filename);
92 #endif
93
94 if (infile.fail())
95 return false;
96
97 buf = new char[32];
98
99 for (int x = 0;infile.getline(buf, 32, '\n'); x++)
100 {
101 if (buf[0] == '^')
102 break;
103 else if (x == 0)
104 {
105 strength.setRange(buf);
106 }
107 else if (x == 1)
108 gold.setRange(buf);
109 else if (x == 2)
110 exp.setRange(buf);
111 else if (x == 3)
112 hp.setRange(buf);
113 }
114 delete []buf;
115 return true;
42106907 116}