]>
Commit | Line | Data |
---|---|---|
1 | #include "extern.h" | |
2 | #include <fstream> | |
3 | #include "level.h" | |
4 | #include <stdlib.h> | |
5 | #include <string.h> | |
6 | ||
7 | using std::ifstream; | |
8 | ||
9 | long int range::random() | |
10 | { | |
11 | if (high == 0 || high <= low) | |
12 | return 0; | |
13 | else | |
14 | return low + rand() % (high - low); | |
15 | } | |
16 | ||
17 | void range::setRange(char *r) | |
18 | { | |
19 | low = stringtoint(strtok(r, "~")); | |
20 | high = stringtoint(strtok(NULL, "")); | |
21 | } | |
22 | ||
23 | Level::Level() | |
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 | ||
38 | void Level::setStrength(range &s) | |
39 | { | |
40 | strength.high = s.high; | |
41 | strength.low = s.low; | |
42 | } | |
43 | ||
44 | void Level::setGold(range &g) | |
45 | { | |
46 | gold.high = g.high; | |
47 | gold.low = g.low; | |
48 | } | |
49 | ||
50 | void Level::setExp(range &e) | |
51 | { | |
52 | exp.high = e.high; | |
53 | exp.low = e.low; | |
54 | } | |
55 | ||
56 | void Level::setHP(range &h) | |
57 | { | |
58 | hp.high = h.high; | |
59 | hp.low = h.low; | |
60 | } | |
61 | ||
62 | Monster *Level::randomMonster() | |
63 | { | |
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 | ||
74 | bool 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; | |
105 | } |