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; } } }