OpenTTD
pathfinder_func.h
Go to the documentation of this file.
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  */
7 
10 #ifndef PATHFINDER_FUNC_H
11 #define PATHFINDER_FUNC_H
12 
13 #include "../waypoint_base.h"
14 
24 static inline TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type)
25 {
26  const BaseStation *st = BaseStation::Get(station);
27  TileArea ta;
28  st->GetTileArea(&ta, station_type);
29 
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;
32 
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;
37 
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);
41 
42  /* same for y coordinate, see above comment */
43  uint y = ClampU(TileY(tile), miny, maxy);
44 
45  /* return the tile of our target coordinates */
46  return TileXY(x, y);
47 }
48 
49 #endif /* PATHFINDER_FUNC_H */
static Titem * Get(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:291
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
virtual void GetTileArea(TileArea *ta, StationType type) const =0
Get the tile area for a given station type.
uint16 w
The width of the area.
Definition: tilearea_type.h:18
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:182
Represents the covered area of e.g.
Definition: tilearea_type.h:16
static TileIndex CalcClosestStationTile(StationID station, TileIndex tile, StationType station_type)
Calculates the tile of given station that is closest to a given tile for this we assume the station i...
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:17
StationType
Station types.
Definition: station_type.h:32
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
TileIndex xy
Base tile of the station.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:83
uint16 h
The height of the area.
Definition: tilearea_type.h:19
Base class for all station-ish types.
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163