From cf38e410ce390526682cf75caab8c86236187542 Mon Sep 17 00:00:00 2001 From: SamuXarick <43006711+SamuXarick@users.noreply.github.com> Date: Sun, 18 May 2025 17:54:09 +0100 Subject: [PATCH] Codechange: Iterate with VehiclesOnTile when moving free wagons to the new vehicle --- src/train_cmd.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index cacc922ddf..7a43b643ad 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -692,12 +692,19 @@ static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const void NormalizeTrainVehInDepot(const Train *u) { assert(u->IsEngine()); - for (const Train *v : Train::Iterate()) { - if (v->IsFreeWagon() && v->tile == u->tile && - v->track == TRACK_BIT_DEPOT) { - if (Command::Do(DoCommandFlag::Execute, v->index, u->index, true).Failed()) { - break; - } + std::vector freewagons; + for (const Vehicle *w : VehiclesOnTile(u->tile)) { + if (w->type != VEH_TRAIN) continue; + + const Train *v = Train::From(w); + if (v->IsFreeWagon() && v->track == TRACK_BIT_DEPOT) { + freewagons.push_back(v->index); + } + } + std::ranges::sort(freewagons); + for (VehicleID veh_id : freewagons) { + if (Command::Do(DoCommandFlag::Execute, veh_id, u->index, true).Failed()) { + break; } } }