From 38619687652e2078ceacd18ce2f1f9467affd8d4 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sat, 20 Feb 2016 22:13:30 +0000 Subject: [PATCH] Enable setting semaphore/electric type of signals on bridges/tunnels. --- src/rail_cmd.cpp | 38 +++++++++++++++++++++++++++++--------- src/tunnelbridge_cmd.cpp | 4 ++-- src/tunnelbridge_map.h | 12 ++++++++++++ 3 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index e71d732d00..edaa0d56c6 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -1056,21 +1056,39 @@ CommandCost CmdBuildSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, if (IsTileType(tile, MP_TUNNELBRIDGE)) { TileIndex tile_exit = GetOtherTunnelBridgeEnd(tile); cost = CommandCost(); + bool flip_variant = false; if (!HasWormholeSignals(tile)) { // toggle signal zero costs. + if (convert_signal) return_cmd_error(STR_ERROR_THERE_ARE_NO_SIGNALS); if (p2 != 12) cost = CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_SIGNALS] * ((GetTunnelBridgeLength(tile, tile_exit) + 4) >> 2)); // minimal 1 + } else { + if (HasBit(p1, 17)) return CommandCost(); + if (ctrl_pressed && !convert_signal) return CommandCost(); + if ((p2 != 0 && (sigvar == SIG_SEMAPHORE) != IsTunnelBridgeSemaphore(tile)) || + (convert_signal && (ctrl_pressed || (sigvar == SIG_SEMAPHORE) != IsTunnelBridgeSemaphore(tile)))) { + flip_variant = true; + cost = CommandCost(EXPENSES_CONSTRUCTION, (_price[PR_BUILD_SIGNALS] + _price[PR_CLEAR_SIGNALS]) * + ((GetTunnelBridgeLength(tile, tile_exit) + 4) >> 2)); // minimal 1 + } } if (flags & DC_EXEC) { if (p2 == 0 && HasWormholeSignals(tile)) { // Toggle signal if already signals present. - if (IsTunnelBridgeEntrance(tile)) { - ClrBitTunnelBridgeSignal(tile); - ClrBitTunnelBridgeExit(tile_exit); - SetBitTunnelBridgeExit(tile); - SetBitTunnelBridgeSignal(tile_exit); + if (convert_signal) { + if (flip_variant) { + SetTunnelBridgeSemaphore(tile, !IsTunnelBridgeSemaphore(tile)); + SetTunnelBridgeSemaphore(tile_exit, !IsTunnelBridgeSemaphore(tile_exit)); + } } else { - ClrBitTunnelBridgeSignal(tile_exit); - ClrBitTunnelBridgeExit(tile); - SetBitTunnelBridgeExit(tile_exit); - SetBitTunnelBridgeSignal(tile); + if (IsTunnelBridgeEntrance(tile)) { + ClrBitTunnelBridgeSignal(tile); + ClrBitTunnelBridgeExit(tile_exit); + SetBitTunnelBridgeExit(tile); + SetBitTunnelBridgeSignal(tile_exit); + } else { + ClrBitTunnelBridgeSignal(tile_exit); + ClrBitTunnelBridgeExit(tile); + SetBitTunnelBridgeExit(tile_exit); + SetBitTunnelBridgeSignal(tile); + } } } else { /* Create one direction tunnel/bridge if required. */ @@ -1089,6 +1107,8 @@ CommandCost CmdBuildSingleSignal(TileIndex tile, DoCommandFlag flags, uint32 p1, SetBitTunnelBridgeExit(tile); } } + SetTunnelBridgeSemaphore(tile, sigvar == SIG_SEMAPHORE); + SetTunnelBridgeSemaphore(tile_exit, sigvar == SIG_SEMAPHORE); } MarkBridgeOrTunnelDirty(tile); AddSideToSignalBuffer(tile, INVALID_DIAGDIR, GetTileOwner(tile)); diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 0149b6aaba..1700a28757 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -1183,7 +1183,7 @@ static void DrawTunnelBridgeRampSignal(const TileInfo *ti) if (ti->tileh == SLOPE_FLAT && side != show_exit && dir == DIAGDIR_SW) z += 2; if (ti->tileh != SLOPE_FLAT && IsBridge(ti->tile)) z += 8; // sloped bridge head - SignalVariant variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC); + SignalVariant variant = IsTunnelBridgeSemaphore(ti->tile) ? SIG_SEMAPHORE : SIG_ELECTRIC; SpriteID sprite; if (variant == SIG_ELECTRIC) { @@ -1233,7 +1233,7 @@ static void DrawBrigeSignalOnMiddelPart(const TileInfo *ti, TileIndex bridge_sta uint y = TileY(ti->tile) * TILE_SIZE + SignalPositions[side][position].y; z += 5; - SignalVariant variant = (_cur_year < _settings_client.gui.semaphore_build_before ? SIG_SEMAPHORE : SIG_ELECTRIC); + SignalVariant variant = IsTunnelBridgeSemaphore(bridge_start_tile) ? SIG_SEMAPHORE : SIG_ELECTRIC; SpriteID sprite; diff --git a/src/tunnelbridge_map.h b/src/tunnelbridge_map.h index 43aa4f29cd..ccaface2fc 100644 --- a/src/tunnelbridge_map.h +++ b/src/tunnelbridge_map.h @@ -225,4 +225,16 @@ static inline void SetTunnelBridgeExitGreen(TileIndex t, bool green) SB(_me[t].m6, 0, 1, green ? 1 : 0); } +static inline bool IsTunnelBridgeSemaphore(TileIndex t) +{ + assert(IsTileType(t, MP_TUNNELBRIDGE) && HasWormholeSignals(t)); + return HasBit(_me[t].m6, 1); +} + +static inline void SetTunnelBridgeSemaphore(TileIndex t, bool is_semaphore) +{ + assert(IsTileType(t, MP_TUNNELBRIDGE) && HasWormholeSignals(t)); + SB(_me[t].m6, 1, 1, is_semaphore ? 1 : 0); +} + #endif /* TUNNELBRIDGE_MAP_H */