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 "gui.h"`
`#include "table/strings.h"`
`#include "programmable_signals.h"`
`#include "infrastructure_func.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...
struct | SigInfo
## Typedefs
typedef SmallVector < SignalReference, 4 > | SignalDependencyList
| List of signals dependent upon this one.
typedef std::map < SignalReference, SignalDependencyList > | SignalDependencyMap
| Map of dependencies.
## Enumerations
enum | SigFlags { SF_NONE = 0, SF_TRAIN = 1 << 0, SF_FULL = 1 << 1, SF_PBS = 1 << 2 }
| Current signal block state flags. More...
## Functions
static void | MarkDependencidesForUpdate (SignalReference sig)
| assert_compile (SIG_GLOB_UPDATE<=SIG_GLOB_SIZE)
static Vehicle * | TrainOnTileEnum (Vehicle *v, void *)
| 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 seaching won't enter any tile twice.
static Vehicle * | TrainInWormholeTileEnum (Vehicle *v, void *data)
| Check whether there is a train only on ramp.
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 seaching won't enter any tile twice.
static SigInfo | ExploreSegment (Owner owner)
| Search signal block.
static void | UpdateSignalsAroundSegment (SigInfo info)
| 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.
void | AddSignalDependency (SignalReference on, SignalReference dep)
| Adds a signal dependency The signal identified by `dep` will be marked as dependend upon the signal identified by `on`.
void | RemoveSignalDependency (SignalReference on, SignalReference dep)
| Removes a signal dependency.
void | FreeSignalDependencies ()
| Frees signal dependencies (for newgame/load).
void | CheckRemoveSignalsFromTile (TileIndex tile)
| Checks for any data attached to any signals, and removes it.
static void | NotifyRemovingDependentSignal (SignalReference on, SignalReference by)
void | CheckRemoveSignal (TileIndex tile, Track track)
| Checks for, and removes, any extra signal data.
## Variables
static SignalDependencyMap | _signal_dependencies
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 uint | _num_signals_evaluated
| Number of programmable signals evaluated.
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

typedef std::map<SignalReference, SignalDependencyList> SignalDependencyMap

Map of dependencies.

The key identifies the signal, the value is a list of all of the signals which depend upon that signal.

Definition at line 32 of file signal.cpp.

## Enumeration Type Documentation

enum SigFlags

Current signal block state flags.

Enumerator:

  SF_TRAIN | train found in segment
  SF_FULL | some of buffers was full, do not continue
  SF_PBS | pbs signal found

Definition at line 269 of file signal.cpp.

## Function Documentation

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 697 of file signal.cpp.

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

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

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 664 of file signal.cpp.

References _globset, _last_owner, DIAGDIR_NE, IsOneSignalBlock(), SIG_GLOB_UPDATE, and UpdateSignalsInBuffer().

Referenced by CmdBuildRailStation(), CmdBuildSingleRail(), CmdBuildSingleSignal(), CmdInsertSignalInstruction(), CmdModifySignalInstruction(), CmdRemoveSignalInstruction(), CmdRemoveSingleRail(), CmdRemoveSingleSignal(), CmdSetSignalSpeedLimit(), DeleteVisibleTrain(), RemoveFromRailBaseStation(), RemoveRailStation(), SetSignalsOnBothDir(), and UpdateAllBlockSignals().

static bool CheckAddToTodoSet | ( | TileIndex | t1,
|  | DiagDirection | d1,
|  | TileIndex | t2,
|  | DiagDirection | d2 |
| ) |  |  | `[inline, static]`

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 seaching 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 226 of file signal.cpp.

References _globset, and _tbdset.

Referenced by MaybeAddToTodoSet().

void CheckRemoveSignal | ( | TileIndex | tile,
|  | Track | track |
| ) |  |  |

Checks for, and removes, any extra signal data.

Call when removing a piece of track which is potentially signalled, in order to free any extra data that may be associated with said track.

Definition at line 811 of file signal.cpp.

References SmallVector< T, S >::Begin(), SmallVector< T, S >::End(), GetTileOwner(), HasSignalOnTrack(), IsProgrammableSignal(), and IsSpeedSignal().

Referenced by CheckRemoveSignalsFromTile(), CmdRemoveSingleRail(), and CmdRemoveSingleSignal().

void CheckRemoveSignalsFromTile | ( | TileIndex | tile | ) |

Checks for any data attached to any signals, and removes it.

Call when performing an action which may potentially remove signals from a tile, in order to avoid leaking data.

Definition at line 792 of file signal.cpp.

References CheckRemoveSignal(), GetTrackBits(), HasSignalOnTrack(), HasSignals(), INVALID_TRACK, and RemoveFirstTrack().

static SigInfo ExploreSegment | ( | Owner | owner | ) | `[static]`

Search signal block.

Parameters:

  | owner | owner whose signals we are updating

Returns:
  SigFlags

Definition at line 296 of file signal.cpp.

References _enterdir_to_trackbits, _enterdir_to_trackdirbits, _tbdset, _tbuset, DIAGDIR_BEGIN, DiagDirToAxis(), EnsureNoTrainOnTrackBits(), CommandCost::Failed(), FindFirstBit(), GetCrossingRoadAxis(), GetOtherTunnelBridgeEnd(), GetRailDepotDirection(), GetRailStationAxis(), GetSignalStateByTrackdir(), GetTileOwner(), GetTileType(), GetTrackBits(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), HasSignalOnTrack(), HasSignalOnTrackdir(), HasSignals(), HasStationRail(), HasVehicleOnPos(), HasWormholeSignals(), INVALID_DIAGDIR, IsLevelCrossing(), IsOneSignalBlock(), IsOnewaySignal(), IsPbsSignal(), IsRailDepot(), IsSpeedSignal(), IsStationTileBlocked(), IsTunnelBridgeExit(), MaybeAddToTodoSet(), MP_RAILWAY, MP_ROAD, MP_STATION, MP_TUNNELBRIDGE, ReverseDiagDir(), ReverseTrackdir(), SF_PBS, SF_TRAIN, TileOffsByDiagDir(), TRACK_BIT_HORZ, TRACK_BIT_NONE, TRACK_BIT_VERT, TrackBitsToTrack(), TrainInWormholeTileEnum(), TrainOnTileEnum(), and TRANSPORT_RAIL.

Referenced by UpdateSignalsInBuffer().

static bool MaybeAddToTodoSet | ( | TileIndex | t1,
|  | DiagDirection | d1,
|  | TileIndex | t2,
|  | DiagDirection | d2 |
| ) |  |  | `[inline, static]`

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 seaching 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 260 of file signal.cpp.

References _tbdset, and CheckAddToTodoSet().

Referenced by ExploreSegment().

void RemoveSignalDependency | ( | SignalReference | on,
|  | SignalReference | dep |
| ) |  |  |

Removes a signal dependency.

Arguments same as AddSignalDependency

Definition at line 757 of file signal.cpp.

References SmallVector< T, S >::End(), SmallVector< T, S >::Erase(), SmallVector< T, S >::Find(), and SmallVector< T, S >::Length().

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 742 of file signal.cpp.

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

Referenced by DeleteLastWagon().

static Vehicle* TrainInWormholeTileEnum | ( | Vehicle * | v,
|  | void * | data |
| ) |  |  | `[static]`

Check whether there is a train only on ramp.

Definition at line 239 of file signal.cpp.

References Vehicle::Next(), Vehicle::Previous(), TileVirtXY(), BaseVehicle::type, VEH_TRAIN, Vehicle::x_pos, and Vehicle::y_pos.

Referenced by ExploreSegment().

static void UpdateSignalsAroundSegment | ( | SigInfo | info | ) | `[static]`

Update signals around segment in _tbuset.

Parameters:

  | flags | info about segment

Definition at line 462 of file signal.cpp.

References _globset, _num_signals_evaluated, _settings_game, _tbuset, GameSettings::construction, GetSignalStateByTrackdir(), HasSignalOnTrackdir(), IsComboSignal(), IsEntrySignal(), IsExitSignal(), MarkTileDirtyByTile(), ConstructionSettings::maximum_signal_evaluations, ReverseTrackdir(), SetSignalStateByTrackdir(), SF_TRAIN, SIGNAL_STATE_GREEN, SIGTYPE_PROG, SIGTYPE_SPEED, TrackdirToExitdir(), and TrackdirToTrack().

Referenced by UpdateSignalsInBuffer().

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 549 of file signal.cpp.

References _enterdir_to_trackbits, _globset, _num_signals_evaluated, _settings_game, _tbdset, _tbuset, GameSettings::construction, ExploreSegment(), GetOtherTunnelBridgeEnd(), GetRailDepotDirection(), GetTileTrackStatus(), GetTileType(), GetTunnelBridgeDirection(), GetTunnelBridgeTransportType(), HasWormholeSignals(), INVALID_DIAGDIR, IsRailDepot(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), ConstructionSettings::maximum_signal_evaluations, MP_RAILWAY, MP_ROAD, MP_STATION, MP_TUNNELBRIDGE, ResetSets(), ReverseDiagDir(), SF_FULL, SF_PBS, SF_TRAIN, ShowErrorMessage(), TileOffsByDiagDir(), TRACK_BIT_NONE, TrackStatusToTrackBits(), TRANSPORT_RAIL, UpdateSignalsAroundSegment(), and WL_INFO.

Referenced by AddSideToSignalBuffer(), AddTrackToSignalBuffer(), ChangeOwnershipOfCompanyItems(), CmdInsertSignalInstruction(), CmdModifySignalInstruction(), CmdRemoveSignalInstruction(), CmdSetSignalSpeedLimit(), DeleteVisibleTrain(), DoCommandPInternal(), DoFloodTile(), SetSignalsOnBothDir(), UpdateAllBlockSignals(), UpdateSignalsInBuffer(), and 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 724 of file signal.cpp.

References _globset, and UpdateSignalsInBuffer().

Referenced by CheckTrainStayInWormHole(), DeleteLastWagon(), and VehicleEnterDepot().

## Variable Documentation

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
    }

incidating trackbits with given enterdir

Definition at line 46 of file signal.cpp.

Referenced by ExploreSegment(), and UpdateSignalsInBuffer().

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
    }

incidating trackdirbits with given enterdir

Definition at line 54 of file signal.cpp.

Referenced by ExploreSegment().

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 38 of file signal.cpp.