mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-08-28 08:59:09 +00:00
Codechange: Iterate with VehiclesOnTile when connecting free wagon chains
This commit is contained in:
@@ -672,15 +672,22 @@ static CommandCost CmdBuildRailWagon(DoCommandFlags flags, TileIndex tile, const
|
|||||||
CheckConsistencyOfArticulatedVehicle(v);
|
CheckConsistencyOfArticulatedVehicle(v);
|
||||||
|
|
||||||
/* Try to connect the vehicle to one of free chains of wagons. */
|
/* Try to connect the vehicle to one of free chains of wagons. */
|
||||||
for (Train *w : Train::Iterate()) {
|
std::vector<VehicleID> freewagons;
|
||||||
if (w->tile == tile && ///< Same depot
|
for (Vehicle *u : VehiclesOnTile(tile)) {
|
||||||
w->IsFreeWagon() && ///< A free wagon chain
|
if (u->type != VEH_TRAIN) continue;
|
||||||
|
|
||||||
|
Train *w = Train::From(u);
|
||||||
|
if (w->IsFreeWagon() && ///< A free wagon chain
|
||||||
w->engine_type == e->index && ///< Same type
|
w->engine_type == e->index && ///< Same type
|
||||||
w->First() != v && ///< Don't connect to ourself
|
w->First() != v && ///< Don't connect to ourself
|
||||||
!w->vehstatus.Test(VehState::Crashed)) { ///< Not crashed/flooded
|
!w->vehstatus.Test(VehState::Crashed)) { ///< Not crashed/flooded
|
||||||
if (Command<CMD_MOVE_RAIL_VEHICLE>::Do(DoCommandFlag::Execute, v->index, w->Last()->index, true).Succeeded()) {
|
freewagons.push_back(w->Last()->index);
|
||||||
break;
|
}
|
||||||
}
|
}
|
||||||
|
std::ranges::sort(freewagons);
|
||||||
|
for (VehicleID veh_id : freewagons) {
|
||||||
|
if (Command<CMD_MOVE_RAIL_VEHICLE>::Do(DoCommandFlag::Execute, v->index, veh_id, true).Succeeded()) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user