Skip to content

Commit

Permalink
Turn all cheats off when cheat mode is toggled off
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaau committed Dec 17, 2024
1 parent ac3d720 commit e36a78e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
30 changes: 23 additions & 7 deletions libs/s25main/Cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ bool Cheats::areCheatsAllowed() const

void Cheats::toggleCheatMode()
{
// In S2, if you enabled cheat mode, revealed the map and disabled cheat mode, the map would remain revealed and you
// would be unable to unreveal the map.
// In RTTR, disabling cheat mode turns all cheats off and they have to be turned on again manually.
if(isCheatModeOn_)
turnAllCheatsOff();

isCheatModeOn_ = !isCheatModeOn_;
}

void Cheats::toggleAllVisible()
{
// 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())
if(isCheatModeOn())
{
isAllVisible_ = !isAllVisible_;

Expand All @@ -38,20 +42,32 @@ 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())
// all buildings after enabling cheats.
if(isCheatModeOn())
areAllBuildingsEnabled_ = !areAllBuildingsEnabled_;
}

void Cheats::toggleHumanAIPlayer() const
void Cheats::toggleHumanAIPlayer()
{
if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn())
{
GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy});
isHumanAIPlayer_ = !isHumanAIPlayer_;

Check warning on line 55 in libs/s25main/Cheats.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/Cheats.cpp#L55

Added line #L55 was not covered by tests
}
}

void Cheats::armageddon() const
{
if(isCheatModeOn())
GAMECLIENT.CheatArmageddon();
}

void Cheats::turnAllCheatsOff()
{
if(isAllVisible_)
toggleAllVisible();
if(areAllBuildingsEnabled_)
toggleAllBuildingsEnabled();
if(isHumanAIPlayer_)
toggleHumanAIPlayer();

Check warning on line 72 in libs/s25main/Cheats.cpp

View check run for this annotation

Codecov / codecov/patch

libs/s25main/Cheats.cpp#L72

Added line #L72 was not covered by tests
}
5 changes: 4 additions & 1 deletion libs/s25main/Cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@ class Cheats
bool areAllBuildingsEnabled() const { return areAllBuildingsEnabled_; }

// RTTR cheats
void toggleHumanAIPlayer() const;
void toggleHumanAIPlayer();
void armageddon() const;

private:
void turnAllCheatsOff();

bool isCheatModeOn_ = false;
bool isAllVisible_ = false;
bool areAllBuildingsEnabled_ = false;
bool isHumanAIPlayer_ = false;
GameWorldBase& world_;
};
32 changes: 17 additions & 15 deletions tests/s25Main/integration/testCheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,20 @@ BOOST_FIXTURE_TEST_CASE(CanToggleCheatModeOnAndOffRepeatedly, CheatsFixture)
BOOST_TEST_REQUIRE(cheats.isCheatModeOn() == false);
}

BOOST_FIXTURE_TEST_CASE(TurningCheatModeOffDisablesAllCheats, CheatsFixture)
{
cheats.toggleCheatMode();
cheats.toggleAllVisible();
BOOST_TEST_REQUIRE(cheats.isAllVisible() == true);
cheats.toggleAllBuildingsEnabled();
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true);
cheats.toggleCheatMode();
BOOST_TEST_REQUIRE(cheats.isAllVisible() == false);
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
// testing toggleHumanAIPlayer would require GameClient::state==Loaded, which is guaranteed in code (because Cheats
// only exist after the game is loaded) but is not the case in tests - skipping
}

namespace {
MOCK_BASE_CLASS(MockGameInterface, GameInterface)
{
Expand All @@ -65,11 +79,11 @@ MOCK_BASE_CLASS(MockGameInterface, GameInterface)
};
} // namespace

BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn_ButOnlyDisableAllVisible_IfCheatModeIsNotOn, CheatsFixture)
BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn, CheatsFixture)
{
MockGameInterface mgi;
MOCK_EXPECT(mgi.GI_GetCheats).returns(std::ref(gameDesktop.GI_GetCheats()));
MOCK_EXPECT(mgi.GI_UpdateMapVisibility).exactly(4); // because the actual toggling should occur 4 times
MOCK_EXPECT(mgi.GI_UpdateMapVisibility).exactly(3); // how many toggles should actually occur
world.SetGameInterface(&mgi);

MapPoint farawayPos = p1HQPos;
Expand All @@ -88,21 +102,12 @@ BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn_ButOnlyDisableAllVis
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == true);

cheats.toggleAllVisible();
// now not visible
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == false);

cheats.toggleAllVisible();
// visible again
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == true);

cheats.toggleCheatMode();
cheats.toggleAllVisible();
// again not visible, despite cheat mode being off
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == false);
}

BOOST_FIXTURE_TEST_CASE(CanToggleAllBuildingsEnabled_IfCheatModeIsOn_ButOnlyDisableThisFeature_IfCheatModeIsNotOn,
CheatsFixture)
BOOST_FIXTURE_TEST_CASE(CanToggleAllBuildingsEnabled_IfCheatModeIsOn, CheatsFixture)
{
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
cheats.toggleAllBuildingsEnabled();
Expand All @@ -114,9 +119,6 @@ BOOST_FIXTURE_TEST_CASE(CanToggleAllBuildingsEnabled_IfCheatModeIsOn_ButOnlyDisa
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()

0 comments on commit e36a78e

Please sign in to comment.