From a0f0ece3845a5b3675256c28852c41cb11f11a86 Mon Sep 17 00:00:00 2001 From: J0anJosep Date: Sat, 26 Aug 2023 19:43:12 +0200 Subject: [PATCH] Change: Add the DC_AUTOREPLACE flag in missing places. --- src/autoreplace_cmd.cpp | 23 ++++++++++++++--------- src/train.h | 2 +- src/train_cmd.cpp | 8 +++++--- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp index ffcaf46710..eaa1e1f5cd 100644 --- a/src/autoreplace_cmd.cpp +++ b/src/autoreplace_cmd.cpp @@ -373,13 +373,13 @@ static CommandCost BuildReplacementVehicle(Vehicle *old_veh, Vehicle **new_vehic if (refit_cargo != CARGO_NO_REFIT) { uint8_t subtype = GetBestFittingSubType(old_veh, new_veh, refit_cargo); - cost.AddCost(std::get<0>(Command::Do(DC_EXEC, new_veh->index, refit_cargo, subtype, false, false, 0))); + cost.AddCost(std::get<0>(Command::Do(DC_EXEC | DC_AUTOREPLACE, new_veh->index, refit_cargo, subtype, false, false, 0))); assert(cost.Succeeded()); // This should be ensured by GetNewCargoTypeForReplace() } /* Try to reverse the vehicle, but do not care if it fails as the new type might not be reversible */ if (new_veh->type == VEH_TRAIN && HasBit(Train::From(old_veh)->flags, VRF_REVERSE_DIRECTION)) { - Command::Do(DC_EXEC, new_veh->index, true); + Command::Do(DC_EXEC | DC_AUTOREPLACE, new_veh->index, true); } return cost; @@ -474,7 +474,7 @@ static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, b if ((flags & DC_EXEC) != 0) { /* Move the new vehicle behind the old */ - CmdMoveVehicle(new_v, old_v, DC_EXEC, false); + CmdMoveVehicle(new_v, old_v, DC_EXEC | DC_AUTOREPLACE, false); /* Take over cargo * Note: We do only transfer cargo from the old to the new vehicle. @@ -494,7 +494,7 @@ static CommandCost ReplaceFreeUnit(Vehicle **single_unit, DoCommandFlag flags, b /* If we are not in DC_EXEC undo everything */ if ((flags & DC_EXEC) == 0) { - Command::Do(DC_EXEC, new_v->index, false, false, INVALID_CLIENT_ID); + Command::Do(DC_EXEC | DC_AUTOREPLACE, new_v->index, false, false, INVALID_CLIENT_ID); } } @@ -545,6 +545,8 @@ void CopyShipStatusInExtendedDepot(const Ship *old_ship, Ship *new_ship) */ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon_removal, bool *nothing_to_do) { + assert(flags & DC_AUTOREPLACE); + Vehicle *old_head = *chain; assert(old_head->IsPrimaryVehicle()); TileIndex tile = old_head->tile; @@ -598,7 +600,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon } if (last_engine == nullptr) last_engine = append; - cost.AddCost(CmdMoveVehicle(append, new_head, DC_EXEC, false)); + cost.AddCost(CmdMoveVehicle(append, new_head, DC_EXEC | DC_AUTOREPLACE, false)); if (cost.Failed()) break; } if (last_engine == nullptr) last_engine = new_head; @@ -617,7 +619,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon if (RailVehInfo(append->engine_type)->railveh_type == RAILVEH_WAGON) { /* Insert wagon after 'last_engine' */ - CommandCost res = CmdMoveVehicle(append, last_engine, DC_EXEC, false); + CommandCost res = CmdMoveVehicle(append, last_engine, DC_EXEC | DC_AUTOREPLACE, false); /* When we allow removal of wagons, either the move failing due * to the train becoming too long, or the train becoming longer @@ -648,7 +650,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon assert(RailVehInfo(wagon->engine_type)->railveh_type == RAILVEH_WAGON); /* Sell wagon */ - [[maybe_unused]] CommandCost ret = Command::Do(DC_EXEC, wagon->index, false, false, INVALID_CLIENT_ID); + [[maybe_unused]] CommandCost ret = Command::Do(DC_EXEC | DC_AUTOREPLACE, wagon->index, false, false, INVALID_CLIENT_ID); assert(ret.Succeeded()); it->new_veh = nullptr; @@ -714,7 +716,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon if ((flags & DC_EXEC) == 0) { for (auto it = std::rbegin(replacements); it != std::rend(replacements); ++it) { if (it->new_veh != nullptr) { - Command::Do(DC_EXEC, it->new_veh->index, false, false, INVALID_CLIENT_ID); + Command::Do(DC_EXEC | DC_AUTOREPLACE, it->new_veh->index, false, false, INVALID_CLIENT_ID); it->new_veh = nullptr; } } @@ -751,7 +753,7 @@ static CommandCost ReplaceChain(Vehicle **chain, DoCommandFlag flags, bool wagon /* If we are not in DC_EXEC undo everything */ if ((flags & DC_EXEC) == 0) { - Command::Do(DC_EXEC, new_head->index, false, false, INVALID_CLIENT_ID); + Command::Do(DC_EXEC | DC_AUTOREPLACE, new_head->index, false, false, INVALID_CLIENT_ID); } } } @@ -817,6 +819,9 @@ CommandCost CmdAutoreplaceVehicle(DoCommandFlag flags, VehicleID veh_id) if (IsExtendedDepotTile(v->tile)) UpdateExtendedDepotReservation(v, false); + /* Start autoreplacing the vehicle. */ + flags |= DC_AUTOREPLACE; + /* 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 */ diff --git a/src/train.h b/src/train.h index 7edc4f5832..9fcd925c82 100644 --- a/src/train.h +++ b/src/train.h @@ -69,7 +69,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); +void NormalizeTrainVehInDepot(const Train *u, DoCommandFlag flags = DC_EXEC); /** Variables that are cached to improve performance and such */ struct TrainCache { diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index e14eb6298d..232be134a8 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -742,7 +742,7 @@ static CommandCost CmdBuildRailWagon(DoCommandFlag flags, TileIndex tile, const w->engine_type == e->index && ///< Same type w->First() != v && ///< Don't connect to ourself !(w->vehstatus & VS_CRASHED)) { ///< Not crashed/flooded - if (Command::Do(DC_EXEC, v->index, w->Last()->index, true).Succeeded()) { + if (Command::Do(flags | DC_EXEC, v->index, w->Last()->index, true).Succeeded()) { break; } } @@ -753,14 +753,16 @@ static CommandCost CmdBuildRailWagon(DoCommandFlag flags, TileIndex tile, const } /** Move all free vehicles in the depot to the train */ -void NormalizeTrainVehInDepot(const Train *u) +void NormalizeTrainVehInDepot(const Train *u, DoCommandFlag flags) { assert(u->IsEngine()); + assert(flags & DC_EXEC); + DepotID dep_id = GetDepotIndex(u->tile); for (const Train *v : Train::Iterate()) { if (v->IsFreeWagon() && v->IsInDepot() && GetDepotIndex(v->tile) == dep_id) { - if (Command::Do(DC_EXEC, v->index, u->index, true).Failed()) { + if (Command::Do(flags, v->index, u->index, true).Failed()) { break; } }