forked from cameronmoreau/MUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorldLoader.cpp
77 lines (72 loc) · 1.8 KB
/
WorldLoader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include "WorldLoader.h"
#include <fstream>
#include <sstream>
#include <cstdlib>
//CWH: moved constant initalization to the implementation
const std::string WorldLoader::atlasPath = "res/world/world_atlas.txt";
const std::string WorldLoader::worldsPath = "res/world/worlds.txt";
World WorldLoader::getWorld(int id) {
//CWH: There are some good uses of the comma operator, but it is usually more trouble than it is worth
//I have a rule of thumb that I generally don't bother using it unless I have a good reason
string name, temp, temp2;
World world;
ifstream file;
int counter = 0;
char map[8][15];
file.open(worldsPath.c_str());
while (!file.eof())
{
counter++;
getline(file, temp);
if (counter == 1)
{
stringstream ss(temp);
getline(ss, name, ':');
if (name == to_string(id)) {
getline(ss, name, ':');
for (int y = 0; y < 8; y++)
{
getline(file, temp2);
for (int x = 0; x < 15; x++)
{
map[y][x] = temp2[x];
}
}
WorldLinks wl = setWorldLinks(id);
world = World(id, name, wl.n, wl.e, wl.s, wl.w, map);
file.seekg(0, ios_base::end);
}
}
else if (counter == 9)
{
counter = 0;
}
}
file.close();
return world;
}
WorldLinks WorldLoader::setWorldLinks(int id) {
WorldLinks wl;
string temp, temp2, temp3;
ifstream file;
file.open(atlasPath.c_str());
while (!file.eof()) {
getline(file, temp);
stringstream ss(temp);
getline(ss, temp2, ':');
if (temp2 == to_string(id)) {
getline(ss, temp2, ':');
stringstream ss(temp2);
getline(ss, temp3, ',');
wl.n = atoi(temp3.c_str());
getline(ss, temp3, ',');
wl.e = atoi(temp3.c_str());
getline(ss, temp3, ',');
wl.s = atoi(temp3.c_str());
getline(ss, temp3, ',');
wl.w = atoi(temp3.c_str());
file.seekg(0, ios_base::end);
}
}
return wl;
}