1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-26 03:52:01 +02:00

Merge pull request #2617 from dydzio0614/configurable-autobattle-spells

Configurable autobattle spells usage
This commit is contained in:
Ivan Savenko 2023-08-25 01:29:16 +03:00 committed by GitHub
commit 43096f0ed1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 95 additions and 7 deletions

View File

@ -93,6 +93,12 @@ void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
movesSkippedByDefense = 0;
}
void CBattleAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> 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));

View File

@ -68,6 +68,7 @@ public:
~CBattleAI();
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> 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<BattleHex> hexes) const;
std::vector<BattleHex> getBrokenWallMoatHexes() const;
AutocombatPreferences autobattlePreferences = AutocombatPreferences();
};

View File

@ -47,6 +47,11 @@ void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::share
CB->unlockGsWhenWaiting = false;
}
void CStupidAI::initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences)
{
initBattleInterface(ENV, CB);
}
void CStupidAI::actionFinished(const BattleAction &action)
{
print("actionFinished called");

View File

@ -29,6 +29,7 @@ public:
~CStupidAI();
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB) override;
void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> 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

View File

@ -659,7 +659,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);

View File

@ -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);

View File

@ -64,6 +64,10 @@ BattleOptionsTab::BattleOptionsTab(BattleInterface * owner)
{
showStickyHeroWindowsChangedCallback(value, owner);
});
addCallback("enableAutocombatSpellsChanged", [this](bool value)
{
enableAutocombatSpellsChangedCallback(value);
});
build(config);
std::shared_ptr<CToggleGroup> animationSpeedToggle = widget<CToggleGroup>("animationSpeedPicker");
@ -92,6 +96,9 @@ BattleOptionsTab::BattleOptionsTab(BattleInterface * owner)
std::shared_ptr<CToggleButton> skipBattleIntroMusicCheckbox = widget<CToggleButton>("skipBattleIntroMusicCheckbox");
skipBattleIntroMusicCheckbox->setSelected(settings["gameTweaks"]["skipBattleIntroMusic"].Bool());
std::shared_ptr<CToggleButton> enableAutocombatSpellsCheckbox = widget<CToggleButton>("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;
}

View File

@ -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);
};

View File

@ -322,6 +322,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

View File

@ -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
}
}
},

View File

@ -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":
[
{},
{},
{}

View File

@ -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<Environment> ENV, std::shared_ptr<CBattleCallback> CB){};
virtual void initBattleInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CBattleCallback> CB, AutocombatPreferences autocombatPreferences){};
//battle call-ins
virtual void activeStack(const CStack * stack)=0; //called when it's turn of that stack

View File

@ -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;
};