Skip to content

Commit

Permalink
Add cheat to enable all buildings
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaau committed Dec 17, 2024
1 parent 17844c8 commit ac3d720
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions libs/s25main/CheatCommandTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions libs/s25main/Cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions libs/s25main/Cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ class Cheats
void toggleAllVisible();
bool isAllVisible() const { return isAllVisible_; }

void toggleAllBuildingsEnabled();
bool areAllBuildingsEnabled() const { return areAllBuildingsEnabled_; }

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

private:
bool isCheatModeOn_ = false;
bool isAllVisible_ = false;
bool areAllBuildingsEnabled_ = false;
GameWorldBase& world_;
};
6 changes: 6 additions & 0 deletions libs/s25main/GamePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -2254,6 +2255,11 @@ void GamePlayer::Trade(nobBaseWarehouse* goalWh, const boost_variant2<GoodType,
}
}

bool GamePlayer::IsBuildingEnabled(BuildingType type) const
{
return building_enabled[type] || (isHuman() && world.GetGameInterface()->GI_GetCheats().areAllBuildingsEnabled());
}

void GamePlayer::FillVisualSettings(VisualSettings& visualSettings) const
{
Distributions& visDistribution = visualSettings.distribution;
Expand Down
2 changes: 1 addition & 1 deletion libs/s25main/GamePlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<MapPoint>& GetRestrictedArea() { return restricted_area; }
Expand Down
18 changes: 18 additions & 0 deletions tests/s25Main/integration/testCheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit ac3d720

Please sign in to comment.