OpenTTD AI API: AITunnel Class Reference

OpenTTD AI API 13.1

Public Types | Static Public Member Functions
AITunnel Class Reference

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

Inheritance diagram for AITunnel:

## Public Types
enum | ErrorMessages { ERR_TUNNEL_BASE, ERR_TUNNEL_CANNOT_BUILD_ON_WATER, ERR_TUNNEL_START_SITE_UNSUITABLE, ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY, ERR_TUNNEL_END_SITE_UNSUITABLE }
| All tunnel related errors. More...

## Static Public Member Functions
static bool | IsTunnelTile (TileIndex tile)
| Check whether the tile is an entrance to a tunnel. More...
static TileIndex | GetOtherTunnelEnd (TileIndex tile)
| Get the tile that exits on the other end of a (would be) tunnel starting at tile. More...
static bool | BuildTunnel (AIVehicle::VehicleType vehicle_type, TileIndex start)
| Builds a tunnel starting at start. More...
static bool | RemoveTunnel (TileIndex tile)
| Remove the tunnel whose entrance is located at tile. More...

## Detailed Description

Class that handles all tunnel related functions.

## Member Enumeration Documentation

## ◆ ErrorMessages

enum AITunnel::ErrorMessages

All tunnel related errors.

Enumerator
ERR_TUNNEL_BASE | Base for bridge related errors.
ERR_TUNNEL_CANNOT_BUILD_ON_WATER | Can't build tunnels on water.
ERR_TUNNEL_START_SITE_UNSUITABLE | The start tile must slope either North, South, West or East.
ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY | Another tunnel is in the way.
ERR_TUNNEL_END_SITE_UNSUITABLE | Unable to excavate land at the end to create the tunnel's exit.

## Member Function Documentation

## ◆ BuildTunnel()

static bool AITunnel::BuildTunnel | ( | AIVehicle::VehicleType | vehicle_type,
|  | TileIndex | start
| ) |  |
| static

Builds a tunnel starting at start.

The direction of the tunnel depends on the slope of the start tile. Tunnels can be created for either rails or roads; use the appropriate AIVehicle::VehicleType. As an extra for road, this functions builds two half-pieces of road on each end of the tunnel, making it easier for you to connect it to your network.

Parameters

  start | Where to start the tunnel.
  vehicle_type | The vehicle-type of tunnel to build.

Precondition
  AIMap::IsValidTile(start).
  (vehicle_type == AIVehicle::VT_ROAD && AIRoad::IsRoadTypeAvailable(AIRoad::GetCurrentRoadType())) || (vehicle_type == AIVehicle::VT_RAIL && AIRail::IsRailTypeAvailable(AIRail::GetCurrentRailType())).

Exceptions

  AIError::ERR_AREA_NOT_CLEAR |
  AITunnel::ERR_TUNNEL_CANNOT_BUILD_ON_WATER |
  AITunnel::ERR_TUNNEL_START_SITE_UNSUITABLE |
  AITunnel::ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY |
  AITunnel::ERR_TUNNEL_END_SITE_UNSUITABLE |

Returns
  Whether the tunnel has been/can be build or not.

Note
  The slope of a tile can be determined by AITile::GetSlope(TileIndex).
  No matter if the road pieces were build or not, if building the tunnel succeeded, this function returns true.

## ◆ GetOtherTunnelEnd()

static TileIndex AITunnel::GetOtherTunnelEnd | ( | TileIndex | tile | ) |
| static

Get the tile that exits on the other end of a (would be) tunnel starting at tile.

If there is no 'simple' inclined slope at the start tile, this function will return AIMap::TILE_INVALID.

Parameters

  tile | The tile that is an entrance to a tunnel or the tile where you may want to build a tunnel.

Precondition
  AIMap::IsValidTile(tile).

Returns
  The TileIndex that is the other end of the (would be) tunnel, or AIMap::TILE_INVALID if no other end was found (can't build tunnel).

Note
  Even if this function returns a valid tile, that is no guarantee that building a tunnel will succeed. Use BuildTunnel in AITestMode to check whether a tunnel can actually be build.

## ◆ IsTunnelTile()

static bool AITunnel::IsTunnelTile | ( | TileIndex | tile | ) |
| static

Check whether the tile is an entrance to a tunnel.

Parameters

  tile | The tile to check.

Precondition
  AIMap::IsValidTile(tile).

Returns
  True if and only if the tile is the beginning or end of a tunnel.

## ◆ RemoveTunnel()

static bool AITunnel::RemoveTunnel | ( | TileIndex | tile | ) |
| static

Remove the tunnel whose entrance is located at tile.

Parameters

  tile | The tile that is an entrance to a tunnel.

Precondition
  AIMap::IsValidTile(tile) && IsTunnelTile(tile).

Exceptions

  AIError::ERR_OWNED_BY_ANOTHER_COMPANY |

Returns
  Whether the tunnel has been/can be removed or not.