-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathUtils.cpp
343 lines (308 loc) · 9.46 KB
/
PathUtils.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "PathUtils.h"
#include "Cell.h"
Direction GetOppositeDirection(Direction dir)
{
switch (dir) {
case LEFT:
return RIGHT;
case RIGHT:
return LEFT;
case UP:
return DOWN;
case DOWN:
return UP;
}
return UP;
}
bool IsDirectionOpen(TileType const ttype, Direction dir)
{
if (ttype == CROSSROADS)
return true;
switch (dir) {
case LEFT:
return (ttype == HORIZONTAL || ttype == RIGHT_TOP_CORNER || ttype == RIGHT_BOTTOM_CORNER
|| ttype == LEFT_HEADED_T || ttype == BOTTOM_HEADED_T || ttype == TOP_HEADED_T);
case RIGHT:
return (ttype == HORIZONTAL || ttype == LEFT_TOP_CORNER || ttype == LEFT_BOTTOM_CORNER
|| ttype == RIGHT_HEADED_T || ttype == BOTTOM_HEADED_T || ttype == TOP_HEADED_T);
case UP:
return (ttype == VERTICAL || ttype == LEFT_BOTTOM_CORNER || ttype == RIGHT_BOTTOM_CORNER
|| ttype == LEFT_HEADED_T || ttype == TOP_HEADED_T || ttype == RIGHT_HEADED_T);
case DOWN:
return (ttype == VERTICAL || ttype == RIGHT_TOP_CORNER || ttype == LEFT_TOP_CORNER
|| ttype == LEFT_HEADED_T || ttype == RIGHT_HEADED_T || ttype == BOTTOM_HEADED_T);
}
return false;
}
TileType GetCellType(TMap const & map, Cell const & cell)
{
return map[cell.m_x][cell.m_y];
}
bool IsValidCell(TMap const & map, Cell const & cell)
{
if (cell.m_x < 0)
return false;
if (cell.m_y < 0)
return false;
if (cell.m_x >= map.size())
return false;
if (cell.m_y >= map[0].size())
return false;
return true;
}
bool CanPass(TMap const & map, Cell const & start, Direction const dir)
{
Cell finish = start.GetNeibor(dir);
if (!IsValidCell(map, finish))
return false;
if (IsDirectionOpen(GetCellType(map, start), dir) && IsDirectionOpen(GetCellType(map, finish), GetOppositeDirection(dir)))
return true;
return false;
}
//void DSF(TMap const & maze, Cell const & cur, Cell const & prev, Cell const & target, std::map<Cell, pair<int, Cell>> & data)
//{
// if (cur == target)
// {
// if (data.count(target) ==1)
// {
// if (data[target].first + 1 > data[prev].first + 1 )
// data[target] = {data[prev].first + 1, prev};
// }
// else
// {
// data[target] = {data[prev].first + 1, prev};
// }
// return;
// }
// bool bImprooved = false;
// if (data.count(cur) == 0)
// {
// data[cur] = {data[prev].first + 1, prev};
// bImprooved = true;
// }
// else
// {
// if (data[cur].first + 1 > data[prev].first + 1)
// {
// data[cur] = {data[prev].first + 1, prev};
// bImprooved = true;
// }
// }
// if (bImprooved)
// for (auto dir: AllDirections())
// {
// if (CanPass(maze, cur, cur.GetNeibor(dir)))
// DSF(maze, cur.GetNeibor(dir), cur, target, data);
// }
//}
//vector<Cell> GetClosestPath(const model::World& world, Cell start, Cell finish)
//{
// std::map<Cell, pair<int, Cell>> data;
// data[start] = {0, start};
// for (auto dir: AllDirections())
// {
// if (CanPass(world.getTilesXY(), start, start.GetNeibor(dir)))
// DSF(world.getTilesXY(), start.GetNeibor(dir), start, finish, data);
// }
// vector<Cell> res(1, finish);
// Cell cur = finish;
// while (cur != start)
// {
// cur = data[cur].second;
// res.push_back(cur);
// }
// reverse(res.begin(), res.end());
// return res;
//}
typedef pair<Cell, Direction> MapKeyT;
typedef std::map<MapKeyT, pair<double, MapKeyT>> MapT;
void DSF(TMap const & maze,
MapKeyT const & cur, MapKeyT const & prev, Cell const & target,
MapT & data, vector<vector<int>> const & bonuses)
{
bool IsOpposite = GetOppositeDirection(prev.second) == cur.second;
double to_add = (IsOpposite ? 3 : 1);
if (bonuses[cur.first.m_x][cur.first.m_y] == 1)
to_add -= 0.5;
bool need_update = data.count(cur) == 0;
need_update |= data[cur].first > data[prev].first + to_add;
if (need_update)
{
double prev_dist = data.count(prev) == 0 ? 0 : data[prev].first;
data[cur] = {prev_dist + to_add, prev};
}
if (cur.first == target)
return;
if (need_update)
{
if (CanPass(maze, cur.first, cur.second))
DSF(maze, {cur.first.GetNeibor(cur.second), cur.second}, cur, target, data, bonuses);
for (auto const & dir: AllDirections())
if (CanPass(maze, cur.first, dir))
DSF(maze, {cur.first.GetNeibor(dir), dir}, cur, target, data, bonuses);
}
}
//void PrintMap(TMap const & mp, std::map<Cell, pair<int, Cell>> const & data)
//{
// for (size_t y = 0; y < mp[0].size(); ++y)
// {
// for (size_t x = 0; x < mp.size(); ++x)
// {
// if (mp[x][y] == 0)
// cout << "0";
// else
// {
// // cout << ".";
// if (data.count(Cell(x,y)) == 1)
// cout << "+";
// else
// cout << ".";
//
// }
// }
// cout << endl;
// }
// cout << endl;
//
//}
struct cashe_key
{
bool operator < (cashe_key const & other) const
{
if (start < other.start)
return true;
if (other.start < start)
return false;
if (start_dir < other.start_dir)
return true;
if (other.start_dir < start_dir)
return false;
if (finish < other.finish)
return true;
if (other.finish < finish)
return false;
return false;
}
Cell const start;
Direction const start_dir;
Cell const finish;
};
vector<Cell> GetClosestPath(const model::World& world,
Cell const & start, Direction const start_dir, Cell const & finish, Game const & game)
{
static map<cashe_key, vector<Cell> > cacshe;
static int bonus_hash = 0;
int bonus_hash_cur = 0;
auto const & map = world.getTilesXY();
vector<vector<int>> bonuses(map.size(), vector<int>(map[0].size(), 0));
for (Bonus const & bonus: world.getBonuses())
{
bonus_hash_cur += bonus.getX() * bonus.getX() + bonus.getY() * bonus.getY();
auto bonus_cell= GetCell(bonus, game);
bonuses[bonus_cell.m_x][bonus_cell.m_y] = (bonus.getType() == PURE_SCORE || bonus.getType() == REPAIR_KIT) ? 1 : 0;
}
if (bonus_hash != bonus_hash_cur)
{
bonus_hash = bonus_hash_cur;
cacshe.clear();
}
cashe_key ck = {start, start_dir, finish};
if (cacshe.count(ck) == 1)
return cacshe[ck];
MapT data;
DSF(world.getTilesXY(), {start, start_dir}, {start, start_dir}, finish, data, bonuses);
// PrintMap(world.getTilesXY(), data);
vector<Cell> res;
int const INF = 1000000;
int best = INF;
MapKeyT cur = {finish, LEFT};
for (auto dir: AllDirections())
{
if (data.count({finish,dir}) == 0)
continue;
if (data[{finish,dir}].first < best)
{
best = data[{finish,dir}].first;
cur = {finish,dir};
}
}
if (best == INF)
return res;
while (cur.first != start)
{
res.push_back(cur.first);
cur = data[cur].second;
}
res.push_back(start);
reverse(res.begin(), res.end());
cacshe[ck] = res;
return res;
}
bool IsStraight(Car const & car, std::vector<Cell> const & path, int N, Game const & game, World const & world)
{
bool bStrait = false;
if (path.size() >= N + 1)
{
double target4X = (path[N].m_x + 0.5) * game.getTrackTileSize();
double target4Y = (path[N].m_y + 0.5) * game.getTrackTileSize();
if (car.getAngleTo(target4X, target4Y) < 20*PI/180)
{
bStrait = true;
TileType curType = GetCellType(world.getTilesXY(), GetCell(car, game));
for (size_t i =1; i < N; ++i)
if (curType != GetCellType(world.getTilesXY(), path[i]))
bStrait = false;
}
}
return bStrait;
}
int GetStraightLength(std::vector<Cell> const & path)
{
if (path.empty())
return 0;
int res = 0;
for (auto dir: AllDirections())
{
auto cur = path[0];
int i = 0;
while (cur == path[i])
{
i++;
switch (dir) {
case LEFT:
cur.m_x--;
break;
case RIGHT:
cur.m_x++;
break;
case UP:
cur.m_y--;
break;
case DOWN:
cur.m_y++;
break;
}
}
res = max(res, i);
}
return res;
}
bool IsOnPath(Unit const & unit, std::vector<Cell> const & path, Game const & game, int max_length)
{
auto targetCell = GetCell(unit, game);
for (size_t i = 0; i < path.size() && i <= max_length; ++i)
{
if (targetCell == path[i])
return true;
}
return false;
}
bool Is180Turn(std::vector<Cell> const & path)
{
if (path.size()< 4)
return false;
for (auto dir: AllDirections())
if (path[0].GetNeibor(dir) == path[3])
return true;
return false;
}