OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

aystar.h
Go to the documentation of this file.
1 /* $Id: aystar.h 27733 2017-01-15 13:59:46Z frosch $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
18 #ifndef AYSTAR_H
19 #define AYSTAR_H
20
21 #include "queue.h"
22 #include "../../tile_type.h"
23 #include "../../track_type.h"
24
25 //#define AYSTAR_DEBUG
26
28 enum AystarStatus {
29 AYSTAR_FOUND_END_NODE,
30 AYSTAR_EMPTY_OPENLIST,
31 AYSTAR_STILL_BUSY,
32 AYSTAR_NO_PATH,
33 AYSTAR_LIMIT_REACHED,
34 AYSTAR_DONE,
35 };
36
37 static const int AYSTAR_INVALID_NODE = -1;
38
40 struct AyStarNode {
41 TileIndex tile;
42 Trackdir direction;
43 uint user_data[2];
44 };
45
47 struct PathNode {
48 AyStarNode node;
49 PathNode *parent;
50 };
51
57 struct OpenListNode {
58 int g;
59 PathNode path;
60 };
61
62 struct AyStar;
63
78 typedef int32 AyStar_EndNodeCheck(AyStar *aystar, OpenListNode *current);
79
86 typedef int32 AyStar_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
87
93 typedef int32 AyStar_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
94
100 typedef void AyStar_GetNeighbours(AyStar *aystar, OpenListNode *current);
101
106 typedef void AyStar_FoundEndNode(AyStar *aystar, OpenListNode *current);
107
116 struct AyStar {
117 /* These fields should be filled before initing the AyStar, but not changed
118 * afterwards (except for user_data and user_path)! (free and init again to change them) */
119
120 /* These should point to the application specific routines that do the
121 * actual work */
122 AyStar_CalculateG *CalculateG;
123 AyStar_CalculateH *CalculateH;
124 AyStar_GetNeighbours *GetNeighbours;
125 AyStar_EndNodeCheck *EndNodeCheck;
126 AyStar_FoundEndNode *FoundEndNode;
127
128 /* These are completely untouched by AyStar, they can be accessed by
129 * the application specific routines to input and output data.
130 * user_path should typically contain data about the resulting path
131 * afterwards, user_target should typically contain information about
132 * what you where looking for, and user_data can contain just about
133 * everything */
134 void *user_path;
135 void *user_target;
136 void *user_data;
137
138 byte loops_per_tick;
139 uint max_path_cost;
140 uint max_search_nodes;
141
142 /* These should be filled with the neighbours of a tile by
143 * GetNeighbours */
144 AyStarNode neighbours[12];
145 byte num_neighbours;
146
147 void Init(Hash_HashProc hash, uint num_buckets);
148
149 /* These will contain the methods for manipulating the AyStar. Only
150 * Main() should be called externally */
151 void AddStartNode(AyStarNode *start_node, uint g);
152 int Main();
153 int Loop();
154 void Free();
155 void Clear();
156 void CheckTile(AyStarNode *current, OpenListNode *parent);
157
158 protected:
159 Hash closedlist_hash;
160 BinaryHeap openlist_queue;
161 Hash openlist_hash;
162
163 void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g);
164 OpenListNode *OpenListIsInList(const AyStarNode *node);
165 OpenListNode *OpenListPop();
166
167 void ClosedListAdd(const PathNode *node);
168 PathNode *ClosedListIsInList(const AyStarNode *node);
169 };
170
171 #endif /* AYSTAR_H */