OpenTTD Source  20241108-master-g80f628063a
tunnelbridge_cmd.cpp File Reference

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

#include "stdafx.h"
#include "newgrf_object.h"
#include "viewport_func.h"
#include "command_func.h"
#include "town.h"
#include "train.h"
#include "ship.h"
#include "roadveh.h"
#include "pathfinder/yapf/yapf_cache.h"
#include "pathfinder/water_regions.h"
#include "newgrf_sound.h"
#include "autoslope.h"
#include "tunnelbridge_map.h"
#include "strings_func.h"
#include "timer/timer_game_calendar.h"
#include "clear_func.h"
#include "vehicle_func.h"
#include "sound_func.h"
#include "tunnelbridge.h"
#include "cheat_type.h"
#include "elrail_func.h"
#include "pbs.h"
#include "company_base.h"
#include "newgrf_railtype.h"
#include "newgrf_roadtype.h"
#include "object_base.h"
#include "water.h"
#include "company_gui.h"
#include "station_func.h"
#include "tunnelbridge_cmd.h"
#include "landscape_cmd.h"
#include "terraform_cmd.h"
#include "table/strings.h"
#include "table/bridge_land.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

void MarkBridgeDirty (TileIndex begin, TileIndex end, DiagDirection direction, uint bridge_height)
 Mark bridge tiles dirty. More...
 
void MarkBridgeDirty (TileIndex tile)
 Mark bridge tiles dirty. More...
 
void ResetBridges ()
 Reset the data been eventually changed by the grf loaded.
 
int CalcBridgeLenCostFactor (int length)
 Calculate the price factor for building a long bridge. More...
 
Foundation GetBridgeFoundation (Slope tileh, Axis axis)
 Get the foundation for a bridge. More...
 
bool HasBridgeFlatRamp (Slope tileh, Axis axis)
 Determines if the track on a bridge ramp is flat or goes up/down. More...
 
static const PalSpriteIDGetBridgeSpriteTable (int index, BridgePieces table)
 
static CommandCost CheckBridgeSlope (BridgePieces bridge_piece, Axis axis, Slope &tileh, int &z)
 Determines the foundation for the bridge head, and tests if the resulting slope is valid. More...
 
CommandCost CheckBridgeAvailability (BridgeType bridge_type, uint bridge_len, DoCommandFlag flags)
 Is a bridge of the specified type and length available? More...
 
static Money TunnelBridgeClearCost (TileIndex tile, Price base_price)
 Calculate the base cost of clearing a tunnel/bridge per tile. More...
 
CommandCost CmdBuildBridge (DoCommandFlag flags, TileIndex tile_end, TileIndex tile_start, TransportType transport_type, BridgeType bridge_type, uint8_t road_rail_type)
 Build a Bridge. More...
 
CommandCost CmdBuildTunnel (DoCommandFlag flags, TileIndex start_tile, TransportType transport_type, uint8_t road_rail_type)
 Build Tunnel. More...
 
static CommandCost CheckAllowRemoveTunnelBridge (TileIndex tile)
 Are we allowed to remove the tunnel or bridge at tile? More...
 
static CommandCost DoClearTunnel (TileIndex tile, DoCommandFlag flags)
 Remove a tunnel from the game, update town rating, etc. More...
 
static CommandCost DoClearBridge (TileIndex tile, DoCommandFlag flags)
 Remove a bridge from the game, update town rating, etc. More...
 
static CommandCost ClearTile_TunnelBridge (TileIndex tile, DoCommandFlag flags)
 Remove a tunnel or a bridge from the game. More...
 
static void DrawPillar (const PalSpriteID *psid, int x, int y, int z, int w, int h, const SubSprite *subsprite)
 Draw a single pillar sprite. More...
 
static int DrawPillarColumn (int z_bottom, int z_top, const PalSpriteID *psid, int x, int y, int w, int h)
 Draw two bridge pillars (north and south). More...
 
static void DrawBridgePillars (const PalSpriteID *psid, const TileInfo *ti, Axis axis, bool drawfarpillar, int x, int y, int z_bridge)
 Draws the pillars under high bridges. More...
 
static void GetBridgeRoadCatenary (const RoadTypeInfo *rti, TileIndex head_tile, int offset, bool head, SpriteID &spr_back, SpriteID &spr_front)
 Retrieve the sprites required for catenary on a road/tram bridge. More...
 
static void DrawBridgeRoadBits (TileIndex head_tile, int x, int y, int z, int offset, bool head)
 Draws the road and trambits over an already drawn (lower end) of a bridge. More...
 
static void DrawTile_TunnelBridge (TileInfo *ti)
 Draws a tunnel of bridge tile. More...
 
static BridgePieces CalcBridgePiece (uint north, uint south)
 Compute bridge piece. More...
 
void DrawBridgeMiddle (const TileInfo *ti)
 Draw the middle bits of a bridge. More...
 
static int GetSlopePixelZ_TunnelBridge (TileIndex tile, uint x, uint y, bool ground_vehicle)
 
static Foundation GetFoundation_TunnelBridge (TileIndex tile, Slope tileh)
 
static void GetTileDesc_TunnelBridge (TileIndex tile, TileDesc *td)
 
static void TileLoop_TunnelBridge (TileIndex tile)
 
static TrackStatus GetTileTrackStatus_TunnelBridge (TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
 
static void ChangeTileOwner_TunnelBridge (TileIndex tile, Owner old_owner, Owner new_owner)
 
template<typename T >
static void PrepareToEnterBridge (T *gv)
 Helper to prepare the ground vehicle when entering a bridge. More...
 
static VehicleEnterTileStatus VehicleEnter_TunnelBridge (Vehicle *v, TileIndex tile, int x, int y)
 
static CommandCost TerraformTile_TunnelBridge (TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
 

Variables

BridgeSpec _bridge [MAX_BRIDGES]
 The specification of all bridges.
 
TileIndex _build_tunnel_endtile
 The end of a tunnel; as hidden return from the tunnel build command for GUI purposes.
 
static const int BRIDGE_Z_START = 3
 Z position of the bridge sprites relative to bridge height (downwards)
 
static const uint8_t TUNNEL_SOUND_FRAME = 1
 Frame when the 'enter tunnel' sound should be played. More...
 
const uint8_t _tunnel_visibility_frame [DIAGDIR_END] = {12, 8, 8, 12}
 Frame when a vehicle should be hidden in a tunnel with a certain direction. More...
 
const TileTypeProcs _tile_type_tunnelbridge_procs
 

Detailed Description

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

Todo:
separate this file into two

Definition in file tunnelbridge_cmd.cpp.

Function Documentation

◆ CalcBridgeLenCostFactor()

int CalcBridgeLenCostFactor ( int  length)

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,

Parameters
lengthLength of the bridge.
Returns
Price factor for the bridge.

Definition at line 108 of file tunnelbridge_cmd.cpp.

Referenced by ShowBuildBridgeWindow().

◆ CalcBridgePiece()

static BridgePieces 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). Note that it is not covering the bridge heads, which are always referenced by the same sprite table. bridge len 1: BRIDGE_PIECE_NORTH bridge len 2: BRIDGE_PIECE_NORTH BRIDGE_PIECE_SOUTH bridge len 3: BRIDGE_PIECE_NORTH BRIDGE_PIECE_MIDDLE_ODD BRIDGE_PIECE_SOUTH bridge len 4: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH bridge len 5: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_MIDDLE_EVEN BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH bridge len 6: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH bridge len 7: BRIDGE_PIECE_NORTH BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_MIDDLE_ODD BRIDGE_PIECE_INNER_NORTH BRIDGE_PIECE_INNER_SOUTH BRIDGE_PIECE_SOUTH #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
northNorthernmost tile of bridge
southSouthernmost tile of bridge
Returns
Index of bridge piece

Definition at line 1523 of file tunnelbridge_cmd.cpp.

Referenced by DrawBridgeMiddle().

◆ CheckAllowRemoveTunnelBridge()

static CommandCost CheckAllowRemoveTunnelBridge ( TileIndex  tile)
inlinestatic

Are we allowed to remove the tunnel or bridge at tile?

Parameters
tileEnd point of the tunnel or bridge.
Returns
A succeeded command if the tunnel or bridge may be removed, a failed command otherwise.

Definition at line 798 of file tunnelbridge_cmd.cpp.

References _current_company, and OWNER_WATER.

Referenced by DoClearBridge(), and DoClearTunnel().

◆ CheckBridgeAvailability()

CommandCost CheckBridgeAvailability ( BridgeType  bridge_type,
uint  bridge_len,
DoCommandFlag  flags 
)

Is a bridge of the specified type and length available?

Parameters
bridge_typeWanted type of bridge.
bridge_lenWanted length of the bridge.
flagsType of operation.
Returns
A succeeded (the requested bridge is available) or failed (it cannot be built) command.

Definition at line 200 of file tunnelbridge_cmd.cpp.

References _settings_game, BridgeSpec::avail_year, CMD_ERROR, GameSettings::construction, DC_QUERY_COST, GetBridgeSpec(), ConstructionSettings::max_bridge_length, MAX_BRIDGES, BridgeSpec::max_length, BridgeSpec::min_length, return_cmd_error, and TimerGameCalendar::year.

Referenced by ShowBuildBridgeWindow().

◆ CheckBridgeSlope()

static CommandCost CheckBridgeSlope ( BridgePieces  bridge_piece,
Axis  axis,
Slope tileh,
int &  z 
)
static

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

Parameters
bridge_pieceDirection of the bridge head.
axisAxis of the bridge
tilehSlope of the tile under the north bridge head; returns slope on top of foundation
zTileZ corresponding to tileh, gets modified as well
Returns
Error or cost for bridge foundation

Definition at line 173 of file tunnelbridge_cmd.cpp.

◆ ClearTile_TunnelBridge()

static CommandCost ClearTile_TunnelBridge ( TileIndex  tile,
DoCommandFlag  flags 
)
static

Remove a tunnel or a bridge from the game.

Parameters
tileTile containing one of the endpoints.
flagsCommand flags.
Returns
Succeeded or failed command.

Definition at line 1017 of file tunnelbridge_cmd.cpp.

References DC_AUTO, DoClearBridge(), DoClearTunnel(), IsTunnel(), and return_cmd_error.

◆ CmdBuildBridge()

CommandCost CmdBuildBridge ( DoCommandFlag  flags,
TileIndex  tile_end,
TileIndex  tile_start,
TransportType  transport_type,
BridgeType  bridge_type,
uint8_t  road_rail_type 
)

Build a Bridge.

Parameters
flagstype of operation
tile_endend tile
tile_startstart tile
transport_typetransport type.
bridge_typebridge type (hi bh)
road_rail_typerail type or road types.
Returns
the cost of this operation or an error

Definition at line 261 of file tunnelbridge_cmd.cpp.

References _current_company, Town::cache, CalcClosestTownFromTile(), CMD_ERROR, DistanceSquare(), INVALID_RAILTYPE, INVALID_ROADTYPE, IsValidTile(), OWNER_DEITY, OWNER_TOWN, return_cmd_error, TownCache::squared_town_zone_radius, TRANSPORT_RAIL, TRANSPORT_ROAD, TRANSPORT_WATER, ValParamRailType(), ValParamRoadType(), and Town::xy.

◆ CmdBuildTunnel()

CommandCost CmdBuildTunnel ( DoCommandFlag  flags,
TileIndex  start_tile,
TransportType  transport_type,
uint8_t  road_rail_type 
)

Build Tunnel.

Parameters
flagstype of operation
start_tilestart tile of tunnel
transport_typetransport type
road_rail_typerailtype or roadtype
Returns
the cost of this operation or an error

Definition at line 624 of file tunnelbridge_cmd.cpp.

References _build_tunnel_endtile, _current_company, Town::cache, CalcClosestTownFromTile(), CMD_ERROR, DistanceSquare(), INVALID_RAILTYPE, INVALID_ROADTYPE, OWNER_DEITY, OWNER_TOWN, TownCache::squared_town_zone_radius, TRANSPORT_RAIL, TRANSPORT_ROAD, ValParamRailType(), ValParamRoadType(), and Town::xy.

◆ DoClearBridge()

static CommandCost DoClearBridge ( TileIndex  tile,
DoCommandFlag  flags 
)
static

Remove a bridge from the game, update town rating, etc.

Parameters
tileTile containing one of the endpoints of the bridge.
flagsCommand flags.
Returns
Succeeded or failed command.

Definition at line 926 of file tunnelbridge_cmd.cpp.

References CheckAllowRemoveTunnelBridge(), CommandCost::Failed(), GetOtherBridgeEnd(), GetTunnelBridgeDirection(), IsTileOwner(), OWNER_TOWN, TileOffsByDiagDir(), and TunnelBridgeIsFree().

Referenced by ClearTile_TunnelBridge().

◆ DoClearTunnel()

static CommandCost DoClearTunnel ( TileIndex  tile,
DoCommandFlag  flags 
)
static

Remove a tunnel from the game, update town rating, etc.

Parameters
tileTile containing one of the endpoints of the tunnel.
flagsCommand flags.
Returns
Succeeded or failed command.

Definition at line 846 of file tunnelbridge_cmd.cpp.

References _build_tunnel_endtile, CheckAllowRemoveTunnelBridge(), CommandCost::Failed(), GetOtherTunnelEnd(), IsTileOwner(), OWNER_TOWN, and TunnelBridgeIsFree().

Referenced by ClearTile_TunnelBridge().

◆ DrawBridgeMiddle()

◆ DrawBridgePillars()

static void DrawBridgePillars ( const PalSpriteID psid,
const TileInfo ti,
Axis  axis,
bool  drawfarpillar,
int  x,
int  y,
int  z_bridge 
)
static

Draws the pillars under high bridges.

Parameters
psidImage and palette of a bridge pillar.
tiTileInfo of current bridge-middle-tile.
axisOrientation of bridge.
drawfarpillarWhether to draw the pillar at the back
xSprite X position of front pillar.
ySprite Y position of front pillar.
z_bridgeAbsolute height of bridge bottom.

< bounding box size of pillars along bridge direction

< sprite position offset of back facing pillar

< big number compared to sprite size

Definition at line 1075 of file tunnelbridge_cmd.cpp.

◆ DrawBridgeRoadBits()

static void DrawBridgeRoadBits ( TileIndex  head_tile,
int  x,
int  y,
int  z,
int  offset,
bool  head 
)
static

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

Parameters
head_tilebridge head tile with roadtype information
xthe x of the bridge
ythe y of the bridge
zthe z of the bridge
offsetsprite offset identifying flat to sloped bridge tiles
headare we drawing bridge head?

Definition at line 1157 of file tunnelbridge_cmd.cpp.

◆ DrawPillar()

static void DrawPillar ( const PalSpriteID psid,
int  x,
int  y,
int  z,
int  w,
int  h,
const SubSprite subsprite 
)
inlinestatic

Draw a single pillar sprite.

Parameters
psidPillarsprite
xPillar X
yPillar Y
zPillar Z
wBounding box size in X direction
hBounding box size in Y direction
subspriteOptional subsprite for drawing halfpillars

< Start offset of pillar wrt. bridge (downwards)

Definition at line 1038 of file tunnelbridge_cmd.cpp.

◆ DrawPillarColumn()

static int DrawPillarColumn ( int  z_bottom,
int  z_top,
const PalSpriteID psid,
int  x,
int  y,
int  w,
int  h 
)
static

Draw two bridge pillars (north and south).

Parameters
z_bottomBottom Z
z_topTop Z
psidPillarsprite
xPillar X
yPillar Y
wBounding box size in X direction
hBounding box size in Y direction
Returns
Reached Z at the bottom

Definition at line 1055 of file tunnelbridge_cmd.cpp.

◆ DrawTile_TunnelBridge()

static void DrawTile_TunnelBridge ( TileInfo ti)
static

Draws a tunnel of bridge tile.

For tunnels, this is rather simple, as you only need to 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
tiTileInfo 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 1257 of file tunnelbridge_cmd.cpp.

References RailTypeInfo::base_sprites, GetCustomRailSprite(), GetRailType(), GetRailTypeInfo(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), IsTunnel(), RTSG_TUNNEL_PORTAL, SPR_RAILTYPE_TUNNEL_BASE, TileInfo::tile, TRANSPORT_RAIL, and RailTypeInfo::tunnel.

◆ GetBridgeFoundation()

Foundation GetBridgeFoundation ( Slope  tileh,
Axis  axis 
)

Get the foundation for a bridge.

Parameters
tilehThe slope to build the bridge on.
axisThe axis of the bridge entrance.
Returns
The foundation required.

Definition at line 129 of file tunnelbridge_cmd.cpp.

References AXIS_X, AXIS_Y, FlatteningFoundation(), FOUNDATION_NONE, HasSlopeHighestCorner(), InclinedFoundation(), SLOPE_FLAT, SLOPE_NE, SLOPE_NW, SLOPE_SE, and SLOPE_SW.

Referenced by GetBridgeHeight(), and HasBridgeFlatRamp().

◆ GetBridgeRoadCatenary()

static void GetBridgeRoadCatenary ( const RoadTypeInfo rti,
TileIndex  head_tile,
int  offset,
bool  head,
SpriteID spr_back,
SpriteID spr_front 
)
static

Retrieve the sprites required for catenary on a road/tram bridge.

Parameters
rtiRoadTypeInfo for the road or tram type to get catenary for
head_tileBridge head tile with roadtype information
offsetSprite offset identifying flat to sloped bridge tiles
headAre we drawing bridge head?
[out]spr_backBack catenary sprite to use
[out]spr_frontFront catenary sprite to use

Definition at line 1131 of file tunnelbridge_cmd.cpp.

References GetCustomRoadSprite(), ROTSG_CATENARY_BACK, ROTSG_CATENARY_FRONT, SPR_TRAMWAY_BASE, TCX_NORMAL, and TCX_ON_BRIDGE.

◆ HasBridgeFlatRamp()

bool HasBridgeFlatRamp ( Slope  tileh,
Axis  axis 
)

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

Parameters
tilehSlope of the tile under the bridge head
axisOrientation of bridge
Returns
true iff the track is flat.

Definition at line 145 of file tunnelbridge_cmd.cpp.

References ApplyFoundationToSlope(), GetBridgeFoundation(), and SLOPE_FLAT.

Referenced by CYapfCostBase::stSlopeCost().

◆ MarkBridgeDirty() [1/2]

void MarkBridgeDirty ( TileIndex  begin,
TileIndex  end,
DiagDirection  direction,
uint  bridge_height 
)

Mark bridge tiles dirty.

Note: The bridge does not need to exist, everything is passed via parameters.

Parameters
beginStart tile.
endEnd tile.
directionDirection from begin to end.
bridge_heightBridge height level.

Definition at line 67 of file tunnelbridge_cmd.cpp.

References MarkTileDirtyByTile(), TileHeight(), and TileOffsByDiagDir().

Referenced by ClearPathReservation(), CmdConvertRail(), MarkBridgeDirty(), TryReserveRailTrack(), and UnreserveRailTrack().

◆ MarkBridgeDirty() [2/2]

void MarkBridgeDirty ( TileIndex  tile)

Mark bridge tiles dirty.

Parameters
tileBridge head.

Definition at line 80 of file tunnelbridge_cmd.cpp.

References GetBridgeHeight(), GetOtherTunnelBridgeEnd(), GetTunnelBridgeDirection(), and MarkBridgeDirty().

◆ PrepareToEnterBridge()

template<typename T >
static void PrepareToEnterBridge ( T *  gv)
static

Helper to prepare the ground vehicle when entering a bridge.

This get called when entering the bridge, at the last frame of travel on the bridge head. Our calling function gets called before UpdateInclination/UpdateZPosition, which normally controls the Z-coordinate. However, in the wormhole of the bridge the vehicle is in a strange state so UpdateInclination does not get called for the wormhole of the bridge and as such the going up/down bits would remain set. As such, this function clears those. In doing so, the call to UpdateInclination will not update the Z-coordinate, so that has to be done here as well.

Parameters
gvThe ground vehicle entering the bridge.

Definition at line 1869 of file tunnelbridge_cmd.cpp.

◆ TunnelBridgeClearCost()

static Money TunnelBridgeClearCost ( TileIndex  tile,
Price  base_price 
)
static

Calculate the base cost of clearing a tunnel/bridge per tile.

Parameters
tileStart tile of the tunnel/bridge.
Returns
How much clearing this tunnel/bridge costs per tile.

Definition at line 224 of file tunnelbridge_cmd.cpp.

Variable Documentation

◆ _tile_type_tunnelbridge_procs

const TileTypeProcs _tile_type_tunnelbridge_procs
extern
Initial value:
= {
GetSlopePixelZ_TunnelBridge,
nullptr,
GetTileDesc_TunnelBridge,
GetTileTrackStatus_TunnelBridge,
nullptr,
nullptr,
TileLoop_TunnelBridge,
ChangeTileOwner_TunnelBridge,
nullptr,
VehicleEnter_TunnelBridge,
GetFoundation_TunnelBridge,
TerraformTile_TunnelBridge,
}
static void DrawTile_TunnelBridge(TileInfo *ti)
Draws a tunnel of bridge tile.
static CommandCost ClearTile_TunnelBridge(TileIndex tile, DoCommandFlag flags)
Remove a tunnel or a bridge from the game.

Definition at line 57 of file landscape.cpp.

◆ _tunnel_visibility_frame

const uint8_t _tunnel_visibility_frame[DIAGDIR_END] = {12, 8, 8, 12}
extern

Frame when a vehicle should be hidden in a tunnel with a certain direction.

This differs per direction, because of visibility / bounding box issues. Note that direction, in this case, is the direction leading into the tunnel. When entering a tunnel, hide the vehicle when it reaches the given frame. When leaving a tunnel, show the vehicle when it is one frame further to the 'outside', i.e. at (TILE_SIZE-1) - (frame) + 1

◆ TUNNEL_SOUND_FRAME

const uint8_t TUNNEL_SOUND_FRAME = 1
static

Frame when the 'enter tunnel' sound should be played.

This is the second frame on a tile, so the sound is played shortly after entering the tunnel tile, while the vehicle is still visible.

Definition at line 1884 of file tunnelbridge_cmd.cpp.