tron@2186: /* $Id$ */ tron@2186: truelight@110: /* truelight@110: * This file has the core function for AyStar truelight@110: * AyStar is a fast pathfinding routine and is used for things like truelight@110: * AI_pathfinding and Train_pathfinding. truelight@110: * For more information about AyStar (A* Algorithm), you can look at truelight@110: * http://en.wikipedia.org/wiki/A-star_search_algorithm truelight@110: */ truelight@193: truelight@110: /* truelight@110: * Friendly reminder: truelight@110: * Call (AyStar).free() when you are done with Aystar. It reserves a lot of memory truelight@110: * And when not free'd, it can cause system-crashes. truelight@110: * Also remember that when you stop an algorithm before it is finished, your truelight@110: * should call clear() yourself! truelight@110: */ truelight@110: truelight@110: #include "stdafx.h" Darkvater@1891: #include "openttd.h" truelight@110: #include "aystar.h" truelight@110: // This looks in the Hash if a node exists in ClosedList truelight@110: // If so, it returns the PathNode, else NULL tron@1095: static PathNode *AyStarMain_ClosedList_IsInList(AyStar *aystar, AyStarNode *node) tron@1095: { truelight@110: return (PathNode*)Hash_Get(&aystar->ClosedListHash, node->tile, node->direction); truelight@110: } truelight@110: truelight@110: // This adds a node to the ClosedList truelight@110: // It makes a copy of the data tron@1095: static void AyStarMain_ClosedList_Add(AyStar *aystar, PathNode *node) tron@1095: { truelight@110: // Add a node to the ClosedList truelight@110: PathNode *new_node = malloc(sizeof(PathNode)); truelight@110: *new_node = *node; truelight@110: Hash_Set(&aystar->ClosedListHash, node->node.tile, node->node.direction, new_node); truelight@110: } truelight@110: truelight@110: // Checks if a node is in the OpenList truelight@110: // If so, it returns the OpenListNode, else NULL tron@1095: static OpenListNode *AyStarMain_OpenList_IsInList(AyStar *aystar, AyStarNode *node) tron@1095: { truelight@110: return (OpenListNode*)Hash_Get(&aystar->OpenListHash, node->tile, node->direction); truelight@110: } truelight@110: truelight@110: // Gets the best node from OpenList truelight@110: // returns the best node, or NULL of none is found truelight@110: // Also it deletes the node from the OpenList tron@1095: static OpenListNode *AyStarMain_OpenList_Pop(AyStar *aystar) tron@1095: { truelight@110: // Return the item the Queue returns.. the best next OpenList item. truelight@110: OpenListNode* res = (OpenListNode*)aystar->OpenListQueue.pop(&aystar->OpenListQueue); truelight@110: if (res != NULL) truelight@110: Hash_Delete(&aystar->OpenListHash, res->path.node.tile, res->path.node.direction); truelight@193: truelight@110: return res; truelight@110: } truelight@110: truelight@110: // Adds a node to the OpenList truelight@110: // It makes a copy of node, and puts the pointer of parent in the struct matthijs@1777: static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, AyStarNode *node, int f, int g) tron@1095: { truelight@110: // Add a new Node to the OpenList truelight@110: OpenListNode* new_node = malloc(sizeof(OpenListNode)); truelight@110: new_node->g = g; truelight@110: new_node->path.parent = parent; truelight@110: new_node->path.node = *node; truelight@110: Hash_Set(&aystar->OpenListHash, node->tile, node->direction, new_node); truelight@110: truelight@110: // Add it to the queue truelight@110: aystar->OpenListQueue.push(&aystar->OpenListQueue, new_node, f); truelight@110: } truelight@110: truelight@110: /* truelight@110: * Checks one tile and calculate his f-value truelight@110: * return values: truelight@110: * AYSTAR_DONE : indicates we are done truelight@110: */ truelight@110: int AyStarMain_CheckTile(AyStar *aystar, AyStarNode *current, OpenListNode *parent) { truelight@110: int new_f, new_g, new_h; truelight@110: PathNode *closedlist_parent; truelight@110: OpenListNode *check; truelight@110: truelight@110: // Check the new node against the ClosedList truelight@110: if (AyStarMain_ClosedList_IsInList(aystar, current) != NULL) return AYSTAR_DONE; truelight@193: truelight@110: // Calculate the G-value for this node truelight@110: new_g = aystar->CalculateG(aystar, current, parent); truelight@110: // If the value was INVALID_NODE, we don't do anything with this node truelight@110: if (new_g == AYSTAR_INVALID_NODE) return AYSTAR_DONE; truelight@193: truelight@110: // There should not be given any other error-code.. truelight@110: assert(new_g >= 0); truelight@110: // Add the parent g-value to the new g-value truelight@110: new_g += parent->g; truelight@110: if (aystar->max_path_cost != 0 && (uint)new_g > aystar->max_path_cost) return AYSTAR_DONE; truelight@193: truelight@110: // Calculate the h-value truelight@110: new_h = aystar->CalculateH(aystar, current, parent); truelight@110: // There should not be given any error-code.. truelight@110: assert(new_h >= 0); truelight@193: truelight@110: // The f-value if g + h truelight@110: new_f = new_g + new_h; truelight@193: truelight@110: // Get the pointer to the parent in the ClosedList (the currentone is to a copy of the one in the OpenList) truelight@110: closedlist_parent = AyStarMain_ClosedList_IsInList(aystar, &parent->path.node); truelight@193: truelight@110: // Check if this item is already in the OpenList truelight@110: if ((check = AyStarMain_OpenList_IsInList(aystar, current)) != NULL) { tron@959: uint i; truelight@110: // Yes, check if this g value is lower.. truelight@110: if (new_g > check->g) return AYSTAR_DONE; truelight@110: aystar->OpenListQueue.del(&aystar->OpenListQueue, check, 0); truelight@110: // It is lower, so change it to this item truelight@110: check->g = new_g; truelight@110: check->path.parent = closedlist_parent; truelight@110: /* Copy user data, will probably have changed */ truelight@110: for (i=0;iuser_data);i++) truelight@110: check->path.node.user_data[i] = current->user_data[i]; truelight@110: // Readd him in the OpenListQueue truelight@110: aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f); truelight@110: } else { truelight@110: // A new node, add him to the OpenList matthijs@1777: AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g); truelight@110: } truelight@193: truelight@110: return AYSTAR_DONE; truelight@110: } truelight@110: truelight@110: /* truelight@110: * This function is the core of AyStar. It handles one item and checks truelight@110: * his neighbour items. If they are valid, they are added to be checked too. truelight@110: * return values: truelight@110: * AYSTAR_EMPTY_OPENLIST : indicates all items are tested, and no path truelight@110: * has been found. truelight@110: * AYSTAR_LIMIT_REACHED : Indicates that the max_nodes limit has been truelight@110: * reached. truelight@110: * AYSTAR_FOUND_END_NODE : indicates we found the end. Path_found now is true, and in path is the path found. truelight@110: * AYSTAR_STILL_BUSY : indicates we have done this tile, did not found the path yet, and have items left to try. truelight@110: */ truelight@110: int AyStarMain_Loop(AyStar *aystar) { truelight@110: int i, r; truelight@193: truelight@110: // Get the best node from OpenList truelight@110: OpenListNode *current = AyStarMain_OpenList_Pop(aystar); truelight@110: // If empty, drop an error truelight@110: if (current == NULL) return AYSTAR_EMPTY_OPENLIST; truelight@193: truelight@110: // Check for end node and if found, return that code truelight@1617: if (aystar->EndNodeCheck(aystar, current) == AYSTAR_FOUND_END_NODE) { truelight@110: if (aystar->FoundEndNode != NULL) truelight@110: aystar->FoundEndNode(aystar, current); truelight@110: free(current); truelight@110: return AYSTAR_FOUND_END_NODE; truelight@110: } truelight@193: truelight@110: // Add the node to the ClosedList truelight@110: AyStarMain_ClosedList_Add(aystar, ¤t->path); truelight@110: truelight@110: // Load the neighbours truelight@110: aystar->GetNeighbours(aystar, current); truelight@193: truelight@110: // Go through all neighbours truelight@110: for (i=0;inum_neighbours;i++) { truelight@110: // Check and add them to the OpenList if needed truelight@110: r = aystar->checktile(aystar, &aystar->neighbours[i], current); truelight@110: } truelight@193: truelight@110: // Free the node truelight@110: free(current); truelight@193: truelight@110: if (aystar->max_search_nodes != 0 && Hash_Size(&aystar->ClosedListHash) >= aystar->max_search_nodes) truelight@110: /* We've expanded enough nodes */ truelight@110: return AYSTAR_LIMIT_REACHED; truelight@110: else truelight@110: // Return that we are still busy truelight@110: return AYSTAR_STILL_BUSY; truelight@110: } truelight@110: truelight@110: /* truelight@110: * This function frees the memory it allocated truelight@110: */ truelight@110: void AyStarMain_Free(AyStar *aystar) { truelight@110: aystar->OpenListQueue.free(&aystar->OpenListQueue, false); truelight@110: /* 2nd argument above is false, below is true, to free the values only truelight@110: * once */ truelight@110: delete_Hash(&aystar->OpenListHash, true); truelight@110: delete_Hash(&aystar->ClosedListHash, true); truelight@110: #ifdef AYSTAR_DEBUG truelight@110: printf("[AyStar] Memory free'd\n"); truelight@110: #endif truelight@110: } truelight@110: truelight@110: /* truelight@110: * This function make the memory go back to zero truelight@110: * This function should be called when you are using the same instance again. truelight@110: */ truelight@110: void AyStarMain_Clear(AyStar *aystar) { truelight@110: // Clean the Queue, but not the elements within. That will be done by truelight@110: // the hash. truelight@110: aystar->OpenListQueue.clear(&aystar->OpenListQueue, false); truelight@110: // Clean the hashes truelight@110: clear_Hash(&aystar->OpenListHash, true); truelight@110: clear_Hash(&aystar->ClosedListHash, true); truelight@110: truelight@110: #ifdef AYSTAR_DEBUG truelight@110: printf("[AyStar] Cleared AyStar\n"); truelight@110: #endif truelight@110: } truelight@110: truelight@110: /* truelight@110: * This is the function you call to run AyStar. truelight@110: * return values: truelight@110: * AYSTAR_FOUND_END_NODE : indicates we found an end node. truelight@110: * AYSTAR_NO_PATH : indicates that there was no path found. truelight@110: * 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. truelight@110: * When the algorithm is done (when the return value is not AYSTAR_STILL_BUSY) truelight@110: * aystar->clear() is called. Note that when you stop the algorithm halfway, truelight@110: * you should still call clear() yourself! truelight@110: */ truelight@110: int AyStarMain_Main(AyStar *aystar) { truelight@110: int r, i = 0; truelight@110: // Loop through the OpenList miham@826: // Quit if result is no AYSTAR_STILL_BUSY or is more than loops_per_tick truelight@110: while ((r = aystar->loop(aystar)) == AYSTAR_STILL_BUSY && (aystar->loops_per_tick == 0 || ++i < aystar->loops_per_tick)) { } truelight@110: #ifdef AYSTAR_DEBUG truelight@110: if (r == AYSTAR_FOUND_END_NODE) truelight@110: printf("[AyStar] Found path!\n"); truelight@110: else if (r == AYSTAR_EMPTY_OPENLIST) truelight@110: printf("[AyStar] OpenList run dry, no path found\n"); truelight@110: else if (r == AYSTAR_LIMIT_REACHED) truelight@110: printf("[AyStar] Exceeded search_nodes, no path found\n"); truelight@110: #endif hackykid@2008: hackykid@2008: if (aystar->BeforeExit != NULL) hackykid@2008: aystar->BeforeExit(aystar); hackykid@2008: truelight@110: if (r != AYSTAR_STILL_BUSY) truelight@110: /* We're done, clean up */ truelight@110: aystar->clear(aystar); truelight@193: truelight@110: // Check result-value truelight@110: if (r == AYSTAR_FOUND_END_NODE) return AYSTAR_FOUND_END_NODE; truelight@110: // Check if we have some left in the OpenList truelight@110: if (r == AYSTAR_EMPTY_OPENLIST || r == AYSTAR_LIMIT_REACHED) return AYSTAR_NO_PATH; truelight@110: truelight@110: // Return we are still busy truelight@110: return AYSTAR_STILL_BUSY; truelight@110: } truelight@110: truelight@110: /* truelight@110: * Adds a node from where to start an algorithm. Multiple nodes can be added truelight@110: * if wanted. You should make sure that clear() is called before adding nodes truelight@110: * if the AyStar has been used before (though the normal main loop calls truelight@193: * clear() automatically when the algorithm finishes matthijs@1777: * g is the cost for starting with this node. truelight@110: */ matthijs@1777: void AyStarMain_AddStartNode(AyStar *aystar, AyStarNode *start_node, uint g) { truelight@110: #ifdef AYSTAR_DEBUG tron@926: printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n", tron@926: TileX(start_node->tile), TileY(start_node->tile), start_node->direction); truelight@110: #endif matthijs@1777: AyStarMain_OpenList_Add(aystar, NULL, start_node, 0, g); truelight@110: } truelight@110: truelight@110: void init_AyStar(AyStar* aystar, Hash_HashProc hash, uint num_buckets) { truelight@110: // Allocated the Hash for the OpenList and ClosedList truelight@110: init_Hash(&aystar->OpenListHash, hash, num_buckets); truelight@110: init_Hash(&aystar->ClosedListHash, hash, num_buckets); truelight@110: truelight@110: // Set up our sorting queue truelight@110: // BinaryHeap allocates a block of 1024 nodes truelight@110: // When thatone gets full it reserves an otherone, till this number truelight@110: // That is why it can stay this high truelight@110: init_BinaryHeap(&aystar->OpenListQueue, 102400); truelight@110: truelight@110: aystar->addstart = AyStarMain_AddStartNode; truelight@110: aystar->main = AyStarMain_Main; truelight@110: aystar->loop = AyStarMain_Loop; truelight@110: aystar->free = AyStarMain_Free; truelight@110: aystar->clear = AyStarMain_Clear; truelight@110: aystar->checktile = AyStarMain_CheckTile; truelight@110: }