26 #include "../../stdafx.h" 27 #include "../../core/alloc_func.hpp" 30 #include "../../safeguards.h" 50 PathNode *new_node = MallocT<PathNode>(1);
90 new_node->path.
parent = parent;
91 new_node->path.node = *node;
103 int new_f, new_g, new_h;
111 new_g = this->CalculateG(
this, current, parent);
122 new_h = this->CalculateH(
this, current, parent);
127 new_f = new_g + new_h;
137 if (new_g > check->g)
return;
141 check->path.
parent = closedlist_parent;
143 for (i = 0; i <
lengthof(current->user_data); i++) {
144 check->path.node.user_data[i] = current->user_data[i];
150 this->
OpenListAdd(closedlist_parent, current, new_f, new_g);
174 if (this->FoundEndNode != NULL) {
175 this->FoundEndNode(
this, current);
185 this->GetNeighbours(
this, current);
188 for (i = 0; i < this->num_neighbours; i++) {
190 this->
CheckTile(&this->neighbours[i], current);
216 printf(
"[AyStar] Memory free'd\n");
234 printf(
"[AyStar] Cleared AyStar\n");
285 printf(
"[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n",
286 TileX(start_node->tile),
TileY(start_node->tile), start_node->direction);
void Init(Hash_HashProc hash, uint num_buckets)
Initialize an AyStar.
OpenListNode * OpenListIsInList(const AyStarNode *node)
Check whether a node is in the open list.
void Free(bool free_values)
Frees the queue, by reclaiming all memory allocated by it.
void Delete(bool free_values)
Deletes the hash and cleans up.
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
No path to the goal was found.
void Clear(bool free_values)
Cleans the hash, but keeps the memory allocated.
byte loops_per_tick
How many loops are there called before Main() gives control back to the caller. 0 = until done...
uint max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
static uint TileX(TileIndex tile)
Get the X component of a tile.
The #max_nodes limit has been reached, aborting search.
uint GetSize() const
Gets the current size of the hash.
bool Delete(void *item, int priority)
Deletes the item from the queue.
This file has the header for AyStar.
void * Set(uint key1, uint key2, void *value)
Sets the value associated with the given key pair to the given value.
BinaryHeap openlist_queue
The open queue.
void Clear(bool free_values)
Clears the queue, by removing all values from it.
void AddStartNode(AyStarNode *start_node, uint g)
Adds a node from where to start an algorithm.
void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g)
Adds a node to the open list.
uint max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
void CheckTile(AyStarNode *current, OpenListNode *parent)
Checks one tile and calculate its f-value.
All items are tested, and no path has been found.
void * DeleteValue(uint key1, uint key2)
Deletes the value with the specified key pair from the hash and returns that value.
uint Hash_HashProc(uint key1, uint key2)
Generates a hash code from the given key pair.
#define lengthof(x)
Return the length of an fixed size array.
void * Pop()
Pops the first element from the queue.
PathNode * parent
The parent of this item.
void Init(Hash_HashProc *hash, uint num_buckets)
Builds a new hash in an existing struct.
Hash closedlist_hash
The actual closed list.
Some checking was done, but no path found yet, and there are still items left to try.
PathNode * ClosedListIsInList(const AyStarNode *node)
This looks in the hash whether a node exists in the closed list.
void * Get(uint key1, uint key2) const
Gets the value associated with the given key pair, or NULL when it is not present.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
int Main()
This is the function you call to run AyStar.
OpenListNode * OpenListPop()
Gets the best node from the open list.
void Init(uint max_size)
Initializes a binary heap and allocates internal memory for maximum of max_size elements.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
void ClosedListAdd(const PathNode *node)
This adds a node to the closed list.
void Free()
This function frees the memory it allocated.
Hash openlist_hash
An extra hash to speed up the process of looking up an element in the open list.
bool Push(void *item, int priority)
Pushes an element into the queue, at the appropriate place for the queue.
void Clear()
This function make the memory go back to zero.
int Loop()
This function is the core of AyStar.