From b4ea4c29a0c021e3ed7e65254bd81047b7e98d27 Mon Sep 17 00:00:00 2001 From: Jmgr Date: Sun, 30 May 2021 20:16:13 +0100 Subject: [PATCH] Keymapper: add enabled condition, fix being able to save/load in multiplayer --- Source/controls/keymapper.cpp | 6 +++--- Source/controls/keymapper.hpp | 17 +++++------------ Source/diablo.cpp | 30 +++++++++++++++++++++++++----- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/Source/controls/keymapper.cpp b/Source/controls/keymapper.cpp index 5059b6b88..164009954 100644 --- a/Source/controls/keymapper.cpp +++ b/Source/controls/keymapper.cpp @@ -43,7 +43,7 @@ Keymapper::ActionIndex Keymapper::addAction(const Action &action) return actions.size() - 1; } -void Keymapper::keyPressed(int key, bool isPlayerDead) const +void Keymapper::keyPressed(int key) const { auto it = keyIDToAction.find(key); if (it == keyIDToAction.end()) @@ -51,8 +51,8 @@ void Keymapper::keyPressed(int key, bool isPlayerDead) const auto &action = it->second; - // Some actions cannot be triggered while the player is dead. - if (isPlayerDead && action.get().ifDead == Action::IfDead::Ignore) + // Some actions cannot always be triggered. + if (!action.get().enable()) return; action(); diff --git a/Source/controls/keymapper.hpp b/Source/controls/keymapper.hpp index 3f57ae293..f04ae81f2 100644 --- a/Source/controls/keymapper.hpp +++ b/Source/controls/keymapper.hpp @@ -21,25 +21,18 @@ public: */ class Action final { public: - /** Can this action be triggered while the player is dead? */ - - enum class IfDead { - Allow, - Ignore, - }; - Action(const std::string &name, int defaultKey, std::function action) : name(name) , defaultKey(defaultKey) , action(action) - , ifDead(IfDead::Ignore) + , enable([] { return true; }) { } - Action(const std::string &name, int defaultKey, std::function action, IfDead ifDead) + Action(const std::string &name, int defaultKey, std::function action, std::function enable) : name(name) , defaultKey(defaultKey) , action(action) - , ifDead(ifDead) + , enable(enable) { } @@ -52,7 +45,7 @@ public: std::string name; int defaultKey; std::function action; - IfDead ifDead; + std::function enable; int key {}; friend class Keymapper; @@ -67,7 +60,7 @@ public: Keymapper(SetConfigKeyFunction setKeyFunction, GetConfigKeyFunction getKeyFunction); ActionIndex addAction(const Action &action); - void keyPressed(int key, bool isPlayerDead) const; + void keyPressed(int key) const; std::string keyNameForAction(ActionIndex actionIndex) const; void save() const; void load(); diff --git a/Source/diablo.cpp b/Source/diablo.cpp index a43c5cd03..e18d26dd6 100644 --- a/Source/diablo.cpp +++ b/Source/diablo.cpp @@ -1158,7 +1158,7 @@ static void PressKey(int vkey) if (sgnTimeoutCurs != CURSOR_NONE) { return; } - keymapper.keyPressed(vkey, deathflag); + keymapper.keyPressed(vkey); if (vkey == DVL_VK_RETURN) { if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0) dx_reinit(); @@ -1190,7 +1190,7 @@ static void PressKey(int vkey) return; } - keymapper.keyPressed(vkey, deathflag); + keymapper.keyPressed(vkey); if (vkey == DVL_VK_RETURN) { if ((GetAsyncKeyState(DVL_VK_MENU) & 0x8000) != 0) { @@ -2130,23 +2130,31 @@ void spellBookKeyPressed() invflag = false; } +bool isPlayerDead() +{ + return plr[myplr]._pmode == PM_DEATH || deathflag; +} + void initKeymapActions() { keymapper.addAction({ "Help", DVL_VK_F1, helpKeyPressed, + [&]() { return !isPlayerDead(); }, }); #ifdef _DEBUG keymapper.addAction({ "ItemInfo", DVL_VK_INVALID, itemInfoKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "QuestDebug", DVL_VK_INVALID, PrintDebugQuest, + [&]() { return !isPlayerDead(); }, }); #endif for (int i = 0; i < 4; ++i) { @@ -2160,6 +2168,7 @@ void initKeymapActions() } ToggleSpell(i); }, + [&]() { return !isPlayerDead(); }, }); } for (int i = 0; i < 4; ++i) { @@ -2167,33 +2176,37 @@ void initKeymapActions() spszMsgNameTbl[i], DVL_VK_F9 + i, [i]() { diablo_hotkey_msg(i); }, - Keymapper::Action::IfDead::Allow, }); } keymapper.addAction({ "DecreaseGamma", 'G', DecreaseGamma, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "IncreaseGamma", 'F', IncreaseGamma, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "Inventory", 'I', inventoryKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "Character", 'C', characterSheetKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "QuestLog", 'Q', questLogKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "Zoom", @@ -2202,16 +2215,19 @@ void initKeymapActions() zoomflag = !zoomflag; CalcViewportGeometry(); }, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "DisplaySpells", 'S', displaySpellsKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "SpellBook", 'B', spellBookKeyPressed, + [&]() { return !isPlayerDead(); }, }); keymapper.addAction({ "GameInfo", @@ -2226,6 +2242,7 @@ void initKeymapActions() strcpy(pszStr, fmt::format(_(/* TRANSLATORS: {:s} means: Character Name, Game Version, Game Difficulty. */ "{:s}, version = {:s}, mode = {:s}"), gszProductName, PROJECT_VERSION, difficulties[sgGameInitInfo.nDifficulty]).c_str()); NetSendCmdString(1 << myplr, pszStr); }, + [&]() { return !isPlayerDead(); }, }); for (int i = 0; i < 8; ++i) { keymapper.addAction({ @@ -2237,24 +2254,25 @@ void initKeymapActions() UseInvItem(myplr, INVITEM_BELT_FIRST + i); } }, + [&]() { return !isPlayerDead(); }, }); } keymapper.addAction({ "QuickSave", DVL_VK_F2, [] { gamemenu_save_game(false); }, + [&]() { return !gbIsMultiplayer && !isPlayerDead(); }, }); keymapper.addAction({ "QuickLoad", DVL_VK_F3, [] { gamemenu_load_game(false); }, - Keymapper::Action::IfDead::Allow, + [&]() { return !gbIsMultiplayer && gbValidSaveFile; }, }); keymapper.addAction({ "QuitGame", DVL_VK_INVALID, [] { gamemenu_quit_game(false); }, - Keymapper::Action::IfDead::Allow, }); #ifdef _DEBUG keymapper.addAction({ @@ -2266,12 +2284,14 @@ void initKeymapActions() return; } }, + [&]() { return !isPlayerDead(); }, }); #endif keymapper.addAction({ "StopHero", DVL_VK_INVALID, [] { plr[myplr].Stop(); }, + [&]() { return !isPlayerDead(); }, }); }