Codechange: Use cached town, station, industry names for list window sorting
[openttd-github.git] / src / pathfinder / pathfinder_func.h
blob03edf6995b6aa1d188722e74e78681af580d6c70
1 /*
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/>.
6 */
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"
15 /**
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);
27 TileArea ta;
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 */
46 return TileXY(x, y);
49 #endif /* PATHFINDER_FUNC_H */