Typedefs | Functions | Variables

# trafficlight.cpp File Reference

Handling of trafficlights. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "landscape.h"`
`#include "sprite.h"`
`#include "viewport_func.h"`
`#include "road_map.h"`
`#include "command_func.h"`
`#include "cheat_func.h"`
`#include "animated_tile_func.h"`
`#include "economy_func.h"`
`#include "road_cmd.h"`
`#include "company_func.h"`
`#include "company_base.h"`
`#include "settings_type.h"`
`#include "trafficlight.h"`
`#include "trafficlight_type.h"`
`#include "date_func.h"`
`#include "table/sprites.h"`
`#include "table/strings.h"`
`#include <set>`

Go to the source code of this file.

## Typedefs
typedef std::set< TileIndex > | TLC
## Functions
TLC * | GetTrafficLightConsist (TileIndex tile, bool checkroadworks)
| Gets the traffic light consist (a set of adjacent tiles with traffic lights).
TileIndex | GetTLCLowestTileIndexOrRoadWorks (TileIndex tile)
| Gets the lowest TileIndex of the traffic light consist or 0 if roadworks are found in the consist.
TrafficLightState | GetTLState (TileIndex tile)
| Returns the state of the trafficlights on a tile.
TrackdirBits | GetIntraTLCAllowedDirections (TileIndex tile)
| Which directions in tile are allowed to be taken due to adjacent traffic lights (traffic light consist).
TrackdirBits | GetTrafficLightDisallowedDirections (TileIndex tile)
| Get a bitmask of the directions forbidden to drive on due to traffic light(s).
bool | CheckTLCSize (TileIndex tile)
| Checks if the size of a traffic light consist is within the allowed range.
CommandCost | CmdBuildTrafficLights (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Build traffic lights on a crossing.
CommandCost | CmdRemoveTrafficLights (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Removes traffic lights from a tile.
void | ClearAllTrafficLights ()
| Clear all traffic lights from the map.
void | DrawTrafficLights (TileInfo *ti)
| Draws traffic lights on a tile.
## Variables
static const TrackdirBits | _tls_to_trackdir [7]
| Which directions are disallowed due to the TLState (red lights..).

## Detailed Description

Handling of trafficlights.

Definition in file trafficlight.cpp.

## Function Documentation

bool CheckTLCSize | ( | TileIndex | tile | ) |

Checks if the size of a traffic light consist is within the allowed range.

Parameters:

  | tile | Tile to check (can also be a new tile to be added to the TLC).

Returns:
  result True if the TLC size is within the allowed range, else false.

Definition at line 172 of file trafficlight.cpp.

References _settings_game, GameSettings::construction, GetTrafficLightConsist(), and ConstructionSettings::max_tlc_size.

Referenced by CmdBuildTrafficLights().

CommandCost CmdBuildTrafficLights | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Build traffic lights on a crossing.

Parameters:

  | tile | Tile where to place the traffic lights.
  | flags | Operation to perform.
  | p1 | Unused.
  | p2 | Unused.

Returns:
  CommandCost Cost or error.

Definition at line 189 of file trafficlight.cpp.

References _current_company, _settings_game, AddAnimatedTile(), ConstructionSettings::allow_building_tls_in_towns, CheckTLCSize(), CMD_ERROR, GameSettings::construction, CountBits(), DC_EXEC, EXPENSES_CONSTRUCTION, GetAllRoadBits(), GetRoadTileType(), GetTileOwner(), HasTrafficLights(), IsTileOwner(), IsTileType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), MarkTileDirtyByTile(), MP_ROAD, OWNER_NONE, OWNER_TOWN, return_cmd_error, ROAD_TILE_NORMAL, and ConstructionSettings::traffic_lights.

CommandCost CmdRemoveTrafficLights | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Removes traffic lights from a tile.

Parameters:

  | tile | Tile where to remove the traffic lights.
  | flags | Operation to perform.
  | p1 | Unused.
  | p2 | Unused.

Returns:
  CommandCost Cost or error.

Definition at line 230 of file trafficlight.cpp.

References _cheats, _current_company, _settings_game, ConstructionSettings::allow_building_tls_in_towns, CMD_ERROR, GameSettings::construction, DC_EXEC, DeleteAnimatedTile(), EXPENSES_CONSTRUCTION, GetRoadTileType(), GetTileOwner(), HasTrafficLights(), IsTileOwner(), IsTileType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::IsValidID(), Cheats::magic_bulldozer, MarkTileDirtyByTile(), MP_ROAD, OWNER_NONE, OWNER_TOWN, return_cmd_error, ROAD_TILE_NORMAL, and Cheat::value.

void DrawTrafficLights | ( | TileInfo * | ti | ) |

Draws traffic lights on a tile.

Parameters:

  | ti | TileInfo of the tile to draw on.

Definition at line 270 of file trafficlight.cpp.

References _settings_game, _tl_offsets, _tls_to_sprites, DrawRoadDetail(), DRD_BOTH, DRD_NORTHBOUND, DRD_SOUTHBOUND, GetAllRoadBits(), GetDisallowedRoadDirections(), GetTLState(), HasTrafficLights(), IsNormalRoad(), IsTileType(), MP_ROAD, ROAD_NE, VehicleSettings::road_side, TileInfo::tile, TileDiffXY(), and GameSettings::vehicle.

Referenced by DrawRoadBits().

TrackdirBits GetIntraTLCAllowedDirections | ( | TileIndex | tile | ) |

Which directions in tile are allowed to be taken due to adjacent traffic lights (traffic light consist).

Parameters:

  | tile | Tile to search on.

Returns:
  trackdirbits Bitmask of allowed directions.

Definition at line 141 of file trafficlight.cpp.

References HasTrafficLights(), TileDiffXY(), TRACKDIR_BIT_LOWER_E, TRACKDIR_BIT_LOWER_W, TRACKDIR_BIT_UPPER_E, TRACKDIR_BIT_UPPER_W, TRACKDIR_BIT_X_NE, TRACKDIR_BIT_X_SW, TRACKDIR_BIT_Y_NW, and TRACKDIR_BIT_Y_SE.

Referenced by GetTrafficLightDisallowedDirections().

TileIndex GetTLCLowestTileIndexOrRoadWorks | ( | TileIndex | tile | ) |

Gets the lowest TileIndex of the traffic light consist or 0 if roadworks are found in the consist.

Parameters:

  | tile | Tile of the traffic light consist.

Returns:
  lowest TileIndex in the consist or 0 if roadworks were found.

Definition at line 82 of file trafficlight.cpp.

References GetTrafficLightConsist().

Referenced by GetTLState().

TrafficLightState GetTLState | ( | TileIndex | tile | ) |

Returns the state of the trafficlights on a tile.

Note:
  In the scenario editor trafficlights are disabled.

Parameters:

  | tile | This tile.

Precondition:
  The tile must have trafficlights.

Returns:
  Trafficlights state or disabled state.

< All lights are off in scenario editor.

< All lights are off when roadworks are in the consist.

< SW and NE are green, NW and SE are red.

< SW and NE are yellow, NW and SE are red.

< SW and NE are red, NW and SE are red-yellow.

< SW and NE are red, NW and SE are green.

< SW and NE are red, NW and SE are yellow.

< SW and NE are red-yellow, NW and SE are red.

Definition at line 98 of file trafficlight.cpp.

References _settings_game, _tick_counter, GameSettings::construction, GetTLCLowestTileIndexOrRoadWorks(), HasTrafficLights(), TileX(), TileY(), TLS_X_GREEN_Y_RED, TLS_X_RED_Y_GREEN, TLS_X_RED_Y_REDYELLOW, TLS_X_RED_Y_YELLOW, TLS_X_YELLOW_Y_RED, and ConstructionSettings::traffic_lights_green_phase.

Referenced by DrawTrafficLights(), and GetTrafficLightDisallowedDirections().

TLC* GetTrafficLightConsist | ( | TileIndex | tile,
|  | bool | checkroadworks |
| ) |  |  |

Gets the traffic light consist (a set of adjacent tiles with traffic lights).

If specified by the checkroadworks parameter, it returns 0 instead if road works are found within the consist.

Parameters:

  | tile | Tile of the traffic light consist.
  | checkroadworks | Should we check for roadworks in the consist (and return 0 if found).

Returns:
  visited The traffic light consist (TLC); if checkroadworks == true and roadworks were found, return 0 instead.

Definition at line 48 of file trafficlight.cpp.

References _settings_game, _tlc_distance, GameSettings::construction, HasRoadWorks(), HasTrafficLights(), ConstructionSettings::max_tlc_distance, and ToTileIndexDiff().

Referenced by CheckTLCSize(), and GetTLCLowestTileIndexOrRoadWorks().

TrackdirBits GetTrafficLightDisallowedDirections | ( | TileIndex | tile | ) |

Get a bitmask of the directions forbidden to drive on due to traffic light(s).

Parameters:

  | tile | Tile to check.

Returns:
  Bitmask of forbidden directions.

Definition at line 162 of file trafficlight.cpp.

References _tls_to_trackdir, GetIntraTLCAllowedDirections(), and GetTLState().

## Variable Documentation

const TrackdirBits _tls_to_trackdir[7]`[static]`

Initial value:

     {
      TRACKDIR_BIT_MASK,
      TRACKDIR_BIT_Y_NW | TRACKDIR_BIT_Y_SE |
        TRACKDIR_BIT_UPPER_E | TRACKDIR_BIT_LOWER_W |
        TRACKDIR_BIT_LEFT_S | TRACKDIR_BIT_RIGHT_N,
      TRACKDIR_BIT_MASK,
      TRACKDIR_BIT_MASK,
      TRACKDIR_BIT_X_SW | TRACKDIR_BIT_X_NE |
        TRACKDIR_BIT_UPPER_W | TRACKDIR_BIT_LOWER_E |
        TRACKDIR_BIT_LEFT_N | TRACKDIR_BIT_RIGHT_S,
      TRACKDIR_BIT_MASK,
      TRACKDIR_BIT_MASK,
    }

Which directions are disallowed due to the TLState (red lights..).

Definition at line 122 of file trafficlight.cpp.

Referenced by GetTrafficLightDisallowedDirections().