OpenTTD Source  20241125-master-g5b02f51e17
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 
25 void 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 
38 void 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 AYSTAR_EMPTY_OPENLIST;
98 
99  /* Check for end node and if found, return that code */
100  if (this->EndNodeCheck(this, current) == AYSTAR_FOUND_END_NODE && current->parent != nullptr) {
101  if (this->FoundEndNode != nullptr) {
102  this->FoundEndNode(this, current);
103  }
104  return AYSTAR_FOUND_END_NODE;
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 */
121  return AYSTAR_LIMIT_REACHED;
122  } else {
123  /* Return that we are still busy */
124  return AYSTAR_STILL_BUSY;
125  }
126 }
127 
138 {
139  int r, i = 0;
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)) { }
143 #ifdef AYSTAR_DEBUG
144  switch (r) {
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;
148  default: break;
149  }
150 #endif
151 
152  switch (r) {
156  default: return AYSTAR_STILL_BUSY;
157  }
158 }
159 
168 void AyStar::AddStartNode(AyStarNode *start_node, int g)
169 {
170 #ifdef AYSTAR_DEBUG
171  Debug(misc, 0, "[AyStar] Starting A* Algorithm from node ({}, {}, {})\n",
172  TileX(start_node->tile), TileY(start_node->tile), start_node->direction);
173 #endif
174  this->OpenListAdd(nullptr, start_node, 0, g);
175 }
This file has the header for AyStar.
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
Definition: aystar.h:40
@ AYSTAR_LIMIT_REACHED
The AyStar::max_search_nodes limit has been reached, aborting search.
Definition: aystar.h:36
@ AYSTAR_FOUND_END_NODE
An end node was found.
Definition: aystar.h:32
@ AYSTAR_NO_PATH
No path to the goal was found.
Definition: aystar.h:35
@ AYSTAR_STILL_BUSY
Some checking was done, but no path found yet, and there are still items left to try.
Definition: aystar.h:34
@ AYSTAR_EMPTY_OPENLIST
All items are tested, and no path has been found.
Definition: aystar.h:33
Titem * PopBestOpenNode()
remove and return the best open node
Definition: nodelist.hpp:96
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
Titem * FindOpenNode(const Key &key)
return the open node specified by a key or nullptr if not found
Definition: nodelist.hpp:107
Titem & CreateNewNode()
allocate new data item from items
Definition: nodelist.hpp:59
void InsertOpenNode(Titem &item)
insert given item as open node (into m_open and m_open_queue)
Definition: nodelist.hpp:76
int ClosedCount()
return number of closed nodes
Definition: nodelist.hpp:47
Titem & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition: nodelist.hpp:113
#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 Loop()
This function is the core of AyStar.
Definition: aystar.cpp:92
int max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
Definition: aystar.h:125
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:168
int Main()
This is the function you call to run AyStar.
Definition: aystar.cpp:137
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