From bc939ccf1b14804bfcdb5fc2d8255eb385008db6 Mon Sep 17 00:00:00 2001 From: Patrick Parker Date: Sun, 29 Jul 2007 16:31:00 +0000 Subject: [PATCH] code cleanup: remove an unnecessary goto (http://www.acm.org/classics/oct95/) - "Go To Statements Considered Harmful" --- src/astarsearch.cpp | 65 +++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/src/astarsearch.cpp b/src/astarsearch.cpp index 0202eddaf1d..7d6592a426a 100644 --- a/src/astarsearch.cpp +++ b/src/astarsearch.cpp @@ -166,48 +166,49 @@ paths::route a_star_search(gamemap::location const &src, gamemap::location const } a_star_init(src, dst, openList, aStarGameWorld, parWidth, parHeight, vectLocation, teleports, locNbTeleport); - - while (!openList.empty()) + + bool routeSolved = false; + while (!routeSolved && !openList.empty()) { locCurNode = openList.front(); wassert(locCurNode != NULL); //if we have found a solution if (locCurNode->loc == dst) - { - locDestNode = locCurNode; + { + routeSolved = true; + } else { + std::pop_heap(openList.begin(), openList.end(), compare_lt_a_star_node); + openList.pop_back(); - LOG_PF << "found solution; calculating it...\n"; - while (locCurNode != NULL) - { - locRoute.steps.push_back(locCurNode->loc); - locCurNode = locCurNode->nodeParent; - } - std::reverse(locRoute.steps.begin(), locRoute.steps.end()); - locRoute.move_left = int(locDestNode->g); + wassert(locCurNode->isInCloseList == false); + locCurNode->isInCloseList = true; - wassert(locRoute.steps.front() == src); - wassert(locRoute.steps.back() == dst); - - LOG_PF << "exiting a* search (solved)\n"; - goto label_AStarSearch_end; + a_star_explore_neighbours(dst, stop_at, costCalculator, parWidth, parHeight, + teleports, vectLocation, openList, aStarGameWorld, locCurNode, locNbTeleport); } - - std::pop_heap(openList.begin(), openList.end(), compare_lt_a_star_node); - openList.pop_back(); - - wassert(locCurNode->isInCloseList == false); - locCurNode->isInCloseList = true; - - a_star_explore_neighbours(dst, stop_at, costCalculator, parWidth, parHeight, teleports, vectLocation, - openList, aStarGameWorld, locCurNode, locNbTeleport); - + } + if(routeSolved) { + locDestNode = locCurNode; + + LOG_PF << "found solution; calculating it...\n"; + while (locCurNode != NULL) + { + locRoute.steps.push_back(locCurNode->loc); + locCurNode = locCurNode->nodeParent; + } + std::reverse(locRoute.steps.begin(), locRoute.steps.end()); + locRoute.move_left = int(locDestNode->g); + + wassert(locRoute.steps.front() == src); + wassert(locRoute.steps.back() == dst); + + LOG_PF << "exiting a* search (solved)\n"; + } else { + //route not solved + LOG_PF << "aborted a* search\n"; + locRoute.move_left = int(costCalculator->getNoPathValue()); } - - LOG_PF << "aborted a* search\n"; - locRoute.move_left = int(costCalculator->getNoPathValue()); - -label_AStarSearch_end: openList.clear(); aStarGameWorld.clear(); return locRoute;