diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index afcaa51f93..561a625e6d 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -40,7 +40,7 @@ /* Clients shouldn't start AIs */ if (_networking && !_network_server) return; - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); Company *c = Company::Get(company); AIConfig *config = c->ai_config.get(); @@ -65,8 +65,6 @@ c->ai_instance->LoadOnStack(config->GetToLoadData()); config->SetToLoadData(nullptr); - cur_company.Restore(); - InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); return; } @@ -81,11 +79,10 @@ assert(_settings_game.difficulty.competitor_speed <= 4); if ((AI::frame_counter & ((1 << (4 - _settings_game.difficulty.competitor_speed)) - 1)) != 0) return; - Backup cur_company(_current_company); for (const Company *c : Company::Iterate()) { if (c->is_ai) { PerformanceMeasurer framerate((PerformanceElement)(PFE_AI0 + c->index)); - cur_company.Change(c->index); + AutoRestoreBackup cur_company(_current_company, c->index); c->ai_instance->GameLoop(); /* Occasionally collect garbage; every 255 ticks do one company. * Effectively collecting garbage once every two months per AI. */ @@ -96,7 +93,6 @@ PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + c->index)); } } - cur_company.Restore(); } /* static */ uint AI::GetTick() @@ -109,15 +105,13 @@ if (_networking && !_network_server) return; PerformanceMeasurer::SetInactive((PerformanceElement)(PFE_AI0 + company)); - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); Company *c = Company::Get(company); c->ai_instance.reset(); c->ai_info = nullptr; c->ai_config.reset(); - cur_company.Restore(); - InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); } @@ -128,28 +122,20 @@ * for the server owner to unpause the script again. */ if (_network_dedicated) return; - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); Company::Get(company)->ai_instance->Pause(); - - cur_company.Restore(); } /* static */ void AI::Unpause(CompanyID company) { - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); Company::Get(company)->ai_instance->Unpause(); - - cur_company.Restore(); } /* static */ bool AI::IsPaused(CompanyID company) { - Backup cur_company(_current_company, company); - bool paused = Company::Get(company)->ai_instance->IsPaused(); - - cur_company.Restore(); - - return paused; + AutoRestoreBackup cur_company(_current_company, company); + return Company::Get(company)->ai_instance->IsPaused(); } /* static */ void AI::KillAll() @@ -247,9 +233,8 @@ } /* Queue the event */ - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); Company::Get(_current_company)->ai_instance->InsertEvent(event); - cur_company.Restore(); } /* static */ void AI::BroadcastNewEvent(ScriptEvent *event, CompanyID skip_company) @@ -275,9 +260,8 @@ /* When doing emergency saving, an AI can be not fully initialised. */ if (c->ai_instance != nullptr) { - Backup cur_company(_current_company, company); + AutoRestoreBackup cur_company(_current_company, company); c->ai_instance->Save(); - cur_company.Restore(); return; } } diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index e2a250c1bb..27079a8941 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -1300,9 +1300,8 @@ void HandleMissingAircraftOrders(Aircraft *v) */ const Station *st = GetTargetAirportIfValid(v); if (st == nullptr) { - Backup cur_company(_current_company, v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); CommandCost ret = Command::Do(DoCommandFlag::Execute, v->index, DepotCommandFlag{}, {}); - cur_company.Restore(); if (ret.Failed()) CrashAirplane(v); } else if (!v->current_order.IsType(OT_GOTO_DEPOT)) { @@ -1660,9 +1659,8 @@ static void AircraftEventHandler_HeliTakeOff(Aircraft *v, [[maybe_unused]] const /* Send the helicopter to a hangar if needed for replacement */ if (v->NeedsAutomaticServicing()) { - Backup cur_company(_current_company, v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); Command::Do(DoCommandFlag::Execute, v->index, DepotCommandFlag::Service, {}); - cur_company.Restore(); } } @@ -1713,9 +1711,8 @@ static void AircraftEventHandler_Landing(Aircraft *v, [[maybe_unused]] const Air /* check if the aircraft needs to be replaced or renewed and send it to a hangar if needed */ if (v->NeedsAutomaticServicing()) { - Backup cur_company(_current_company, v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); Command::Do(DoCommandFlag::Execute, v->index, DepotCommandFlag::Service, {}); - cur_company.Restore(); } } diff --git a/src/disaster_vehicle.cpp b/src/disaster_vehicle.cpp index 733cd35382..e9c6b483a2 100644 --- a/src/disaster_vehicle.cpp +++ b/src/disaster_vehicle.cpp @@ -63,9 +63,8 @@ static void DisasterClearSquare(TileIndex tile) switch (GetTileType(tile)) { case TileType::Railway: if (Company::IsHumanID(GetTileOwner(tile)) && !IsRailDepot(tile)) { - Backup cur_company(_current_company, OWNER_WATER); + AutoRestoreBackup cur_company(_current_company, OWNER_WATER); Command::Do(DoCommandFlag::Execute, tile); - cur_company.Restore(); /* update signals in buffer */ UpdateSignalsInBuffer(); @@ -73,9 +72,8 @@ static void DisasterClearSquare(TileIndex tile) break; case TileType::House: { - Backup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); Command::Do(DoCommandFlag::Execute, tile); - cur_company.Restore(); break; } diff --git a/src/economy.cpp b/src/economy.cpp index 3441fc5ceb..5b0f13ed6f 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -324,7 +324,7 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) /* We need to set _current_company to old_owner before we try to move * the client. This is needed as it needs to know whether "you" really * are the current local company. */ - Backup cur_company(_current_company, old_owner); + AutoRestoreBackup cur_company(_current_company, old_owner); /* In all cases, make spectators of clients connected to that company */ if (_networking) NetworkClientsToSpectators(old_owner); if (old_owner == _local_company) { @@ -538,8 +538,6 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner) /* Change colour of existing windows */ if (new_owner != INVALID_OWNER) ChangeWindowOwner(old_owner, new_owner); - cur_company.Restore(); - MarkWholeScreenDirty(); } @@ -1185,7 +1183,7 @@ CargoPayment::~CargoPayment() if (this->visual_profit == 0 && this->visual_transfer == 0) return; - Backup cur_company(_current_company, this->front->owner); + AutoRestoreBackup cur_company(_current_company, this->front->owner); SubtractMoneyFromCompany(_current_company, CommandCost(this->front->GetExpenseType(true), -this->route_profit)); this->front->profit_this_year += (this->visual_profit + this->visual_transfer) << 8; @@ -1202,8 +1200,6 @@ CargoPayment::~CargoPayment() ShowCostOrIncomeAnimation(moving_front->x_pos, moving_front->y_pos, moving_front->z_pos, -this->visual_profit); } - - cur_company.Restore(); } /** @@ -1476,7 +1472,7 @@ static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station Vehicle *v_start = v->GetFirstEnginePart(); if (!IterateVehicleParts(v_start, IsEmptyAction())) return; - Backup cur_company(_current_company, v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); CargoTypes refit_mask = v->GetEngine()->info.refit_mask; @@ -1521,8 +1517,6 @@ static void HandleStationRefit(Vehicle *v, CargoArray &consist_capleft, Station /* Add new capacity to consist capacity and reserve cargo */ IterateVehicleParts(v_start, FinalizeRefitAction(consist_capleft, st, next_station, is_auto_refit || v->First()->current_order.IsFullLoadOrder())); - - cur_company.Restore(); } /** diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index 6371a16b91..e9a4384da7 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -43,10 +43,8 @@ Game::frame_counter++; - Backup cur_company(_current_company); - cur_company.Change(OWNER_DEITY); + AutoRestoreBackup cur_company(_current_company, OWNER_DEITY); Game::instance->GameLoop(); - cur_company.Restore(); /* Occasionally collect garbage */ if ((Game::frame_counter & 255) == 0) { @@ -85,8 +83,7 @@ config->AnchorUnchangeableSettings(); - Backup cur_company(_current_company); - cur_company.Change(OWNER_DEITY); + AutoRestoreBackup cur_company(_current_company, OWNER_DEITY); Game::info = info; Game::instance = std::make_unique(); @@ -94,8 +91,6 @@ Game::instance->LoadOnStack(config->GetToLoadData()); config->SetToLoadData(nullptr); - cur_company.Restore(); - InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1); } @@ -148,9 +143,8 @@ } /* Queue the event */ - Backup cur_company(_current_company, OWNER_DEITY); + AutoRestoreBackup cur_company(_current_company, OWNER_DEITY); Game::instance->InsertEvent(event); - cur_company.Restore(); } /* static */ void Game::ResetConfig() @@ -192,9 +186,8 @@ /* static */ void Game::Save() { if (Game::instance != nullptr && (!_networking || _network_server)) { - Backup cur_company(_current_company, OWNER_DEITY); + AutoRestoreBackup cur_company(_current_company, OWNER_DEITY); Game::instance->Save(); - cur_company.Restore(); } else { GameInstance::SaveEmpty(); } diff --git a/src/genworld.cpp b/src/genworld.cpp index d6f4eefec9..5a239709ba 100644 --- a/src/genworld.cpp +++ b/src/genworld.cpp @@ -98,7 +98,7 @@ static void CleanupGeneration() static void _GenerateWorld() { /* Make sure everything is done via OWNER_NONE. */ - Backup _cur_company(_current_company, OWNER_NONE); + Backup cur_company(_current_company, OWNER_NONE); try { _generating_world = true; @@ -198,7 +198,7 @@ static void _GenerateWorld() BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP); ResetObjectToPlace(); - _cur_company.Trash(); + cur_company.Trash(); _current_company = _local_company = GenWorldInfo::lc; /* Show all vital windows again, because we have hidden them. */ if (_game_mode != GM_MENU) ShowVitalWindows(); @@ -223,7 +223,7 @@ static void _GenerateWorld() CleanupGeneration(); BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP, true); - if (_cur_company.IsValid()) _cur_company.Restore(); + if (cur_company.IsValid()) cur_company.Restore(); if (_network_dedicated) { /* Exit the game to prevent a return to main menu. */ diff --git a/src/industry_cmd.cpp b/src/industry_cmd.cpp index 6fa6292d2a..b718dbaa42 100644 --- a/src/industry_cmd.cpp +++ b/src/industry_cmd.cpp @@ -1131,7 +1131,7 @@ static void ChopLumberMillTrees(Industry *i) _industry_sound_tile = tile; if (_settings_client.sound.ambient) SndPlayTileFx(SND_38_LUMBER_MILL_1, tile); - AutoRestoreBackup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); Command::Do(DoCommandFlag::Execute, tile); /* Add according value to waiting cargo. */ @@ -1503,9 +1503,8 @@ static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTil } /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */ - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); ret = Command::Do({}, cur_tile); - cur_company.Restore(); if (ret.Failed()) return ret; } else { @@ -1649,7 +1648,7 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlags flags /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry. * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */ - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); for (TileIndex tile_walk : ta) { uint curh = TileHeight(tile_walk); @@ -1657,13 +1656,11 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlags flags /* This tile needs terraforming. Check if we can do that without * damaging the surroundings too much. */ if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) { - cur_company.Restore(); return false; } /* This is not 100% correct check, but the best we can do without modifying the map. * What is missing, is if the difference in height is more than 1.. */ if (std::get<0>(Command::Do(DoCommandFlags{flags}.Reset(DoCommandFlag::Execute), tile_walk, SLOPE_N, curh <= h)).Failed()) { - cur_company.Restore(); return false; } } @@ -1683,7 +1680,6 @@ static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlags flags } } - cur_company.Restore(); return true; } @@ -2102,7 +2098,7 @@ CommandCost CmdBuildIndustry(DoCommandFlags flags, TileIndex tile, IndustryType if (prospect_success) { /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */ IndustryAvailabilityCallType calltype = _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_PROSPECTCREATION; - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); for (int i = 0; i < 5000; i++) { /* We should not have more than one Random() in a function call * because parameter evaluation order is not guaranteed in the c++ standard @@ -2118,7 +2114,6 @@ CommandCost CmdBuildIndustry(DoCommandFlags flags, TileIndex tile, IndustryType } if (ret.Succeeded()) break; } - cur_company.Restore(); } if (ret.Failed() && IsLocalCompany()) { if (prospect_success) { @@ -2406,12 +2401,10 @@ static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType c */ static void PlaceInitialIndustry(IndustryType type, bool water, bool try_hard) { - Backup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); IncreaseGeneratingWorldProgress(water ? GWP_WATER_INDUSTRY : GWP_LAND_INDUSTRY); PlaceIndustry(type, IACT_MAPGENERATION, try_hard); - - cur_company.Restore(); } /** @@ -3101,7 +3094,7 @@ static const IntervalTimer _economy_industries_daily({TimerGam return; // Nothing to do? get out } - Backup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); /* perform the required industry changes for the day */ @@ -3121,15 +3114,14 @@ static const IntervalTimer _economy_industries_daily({TimerGam } } - cur_company.Restore(); - /* production-change */ InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE); }); +/** Economy monthly loop for industries. */ static const IntervalTimer _economy_industries_monthly({TimerGameEconomy::Trigger::Month, TimerGameEconomy::Priority::Industry}, [](auto) { - Backup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); _industry_builder.EconomyMonthlyLoop(); @@ -3143,8 +3135,6 @@ static const IntervalTimer _economy_industries_monthly({TimerG } } - cur_company.Restore(); - /* production-change */ InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, IDIWD_PRODUCTION_CHANGE); }); diff --git a/src/industry_gui.cpp b/src/industry_gui.cpp index 55f264771c..0f00aca28e 100644 --- a/src/industry_gui.cpp +++ b/src/industry_gui.cpp @@ -604,11 +604,10 @@ public: if (Town::GetNumItems() == 0) { ShowErrorMessage(GetEncodedString(STR_ERROR_CAN_T_GENERATE_INDUSTRIES), GetEncodedString(STR_ERROR_MUST_FOUND_TOWN_FIRST), WL_INFO); } else { - Backup old_generating_world(_generating_world, true); + AutoRestoreBackup old_generating_world(_generating_world, true); BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP); GenerateIndustries(); BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP); - old_generating_world.Restore(); } } diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index a4906a91a6..4ac960e842 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -1270,7 +1270,7 @@ void NetworkClientRequestMove(CompanyID company_id) */ void NetworkClientsToSpectators(CompanyID cid) { - Backup cur_company(_current_company); + AutoRestoreBackup cur_company(_current_company, _current_company); /* If our company is changing owner, go to spectators */ if (cid == _local_company) SetLocalCompany(COMPANY_SPECTATOR); @@ -1279,8 +1279,6 @@ void NetworkClientsToSpectators(CompanyID cid) NetworkTextMessage(NetworkAction::CompanySpectator, CC_DEFAULT, false, ci->client_name); ci->client_playas = COMPANY_SPECTATOR; } - - cur_company.Restore(); } /** diff --git a/src/network/network_gui.cpp b/src/network/network_gui.cpp index 6f1bec2bf4..ff84328150 100644 --- a/src/network/network_gui.cpp +++ b/src/network/network_gui.cpp @@ -1715,7 +1715,7 @@ private: */ static void OnClickClientAuthorize(NetworkClientListWindow *, Point, ClientID client_id) { - AutoRestoreBackup cur_company(_current_company, NetworkClientInfo::GetByClientID(_network_own_client_id)->client_playas); + AutoRestoreBackup cur_company(_current_company, NetworkClientInfo::GetByClientID(_network_own_client_id)->client_playas); Command::Post(CompanyAllowListCtrlAction::AddKey, NetworkClientInfo::GetByClientID(client_id)->public_key); } diff --git a/src/openttd.cpp b/src/openttd.cpp index c7f517c100..0f55c0c55a 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1252,7 +1252,7 @@ void StateGameLoop() /* All these actions has to be done from OWNER_NONE * for multiplayer compatibility */ - Backup cur_company(_current_company, OWNER_NONE); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP); AnimateAnimatedTiles(); @@ -1277,7 +1277,6 @@ void StateGameLoop() CallWindowGameTickEvent(); NewsLoop(); - cur_company.Restore(); } assert(IsLocalCompany()); diff --git a/src/rail_cmd.cpp b/src/rail_cmd.cpp index ea954bf2e2..a690073112 100644 --- a/src/rail_cmd.cpp +++ b/src/rail_cmd.cpp @@ -773,10 +773,10 @@ bool FloodHalftile(TileIndex t) TrackBits to_remove = lower_track & rail_bits; if (to_remove != TRACK_BIT_NONE) { - Backup cur_company(_current_company, OWNER_WATER); + AutoRestoreBackup cur_company(_current_company, OWNER_WATER); flooded = Command::Do(DoCommandFlag::Execute, t, FindFirstTrack(to_remove)).Succeeded(); - cur_company.Restore(); if (!flooded) return flooded; // not yet floodable + rail_bits = rail_bits & ~to_remove; if (rail_bits == TRACK_BIT_NONE) { MakeShore(t); diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 42f7bf4e36..30f8f90e4b 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -1143,12 +1143,8 @@ static Trackdir FollowPreviousRoadVehicle(const RoadVehicle *v, const RoadVehicl static bool CanBuildTramTrackOnTile(CompanyID c, TileIndex t, RoadType rt, RoadBits r) { /* The 'current' company is not necessarily the owner of the vehicle. */ - Backup cur_company(_current_company, c); - - CommandCost ret = Command::Do(DoCommandFlag::NoWater, t, r, rt, {}, TownID::Invalid()); - - cur_company.Restore(); - return ret.Succeeded(); + AutoRestoreBackup cur_company(_current_company, c); + return Command::Do(DoCommandFlag::NoWater, t, r, rt, {}, TownID::Invalid()).Succeeded(); } bool IndividualRoadVehicleController(RoadVehicle *v, const RoadVehicle *prev) diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 92538080a6..a4b5048b65 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -1941,9 +1941,8 @@ bool AfterLoadGame() if (IsBuoyTile(t) || IsDriveThroughStopTile(t) || IsTileType(t, TileType::Water)) { Owner o = GetTileOwner(t); if (o < MAX_COMPANIES && !Company::IsValidID(o)) { - Backup cur_company(_current_company, o); + AutoRestoreBackup cur_company(_current_company, o); ChangeTileOwner(t, o, INVALID_OWNER); - cur_company.Restore(); } if (IsBuoyTile(t)) { /* reset buoy owner to OWNER_NONE in the station struct diff --git a/src/town_cmd.cpp b/src/town_cmd.cpp index 8313fff36b..270874fbdc 100644 --- a/src/town_cmd.cpp +++ b/src/town_cmd.cpp @@ -650,7 +650,7 @@ static void TileLoop_Town(TileIndex tile) } } - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); if (hs->building_flags.Any(BUILDING_HAS_1_TILE) && t->flags.Test(TownFlag::IsGrowing) && @@ -688,8 +688,6 @@ static void TileLoop_Town(TileIndex tile) TryBuildTownHouse(t, tile, modes); } } - - cur_company.Restore(); } /** @copydoc ClearTileProc */ @@ -1891,7 +1889,7 @@ static bool GrowTown(Town *t, TownExpandModes modes) }; /* Current "company" is a town */ - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); TileIndex tile = t->xy; // The tile we are working with ATM @@ -1899,7 +1897,6 @@ static bool GrowTown(Town *t, TownExpandModes modes) for (const auto &ptr : _town_coord_mod) { if (GetTownRoadBits(tile).Any()) { bool success = GrowTownAtRoad(t, tile, modes); - cur_company.Restore(); return success; } tile = TileAdd(tile, ToTileIndexDiff(ptr)); @@ -1915,7 +1912,6 @@ static bool GrowTown(Town *t, TownExpandModes modes) if (Command::Do({DoCommandFlag::Auto, DoCommandFlag::NoWater}, tile).Succeeded()) { RoadType rt = GetTownRoadType(); Command::Do({DoCommandFlag::Execute, DoCommandFlag::Auto}, tile, GenRandomRoadBits(), rt, {}, t->index); - cur_company.Restore(); return true; } } @@ -1923,7 +1919,6 @@ static bool GrowTown(Town *t, TownExpandModes modes) } } - cur_company.Restore(); return false; } @@ -2387,9 +2382,8 @@ static Town *CreateRandomTown(uint attempts, uint32_t townnameparts, TownSize si * placement is so bad it couldn't grow at all */ if (t->cache.population > 0) return t; - Backup cur_company(_current_company, OWNER_TOWN); + AutoRestoreBackup cur_company(_current_company, OWNER_TOWN); [[maybe_unused]] CommandCost rc = Command::Do(DoCommandFlag::Execute, t->index); - cur_company.Restore(); assert(rc.Succeeded()); /* We already know that we can allocate a single town when @@ -3516,10 +3510,8 @@ static CommandCost TownActionRoadRebuild(Town *t, DoCommandFlags flags) */ static bool CheckClearTile(TileIndex tile) { - Backup cur_company(_current_company, OWNER_NONE); - CommandCost r = Command::Do({}, tile); - cur_company.Restore(); - return r.Succeeded(); + AutoRestoreBackup cur_company(_current_company, OWNER_NONE); + return Command::Do({}, tile).Succeeded(); } /** diff --git a/src/town_gui.cpp b/src/town_gui.cpp index 984e1f3dfb..241b31893a 100644 --- a/src/town_gui.cpp +++ b/src/town_gui.cpp @@ -1316,13 +1316,12 @@ public: auto value = ParseInteger(*str, 10, true); if (!value.has_value()) return; - Backup old_generating_world(_generating_world, true); + AutoRestoreBackup old_generating_world(_generating_world, true); UpdateNearestTownForRoadTiles(true); if (!GenerateTowns(this->town_layout, value)) { ShowErrorMessage(GetEncodedString(STR_ERROR_CAN_T_GENERATE_TOWN), GetEncodedString(STR_ERROR_NO_SPACE_FOR_TOWN), WL_INFO); } UpdateNearestTownForRoadTiles(false); - old_generating_world.Restore(); } void OnPlaceObject([[maybe_unused]] Point pt, TileIndex tile) override diff --git a/src/vehicle.cpp b/src/vehicle.cpp index c8dbea42ed..a8dd4c528c 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1069,11 +1069,10 @@ void CallVehicleTicks() } } - Backup cur_company(_current_company); for (auto &it : _vehicles_to_autoreplace) { Vehicle *v = Vehicle::Get(it.first); /* Autoreplace needs the current company set as the vehicle owner */ - cur_company.Change(v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); /* Start vehicle if we stopped them in VehicleEnteredDepotThisTick() * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that @@ -1111,8 +1110,6 @@ void CallVehicleTicks() AddVehicleAdviceNewsItem(AdviceType::AutorenewFailed, std::move(headline), v->index); } - - cur_company.Restore(); } /** @@ -1637,9 +1634,8 @@ void VehicleEnterDepot(Vehicle *v) } if (v->current_order.IsRefit()) { - Backup cur_company(_current_company, v->owner); + AutoRestoreBackup cur_company(_current_company, v->owner); CommandCost cost = std::get<0>(Command::Do(DoCommandFlag::Execute, v->index, v->current_order.GetRefitCargo(), 0xFF, false, false, 0)); - cur_company.Restore(); if (cost.Failed()) { _vehicles_to_autoreplace[v->index] = false; diff --git a/src/water_cmd.cpp b/src/water_cmd.cpp index 1bbd1d412e..e45048a9d4 100644 --- a/src/water_cmd.cpp +++ b/src/water_cmd.cpp @@ -1183,7 +1183,7 @@ static void DoFloodTile(TileIndex target) bool flooded = false; // Will be set to true if something is changed. - Backup cur_company(_current_company, OWNER_WATER); + AutoRestoreBackup cur_company(_current_company, OWNER_WATER); Slope tileh = GetTileSlope(target); if (tileh != SLOPE_FLAT) { @@ -1238,8 +1238,6 @@ static void DoFloodTile(TileIndex target) if (IsPossibleDockingTile(target)) CheckForDockingTile(target); InvalidateWaterRegion(target); } - - cur_company.Restore(); } /** @@ -1248,7 +1246,7 @@ static void DoFloodTile(TileIndex target) */ static void DoDryUp(TileIndex tile) { - Backup cur_company(_current_company, OWNER_WATER); + AutoRestoreBackup cur_company(_current_company, OWNER_WATER); switch (GetTileType(tile)) { case TileType::Railway: @@ -1283,8 +1281,6 @@ static void DoDryUp(TileIndex tile) default: NOT_REACHED(); } - - cur_company.Restore(); } /**