# tunnelbridge_cmd.cpp File Reference

This file deals with tunnels and bridges (non-gui stuff). More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "bridge_map.h"`
`#include "rail_map.h"`
`#include "road_map.h"`
`#include "tile_cmd.h"`
`#include "landscape.h"`
`#include "tunnel_map.h"`
`#include "unmovable_map.h"`
`#include "viewport_func.h"`
`#include "command_func.h"`
`#include "town.h"`
`#include "variables.h"`
`#include "bridge.h"`
`#include "train.h"`
`#include "water_map.h"`
`#include "yapf/yapf.h"`
`#include "newgrf_sound.h"`
`#include "autoslope.h"`
`#include "transparency.h"`
`#include "tunnelbridge_map.h"`
`#include "strings_func.h"`
`#include "date_func.h"`
`#include "functions.h"`
`#include "vehicle_func.h"`
`#include "sound_func.h"`
`#include "signal_func.h"`
`#include "tunnelbridge.h"`
`#include "player_base.h"`
`#include "table/sprites.h"`
`#include "table/strings.h"`
`#include "table/bridge_land.h"`

Go to the source code of this file.

## Functions
void | ResetBridges ()
| Reset the data been eventually changed by the grf loaded.
int | CalcBridgeLenCostFactor (int x)
| calculate the price factor for building a long bridge.
Foundation | GetBridgeFoundation (Slope tileh, Axis axis)
bool | HasBridgeFlatRamp (Slope tileh, Axis axis)
| Determines if the track on a bridge ramp is flat or goes up/down.
static const PalSpriteID * | GetBridgeSpriteTable (int index, byte table)
static CommandCost | CheckBridgeSlopeNorth (Axis axis, Slope *tileh, uint *z)
| Determines the foundation for the north bridge head, and tests if the resulting slope is valid.
static CommandCost | CheckBridgeSlopeSouth (Axis axis, Slope *tileh, uint *z)
| Determines the foundation for the south bridge head, and tests if the resulting slope is valid.
bool | CheckBridge_Stuff (BridgeType bridge_type, uint bridge_len, uint32 flags)
CommandCost | CmdBuildBridge (TileIndex end_tile, uint32 flags, uint32 p1, uint32 p2)
| Build a Bridge.
CommandCost | CmdBuildTunnel (TileIndex start_tile, uint32 flags, uint32 p1, uint32 p2)
| Build Tunnel.
static bool | CheckAllowRemoveTunnelBridge (TileIndex tile)
static CommandCost | DoClearTunnel (TileIndex tile, uint32 flags)
static CommandCost | DoClearBridge (TileIndex tile, uint32 flags)
static CommandCost | ClearTile_TunnelBridge (TileIndex tile, byte flags)
static void | DrawBridgePillars (const PalSpriteID *psid, const TileInfo *ti, Axis axis, BridgeType type, int x, int y, int z_bridge)
| Draws the pillars under high bridges.
static void | DrawBridgeTramBits (int x, int y, byte z, int offset, bool overlay)
| Draws the trambits over an already drawn (lower end) of a bridge.
static void | DrawTile_TunnelBridge (TileInfo *ti)
| Draws a tunnel of bridge tile.
static uint | CalcBridgePiece (uint north, uint south)
| Compute bridge piece.
void | DrawBridgeMiddle (const TileInfo *ti)
static uint | GetSlopeZ_TunnelBridge (TileIndex tile, uint x, uint y)
static Foundation | GetFoundation_TunnelBridge (TileIndex tile, Slope tileh)
static void | GetAcceptedCargo_TunnelBridge (TileIndex tile, AcceptedCargo ac)
static void | GetTileDesc_TunnelBridge (TileIndex tile, TileDesc *td)
static void | AnimateTile_TunnelBridge (TileIndex tile)
static void | TileLoop_TunnelBridge (TileIndex tile)
static void | ClickTile_TunnelBridge (TileIndex tile)
static TrackStatus | GetTileTrackStatus_TunnelBridge (TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
static void | ChangeTileOwner_TunnelBridge (TileIndex tile, PlayerID old_player, PlayerID new_player)
static VehicleEnterTileStatus | VehicleEnter_TunnelBridge (Vehicle *v, TileIndex tile, int x, int y)
static CommandCost | TerraformTile_TunnelBridge (TileIndex tile, uint32 flags, uint z_new, Slope tileh_new)
## Variables
BridgeSpec | _bridge [MAX_BRIDGES]
static const byte | _tunnel_fractcoord_1 [4] = {0x8E, 0x18, 0x81, 0xE8}
static const byte | _tunnel_fractcoord_2 [4] = {0x81, 0x98, 0x87, 0x38}
static const byte | _tunnel_fractcoord_3 [4] = {0x82, 0x88, 0x86, 0x48}
static const byte | _exit_tunnel_track [4] = {1, 2, 1, 2}
static const Trackdir | _road_exit_tunnel_state [DIAGDIR_END]
| Get the trackdir of the exit of a tunnel.
static const byte | _road_exit_tunnel_frame [4] = {2, 7, 9, 4}
static const byte | _tunnel_fractcoord_4 [4] = {0x52, 0x85, 0x98, 0x29}
static const byte | _tunnel_fractcoord_5 [4] = {0x92, 0x89, 0x58, 0x25}
static const byte | _tunnel_fractcoord_6 [4] = {0x92, 0x89, 0x56, 0x45}
static const byte | _tunnel_fractcoord_7 [4] = {0x52, 0x85, 0x96, 0x49}
const TileTypeProcs | _tile_type_tunnelbridge_procs

## Detailed Description

This file deals with tunnels and bridges (non-gui stuff).

Todo:
  seperate this file into two

Definition in file tunnelbridge_cmd.cpp.

## Function Documentation

int CalcBridgeLenCostFactor | ( | int | x | ) |

calculate the price factor for building a long bridge.

basically the cost delta is 1,1, 1, 2,2, 3,3,3, 4,4,4,4, 5,5,5,5,5, 6,6,6,6,6,6, 7,7,7,7,7,7,7, 8,8,8,8,8,8,8,8,

Definition at line 64 of file tunnelbridge_cmd.cpp.

Referenced by CmdBuildBridge().

static uint CalcBridgePiece | ( | uint | north,
|  | uint | south |
| ) |  |  | `[static]`

Compute bridge piece.

Computes the bridge piece to display depending on the position inside the bridge. bridges pieces sequence (middle parts) bridge len 1: 0 bridge len 2: 0 1 bridge len 3: 0 4 1 bridge len 4: 0 2 3 1 bridge len 5: 0 2 5 3 1 bridge len 6: 0 2 3 2 3 1 bridge len 7: 0 2 3 4 2 3 1 #0 - always as first, #1 - always as last (if len>1) #2,#3 are to pair in order for odd bridges: #5 is going in the bridge middle if on even position, #4 on odd (counting from 0)

Parameters:

  | north | Northernmost tile of bridge
  | south | Southernmost tile of bridge

Returns:
  Index of bridge piece

Definition at line 966 of file tunnelbridge_cmd.cpp.

static CommandCost CheckBridgeSlopeNorth | ( | Axis | axis,
|  | Slope * | tileh,
|  | uint * | z |
| ) |  |  | `[static]`

Determines the foundation for the north bridge head, and tests if the resulting slope is valid.

Parameters:

  | axis | Axis of the bridge
  | tileh | Slope of the tile under the north bridge head; returns slope on top of foundation
  | z | TileZ corresponding to tileh, gets modified as well

Returns:
  Error or cost for bridge foundation

Definition at line 121 of file tunnelbridge_cmd.cpp.

References AXIS_X, CMD_ERROR, FOUNDATION_NONE, SLOPE_FLAT, SLOPE_NE, and SLOPE_NW.

Referenced by CmdBuildBridge().

static CommandCost CheckBridgeSlopeSouth | ( | Axis | axis,
|  | Slope * | tileh,
|  | uint * | z |
| ) |  |  | `[static]`

Determines the foundation for the south bridge head, and tests if the resulting slope is valid.

Parameters:

  | axis | Axis of the bridge
  | tileh | Slope of the tile under the south bridge head; returns slope on top of foundation
  | z | TileZ corresponding to tileh, gets modified as well

Returns:
  Error or cost for bridge foundation

Definition at line 142 of file tunnelbridge_cmd.cpp.

References AXIS_X, CMD_ERROR, FOUNDATION_NONE, SLOPE_FLAT, SLOPE_SE, and SLOPE_SW.

Referenced by CmdBuildBridge().

CommandCost CmdBuildBridge | ( | TileIndex | end_tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Build a Bridge.

Parameters:

  | end_tile | end tile
  | flags | type of operation
  | p1 | packed start tile coords (~ dx)
  | p2 | various bitstuffed elements p2 = (bit 0- 7) - bridge type (hi bh) p2 = (bit 8-..) - rail type or road types. p2 = (bit 15 ) - set means road bridge.

Definition at line 181 of file tunnelbridge_cmd.cpp.

References CommandCost::AddCost(), AddSideToSignalBuffer(), AreValidRoadTypes(), AXIS_X, AXIS_Y, AxisToDiagDir(), AxisToTrack(), CalcBridgeLenCostFactor(), CheckBridgeSlopeNorth(), CheckBridgeSlopeSouth(), CMD_ERROR, CMD_LANDSCAPE_CLEAR, CmdFailed(), DC_EXEC, DiagDirToAxis(), DoCommand(), GB(), GetBridgeAxis(), GetBridgeHeight(), GetBridgeType(), CommandCost::GetCost(), GetNorthernBridgeEnd(), GetOtherBridgeEnd(), GetRailType(), GetTileType(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), INVALID_DIAGDIR, INVALID_RAILTYPE, IsBridgeAbove(), IsBridgeTile(), IsOwnedLand(), IsPlainRailTile(), IsTileOwner(), IsTunnel(), IsWater(), MakeRailBridgeRamp(), MakeRoadBridgeRamp(), MapSize(), MarkTileDirtyByTile(), MayHaveBridgeAbove(), MP_CLEAR, MP_RAILWAY, MP_ROAD, MP_TUNNELBRIDGE, MP_UNMOVABLE, MP_WATER, OWNER_TOWN, return_cmd_error, ReverseDiagDir(), ROADTYPES_NONE, SetBridgeMiddle(), Swap(), TILE_HEIGHT, TileDiffXY(), TileX(), TileXY(), TileY(), ValParamRailtype(), and YapfNotifyTrackLayoutChange().

CommandCost CmdBuildTunnel | ( | TileIndex | start_tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Build Tunnel.

Parameters:

  | start_tile | start tile of tunnel
  | flags | type of operation
  | p1 | railtype or roadtypes. bit 9 set means road tunnel
  | p2 | unused

Tile shift coeficient. Will decrease for very long tunnels to avoid exponential growth of price

Number of tiles from start of tunnel

Number of tiles at which the cost increase coefficient per tile is halved

Definition at line 455 of file tunnelbridge_cmd.cpp.

References CommandCost::AddCost(), AddSideToSignalBuffer(), AreValidRoadTypes(), AXIS_X, AxisToTrack(), CMD_ERROR, CMD_LANDSCAPE_CLEAR, CMD_TERRAFORM_LAND, CmdFailed(), ComplementSlope(), DC_AUTO, DC_EXEC, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, DiagDirToAxis(), DoCommand(), GB(), CommandCost::GetCost(), GetInclinedSlopeDirection(), INVALID_DIAGDIR, IsTunnelInWayDir(), MakeRailTunnel(), MakeRoadTunnel(), MapMaxX(), OtherAxis(), return_cmd_error, ReverseDiagDir(), TileOffsByDiagDir(), TileX(), TileY(), ValParamRailtype(), and YapfNotifyTrackLayoutChange().

static void DrawBridgePillars | ( | const PalSpriteID * | psid,
|  | const TileInfo * | ti,
|  | Axis | axis,
|  | BridgeType | type,
|  | int | x,
|  | int | y,
|  | int | z_bridge |
| ) |  |  | `[static]`

Draws the pillars under high bridges.

Parameters:

  | psid | Image and palette of a bridge pillar.
  | ti | TileInfo of current bridge-middle-tile.
  | axis | Orientation of bridge.
  | type | Bridge type.
  | x | Sprite X position of front pillar.
  | y | Sprite Y position of front pillar.
  | z_bridge | Absolute height of bridge bottom.

Definition at line 722 of file tunnelbridge_cmd.cpp.

References AddSortableSpriteToDraw(), AXIS_X, AXIS_Y, AxisToDiagDir(), BB_HEIGHT_UNDER_BRIDGE, HasBit(), IsTransparencySet(), ReverseDiagDir(), TILE_HEIGHT, and TO_BRIDGES.

static void DrawBridgeTramBits | ( | int | x,
|  | int | y,
|  | byte | z,
|  | int | offset,
|  | bool | overlay |
| ) |  |  | `[static]`

Draws the trambits over an already drawn (lower end) of a bridge.

Parameters:

  | x | the x of the bridge
  | y | the y of the bridge
  | z | the z of the bridge
  | offset | number representing whether to level or sloped and the direction
  | overlay | do we want to still see the road?

Definition at line 777 of file tunnelbridge_cmd.cpp.

References AddSortableSpriteToDraw(), IsTransparencySet(), TO_BRIDGES, and TO_CATENARY.

Referenced by DrawTile_TunnelBridge().

static void DrawTile_TunnelBridge | ( | TileInfo * | ti | ) | `[static]`

Draws a tunnel of bridge tile.

For tunnels, this is rather simple, as you only needa draw the entrance. Bridges are a bit more complex. base_offset is where the sprite selection comes into play and it works a bit like a bitmask.

For bridge heads:

Parameters:

  | ti | TileInfo of the structure to draw Bit 0: direction Bit 1: northern or southern heads Bit 2: Set if the bridge head is sloped Bit 3 and more: Railtype Specific subset Please note that in this code, "roads" are treated as railtype 1, whilst the real railtypes are 0, 2 and 3

Definition at line 815 of file tunnelbridge_cmd.cpp.

References AddSortableSpriteToDraw(), RailtypeInfo::base_sprites, BB_Z_SEPARATOR, RailtypeInfo::bridge_offset, DiagDirToAxis(), DrawBridgeTramBits(), DrawCatenary(), DrawCatenaryOnTunnel(), DrawGroundSprite(), GetBridgeType(), GetRailType(), GetRailTypeInfo(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), HasBit(), HasTunnelBridgeSnowOrDesert(), IsTransparencySet(), IsTunnel(), RAILTYPE_ELECTRIC, ROADTYPE_ROAD, ROADTYPE_TRAM, ROADTYPES_TRAM, SLOPE_FLAT, TILE_HEIGHT, TILE_SIZE, TO_BRIDGES, TO_CATENARY, and RailtypeInfo::tunnel.

bool HasBridgeFlatRamp | ( | Slope | tileh,
|  | Axis | axis |
| ) |  |  |

Determines if the track on a bridge ramp is flat or goes up/down.

Parameters:

  | tileh | Slope of the tile under the bridge head
  | axis | Orientation of bridge

Returns:
  true iff the track is flat.

Definition at line 94 of file tunnelbridge_cmd.cpp.

References SLOPE_FLAT.

void ResetBridges | ( |  | ) |

Reset the data been eventually changed by the grf loaded.

Definition at line 45 of file tunnelbridge_cmd.cpp.

Referenced by ResetNewGRFData().

## Variable Documentation

const Trackdir _road_exit_tunnel_state[DIAGDIR_END]`[static]`

Initial value:

     {
      TRACKDIR_X_SW, TRACKDIR_Y_NW, TRACKDIR_X_NE, TRACKDIR_Y_SE
    }

Get the trackdir of the exit of a tunnel.

Definition at line 1234 of file tunnelbridge_cmd.cpp.

const TileTypeProcs _tile_type_tunnelbridge_procs

Initial value:

     {
      DrawTile_TunnelBridge,
      GetSlopeZ_TunnelBridge,
      ClearTile_TunnelBridge,
      GetAcceptedCargo_TunnelBridge,
      GetTileDesc_TunnelBridge,
      GetTileTrackStatus_TunnelBridge,
      ClickTile_TunnelBridge,
      AnimateTile_TunnelBridge,
      TileLoop_TunnelBridge,
      ChangeTileOwner_TunnelBridge,
      NULL,
      VehicleEnter_TunnelBridge,
      GetFoundation_TunnelBridge,
      TerraformTile_TunnelBridge,
    }