-
Notifications
You must be signed in to change notification settings - Fork 87
Refactoring of existing cheats (see #1679) #1688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Flamefire
merged 18 commits into
Return-To-The-Roots:master
from
kubaau:cheats_refactoring
Dec 14, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
5d3eef8
Move existing cheat logic to new class "Cheats"
kubaau d828a0c
Move tracking key events and chat commands from Cheats to separate cl…
kubaau 435960e
Make "winter" cheat string tracking more similar to what it was in S2
kubaau 1c60ac0
Disable cheats in multiplayer
kubaau bd0d941
Do not enable armageddon if cheat mode is not on
kubaau 13d6f49
Do not enable toggleHumanAIPlayer simply by running debug build
kubaau 64b1600
Apply review remarks:
kubaau 0cc808d
Move Cheats out of GameWorld and into GameInterface
kubaau 92e9c95
Move CheatCommandTracker out of Cheats
kubaau 92d7a0d
Rename "track" function prefix in CheatCommandTracker to "on" and "ch…
kubaau 543514a
Mark cheats-related tests with proper BOOST_AUTO_TEST_SUITE
kubaau 503fa37
Fix Linux UT compilation for Cheats
kubaau 5059273
clang-tidy fixes for cheats
kubaau c8387ec
Remove useless GI_GetCheats() indirection inside dskGameInterface
kubaau e761d04
Remove cheats stuff from GameWorldBase - not needed yet
kubaau c258780
Remove GI_GetCheats - not needed yet
kubaau 7e31a47
Fix CheatModeIsNotTurnedOn_WhenOrderOfCharactersIsWrong_Wraparound logic
kubaau 9e790e3
Simplify logic related to cheats being disallowed in multiplayer
kubaau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| // Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "CheatCommandTracker.h" | ||
| #include "Cheats.h" | ||
| #include "driver/KeyEvent.h" | ||
|
|
||
| namespace { | ||
| auto makeCircularBuffer(const std::string& str) | ||
| { | ||
| return boost::circular_buffer<char>{cbegin(str), cend(str)}; | ||
| } | ||
| const auto enableCheatsStr = makeCircularBuffer("winter"); | ||
| } // namespace | ||
|
|
||
| CheatCommandTracker::CheatCommandTracker(Cheats& cheats) : cheats_(cheats), lastChars_(enableCheatsStr.size()) {} | ||
|
|
||
| void CheatCommandTracker::onKeyEvent(const KeyEvent& ke) | ||
| { | ||
| if(!cheats_.areCheatsAllowed()) | ||
| return; | ||
|
|
||
| if(checkSpecialKeyEvent(ke)) | ||
| lastChars_.clear(); | ||
| else | ||
| onCharKeyEvent(ke); | ||
| } | ||
|
|
||
| void CheatCommandTracker::onChatCommand(const std::string& cmd) | ||
| { | ||
| if(!cheats_.areCheatsAllowed()) | ||
| return; | ||
|
|
||
| if(cmd == "apocalypsis") | ||
| cheats_.armageddon(); | ||
| } | ||
|
|
||
| bool CheatCommandTracker::checkSpecialKeyEvent(const KeyEvent& ke) | ||
| { | ||
| if(ke.kt == KeyType::Char) | ||
| return false; | ||
|
|
||
| switch(ke.kt) | ||
| { | ||
| case KeyType::F10: cheats_.toggleHumanAIPlayer(); break; | ||
| default: break; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| void CheatCommandTracker::onCharKeyEvent(const KeyEvent& ke) | ||
| { | ||
| lastChars_.push_back(ke.c); | ||
|
|
||
| if(lastChars_ == enableCheatsStr) | ||
| cheats_.toggleCheatMode(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <boost/circular_buffer.hpp> | ||
| #include <string> | ||
|
|
||
| class Cheats; | ||
| struct KeyEvent; | ||
|
|
||
| class CheatCommandTracker | ||
| { | ||
| public: | ||
| CheatCommandTracker(Cheats& cheats); | ||
|
|
||
| void onKeyEvent(const KeyEvent& ke); | ||
| void onChatCommand(const std::string& cmd); | ||
|
|
||
| private: | ||
| bool checkSpecialKeyEvent(const KeyEvent& ke); | ||
| void onCharKeyEvent(const KeyEvent& ke); | ||
|
|
||
| Cheats& cheats_; | ||
| boost::circular_buffer<char> lastChars_; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "Cheats.h" | ||
| #include "network/GameClient.h" | ||
| #include "world/GameWorldBase.h" | ||
|
|
||
| Cheats::Cheats(GameWorldBase& world) : world_(world) {} | ||
|
|
||
| bool Cheats::areCheatsAllowed() const | ||
| { | ||
| return world_.IsSinglePlayer(); | ||
| } | ||
|
|
||
| void Cheats::toggleCheatMode() | ||
| { | ||
| isCheatModeOn_ = !isCheatModeOn_; | ||
| } | ||
|
|
||
| void Cheats::toggleHumanAIPlayer() const | ||
| { | ||
| if(isCheatModeOn() && !GAMECLIENT.IsReplayModeOn()) | ||
| GAMECLIENT.ToggleHumanAIPlayer(AI::Info{AI::Type::Default, AI::Level::Easy}); | ||
| } | ||
|
|
||
| void Cheats::armageddon() const | ||
| { | ||
| if(isCheatModeOn()) | ||
| GAMECLIENT.CheatArmageddon(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #pragma once | ||
|
|
||
| class GameWorldBase; | ||
|
|
||
| class Cheats | ||
| { | ||
| public: | ||
| Cheats(GameWorldBase& world); | ||
|
|
||
| bool areCheatsAllowed() const; | ||
|
|
||
| void toggleCheatMode(); | ||
| bool isCheatModeOn() const { return isCheatModeOn_; } | ||
|
|
||
| void toggleHumanAIPlayer() const; | ||
| void armageddon() const; | ||
|
|
||
| private: | ||
| bool isCheatModeOn_ = false; | ||
| GameWorldBase& world_; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright (C) 2024 Settlers Freaks (sf-team at siedler25.org) | ||
| // | ||
| // SPDX-License-Identifier: GPL-2.0-or-later | ||
|
|
||
| #include "CheatCommandTracker.h" | ||
| #include "Cheats.h" | ||
| #include "driver/KeyEvent.h" | ||
| #include "worldFixtures/CreateEmptyWorld.h" | ||
| #include "worldFixtures/WorldFixture.h" | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(CheatCommandTrackerTests) | ||
|
|
||
| namespace { | ||
| template<unsigned T_numPlayers> | ||
| struct CheatCommandTrackerFixture : WorldFixture<CreateEmptyWorld, T_numPlayers> | ||
| { | ||
| Cheats cheats_{this->world}; | ||
| CheatCommandTracker tracker_{cheats_}; | ||
|
|
||
| KeyEvent makeKeyEvent(unsigned c) { return {KeyType::Char, c, false, false, false}; } | ||
| KeyEvent makeKeyEvent(KeyType kt) { return {kt, 0, false, false, false}; } | ||
|
|
||
| void trackString(const std::string& str) | ||
| { | ||
| for(char c : str) | ||
| tracker_.onKeyEvent(makeKeyEvent(c)); | ||
| } | ||
| }; | ||
| using CheatCommandTrackerFixture1P = CheatCommandTrackerFixture<1>; | ||
| using CheatCommandTrackerFixture2P = CheatCommandTrackerFixture<2>; | ||
| } // namespace | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsOffByDefault, CheatCommandTrackerFixture1P) | ||
| { | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOn, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeCannotBeTurnedOn_InMultiplayer, CheatCommandTrackerFixture2P) | ||
| { | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOff, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeCanBeTurnedOnAndOffRepeatedly, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenIncomplete, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winte"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenInterruptedByAnotherKeyType, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("win"); | ||
| tracker_.onKeyEvent(makeKeyEvent(KeyType::F10)); | ||
| trackString("ter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenInterruptedByAnotherLetter, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("wainter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenOrderOfCharactersIsWrong, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winetr"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenOrderOfCharactersIsWrong_Wraparound, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("rwinte"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsNotTurnedOn_WhenACharacterIsRepeated, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("winnter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsTurnedOn_WhenTheFirstCharacterIsRepeated, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("wwwinter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| } | ||
|
|
||
| BOOST_FIXTURE_TEST_CASE(CheatModeIsTurnedOn_EvenWhenWrongInputsWereProvidedBefore, CheatCommandTrackerFixture1P) | ||
| { | ||
| trackString("www"); | ||
| auto ke = makeKeyEvent('1'); | ||
| ke.alt = true; | ||
| tracker_.onKeyEvent(ke); | ||
| trackString("interwitter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == false); | ||
| trackString("winter"); | ||
| BOOST_TEST_REQUIRE(cheats_.isCheatModeOn() == true); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.