-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.cpp
More file actions
155 lines (129 loc) · 3.7 KB
/
Copy pathFile.cpp
File metadata and controls
155 lines (129 loc) · 3.7 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include "File.h"
File::File()
{
}
File::~File()
{
}
Room*** File::getMap() {
//Read settings file
ifstream t("settings.txt");
str.assign((istreambuf_iterator<char>(t)),
istreambuf_iterator<char>());
if (str == "") {
system("CLS");
cout << "Settings file not found...." << endl;
system("PAUSE");
exit(1);
}
//Generate map size
vector<string> mapsize = splitString(str.substr(str.find("MAPSIZE"), str.find('\n')));
Room*** map = new Room**[stoi(mapsize.at(1))];
for (int i = 0; i < stoi(mapsize.at(1)); i++) {
map[i] = new Room*[stoi(mapsize.at(2))];
for (int y = 0; y < stoi(mapsize.at(2)); y++) {
map[i][y] = nullptr;
}
}
//Generate room
vector <string> roomsString = findAllLines(str, "ROOM");
vector <string> roomString;
int roomX = 0;
int roomY = 0;
for (int i = 0; i < roomsString.size(); i++) {
roomString = splitString(roomsString.at(i), ";");
roomX = stoi(roomString.at(1));
roomY = stoi(roomString.at(2));
Room* room;
if (roomString.at(0) == "ROOMFIGHT") {
room = new RoomFight();
Monster* monster = new Monster(stoi(roomString.at(11)));
monster->setMonsterName(roomString.at(10));
monster->setWeapon(findItem(stoi(roomString.at(12))));
room->addMonster(monster);
monster = nullptr;
}
else if (roomString.at(0) == "ROOMLOOT") {
room = new RoomLoot();
vector<string> items;
items = splitString(roomString.at(10), ",");
for (int y = 0; y < items.size(); y++) {
room->addItem(findItem(stoi(items.at(y))));
}
}
else {
room = new Room();
}
room->setDescription(roomString.at(5));
room->setInitText(roomString.at(4));
room->setLabel(roomString.at(3));
room->setStatusDoor("top", stoi(roomString.at(6)));
room->setStatusDoor("right", stoi(roomString.at(7)));
room->setStatusDoor("bottom", stoi(roomString.at(8)));
room->setStatusDoor("left", stoi(roomString.at(9)));
map[roomX][roomY] = room;
}
return map;
}
int* File::getStartPos() {
int returnVal[2];
vector<string> startpos = splitString(str.substr(str.find("STARTPOS"), str.find('\n')));
returnVal[0] = stoi(startpos.at(1));
returnVal[1] = stoi(startpos.at(2));
return returnVal;
}
vector<string> findAllLines(string str, string sub) {
vector<size_t> positions;
vector<string> returnVec;
size_t pos = str.find(sub, 0);
while (pos != string::npos)
{
positions.push_back(pos);
pos = str.find(sub, pos + 1);
}
for (int i = 0; i < positions.size(); i++) {
returnVec.push_back(str.substr(positions.at(i), str.substr(positions.at(i)).find("\n")));
}
return returnVec;
}
vector<string> splitString(string str, string delimiter) {
size_t pos = 0;
string token;
vector<string> returnVec;
while ((pos = str.find(delimiter)) != string::npos) {
token = str.substr(0, pos);
returnVec.push_back(token);
str.erase(0, pos + delimiter.length());
}
return returnVec;
}
Item* File::findItem(int id) {
vector <string> itemsString = findAllLines(str, "ITEM;");
vector <string> itemString;
Item* items;
for (int i = 0; i < itemsString.size(); i++) {
itemString = splitString(itemsString.at(i));
if (stoi(itemString.at(1)) == id) {
Item* item;
if (itemString.at(2) == "ITEMWEAPON") {
item = new ItemWeapon();
item->setName(itemString.at(5));
item->setDmg(stoi(itemString.at(3)), stoi(itemString.at(4)));
}
else {
item = new ItemHealth();
item->setName(itemString.at(4));
item->setHealthChange(stoi(itemString.at(3)));
}
return item;
}
}
}
int File::getMapSizeX() {
vector<string> mapsize = splitString(str.substr(str.find("MAPSIZE"), str.find('\n')));
return stoi(mapsize.at(1));
}
int File::getMapSizeY() {
vector<string> mapsize = splitString(str.substr(str.find("MAPSIZE"), str.find('\n')));
return stoi(mapsize.at(2));
}