-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.cpp
More file actions
202 lines (184 loc) · 5.57 KB
/
Copy pathAStar.cpp
File metadata and controls
202 lines (184 loc) · 5.57 KB
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
#include "AStar.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <ostream>
bool AStar::Vec2i::operator==(const Vec2i &coordinates_) const
{
return (x == coordinates_.x && y == coordinates_.y);
}
std::ostream &operator<<(std::ostream &os, const AStar::Vec2i &coord_)
{
return os << coord_.x << " " << coord_.y;
}
AStar::Vec2i operator+(const AStar::Vec2i &left_, const AStar::Vec2i &right_)
{
return {left_.x + right_.x, left_.y + right_.y};
}
AStar::Node::Node(const Vec2i &coordinates_, Node *parent_)
{
parent = parent_;
coordinates = coordinates_;
G = H = 0;
}
AStar::uint AStar::Node::getScore() const
{
return G + H;
}
AStar::Generator::Generator() : collision(nullptr)
{
setDiagonalMovement(false);
setHeuristic(&Heuristic::manhattan);
direction = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
}
void AStar::Generator::setWorldSize(const Vec2i &worldSize_)
{
worldSize = worldSize_;
}
void AStar::Generator::setDiagonalMovement(bool enable_)
{
directions = (enable_ ? 8 : 4);
}
void AStar::Generator::setHeuristic(HeuristicFunction heuristic_)
{
heuristic = heuristic_;
}
void AStar::Generator::addCollision(const Vec2i &coordinates_)
{
walls.push_back(coordinates_);
}
void AStar::Generator::removeCollision(const Vec2i &coordinates_)
{
auto it = std::find(walls.begin(), walls.end(), coordinates_);
if (it != walls.end())
{
walls.erase(it);
}
}
void AStar::Generator::clearCollisions()
{
walls.clear();
}
AStar::CoordinateList AStar::Generator::findPath(const Vec2i &source_, const Vec2i &target_)
{
if (detectCollision(source_) || detectCollision(target_))
{
return {};
}
auto delta = Heuristic::getDelta(source_, target_);
auto dist = std::max(delta.x, delta.y) + 1;
Node *current = nullptr;
CordSet openSet;
std::vector<Node> openHeap;
openHeap.reserve(dist * 5);
CoordMap closedMap;
openSet.reserve(dist * 6);
closedMap.reserve(dist * 2);
openHeap.emplace_back(source_);
std::push_heap(openHeap.begin(), openHeap.end(), comp);
openSet.emplace(source_);
while (!openHeap.empty())
{
current = &openHeap.front();
if (current->coordinates == target_)
{
break;
}
auto coord = current->coordinates;
closedMap[coord] = current;
std::pop_heap(openHeap.begin(), openHeap.end(), comp);
openHeap.pop_back();
openSet.erase(coord);
current = closedMap[coord];
for (uint i = 0; i < directions; ++i)
{
Vec2i newCoordinates(current->coordinates + direction[i]);
if (openSet.find(newCoordinates) != openSet.end())
{
continue;
}
Node *successor = nullptr;
if (detectCollision(newCoordinates) || (successor = findNodeOnMap(closedMap, newCoordinates)))
{
continue;
}
uint totalCost = current->G + ((i < 4) ? 10 : 14);
if (successor == nullptr)
{
openHeap.emplace_back(newCoordinates, current);
auto &ref = openHeap.back();
ref.G = totalCost;
ref.H = heuristic(ref.coordinates, target_);
std::push_heap(openHeap.begin(), openHeap.end(), comp);
openSet.emplace(std::move(newCoordinates));
}
else if (totalCost < successor->G)
{
successor->parent = current;
successor->G = totalCost;
}
}
}
CoordinateList path;
bool reverse = false;
if (current->coordinates == target_)
{
reverse = true;
}
while (current != nullptr)
{
if (reverse)
{
path.emplace(path.begin(), std::move(current->coordinates));
}
else
{
path.emplace_back(std::move(current->coordinates));
}
current = current->parent;
}
return path;
}
AStar::Node *AStar::Generator::findNodeOnMap(CoordMap &nodes_, const Vec2i &coordinates_)
{
const auto &it = nodes_.find(coordinates_);
if (it != nodes_.end())
{
return it->second;
}
return nullptr;
}
bool AStar::Generator::detectCollision(const Vec2i &coordinates_)
{
if (coordinates_.x < 0 || coordinates_.x >= worldSize.x || coordinates_.y < 0 || coordinates_.y >= worldSize.y ||
std::find(walls.begin(), walls.end(), coordinates_) != walls.end() || (collision && collision(coordinates_)))
{
return true;
}
return false;
}
AStar::Vec2i AStar::Heuristic::getDelta(const Vec2i &source_, const Vec2i &target_)
{
return {abs(source_.x - target_.x), abs(source_.y - target_.y)};
}
AStar::uint AStar::Heuristic::manhattan(const Vec2i &source_, const Vec2i &target_)
{
const auto &delta = getDelta(source_, target_);
// if(delta.x>delta.y)
// {
// return static_cast<uint>(10*delta.x + 4*delta.y);
// }else{
// return static_cast<uint>(4*delta.x + 10*delta.y);
// }
return static_cast<uint>(10 * (delta.x + delta.y));
}
AStar::uint AStar::Heuristic::euclidean(const Vec2i &source_, const Vec2i &target_)
{
const auto &delta = getDelta(source_, target_);
return static_cast<uint>(10 * sqrt(pow(delta.x, 2) + pow(delta.y, 2)));
}
AStar::uint AStar::Heuristic::octagonal(const Vec2i &source_, const Vec2i &target_)
{
const auto &delta = getDelta(source_, target_);
return 10 * (delta.x + delta.y) + (-6) * std::min(delta.x, delta.y);
}