-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsing.cpp
258 lines (193 loc) · 6.25 KB
/
parsing.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include "core.h"
void object_info::load(const std::string & value, const std::string & variable)
{
if(variable == "name")
name = value;
else if(variable == "text")
text = value;
else if(variable == "life")
life = std::stoi(value);
else if(variable == "armor")
armor = std::stoi(value);
else if(variable == "influence")
influence = std::stod(value);
}
void object_info::parse_file(std::ifstream & file)
{
while(!file.eof())
{
std::string line;
getline(file, line);
std::string variable, value;
size_t equality_index = line.find('=');
if(equality_index == std::string::npos)
continue;
variable = line.substr(0, equality_index);
value = line.substr(equality_index + 1);
remove_spaces_and_apostrophs(variable);
remove_spaces_and_apostrophs(value);
this->load(value, variable);
}
}
void object_info::remove_spaces_and_apostrophs(std::string & word)
{
int start_index = 0;
while((word[start_index] == ' ') || (word[start_index] == '\t'))
start_index++;
int end_index = word.size() - 1;
while((word[end_index] == ' ') || (word[end_index] == '\t'))
end_index--;
if(word[start_index] == '"')
start_index++;
if(word[end_index] == '"')
end_index--;
word = word.substr(start_index, end_index - start_index + 1);
}
/**
* \brief Loads informations about buildings or people from configurations file
* Can throw exception.
* \param INFO_TYPE: building_info or people_info
* \param DATABASE_TYPE: building_names or people_names - generally some constant database (see constant_database.h) with names and images of objects
* \param ENUM_TYPE: building_type of people_type
*/
template <typename INFO_TYPE, typename DATABASE_TYPE, typename ENUM_TYPE>
std::vector<INFO_TYPE> object_info::load_info(int number_of_enums, std::string folder_name)
{
std::vector<INFO_TYPE> info_vector;
INFO_TYPE info;
std::string file_name;
for(int i=0; i<number_of_enums; ++i)
{
try
{
std::string name_from_database = std::get<1>(find<DATABASE_TYPE, 0>(static_cast<ENUM_TYPE>(i)));
file_name = folder_name + name_from_database + ".conf";
}
catch (std::exception ex)
{
LOG("error");
throw ex;
}
std::ifstream file(file_name, std::ios::in);
if(!file)
{
LOG("error - cant open file " + file_name);
throw std::exception();
}
INFO_TYPE info;
info.image = std::get<2>(find<DATABASE_TYPE, 0>(static_cast<ENUM_TYPE>(i)));
info.parse_file(file);
info_vector.push_back(info);
}
return info_vector;
}
void building_info::load(const std::string & value, const std::string & variable)
{
object_info::load(value, variable);
if(variable == "building_wood")
building_price[WOOD] = std::stoi(value);
else if(variable == "building_stone")
building_price[STONE] = std::stoi(value);
else if(variable == "building_marble")
building_price[MARBLE] = std::stoi(value);
else if(variable == "building_bricks")
building_price[BRICKS] = std::stoi(value);
if(variable == "first_wood") //first upgrade
first_upgrade_price[WOOD] = std::stoi(value);
else if(variable == "first_stone")
first_upgrade_price[STONE] = std::stoi(value);
else if(variable == "first_marble")
first_upgrade_price[MARBLE] = std::stoi(value);
else if(variable == "first_bricks")
first_upgrade_price[BRICKS] = std::stoi(value);
if(variable == "second_wood") //second upgrade
second_upgrade_price[WOOD] = std::stoi(value);
else if(variable == "second_stone")
second_upgrade_price[STONE] = std::stoi(value);
else if(variable == "second_marble")
second_upgrade_price[MARBLE] = std::stoi(value);
else if(variable == "second_bricks")
second_upgrade_price[BRICKS] = std::stoi(value);
if(variable == "third_wood") //third_upgrade
third_upgrade_price[WOOD] = std::stoi(value);
else if(variable == "third_stone")
third_upgrade_price[STONE] = std::stoi(value);
else if(variable == "third_marble")
third_upgrade_price[MARBLE] = std::stoi(value);
else if(variable == "third_bricks")
third_upgrade_price[BRICKS] = std::stoi(value);
else if(variable == "height_of_life_bar")
height_of_life_bar = std::stoi(value);
else if(variable == "number_of_floors")
number_of_floors = std::stoi(value);
else if(variable == "honour_price")
honour_price = std::stoi(value);
else if(variable == "upgrade_info")
upgrade_info = value;
else if(variable == "number_of_carriers")
number_of_carriers = std::stoi(value);
else if(variable == "number_of_workers")
number_of_workers = std::stoi(value);
else if(variable == "capacity")
{
if(value == "infinity")
capacity = std::numeric_limits<int>::max();
else
capacity = std::stoi(value);
}
else if(variable == "can_be_upgraded")
{
if((value == "true") || (value == "TRUE") || (value == "True") || (value == "1"))
{
can_be_upgraded = true;
}
else
{
can_be_upgraded = false;
}
}
else if(variable == "size")
{
if(value == "ONE_TILE_BUILDING")
size = ONE_TILE_BUILDING;
else if(value == "FOUR_TILE_BUILDING")
size = FOUR_TILE_BUILDING;
else if(value == "NINE_TILE_BUILDING")
size = NINE_TILE_BUILDING;
else if(value == "LEFT_GATE_BUILDING")
size = LEFT_GATE_BUILDING;
else
{
LOG("error in parsing - wrong size");
throw std::exception();
}
}
}
const building_info& building_info::show_building_info(building_type type)
{
static std::vector<building_info> info = load_info<building_info, building_names, building_type>(NUMBER_OF_BUILDINGS, "data/buildings/");
return info[static_cast<int>(type)];
}
void people_info::load(const std::string & value, const std::string & variable)
{
object_info::load(value, variable);
if(variable == "attack")
attack = std::stoi(value);
else if(variable == "weapons")
price[WEAPONS] = std::stoi(value);
else if(variable == "bows")
price[BOWS] = std::stoi(value);
else if(variable == "plate_armour")
price[PLATE_ARMOR] = std::stoi(value);
else if(variable == "frames_to_move")
frames_to_move = std::stoi(value);
else if(variable == "frames_to_attack")
frames_to_attack = std::stoi(value);
else if(variable == "honour_price")
honour_price = std::stoi(value);
}
const people_info& people_info::show_people_info(people_type type)
{
static std::vector<people_info> info = load_info<people_info, people_names, people_type>(NUMBER_OF_PEOPLE, "data/people/");
return info[static_cast<int>(type)];
}