From ac3d7203cd2707390f73175f311a9c1246364237 Mon Sep 17 00:00:00 2001 From: Jakub Audykowicz Date: Sat, 14 Dec 2024 20:23:35 +0100 Subject: [PATCH] Add cheat to enable all buildings --- libs/s25main/CheatCommandTracker.cpp | 2 ++ libs/s25main/Cheats.cpp | 17 ++++++++++++++--- libs/s25main/Cheats.h | 4 ++++ libs/s25main/GamePlayer.cpp | 6 ++++++ libs/s25main/GamePlayer.h | 2 +- tests/s25Main/integration/testCheats.cpp | 18 ++++++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/libs/s25main/CheatCommandTracker.cpp b/libs/s25main/CheatCommandTracker.cpp index a74733721d..f1b4e1aceb 100644 --- a/libs/s25main/CheatCommandTracker.cpp +++ b/libs/s25main/CheatCommandTracker.cpp @@ -34,6 +34,8 @@ void CheatCommandTracker::onChatCommand(const std::string& cmd) if(cmd == "apocalypsis") cheats_.armageddon(); + else if(cmd == "impulse9") + cheats_.toggleAllBuildingsEnabled(); } bool CheatCommandTracker::checkSpecialKeyEvent(const KeyEvent& ke) diff --git a/libs/s25main/Cheats.cpp b/libs/s25main/Cheats.cpp index d86f5d2b3d..e875da0810 100644 --- a/libs/s25main/Cheats.cpp +++ b/libs/s25main/Cheats.cpp @@ -21,18 +21,29 @@ void Cheats::toggleCheatMode() void Cheats::toggleAllVisible() { - // In the original game if you enabled cheats, revealed the map and disabled cheats, you would be unable to unreveal - // the map. In RTTR, with cheats disabled, you can unreveal the map but cannot reveal it. + // In S2, if you enabled cheats, revealed the map and disabled cheats, you would be unable to unreveal the map. + // In RTTR, with cheats disabled, you can unreveal the map but cannot reveal it. if(isCheatModeOn() || isAllVisible()) { isAllVisible_ = !isAllVisible_; - // The minimap in the original game is not updated immediately, but in RTTR this would mess up the minimap. + // In S2, the minimap is not updated immediately. + // In RTTR, the minimap would become messed up if it wasn't updated here. if(GameInterface* gi = world_.GetGameInterface()) gi->GI_UpdateMapVisibility(); } } +void Cheats::toggleAllBuildingsEnabled() +{ + // In S2, if you enabled cheats you would automatically have all buildings enabled. + // In RTTR, because this may have unintended consequences when playing campaigns, the user must explicitly enable + // all buildings after enabling cheats. This function follows the same logic as toggleAllVisible in that it will + // allow disabling this feature even when cheats are off. + if(isCheatModeOn() || areAllBuildingsEnabled()) + areAllBuildingsEnabled_ = !areAllBuildingsEnabled_; +} + void Cheats::toggleHumanAIPlayer() const { if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn()) diff --git a/libs/s25main/Cheats.h b/libs/s25main/Cheats.h index c59e384d70..a8b776926d 100644 --- a/libs/s25main/Cheats.h +++ b/libs/s25main/Cheats.h @@ -20,6 +20,9 @@ class Cheats void toggleAllVisible(); bool isAllVisible() const { return isAllVisible_; } + void toggleAllBuildingsEnabled(); + bool areAllBuildingsEnabled() const { return areAllBuildingsEnabled_; } + // RTTR cheats void toggleHumanAIPlayer() const; void armageddon() const; @@ -27,5 +30,6 @@ class Cheats private: bool isCheatModeOn_ = false; bool isAllVisible_ = false; + bool areAllBuildingsEnabled_ = false; GameWorldBase& world_; }; diff --git a/libs/s25main/GamePlayer.cpp b/libs/s25main/GamePlayer.cpp index d00de03b98..18c09f3102 100644 --- a/libs/s25main/GamePlayer.cpp +++ b/libs/s25main/GamePlayer.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "GamePlayer.h" +#include "Cheats.h" #include "EventManager.h" #include "FindWhConditions.h" #include "GameInterface.h" @@ -2254,6 +2255,11 @@ void GamePlayer::Trade(nobBaseWarehouse* goalWh, const boost_variant2GI_GetCheats().areAllBuildingsEnabled()); +} + void GamePlayer::FillVisualSettings(VisualSettings& visualSettings) const { Distributions& visDistribution = visualSettings.distribution; diff --git a/libs/s25main/GamePlayer.h b/libs/s25main/GamePlayer.h index 59ee843473..a646a12fd9 100644 --- a/libs/s25main/GamePlayer.h +++ b/libs/s25main/GamePlayer.h @@ -285,7 +285,7 @@ class GamePlayer : public GamePlayerInfo void EnableBuilding(BuildingType type) { building_enabled[type] = true; } void DisableBuilding(BuildingType type) { building_enabled[type] = false; } - bool IsBuildingEnabled(BuildingType type) const { return building_enabled[type]; } + bool IsBuildingEnabled(BuildingType type) const; /// Set the area the player may have territory in /// Nothing means all is allowed. See Lua description std::vector& GetRestrictedArea() { return restricted_area; } diff --git a/tests/s25Main/integration/testCheats.cpp b/tests/s25Main/integration/testCheats.cpp index bbcf968fd4..b3967428cf 100644 --- a/tests/s25Main/integration/testCheats.cpp +++ b/tests/s25Main/integration/testCheats.cpp @@ -101,4 +101,22 @@ BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn_ButOnlyDisableAllVis BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == false); } +BOOST_FIXTURE_TEST_CASE(CanToggleAllBuildingsEnabled_IfCheatModeIsOn_ButOnlyDisableThisFeature_IfCheatModeIsNotOn, + CheatsFixture) +{ + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false); + cheats.toggleAllBuildingsEnabled(); + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false); + cheats.toggleCheatMode(); + cheats.toggleAllBuildingsEnabled(); + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true); + cheats.toggleAllBuildingsEnabled(); + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false); + cheats.toggleAllBuildingsEnabled(); + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true); + cheats.toggleCheatMode(); + cheats.toggleAllBuildingsEnabled(); + BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false); +} + BOOST_AUTO_TEST_SUITE_END()