Fix #10502: Refit engine before attaching free wagons. (#10926)

Caused by incorrect order of operations when buying a train engine with refit and attaching free wagons.
pull/603/head
PeterN 1 year ago committed by GitHub
parent 87ccff16b5
commit ee2d0745e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -66,6 +66,7 @@ int GetTrainStopLocation(StationID station_id, TileIndex tile, const Train *v, i
void GetTrainSpriteSize(EngineID engine, uint &width, uint &height, int &xoffs, int &yoffs, EngineImageType image_type);
bool TrainOnCrossing(TileIndex tile);
void NormalizeTrainVehInDepot(const Train *u);
/** Variables that are cached to improve performance and such */
struct TrainCache {

@ -686,7 +686,7 @@ static CommandCost CmdBuildRailWagon(DoCommandFlag flags, TileIndex tile, const
}
/** Move all free vehicles in the depot to the train */
static void NormalizeTrainVehInDepot(const Train *u)
void NormalizeTrainVehInDepot(const Train *u)
{
for (const Train *v : Train::Iterate()) {
if (v->IsFreeWagon() && v->tile == u->tile &&
@ -737,11 +737,10 @@ static void AddRearEngineToMultiheadedTrain(Train *v)
* @param flags type of operation.
* @param tile tile of the depot where rail-vehicle is built.
* @param e the engine to build.
* @param free_cars add any free cars to the train.
* @param[out] ret the vehicle that has been built.
* @return the cost of this operation or an error.
*/
CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, bool free_cars, Vehicle **ret)
CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret)
{
const RailVehicleInfo *rvi = &e->u.rail;
@ -807,10 +806,6 @@ CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engin
v->ConsistChanged(CCF_ARRANGE);
UpdateTrainGroupID(v);
if (free_cars && !(flags & DC_AUTOREPLACE)) { // check if the cars should be added to the new vehicle
NormalizeTrainVehInDepot(v);
}
CheckConsistencyOfArticulatedVehicle(v);
}

@ -14,7 +14,7 @@
#include "engine_type.h"
#include "vehicle_type.h"
CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, bool free_cars, Vehicle **ret);
CommandCost CmdBuildRailVehicle(DoCommandFlag flags, TileIndex tile, const Engine *e, Vehicle **ret);
CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *t, bool sell_chain, bool backup_order, ClientID user);
CommandCost CmdMoveRailVehicle(DoCommandFlag flags, VehicleID src_veh, VehicleID dest_veh, bool move_chain);

@ -135,7 +135,7 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
Vehicle *v = nullptr;
switch (type) {
case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(subflags, tile, e, use_free_vehicles, &v)); break;
case VEH_TRAIN: value.AddCost(CmdBuildRailVehicle(subflags, tile, e, &v)); break;
case VEH_ROAD: value.AddCost(CmdBuildRoadVehicle(subflags, tile, e, &v)); break;
case VEH_SHIP: value.AddCost(CmdBuildShip (subflags, tile, e, &v)); break;
case VEH_AIRCRAFT: value.AddCost(CmdBuildAircraft (subflags, tile, e, &v)); break;
@ -172,6 +172,11 @@ std::tuple<CommandCost, VehicleID, uint, uint16, CargoArray> CmdBuildVehicle(DoC
}
if (flags & DC_EXEC) {
if (type == VEH_TRAIN && use_free_vehicles && !(flags & DC_AUTOREPLACE)) {
/* Move any free wagons to the new vehicle. */
NormalizeTrainVehInDepot(Train::From(v));
}
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
InvalidateWindowClassesData(GetWindowClassForVehicleType(type), 0);
SetWindowDirty(WC_COMPANY, _current_company);

Loading…
Cancel
Save