1 /* $Id: pathfinder_func.h 18809 2010-01-15 16:41:15Z rubidium $ */
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/>.
10 /** @file pathfinder_func.h General functions related to pathfinders. */
12 #ifndef PATHFINDER_FUNC_H
13 #define PATHFINDER_FUNC_H
15 #include "../waypoint_base.h"
18 * Calculates the tile of given station that is closest to a given tile
19 * for this we assume the station is a rectangle,
20 * as defined by its tile are (st->train_station)
21 * @param station The station to calculate the distance to
22 * @param tile The tile from where to calculate the distance
23 * @param station_type the station type to get the closest tile of
24 * @return The closest station tile to the given tile.
26 static inline TileIndex
CalcClosestStationTile(StationID station
, TileIndex tile
, StationType station_type
)
28 const BaseStation
*st
= BaseStation::Get(station
);
30 st
->GetTileArea(&ta
, station_type
);
32 /* If the rail station is (temporarily) not present, use the station sign to drive near the station */
33 if (ta
.tile
== INVALID_TILE
) return st
->xy
;
35 uint minx
= TileX(ta
.tile
); // topmost corner of station
36 uint miny
= TileY(ta
.tile
);
37 uint maxx
= minx
+ ta
.w
- 1; // lowermost corner of station
38 uint maxy
= miny
+ ta
.h
- 1;
40 /* we are going the aim for the x coordinate of the closest corner
41 * but if we are between those coordinates, we will aim for our own x coordinate */
42 uint x
= ClampU(TileX(tile
), minx
, maxx
);
44 /* same for y coordinate, see above comment */
45 uint y
= ClampU(TileY(tile
), miny
, maxy
);
47 /* return the tile of our target coordinates */
51 #endif /* PATHFINDER_FUNC_H */