From acacd02ef917c1ebd7a2d11076d734a0ace3dab9 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 27 Mar 2021 22:54:51 +0100 Subject: [PATCH] Adds script hook for preventing the opponent from switching out. Signed-off-by: Deukhoofd --- src/Battling/Flow/TurnHandler.cpp | 21 +++++++++++++++++++- src/Battling/ScriptHandling/BattleScript.hpp | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 2c63cde..644d604 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -251,14 +251,33 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo } void TurnHandler::ExecuteSwitchChoice(const ArbUt::BorrowedPtr& choice) { + auto user = choice->GetUser(); + auto battle = user->GetBattle(); + if (!battle.HasValue()) { + return; + } + bool preventSwitch = false; HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch); if (preventSwitch) { return; } // HOOK: PreventOpponentSwitch for each opponent. + for (auto* side : battle.GetValue()->GetSides()) { + if (side == user->GetBattleSide()) { + continue; + } + for (const auto& creature : side->GetCreatures()) { + if (!creature.HasValue()) { + continue; + } + HOOK(PreventOpponentSwitch, creature.GetValue(), choice, &preventSwitch); + if (preventSwitch) { + return; + } + } + } - auto user = choice->GetUser(); user->ClearVolatileScripts(); auto userSide = user->GetBattleSide(); if (userSide.HasValue()) { diff --git a/src/Battling/ScriptHandling/BattleScript.hpp b/src/Battling/ScriptHandling/BattleScript.hpp index 2b070e1..2eff568 100644 --- a/src/Battling/ScriptHandling/BattleScript.hpp +++ b/src/Battling/ScriptHandling/BattleScript.hpp @@ -71,6 +71,8 @@ namespace CreatureLib::Battling { virtual void PreventSelfSwitch([[maybe_unused]] const SwitchTurnChoice* choice, [[maybe_unused]] bool* outResult){}; + virtual void PreventOpponentSwitch([[maybe_unused]] const SwitchTurnChoice* choice, + [[maybe_unused]] bool* outResult){}; virtual void ModifyEffectChance([[maybe_unused]] const ExecutingAttack* attack, [[maybe_unused]] Creature* target, [[maybe_unused]] float* chance){};