-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathFindingAlgorithms.cpp
More file actions
220 lines (173 loc) · 5.66 KB
/
Copy pathpathFindingAlgorithms.cpp
File metadata and controls
220 lines (173 loc) · 5.66 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "Headers.h"
#include "pathFindingAlgorithms.h"
namespace auxiliaryFunctions
{
//auxiliary functions
int getRandomInt(int min, int max)
{
static std::mt19937 mt{ static_cast<std::mt19937::result_type>(std::time(nullptr)) };
return std::uniform_int_distribution<>{ min, max }(mt);
}
void drawShortestPath(Tile* endTile)
{
if (endTile != nullptr)
{
Tile* tempTile = endTile;
while (tempTile->getParentTile() != nullptr)
{
if (tempTile != endTile)
tempTile->setFillColor(sf::Color::Magenta);
tempTile = tempTile->getParentTile();
}
}
}
void clearOldPath(std::vector<std::vector<Tile>>& tileMap)
{
for (int iii{ 0 }; iii < Constants::mapSizeY; ++iii)
{
for (int kkk{ 0 }; kkk < Constants::mapSizeX; ++kkk)
{
if (tileMap[iii][kkk].getFillColor() == sf::Color::Magenta)
tileMap[iii][kkk].setFillColor(sf::Color::Black);
}
}
}
void clearOldVisitedTiles(std::vector<std::vector<Tile>>& tileMap)
{
for (int iii{ 0 }; iii < Constants::mapSizeY; ++iii)
{
for (int kkk{ 0 }; kkk < Constants::mapSizeX; ++kkk)
{
if (tileMap[iii][kkk].getFillColor() == sf::Color{ 255,94,19,255 })
tileMap[iii][kkk].setFillColor(sf::Color::Black);
}
}
}
void updateVisualization(std::vector<std::vector<Tile>>& tileMap, sf::RenderWindow* window)
{
window->clear();
for (int iii{ 0 }; iii < Constants::mapSizeY; ++iii)
{
for (int kkk{ 0 }; kkk < Constants::mapSizeX; ++kkk)
{
window->draw(tileMap[iii][kkk].getRectangle());
}
}
window->display();
}
void sortTilesToTest(std::vector<Tile*>& tilesToTest, int startIndex, int endIndex)
{
if (startIndex >= endIndex)
return;
int pivotIndex{ partition(tilesToTest, startIndex,endIndex) };
sortTilesToTest(tilesToTest, startIndex, pivotIndex - 1);
sortTilesToTest(tilesToTest, pivotIndex + 1, endIndex);
}
int partition(std::vector<Tile*>& tilesToTest, int startIndex, int endIndex)
{
float pivot{ tilesToTest[endIndex]->getGlobalGoal() };
int partitionIndex{ startIndex };
for (int iii{ startIndex }; iii < endIndex; ++iii)
{
if (tilesToTest[iii]->getGlobalGoal() <= pivot)
{
std::swap(tilesToTest[iii], tilesToTest[partitionIndex]);
++partitionIndex;
}
}
std::swap(tilesToTest[endIndex], tilesToTest[partitionIndex]);
return partitionIndex;
}
void setTilesDefaultValues(std::vector<std::vector<Tile>>& tileMap)
{
for (int iii{ 0 }; iii < Constants::mapSizeY; ++iii)
{
for (int kkk{ 0 }; kkk < Constants::mapSizeX; ++kkk)
{
tileMap[iii][kkk].setDistanceToStart(INFINITY);
tileMap[iii][kkk].setGlobalGoal(INFINITY);
tileMap[iii][kkk].setParent(nullptr);
tileMap[iii][kkk].setVisit(false);
}
}
}
void setTilesToTestColor(std::vector<Tile*>& tilesToTest, Tile* startTile, Tile* endTile)
{
for (int iii{ 0 }; iii < tilesToTest.size(); ++iii)
{
if (tilesToTest[iii]->getFillColor() != sf::Color{ 255,200,200,200 })
{
if (tilesToTest[iii] != startTile && tilesToTest[iii] != endTile)
tilesToTest[iii]->setFillColor(sf::Color{ 255,200,200,200 });
}
}
}
void setVisitedTilesColor(std::vector<std::vector<Tile>>& tileMap, Tile* startTile, Tile* endTile)
{
for (int iii{ 0 }; iii < Constants::mapSizeY; ++iii)
{
for (int kkk{ 0 }; kkk < Constants::mapSizeX; ++kkk)
{
if (&tileMap[iii][kkk] != startTile && &tileMap[iii][kkk] != endTile &&
tileMap[iii][kkk].isVisited() == true && tileMap[iii][kkk].getFillColor() != sf::Color::Magenta)
{
tileMap[iii][kkk].setFillColor(sf::Color{ 255,94,19,255 });
}
}
}
}
void visualizationMode(std::vector<std::vector<Tile>>& tileMap, sf::RenderWindow* window,
std::vector<Tile*>& tilesToTest, Tile* currentTile, Tile* startTile, Tile* endTile)
{
setTilesToTestColor(tilesToTest, startTile, endTile);
setVisitedTilesColor(tileMap, startTile, endTile);
updateVisualization(tileMap, window);
}
}
namespace pathFindingAlgorithms
{
//Path finding functions
void A_STAR_ALGORITHM(std::vector<std::vector<Tile>>& tileMap, Tile* startTile, Tile* endTile,
sf::RenderWindow* window)
{
auxiliaryFunctions::setTilesDefaultValues(tileMap);
Tile* currentTile = startTile;
startTile->setGlobalGoal(Tile::distanceBetweenTiles(*startTile,*endTile));
startTile->setDistanceToStart(0.0f);
std::vector<Tile*> tilesToTest;
tilesToTest.push_back(startTile);
while (!tilesToTest.empty() && currentTile != endTile)
{
auxiliaryFunctions::sortTilesToTest(tilesToTest, 0, tilesToTest.size() - 1);
while (!tilesToTest.empty() && tilesToTest[0]->isVisited() == true)
{
tilesToTest.erase(tilesToTest.begin() + 0);
}
if (tilesToTest.empty())
{
break;
}
currentTile = tilesToTest[0];
currentTile->setVisit(true);
for (int iii{ 0 } ; iii<currentTile->getNeighborVector().size() ; ++iii)
{
Tile* neighbourTile{ currentTile->getNeighborVector().at(iii) };
if (neighbourTile->isVisited() == false && neighbourTile->isObsticle() == false)
tilesToTest.push_back(neighbourTile);
float lowestLocalGoal =
(currentTile->getDistanceToStart() + Tile::distanceBetweenTiles(*currentTile, *neighbourTile));
if (lowestLocalGoal < neighbourTile->getDistanceToStart())
{
neighbourTile->setParent(currentTile);
neighbourTile->setDistanceToStart(lowestLocalGoal);
neighbourTile->setGlobalGoal(
neighbourTile->getDistanceToStart() + Tile::distanceBetweenTiles(*neighbourTile, *endTile));
}
}
if (Constants::isStaticModeSeleceted == true)
auxiliaryFunctions::visualizationMode(tileMap, window, tilesToTest, currentTile, startTile, endTile);
}
if(Constants::isStaticModeSeleceted == true)
Constants::isPathFound = true;
}
}