]>
Commit | Line | Data |
---|---|---|
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 | ||
3cadd0c3 | 9 | using namespace std; |
ea93c39a | 10 | |
11 | long int range::random() | |
12 | { | |
5de7a0b0 | 13 | if (high == 0 || high <= low) |
ea93c39a | 14 | return 0; |
5de7a0b0 | 15 | else |
ea93c39a | 16 | return low + rand() % (high - low); |
17 | } | |
18 | ||
19 | void range::setRange(char *r) | |
20 | { | |
5de7a0b0 | 21 | low = stringtoint(strtok(r, "~")); |
22 | high = stringtoint(strtok(NULL, "")); | |
ea93c39a | 23 | } |
e99a4410 | 24 | |
42106907 | 25 | Level::Level() |
e99a4410 | 26 | { |
5de7a0b0 | 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; | |
e99a4410 | 38 | } |
39 | ||
42106907 | 40 | void Level::setStrength(range &s) |
e99a4410 | 41 | { |
5de7a0b0 | 42 | strength.high = s.high; |
43 | strength.low = s.low; | |
e99a4410 | 44 | } |
45 | ||
42106907 | 46 | void Level::setGold(range &g) |
e99a4410 | 47 | { |
5de7a0b0 | 48 | gold.high = g.high; |
49 | gold.low = g.low; | |
e99a4410 | 50 | } |
51 | ||
42106907 | 52 | void Level::setExp(range &e) |
e99a4410 | 53 | { |
5de7a0b0 | 54 | exp.high = e.high; |
55 | exp.low = e.low; | |
e99a4410 | 56 | } |
57 | ||
42106907 | 58 | void Level::setHP(range &h) |
e99a4410 | 59 | { |
5de7a0b0 | 60 | hp.high = h.high; |
61 | hp.low = h.low; | |
e99a4410 | 62 | } |
42106907 | 63 | |
ea93c39a | 64 | Monster *Level::randomMonster() |
42106907 | 65 | { |
c10b78ac | 66 | int num, x; |
67 | Monster *ptr; | |
68 | list<Monster*>::iterator iter; | |
5de7a0b0 | 69 | |
c10b78ac | 70 | num = rand() % monsters.size(); |
71 | for (x = 0, iter = monsters.begin(); x < num; x++) | |
72 | { | |
73 | iter++; | |
74 | } | |
5de7a0b0 | 75 | |
c10b78ac | 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; | |
5de7a0b0 | 82 | return ptr; |
ea93c39a | 83 | } |
84 | ||
85 | bool Level::loadLevel(char *filename) | |
86 | { | |
5de7a0b0 | 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()) | |
ea93c39a | 95 | return false; |
5de7a0b0 | 96 | |
97 | buf = new char[32]; | |
98 | ||
99 | for (int x = 0;infile.getline(buf, 32, '\n'); x++) | |
ea93c39a | 100 | { |
5de7a0b0 | 101 | if (buf[0] == '^') |
ea93c39a | 102 | break; |
5de7a0b0 | 103 | else if (x == 0) |
104 | { | |
105 | strength.setRange(buf); | |
106 | } | |
107 | else if (x == 1) | |
ea93c39a | 108 | gold.setRange(buf); |
5de7a0b0 | 109 | else if (x == 2) |
ea93c39a | 110 | exp.setRange(buf); |
5de7a0b0 | 111 | else if (x == 3) |
ea93c39a | 112 | hp.setRange(buf); |
113 | } | |
5de7a0b0 | 114 | delete []buf; |
115 | return true; | |
42106907 | 116 | } |