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