]> jfr.im git - irc/gameservirc.git/blob - gameserv/level.cpp
updated the Change log w/ new additions
[irc/gameservirc.git] / gameserv / level.cpp
1
2 #include "item.h"
3 #include "extern.h"
4 #include <fstream>
5 #include "level.h"
6 #include <stdlib.h>
7 #include <string.h>
8
9 using std::ifstream;
10
11 long int range::random()
12 {
13 if (high == 0 || high <= low)
14 return 0;
15 else
16 return low + rand() % (high - low);
17 }
18
19 void range::setRange(char *r)
20 {
21 low = stringtoint(strtok(r, "~"));
22 high = stringtoint(strtok(NULL, ""));
23 }
24
25 Level::Level()
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
40 void Level::setStrength(range &s)
41 {
42 strength.high = s.high;
43 strength.low = s.low;
44 }
45
46 void Level::setGold(range &g)
47 {
48 gold.high = g.high;
49 gold.low = g.low;
50 }
51
52 void Level::setExp(range &e)
53 {
54 exp.high = e.high;
55 exp.low = e.low;
56 }
57
58 void Level::setHP(range &h)
59 {
60 hp.high = h.high;
61 hp.low = h.low;
62 }
63
64 Monster *Level::randomMonster()
65 {
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;
82 return ptr;
83 }
84
85 bool 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;
116 }