OpenTTD Source: src/pathfinder/aystar.h Source File

OpenTTD Source 15.0-beta1

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 "../track_func.h"
20
21#include "../misc/hashtable.hpp"
22#include "../misc/binaryheap.hpp"
23#include "../misc/dbg_helpers.h"
24
25#include "yapf/nodelist.hpp"
26#include "yapf/yapf_node.hpp"
27
28 static const int AYSTAR_DEF_MAX_SEARCH_NODES = 10000;
29
31 enum class AyStarStatus : uint8_t {
32 FoundEndNode,
33 EmptyOpenList,
34 StillBusy,
35 NoPath,
36 LimitReached,
37 Done,
38};
39
40 static const int AYSTAR_INVALID_NODE = -1;
41
42using AyStarNode = CYapfNodeKeyTrackDir;
43
44 struct PathNode : CYapfNodeT<AyStarNode, PathNode> {
45};
46
47bool CheckIgnoreFirstTile(const PathNode *node);
48
49struct AyStar;
50
65 typedef AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current);
66
73 typedef int32_t AyStar_CalculateG(AyStar *aystar, AyStarNode *current, PathNode *parent);
74
80 typedef int32_t AyStar_CalculateH(AyStar *aystar, AyStarNode *current, PathNode *parent);
81
87 typedef void AyStar_GetNeighbours(AyStar *aystar, PathNode *current);
88
93 typedef void AyStar_FoundEndNode(AyStar *aystar, PathNode *current);
94
103 struct AyStar {
104/* These fields should be filled before initing the AyStar, but not changed
105 * afterwards (except for user_data)! (free and init again to change them) */
106
107 /* These should point to the application specific routines that do the
108 * actual work */
109 AyStar_CalculateG *CalculateG;
110 AyStar_CalculateH *CalculateH;
111 AyStar_GetNeighbours *GetNeighbours;
112 AyStar_EndNodeCheck *EndNodeCheck;
113 AyStar_FoundEndNode *FoundEndNode;
114
115 /* These are completely untouched by AyStar, they can be accessed by
116 * the application specific routines to input and output data.
117 * user_path should typically contain data about the resulting path
118 * afterwards, user_target should typically contain information about
119 * what you where looking for, and user_data can contain just about
120 * everything */
121 void *user_target;
122 void *user_data;
123
124 uint8_t loops_per_tick;
125 int max_path_cost;
126 int max_search_nodes = AYSTAR_DEF_MAX_SEARCH_NODES;
127
128 /* These should be filled with the neighbours of a tile by GetNeighbours */
129 std::vector<AyStarNode> neighbours;
130
131 /* These will contain the methods for manipulating the AyStar. Only
132 * Main() should be called externally */
133 void AddStartNode(AyStarNode *start_node, int g);
134 AyStarStatus Main();
135 AyStarStatus Loop();
136 void CheckTile(AyStarNode *current, PathNode *parent);
137
138protected:
139 NodeList<PathNode, 8, 10> nodes;
140
141 void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g);
142};
143
144#endif /* AYSTAR_H */
AyStar_EndNodeCheck
AyStarStatus AyStar_EndNodeCheck(const AyStar *aystar, const PathNode *current)
Check whether the end-tile is found.
Definition aystar.h:65
AyStar_CalculateG
int32_t AyStar_CalculateG(AyStar *aystar, AyStarNode *current, PathNode *parent)
Calculate the G-value for the AyStar algorithm.
Definition aystar.h:73
AyStarStatus
AyStarStatus
Return status of AyStar methods.
Definition aystar.h:31
AyStarStatus::EmptyOpenList
@ EmptyOpenList
All items are tested, and no path has been found.
AyStarStatus::StillBusy
@ StillBusy
Some checking was done, but no path found yet, and there are still items left to try.
AyStarStatus::LimitReached
@ LimitReached
The AyStar::max_search_nodes limit has been reached, aborting search.
AyStarStatus::NoPath
@ NoPath
No path to the goal was found.
AyStarStatus::FoundEndNode
@ FoundEndNode
An end node was found.
AyStarStatus::Done
@ Done
Not an end-tile, or wrong direction.
AYSTAR_INVALID_NODE
static const int AYSTAR_INVALID_NODE
Item is not valid (for example, not walkable).
Definition aystar.h:40
AyStar_CalculateH
int32_t AyStar_CalculateH(AyStar *aystar, AyStarNode *current, PathNode *parent)
Calculate the H-value for the AyStar algorithm.
Definition aystar.h:80
AyStar_GetNeighbours
void AyStar_GetNeighbours(AyStar *aystar, PathNode *current)
This function requests the tiles around the current tile and put them in #neighbours.
Definition aystar.h:87
AYSTAR_DEF_MAX_SEARCH_NODES
static const int AYSTAR_DEF_MAX_SEARCH_NODES
Reference limit for AyStar::max_search_nodes.
Definition aystar.h:28
AyStar_FoundEndNode
void AyStar_FoundEndNode(AyStar *aystar, PathNode *current)
If the End Node is found, this function is called.
Definition aystar.h:93
NodeList
Hash table based node list multi-container class.
Definition nodelist.hpp:21
nodelist.hpp
List of nodes used for the A-star pathfinder.
AyStar
AyStar search algorithm struct.
Definition aystar.h:103
AyStar::max_search_nodes
int max_search_nodes
The maximum number of nodes that will be expanded, 0 = infinite.
Definition aystar.h:126
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::max_path_cost
int max_path_cost
If the g-value goes over this number, it stops searching, 0 = infinite.
Definition aystar.h:125
AyStar::Loop
AyStarStatus Loop()
This function is the core of AyStar.
Definition aystar.cpp:92
AyStar::Main
AyStarStatus Main()
This is the function you call to run AyStar.
Definition aystar.cpp:137
AyStar::CheckTile
void CheckTile(AyStarNode *current, PathNode *parent)
Checks one tile and calculate its f-value.
Definition aystar.cpp:38
AyStar::AddStartNode
void AddStartNode(AyStarNode *start_node, int g)
Adds a node from where to start an algorithm.
Definition aystar.cpp:169
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
CYapfNodeKeyTrackDir
Definition yapf_node.hpp:48
CYapfNodeT
Yapf Node base.
Definition yapf_node.hpp:62
PathNode
Definition aystar.h:44
yapf_node.hpp
Node in the pathfinder's graph.