diff options
author | rubidium <rubidium@openttd.org> | 2009-02-19 00:15:36 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2009-02-19 00:15:36 +0000 |
commit | c2d9c85c0fbb26b5db2ef20b7ac610e63de2b46b (patch) | |
tree | 76c1b93af5e93efa3bcc9984e1c04884640ea566 /src/pathfind.h | |
parent | 336fda62bec39eb9ad1ee966a23faa0886a75309 (diff) | |
download | openttd-c2d9c85c0fbb26b5db2ef20b7ac610e63de2b46b.tar.xz |
(svn r15518) -Fix [FS#2631]: use distance to closest station tile as estimate for YAPF too (NPF already does so). This makes it behave 'better' with wide stations.
Diffstat (limited to 'src/pathfind.h')
-rw-r--r-- | src/pathfind.h | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/pathfind.h b/src/pathfind.h index d09b2a053..0a006190f 100644 --- a/src/pathfind.h +++ b/src/pathfind.h @@ -77,4 +77,36 @@ DECLARE_ENUM_AS_BIT_SET(PathfindFlags) void FollowTrack(TileIndex tile, PathfindFlags flags, TransportType tt, uint sub_type, DiagDirection direction, TPFEnumProc *enum_proc, TPFAfterProc *after_proc, void *data); void NewTrainPathfind(TileIndex tile, TileIndex dest, RailTypes railtypes, DiagDirection direction, NTPEnumProc *enum_proc, void *data); + +/** + * Calculates the tile of given station that is closest to a given tile + * for this we assume the station is a rectangle, + * as defined by its top tile (st->train_tile) and its width/height (st->trainst_w, st->trainst_h) + * @param station The station to calculate the distance to + * @param tile The tile from where to calculate the distance + * @return The closest station tile to the given tile. + */ +static inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile) +{ + const Station *st = GetStation(station); + + /* If the rail station is (temporarily) not present, use the station sign to drive near the station */ + if (st->train_tile == INVALID_TILE) return st->xy; + + uint minx = TileX(st->train_tile); // topmost corner of station + uint miny = TileY(st->train_tile); + uint maxx = minx + st->trainst_w - 1; // lowermost corner of station + uint maxy = miny + st->trainst_h - 1; + + /* we are going the aim for the x coordinate of the closest corner + * but if we are between those coordinates, we will aim for our own x coordinate */ + uint x = ClampU(TileX(tile), minx, maxx); + + /* same for y coordinate, see above comment */ + uint y = ClampU(TileY(tile), miny, maxy); + + /* return the tile of our target coordinates */ + return TileXY(x, y); +} + #endif /* PATHFIND_H */ |