Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / bridge_map.cpp
blob7d155fed642ba6c2a000df76eebd032030bdc46f
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file bridge_map.cpp Map accessor functions for bridges. */
12 #include "stdafx.h"
13 #include "landscape.h"
14 #include "tunnelbridge_map.h"
15 #include "bridge_signal_map.h"
16 #include "debug.h"
18 #include "safeguards.h"
21 /**
22 * Finds the end of a bridge in the specified direction starting at a middle tile
23 * @param tile the bridge tile to find the bridge ramp for
24 * @param dir the direction to search in
26 static TileIndex GetBridgeEnd(TileIndex tile, DiagDirection dir)
28 TileIndexDiff delta = TileOffsByDiagDir(dir);
30 dir = ReverseDiagDir(dir);
31 do {
32 tile += delta;
33 } while (!IsBridgeTile(tile) || GetTunnelBridgeDirection(tile) != dir);
35 return tile;
39 /**
40 * Finds the northern end of a bridge starting at a middle tile
41 * @param t the bridge tile to find the bridge ramp for
43 TileIndex GetNorthernBridgeEnd(TileIndex t)
45 return GetBridgeEnd(t, ReverseDiagDir(AxisToDiagDir(GetBridgeAxis(t))));
49 /**
50 * Finds the southern end of a bridge starting at a middle tile
51 * @param t the bridge tile to find the bridge ramp for
53 TileIndex GetSouthernBridgeEnd(TileIndex t)
55 return GetBridgeEnd(t, AxisToDiagDir(GetBridgeAxis(t)));
59 /**
60 * Starting at one bridge end finds the other bridge end
61 * @param t the bridge ramp tile to find the other bridge ramp for
63 TileIndex GetOtherBridgeEnd(TileIndex tile)
65 assert(IsBridgeTile(tile));
66 return GetBridgeEnd(tile, GetTunnelBridgeDirection(tile));
69 /**
70 * Get the height ('z') of a bridge.
71 * @param tile the bridge ramp tile to get the bridge height from
72 * @return the height of the bridge.
74 int GetBridgeHeight(TileIndex t)
76 int h;
77 Slope tileh = GetTileSlope(t, &h);
78 Foundation f = GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(t)));
80 /* one height level extra for the ramp */
81 return h + 1 + ApplyFoundationToSlope(f, &tileh);
84 std::unordered_map<TileIndex, LongBridgeSignalStorage> _long_bridge_signal_sim_map;
86 SignalState GetBridgeEntranceSimulatedSignalStateExtended(TileIndex t, uint16 signal)
88 const auto it = _long_bridge_signal_sim_map.find(t);
89 if (it != _long_bridge_signal_sim_map.end()) {
90 const LongBridgeSignalStorage &lbss = it->second;
91 uint16 offset = signal - 15;
92 uint16 slot = offset >> 6;
93 uint16 bit = offset & 0x3F;
94 if (slot >= lbss.signal_red_bits.size()) return SIGNAL_STATE_GREEN;
95 return GB(lbss.signal_red_bits[slot], bit, 1) ? SIGNAL_STATE_RED : SIGNAL_STATE_GREEN;
96 } else {
97 return SIGNAL_STATE_GREEN;
101 void SetBridgeEntranceSimulatedSignalStateExtended(TileIndex t, uint16 signal, SignalState state)
103 LongBridgeSignalStorage &lbss = _long_bridge_signal_sim_map[t];
104 uint16 offset = signal - 15;
105 uint16 slot = offset >> 6;
106 uint16 bit = offset & 0x3F;
107 if (slot >= lbss.signal_red_bits.size()) lbss.signal_red_bits.resize(slot + 1);
108 SB(lbss.signal_red_bits[slot], bit, 1, (uint64) ((state == SIGNAL_STATE_RED) ? 1 : 0));
109 _m[t].m2 |= 0x8000;
112 void SetAllBridgeEntranceSimulatedSignalsGreenExtended(TileIndex t)
114 auto it = _long_bridge_signal_sim_map.find(t);
115 if (it != _long_bridge_signal_sim_map.end()) {
116 LongBridgeSignalStorage &lbss = it->second;
117 for (auto &it : lbss.signal_red_bits) {
118 it = 0;
120 _m[t].m2 = 0x8000;
121 } else {
122 _m[t].m2 = 0;
126 void ClearBridgeEntranceSimulatedSignalsExtended(TileIndex t)
128 _long_bridge_signal_sim_map.erase(t);
129 _m[t].m2 = 0;
132 void ClearBridgeSimulatedSignalMapping()
134 _long_bridge_signal_sim_map.clear();