From fc4827c89c1db7765db1074c1473459b95a580e3 Mon Sep 17 00:00:00 2001 From: Laserlicht <13953785+Laserlicht@users.noreply.github.com> Date: Wed, 27 Dec 2023 14:39:35 +0100 Subject: [PATCH] option for allowing cheats --- Mods/vcmi/config/vcmi/english.json | 2 ++ Mods/vcmi/config/vcmi/german.json | 2 ++ client/CServerHandler.cpp | 7 +++++++ client/CServerHandler.h | 2 ++ client/lobby/OptionsTabBase.cpp | 4 ++++ config/widgets/turnOptionsTab.json | 21 ++++++++++++++++++-- lib/StartInfo.h | 5 ++++- lib/networkPacks/NetPackVisitor.h | 1 + lib/networkPacks/NetPacksLib.cpp | 5 +++++ lib/networkPacks/PacksForLobby.h | 12 +++++++++++ lib/registerTypes/RegisterTypesLobbyPacks.h | 1 + server/LobbyNetPackVisitors.h | 1 + server/NetPacksLobbyServer.cpp | 6 ++++++ server/processors/PlayerMessageProcessor.cpp | 2 +- 14 files changed, 67 insertions(+), 4 deletions(-) diff --git a/Mods/vcmi/config/vcmi/english.json b/Mods/vcmi/config/vcmi/english.json index fc00410a3..ee8207dec 100644 --- a/Mods/vcmi/config/vcmi/english.json +++ b/Mods/vcmi/config/vcmi/english.json @@ -264,9 +264,11 @@ "vcmi.optionsTab.simturnsMin.hover" : "At least for", "vcmi.optionsTab.simturnsMax.hover" : "At most for", "vcmi.optionsTab.simturnsAI.hover" : "(Experimental) Simultaneous AI Turns", + "vcmi.optionsTab.cheatAllowed.hover" : "Allow cheats", "vcmi.optionsTab.simturnsMin.help" : "Play simultaneously for specified number of days. Contacts between players during this period are blocked", "vcmi.optionsTab.simturnsMax.help" : "Play simultaneously for specified number of days or until contact with another player", "vcmi.optionsTab.simturnsAI.help" : "{Simultaneous AI Turns}\nExperimental option. Allows AI players to act at the same time as human player when simultaneous turns are enabled.", + "vcmi.optionsTab.cheatAllowed.help" : "{Allow cheats}\nAllows the inputs of cheats during the game.", "vcmi.optionsTab.turnTime.select" : "Select turn timer preset", "vcmi.optionsTab.turnTime.unlimited" : "Unlimited turn time", diff --git a/Mods/vcmi/config/vcmi/german.json b/Mods/vcmi/config/vcmi/german.json index 90b0fafb6..09c71c505 100644 --- a/Mods/vcmi/config/vcmi/german.json +++ b/Mods/vcmi/config/vcmi/german.json @@ -283,9 +283,11 @@ "vcmi.optionsTab.simturnsMin.hover" : "Zumindest für", "vcmi.optionsTab.simturnsMax.hover" : "Höchstens für", "vcmi.optionsTab.simturnsAI.hover" : "(Experimentell) Simultane KI Züge", + "vcmi.optionsTab.cheatAllowed.hover" : "Cheats erlauben", "vcmi.optionsTab.simturnsMin.help" : "Spielt gleichzeitig für eine bestimmte Anzahl von Tagen. Die Kontakte zwischen den Spielern sind während dieser Zeit blockiert", "vcmi.optionsTab.simturnsMax.help" : "Spielt gleichzeitig für eine bestimmte Anzahl von Tagen oder bis zum Kontakt mit einem anderen Spieler", "vcmi.optionsTab.simturnsAI.help" : "{Simultane KI Züge}\nExperimentelle Option. Ermöglicht es den KI-Spielern, gleichzeitig mit dem menschlichen Spieler zu agieren, wenn simultane Spielzüge aktiviert sind.", + "vcmi.optionsTab.cheatAllowed.help" : "{Cheats erlauben}\nErlaubt die Eingabe von Cheats während des Spiels.", // Translation note: translate strings below using form that is correct for "0 days", "1 day" and "2 days" in your language // Using this information, VCMI will automatically select correct plural form for every possible amount diff --git a/client/CServerHandler.cpp b/client/CServerHandler.cpp index 2def4c5fa..0fd4f4db6 100644 --- a/client/CServerHandler.cpp +++ b/client/CServerHandler.cpp @@ -507,6 +507,13 @@ void CServerHandler::setTurnTimerInfo(const TurnTimerInfo & info) const sendLobbyPack(lstt); } +void CServerHandler::setCheatAllowedInfo(bool allowed) const +{ + LobbySetCheatAllowed lsca; + lsca.allowed = allowed; + sendLobbyPack(lsca); +} + void CServerHandler::sendMessage(const std::string & txt) const { std::istringstream readed; diff --git a/client/CServerHandler.h b/client/CServerHandler.h index 92ade9162..9d196fab8 100644 --- a/client/CServerHandler.h +++ b/client/CServerHandler.h @@ -72,6 +72,7 @@ public: virtual void setDifficulty(int to) const = 0; virtual void setTurnTimerInfo(const TurnTimerInfo &) const = 0; virtual void setSimturnsInfo(const SimturnsInfo &) const = 0; + virtual void setCheatAllowedInfo(bool allowed) const = 0; virtual void sendMessage(const std::string & txt) const = 0; virtual void sendGuiAction(ui8 action) const = 0; // TODO: possibly get rid of it? virtual void sendStartGame(bool allowOnlyAI = false) const = 0; @@ -158,6 +159,7 @@ public: void setDifficulty(int to) const override; void setTurnTimerInfo(const TurnTimerInfo &) const override; void setSimturnsInfo(const SimturnsInfo &) const override; + void setCheatAllowedInfo(bool allowed) const override; void sendMessage(const std::string & txt) const override; void sendGuiAction(ui8 action) const override; void sendRestartGame() const override; diff --git a/client/lobby/OptionsTabBase.cpp b/client/lobby/OptionsTabBase.cpp index 3dc78819b..914d630f6 100644 --- a/client/lobby/OptionsTabBase.cpp +++ b/client/lobby/OptionsTabBase.cpp @@ -90,6 +90,10 @@ OptionsTabBase::OptionsTabBase(const JsonPath & configPath) CSH->setSimturnsInfo(info); }); + addCallback("setCheatAllowed", [&](int index){ + CSH->setCheatAllowedInfo(index); + }); + addCallback("setTurnTimerAccumulate", [&](int index){ TurnTimerInfo info = SEL->getStartInfo()->turnTimerInfo; info.accumulatingTurnTimer = index; diff --git a/config/widgets/turnOptionsTab.json b/config/widgets/turnOptionsTab.json index 0c0e6c220..f3e367ece 100644 --- a/config/widgets/turnOptionsTab.json +++ b/config/widgets/turnOptionsTab.json @@ -316,7 +316,7 @@ }, { "name": "buttonSimturnsAI", - "position": {"x": 70, "y": 535}, + "position": {"x": 70, "y": 530}, "type": "toggleButton", "image": "lobby/checkbox", "callback" : "setSimturnAI" @@ -328,7 +328,24 @@ "alignment": "left", "color": "yellow", "text": "vcmi.optionsTab.simturnsAI.hover", - "position": {"x": 110, "y": 540} + "position": {"x": 110, "y": 533} + }, + { + "name": "buttonCheatAllowed", + "position": {"x": 70, "y": 555}, + "type": "toggleButton", + "image": "lobby/checkbox", + "callback" : "setCheatAllowed", + "selected" : true + }, + { + "name": "labelCheatAllowed", + "type": "label", + "font": "small", + "alignment": "left", + "color": "yellow", + "text": "vcmi.optionsTab.cheatAllowed.hover", + "position": {"x": 110, "y": 558} } ], diff --git a/lib/StartInfo.h b/lib/StartInfo.h index 86b5d6815..f60bb3c10 100644 --- a/lib/StartInfo.h +++ b/lib/StartInfo.h @@ -105,6 +105,8 @@ struct DLL_LINKAGE StartInfo EMode mode; ui8 difficulty; //0=easy; 4=impossible + bool cheatAllowed; + using TPlayerInfos = std::map; TPlayerInfos playerInfos; //color indexed @@ -141,13 +143,14 @@ struct DLL_LINKAGE StartInfo h & fileURI; h & simturnsInfo; h & turnTimerInfo; + h & cheatAllowed; h & mapname; h & mapGenOptions; h & campState; } StartInfo() : mode(INVALID), difficulty(1), seedToBeUsed(0), seedPostInit(0), - mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI("") + mapfileChecksum(0), startTimeIso8601(vstd::getDateTimeISO8601Basic(std::time(nullptr))), fileURI(""), cheatAllowed(true) { } diff --git a/lib/networkPacks/NetPackVisitor.h b/lib/networkPacks/NetPackVisitor.h index 05df2897a..94a24c533 100644 --- a/lib/networkPacks/NetPackVisitor.h +++ b/lib/networkPacks/NetPackVisitor.h @@ -164,6 +164,7 @@ public: virtual void visitLobbySetPlayerName(LobbySetPlayerName & pack) {} virtual void visitLobbySetSimturns(LobbySetSimturns & pack) {} virtual void visitLobbySetTurnTime(LobbySetTurnTime & pack) {} + virtual void visitLobbySetCheatAllowed(LobbySetCheatAllowed & pack) {} virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) {} virtual void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) {} virtual void visitLobbyShowMessage(LobbyShowMessage & pack) {} diff --git a/lib/networkPacks/NetPacksLib.cpp b/lib/networkPacks/NetPacksLib.cpp index a0f3656e8..08134f3aa 100644 --- a/lib/networkPacks/NetPacksLib.cpp +++ b/lib/networkPacks/NetPacksLib.cpp @@ -770,6 +770,11 @@ void LobbySetTurnTime::visitTyped(ICPackVisitor & visitor) visitor.visitLobbySetTurnTime(*this); } +void LobbySetCheatAllowed::visitTyped(ICPackVisitor & visitor) +{ + visitor.visitLobbySetCheatAllowed(*this); +} + void LobbySetDifficulty::visitTyped(ICPackVisitor & visitor) { visitor.visitLobbySetDifficulty(*this); diff --git a/lib/networkPacks/PacksForLobby.h b/lib/networkPacks/PacksForLobby.h index 6081f7279..0abda3c24 100644 --- a/lib/networkPacks/PacksForLobby.h +++ b/lib/networkPacks/PacksForLobby.h @@ -287,6 +287,18 @@ struct DLL_LINKAGE LobbySetTurnTime : public CLobbyPackToServer } }; +struct DLL_LINKAGE LobbySetCheatAllowed : public CLobbyPackToServer +{ + bool allowed; + + void visitTyped(ICPackVisitor & visitor) override; + + template void serialize(Handler &h, const int version) + { + h & allowed; + } +}; + struct DLL_LINKAGE LobbySetDifficulty : public CLobbyPackToServer { ui8 difficulty = 0; diff --git a/lib/registerTypes/RegisterTypesLobbyPacks.h b/lib/registerTypes/RegisterTypesLobbyPacks.h index 6e20ee244..9028229cf 100644 --- a/lib/registerTypes/RegisterTypesLobbyPacks.h +++ b/lib/registerTypes/RegisterTypesLobbyPacks.h @@ -54,6 +54,7 @@ void registerTypesLobbyPacks(Serializer &s) s.template registerType(); s.template registerType(); s.template registerType(); + s.template registerType(); s.template registerType(); s.template registerType(); s.template registerType(); diff --git a/server/LobbyNetPackVisitors.h b/server/LobbyNetPackVisitors.h index 5ee02504c..4b93ea26c 100644 --- a/server/LobbyNetPackVisitors.h +++ b/server/LobbyNetPackVisitors.h @@ -88,6 +88,7 @@ public: virtual void visitLobbySetPlayer(LobbySetPlayer & pack) override; virtual void visitLobbySetPlayerName(LobbySetPlayerName & pack) override; virtual void visitLobbySetTurnTime(LobbySetTurnTime & pack) override; + virtual void visitLobbySetCheatAllowed(LobbySetCheatAllowed & pack) override; virtual void visitLobbySetSimturns(LobbySetSimturns & pack) override; virtual void visitLobbySetDifficulty(LobbySetDifficulty & pack) override; virtual void visitLobbyForceSetPlayer(LobbyForceSetPlayer & pack) override; diff --git a/server/NetPacksLobbyServer.cpp b/server/NetPacksLobbyServer.cpp index 63258d471..787d8a63a 100644 --- a/server/NetPacksLobbyServer.cpp +++ b/server/NetPacksLobbyServer.cpp @@ -414,6 +414,12 @@ void ApplyOnServerNetPackVisitor::visitLobbySetTurnTime(LobbySetTurnTime & pack) result = true; } +void ApplyOnServerNetPackVisitor::visitLobbySetCheatAllowed(LobbySetCheatAllowed & pack) +{ + srv.si->cheatAllowed = pack.allowed; + result = true; +} + void ApplyOnServerNetPackVisitor::visitLobbySetDifficulty(LobbySetDifficulty & pack) { srv.si->difficulty = std::clamp(pack.difficulty, 0, 4); diff --git a/server/processors/PlayerMessageProcessor.cpp b/server/processors/PlayerMessageProcessor.cpp index afb331148..94a9ca789 100644 --- a/server/processors/PlayerMessageProcessor.cpp +++ b/server/processors/PlayerMessageProcessor.cpp @@ -440,7 +440,7 @@ bool PlayerMessageProcessor::handleCheatCode(const std::string & cheat, PlayerCo std::vector words; boost::split(words, cheat, boost::is_any_of("\t\r\n ")); - if (words.empty()) + if (words.empty() || !gameHandler->getStartInfo()->cheatAllowed) return false; //Make cheat name case-insensitive, but keep words/parameters (e.g. creature name) as it