54 return res; |
54 return res; |
55 } |
55 } |
56 |
56 |
57 // Adds a node to the OpenList |
57 // Adds a node to the OpenList |
58 // It makes a copy of node, and puts the pointer of parent in the struct |
58 // It makes a copy of node, and puts the pointer of parent in the struct |
59 static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, AyStarNode *node, int f, int g, int userdata) |
59 static void AyStarMain_OpenList_Add(AyStar *aystar, PathNode *parent, AyStarNode *node, int f, int g) |
60 { |
60 { |
61 // Add a new Node to the OpenList |
61 // Add a new Node to the OpenList |
62 OpenListNode* new_node = malloc(sizeof(OpenListNode)); |
62 OpenListNode* new_node = malloc(sizeof(OpenListNode)); |
63 new_node->g = g; |
63 new_node->g = g; |
64 new_node->path.parent = parent; |
64 new_node->path.parent = parent; |
118 check->path.node.user_data[i] = current->user_data[i]; |
118 check->path.node.user_data[i] = current->user_data[i]; |
119 // Readd him in the OpenListQueue |
119 // Readd him in the OpenListQueue |
120 aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f); |
120 aystar->OpenListQueue.push(&aystar->OpenListQueue, check, new_f); |
121 } else { |
121 } else { |
122 // A new node, add him to the OpenList |
122 // A new node, add him to the OpenList |
123 AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g, 0); |
123 AyStarMain_OpenList_Add(aystar, closedlist_parent, current, new_f, new_g); |
124 } |
124 } |
125 |
125 |
126 return AYSTAR_DONE; |
126 return AYSTAR_DONE; |
127 } |
127 } |
128 |
128 |
246 /* |
246 /* |
247 * Adds a node from where to start an algorithm. Multiple nodes can be added |
247 * Adds a node from where to start an algorithm. Multiple nodes can be added |
248 * if wanted. You should make sure that clear() is called before adding nodes |
248 * if wanted. You should make sure that clear() is called before adding nodes |
249 * if the AyStar has been used before (though the normal main loop calls |
249 * if the AyStar has been used before (though the normal main loop calls |
250 * clear() automatically when the algorithm finishes |
250 * clear() automatically when the algorithm finishes |
251 */ |
251 * g is the cost for starting with this node. |
252 void AyStarMain_AddStartNode(AyStar *aystar, AyStarNode *start_node) { |
252 */ |
|
253 void AyStarMain_AddStartNode(AyStar *aystar, AyStarNode *start_node, uint g) { |
253 #ifdef AYSTAR_DEBUG |
254 #ifdef AYSTAR_DEBUG |
254 printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n", |
255 printf("[AyStar] Starting A* Algorithm from node (%d, %d, %d)\n", |
255 TileX(start_node->tile), TileY(start_node->tile), start_node->direction); |
256 TileX(start_node->tile), TileY(start_node->tile), start_node->direction); |
256 #endif |
257 #endif |
257 AyStarMain_OpenList_Add(aystar, NULL, start_node, 0, 0, 0); |
258 AyStarMain_OpenList_Add(aystar, NULL, start_node, 0, g); |
258 } |
259 } |
259 |
260 |
260 void init_AyStar(AyStar* aystar, Hash_HashProc hash, uint num_buckets) { |
261 void init_AyStar(AyStar* aystar, Hash_HashProc hash, uint num_buckets) { |
261 // Allocated the Hash for the OpenList and ClosedList |
262 // Allocated the Hash for the OpenList and ClosedList |
262 init_Hash(&aystar->OpenListHash, hash, num_buckets); |
263 init_Hash(&aystar->OpenListHash, hash, num_buckets); |