Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / pathfinder / pathfinder_func.h
blob444b100ce7b0a50adc68d266b58e8faac7bfd54d
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 "../tile_cmd.h"
14 #include "../waypoint_base.h"
16 /**
17 * Calculates the tile of given station that is closest to a given tile
18 * for this we assume the station is a rectangle,
19 * as defined by its tile are (st->train_station)
20 * @param station The station to calculate the distance to
21 * @param tile The tile from where to calculate the distance
22 * @param station_type the station type to get the closest tile of
23 * @return The closest station tile to the given tile.
25 inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type)
27 const BaseStation *st = BaseStation::Get(station);
28 TileArea ta;
29 st->GetTileArea(&ta, station_type);
31 /* If the rail station is (temporarily) not present, use the station sign to drive near the station */
32 if (ta.tile == INVALID_TILE) return st->xy;
34 uint minx = TileX(ta.tile); // topmost corner of station
35 uint miny = TileY(ta.tile);
36 uint maxx = minx + ta.w - 1; // lowermost corner of station
37 uint maxy = miny + ta.h - 1;
39 /* we are going the aim for the x coordinate of the closest corner
40 * but if we are between those coordinates, we will aim for our own x coordinate */
41 uint x = ClampU(TileX(tile), minx, maxx);
43 /* same for y coordinate, see above comment */
44 uint y = ClampU(TileY(tile), miny, maxy);
46 /* return the tile of our target coordinates */
47 return TileXY(x, y);
50 /**
51 * Wrapper around GetTileTrackStatus() and TrackStatusToTrackdirBits(), as for
52 * single tram bits GetTileTrackStatus() returns 0. The reason for this is
53 * that there are no half-tile TrackBits in OpenTTD.
54 * This tile, however, is a valid tile for trams, one on which they can
55 * reverse safely. To "fix" this, pretend that if we are on a half-tile, we
56 * are in fact on a straight tram track tile. CFollowTrackT will make sure
57 * the pathfinders cannot exit on the wrong side and allows reversing on such
58 * tiles.
60 inline TrackdirBits GetTrackdirBitsForRoad(TileIndex tile, RoadTramType rtt)
62 TrackdirBits bits = TrackStatusToTrackdirBits(GetTileTrackStatus(tile, TRANSPORT_ROAD, rtt));
64 if (rtt == RTT_TRAM && bits == TRACKDIR_BIT_NONE) {
65 if (IsNormalRoadTile(tile)) {
66 RoadBits rb = GetRoadBits(tile, RTT_TRAM);
67 switch (rb) {
68 case ROAD_NE:
69 case ROAD_SW:
70 bits = TRACKDIR_BIT_X_NE | TRACKDIR_BIT_X_SW;
71 break;
73 case ROAD_NW:
74 case ROAD_SE:
75 bits = TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE;
76 break;
78 default: break;
83 return bits;
86 #endif /* PATHFINDER_FUNC_H */