From e8c541f873f9dda10587daa308aaf43f545d43ba Mon Sep 17 00:00:00 2001
From: Laserlicht <13953785+Laserlicht@users.noreply.github.com>
Date: Fri, 26 Jan 2024 23:15:56 +0100
Subject: [PATCH] end with auto combat

---
 client/battle/BattleWindow.cpp | 23 +++++++++++++++++++++++
 client/battle/BattleWindow.h   |  3 +++
 client/gui/Shortcut.h          |  1 +
 client/gui/ShortcutHandler.cpp |  2 ++
 4 files changed, 29 insertions(+)

diff --git a/client/battle/BattleWindow.cpp b/client/battle/BattleWindow.cpp
index 85d2618d8..0b9cbb78c 100644
--- a/client/battle/BattleWindow.cpp
+++ b/client/battle/BattleWindow.cpp
@@ -60,6 +60,7 @@ BattleWindow::BattleWindow(BattleInterface & owner):
 	addShortcut(EShortcut::BATTLE_SURRENDER, std::bind(&BattleWindow::bSurrenderf, this));
 	addShortcut(EShortcut::BATTLE_RETREAT, std::bind(&BattleWindow::bFleef, this));
 	addShortcut(EShortcut::BATTLE_AUTOCOMBAT, std::bind(&BattleWindow::bAutofightf, this));
+	addShortcut(EShortcut::BATTLE_END_WITH_AUTOCOMBAT, std::bind(&BattleWindow::endWithAutocombat, this));
 	addShortcut(EShortcut::BATTLE_CAST_SPELL, std::bind(&BattleWindow::bSpellf, this));
 	addShortcut(EShortcut::BATTLE_WAIT, std::bind(&BattleWindow::bWaitf, this));
 	addShortcut(EShortcut::BATTLE_DEFEND, std::bind(&BattleWindow::bDefencef, this));
@@ -722,6 +723,7 @@ void BattleWindow::blockUI(bool on)
 	setShortcutBlocked(EShortcut::BATTLE_DEFEND, on || owner.tacticsMode);
 	setShortcutBlocked(EShortcut::BATTLE_SELECT_ACTION, on || owner.tacticsMode);
 	setShortcutBlocked(EShortcut::BATTLE_AUTOCOMBAT, owner.actionsController->spellcastingModeActive());
+	setShortcutBlocked(EShortcut::BATTLE_END_WITH_AUTOCOMBAT, on || owner.tacticsMode);
 	setShortcutBlocked(EShortcut::BATTLE_TACTICS_END, on && owner.tacticsMode);
 	setShortcutBlocked(EShortcut::BATTLE_TACTICS_NEXT, on && owner.tacticsMode);
 	setShortcutBlocked(EShortcut::BATTLE_CONSOLE_DOWN, on && !owner.tacticsMode);
@@ -733,6 +735,27 @@ std::optional<uint32_t> BattleWindow::getQueueHoveredUnitId()
 	return queue->getHoveredUnitIdIfAny();
 }
 
+void BattleWindow::endWithAutocombat() 
+{
+	close();
+
+	auto ai = CDynLibHandler::getNewBattleAI(settings["server"]["friendlyAI"].String());
+
+	AutocombatPreferences autocombatPreferences = AutocombatPreferences();
+	autocombatPreferences.enableSpellsUsage = settings["battle"]["enableAutocombatSpells"].Bool();
+
+	ai->initBattleInterface(owner.curInt->env, owner.curInt->cb, autocombatPreferences);
+	ai->battleStart(owner.getBattleID(), owner.army1, owner.army2, int3(0,0,0), owner.attackingHeroInstance, owner.defendingHeroInstance, owner.getBattle()->battleGetMySide(), false);
+
+	owner.curInt->isAutoFightOn = true;
+	owner.curInt->cb->registerBattleInterface(ai);
+	owner.curInt->autofightingAI = ai;
+
+	owner.requestAutofightingAIToTakeAction();
+
+	owner.curInt->battleInt.reset();
+}
+
 void BattleWindow::showAll(Canvas & to)
 {
 	CIntObject::showAll(to);
diff --git a/client/battle/BattleWindow.h b/client/battle/BattleWindow.h
index c8317e884..4e0e332cb 100644
--- a/client/battle/BattleWindow.h
+++ b/client/battle/BattleWindow.h
@@ -125,5 +125,8 @@ public:
 
 	/// Set possible alternative options. If more than 1 - the last will be considered as default option
 	void setAlternativeActions(const std::list<PossiblePlayerBattleAction> &);
+
+	/// ends battle with autocombat
+	void endWithAutocombat();
 };
 
diff --git a/client/gui/Shortcut.h b/client/gui/Shortcut.h
index 39ad21008..2fc864ebd 100644
--- a/client/gui/Shortcut.h
+++ b/client/gui/Shortcut.h
@@ -125,6 +125,7 @@ enum class EShortcut
 	BATTLE_SURRENDER,
 	BATTLE_RETREAT,
 	BATTLE_AUTOCOMBAT,
+	BATTLE_END_WITH_AUTOCOMBAT,
 	BATTLE_CAST_SPELL,
 	BATTLE_WAIT,
 	BATTLE_DEFEND,
diff --git a/client/gui/ShortcutHandler.cpp b/client/gui/ShortcutHandler.cpp
index 13f4f7307..839a3fc99 100644
--- a/client/gui/ShortcutHandler.cpp
+++ b/client/gui/ShortcutHandler.cpp
@@ -124,6 +124,7 @@ std::vector<EShortcut> ShortcutHandler::translateKeycode(SDL_Keycode key) const
 		{SDLK_s,         EShortcut::BATTLE_SURRENDER          },
 		{SDLK_r,         EShortcut::BATTLE_RETREAT            },
 		{SDLK_a,         EShortcut::BATTLE_AUTOCOMBAT         },
+		{SDLK_e,         EShortcut::BATTLE_END_WITH_AUTOCOMBAT},
 		{SDLK_c,         EShortcut::BATTLE_CAST_SPELL         },
 		{SDLK_w,         EShortcut::BATTLE_WAIT               },
 		{SDLK_d,         EShortcut::BATTLE_DEFEND             },
@@ -265,6 +266,7 @@ EShortcut ShortcutHandler::findShortcut(const std::string & identifier ) const
 		{"battleSurrender",          EShortcut::BATTLE_SURRENDER          },
 		{"battleRetreat",            EShortcut::BATTLE_RETREAT            },
 		{"battleAutocombat",         EShortcut::BATTLE_AUTOCOMBAT         },
+		{"battleAutocombatEnd",      EShortcut::BATTLE_END_WITH_AUTOCOMBAT},
 		{"battleCastSpell",          EShortcut::BATTLE_CAST_SPELL         },
 		{"battleWait",               EShortcut::BATTLE_WAIT               },
 		{"battleDefend",             EShortcut::BATTLE_DEFEND             },