forked from miki151/keeperrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest_path.cpp
189 lines (171 loc) · 6.29 KB
/
shortest_path.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
#include "stdafx.h"
#include "shortest_path.h"
#include "level.h"
#include "creature.h"
using namespace std;
const double ShortestPath::infinity = 1000000000;
const int revShortestLimit = 15;
const int maxSize = 600;
Table<double> ddist(maxSize, maxSize);
Table<int> dirty(maxSize, maxSize, 0);
int counter = 1;
int margin = 15;
ShortestPath::ShortestPath(const Level* level, const Creature* creature, Vec2 to, Vec2 from, double mult,
bool avoidEnemies) : target(to), directions(Vec2::directions8()), bounds(level->getBounds()) {
CHECK(level->getBounds().getKX() <= maxSize && level->getBounds().getKY() <= maxSize);
auto entryFun = [=](Vec2 pos) {
if (level->getSquare(pos)->canEnter(creature) || creature->getPosition() == pos)
return 1.0;
if ((level->getSquare(pos)->canEnterEmpty(creature) || level->getSquare(pos)->canDestroy())
&& (!avoidEnemies || !level->getSquare(pos)->getCreature()
|| !level->getSquare(pos)->getCreature()->isEnemy(creature)))
return 5.0;
return infinity;};
CHECK(to.inRectangle(level->getBounds()));
CHECK(from.inRectangle(level->getBounds()));
if (mult == 0) {
// Use a suboptimal, but faster pathfinding.
init(entryFun, [](Vec2 v)->double { return 2 * v.lengthD(); }, target, from);
} else {
auto lengthFun = [](Vec2 v)->double { return v.length8(); };
bounds = bounds.intersection(Rectangle(min(to.x, from.x) - margin, min(to.y, from.y) - margin,
max(to.x, from.x) + margin, max(to.y, from.y) + margin));
init(entryFun, lengthFun, target, Nothing(), revShortestLimit);
setDistance(target, infinity);
reverse(entryFun, lengthFun, mult, from, revShortestLimit);
}
}
ShortestPath::ShortestPath(Rectangle a, function<double(Vec2)> entryFun, function<int(Vec2)> lengthFun,
vector<Vec2> dir, Vec2 to, Vec2 from, double mult) : target(to), directions(dir), bounds(a) {
CHECK(a.getKX() <= maxSize && a.getKY() <= maxSize && a.getPX() >= 0 && a.getPY() >= 0);
if (mult == 0)
init(entryFun, lengthFun, target, from);
else {
init(entryFun, lengthFun, target, Nothing(), revShortestLimit);
setDistance(target, infinity);
reverse(entryFun, lengthFun, mult, from, revShortestLimit);
}
}
double ShortestPath::getDistance(Vec2 v) const {
return dirty[v] < counter ? infinity : ddist[v];
}
void ShortestPath::setDistance(Vec2 v, double d) {
ddist[v] = d;
dirty[v] = counter;
}
void ShortestPath::init(function<double(Vec2)> entryFun, function<double(Vec2)> lengthFun, Vec2 target,
Optional<Vec2> from, Optional<int> limit) {
reversed = false;
++counter;
function<bool(Vec2, Vec2)> comparator;
if (from)
comparator = [=](Vec2 pos1, Vec2 pos2) {
return this->getDistance(pos1) + lengthFun(*from - pos1) > this->getDistance(pos2) + lengthFun(*from - pos2); };
else
comparator = [this](Vec2 pos1, Vec2 pos2) { return this->getDistance(pos1) > this->getDistance(pos2); };
priority_queue<Vec2, vector<Vec2>, decltype(comparator)> q(comparator) ;
setDistance(target, 0);
q.push(target);
int numPopped = 0;
while (!q.empty()) {
++numPopped;
Vec2 pos = q.top();
// Debug() << "Popping " << pos << " " << distance[pos] << " " << (from ? (*from - pos).length4() : 0);
if (from == pos || (limit && getDistance(pos) >= *limit)) {
Debug() << "Shortest path from " << (from ? *from : Vec2(-1, -1)) << " to " << target << " " << numPopped << " visited distance " << getDistance(pos);
constructPath(pos);
return;
}
q.pop();
for (Vec2 dir : directions) {
Vec2 next = pos + dir;
if (next.inRectangle(bounds)) {
double cdist = getDistance(pos);
double ndist = getDistance(next);
if (cdist < ndist) {
double dist = cdist + entryFun(next);
CHECK(dist > cdist) << "Entry fun non positive " << dist - cdist;
if (dist < ndist) {
setDistance(next, dist);
q.push(next);
}
}
}
}
}
Debug() << "Shortest path exhausted, " << numPopped << " visited";
}
void ShortestPath::reverse(function<double(Vec2)> entryFun, function<double(Vec2)> lengthFun, double mult, Vec2 from,
int limit) {
reversed = true;
function<bool(Vec2, Vec2)> comparator = [=](Vec2 pos1, Vec2 pos2) {
return this->getDistance(pos1) + lengthFun(from - pos1) > this->getDistance(pos2) + lengthFun(from - pos2); };
priority_queue<Vec2, vector<Vec2>, decltype(comparator)> q(comparator) ;
for (Vec2 v : bounds) {
double dist = getDistance(v);
if (dist <= limit) {
setDistance(v, mult * dist);
q.push(v);
}
}
int numPopped = 0;
while (!q.empty()) {
++numPopped;
Vec2 pos = q.top();
if (from == pos) {
Debug() << "Rev shortest path from " << " from " << target << " " << numPopped << " visited";
constructPath(pos, true);
return;
}
q.pop();
for (Vec2 dir : directions)
if ((pos + dir).inRectangle(bounds)) {
if (getDistance(pos + dir) > getDistance(pos) + entryFun(pos + dir) && getDistance(pos + dir) < 0) {
setDistance(pos + dir, getDistance(pos) + entryFun(pos + dir));
q.push(pos + dir);
}
}
}
Debug() << "Rev shortest path from " << " from " << target << " " << numPopped << " visited";
}
void ShortestPath::constructPath(Vec2 pos, bool reversed) {
vector<Vec2> ret;
while (pos != target) {
Vec2 next;
double lowest = getDistance(pos);
CHECK(lowest < infinity);
for (Vec2 dir : directions) {
double dist;
if ((pos + dir).inRectangle(bounds) && (dist = getDistance(pos + dir)) < lowest) {
lowest = dist;
next = pos + dir;
}
}
if (lowest >= getDistance(pos)) {
if (reversed)
break;
else
FAIL << "can't track path";
}
ret.push_back(pos);
pos = next;
}
if (!reversed)
ret.push_back(target);
path = vector<Vec2>(ret.rbegin(), ret.rend());
}
bool ShortestPath::isReversed() const {
return reversed;
}
bool ShortestPath::isReachable(Vec2 pos) const {
return (path.size() >= 2 && path.back() == pos) || (path.size() >= 3 && path[path.size() - 2] == pos);
}
Vec2 ShortestPath::getNextMove(Vec2 pos) {
CHECK(isReachable(pos));
if (pos != path.back())
path.pop_back();
return path[path.size() - 2];
}
Vec2 ShortestPath::getTarget() const {
return target;
}