OpenTTD Source: src/signal.cpp File Reference

OpenTTD Source 15.3

Data Structures | Typedefs | Enumerations | Functions | Variables
signal.cpp File Reference

functions related to rail signals updating More...

`#include "stdafx.h"`
`#include "debug.h"`
`#include "station_map.h"`
`#include "tunnelbridge_map.h"`
`#include "vehicle_func.h"`
`#include "viewport_func.h"`
`#include "train.h"`
`#include "company_base.h"`
`#include "pbs.h"`
`#include "table/signal_data.h"`
`#include "safeguards.h"`

Go to the source code of this file.

## Data Structures
struct | SmallSet< Tdir, items >
| Set containing 'items' items of 'tile and Tdir' No tree structure is used because it would cause slowdowns in most usual cases. More...
struct | SmallSet< Tdir, items >::SSdata
| Element of set. More...

## Typedefs
using | SigFlags = EnumBitSet< SigFlag, uint16_t >

## Enumerations
enum class | SigFlag : uint8_t { Train , Exit , MultiExit , Green , MultiGreen , Full , Pbs , Split , Enter , MultiEnter }
| Current signal block state flags. More...

## Functions
static bool | IsTrainAndNotInDepot (const Vehicle *v)
| Check whether there is a train on rail, not in a depot.
static bool | CheckAddToTodoSet (TileIndex t1, DiagDirection d1, TileIndex t2, DiagDirection d2)
| Perform some operations before adding data into Todo set The new and reverse direction is removed from _globset, because we are sure it doesn't need to be checked again Also, remove reverse direction from _tbdset This is the 'core' part so the graph searching won't enter any tile twice.
static bool | MaybeAddToTodoSet (TileIndex t1, DiagDirection d1, TileIndex t2, DiagDirection d2)
| Perform some operations before adding data into Todo set The new and reverse direction is removed from Global set, because we are sure it doesn't need to be checked again Also, remove reverse direction from Todo set This is the 'core' part so the graph searching won't enter any tile twice.
static SigFlags | ExploreSegment (Owner owner)
| Search signal block.
static void | UpdateSignalsAroundSegment (SigFlags flags)
| Update signals around segment in _tbuset.
static void | ResetSets ()
| Reset all sets after one set overflowed.
static SigSegState | UpdateSignalsInBuffer (Owner owner)
| Updates blocks in _globset buffer.
void | UpdateSignalsInBuffer ()
| Update signals in buffer Called from 'outside'.
void | AddTrackToSignalBuffer (TileIndex tile, Track track, Owner owner)
| Add track to signal update buffer.
void | AddSideToSignalBuffer (TileIndex tile, DiagDirection side, Owner owner)
| Add side of tile to signal update buffer.
SigSegState | UpdateSignalsOnSegment (TileIndex tile, DiagDirection side, Owner owner)
| Update signals, starting at one side of a tile Will check tile next to this at opposite side too.
void | SetSignalsOnBothDir (TileIndex tile, Track track, Owner owner)
| Update signals at segments that are at both ends of given (existent or non-existent) track.

## Variables
static const uint | SIG_TBU_SIZE = 64
| these are the maximums used for updating signal blocks
static const uint | SIG_TBD_SIZE = 256
| number of intersections - open nodes in current block
static const uint | SIG_GLOB_SIZE = 128
| number of open blocks (block can be opened more times until detected)
static const uint | SIG_GLOB_UPDATE = 64
| how many items need to be in _globset to force update
static const TrackBits | _enterdir_to_trackbits [DIAGDIR_END]
| incidating trackbits with given enterdir
static const TrackdirBits | _enterdir_to_trackdirbits [DIAGDIR_END]
| incidating trackdirbits with given enterdir
static SmallSet< Trackdir, SIG_TBU_SIZE > | _tbuset ("_tbuset")
| set of signals that will be updated
static SmallSet< DiagDirection, SIG_TBD_SIZE > | _tbdset ("_tbdset")
| set of open nodes in current signal block
static SmallSet< DiagDirection, SIG_GLOB_SIZE > | _globset ("_globset")
| set of places to be updated in following runs
static Owner | _last_owner = INVALID_OWNER
| last owner whose track was put into _globset

## Detailed Description

functions related to rail signals updating

Definition in file signal.cpp.

## Typedef Documentation

## ◆ SigFlags

using SigFlags = EnumBitSet<SigFlag, uint16_t>

Definition at line 258 of file signal.cpp.

## Enumeration Type Documentation

## ◆ SigFlag

enum class SigFlag : uint8_t
| strong

Current signal block state flags.

Enumerator
Train | train found in segment
Exit | exitsignal found
MultiExit | two or more exits found
Green | green exitsignal found
MultiGreen | two or more green exits found
Full | some of buffers was full, do not continue
Pbs | pbs signal found
Split | track merge/split found
Enter | signal entering the block found
MultiEnter | two or more signals entering the block found

Definition at line 246 of file signal.cpp.

## Function Documentation

## ◆ AddSideToSignalBuffer()

void AddSideToSignalBuffer | ( | TileIndex | tile,
|  | DiagDirection | side,
|  | Owner | owner
| ) |  |

Add side of tile to signal update buffer.

Parameters

  tile | tile where we start
  side | side of tile
  owner | owner whose signals we will update

Definition at line 621 of file signal.cpp.

References _globset, _last_owner, INVALID_OWNER, SIG_GLOB_UPDATE, and UpdateSignalsInBuffer().

Referenced by CmdBuildBridge(), CmdBuildTrainDepot(), CmdBuildTunnel(), DoClearBridge(), DoClearTunnel(), and FreeTrainTrackReservation().

## ◆ AddTrackToSignalBuffer()

void AddTrackToSignalBuffer | ( | TileIndex | tile,
|  | Track | track,
|  | Owner | owner
| ) |  |

Add track to signal update buffer.

Parameters

  tile | tile where we start
  track | track at which ends we will update signals
  owner | owner whose signals we will update

Definition at line 589 of file signal.cpp.

References _globset, _last_owner, DIAGDIR_NE, DIAGDIR_NW, DIAGDIR_SE, DIAGDIR_SW, INVALID_OWNER, SIG_GLOB_UPDATE, and UpdateSignalsInBuffer().

Referenced by ChangeOwnershipOfCompanyItems(), CmdBuildRailStation(), CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), RemoveFromRailBaseStation(), and SetSignalsOnBothDir().

## ◆ CheckAddToTodoSet()

static bool CheckAddToTodoSet | ( | TileIndex | t1,
|  | DiagDirection | d1,
|  | TileIndex | t2,
|  | DiagDirection | d2
| ) |  |
| inlinestatic

Perform some operations before adding data into Todo set The new and reverse direction is removed from _globset, because we are sure it doesn't need to be checked again Also, remove reverse direction from _tbdset This is the 'core' part so the graph searching won't enter any tile twice.

Parameters

  t1 | tile we are entering
  d1 | direction (tile side) we are entering
  t2 | tile we are leaving
  d2 | direction (tile side) we are leaving

Returns
  false iff reverse direction was in Todo set

Definition at line 213 of file signal.cpp.

References _globset, and _tbdset.

Referenced by MaybeAddToTodoSet().

## ◆ ExploreSegment()

static SigFlags ExploreSegment | ( | Owner | owner | ) |
| static

Search signal block.

Parameters

  owner | owner whose signals we are updating

Returns
  SigFlags

Definition at line 266 of file signal.cpp.

References _enterdir_to_trackbits, _enterdir_to_trackdirbits, _tbdset, _tbuset, DIAGDIR_BEGIN, DIAGDIR_END, DiagDirToAxis(), EnsureNoTrainOnTrackBits(), Enter, Exit, CommandCost::Failed(), FindFirstBit(), Full, GetCrossingRoadAxis(), GetOtherTunnelBridgeEnd(), GetRailDepotDirection(), GetRailStationAxis(), GetSignalStateByTrackdir(), GetTileOwner(), GetTileType(), GetTrackBits(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), Green, HasAtMostOneBit(), HasSignalOnTrack(), HasSignalOnTrackdir(), HasSignals(), HasStationRail(), HasVehicleOnTile(), INVALID_DIAGDIR, INVALID_TILE, IsLevelCrossing(), IsOnewaySignal(), IsRailDepot(), IsStationTileBlocked(), IsTrainAndNotInDepot(), IsValidDiagDirection(), MaybeAddToTodoSet(), MP_RAILWAY, MP_ROAD, MP_STATION, MP_TUNNELBRIDGE, MultiEnter, MultiExit, MultiGreen, Pbs, ReverseDiagDir(), ReverseTrackdir(), SIGNAL_STATE_GREEN, Split, TileOffsByDiagDir(), TRACK_BIT_HORZ, TRACK_BIT_NONE, TRACK_BIT_VERT, TrackBitsToTrack(), Train, and TRANSPORT_RAIL.

Referenced by UpdateSignalsInBuffer().

## ◆ IsTrainAndNotInDepot()

static bool IsTrainAndNotInDepot | ( | const Vehicle * | v | ) |
| static

Check whether there is a train on rail, not in a depot.

Definition at line 194 of file signal.cpp.

References SpecializedVehicle< T, Type >::From(), TRACK_BIT_DEPOT, BaseVehicle::type, and VEH_TRAIN.

Referenced by ExploreSegment().

## ◆ MaybeAddToTodoSet()

static bool MaybeAddToTodoSet | ( | TileIndex | t1,
|  | DiagDirection | d1,
|  | TileIndex | t2,
|  | DiagDirection | d2
| ) |  |
| inlinestatic

Perform some operations before adding data into Todo set The new and reverse direction is removed from Global set, because we are sure it doesn't need to be checked again Also, remove reverse direction from Todo set This is the 'core' part so the graph searching won't enter any tile twice.

Parameters

  t1 | tile we are entering
  d1 | direction (tile side) we are entering
  t2 | tile we are leaving
  d2 | direction (tile side) we are leaving

Returns
  false iff the Todo buffer would be overrun

Definition at line 237 of file signal.cpp.

References _tbdset, and CheckAddToTodoSet().

Referenced by ExploreSegment().

## ◆ ResetSets()

static void ResetSets | ( |  | ) |
| inlinestatic

Reset all sets after one set overflowed.

Definition at line 464 of file signal.cpp.

References _globset, _tbdset, and _tbuset.

Referenced by UpdateSignalsInBuffer().

## ◆ SetSignalsOnBothDir()

void SetSignalsOnBothDir | ( | TileIndex | tile,
|  | Track | track,
|  | Owner | owner
| ) |  |

Update signals at segments that are at both ends of given (existent or non-existent) track.

See also
  UpdateSignalsInBuffer()

Parameters

  tile | tile where we start
  track | track at which ends we will update signals
  owner | owner whose signals we will update

Definition at line 665 of file signal.cpp.

References _globset, AddTrackToSignalBuffer(), and UpdateSignalsInBuffer().

Referenced by DeleteLastWagon().

## ◆ UpdateSignalsAroundSegment()

static void UpdateSignalsAroundSegment | ( | SigFlags | flags | ) |
| static

Update signals around segment in _tbuset.

Parameters

  flags | info about segment

Definition at line 410 of file signal.cpp.

References _globset, _tbuset, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Any(), Exit, GetRailReservationTrackBits(), GetSignalStateByTrackdir(), Green, HasSignalOnTrackdir(), INVALID_TILE, INVALID_TRACKDIR, MarkTileDirtyByTile(), MultiExit, MultiGreen, ReverseTrackdir(), SetSignalStateByTrackdir(), SIGNAL_STATE_GREEN, SIGNAL_STATE_RED, SIGTYPE_COMBO, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), TRACK_BIT_NONE, TrackdirToExitdir(), TrackdirToTrack(), TrackToTrackBits(), and Train.

Referenced by UpdateSignalsInBuffer().

## ◆ UpdateSignalsInBuffer() [1/2]

void UpdateSignalsInBuffer | ( |  | ) |

Update signals in buffer Called from 'outside'.

Definition at line 573 of file signal.cpp.

References _globset, _last_owner, INVALID_OWNER, and UpdateSignalsInBuffer().

Referenced by AddSideToSignalBuffer(), AddTrackToSignalBuffer(), ChangeOwnershipOfCompanyItems(), DoFloodTile(), FreeTrainTrackReservation(), CommandHelperBase::InternalExecuteProcessResult(), SetSignalsOnBothDir(), UpdateSignalsInBuffer(), and UpdateSignalsOnSegment().

## ◆ UpdateSignalsInBuffer() [2/2]

static SigSegState UpdateSignalsInBuffer | ( | Owner | owner | ) |
| static

Updates blocks in _globset buffer.

Parameters

  owner | company whose signals we are updating

Returns
  state of the first block from _globset

Precondition
  Company::IsValidID(owner)

Definition at line 479 of file signal.cpp.

References _enterdir_to_trackbits, _globset, _tbdset, _tbuset, Exit, ExploreSegment(), Full, GetOtherTunnelBridgeEnd(), GetRailDepotDirection(), GetTileTrackStatus(), GetTileType(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), Green, INVALID_DIAGDIR, INVALID_TILE, IsRailDepot(), Pool< Titem, Tindex, Tgrowth_step, Tpool_type, Tcache >::PoolItem<&_company_pool >::IsValidID(), MP_RAILWAY, MP_ROAD, MP_STATION, MP_TUNNELBRIDGE, Pbs, ResetSets(), ReverseDiagDir(), SIGSEG_FREE, SIGSEG_FULL, SIGSEG_PBS, BaseBitSet< Timpl, Tvalue_type, Tstorage, Tmask >::Test(), TileOffsByDiagDir(), TRACK_BIT_NONE, TrackStatusToTrackBits(), Train, TRANSPORT_RAIL, and UpdateSignalsAroundSegment().

## ◆ UpdateSignalsOnSegment()

SigSegState UpdateSignalsOnSegment | ( | TileIndex | tile,
|  | DiagDirection | side,
|  | Owner | owner
| ) |  |

Update signals, starting at one side of a tile Will check tile next to this at opposite side too.

See also
  UpdateSignalsInBuffer()

Parameters

  tile | tile where we start
  side | side of tile
  owner | owner whose signals we will update

Returns
  the state of the signal segment

Definition at line 647 of file signal.cpp.

References _globset, and UpdateSignalsInBuffer().

Referenced by CheckTrainStayInDepot(), DeleteLastWagon(), ReverseTrainDirection(), TrainController(), and VehicleEnterDepot().

## Variable Documentation

## ◆ _enterdir_to_trackbits

const TrackBits _enterdir_to_trackbits[DIAGDIR_END]
| static

Initial value:
= {
TRACK_BIT_3WAY_NE,
TRACK_BIT_3WAY_SE,
TRACK_BIT_3WAY_SW,
TRACK_BIT_3WAY_NW
}
TRACK_BIT_3WAY_NW
@ TRACK_BIT_3WAY_NW
"Arrow" to the north-west
Definition track_type.h:49
TRACK_BIT_3WAY_NE
@ TRACK_BIT_3WAY_NE
"Arrow" to the north-east
Definition track_type.h:46
TRACK_BIT_3WAY_SW
@ TRACK_BIT_3WAY_SW
"Arrow" to the south-west
Definition track_type.h:48
TRACK_BIT_3WAY_SE
@ TRACK_BIT_3WAY_SE
"Arrow" to the south-east
Definition track_type.h:47

incidating trackbits with given enterdir

Definition at line 34 of file signal.cpp.

Referenced by ExploreSegment(), and UpdateSignalsInBuffer().

## ◆ _enterdir_to_trackdirbits

const TrackdirBits _enterdir_to_trackdirbits[DIAGDIR_END]
| static

Initial value:
= {
TRACKDIR_BIT_X_SW | TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_RIGHT_S,
TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_LOWER_W | TRACKDIR_BIT_RIGHT_N,
TRACKDIR_BIT_X_NE | TRACKDIR_BIT_LOWER_E | TRACKDIR_BIT_LEFT_N,
TRACKDIR_BIT_Y_SE | TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_LEFT_S
}
TRACKDIR_BIT_LEFT_S
@ TRACKDIR_BIT_LEFT_S
Track left, direction south.
Definition track_type.h:103
TRACKDIR_BIT_Y_NW
@ TRACKDIR_BIT_Y_NW
Track y-axis, direction north-west.
Definition track_type.h:107
TRACKDIR_BIT_UPPER_E
@ TRACKDIR_BIT_UPPER_E
Track upper, direction east.
Definition track_type.h:101
TRACKDIR_BIT_X_NE
@ TRACKDIR_BIT_X_NE
Track x-axis, direction north-east.
Definition track_type.h:99
TRACKDIR_BIT_LOWER_E
@ TRACKDIR_BIT_LOWER_E
Track lower, direction east.
Definition track_type.h:102
TRACKDIR_BIT_LEFT_N
@ TRACKDIR_BIT_LEFT_N
Track left, direction north.
Definition track_type.h:110
TRACKDIR_BIT_RIGHT_S
@ TRACKDIR_BIT_RIGHT_S
Track right, direction south.
Definition track_type.h:104
TRACKDIR_BIT_Y_SE
@ TRACKDIR_BIT_Y_SE
Track y-axis, direction south-east.
Definition track_type.h:100
TRACKDIR_BIT_RIGHT_N
@ TRACKDIR_BIT_RIGHT_N
Track right, direction north.
Definition track_type.h:111
TRACKDIR_BIT_UPPER_W
@ TRACKDIR_BIT_UPPER_W
Track upper, direction west.
Definition track_type.h:108
TRACKDIR_BIT_LOWER_W
@ TRACKDIR_BIT_LOWER_W
Track lower, direction west.
Definition track_type.h:109
TRACKDIR_BIT_X_SW
@ TRACKDIR_BIT_X_SW
Track x-axis, direction south-west.
Definition track_type.h:106

incidating trackdirbits with given enterdir

Definition at line 42 of file signal.cpp.

Referenced by ExploreSegment().

## ◆ _last_owner

Owner _last_owner = INVALID_OWNER
| static

last owner whose track was put into _globset

Definition at line 566 of file signal.cpp.

Referenced by AddSideToSignalBuffer(), AddTrackToSignalBuffer(), and UpdateSignalsInBuffer().

## ◆ SIG_GLOB_SIZE

const uint SIG_GLOB_SIZE = 128
| static

number of open blocks (block can be opened more times until detected)

Definition at line 28 of file signal.cpp.

## ◆ SIG_GLOB_UPDATE

const uint SIG_GLOB_UPDATE = 64
| static

how many items need to be in _globset to force update

Definition at line 29 of file signal.cpp.

Referenced by AddSideToSignalBuffer(), and AddTrackToSignalBuffer().

## ◆ SIG_TBD_SIZE

const uint SIG_TBD_SIZE = 256
| static

number of intersections - open nodes in current block

Definition at line 27 of file signal.cpp.

## ◆ SIG_TBU_SIZE

const uint SIG_TBU_SIZE = 64
| static

these are the maximums used for updating signal blocks

number of signals entering to block

Definition at line 26 of file signal.cpp.