Codechange: Make AyStarStatus an enum class. (#13129)

This enforces type-safety.
This commit is contained in:
Peter Nelson 2024-11-30 14:23:32 +00:00 committed by GitHub
parent 8d394c697c
commit 61cbdef92d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 33 deletions

View File

@ -1219,7 +1219,7 @@ struct River_UserData {
/* AyStar callback for checking whether we reached our destination. */ /* AyStar callback for checking whether we reached our destination. */
static AyStarStatus River_EndNodeCheck(const AyStar *aystar, const PathNode *current) static AyStarStatus River_EndNodeCheck(const AyStar *aystar, const PathNode *current)
{ {
return current->GetTile() == *(TileIndex*)aystar->user_target ? AYSTAR_FOUND_END_NODE : AYSTAR_DONE; return current->GetTile() == *(TileIndex*)aystar->user_target ? AyStarStatus::FoundEndNode : AyStarStatus::Done;
} }
/* AyStar callback for getting the cost of the current node. */ /* AyStar callback for getting the cost of the current node. */

View File

@ -84,24 +84,24 @@ void AyStar::CheckTile(AyStarNode *current, PathNode *parent)
* This function is the core of %AyStar. It handles one item and checks * This function is the core of %AyStar. It handles one item and checks
* its neighbour items. If they are valid, they are added to be checked too. * its neighbour items. If they are valid, they are added to be checked too.
* @return Possible values: * @return Possible values:
* - #AYSTAR_EMPTY_OPENLIST : indicates all items are tested, and no path has been found. * - #AyStarStatus::EmptyOpenList
* - #AYSTAR_LIMIT_REACHED : Indicates that the max_search_nodes limit has been reached. * - #AyStarStatus::LimitReached
* - #AYSTAR_FOUND_END_NODE : indicates we found the end. Path_found now is true, and in path is the path found. * - #AyStarStatus::FoundEndNode
* - #AYSTAR_STILL_BUSY : indicates we have done this tile, did not found the path yet, and have items left to try. * - #AyStarStatus::StillBusy
*/ */
AyStarStatus AyStar::Loop() AyStarStatus AyStar::Loop()
{ {
/* Get the best node from OpenList */ /* Get the best node from OpenList */
PathNode *current = this->nodes.PopBestOpenNode(); PathNode *current = this->nodes.PopBestOpenNode();
/* If empty, drop an error */ /* If empty, drop an error */
if (current == nullptr) return AYSTAR_EMPTY_OPENLIST; if (current == nullptr) return AyStarStatus::EmptyOpenList;
/* Check for end node and if found, return that code */ /* Check for end node and if found, return that code */
if (this->EndNodeCheck(this, current) == AYSTAR_FOUND_END_NODE && current->parent != nullptr) { if (this->EndNodeCheck(this, current) == AyStarStatus::FoundEndNode && current->parent != nullptr) {
if (this->FoundEndNode != nullptr) { if (this->FoundEndNode != nullptr) {
this->FoundEndNode(this, current); this->FoundEndNode(this, current);
} }
return AYSTAR_FOUND_END_NODE; return AyStarStatus::FoundEndNode;
} }
/* Add the node to the ClosedList */ /* Add the node to the ClosedList */
@ -118,43 +118,43 @@ AyStarStatus AyStar::Loop()
if (this->max_search_nodes != 0 && this->nodes.ClosedCount() >= this->max_search_nodes) { if (this->max_search_nodes != 0 && this->nodes.ClosedCount() >= this->max_search_nodes) {
/* We've expanded enough nodes */ /* We've expanded enough nodes */
return AYSTAR_LIMIT_REACHED; return AyStarStatus::LimitReached;
} else { } else {
/* Return that we are still busy */ /* Return that we are still busy */
return AYSTAR_STILL_BUSY; return AyStarStatus::StillBusy;
} }
} }
/** /**
* This is the function you call to run AyStar. * This is the function you call to run AyStar.
* @return Possible values: * @return Possible values:
* - #AYSTAR_FOUND_END_NODE : indicates we found an end node. * - #AyStarStatus::FoundEndNode
* - #AYSTAR_NO_PATH : indicates that there was no path found. * - #AyStarStatus::NoPath
* - #AYSTAR_STILL_BUSY : indicates we have done some checked, that we did not found the path yet, and that we still have items left to try. * - #AyStarStatus::StillBusy
* @note When the algorithm is done (when the return value is not #AYSTAR_STILL_BUSY) #Clear() is called automatically. * @note When the algorithm is done (when the return value is not #AyStarStatus::StillBusy) #Clear() is called automatically.
* When you stop the algorithm halfway, you should call #Clear() yourself! * When you stop the algorithm halfway, you should call #Clear() yourself!
*/ */
AyStarStatus AyStar::Main() AyStarStatus AyStar::Main()
{ {
AyStarStatus r = AYSTAR_FOUND_END_NODE; AyStarStatus r = AyStarStatus::FoundEndNode;
int i = 0; int i = 0;
/* Loop through the OpenList /* Loop through the OpenList
* Quit if result is no AYSTAR_STILL_BUSY or is more than loops_per_tick */ * Quit if result is no AyStarStatus::StillBusy or is more than loops_per_tick */
while ((r = this->Loop()) == AYSTAR_STILL_BUSY && (this->loops_per_tick == 0 || ++i < this->loops_per_tick)) { } while ((r = this->Loop()) == AyStarStatus::StillBusy && (this->loops_per_tick == 0 || ++i < this->loops_per_tick)) { }
#ifdef AYSTAR_DEBUG #ifdef AYSTAR_DEBUG
switch (r) { switch (r) {
case AYSTAR_FOUND_END_NODE: Debug(misc, 0, "[AyStar] Found path!"); break; case AyStarStatus::FoundEndNode: Debug(misc, 0, "[AyStar] Found path!"); break;
case AYSTAR_EMPTY_OPENLIST: Debug(misc, 0, "[AyStar] OpenList run dry, no path found"); break; case AyStarStatus::EmptyOpenList: Debug(misc, 0, "[AyStar] OpenList run dry, no path found"); break;
case AYSTAR_LIMIT_REACHED: Debug(misc, 0, "[AyStar] Exceeded search_nodes, no path found"); break; case AyStarStatus::LimitReached: Debug(misc, 0, "[AyStar] Exceeded search_nodes, no path found"); break;
default: break; default: break;
} }
#endif #endif
switch (r) { switch (r) {
case AYSTAR_FOUND_END_NODE: return AYSTAR_FOUND_END_NODE; case AyStarStatus::FoundEndNode: return AyStarStatus::FoundEndNode;
case AYSTAR_EMPTY_OPENLIST: case AyStarStatus::EmptyOpenList:
case AYSTAR_LIMIT_REACHED: return AYSTAR_NO_PATH; case AyStarStatus::LimitReached: return AyStarStatus::NoPath;
default: return AYSTAR_STILL_BUSY; default: return AyStarStatus::StillBusy;
} }
} }

View File

@ -28,13 +28,13 @@
static const int AYSTAR_DEF_MAX_SEARCH_NODES = 10000; ///< Reference limit for #AyStar::max_search_nodes static const int AYSTAR_DEF_MAX_SEARCH_NODES = 10000; ///< Reference limit for #AyStar::max_search_nodes
/** Return status of #AyStar methods. */ /** Return status of #AyStar methods. */
enum AyStarStatus { enum class AyStarStatus : uint8_t {
AYSTAR_FOUND_END_NODE, ///< An end node was found. FoundEndNode, ///< An end node was found.
AYSTAR_EMPTY_OPENLIST, ///< All items are tested, and no path has been found. EmptyOpenList, ///< All items are tested, and no path has been found.
AYSTAR_STILL_BUSY, ///< Some checking was done, but no path found yet, and there are still items left to try. StillBusy, ///< Some checking was done, but no path found yet, and there are still items left to try.
AYSTAR_NO_PATH, ///< No path to the goal was found. NoPath, ///< No path to the goal was found.
AYSTAR_LIMIT_REACHED, ///< The #AyStar::max_search_nodes limit has been reached, aborting search. LimitReached, ///< The #AyStar::max_search_nodes limit has been reached, aborting search.
AYSTAR_DONE, ///< Not an end-tile, or wrong direction. Done, ///< Not an end-tile, or wrong direction.
}; };
static const int AYSTAR_INVALID_NODE = -1; ///< Item is not valid (for example, not walkable). static const int AYSTAR_INVALID_NODE = -1; ///< Item is not valid (for example, not walkable).
@ -59,8 +59,8 @@ struct AyStar;
* don't try to enter the file tile with a 90-degree curve. So please, leave * don't try to enter the file tile with a 90-degree curve. So please, leave
* this an #OpenListNode, it works just fine. * this an #OpenListNode, it works just fine.
* @return Status of the node: * @return Status of the node:
* - #AYSTAR_FOUND_END_NODE : indicates this is the end tile * - #AyStarStatus::FoundEndNode : indicates this is the end tile
* - #AYSTAR_DONE : indicates this is not the end tile (or direction was wrong) * - #AyStarStatus::Done : indicates this is not the end tile (or direction was wrong)
*/ */
typedef AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current); typedef AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current);