OpenTTD Source 20250205-master-gfd85ab1e2c
aystar.h
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#ifndef AYSTAR_H
17#define AYSTAR_H
18
19#include "yapf/nodelist.hpp"
20#include "yapf/yapf_node.hpp"
21
22static const int AYSTAR_DEF_MAX_SEARCH_NODES = 10000;
23
25enum class AyStarStatus : uint8_t {
28 StillBusy,
29 NoPath,
31 Done,
32};
33
34static const int AYSTAR_INVALID_NODE = -1;
35
37
38struct PathNode : CYapfNodeT<AyStarNode, PathNode> {
39};
40
41struct AyStar;
42
57typedef AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current);
58
65typedef int32_t AyStar_CalculateG(AyStar *aystar, AyStarNode *current, PathNode *parent);
66
72typedef int32_t AyStar_CalculateH(AyStar *aystar, AyStarNode *current, PathNode *parent);
73
79typedef void AyStar_GetNeighbours(AyStar *aystar, PathNode *current);
80
85typedef void AyStar_FoundEndNode(AyStar *aystar, PathNode *current);
86
95struct AyStar {
96/* These fields should be filled before initing the AyStar, but not changed
97 * afterwards (except for user_data)! (free and init again to change them) */
98
99 /* These should point to the application specific routines that do the
100 * actual work */
101 AyStar_CalculateG *CalculateG;
102 AyStar_CalculateH *CalculateH;
103 AyStar_GetNeighbours *GetNeighbours;
104 AyStar_EndNodeCheck *EndNodeCheck;
105 AyStar_FoundEndNode *FoundEndNode;
106
107 /* These are completely untouched by AyStar, they can be accessed by
108 * the application specific routines to input and output data.
109 * user_path should typically contain data about the resulting path
110 * afterwards, user_target should typically contain information about
111 * what you where looking for, and user_data can contain just about
112 * everything */
113 void *user_target;
114 void *user_data;
115
119
120 /* These should be filled with the neighbours of a tile by GetNeighbours */
121 std::vector<AyStarNode> neighbours;
122
123 /* These will contain the methods for manipulating the AyStar. Only
124 * Main() should be called externally */
125 void AddStartNode(AyStarNode *start_node, int g);
128 void CheckTile(AyStarNode *current, PathNode *parent);
129
130protected:
132
133 void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g);
134};
135
136#endif /* AYSTAR_H */
AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current)
Check whether the end-tile is found.
Definition aystar.h:57
int32_t AyStar_CalculateG(AyStar *aystar, AyStarNode *current, PathNode *parent)
Calculate the G-value for the AyStar algorithm.
Definition aystar.h:65
AyStarStatus
Return status of AyStar methods.
Definition aystar.h:25
@ 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.
@ Done
Not an end-tile, or wrong direction.
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
Definition aystar.h:34
int32_t AyStar_CalculateH(AyStar *aystar, AyStarNode *current, PathNode *parent)
Calculate the H-value for the AyStar algorithm.
Definition aystar.h:72
void AyStar_GetNeighbours(AyStar *aystar, PathNode *current)
This function requests the tiles around the current tile and put them in #neighbours.
Definition aystar.h:79
static const int AYSTAR_DEF_MAX_SEARCH_NODES
Reference limit for AyStar::max_search_nodes.
Definition aystar.h:22
void AyStar_FoundEndNode(AyStar *aystar, PathNode *current)
If the End Node is found, this function is called.
Definition aystar.h:85
Hash table based node list multi-container class.
Definition nodelist.hpp:21
List of nodes used for the A-star pathfinder.
AyStar search algorithm struct.
Definition aystar.h:95
int max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
Definition aystar.h:118
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:117
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:116
Yapf Node base.
Definition yapf_node.hpp:62
Node in the pathfinder's graph.