From 6bfbe80cc9524612251599e47ec22aa3feb6ae8e Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sat, 19 Aug 2023 17:23:55 +0200 Subject: [PATCH 1/5] Allow toggling quick combat spells usage --- AI/BattleAI/BattleAI.cpp | 9 +++++- AI/BattleAI/BattleAI.h | 2 ++ client/CPlayerInterface.cpp | 6 +++- client/battle/BattleWindow.cpp | 6 +++- client/windows/settings/BattleOptionsTab.cpp | 13 +++++++++ client/windows/settings/BattleOptionsTab.h | 1 + cmake_modules/VCMI_lib.cmake | 2 ++ config/schemas/settings.json | 6 +++- config/widgets/settings/battleOptionsTab.json | 29 +++++++++++++++++-- lib/CGameInterface.h | 2 ++ lib/battle/AutocombatPreferences.cpp | 11 +++++++ lib/battle/AutocombatPreferences.h | 21 ++++++++++++++ 12 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 lib/battle/AutocombatPreferences.cpp create mode 100644 lib/battle/AutocombatPreferences.h diff --git a/AI/BattleAI/BattleAI.cpp b/AI/BattleAI/BattleAI.cpp index 0f7068402..00f6295e7 100644 --- a/AI/BattleAI/BattleAI.cpp +++ b/AI/BattleAI/BattleAI.cpp @@ -93,6 +93,12 @@ void CBattleAI::initBattleInterface(std::shared_ptr ENV, std::share movesSkippedByDefense = 0; } +void CBattleAI::initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB, AutocombatPreferences autocombatPreferences) +{ + initBattleInterface(ENV, CB); + autobattlePreferences = autocombatPreferences; +} + BattleAction CBattleAI::useHealingTent(const CStack *stack) { auto healingTargets = cb->battleGetStacks(CBattleInfoEssentials::ONLY_MINE); @@ -283,7 +289,8 @@ void CBattleAI::activeStack( const CStack * stack ) return; } - attemptCastingSpell(); + if(autobattlePreferences.enableSpellsUsage) + attemptCastingSpell(); logAi->trace("Spellcast attempt completed in %lld", timeElapsed(start)); diff --git a/AI/BattleAI/BattleAI.h b/AI/BattleAI/BattleAI.h index 37338d299..a83fcfd25 100644 --- a/AI/BattleAI/BattleAI.h +++ b/AI/BattleAI/BattleAI.h @@ -68,6 +68,7 @@ public: ~CBattleAI(); void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB) override; + void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB, AutocombatPreferences autocombatPreferences) override; void attemptCastingSpell(); void evaluateCreatureSpellcast(const CStack * stack, PossibleSpellcast & ps); //for offensive damaging spells only @@ -102,4 +103,5 @@ public: private: BattleAction goTowardsNearest(const CStack * stack, std::vector hexes) const; std::vector getBrokenWallMoatHexes() const; + AutocombatPreferences autobattlePreferences = AutocombatPreferences(); }; diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index 69fde8c7b..3232fd724 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -662,7 +662,11 @@ void CPlayerInterface::battleStart(const CCreatureSet *army1, const CCreatureSet if ((replayAllowed && useQuickCombat) || forceQuickCombat) { autofightingAI = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String()); - autofightingAI->initBattleInterface(env, cb); + + AutocombatPreferences autocombatPreferences = AutocombatPreferences(); + autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool(); + + autofightingAI->initBattleInterface(env, cb, autocombatPreferences); autofightingAI->battleStart(army1, army2, tile, hero1, hero2, side, false); isAutoFightOn = true; cb->registerBattleInterface(autofightingAI); diff --git a/client/battle/BattleWindow.cpp b/client/battle/BattleWindow.cpp index 36c430272..36cf2441c 100644 --- a/client/battle/BattleWindow.cpp +++ b/client/battle/BattleWindow.cpp @@ -500,7 +500,11 @@ void BattleWindow::bAutofightf() blockUI(true); auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String()); - ai->initBattleInterface(owner.curInt->env, owner.curInt->cb); + + AutocombatPreferences autocombatPreferences = AutocombatPreferences(); + autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool(); + + ai->initBattleInterface(owner.curInt->env, owner.curInt->cb, autocombatPreferences); ai->battleStart(owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.curInt->cb->battleGetMySide(), false); owner.curInt->autofightingAI = ai; owner.curInt->cb->registerBattleInterface(ai); diff --git a/client/windows/settings/BattleOptionsTab.cpp b/client/windows/settings/BattleOptionsTab.cpp index 99f874a4e..1e657d61c 100644 --- a/client/windows/settings/BattleOptionsTab.cpp +++ b/client/windows/settings/BattleOptionsTab.cpp @@ -64,6 +64,10 @@ BattleOptionsTab::BattleOptionsTab(BattleInterface * owner) { showStickyHeroWindowsChangedCallback(value, owner); }); + addCallback("enableAutocombatSpellsChanged", [this](bool value) + { + enableAutocombatSpellsChangedCallback(value); + }); build(config); std::shared_ptr animationSpeedToggle = widget("animationSpeedPicker"); @@ -92,6 +96,9 @@ BattleOptionsTab::BattleOptionsTab(BattleInterface * owner) std::shared_ptr skipBattleIntroMusicCheckbox = widget("skipBattleIntroMusicCheckbox"); skipBattleIntroMusicCheckbox->setSelected(settings["gameTweaks"]["skipBattleIntroMusic"].Bool()); + + std::shared_ptr enableAutocombatSpellsCheckbox = widget("enableAutocombatSpellsCheckbox"); + enableAutocombatSpellsCheckbox->setSelected(settings["battle"]["enableAutocombatSpells"].Bool()); } int BattleOptionsTab::getAnimSpeed() const @@ -235,3 +242,9 @@ void BattleOptionsTab::skipBattleIntroMusicChangedCallback(bool value) musicSkipSettingValue->Bool() = value; } +void BattleOptionsTab::enableAutocombatSpellsChangedCallback(bool value) +{ + Settings enableAutocombatSpells = settings.write["battle"]["enableAutocombatSpells"]; + enableAutocombatSpells->Bool() = value; +} + diff --git a/client/windows/settings/BattleOptionsTab.h b/client/windows/settings/BattleOptionsTab.h index 6a71d2e68..43d4d7e50 100644 --- a/client/windows/settings/BattleOptionsTab.h +++ b/client/windows/settings/BattleOptionsTab.h @@ -32,6 +32,7 @@ private: void queueSizeChangedCallback(int value, BattleInterface * parentBattleInterface); void skipBattleIntroMusicChangedCallback(bool value); void showStickyHeroWindowsChangedCallback(bool value, BattleInterface * parentBattleInterface); + void enableAutocombatSpellsChangedCallback(bool value); public: BattleOptionsTab(BattleInterface * owner = nullptr); }; diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index 70c41549a..8f6ccbdc1 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -7,6 +7,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/StdInc.cpp ${MAIN_LIB_DIR}/battle/AccessibilityInfo.cpp + ${MAIN_LIB_DIR}/battle/AutocombatPreferences.cpp ${MAIN_LIB_DIR}/battle/BattleAction.cpp ${MAIN_LIB_DIR}/battle/BattleAttackInfo.cpp ${MAIN_LIB_DIR}/battle/BattleHex.cpp @@ -322,6 +323,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/../include/vcmi/Team.h ${MAIN_LIB_DIR}/battle/AccessibilityInfo.h + ${MAIN_LIB_DIR}/battle/AutocombatPreferences.h ${MAIN_LIB_DIR}/battle/BattleAction.h ${MAIN_LIB_DIR}/battle/BattleAttackInfo.h ${MAIN_LIB_DIR}/battle/BattleHex.h diff --git a/config/schemas/settings.json b/config/schemas/settings.json index 20699af25..5f3acffbc 100644 --- a/config/schemas/settings.json +++ b/config/schemas/settings.json @@ -272,7 +272,7 @@ "type" : "object", "additionalProperties" : false, "default" : {}, - "required" : [ "speedFactor", "mouseShadow", "cellBorders", "stackRange", "movementHighlightOnHover", "rangeLimitHighlightOnHover", "showQueue", "swipeAttackDistance", "queueSize", "stickyHeroInfoWindows" ], + "required" : [ "speedFactor", "mouseShadow", "cellBorders", "stackRange", "movementHighlightOnHover", "rangeLimitHighlightOnHover", "showQueue", "swipeAttackDistance", "queueSize", "stickyHeroInfoWindows", "enableAutocombatSpells" ], "properties" : { "speedFactor" : { "type" : "number", @@ -314,6 +314,10 @@ "stickyHeroInfoWindows" : { "type" : "boolean", "default" : true + }, + "enableAutocombatSpells" : { + "type": "boolean", + "default": true } } }, diff --git a/config/widgets/settings/battleOptionsTab.json b/config/widgets/settings/battleOptionsTab.json index 0fde2a031..8f47a3fd5 100644 --- a/config/widgets/settings/battleOptionsTab.json +++ b/config/widgets/settings/battleOptionsTab.json @@ -51,14 +51,37 @@ ] }, { - "name": "autoCombatCheckboxes", + "name": "autoCombatFakeCheckboxes", "type" : "verticalLayout", "customType" : "checkboxFake", "position": {"x": 380, "y": 83}, "items": [ - {}, - {}, + {} + ] + }, + + { + "type" : "verticalLayout", + "customType" : "checkbox", + "position": {"x": 380, "y": 113}, + "items": + [ + { + "name": "enableAutocombatSpellsCheckbox", + "help": "vcmi.battleOptions.enableAutocombatSpells", + "callback": "enableAutocombatSpellsChanged" + } + ] + }, + + { + "name": "autoCombatFakeCheckboxes2", + "type" : "verticalLayout", + "customType" : "checkboxFake", + "position": {"x": 380, "y": 143}, + "items": + [ {}, {}, {} diff --git a/lib/CGameInterface.h b/lib/CGameInterface.h index 3ee27c21a..9ffcc80ad 100644 --- a/lib/CGameInterface.h +++ b/lib/CGameInterface.h @@ -9,6 +9,7 @@ */ #pragma once +#include "battle/AutocombatPreferences.h" #include "battle/BattleAction.h" #include "IGameEventsReceiver.h" @@ -76,6 +77,7 @@ public: virtual ~CBattleGameInterface() {}; virtual void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB){}; + virtual void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB, AutocombatPreferences autocombatPreferences){}; //battle call-ins virtual void activeStack(const CStack * stack)=0; //called when it's turn of that stack diff --git a/lib/battle/AutocombatPreferences.cpp b/lib/battle/AutocombatPreferences.cpp new file mode 100644 index 000000000..669a5f776 --- /dev/null +++ b/lib/battle/AutocombatPreferences.cpp @@ -0,0 +1,11 @@ +/* + * AutocombatPreferences.cpp, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ + +#include "AutocombatPreferences.h" diff --git a/lib/battle/AutocombatPreferences.h b/lib/battle/AutocombatPreferences.h new file mode 100644 index 000000000..da6224ead --- /dev/null +++ b/lib/battle/AutocombatPreferences.h @@ -0,0 +1,21 @@ +/* + * AutocombatPreferences.h, part of VCMI engine + * + * Authors: listed in file AUTHORS in main folder + * + * License: GNU General Public License v2.0 or later + * Full text of license available in license.txt file, in main folder + * + */ +#pragma once + +struct AutocombatPreferences +{ + bool enableSpellsUsage = true; + //TODO: below options exist in original H3, consider usefulness of mixed human-AI combat when enabling autocombat inside battle + bool enableUnitsUsage = true; + bool enableCatapultUsage = true; + bool enableBallistaUsage = true; + bool enableFirstAidTendUsage = true; +}; + From d8eef4905abe8c7cdb39e003ad30e92368f69c2d Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sun, 20 Aug 2023 13:53:25 +0200 Subject: [PATCH 2/5] Implement new callback in stupidAI --- AI/StupidAI/StupidAI.cpp | 5 +++++ AI/StupidAI/StupidAI.h | 1 + 2 files changed, 6 insertions(+) diff --git a/AI/StupidAI/StupidAI.cpp b/AI/StupidAI/StupidAI.cpp index 85a9fe571..aad18351e 100644 --- a/AI/StupidAI/StupidAI.cpp +++ b/AI/StupidAI/StupidAI.cpp @@ -47,6 +47,11 @@ void CStupidAI::initBattleInterface(std::shared_ptr ENV, std::share CB->unlockGsWhenWaiting = false; } +void CStupidAI::initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB, AutocombatPreferences autocombatPreferences) +{ + initBattleInterface(ENV, CB); +} + void CStupidAI::actionFinished(const BattleAction &action) { print("actionFinished called"); diff --git a/AI/StupidAI/StupidAI.h b/AI/StupidAI/StupidAI.h index 3b074d643..6b0d230ad 100644 --- a/AI/StupidAI/StupidAI.h +++ b/AI/StupidAI/StupidAI.h @@ -29,6 +29,7 @@ public: ~CStupidAI(); void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB) override; + void initBattleInterface(std::shared_ptr ENV, std::shared_ptr CB, AutocombatPreferences autocombatPreferences) override; void actionFinished(const BattleAction &action) override;//occurs AFTER every action taken by any stack or by the hero void actionStarted(const BattleAction &action) override;//occurs BEFORE every action taken by any stack or by the hero void activeStack(const CStack * stack) override; //called when it's turn of that stack From 78f56df44faae891013f89a036425e4afd2efef1 Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sun, 20 Aug 2023 19:11:36 +0200 Subject: [PATCH 3/5] Comment out unused OH3 options so they do not clutter code suggestions Co-authored-by: Ivan Savenko --- lib/battle/AutocombatPreferences.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/battle/AutocombatPreferences.h b/lib/battle/AutocombatPreferences.h index da6224ead..0ca89c53e 100644 --- a/lib/battle/AutocombatPreferences.h +++ b/lib/battle/AutocombatPreferences.h @@ -13,9 +13,9 @@ struct AutocombatPreferences { bool enableSpellsUsage = true; //TODO: below options exist in original H3, consider usefulness of mixed human-AI combat when enabling autocombat inside battle - bool enableUnitsUsage = true; - bool enableCatapultUsage = true; - bool enableBallistaUsage = true; - bool enableFirstAidTendUsage = true; +// bool enableUnitsUsage = true; +// bool enableCatapultUsage = true; +// bool enableBallistaUsage = true; +// bool enableFirstAidTendUsage = true; }; From e81cd4e0e6c5459b7ae42f49ea4fc26aeb0a74ff Mon Sep 17 00:00:00 2001 From: Dydzio Date: Mon, 21 Aug 2023 20:09:50 +0200 Subject: [PATCH 4/5] Delete empty file --- lib/battle/AutocombatPreferences.cpp | 11 ----------- 1 file changed, 11 deletions(-) delete mode 100644 lib/battle/AutocombatPreferences.cpp diff --git a/lib/battle/AutocombatPreferences.cpp b/lib/battle/AutocombatPreferences.cpp deleted file mode 100644 index 669a5f776..000000000 --- a/lib/battle/AutocombatPreferences.cpp +++ /dev/null @@ -1,11 +0,0 @@ -/* - * AutocombatPreferences.cpp, part of VCMI engine - * - * Authors: listed in file AUTHORS in main folder - * - * License: GNU General Public License v2.0 or later - * Full text of license available in license.txt file, in main folder - * - */ - -#include "AutocombatPreferences.h" From 988843c4ee57e642394ebacc2540e79a7cd16a38 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Fri, 25 Aug 2023 01:14:22 +0300 Subject: [PATCH 5/5] Update cmake_modules/VCMI_lib.cmake --- cmake_modules/VCMI_lib.cmake | 1 - 1 file changed, 1 deletion(-) diff --git a/cmake_modules/VCMI_lib.cmake b/cmake_modules/VCMI_lib.cmake index 8f6ccbdc1..15efa56c2 100644 --- a/cmake_modules/VCMI_lib.cmake +++ b/cmake_modules/VCMI_lib.cmake @@ -7,7 +7,6 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) ${MAIN_LIB_DIR}/StdInc.cpp ${MAIN_LIB_DIR}/battle/AccessibilityInfo.cpp - ${MAIN_LIB_DIR}/battle/AutocombatPreferences.cpp ${MAIN_LIB_DIR}/battle/BattleAction.cpp ${MAIN_LIB_DIR}/battle/BattleAttackInfo.cpp ${MAIN_LIB_DIR}/battle/BattleHex.cpp