How is tunnel price calculated in openttd?

These data are taken from a new game with time paused, construction during pause turned on, and the ground on both sides demolished (to remove any difference caused by the cost of removing whatever was on the tile). Each tunnel's cost was estimated by Shift-Clicking on the position it will be build.

The costs are mostly exponential, but at the higher end for tunnels longer than ~45 tiles the curve seems to bottom out and become more linear. In almost all instance it definitely makes sense to break a long tunnel up into smaller tunnels whenever possible. In addition to being cheaper, this also increases the capacity for railroad tunnels, since only one train can occupy a tunnel at once because signals cannot be built inside tunnels.


Also see tunnelbridge.cpp in the openTTD sources:

/* Tile shift coefficient. Will decrease for very long tunnels to avoid exponential growth of price*/
int tiles_coef = 3;
/* Number of tiles from start of tunnel */
int tiles = 0;
/* Number of tiles at which the cost increase coefficient per tile is halved */
int tiles_bump = 25;
CommandCost cost(EXPENSES_CONSTRUCTION);
Slope end_tileh;
for (;;) {
  end_tile += delta;
  if (!IsValidTile(end_tile)) return_cmd_error(STR_ERROR_TUNNEL_THROUGH_MAP_BORDER);
  end_tileh = GetTileSlope(end_tile, &end_z);

  if (start_z == end_z) break;

  if (!_cheats.crossing_tunnels.value && IsTunnelInWayDir(end_tile, start_z, tunnel_in_way_dir)) {
    return_cmd_error(STR_ERROR_ANOTHER_TUNNEL_IN_THE_WAY);
  }

  tiles++;
  if (tiles == tiles_bump) {
    tiles_coef++;
    tiles_bump *= 2;
  }

  cost.AddCost(_price[PR_BUILD_TUNNEL]);
  cost.AddCost(cost.GetCost() >> tiles_coef); // add a multiplier for longer tunnels
}

/* Add the cost of the entrance */
cost.AddCost(_price[PR_BUILD_TUNNEL]);
cost.AddCost(ret);

What this loop will do: The cost is determined by a recursion. Let the tunnel length be 'n'. Then F is the cost of the tunnel, with the following definition:

formula

In this formula, B is the 'base cost' of a tunnel tile, a fixed quantity. I is a multiplier based on 'inflation'. L is the cost to 'bulldoze' a single tile. Note that this cost is still added even if you bulldozed both sides of the tunnel. This formula corresponds to exponential growth of the cost per tile by 12.5% for each tile up until 25 tiles, then it grows by 6.25%, then 3.125%, and so on.

This process resembles a linear function on larger scales. It's very closely approximated by the curve

formula

for n > 50. (Before inflation, With C being a constant value in this function). Some experimentation with Private Pansy's values yields B = 400 with her settings. Depending on your 'construction cost' setting and any NewGRFs you use this value may differ.