pathfind.h

Go to the documentation of this file.
00001 /* $Id: pathfind.h 15518 2009-02-19 00:15:36Z rubidium $ */
00002 
00005 #ifndef PATHFIND_H
00006 #define PATHFIND_H
00007 
00008 #include "direction_type.h"
00009 
00010 enum {
00011   STR_FACTOR  = 2,
00012   DIAG_FACTOR = 3
00013 };
00014 
00015 //#define PF_BENCH // perform simple benchmarks on the train pathfinder (not
00016 //supported on all archs)
00017 
00018 struct TrackPathFinder;
00019 typedef bool TPFEnumProc(TileIndex tile, void *data, Trackdir trackdir, uint length);
00020 typedef void TPFAfterProc(TrackPathFinder *tpf);
00021 
00022 typedef bool NTPEnumProc(TileIndex tile, void *data, int track, uint length);
00023 
00024 #define PATHFIND_GET_LINK_OFFS(tpf, link) ((byte*)(link) - (byte*)tpf->links)
00025 #define PATHFIND_GET_LINK_PTR(tpf, link_offs) (TrackPathFinderLink*)((byte*)tpf->links + (link_offs))
00026 
00027 /* y7 y6 y5 y4 y3 y2 y1 y0 x7 x6 x5 x4 x3 x2 x1 x0
00028  * y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0  0  0
00029  *  0  0 y7 y6 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0
00030  *  0  0  0  0 y5 y4 y3 y2 y1 y0 x4 x3 x2 x1 x0  0
00031  */
00032 #define PATHFIND_HASH_TILE(tile) (TileX(tile) & 0x1F) + ((TileY(tile) & 0x1F) << 5)
00033 
00034 struct TrackPathFinderLink {
00035   TileIndex tile;
00036   uint16 flags;
00037   uint16 next;
00038 };
00039 
00040 struct RememberData {
00041   uint16 cur_length;
00042   byte depth;
00043   Track last_choosen_track;
00044 };
00045 
00046 struct TrackPathFinder {
00047   int num_links_left;
00048   TrackPathFinderLink *new_link;
00049 
00050   TPFEnumProc *enum_proc;
00051 
00052   void *userdata;
00053 
00054   RememberData rd;
00055 
00056   TrackdirByte the_dir;
00057 
00058   TransportType tracktype;
00059   uint sub_type;
00060 
00061   bool disable_tile_hash;
00062 
00063   uint16 hash_head[0x400];
00064   TileIndex hash_tile[0x400];       
00065 
00066   TrackPathFinderLink links[0x400]; 
00067 };
00068 
00070 enum PathfindFlags {
00071   PATHFIND_FLAGS_NONE              = 0,
00072   PATHFIND_FLAGS_SHIP_MODE         = 0x0800, 
00073   PATHFIND_FLAGS_DISABLE_TILE_HASH = 0x1000, 
00074 };
00075 DECLARE_ENUM_AS_BIT_SET(PathfindFlags)
00076 
00077 void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data);
00078 void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc *enum_proc, void *data);
00079 
00080 
00089 static inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile)
00090 {
00091   const Station *st = GetStation(station);
00092 
00093   /* If the rail station is (temporarily) not present, use the station sign to drive near the station */
00094   if (st->train_tile == INVALID_TILE) return st->xy;
00095 
00096   uint minx = TileX(st->train_tile);  // topmost corner of station
00097   uint miny = TileY(st->train_tile);
00098   uint maxx = minx + st->trainst_w - 1; // lowermost corner of station
00099   uint maxy = miny + st->trainst_h - 1;
00100 
00101   /* we are going the aim for the x coordinate of the closest corner
00102    * but if we are between those coordinates, we will aim for our own x coordinate */
00103   uint x = ClampU(TileX(tile), minx, maxx);
00104 
00105   /* same for y coordinate, see above comment */
00106   uint y = ClampU(TileY(tile), miny, maxy);
00107 
00108   /* return the tile of our target coordinates */
00109   return TileXY(x, y);
00110 }
00111 
00112 #endif /* PATHFIND_H */

Generated on Mon Jun 8 23:04:06 2009 for OpenTTD by  doxygen 1.5.6