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