OpenTTD Source  20240917-master-g9ab0a47812
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->m_tile, node->m_td, true);
30  new_node->m_estimate = f;
31  new_node->m_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->m_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->m_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->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);
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->m_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 }
TileY
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:437
AyStar::loops_per_tick
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
AYSTAR_LIMIT_REACHED
@ AYSTAR_LIMIT_REACHED
The AyStar::max_search_nodes limit has been reached, aborting search.
Definition: aystar.h:36
AyStar::AddStartNode
void AddStartNode(AyStarNode *start_node, int g)
Adds a node from where to start an algorithm.
Definition: aystar.cpp:168
AyStar::max_search_nodes
int max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
Definition: aystar.h:126
CNodeList_HashTableT::PopOpenNode
Titem_ & PopOpenNode(const Key &key)
remove and return the open node specified by a key
Definition: nodelist.hpp:118
AyStar::CheckTile
void CheckTile(AyStarNode *current, PathNode *parent)
Checks one tile and calculate its f-value.
Definition: aystar.cpp:38
AYSTAR_EMPTY_OPENLIST
@ AYSTAR_EMPTY_OPENLIST
All items are tested, and no path has been found.
Definition: aystar.h:33
AyStar::Main
int Main()
This is the function you call to run AyStar.
Definition: aystar.cpp:137
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
CNodeList_HashTableT::FindClosedNode
Titem_ * FindClosedNode(const Key &key)
return the closed node specified by a key or nullptr if not found
Definition: nodelist.hpp:134
AYSTAR_NO_PATH
@ AYSTAR_NO_PATH
No path to the goal was found.
Definition: aystar.h:35
CNodeList_HashTableT::ClosedCount
int ClosedCount()
return number of closed nodes
Definition: nodelist.hpp:57
PathNode
Definition: aystar.h:44
AYSTAR_STILL_BUSY
@ AYSTAR_STILL_BUSY
Some checking was done, but no path found yet, and there are still items left to try.
Definition: aystar.h:34
CNodeList_HashTableT::InsertClosedNode
void InsertClosedNode(Titem_ &item)
close node
Definition: nodelist.hpp:127
CNodeList_HashTableT::FindOpenNode
Titem_ * FindOpenNode(const Key &key)
return the open node specified by a key or nullptr if not found
Definition: nodelist.hpp:111
AYSTAR_FOUND_END_NODE
@ AYSTAR_FOUND_END_NODE
An end node was found.
Definition: aystar.h:32
AyStar::Loop
int Loop()
This function is the core of AyStar.
Definition: aystar.cpp:92
CYapfNodeKeyTrackDir
Definition: yapf_node.hpp:44
CNodeList_HashTableT::PopBestOpenNode
Titem_ * PopBestOpenNode()
remove and return the best open node
Definition: nodelist.hpp:100
AyStar::OpenListAdd
void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g)
Adds a node to the open list.
Definition: aystar.cpp:25
AYSTAR_INVALID_NODE
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
Definition: aystar.h:40
CNodeList_HashTableT::InsertOpenNode
void InsertOpenNode(Titem_ &item)
insert given item as open node (into m_open and m_open_queue)
Definition: nodelist.hpp:80
AyStar::max_path_cost
int max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
Definition: aystar.h:125
TileX
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:427
aystar.h
CNodeList_HashTableT::CreateNewNode
Titem_ * CreateNewNode()
allocate new data item from m_arr
Definition: nodelist.hpp:63