2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 /** @file aystar.cpp Implementation of A*.
10 * This file has the core function for %AyStar.
11 * %AyStar is a fast path finding routine and is used for things like AI path finding and Train path finding.
12 * For more information about %AyStar (A* Algorithm), you can look at
13 * <A HREF='http://en.wikipedia.org/wiki/A-star_search_algorithm'>http://en.wikipedia.org/wiki/A-star_search_algorithm</A>.
16 #include "../../stdafx.h"
19 #include "../../safeguards.h"
22 * Adds a node to the open list.
23 * It makes a copy of node, and puts the pointer of parent in the struct.
25 void AyStar::OpenListAdd(PathNode
*parent
, const AyStarNode
*node
, int f
, int g
)
27 /* Add a new Node to the OpenList */
28 PathNode
*new_node
= this->nodes
.CreateNewNode();
29 new_node
->Set(parent
, node
->m_tile
, node
->m_td
, true);
30 new_node
->m_estimate
= f
;
32 this->nodes
.InsertOpenNode(*new_node
);
36 * Checks one tile and calculate its f-value
38 void AyStar::CheckTile(AyStarNode
*current
, PathNode
*parent
)
40 /* Check the new node against the ClosedList */
41 if (this->nodes
.FindClosedNode(*current
) != nullptr) return;
43 /* Calculate the G-value for this node */
44 int new_g
= this->CalculateG(this, current
, parent
);
45 /* If the value was INVALID_NODE, we don't do anything with this node */
46 if (new_g
== AYSTAR_INVALID_NODE
) return;
48 /* There should not be given any other error-code.. */
50 /* Add the parent g-value to the new g-value */
51 new_g
+= parent
->m_cost
;
52 if (this->max_path_cost
!= 0 && new_g
> this->max_path_cost
) return;
54 /* Calculate the h-value */
55 int new_h
= this->CalculateH(this, current
, parent
);
56 /* There should not be given any error-code.. */
59 /* The f-value if g + h */
60 int new_f
= new_g
+ new_h
;
62 /* Get the pointer to the parent in the ClosedList (the current one is to a copy of the one in the OpenList) */
63 PathNode
*closedlist_parent
= this->nodes
.FindClosedNode(parent
->m_key
);
65 /* Check if this item is already in the OpenList */
66 PathNode
*check
= this->nodes
.FindOpenNode(*current
);
67 if (check
!= nullptr) {
68 /* Yes, check if this g value is lower.. */
69 if (new_g
> check
->m_cost
) return;
70 this->nodes
.PopOpenNode(check
->m_key
);
71 /* It is lower, so change it to this item */
72 check
->m_estimate
= new_f
;
73 check
->m_cost
= new_g
;
74 check
->m_parent
= closedlist_parent
;
75 /* Re-add it in the openlist_queue. */
76 this->nodes
.InsertOpenNode(*check
);
78 /* A new node, add it to the OpenList */
79 this->OpenListAdd(closedlist_parent
, current
, new_f
, new_g
);
84 * This function is the core of %AyStar. It handles one item and checks
85 * its neighbour items. If they are valid, they are added to be checked too.
86 * @return Possible values:
87 * - #AYSTAR_EMPTY_OPENLIST : indicates all items are tested, and no path has been found.
88 * - #AYSTAR_LIMIT_REACHED : Indicates that the max_search_nodes limit has been reached.
89 * - #AYSTAR_FOUND_END_NODE : indicates we found the end. Path_found now is true, and in path is the path found.
90 * - #AYSTAR_STILL_BUSY : indicates we have done this tile, did not found the path yet, and have items left to try.
94 /* Get the best node from OpenList */
95 PathNode
*current
= this->nodes
.PopBestOpenNode();
96 /* If empty, drop an error */
97 if (current
== nullptr) return AYSTAR_EMPTY_OPENLIST
;
99 /* Check for end node and if found, return that code */
100 if (this->EndNodeCheck(this, current
) == AYSTAR_FOUND_END_NODE
&& current
->m_parent
!= nullptr) {
101 if (this->FoundEndNode
!= nullptr) {
102 this->FoundEndNode(this, current
);
104 return AYSTAR_FOUND_END_NODE
;
107 /* Add the node to the ClosedList */
108 this->nodes
.InsertClosedNode(*current
);
110 /* Load the neighbours */
111 this->GetNeighbours(this, current
);
113 /* Go through all neighbours */
114 for (auto &neighbour
: this->neighbours
) {
115 /* Check and add them to the OpenList if needed */
116 this->CheckTile(&neighbour
, current
);
119 if (this->max_search_nodes
!= 0 && this->nodes
.ClosedCount() >= this->max_search_nodes
) {
120 /* We've expanded enough nodes */
121 return AYSTAR_LIMIT_REACHED
;
123 /* Return that we are still busy */
124 return AYSTAR_STILL_BUSY
;
129 * This is the function you call to run AyStar.
130 * @return Possible values:
131 * - #AYSTAR_FOUND_END_NODE : indicates we found an end node.
132 * - #AYSTAR_NO_PATH : indicates that there was no path found.
133 * - #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.
134 * @note When the algorithm is done (when the return value is not #AYSTAR_STILL_BUSY) #Clear() is called automatically.
135 * When you stop the algorithm halfway, you should call #Clear() yourself!
140 /* Loop through the OpenList
141 * Quit if result is no AYSTAR_STILL_BUSY or is more than loops_per_tick */
142 while ((r
= this->Loop()) == AYSTAR_STILL_BUSY
&& (this->loops_per_tick
== 0 || ++i
< this->loops_per_tick
)) { }
145 case AYSTAR_FOUND_END_NODE
: Debug(misc
, 0, "[AyStar] Found path!"); break;
146 case AYSTAR_EMPTY_OPENLIST
: Debug(misc
, 0, "[AyStar] OpenList run dry, no path found"); break;
147 case AYSTAR_LIMIT_REACHED
: Debug(misc
, 0, "[AyStar] Exceeded search_nodes, no path found"); break;
153 case AYSTAR_FOUND_END_NODE
: return AYSTAR_FOUND_END_NODE
;
154 case AYSTAR_EMPTY_OPENLIST
:
155 case AYSTAR_LIMIT_REACHED
: return AYSTAR_NO_PATH
;
156 default: return AYSTAR_STILL_BUSY
;
161 * Adds a node from where to start an algorithm. Multiple nodes can be added
162 * if wanted. You should make sure that #Clear() is called before adding nodes
163 * if the #AyStar has been used before (though the normal main loop calls
164 * #Clear() automatically when the algorithm finishes.
165 * @param start_node Node to start with.
166 * @param g the cost for starting with this node.
168 void AyStar::AddStartNode(AyStarNode
*start_node
, int g
)
171 Debug(misc
, 0, "[AyStar] Starting A* Algorithm from node ({}, {}, {})\n",
172 TileX(start_node
->tile
), TileY(start_node
->tile
), start_node
->direction
);
174 this->OpenListAdd(nullptr, start_node
, 0, g
);