From 0addb29e35ee3499ea58d724be14abc3e008820c Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Thu, 27 Jun 2024 22:14:35 +0100 Subject: [PATCH] Enable clicking on tunnel to show vehicles inside for road/tram tunnels Use tile hash to enumerate vehicles --- src/tunnelbridge_cmd.cpp | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/tunnelbridge_cmd.cpp b/src/tunnelbridge_cmd.cpp index 9bbd18f1e4..847496ebe1 100644 --- a/src/tunnelbridge_cmd.cpp +++ b/src/tunnelbridge_cmd.cpp @@ -2861,17 +2861,27 @@ static bool ClickTile_TunnelBridge(TileIndex tile) /* Show vehicles found in tunnel. */ if (IsTunnelTile(tile)) { - int count = 0; TileIndex tile_end = GetOtherTunnelBridgeEnd(tile); - for (const Train *t : Train::IterateFrontOnly()) { - if (!t->IsFrontEngine()) continue; - if (tile == t->tile || tile_end == t->tile) { - ShowVehicleViewWindow(t); - count++; + VehicleType veh_type = GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL ? VEH_TRAIN : VEH_ROAD; + + std::vector candidates; + for (TileIndex test_tile : { tile, tile_end }) { + 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; }