# AITunnel Class Reference

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

`#include <ai_tunnel.hpp>`

Inheritance diagram for AITunnel:

## Public Types

enum | ErrorMessages { ERR_TUNNEL_BASE = AIError::ERR_CAT_TUNNEL << AIError::ERR_CAT_BIT_SIZE, 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 const char * | GetClassName ()
static bool | IsTunnelTile (TileIndex tile)
| Check whether the tile is an entrance to a tunnel.
static TileIndex | GetOtherTunnelEnd (TileIndex tile)
| Get the tile that exits on the other end of a (would be) tunnel starting at tile.
static bool | _BuildTunnelRoad1 ()
| Internal function to help BuildTunnel in case of road.
static bool | _BuildTunnelRoad2 ()
| Internal function to help BuildTunnel in case of road.
static bool | BuildTunnel (AIVehicle::VehicleType vehicle_type, TileIndex start)
| Builds a tunnel starting at start.
static bool | RemoveTunnel (TileIndex tile)
| Remove the tunnel whose entrance is located at tile.

## Detailed Description

Class that handles all tunnel related functions.

Definition at line 21 of file ai_tunnel.hpp.

## Member Enumeration Documentation

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 | An other tunnel is in the way.
  ERR_TUNNEL_END_SITE_UNSUITABLE | Unable to excavate land at the end to create the tunnel's exit.

Definition at line 28 of file ai_tunnel.hpp.

## Member Function Documentation

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.

Definition at line 18 of file ai_tunnel.cpp.

References IsTunnelTile(), and IsValidTile().

Referenced by GetOtherTunnelEnd(), and RemoveTunnel().

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.

Definition at line 24 of file ai_tunnel.cpp.

References GetInclinedSlopeDirection(), GetOtherTunnelEnd(), GetTileSlope(), INVALID_DIAGDIR, INVALID_TILE, IsTunnelTile(), IsValidTile(), and TileOffsByDiagDir().

Referenced by _BuildTunnelRoad1(), and _BuildTunnelRoad2().

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 || (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.

Definition at line 72 of file ai_tunnel.cpp.

References CMD_BUILD_TUNNEL, AIObject::DoCommand(), EnforcePrecondition, AIRail::GetCurrentRailType(), AIObject::GetRoadType(), AIRail::IsRailTypeAvailable(), IsValidTile(), RoadTypeToRoadTypes(), AIObject::SetCallbackVariable(), TRANSPORT_RAIL, TRANSPORT_ROAD, AIVehicle::VT_RAIL, and AIVehicle::VT_ROAD.

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.

Definition at line 120 of file ai_tunnel.cpp.

References CMD_LANDSCAPE_CLEAR, AIObject::DoCommand(), EnforcePrecondition, and IsTunnelTile().

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

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