Enable clicking on tunnel to show vehicles inside for road/tram tunnels

Use tile hash to enumerate vehicles
This commit is contained in:
Jonathan G Rennison
2024-06-27 22:14:35 +01:00
parent 4cb3b10f55
commit 0addb29e35

View File

@@ -2861,17 +2861,27 @@ static bool ClickTile_TunnelBridge(TileIndex tile)
/* Show vehicles found in tunnel. */ /* Show vehicles found in tunnel. */
if (IsTunnelTile(tile)) { if (IsTunnelTile(tile)) {
int count = 0;
TileIndex tile_end = GetOtherTunnelBridgeEnd(tile); TileIndex tile_end = GetOtherTunnelBridgeEnd(tile);
for (const Train *t : Train::IterateFrontOnly()) { VehicleType veh_type = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL ? VEH_TRAIN : VEH_ROAD;
if (!t->IsFrontEngine()) continue;
if (tile == t->tile || tile_end == t->tile) { std::vector<const Vehicle *> candidates;
ShowVehicleViewWindow(t); for (TileIndex test_tile : { tile, tile_end }) {
count++; for (const Vehicle *v = GetFirstVehicleOnPos(test_tile, veh_type); v != nullptr; v = v->HashTileNext()) {
if (v->IsFrontEngine()) candidates.push_back(v);
} }
if (count > 19) break; // no more than 20 windows open
} }
if (count > 0) return true; std::sort(candidates.begin(), candidates.end(), [&](const Vehicle *a, const Vehicle *b) {
return a->index < b->index;
});
/* No more than 20 windows open */
if (candidates.size() > 20) candidates.resize(20);
for (const Vehicle *v : candidates) {
ShowVehicleViewWindow(v);
}
if (!candidates.empty()) return true;
} }
return false; return false;
} }