From 3afb732c37f5b631e93d40a434f3cede349648e5 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 17 Sep 2023 13:24:07 +0100 Subject: [PATCH] Fix #11307: Incorrect GroupStatistics after selling leading wagon When this results in a countable consist --- src/train_cmd.cpp | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 0958fb582c..877e2d7e3b 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1421,16 +1421,18 @@ CommandCost CmdSellRailWagon(DoCommandFlag flags, Vehicle *t, bool sell_chain, b /* First normalise the sub types of the chain. */ NormaliseSubtypes(new_head); - if (v == first && v->IsEngine() && !sell_chain && new_head != nullptr && new_head->IsFrontEngine()) { - /* We are selling the front engine. In this case we want to - * 'give' the order, unit number and such to the new head. */ - new_head->orders = first->orders; - new_head->AddToShared(first); - DeleteVehicleOrders(first); + if (v == first && !sell_chain && new_head != nullptr && new_head->IsFrontEngine()) { + if (v->IsEngine()) { + /* We are selling the front engine. In this case we want to + * 'give' the order, unit number and such to the new head. */ + new_head->orders = first->orders; + new_head->AddToShared(first); + DeleteVehicleOrders(first); - /* Copy other important data from the front engine */ - new_head->CopyVehicleConfigAndStatistics(first); - GroupStatistics::CountVehicle(new_head, 1); // after copying over the profit + /* Copy other important data from the front engine */ + new_head->CopyVehicleConfigAndStatistics(first); + } + GroupStatistics::CountVehicle(new_head, 1); // after copying over the profit, if required } else if (v->IsPrimaryVehicle() && backup_order) { OrderBackup::Backup(v, user); }