Codechange: Iterate with VehiclesOnTile when attaching a just bought wagon to a locomotive (#14284)

This commit is contained in:
SamuXarick 2025-06-08 21:45:58 +01:00 committed by GitHub
parent de660cba02
commit 893a75f2c0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -32,8 +32,13 @@ void CcBuildWagon(Commands, const CommandCost &result, VehicleID new_veh_id, uin
/* find a locomotive in the depot. */
const Vehicle *found = nullptr;
for (const Train *t : Train::Iterate()) {
if (t->IsFrontEngine() && t->tile == tile && t->IsStoppedInDepot()) {
/* The non-deterministic order returned from VehiclesOnTile() does not
* matter here as there must only be one locomotive for anything to happen. */
for (const Vehicle *v : VehiclesOnTile(tile)) {
if (v->type != VEH_TRAIN) continue;
const Train *t = Train::From(v);
if (t->IsFrontEngine() && t->IsStoppedInDepot()) {
if (found != nullptr) return; // must be exactly one.
found = t;
}