diff --git a/source/game/ui/console_commands.cpp b/source/game/ui/console_commands.cpp index 6e49f37..b47a40a 100644 --- a/source/game/ui/console_commands.cpp +++ b/source/game/ui/console_commands.cpp @@ -844,62 +844,58 @@ static auto cmdHide(const std::vector& args) -> std::string { } // CHEAT [subcomando] -static auto cmdCheat(const std::vector& args) -> std::string { // NOLINT(readability-function-cognitive-complexity) +// Apply ON/OFF/toggle a un cheat binari. mode="" → toggle; "ON"/"OFF" → estableix; altra cosa → cadena buida (usage error). +static auto applyCheatToggle(Options::Cheat::State& cheat, std::string_view mode, std::string_view label) -> std::string { + using State = Options::Cheat::State; + if (mode.empty()) { + cheat = (cheat == State::ENABLED) ? State::DISABLED : State::ENABLED; + } else if (mode == "ON") { + if (cheat == State::ENABLED) { return std::string(label) + " already ON"; } + cheat = State::ENABLED; + } else if (mode == "OFF") { + if (cheat == State::DISABLED) { return std::string(label) + " already OFF"; } + cheat = State::DISABLED; + } else { + return {}; // sentinel: mode invàlid + } + return std::string(label) + " " + (cheat == State::ENABLED ? "ON" : "OFF"); +} + +// CHEAT OPEN/CLOSE THE JAIL — comprova "the jail" i estableix l'estat +static auto cmdCheatJail(const std::vector& args, bool open) -> std::string { + const std::string_view ACTION = open ? "open" : "close"; + if (args.size() < 3 || args[1] != "THE" || args[2] != "JAIL") { + return std::string("usage: cheat ") + std::string(ACTION) + " the jail"; + } + using State = Options::Cheat::State; + auto& jail = Options::cheats.jail_is_open; + const State TARGET = open ? State::ENABLED : State::DISABLED; + if (jail == TARGET) { + return open ? "Jail already open" : "Jail already closed"; + } + jail = TARGET; + return open ? "Jail opened" : "Jail closed"; +} + +static auto cmdCheat(const std::vector& args) -> std::string { if (SceneManager::current != SceneManager::Scene::GAME) { return "Only available in GAME scene"; } if (args.empty()) { return "usage: cheat [infinite lives|invincibility|open the jail|close the jail]"; } - // CHEAT INFINITE LIVES [ON|OFF] if (args[0] == "INFINITE") { if (args.size() < 2 || args[1] != "LIVES") { return "usage: cheat infinite lives [on|off]"; } - auto& cheat = Options::cheats.infinite_lives; - using State = Options::Cheat::State; - if (args.size() == 2) { - cheat = (cheat == State::ENABLED) ? State::DISABLED : State::ENABLED; - } else if (args[2] == "ON") { - if (cheat == State::ENABLED) { return "Infinite lives already ON"; } - cheat = State::ENABLED; - } else if (args[2] == "OFF") { - if (cheat == State::DISABLED) { return "Infinite lives already OFF"; } - cheat = State::DISABLED; - } else { - return "usage: cheat infinite lives [on|off]"; - } - return std::string("Infinite lives ") + (cheat == State::ENABLED ? "ON" : "OFF"); + const std::string_view MODE = (args.size() > 2) ? std::string_view(args[2]) : std::string_view(); + const std::string RES = applyCheatToggle(Options::cheats.infinite_lives, MODE, "Infinite lives"); + return RES.empty() ? "usage: cheat infinite lives [on|off]" : RES; } - // CHEAT INVINCIBILITY [ON|OFF] if (args[0] == "INVINCIBILITY" || args[0] == "INVENCIBILITY") { - auto& cheat = Options::cheats.invincible; - using State = Options::Cheat::State; - if (args.size() == 1) { - cheat = (cheat == State::ENABLED) ? State::DISABLED : State::ENABLED; - } else if (args[1] == "ON") { - if (cheat == State::ENABLED) { return "Invincibility already ON"; } - cheat = State::ENABLED; - } else if (args[1] == "OFF") { - if (cheat == State::DISABLED) { return "Invincibility already OFF"; } - cheat = State::DISABLED; - } else { - return "usage: cheat invincibility [on|off]"; - } - return std::string("Invincibility ") + (cheat == State::ENABLED ? "ON" : "OFF"); + const std::string_view MODE = (args.size() > 1) ? std::string_view(args[1]) : std::string_view(); + const std::string RES = applyCheatToggle(Options::cheats.invincible, MODE, "Invincibility"); + return RES.empty() ? "usage: cheat invincibility [on|off]" : RES; } - // CHEAT OPEN THE JAIL - if (args[0] == "OPEN") { - if (args.size() < 3 || args[1] != "THE" || args[2] != "JAIL") { return "usage: cheat open the jail"; } - if (Options::cheats.jail_is_open == Options::Cheat::State::ENABLED) { return "Jail already open"; } - Options::cheats.jail_is_open = Options::Cheat::State::ENABLED; - return "Jail opened"; - } - - // CHEAT CLOSE THE JAIL - if (args[0] == "CLOSE") { - if (args.size() < 3 || args[1] != "THE" || args[2] != "JAIL") { return "usage: cheat close the jail"; } - if (Options::cheats.jail_is_open == Options::Cheat::State::DISABLED) { return "Jail already closed"; } - Options::cheats.jail_is_open = Options::Cheat::State::DISABLED; - return "Jail closed"; - } + if (args[0] == "OPEN") { return cmdCheatJail(args, true); } + if (args[0] == "CLOSE") { return cmdCheatJail(args, false); } return "usage: cheat [infinite lives|invincibility|open the jail|close the jail]"; }