diff --git a/src/vehicle.cpp b/src/vehicle.cpp --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -2359,7 +2359,7 @@ const GroundVehicleCache *Vehicle::GetGr * Calculates the set of vehicles that will be affected by a given selection. * @param set Set of affected vehicles. * @param v First vehicle of the selection. - * @param num_vehicles Number of vehicles in the selection. + * @param num_vehicles Number of vehicles in the selection (not counting articulated parts). * @pre \c set must be empty. * @post \c set will contain the vehicles that will be refitted. */ @@ -2367,27 +2367,20 @@ void GetVehicleSet(VehicleSet &set, Vehi { if (v->type == VEH_TRAIN) { Train *u = Train::From(v); - /* If the first vehicle in the selection is part of an articulated vehicle, add the previous parts of the vehicle. */ - if (u->IsArticulatedPart()) { - u = u->GetFirstEnginePart(); - while (u->index != v->index) { - set.Include(u->index); - u = u->GetNextArticulatedPart(); - } - } + /* Only include whole vehicles, so start with the first articulated part */ + u = u->GetFirstEnginePart(); - for (;u != NULL && num_vehicles > 0; num_vehicles--, u = u->Next()) { - /* Include current vehicle in the selection. */ - set.Include(u->index); + /* Include num_vehicles vehicles, not counting articulated parts */ + for (; u != NULL && num_vehicles > 0; num_vehicles--) { + do { + /* Include current vehicle in the selection. */ + set.Include(u->index); - /* If the vehicle is multiheaded, add the other part too. */ - if (u->IsMultiheaded()) set.Include(u->other_multiheaded_part->index); - } + /* If the vehicle is multiheaded, add the other part too. */ + if (u->IsMultiheaded()) set.Include(u->other_multiheaded_part->index); - /* If the last vehicle is part of an articulated vehicle, add the following parts of the vehicle. */ - while (u != NULL && u->IsArticulatedPart()) { - set.Include(u->index); - u = u->Next(); + u = u->Next(); + } while (u != NULL && u->IsArticulatedPart()); } } } diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -245,7 +245,7 @@ static CommandCost GetRefitCost(EngineID * This is the vehicle-type independent part of the CmdRefitXXX functions. * @param v The vehicle to refit. * @param only_this Whether to only refit this vehicle, or to check the rest of them. - * @param num_vehicles Number of vehicles to refit. Zero means the whole chain. + * @param num_vehicles Number of vehicles to refit (not counting articulated parts). Zero means the whole chain. * @param new_cid Cargotype to refit to * @param new_subtype Cargo subtype to refit to * @param flags Command flags @@ -319,7 +319,8 @@ static CommandCost RefitVehicle(Vehicle * - p2 = (bit 0-4) - New cargo type to refit to. * - p2 = (bit 7) - Refit only this vehicle. Used only for cloning vehicles. * - p2 = (bit 8-15) - New cargo subtype to refit to. - * - p2 = (bit 16-23) - Number of vehicles to refit. Zero means all vehicles. Only used if "refit only this vehicle" is false. + * - p2 = (bit 16-23) - Number of vehicles to refit (not counting articulated parts). Zero means all vehicles. + * Only used if "refit only this vehicle" is false. * @param text unused * @return the cost of this operation or an error */ diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -758,9 +758,14 @@ struct RefitWindow : public Window { if (left_x < 0 && !start_counting) { this->selected_vehicle = u->index; start_counting = true; + + /* Count the first vehicle, even if articulated part */ + this->num_vehicles++; + } else if (start_counting && !u->IsArticulatedPart()) { + /* Do not count articulated parts */ + this->num_vehicles++; } - if (start_counting) this->num_vehicles++; if (right_x < 0) break; } }