-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap.cpp
215 lines (195 loc) · 7.39 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
#include "map.h"
void Map::generate_moves()
{
std::vector<Step> moves;
valid_moves.resize(height);
for(int i = 0; i < height; i++)
valid_moves[i].resize(width);
moves = {{0,1}, {1,0}, {-1,0}, {0,-1}};
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
{
std::vector<bool> valid(moves.size(), true);
for(int k = 0; k < 4; k++)
if(!cell_on_grid(i + moves[k].i, j + moves[k].j) || cell_is_obstacle(i + moves[k].i, j + moves[k].j))
valid[k] = false;
std::vector<Step> v_moves = {};
for(int k = 0; k < valid.size(); k++)
if(valid[k])
v_moves.push_back(moves[k]);
valid_moves[i][j] = v_moves;
}
}
bool Map::get_map(const char* FileName)
{
tinyxml2::XMLElement *root = 0, *map = 0, *element = 0, *mapnode;
std::string value;
std::stringstream stream;
bool hasGridMem(false), hasGrid(false), hasHeight(false), hasWidth(false);
tinyxml2::XMLDocument doc;
if (doc.LoadFile(FileName) != tinyxml2::XMLError::XML_SUCCESS)
{
std::cout << "Error opening XML file!" << std::endl;
return false;
}
root = doc.FirstChildElement(CNS_TAG_ROOT);
if (!root)
{
std::cout << "Error! No '" << CNS_TAG_ROOT << "' tag found in XML file!" << std::endl;
return false;
}
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();
stream << element->GetText();
if (!hasGridMem && hasHeight && hasWidth)
{
grid.resize(height);
for (int i = 0; i < height; ++i)
grid[i].resize(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_GRID)
{
int rowiter(0), grid_i(0), grid_j(0);
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();
}
}
}
if (!hasGrid) {
std::cout << "Error! There is no tag 'grid' in xml-file!\n";
return false;
}
return true;
}
void Map::print_map()
{
std::cout<<height<<"x"<<width<<std::endl;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
std::cout<<grid[i][j]<<" ";
std::cout<<std::endl;
}
}
bool Map::cell_is_traversable(int i, int j) const
{
return (grid[i][j] != CN_OBSTL);
}
bool Map::cell_is_obstacle(int i, int j) const
{
return (grid[i][j] == CN_OBSTL);
}
bool Map::cell_on_grid(int i, int j) const
{
return (i < height && i >= 0 && j < width && j >= 0);
}
std::vector<Step> Map::get_valid_moves(int i, int j) const
{
return valid_moves[i][j];
}
int Map::get_value(int i, int j) const
{
if(i < 0 || j < 0 || i >= height || j >= width)
return -1;
else
return grid[i][j];
}