2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2007-04-03 21:51:40 +00:00
|
|
|
/** @file bridge_map.cpp Map accessor functions for bridges. */
|
2007-02-23 11:50:43 +00:00
|
|
|
|
2006-03-15 16:44:50 +00:00
|
|
|
#include "stdafx.h"
|
2007-10-20 16:50:48 +00:00
|
|
|
#include "landscape.h"
|
2007-12-16 15:38:51 +00:00
|
|
|
#include "tunnelbridge_map.h"
|
2016-09-18 18:48:52 +00:00
|
|
|
#include "bridge_signal_map.h"
|
|
|
|
#include "debug.h"
|
2022-06-18 22:37:35 +00:00
|
|
|
#include "newgrf_newsignals.h"
|
2006-03-15 16:44:50 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2006-03-15 16:44:50 +00:00
|
|
|
|
2009-11-09 10:40:33 +00:00
|
|
|
/**
|
|
|
|
* Finds the end of a bridge in the specified direction starting at a middle tile
|
2009-11-13 17:40:21 +00:00
|
|
|
* @param tile the bridge tile to find the bridge ramp for
|
|
|
|
* @param dir the direction to search in
|
2009-11-09 10:40:33 +00:00
|
|
|
*/
|
|
|
|
static TileIndex GetBridgeEnd(TileIndex tile, DiagDirection dir)
|
2006-03-16 05:28:15 +00:00
|
|
|
{
|
2006-09-05 23:21:41 +00:00
|
|
|
TileIndexDiff delta = TileOffsByDiagDir(dir);
|
2006-03-16 05:28:15 +00:00
|
|
|
|
2006-12-27 12:38:02 +00:00
|
|
|
dir = ReverseDiagDir(dir);
|
2006-06-07 19:35:21 +00:00
|
|
|
do {
|
|
|
|
tile += delta;
|
2007-12-16 15:38:51 +00:00
|
|
|
} while (!IsBridgeTile(tile) || GetTunnelBridgeDirection(tile) != dir);
|
2006-03-16 05:28:15 +00:00
|
|
|
|
2006-06-07 19:35:21 +00:00
|
|
|
return tile;
|
2006-06-02 13:05:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Finds the northern end of a bridge starting at a middle tile
|
|
|
|
* @param t the bridge tile to find the bridge ramp for
|
|
|
|
*/
|
2006-12-27 12:38:02 +00:00
|
|
|
TileIndex GetNorthernBridgeEnd(TileIndex t)
|
|
|
|
{
|
|
|
|
return GetBridgeEnd(t, ReverseDiagDir(AxisToDiagDir(GetBridgeAxis(t))));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Finds the southern end of a bridge starting at a middle tile
|
|
|
|
* @param t the bridge tile to find the bridge ramp for
|
|
|
|
*/
|
2006-03-16 05:28:15 +00:00
|
|
|
TileIndex GetSouthernBridgeEnd(TileIndex t)
|
|
|
|
{
|
|
|
|
return GetBridgeEnd(t, AxisToDiagDir(GetBridgeAxis(t)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
|
|
|
* Starting at one bridge end finds the other bridge end
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param tile the bridge ramp tile to find the other bridge ramp for
|
2011-01-18 22:17:15 +00:00
|
|
|
*/
|
2006-03-15 16:44:50 +00:00
|
|
|
TileIndex GetOtherBridgeEnd(TileIndex tile)
|
|
|
|
{
|
2018-07-26 18:13:35 +00:00
|
|
|
assert_tile(IsBridgeTile(tile), tile);
|
2007-12-16 15:38:51 +00:00
|
|
|
return GetBridgeEnd(tile, GetTunnelBridgeDirection(tile));
|
2006-12-27 12:38:02 +00:00
|
|
|
}
|
2006-06-02 13:05:41 +00:00
|
|
|
|
2011-01-18 22:17:15 +00:00
|
|
|
/**
|
2011-11-04 10:20:24 +00:00
|
|
|
* Get the height ('z') of a bridge.
|
2018-10-28 02:17:36 +00:00
|
|
|
* @param t the bridge ramp tile to get the bridge height from
|
2011-11-04 10:20:24 +00:00
|
|
|
* @return the height of the bridge.
|
2011-01-18 22:17:15 +00:00
|
|
|
*/
|
2011-11-04 11:30:37 +00:00
|
|
|
int GetBridgeHeight(TileIndex t)
|
2006-12-27 12:38:02 +00:00
|
|
|
{
|
2024-03-08 11:12:41 +00:00
|
|
|
auto [tileh, h] = GetTileSlopeZ(t);
|
2007-12-16 15:38:51 +00:00
|
|
|
Foundation f = GetBridgeFoundation(tileh, DiagDirToAxis(GetTunnelBridgeDirection(t)));
|
2006-12-27 12:38:02 +00:00
|
|
|
|
2007-10-20 16:50:48 +00:00
|
|
|
/* one height level extra for the ramp */
|
2024-03-08 16:31:20 +00:00
|
|
|
return h + 1 + ApplyFoundationToSlope(f, tileh);
|
2006-03-15 16:44:50 +00:00
|
|
|
}
|
2016-09-18 18:48:52 +00:00
|
|
|
|
|
|
|
std::unordered_map<TileIndex, LongBridgeSignalStorage> _long_bridge_signal_sim_map;
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
SignalState GetBridgeEntranceSimulatedSignalStateExtended(TileIndex t, uint16_t signal)
|
2016-09-18 18:48:52 +00:00
|
|
|
{
|
|
|
|
const auto it = _long_bridge_signal_sim_map.find(t);
|
|
|
|
if (it != _long_bridge_signal_sim_map.end()) {
|
|
|
|
const LongBridgeSignalStorage &lbss = it->second;
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t offset = signal - BRIDGE_M2_SIGNAL_STATE_COUNT;
|
|
|
|
uint16_t slot = offset >> 6;
|
|
|
|
uint16_t bit = offset & 0x3F;
|
2016-09-18 18:48:52 +00:00
|
|
|
if (slot >= lbss.signal_red_bits.size()) return SIGNAL_STATE_GREEN;
|
|
|
|
return GB(lbss.signal_red_bits[slot], bit, 1) ? SIGNAL_STATE_RED : SIGNAL_STATE_GREEN;
|
|
|
|
} else {
|
|
|
|
return SIGNAL_STATE_GREEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
void SetBridgeEntranceSimulatedSignalStateExtended(TileIndex t, uint16_t signal, SignalState state)
|
2016-09-18 18:48:52 +00:00
|
|
|
{
|
|
|
|
LongBridgeSignalStorage &lbss = _long_bridge_signal_sim_map[t];
|
2024-01-07 16:41:53 +00:00
|
|
|
uint16_t offset = signal - BRIDGE_M2_SIGNAL_STATE_COUNT;
|
|
|
|
uint16_t slot = offset >> 6;
|
|
|
|
uint16_t bit = offset & 0x3F;
|
2016-09-18 18:48:52 +00:00
|
|
|
if (slot >= lbss.signal_red_bits.size()) lbss.signal_red_bits.resize(slot + 1);
|
2024-01-07 16:41:53 +00:00
|
|
|
SB(lbss.signal_red_bits[slot], bit, 1, (uint64_t) ((state == SIGNAL_STATE_RED) ? 1 : 0));
|
2018-07-02 17:29:10 +00:00
|
|
|
_m[t].m2 |= BRIDGE_M2_SIGNAL_STATE_EXT_FLAG;
|
2016-09-18 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 01:04:52 +00:00
|
|
|
bool SetAllBridgeEntranceSimulatedSignalsGreenExtended(TileIndex t)
|
2016-09-18 18:48:52 +00:00
|
|
|
{
|
2021-01-05 01:04:52 +00:00
|
|
|
bool changed = GB(_m[t].m2, BRIDGE_M2_SIGNAL_STATE_OFFSET, BRIDGE_M2_SIGNAL_STATE_COUNT) != 0;
|
2018-07-02 17:29:10 +00:00
|
|
|
SB(_m[t].m2, BRIDGE_M2_SIGNAL_STATE_OFFSET, BRIDGE_M2_SIGNAL_STATE_FIELD_SIZE, 0);
|
2016-09-18 18:48:52 +00:00
|
|
|
auto it = _long_bridge_signal_sim_map.find(t);
|
|
|
|
if (it != _long_bridge_signal_sim_map.end()) {
|
|
|
|
LongBridgeSignalStorage &lbss = it->second;
|
|
|
|
for (auto &it : lbss.signal_red_bits) {
|
2021-01-05 01:04:52 +00:00
|
|
|
if (it != 0) {
|
|
|
|
changed = true;
|
|
|
|
it = 0;
|
|
|
|
}
|
2016-09-18 18:48:52 +00:00
|
|
|
}
|
2018-07-02 17:29:10 +00:00
|
|
|
_m[t].m2 |= BRIDGE_M2_SIGNAL_STATE_EXT_FLAG;
|
2016-09-18 18:48:52 +00:00
|
|
|
}
|
2021-01-05 01:04:52 +00:00
|
|
|
return changed;
|
2016-09-18 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearBridgeEntranceSimulatedSignalsExtended(TileIndex t)
|
|
|
|
{
|
|
|
|
_long_bridge_signal_sim_map.erase(t);
|
2018-07-02 17:29:10 +00:00
|
|
|
SB(_m[t].m2, BRIDGE_M2_SIGNAL_STATE_OFFSET, BRIDGE_M2_SIGNAL_STATE_FIELD_SIZE, 0);
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
void ShiftBridgeEntranceSimulatedSignalsExtended(TileIndex t, int shift, uint64_t in)
|
2018-07-02 17:29:10 +00:00
|
|
|
{
|
|
|
|
if (shift > 0) {
|
|
|
|
/* shift into array */
|
|
|
|
LongBridgeSignalStorage *lbss = nullptr;
|
|
|
|
auto it = _long_bridge_signal_sim_map.find(t);
|
|
|
|
if (it != _long_bridge_signal_sim_map.end()) {
|
|
|
|
lbss = &(it->second);
|
|
|
|
} else if (in) {
|
|
|
|
lbss = &(_long_bridge_signal_sim_map[t]);
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const size_t orig_size = lbss->signal_red_bits.size();
|
|
|
|
size_t i = orig_size;
|
2024-01-07 16:41:53 +00:00
|
|
|
auto insert_bits = [&](uint64_t bits, size_t pos) {
|
2018-07-02 17:29:10 +00:00
|
|
|
if (bits) {
|
2018-07-29 22:37:46 +00:00
|
|
|
if (pos >= lbss->signal_red_bits.size()) lbss->signal_red_bits.resize(pos + 1);
|
2018-07-02 17:29:10 +00:00
|
|
|
lbss->signal_red_bits[pos] |= bits;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
while (i) {
|
|
|
|
i--;
|
2024-01-07 16:41:53 +00:00
|
|
|
uint64_t out = GB(lbss->signal_red_bits[i], 64 - shift, shift);
|
2018-07-02 17:29:10 +00:00
|
|
|
lbss->signal_red_bits[i] <<= shift;
|
|
|
|
insert_bits(out, i + 1);
|
|
|
|
}
|
|
|
|
insert_bits(in, 0);
|
|
|
|
} else if (shift < 0) {
|
|
|
|
/* not implemented yet */
|
|
|
|
NOT_REACHED();
|
|
|
|
}
|
2016-09-18 18:48:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClearBridgeSimulatedSignalMapping()
|
|
|
|
{
|
|
|
|
_long_bridge_signal_sim_map.clear();
|
|
|
|
}
|
2022-06-18 22:37:35 +00:00
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
btree::btree_set<uint32_t> _bridge_signal_style_map;
|
2022-06-18 22:37:35 +00:00
|
|
|
static_assert(MAX_MAP_TILES_BITS + 4 <= 32);
|
|
|
|
static_assert(1 << 4 <= MAX_NEW_SIGNAL_STYLES + 1);
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
void SetBridgeSignalStyle(TileIndex t, uint8_t style)
|
2022-06-18 22:37:35 +00:00
|
|
|
{
|
|
|
|
if (style == 0) {
|
|
|
|
/* No style allocated before */
|
|
|
|
if (!HasBit(_m[t].m3, 7)) return;
|
|
|
|
|
|
|
|
auto iter = _bridge_signal_style_map.lower_bound(t << 4);
|
|
|
|
if (iter != _bridge_signal_style_map.end() && *iter >> 4 == t) _bridge_signal_style_map.erase(iter);
|
|
|
|
ClrBit(_m[t].m3, 7);
|
|
|
|
} else {
|
|
|
|
auto iter = _bridge_signal_style_map.lower_bound(t << 4);
|
|
|
|
if (iter != _bridge_signal_style_map.end() && *iter >> 4 == t) iter = _bridge_signal_style_map.erase(iter);
|
|
|
|
_bridge_signal_style_map.insert(iter, (t << 4) | style);
|
|
|
|
SetBit(_m[t].m3, 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 16:41:53 +00:00
|
|
|
uint8_t GetBridgeSignalStyleExtended(TileIndex t)
|
2022-06-18 22:37:35 +00:00
|
|
|
{
|
|
|
|
auto iter = _bridge_signal_style_map.lower_bound(t << 4);
|
|
|
|
if (iter != _bridge_signal_style_map.end() && *iter >> 4 == t) return (*iter) & 0xF;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearBridgeSignalStyleMapping()
|
|
|
|
{
|
|
|
|
_bridge_signal_style_map.clear();
|
|
|
|
}
|