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