OpenTTD Source 20241224-master-gee860a5c8e
aystar.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
16#include "../../stdafx.h"
17#include "aystar.h"
18
19#include "../../safeguards.h"
20
25void AyStar::OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g)
26{
27 /* Add a new Node to the OpenList */
28 PathNode &new_node = this->nodes.CreateNewNode();
29 new_node.Set(parent, node->tile, node->td, true);
30 new_node.estimate = f;
31 new_node.cost = g;
32 this->nodes.InsertOpenNode(new_node);
33}
34
38void AyStar::CheckTile(AyStarNode *current, PathNode *parent)
39{
40 /* Check the new node against the ClosedList */
41 if (this->nodes.FindClosedNode(*current) != nullptr) return;
42
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;
47
48 /* There should not be given any other error-code.. */
49 assert(new_g >= 0);
50 /* Add the parent g-value to the new g-value */
51 new_g += parent->cost;
52 if (this->max_path_cost != 0 && new_g > this->max_path_cost) return;
53
54 /* Calculate the h-value */
55 int new_h = this->CalculateH(this, current, parent);
56 /* There should not be given any error-code.. */
57 assert(new_h >= 0);
58
59 /* The f-value if g + h */
60 int new_f = new_g + new_h;
61
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->key);
64
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->cost) return;
70 this->nodes.PopOpenNode(check->key);
71 /* It is lower, so change it to this item */
72 check->estimate = new_f;
73 check->cost = new_g;
74 check->parent = closedlist_parent;
75 /* Re-add it in the openlist_queue. */
76 this->nodes.InsertOpenNode(*check);
77 } else {
78 /* A new node, add it to the OpenList */
79 this->OpenListAdd(closedlist_parent, current, new_f, new_g);
80 }
81}
82
93{
94 /* Get the best node from OpenList */
95 PathNode *current = this->nodes.PopBestOpenNode();
96 /* If empty, drop an error */
97 if (current == nullptr) return AyStarStatus::EmptyOpenList;
98
99 /* Check for end node and if found, return that code */
100 if (this->EndNodeCheck(this, current) == AyStarStatus::FoundEndNode && current->parent != nullptr) {
101 if (this->FoundEndNode != nullptr) {
102 this->FoundEndNode(this, current);
103 }
105 }
106
107 /* Add the node to the ClosedList */
108 this->nodes.InsertClosedNode(*current);
109
110 /* Load the neighbours */
111 this->GetNeighbours(this, current);
112
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);
117 }
118
119 if (this->max_search_nodes != 0 && this->nodes.ClosedCount() >= this->max_search_nodes) {
120 /* We've expanded enough nodes */
122 } else {
123 /* Return that we are still busy */
125 }
126}
127
138{
140 int i = 0;
141 /* Loop through the OpenList
142 * Quit if result is no AyStarStatus::StillBusy or is more than loops_per_tick */
143 while ((r = this->Loop()) == AyStarStatus::StillBusy && (this->loops_per_tick == 0 || ++i < this->loops_per_tick)) { }
144#ifdef AYSTAR_DEBUG
145 switch (r) {
146 case AyStarStatus::FoundEndNode: Debug(misc, 0, "[AyStar] Found path!"); break;
147 case AyStarStatus::EmptyOpenList: Debug(misc, 0, "[AyStar] OpenList run dry, no path found"); break;
148 case AyStarStatus::LimitReached: Debug(misc, 0, "[AyStar] Exceeded search_nodes, no path found"); break;
149 default: break;
150 }
151#endif
152
153 switch (r) {
157 default: return AyStarStatus::StillBusy;
158 }
159}
160
169void AyStar::AddStartNode(AyStarNode *start_node, int g)
170{
171#ifdef AYSTAR_DEBUG
172 Debug(misc, 0, "[AyStar] Starting A* Algorithm from node ({}, {}, {})\n",
173 TileX(start_node->tile), TileY(start_node->tile), start_node->direction);
174#endif
175 this->OpenListAdd(nullptr, start_node, 0, g);
176}
This file has the header for AyStar.
AyStarStatus
Return status of AyStar methods.
Definition aystar.h:31
@ EmptyOpenList
All items are tested, and no path has been found.
@ StillBusy
Some checking was done, but no path found yet, and there are still items left to try.
@ LimitReached
The AyStar::max_search_nodes limit has been reached, aborting search.
@ NoPath
No path to the goal was found.
@ FoundEndNode
An end node was found.
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
Definition aystar.h:40
void InsertClosedNode(Titem &item)
close node
Definition nodelist.hpp:122
Titem * FindClosedNode(const Key &key)
return the closed node specified by a key or nullptr if not found
Definition nodelist.hpp:129
void InsertOpenNode(Titem &item)
insert given item as open node (into open_nodes and open_queue)
Definition nodelist.hpp:76
Titem * PopBestOpenNode()
remove and return the best open node
Definition nodelist.hpp:96
int ClosedCount()
return number of closed nodes
Definition nodelist.hpp:47
Titem & CreateNewNode()
allocate new data item from items
Definition nodelist.hpp:59
Titem & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition nodelist.hpp:113
Titem * FindOpenNode(const Key &key)
return the open node specified by a key or nullptr if not found
Definition nodelist.hpp:107
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:415
int max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
Definition aystar.h:126
void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g)
Adds a node to the open list.
Definition aystar.cpp:25
int max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
Definition aystar.h:125
AyStarStatus Loop()
This function is the core of AyStar.
Definition aystar.cpp:92
AyStarStatus Main()
This is the function you call to run AyStar.
Definition aystar.cpp:137
void CheckTile(AyStarNode *current, PathNode *parent)
Checks one tile and calculate its f-value.
Definition aystar.cpp:38
void AddStartNode(AyStarNode *start_node, int g)
Adds a node from where to start an algorithm.
Definition aystar.cpp:169
uint8_t loops_per_tick
How many loops are there called before Main() gives control back to the caller. 0 = until done.
Definition aystar.h:124