Skip to content

Commit a8cac96

Browse files
can now ignore diagonals
1 parent 68fc609 commit a8cac96

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

Grid.hpp

+15-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Grid
4242

4343
bool validCoords(int x, int y);
4444

45-
vector<T*> *getNeighbours(int x, int y);
45+
vector<T*> *getNeighbours(int x, int y, bool includeDiagonals);
4646

4747
~Grid();
4848
};
@@ -56,7 +56,7 @@ class Grid
5656
/// <returns>a list of all of the neighbouring cells around the given
5757
/// grid coordinates if the coordinates are valid and null otherwise</returns>
5858
template <typename T>
59-
vector<T*> *Grid<T>::getNeighbours(int x, int y)
59+
vector<T*> *Grid<T>::getNeighbours(int x, int y, bool includeDiagonals)
6060
{
6161
// check for invalid coords
6262
if (!validCoords(x, y))
@@ -73,11 +73,23 @@ vector<T*> *Grid<T>::getNeighbours(int x, int y)
7373
for (int relativeY = -1; relativeY < 2; relativeY++)
7474
{
7575
// skip the center cell
76-
if (x == 0 && y == 0)
76+
if (relativeX == 0 && relativeY == 0)
7777
{
7878
continue;
7979
}
8080

81+
// skip diagonals if needed
82+
if (includeDiagonals)
83+
{
84+
if ((relativeX == -1 && relativeY == -1)
85+
|| (relativeX == -1 && relativeY == 1)
86+
|| (relativeX == 1 && relativeY == -1)
87+
|| (relativeX == 1 && relativeY == 1))
88+
{
89+
continue;
90+
}
91+
}
92+
8193
int currX = x + relativeX;
8294
int currY = y + relativeY;
8395
// add neighbouring cell if it exists in the grid

Main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using namespace sf;
77
// kinda wack to hardcode these values but whatever
88
int windowWidth = 1000;
99
int windowHeight = 1000;
10-
int gridSize = 50;
10+
int gridSize = 25;
1111

1212
RenderWindow *window;
1313
PathFinder *pathFinder;
@@ -69,7 +69,7 @@ void playerController()
6969
}
7070
else if (Keyboard::isKeyPressed(Keyboard::Key::Tab))
7171
{
72-
if (!pathFinder->drawShortestPath(window))
72+
if (!pathFinder->drawShortestPath(window, false))
7373
{
7474
cout << "missing start/end" << endl;
7575
}

PathFinder.hpp

+21-13
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ class PathFinder
7979

8080
bool setValAt(Vector2i pos, GridValue val);
8181

82-
vector<GridNode *> *getShortestPath();
82+
vector<GridNode *> *getShortestPath(bool includeDiagonals);
8383

84-
bool drawShortestPath(RenderWindow* window);
84+
bool drawShortestPath(RenderWindow* window, bool includeDiagonals);
8585

8686
private:
8787
void initializeNodes();
@@ -340,19 +340,23 @@ vector<PathFinder::GridNode*> *PathFinder::retracePath(GridNode *startNode, Grid
340340
/// </summary>
341341
/// <returns>the shortest path from the start and end positions if
342342
/// there are start and end positions, otherwise return NULL</returns>
343-
vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
343+
vector<PathFinder::GridNode *> *PathFinder::getShortestPath(bool includeDiagonals)
344344
{
345+
// only find shortest path if a start and end exist
345346
if (startPos == NULL || endPos == NULL)
346347
{
347348
return NULL;
348349
}
349-
350+
// get the start and end nodes
350351
GridNode* startNode = grid->getValueAt(startPos->x, startPos->y);
351352
GridNode* endNode = grid->getValueAt(endPos->x, endPos->y);
352353

353-
vector<GridNode*> openList;
354+
// list holds the nodes that CAN be part of the path
355+
vector<GridNode*> openList;
356+
// set holds the nodes that HAVE been picked for a path
354357
unordered_set<GridNode*, NodeHash> closedSet;
355358

359+
// openList starts with the start node
356360
openList.push_back(startNode);
357361

358362
while (openList.size() > 0)
@@ -373,6 +377,7 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
373377
}
374378

375379
// remove lowest cost node from open list
380+
// because we are going to consider it as part of a path
376381
vector<GridNode*>::iterator it = openList.begin() + pos;
377382
openList.erase(it);
378383
// add lowest cost node to closed set
@@ -384,26 +389,28 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
384389
return retracePath(startNode, endNode);
385390
}
386391

392+
// update all neighbour nodes
387393
Vector2i lowestCostPos = lowestCostNode->gridPos;
388-
vector<GridNode*>* neighbours = grid->getNeighbours(lowestCostPos.x, lowestCostPos.y);
394+
vector<GridNode*>* neighbours = grid->getNeighbours(lowestCostPos.x, lowestCostPos.y, includeDiagonals);
389395
for (int i = 0; i < neighbours->size(); i++)
390396
{
391397
GridNode* currNeighbour = (*neighbours)[i];
392-
398+
// if the neighbour is occupied (so can't be moved to) or it
399+
// is already considered as part of the path (is in closed set)
400+
// then ignore it and move onto the next neighbour
393401
if (currNeighbour->val == GridValue::OCCUPIED
394402
|| closedSet.find(currNeighbour) != closedSet.end())
395403
{
396-
// go to next neighbour if this node is either occupied
397-
// or is in the closed set
398404
continue;
399405
}
400406

401407
int newMovementCostToNeighbour =
402408
lowestCostNode->gCost + getDistance(lowestCostNode, currNeighbour);
403-
// check to see if the new cost is less than the current cost
404-
// or if the current neightbour is not in the open list
405409
bool isInOpenSet = find(openList.begin(), openList.end(), currNeighbour)
406410
!= openList.end();
411+
// if the neighbour's current cost is greater than the new cost
412+
// (aka part of a longer path) or the neighbour has not been
413+
// considered for a path yet, then update its values
407414
if (newMovementCostToNeighbour < currNeighbour->gCost
408415
|| !isInOpenSet)
409416
{
@@ -413,6 +420,7 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
413420
// set parent of neighbour to the lowestCostNode
414421
currNeighbour->parentNode = lowestCostNode;
415422
// add neighbour to open list if it is not in it
423+
// (aka has not been considered for a path yet)
416424
if (!isInOpenSet)
417425
{
418426
openList.push_back(currNeighbour);
@@ -423,11 +431,11 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
423431
}
424432
}
425433

426-
bool PathFinder::drawShortestPath(RenderWindow* window)
434+
bool PathFinder::drawShortestPath(RenderWindow* window, bool includeDiagonals)
427435
{
428436
int cellSize = grid->getCellSize();
429437

430-
vector<GridNode*> *path = getShortestPath();
438+
vector<GridNode*> *path = getShortestPath(includeDiagonals);
431439
if (path == NULL)
432440
{
433441
return false;

0 commit comments

Comments
 (0)