diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index 52f4588fbc..38b88b85d1 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -28,6 +28,7 @@ #include "order_cmd.h" #include "train_cmd.h" #include "vehicle_cmd.h" +#include "depot_map.h" #include "table/strings.h" @@ -777,45 +778,45 @@ CommandCost CmdAutoreplaceVehicle(DoCommandFlag flags, VehicleID veh_id) any_replacements |= (e != INVALID_ENGINE); w = (!free_wagon && w->type == VEH_TRAIN ? Train::From(w)->GetNextUnit() : nullptr); } + if (!any_replacements) return_cmd_error(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO); CommandCost cost = CommandCost(EXPENSES_NEW_VEHICLES, (Money)0); bool nothing_to_do = true; + bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0); - if (any_replacements) { - bool was_stopped = free_wagon || ((v->vehstatus & VS_STOPPED) != 0); + /* Stop the vehicle */ + if (!was_stopped) cost.AddCost(DoCmdStartStopVehicle(v, true)); + if (cost.Failed()) return cost; - /* Stop the vehicle */ - if (!was_stopped) cost.AddCost(DoCmdStartStopVehicle(v, true)); - if (cost.Failed()) return cost; + assert(free_wagon || v->IsStoppedInDepot()); - assert(free_wagon || v->IsStoppedInDepot()); + /* We have to construct the new vehicle chain to test whether it is valid. + * Vehicle construction needs random bits, so we have to save the random seeds + * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */ + SavedRandomSeeds saved_seeds; + SaveRandomSeeds(&saved_seeds); + if (free_wagon) { + cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do)); + } else { + cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do)); + } + RestoreRandomSeeds(saved_seeds); - /* We have to construct the new vehicle chain to test whether it is valid. - * Vehicle construction needs random bits, so we have to save the random seeds - * to prevent desyncs and to replay newgrf callbacks during DC_EXEC */ - SavedRandomSeeds saved_seeds; - SaveRandomSeeds(&saved_seeds); + if (cost.Succeeded() && (flags & DC_EXEC) != 0) { if (free_wagon) { - cost.AddCost(ReplaceFreeUnit(&v, flags & ~DC_EXEC, ¬hing_to_do)); + ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do); } else { - cost.AddCost(ReplaceChain(&v, flags & ~DC_EXEC, wagon_removal, ¬hing_to_do)); - } - RestoreRandomSeeds(saved_seeds); - - if (cost.Succeeded() && (flags & DC_EXEC) != 0) { - if (free_wagon) { - ret = ReplaceFreeUnit(&v, flags, ¬hing_to_do); - } else { - ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do); - } - assert(ret.Succeeded() && ret.GetCost() == cost.GetCost()); + ret = ReplaceChain(&v, flags, wagon_removal, ¬hing_to_do); } - /* Restart the vehicle */ - if (!was_stopped) cost.AddCost(DoCmdStartStopVehicle(v, false)); + assert(ret.Succeeded()); + assert(ret.GetCost() == cost.GetCost()); } - if (cost.Succeeded() && nothing_to_do) cost = CommandCost(STR_ERROR_AUTOREPLACE_NOTHING_TO_DO); + /* Restart the vehicle */ + if (!was_stopped) cost.AddCost(DoCmdStartStopVehicle(v, false)); + + assert(cost.Failed() || !nothing_to_do); return cost; }