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
|
void AStarSearch::calculateGScore(int x, int y, int prevScore, std::vector<std::vector<bool> > &visitedGScore)
{
/*Get new score*/
float newScore = prevScore + 1;
/*If new score is greater than old score, and we have already visited the node, return*/
if (visitedGScore[y][x] == true && nodes[y][x].getG() < newScore)
return;
nodes[y][x].setG(newScore);
/*Set visited*/
visitedGScore[y][x] = true;
/*Recursively call calculateGScore for surrounding nodes
We need to test to make sure we don't go out of bounds. We also have to check if the node is walkable.
Note that we cant' go out of bounds in the if statement because the second condition won't be tested if the first
is false.*/
if (x > 0 && walkableNodes[y][x - 1])
calculateGScore(x - 1, y, newScore, visitedGScore);
if (x < width - 1 && walkableNodes[y][x + 1])
calculateGScore(x + 1, y, newScore, visitedGScore);
if (y > 0 && walkableNodes[y - 1][x])
calculateGScore(x, y - 1, newScore, visitedGScore);
if (y < height - 1 && walkableNodes[y + 1][x])
calculateGScore(x, y + 1, newScore, visitedGScore);
}
|