@@ -1365,55 +1365,57 @@ CommandCost CmdSellRailWagon(DoCommandFl
Train *first = v->First();
if (v->IsRearDualheaded()) return_cmd_error(STR_ERROR_REAR_ENGINE_FOLLOW_FRONT);
/* First make a backup of the order of the train. That way we can do
* whatever we want with the order and later on easily revert. */
TrainList original;
MakeTrainBackup(original, first);
/* We need to keep track of the new head and the head of what we're going to sell. */
Train *new_head = first;
Train *sell_head = NULL;
/* Split the train in the wanted way. */
ArrangeTrains(&sell_head, NULL, &new_head, v, sell_chain);
/* We don't need to validate the second train; it's going to be sold. */
CommandCost ret = ValidateTrains(NULL, NULL, first, new_head, (flags & DC_AUTOREPLACE) == 0);
if (ret.Failed()) {
/* Restore the train we had. */
RestoreTrainBackup(original);
return ret;
}
if (first->orders.list == NULL && !OrderList::CanAllocateItem()) {
return_cmd_error(STR_ERROR_NO_MORE_SPACE_FOR_ORDERS);
CommandCost cost(EXPENSES_NEW_VEHICLES);
for (Train *t = sell_head; t != NULL; t = t->Next()) cost.AddCost(-t->value);
/* do it? */
if (flags & DC_EXEC) {
/* First normalise the sub types of the chain. */
NormaliseSubtypes(new_head);
if (v == first && v->IsEngine() && !sell_chain && new_head != NULL && 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.list = first->orders.list;
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
} else if (v->IsPrimaryVehicle() && data & (MAKE_ORDER_BACKUP_FLAG >> 20)) {
OrderBackup::Backup(v, user);
/* We need to update the information about the train. */
NormaliseTrainHead(new_head);
/* We are undoubtedly changing something in the depot and train list. */
InvalidateWindowData(WC_VEHICLE_DEPOT, v->tile);
Status change: