-
Notifications
You must be signed in to change notification settings - Fork 28
/
map.cpp
321 lines (290 loc) · 13.1 KB
/
map.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "map.h"
#include "tinyxml2.h"
Map::Map()
{
height = -1;
width = -1;
start_i = -1;
start_j = -1;
goal_i = -1;
goal_j = -1;
Grid = nullptr;
cellSize = 1;
}
Map::~Map()
{
if (Grid) {
for (int i = 0; i < height; ++i)
delete[] Grid[i];
delete[] Grid;
}
}
bool Map::CellIsTraversable(int i, int j) const
{
return (Grid[i][j] == CN_GC_NOOBS);
}
bool Map::CellIsObstacle(int i, int j) const
{
return (Grid[i][j] != CN_GC_NOOBS);
}
bool Map::CellOnGrid(int i, int j) const
{
return (i < height && i >= 0 && j < width && j >= 0);
}
bool Map::getMap(const char *FileName)
{
int rowiter = 0, grid_i = 0, grid_j = 0;
tinyxml2::XMLElement *root = 0, *map = 0, *element = 0, *mapnode;
std::string value;
std::stringstream stream;
bool hasGridMem = false, hasGrid = false, hasHeight = false, hasWidth = false, hasSTX = false, hasSTY = false, hasFINX = false, hasFINY = false, hasCellSize = false;
tinyxml2::XMLDocument doc;
// Load XML File
if (doc.LoadFile(FileName) != tinyxml2::XMLError::XML_SUCCESS) {
std::cout << "Error opening XML file!" << std::endl;
return false;
}
// Get ROOT element
root = doc.FirstChildElement(CNS_TAG_ROOT);
if (!root) {
std::cout << "Error! No '" << CNS_TAG_ROOT << "' tag found in XML file!" << std::endl;
return false;
}
// Get MAP element
map = root->FirstChildElement(CNS_TAG_MAP);
if (!map) {
std::cout << "Error! No '" << CNS_TAG_MAP << "' tag found in XML file!" << std::endl;
return false;
}
for (mapnode = map->FirstChildElement(); mapnode; mapnode = mapnode->NextSiblingElement()) {
element = mapnode->ToElement();
value = mapnode->Value();
std::transform(value.begin(), value.end(), value.begin(), ::tolower);
stream.str("");
stream.clear();
if(value != CNS_TAG_GRID)
stream << element->GetText();
if (!hasGridMem && hasHeight && hasWidth) {
Grid = new int *[height];
for (int i = 0; i < height; ++i)
Grid[i] = new int[width];
hasGridMem = true;
}
if (value == CNS_TAG_HEIGHT) {
if (hasHeight) {
std::cout << "Warning! Duplicate '" << CNS_TAG_HEIGHT << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_HEIGHT << "' =" << height << "will be used."
<< std::endl;
}
else {
if (!((stream >> height) && (height > 0))) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_HEIGHT
<< "' tag encountered (or could not convert to integer)." << std::endl;
std::cout << "Value of '" << CNS_TAG_HEIGHT << "' tag should be an integer >=0" << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_HEIGHT
<< "' tag will be encountered later..." << std::endl;
}
else
hasHeight = true;
}
}
else if (value == CNS_TAG_WIDTH) {
if (hasWidth) {
std::cout << "Warning! Duplicate '" << CNS_TAG_WIDTH << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_WIDTH << "' =" << width << "will be used." << std::endl;
}
else {
if (!((stream >> width) && (width > 0))) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_WIDTH
<< "' tag encountered (or could not convert to integer)." << std::endl;
std::cout << "Value of '" << CNS_TAG_WIDTH << "' tag should be an integer AND >0" << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_WIDTH
<< "' tag will be encountered later..." << std::endl;
}
else
hasWidth = true;
}
}
else if (value == CNS_TAG_CELLSIZE) {
if (hasCellSize) {
std::cout << "Warning! Duplicate '" << CNS_TAG_CELLSIZE << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_CELLSIZE << "' =" << cellSize << "will be used."
<< std::endl;
}
else {
if (!((stream >> cellSize) && (cellSize > 0))) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_CELLSIZE
<< "' tag encountered (or could not convert to double)." << std::endl;
std::cout << "Value of '" << CNS_TAG_CELLSIZE
<< "' tag should be double AND >0. By default it is defined to '1'" << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_CELLSIZE
<< "' tag will be encountered later..." << std::endl;
}
else
hasCellSize = true;
}
}
else if (value == CNS_TAG_STX) {
if (!hasWidth) {
std::cout << "Error! '" << CNS_TAG_STX << "' tag encountered before '" << CNS_TAG_WIDTH << "' tag."
<< std::endl;
return false;
}
if (hasSTX) {
std::cout << "Warning! Duplicate '" << CNS_TAG_STX << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_STX << "' =" << start_j << "will be used." << std::endl;
}
else {
if (!(stream >> start_j && start_j >= 0 && start_j < width)) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_STX
<< "' tag encountered (or could not convert to integer)" << std::endl;
std::cout << "Value of '" << CNS_TAG_STX << "' tag should be an integer AND >=0 AND < '"
<< CNS_TAG_WIDTH << "' value, which is " << width << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_STX
<< "' tag will be encountered later..." << std::endl;
}
else
hasSTX = true;
}
}
else if (value == CNS_TAG_STY) {
if (!hasHeight) {
std::cout << "Error! '" << CNS_TAG_STY << "' tag encountered before '" << CNS_TAG_HEIGHT << "' tag."
<< std::endl;
return false;
}
if (hasSTY) {
std::cout << "Warning! Duplicate '" << CNS_TAG_STY << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_STY << "' =" << start_i << "will be used." << std::endl;
}
else {
if (!(stream >> start_i && start_i >= 0 && start_i < height)) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_STY
<< "' tag encountered (or could not convert to integer)" << std::endl;
std::cout << "Value of '" << CNS_TAG_STY << "' tag should be an integer AND >=0 AND < '"
<< CNS_TAG_HEIGHT << "' value, which is " << height << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_STY
<< "' tag will be encountered later..." << std::endl;
}
else
hasSTY = true;
}
}
else if (value == CNS_TAG_FINX) {
if (!hasWidth) {
std::cout << "Error! '" << CNS_TAG_FINX << "' tag encountered before '" << CNS_TAG_WIDTH << "' tag."
<< std::endl;
return false;
}
if (hasFINX) {
std::cout << "Warning! Duplicate '" << CNS_TAG_FINX << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_FINX << "' =" << goal_j << "will be used." << std::endl;
}
else {
if (!(stream >> goal_j && goal_j >= 0 && goal_j < width)) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_FINX
<< "' tag encountered (or could not convert to integer)" << std::endl;
std::cout << "Value of '" << CNS_TAG_FINX << "' tag should be an integer AND >=0 AND < '"
<< CNS_TAG_WIDTH << "' value, which is " << width << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_FINX
<< "' tag will be encountered later..." << std::endl;
}
else
hasFINX = true;
}
}
else if (value == CNS_TAG_FINY) {
if (!hasHeight) {
std::cout << "Error! '" << CNS_TAG_FINY << "' tag encountered before '" << CNS_TAG_HEIGHT << "' tag."
<< std::endl;
return false;
}
if (hasFINY) {
std::cout << "Warning! Duplicate '" << CNS_TAG_FINY << "' encountered." << std::endl;
std::cout << "Only first value of '" << CNS_TAG_FINY << "' =" << goal_i << "will be used." << std::endl;
}
else {
if (!(stream >> goal_i && goal_i >= 0 && goal_i < height)) {
std::cout << "Warning! Invalid value of '" << CNS_TAG_FINY
<< "' tag encountered (or could not convert to integer)" << std::endl;
std::cout << "Value of '" << CNS_TAG_FINY << "' tag should be an integer AND >=0 AND < '"
<< CNS_TAG_HEIGHT << "' value, which is " << height << std::endl;
std::cout << "Continue reading XML and hope correct value of '" << CNS_TAG_FINY
<< "' tag will be encountered later..." << std::endl;
}
else
hasFINY = true;
}
}
else if (value == CNS_TAG_GRID) {
hasGrid = true;
if (!(hasHeight && hasWidth)) {
std::cout << "Error! No '" << CNS_TAG_WIDTH << "' tag or '" << CNS_TAG_HEIGHT << "' tag before '"
<< CNS_TAG_GRID << "'tag encountered!" << std::endl;
return false;
}
element = mapnode->FirstChildElement();
while (grid_i < height) {
if (!element) {
std::cout << "Error! Not enough '" << CNS_TAG_ROW << "' tags inside '" << CNS_TAG_GRID << "' tag."
<< std::endl;
std::cout << "Number of '" << CNS_TAG_ROW
<< "' tags should be equal (or greater) than the value of '" << CNS_TAG_HEIGHT
<< "' tag which is " << height << std::endl;
return false;
}
std::string str = element->GetText();
std::vector<std::string> elems;
std::stringstream ss(str);
std::string item;
while (std::getline(ss, item, ' '))
elems.push_back(item);
rowiter = grid_j = 0;
int val;
if (elems.size() > 0)
for (grid_j = 0; grid_j < width; ++grid_j) {
if (grid_j == elems.size())
break;
stream.str("");
stream.clear();
stream << elems[grid_j];
stream >> val;
Grid[grid_i][grid_j] = val;
}
if (grid_j != width) {
std::cout << "Invalid value on " << CNS_TAG_GRID << " in the " << grid_i + 1 << " " << CNS_TAG_ROW
<< std::endl;
return false;
}
++grid_i;
element = element->NextSiblingElement();
}
}
}
//some additional checks
if (!hasGrid) {
std::cout << "Error! There is no tag 'grid' in xml-file!\n";
return false;
}
if (!(hasFINX && hasFINY && hasSTX && hasSTY))
return false;
if (Grid[start_i][start_j] != CN_GC_NOOBS) {
std::cout << "Error! Start cell is not traversable (cell's value is" << Grid[start_i][start_j] << ")!"
<< std::endl;
return false;
}
if (Grid[goal_i][goal_j] != CN_GC_NOOBS) {
std::cout << "Error! Goal cell is not traversable (cell's value is" << Grid[goal_i][goal_j] << ")!"
<< std::endl;
return false;
}
return true;
}
int Map::getValue(int i, int j) const
{
if (i < 0 || i >= height)
return -1;
if (j < 0 || j >= width)
return -1;
return Grid[i][j];
}