aystar.h

Go to the documentation of this file.
00001 /* $Id: aystar.h 20890 2010-10-02 19:55:13Z alberth $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00018 #ifndef AYSTAR_H
00019 #define AYSTAR_H
00020 
00021 #include "queue.h"
00022 #include "../../tile_type.h"
00023 #include "../../track_type.h"
00024 
00025 //#define AYSTAR_DEBUG
00026 
00028 enum AystarStatus {
00029   AYSTAR_FOUND_END_NODE, 
00030   AYSTAR_EMPTY_OPENLIST, 
00031   AYSTAR_STILL_BUSY,     
00032   AYSTAR_NO_PATH,        
00033   AYSTAR_LIMIT_REACHED,  
00034   AYSTAR_DONE,           
00035 };
00036 
00037 static const int AYSTAR_INVALID_NODE = -1; 
00038 
00040 struct AyStarNode {
00041   TileIndex tile;
00042   Trackdir direction;
00043   uint user_data[2];
00044 };
00045 
00047 struct PathNode {
00048   AyStarNode node;
00049   PathNode *parent; 
00050 };
00051 
00057 struct OpenListNode {
00058   int g;
00059   PathNode path;
00060 };
00061 
00062 struct AyStar;
00063 
00078 typedef int32 AyStar_EndNodeCheck(AyStar *aystar, OpenListNode *current);
00079 
00086 typedef int32 AyStar_CalculateG(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
00087 
00093 typedef int32 AyStar_CalculateH(AyStar *aystar, AyStarNode *current, OpenListNode *parent);
00094 
00100 typedef void AyStar_GetNeighbours(AyStar *aystar, OpenListNode *current);
00101 
00106 typedef void AyStar_FoundEndNode(AyStar *aystar, OpenListNode *current);
00107 
00116 struct AyStar {
00117 /* These fields should be filled before initing the AyStar, but not changed
00118  * afterwards (except for user_data and user_path)! (free and init again to change them) */
00119 
00120   /* These should point to the application specific routines that do the
00121    * actual work */
00122   AyStar_CalculateG *CalculateG;
00123   AyStar_CalculateH *CalculateH;
00124   AyStar_GetNeighbours *GetNeighbours;
00125   AyStar_EndNodeCheck *EndNodeCheck;
00126   AyStar_FoundEndNode *FoundEndNode;
00127 
00128   /* These are completely untouched by AyStar, they can be accessed by
00129    * the application specific routines to input and output data.
00130    * user_path should typically contain data about the resulting path
00131    * afterwards, user_target should typically contain information about
00132    * what you where looking for, and user_data can contain just about
00133    * everything */
00134   void *user_path;
00135   void *user_target;
00136   uint user_data[10];
00137 
00138   byte loops_per_tick;   
00139   uint max_path_cost;    
00140   uint max_search_nodes; 
00141 
00142   /* These should be filled with the neighbours of a tile by
00143    * GetNeighbours */
00144   AyStarNode neighbours[12];
00145   byte num_neighbours;
00146 
00147   void Init(Hash_HashProc hash, uint num_buckets);
00148 
00149   /* These will contain the methods for manipulating the AyStar. Only
00150    * Main() should be called externally */
00151   void AddStartNode(AyStarNode *start_node, uint g);
00152   int Main();
00153   int Loop();
00154   void Free();
00155   void Clear();
00156   void CheckTile(AyStarNode *current, OpenListNode *parent);
00157 
00158 protected:
00159   Hash       closedlist_hash; 
00160   BinaryHeap openlist_queue;  
00161   Hash       openlist_hash;   
00162 
00163   void OpenListAdd(PathNode *parent, const AyStarNode *node, int f, int g);
00164   OpenListNode *OpenListIsInList(const AyStarNode *node);
00165   OpenListNode *OpenListPop();
00166 
00167   void ClosedListAdd(const PathNode *node);
00168   PathNode *ClosedListIsInList(const AyStarNode *node);
00169 };
00170 
00171 #endif /* AYSTAR_H */

Generated on Fri Dec 31 17:15:36 2010 for OpenTTD by  doxygen 1.6.1