diff --git a/src/infrastructure.cpp b/src/infrastructure.cpp index c459282cca..19f01d2bf7 100644 --- a/src/infrastructure.cpp +++ b/src/infrastructure.cpp @@ -25,6 +25,7 @@ #include "scope_info.h" #include "order_cmd.h" #include "strings_func.h" +#include "scope.h" #include "table/strings.h" @@ -238,13 +239,19 @@ static void FixAllReservations() * If vehicles are still on others' infrastructure or using others' stations, * The change is not possible and false is returned. * @param type The type of vehicle whose setting will be changed. + * @param new_value True if sharing will become enabled. * @return True if the change can take place, false otherwise. */ -bool CheckSharingChangePossible(VehicleType type) +bool CheckSharingChangePossible(VehicleType type, bool new_value) { if (type != VEH_AIRCRAFT) YapfNotifyTrackLayoutChange(INVALID_TILE, INVALID_TRACK); /* Only do something when sharing is being disabled */ - if (_settings_game.economy.infrastructure_sharing[type]) return true; + if (!_settings_game.economy.infrastructure_sharing[type] || new_value) return true; + + _settings_game.economy.infrastructure_sharing[type] = false; + auto guard = scope_guard([type]() { + _settings_game.economy.infrastructure_sharing[type] = true; + }); StringID error_message = STR_NULL; for (Vehicle *v : Vehicle::Iterate()) { diff --git a/src/infrastructure_func.h b/src/infrastructure_func.h index 1bcea4c928..ac2ca81cf3 100644 --- a/src/infrastructure_func.h +++ b/src/infrastructure_func.h @@ -19,7 +19,7 @@ void PayStationSharingFee(Vehicle *v, const Station *st); void PayDailyTrackSharingFee(Train *v); -bool CheckSharingChangePossible(VehicleType type); +bool CheckSharingChangePossible(VehicleType type, bool new_value); void HandleSharingCompanyDeletion(Owner owner); void UpdateAllBlockSignals(Owner owner = INVALID_OWNER); diff --git a/src/settings.cpp b/src/settings.cpp index 15f5b10c11..ba26eb4542 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1617,7 +1617,7 @@ static void StationCatchmentChanged(int32 new_value) static bool CheckSharingRail(int32 &new_value) { - return CheckSharingChangePossible(VEH_TRAIN); + return CheckSharingChangePossible(VEH_TRAIN, new_value); } static void SharingRailChanged(int32 new_value) @@ -1627,17 +1627,17 @@ static void SharingRailChanged(int32 new_value) static bool CheckSharingRoad(int32 &new_value) { - return CheckSharingChangePossible(VEH_ROAD); + return CheckSharingChangePossible(VEH_ROAD, new_value); } static bool CheckSharingWater(int32 &new_value) { - return CheckSharingChangePossible(VEH_SHIP); + return CheckSharingChangePossible(VEH_SHIP, new_value); } static bool CheckSharingAir(int32 &new_value) { - return CheckSharingChangePossible(VEH_AIRCRAFT); + return CheckSharingChangePossible(VEH_AIRCRAFT, new_value); } static void MaxVehiclesChanged(int32 new_value)