# AIStation Class Reference

Class that handles all station related functions. More...

`#include <ai_station.hpp>`

Inheritance diagram for AIStation:

## Public Types

enum | ErrorMessages { ERR_STATION_BASE = AIError::ERR_CAT_STATION << AIError::ERR_CAT_BIT_SIZE, ERR_STATION_TOO_LARGE, ERR_STATION_TOO_CLOSE_TO_ANOTHER_STATION, ERR_STATION_TOO_MANY_STATIONS, ERR_STATION_TOO_MANY_STATIONS_IN_TOWN }
| All station related error messages. More...
enum | StationType { STATION_TRAIN = 0x01, STATION_TRUCK_STOP = 0x02, STATION_BUS_STOP = 0x04, STATION_AIRPORT = 0x08, STATION_DOCK = 0x10, STATION_ANY = 0x1F }
| Type of stations known in the game. More...
enum | SpecialStationIDs { STATION_NEW = 0xFFFD, STATION_JOIN_ADJACENT = 0xFFFE, STATION_INVALID = 0xFFFF }
| Special station IDs for building adjacent/new stations when the adjacent/distant join features are enabled. More...

## Static Public Member Functions

static const char * | GetClassName ()
static bool | IsValidStation (StationID station_id)
| Checks whether the given station is valid and owned by you.
static StationID | GetStationID (TileIndex tile)
| Get the StationID of a tile, if there is a station.
static char * | GetName (StationID station_id)
| Get the name of a station.
static bool | SetName (StationID station_id, const char *name)
| Set the name this station.
static TileIndex | GetLocation (StationID station_id)
| Get the current location of a station.
static int32 | GetCargoWaiting (StationID station_id, CargoID cargo_id)
| See how much cargo there is waiting on a station.
static int32 | GetCargoRating (StationID station_id, CargoID cargo_id)
| See how high the rating is of a cargo on a station.
static int32 | GetCoverageRadius (AIStation::StationType station_type)
| Get the coverage radius of this type of station.
static int32 | GetDistanceManhattanToTile (StationID station_id, TileIndex tile)
| Get the manhattan distance from the tile to the AIStation::GetLocation() of the station.
static int32 | GetDistanceSquareToTile (StationID station_id, TileIndex tile)
| Get the square distance from the tile to the AIStation::GetLocation() of the station.
static bool | IsWithinTownInfluence (StationID station_id, TownID town_id)
| Find out if this station is within the rating influence of a town.
static bool | HasStationType (StationID station_id, StationType station_type)
| Check if any part of the station contains a station of the type StationType.
static bool | HasRoadType (StationID station_id, AIRoad::RoadType road_type)
| Check if any part of the station contains a station of the type RoadType.
static TownID | GetNearestTown (StationID station_id)
| Get the town that was nearest to the given station when the station was built.

## Detailed Description

Class that handles all station related functions.

Definition at line 15 of file ai_station.hpp.

## Member Enumeration Documentation

enum AIStation::ErrorMessages

All station related error messages.

Enumerator:

  ERR_STATION_BASE | Base for station related errors.
  ERR_STATION_TOO_LARGE | The station size exceeds the station spread.
  ERR_STATION_TOO_CLOSE_TO_ANOTHER_STATION | The station is build too close to another station, airport or dock.
  ERR_STATION_TOO_MANY_STATIONS | There are too many stations, airports and docks in the game.
  ERR_STATION_TOO_MANY_STATIONS_IN_TOWN | There are too many stations, airports of docks in a town.

Definition at line 22 of file ai_station.hpp.

enum AIStation::StationType

Type of stations known in the game.

Enumerator:

  STATION_TRAIN | Train station.
  STATION_TRUCK_STOP | Truck station.
  STATION_BUS_STOP | Bus station.
  STATION_AIRPORT | Airport.
  STATION_DOCK | Dock.
  STATION_ANY | All station types.

Definition at line 42 of file ai_station.hpp.

enum AIStation::SpecialStationIDs

Special station IDs for building adjacent/new stations when the adjacent/distant join features are enabled.

Enumerator:

  STATION_NEW | Build a new station.
  STATION_JOIN_ADJACENT | Join an neighbouring station if one exists.
  STATION_INVALID | Invalid station id.

Definition at line 56 of file ai_station.hpp.

## Member Function Documentation

bool AIStation::IsValidStation | ( | StationID | station_id | ) | `[static]`

Checks whether the given station is valid and owned by you.

Parameters:

  | station_id | The station to check.

Returns:
  True if and only if the station is valid.

Definition at line 18 of file ai_station.cpp.

References Station::owner, and OWNER_NONE.

Referenced by AIRoad::_BuildRoadStationInternal(), AITileList_StationType::AITileList_StationType(), AIVehicleList_Station::AIVehicleList_Station(), AIAirport::BuildAirport(), AIMarine::BuildDock(), AIRail::BuildNewGRFRailStation(), AIRail::BuildRailStation(), GetCargoRating(), GetCargoWaiting(), GetDistanceManhattanToTile(), GetDistanceSquareToTile(), GetLocation(), GetName(), GetNearestTown(), HasRoadType(), HasStationType(), IsWithinTownInfluence(), and SetName().

StationID AIStation::GetStationID | ( | TileIndex | tile | ) | `[static]`

Get the StationID of a tile, if there is a station.

Parameters:

  | tile | The tile to find the stationID of

Returns:
  StationID of the station.

Postcondition:
  Use IsValidStation() to see if the station is valid.

Definition at line 24 of file ai_station.cpp.

References GetStationIndex(), IsTileType(), IsValidTile(), and MP_STATION.

char * AIStation::GetName | ( | StationID | station_id | ) | `[static]`

Get the name of a station.

Parameters:

  | station_id | The station to get the name of.

Precondition:
  IsValidStation(station_id).

Returns:
  The name of the station.

Definition at line 30 of file ai_station.cpp.

References IsValidStation().

bool AIStation::SetName | ( | StationID | station_id,
|  | const char * | name |
| ) |  |  | `[static]`

Set the name this station.

Parameters:

  | station_id | The station to set the name of.
  | name | The new name of the station.

Precondition:
  IsValidStation(station_id).

  'name' must have at least one character.

  'name' must have at most 30 characters.

Exceptions:

  | AIError::ERR_NAME_IS_NOT_UNIQUE |

Returns:
  True if the name was changed.

Definition at line 42 of file ai_station.cpp.

References CMD_RENAME_STATION, AIObject::DoCommand(), EnforcePrecondition, EnforcePreconditionCustomError, AIError::ERR_PRECONDITION_STRING_TOO_LONG, IsValidStation(), MAX_LENGTH_STATION_NAME_BYTES, and StrEmpty().

TileIndex AIStation::GetLocation | ( | StationID | station_id | ) | `[static]`

Get the current location of a station.

Parameters:

  | station_id | The station to get the location of.

Precondition:
  IsValidStation(station_id).

Returns:
  The tile the station is currently on.

Definition at line 51 of file ai_station.cpp.

References INVALID_TILE, and IsValidStation().

Referenced by GetDistanceManhattanToTile(), GetDistanceSquareToTile(), and IsWithinTownInfluence().

int32 AIStation::GetCargoWaiting | ( | StationID | station_id,
|  | CargoID | cargo_id |
| ) |  |  | `[static]`

See how much cargo there is waiting on a station.

Parameters:

  | station_id | The station to get the cargo-waiting of.
  | cargo_id | The cargo to get the cargo-waiting of.

Precondition:
  IsValidStation(station_id).

  IsValidCargo(cargo_id).

Returns:
  The amount of units waiting at the station.

Definition at line 58 of file ai_station.cpp.

References AICargo::IsValidCargo(), and IsValidStation().

int32 AIStation::GetCargoRating | ( | StationID | station_id,
|  | CargoID | cargo_id |
| ) |  |  | `[static]`

See how high the rating is of a cargo on a station.

Parameters:

  | station_id | The station to get the cargo-rating of.
  | cargo_id | The cargo to get the cargo-rating of.

Precondition:
  IsValidStation(station_id).

  IsValidCargo(cargo_id).

Returns:
  The rating in percent of the cargo on the station.

Definition at line 66 of file ai_station.cpp.

References AICargo::IsValidCargo(), and IsValidStation().

int32 AIStation::GetCoverageRadius | ( | AIStation::StationType | station_type | ) | `[static]`

Get the coverage radius of this type of station.

Parameters:

  | station_type | The type of station.

Returns:
  The radius in tiles.

Definition at line 74 of file ai_station.cpp.

References _settings_game, CA_UNMODIFIED, CountBits(), StationSettings::modified_catchment, GameSettings::station, STATION_AIRPORT, STATION_BUS_STOP, STATION_DOCK, STATION_TRAIN, and STATION_TRUCK_STOP.

int32 AIStation::GetDistanceManhattanToTile | ( | StationID | station_id,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Get the manhattan distance from the tile to the AIStation::GetLocation() of the station.

Parameters:

  | station_id | The station to get the distance to.
  | tile | The tile to get the distance to.

Precondition:
  IsValidStation(station_id).

Returns:
  The distance between station and tile.

Definition at line 92 of file ai_station.cpp.

References AIMap::DistanceManhattan(), GetLocation(), and IsValidStation().

int32 AIStation::GetDistanceSquareToTile | ( | StationID | station_id,
|  | TileIndex | tile |
| ) |  |  | `[static]`

Get the square distance from the tile to the AIStation::GetLocation() of the station.

Parameters:

  | station_id | The station to get the distance to.
  | tile | The tile to get the distance to.

Precondition:
  IsValidStation(station_id).

Returns:
  The distance between station and tile.

Definition at line 99 of file ai_station.cpp.

References AIMap::DistanceSquare(), GetLocation(), and IsValidStation().

bool AIStation::IsWithinTownInfluence | ( | StationID | station_id,
|  | TownID | town_id |
| ) |  |  | `[static]`

Find out if this station is within the rating influence of a town.

Stations within the radius influence the rating of the town.

Parameters:

  | station_id | The station to check.
  | town_id | The town to check.

Returns:
  True if the tile is within the rating influence of the town.

Definition at line 106 of file ai_station.cpp.

References GetLocation(), IsValidStation(), and AITown::IsWithinTownInfluence().

bool AIStation::HasStationType | ( | StationID | station_id,
|  | StationType | station_type |
| ) |  |  | `[static]`

Check if any part of the station contains a station of the type StationType.

Parameters:

  | station_id | The station to look at.
  | station_type | The StationType to look for.

Returns:
  True if the station has a station part of the type StationType.

Definition at line 113 of file ai_station.cpp.

References CountBits(), and IsValidStation().

Referenced by AIAirport::GetAirportType().

bool AIStation::HasRoadType | ( | StationID | station_id,
|  | AIRoad::RoadType | road_type |
| ) |  |  | `[static]`

Check if any part of the station contains a station of the type RoadType.

Parameters:

  | station_id | The station to look at.
  | road_type | The RoadType to look for.

Returns:
  True if the station has a station part of the type RoadType.

Definition at line 121 of file ai_station.cpp.

References AIRoad::IsRoadTypeAvailable(), IsValidStation(), ROADSTOP_BUS, ROADSTOP_TRUCK, and RoadTypeToRoadTypes().

TownID AIStation::GetNearestTown | ( | StationID | station_id | ) | `[static]`

Get the town that was nearest to the given station when the station was built.

Parameters:

  | station_id | The station to look at.

Returns:
  The TownID of the town whose center tile was closest to the station at the time the station was built.

Note:
  There is no guarantee that the station is even near the returned town nor that the returns town is closest to the station now. A station that was 'walked' to the other end of the map will still return the same town. Also, towns grow, towns change. So don't depend on this value too much.

Definition at line 138 of file ai_station.cpp.

References IsValidStation().

The documentation for this class was generated from the following files:

- src/ai/api/ai_station.hpp
- src/ai/api/ai_station.cpp