@@ -79,9 +79,9 @@ class PathFinder
79
79
80
80
bool setValAt (Vector2i pos, GridValue val);
81
81
82
- vector<GridNode *> *getShortestPath ();
82
+ vector<GridNode *> *getShortestPath (bool includeDiagonals );
83
83
84
- bool drawShortestPath (RenderWindow* window);
84
+ bool drawShortestPath (RenderWindow* window, bool includeDiagonals );
85
85
86
86
private:
87
87
void initializeNodes ();
@@ -340,19 +340,23 @@ vector<PathFinder::GridNode*> *PathFinder::retracePath(GridNode *startNode, Grid
340
340
// / </summary>
341
341
// / <returns>the shortest path from the start and end positions if
342
342
// / there are start and end positions, otherwise return NULL</returns>
343
- vector<PathFinder::GridNode *> *PathFinder::getShortestPath ()
343
+ vector<PathFinder::GridNode *> *PathFinder::getShortestPath (bool includeDiagonals )
344
344
{
345
+ // only find shortest path if a start and end exist
345
346
if (startPos == NULL || endPos == NULL )
346
347
{
347
348
return NULL ;
348
349
}
349
-
350
+ // get the start and end nodes
350
351
GridNode* startNode = grid->getValueAt (startPos->x , startPos->y );
351
352
GridNode* endNode = grid->getValueAt (endPos->x , endPos->y );
352
353
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
354
357
unordered_set<GridNode*, NodeHash> closedSet;
355
358
359
+ // openList starts with the start node
356
360
openList.push_back (startNode);
357
361
358
362
while (openList.size () > 0 )
@@ -373,6 +377,7 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
373
377
}
374
378
375
379
// remove lowest cost node from open list
380
+ // because we are going to consider it as part of a path
376
381
vector<GridNode*>::iterator it = openList.begin () + pos;
377
382
openList.erase (it);
378
383
// add lowest cost node to closed set
@@ -384,26 +389,28 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
384
389
return retracePath (startNode, endNode);
385
390
}
386
391
392
+ // update all neighbour nodes
387
393
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 );
389
395
for (int i = 0 ; i < neighbours->size (); i++)
390
396
{
391
397
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
393
401
if (currNeighbour->val == GridValue::OCCUPIED
394
402
|| closedSet.find (currNeighbour) != closedSet.end ())
395
403
{
396
- // go to next neighbour if this node is either occupied
397
- // or is in the closed set
398
404
continue ;
399
405
}
400
406
401
407
int newMovementCostToNeighbour =
402
408
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
405
409
bool isInOpenSet = find (openList.begin (), openList.end (), currNeighbour)
406
410
!= 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
407
414
if (newMovementCostToNeighbour < currNeighbour->gCost
408
415
|| !isInOpenSet)
409
416
{
@@ -413,6 +420,7 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
413
420
// set parent of neighbour to the lowestCostNode
414
421
currNeighbour->parentNode = lowestCostNode;
415
422
// add neighbour to open list if it is not in it
423
+ // (aka has not been considered for a path yet)
416
424
if (!isInOpenSet)
417
425
{
418
426
openList.push_back (currNeighbour);
@@ -423,11 +431,11 @@ vector<PathFinder::GridNode *> *PathFinder::getShortestPath()
423
431
}
424
432
}
425
433
426
- bool PathFinder::drawShortestPath (RenderWindow* window)
434
+ bool PathFinder::drawShortestPath (RenderWindow* window, bool includeDiagonals )
427
435
{
428
436
int cellSize = grid->getCellSize ();
429
437
430
- vector<GridNode*> *path = getShortestPath ();
438
+ vector<GridNode*> *path = getShortestPath (includeDiagonals );
431
439
if (path == NULL )
432
440
{
433
441
return false ;
0 commit comments