Skip to content

Commit 828eaad

Browse files
authored
Merge pull request #1723 from kubaau/cheat_enable_buildings
Add cheat to enable all buildings
2 parents 6b51676 + 293be34 commit 828eaad

File tree

6 files changed

+77
-15
lines changed

6 files changed

+77
-15
lines changed

libs/s25main/CheatCommandTracker.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ void CheatCommandTracker::onChatCommand(const std::string& cmd)
3434

3535
if(cmd == "apocalypsis")
3636
cheats_.armageddon();
37+
else if(cmd == "impulse9")
38+
cheats_.toggleAllBuildingsEnabled();
3739
}
3840

3941
bool CheatCommandTracker::checkSpecialKeyEvent(const KeyEvent& ke)

libs/s25main/Cheats.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,31 +16,58 @@ bool Cheats::areCheatsAllowed() const
1616

1717
void Cheats::toggleCheatMode()
1818
{
19+
// In S2, if you enabled cheat mode, revealed the map and disabled cheat mode, the map would remain revealed and you
20+
// would be unable to unreveal the map.
21+
// In RTTR, disabling cheat mode turns all cheats off and they have to be turned on again manually.
22+
if(isCheatModeOn_)
23+
turnAllCheatsOff();
24+
1925
isCheatModeOn_ = !isCheatModeOn_;
2026
}
2127

2228
void Cheats::toggleAllVisible()
2329
{
24-
// In the original game if you enabled cheats, revealed the map and disabled cheats, you would be unable to unreveal
25-
// the map. In RTTR, with cheats disabled, you can unreveal the map but cannot reveal it.
26-
if(isCheatModeOn() || isAllVisible())
30+
if(isCheatModeOn())
2731
{
2832
isAllVisible_ = !isAllVisible_;
2933

30-
// The minimap in the original game is not updated immediately, but in RTTR this would mess up the minimap.
34+
// In S2, the minimap is not updated immediately.
35+
// In RTTR, the minimap would become messed up if it wasn't updated here.
3136
if(GameInterface* gi = world_.GetGameInterface())
3237
gi->GI_UpdateMapVisibility();
3338
}
3439
}
3540

36-
void Cheats::toggleHumanAIPlayer() const
41+
void Cheats::toggleAllBuildingsEnabled()
42+
{
43+
// In S2, if you enabled cheats you would automatically have all buildings enabled.
44+
// In RTTR, because this may have unintended consequences when playing campaigns, the user must explicitly enable
45+
// all buildings after enabling cheats.
46+
if(isCheatModeOn())
47+
areAllBuildingsEnabled_ = !areAllBuildingsEnabled_;
48+
}
49+
50+
void Cheats::toggleHumanAIPlayer()
3751
{
3852
if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn())
53+
{
3954
GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy});
55+
isHumanAIPlayer_ = !isHumanAIPlayer_;
56+
}
4057
}
4158

4259
void Cheats::armageddon() const
4360
{
4461
if(isCheatModeOn())
4562
GAMECLIENT.CheatArmageddon();
4663
}
64+
65+
void Cheats::turnAllCheatsOff()
66+
{
67+
if(isAllVisible_)
68+
toggleAllVisible();
69+
if(areAllBuildingsEnabled_)
70+
toggleAllBuildingsEnabled();
71+
if(isHumanAIPlayer_)
72+
toggleHumanAIPlayer();
73+
}

libs/s25main/Cheats.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ class Cheats
2020
void toggleAllVisible();
2121
bool isAllVisible() const { return isAllVisible_; }
2222

23+
void toggleAllBuildingsEnabled();
24+
bool areAllBuildingsEnabled() const { return areAllBuildingsEnabled_; }
25+
2326
// RTTR cheats
24-
void toggleHumanAIPlayer() const;
27+
void toggleHumanAIPlayer();
2528
void armageddon() const;
2629

2730
private:
31+
void turnAllCheatsOff();
32+
2833
bool isCheatModeOn_ = false;
2934
bool isAllVisible_ = false;
35+
bool areAllBuildingsEnabled_ = false;
36+
bool isHumanAIPlayer_ = false;
3037
GameWorldBase& world_;
3138
};

libs/s25main/GamePlayer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: GPL-2.0-or-later
44

55
#include "GamePlayer.h"
6+
#include "Cheats.h"
67
#include "EventManager.h"
78
#include "FindWhConditions.h"
89
#include "GameInterface.h"
@@ -2254,6 +2255,11 @@ void GamePlayer::Trade(nobBaseWarehouse* goalWh, const boost_variant2<GoodType,
22542255
}
22552256
}
22562257

2258+
bool GamePlayer::IsBuildingEnabled(BuildingType type) const
2259+
{
2260+
return building_enabled[type] || (isHuman() && world.GetGameInterface()->GI_GetCheats().areAllBuildingsEnabled());
2261+
}
2262+
22572263
void GamePlayer::FillVisualSettings(VisualSettings& visualSettings) const
22582264
{
22592265
Distributions& visDistribution = visualSettings.distribution;

libs/s25main/GamePlayer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ class GamePlayer : public GamePlayerInfo
285285

286286
void EnableBuilding(BuildingType type) { building_enabled[type] = true; }
287287
void DisableBuilding(BuildingType type) { building_enabled[type] = false; }
288-
bool IsBuildingEnabled(BuildingType type) const { return building_enabled[type]; }
288+
bool IsBuildingEnabled(BuildingType type) const;
289289
/// Set the area the player may have territory in
290290
/// Nothing means all is allowed. See Lua description
291291
std::vector<MapPoint>& GetRestrictedArea() { return restricted_area; }

tests/s25Main/integration/testCheats.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ BOOST_FIXTURE_TEST_CASE(CanToggleCheatModeOnAndOffRepeatedly, CheatsFixture)
4444
BOOST_TEST_REQUIRE(cheats.isCheatModeOn() == false);
4545
}
4646

47+
BOOST_FIXTURE_TEST_CASE(TurningCheatModeOffDisablesAllCheats, CheatsFixture)
48+
{
49+
cheats.toggleCheatMode();
50+
cheats.toggleAllVisible();
51+
BOOST_TEST_REQUIRE(cheats.isAllVisible() == true);
52+
cheats.toggleAllBuildingsEnabled();
53+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true);
54+
cheats.toggleCheatMode();
55+
BOOST_TEST_REQUIRE(cheats.isAllVisible() == false);
56+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
57+
// testing toggleHumanAIPlayer would require GameClient::state==Loaded, which is guaranteed in code (because Cheats
58+
// only exist after the game is loaded) but is not the case in tests - skipping
59+
}
60+
4761
namespace {
4862
MOCK_BASE_CLASS(MockGameInterface, GameInterface)
4963
{
@@ -65,11 +79,11 @@ MOCK_BASE_CLASS(MockGameInterface, GameInterface)
6579
};
6680
} // namespace
6781

68-
BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn_ButOnlyDisableAllVisible_IfCheatModeIsNotOn, CheatsFixture)
82+
BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn, CheatsFixture)
6983
{
7084
MockGameInterface mgi;
7185
MOCK_EXPECT(mgi.GI_GetCheats).returns(std::ref(gameDesktop.GI_GetCheats()));
72-
MOCK_EXPECT(mgi.GI_UpdateMapVisibility).exactly(4); // because the actual toggling should occur 4 times
86+
MOCK_EXPECT(mgi.GI_UpdateMapVisibility).exactly(3); // how many toggles should actually occur
7387
world.SetGameInterface(&mgi);
7488

7589
MapPoint farawayPos = p1HQPos;
@@ -88,17 +102,23 @@ BOOST_FIXTURE_TEST_CASE(CanToggleAllVisible_IfCheatModeIsOn_ButOnlyDisableAllVis
88102
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == true);
89103

90104
cheats.toggleAllVisible();
91-
// now not visible
92105
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == false);
93-
94106
cheats.toggleAllVisible();
95-
// visible again
96107
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == true);
108+
}
97109

110+
BOOST_FIXTURE_TEST_CASE(CanToggleAllBuildingsEnabled_IfCheatModeIsOn, CheatsFixture)
111+
{
112+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
113+
cheats.toggleAllBuildingsEnabled();
114+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
98115
cheats.toggleCheatMode();
99-
cheats.toggleAllVisible();
100-
// again not visible, despite cheat mode being off
101-
BOOST_TEST_REQUIRE((viewer.GetVisibility(farawayPos) == Visibility::Visible) == false);
116+
cheats.toggleAllBuildingsEnabled();
117+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true);
118+
cheats.toggleAllBuildingsEnabled();
119+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == false);
120+
cheats.toggleAllBuildingsEnabled();
121+
BOOST_TEST_REQUIRE(cheats.areAllBuildingsEnabled() == true);
102122
}
103123

104124
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)