From 4dd3700352e6c80da9a8194e0c5f970722198bb4 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 27 Mar 2021 14:49:17 +0100 Subject: [PATCH] Adds script hooks for preventing runaway. Signed-off-by: Deukhoofd --- src/Battling/Flow/TurnHandler.cpp | 40 +++++++++++++++----- src/Battling/Flow/TurnHandler.hpp | 8 ++-- src/Battling/ScriptHandling/BattleScript.hpp | 3 ++ 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index 6944a26..03f42f3 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -6,8 +6,8 @@ using namespace CreatureLib::Battling; -void TurnHandler::RunTurn(ArbUt::BorrowedPtr queue, ArbUt::BorrowedPtr battle) { - for (auto choice : queue->GetInnerQueue()) { +void TurnHandler::RunTurn(const ArbUt::BorrowedPtr& queue, const ArbUt::BorrowedPtr& battle) { + for (const auto& choice : queue->GetInnerQueue()) { HOOK(OnBeforeTurn, choice, choice.get()); } while (queue->HasNext() && !battle->HasEnded()) { @@ -27,17 +27,19 @@ void TurnHandler::RunTurn(ArbUt::BorrowedPtr queue, ArbUt::Borrowed queue->HasCompletedQueue = true; } -void TurnHandler::ExecuteChoice(ArbUt::BorrowedPtr choice) { +void TurnHandler::ExecuteChoice(const ArbUt::BorrowedPtr& choice) { auto choiceKind = choice->GetKind(); if (choiceKind == TurnChoiceKind::Pass) { return; } auto user = choice->GetUser(); - if (!user->GetBattle().HasValue()) + if (!user->GetBattle().HasValue()) { return; - auto battle = user->GetBattle().GetValue(); - if (battle->HasEnded()) + } + auto* battle = user->GetBattle().GetValue(); + if (battle->HasEnded()) { return; + } // If the user is fainted, we don't want to execute its choice. if (user->IsFainted()) { return; @@ -248,7 +250,7 @@ void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::Bo } } -void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr choice) { +void TurnHandler::ExecuteSwitchChoice(const ArbUt::BorrowedPtr& choice) { bool preventSwitch = false; HOOK(PreventSelfSwitch, choice, choice.GetRaw(), &preventSwitch); if (preventSwitch) { @@ -264,14 +266,32 @@ void TurnHandler::ExecuteSwitchChoice(ArbUt::BorrowedPtr choic userSide.GetValue()->SetCreature(choice->GetNewCreature(), userIndex); } } -void TurnHandler::ExecuteFleeChoice(ArbUt::BorrowedPtr choice) { +void TurnHandler::ExecuteFleeChoice(const ArbUt::BorrowedPtr& choice) { auto user = choice->GetUser(); auto battle = user->GetBattle(); if (!battle.HasValue() || !battle.GetValue()->CanFlee()) { return; } - // TODO: If any of the creatures on the users side has a script that prevents it from running, block. - // TODO: If any of the creatures on any other side has a script that prevents this side from running, block. + + bool preventRun = false; + HOOK(PreventRunAway, choice, &preventRun); + if (preventRun) { + return; + } + for (auto* side : battle.GetValue()->GetSides()) { + if (side == user->GetBattleSide()) { + continue; + } + for (const auto& creature : side->GetCreatures()) { + if (!creature.HasValue()) { + continue; + } + HOOK(PreventOpponentRunAway, creature.GetValue(), &preventRun); + if (preventRun) { + return; + } + } + } if (battle.GetValue()->GetLibrary()->GetMiscLibrary()->CanFlee(choice.GetRaw())) { if (user->GetBattleSide().HasValue()) { diff --git a/src/Battling/Flow/TurnHandler.hpp b/src/Battling/Flow/TurnHandler.hpp index 442aafb..9067486 100644 --- a/src/Battling/Flow/TurnHandler.hpp +++ b/src/Battling/Flow/TurnHandler.hpp @@ -11,16 +11,16 @@ namespace CreatureLib::Battling { class Battle; class TurnHandler { - static void ExecuteChoice(ArbUt::BorrowedPtr choice); + static void ExecuteChoice(const ArbUt::BorrowedPtr& choice); static void ExecuteAttackChoice(const ArbUt::BorrowedPtr& choice); static void HandleAttackForTarget(ExecutingAttack* attack, const ArbUt::BorrowedPtr& target); - static void ExecuteSwitchChoice(ArbUt::BorrowedPtr choice); - static void ExecuteFleeChoice(ArbUt::BorrowedPtr choice); + static void ExecuteSwitchChoice(const ArbUt::BorrowedPtr& choice); + static void ExecuteFleeChoice(const ArbUt::BorrowedPtr& choice); public: - static void RunTurn(ArbUt::BorrowedPtr queue, ArbUt::BorrowedPtr battle); + static void RunTurn(const ArbUt::BorrowedPtr& queue, const ArbUt::BorrowedPtr& battle); }; } diff --git a/src/Battling/ScriptHandling/BattleScript.hpp b/src/Battling/ScriptHandling/BattleScript.hpp index ed55c58..64a23ad 100644 --- a/src/Battling/ScriptHandling/BattleScript.hpp +++ b/src/Battling/ScriptHandling/BattleScript.hpp @@ -78,6 +78,9 @@ namespace CreatureLib::Battling { virtual void OnFail([[maybe_unused]] Creature* target){}; virtual void OnOpponentFail([[maybe_unused]] Creature* target){}; + + virtual void PreventRunAway([[maybe_unused]] bool* result){}; + virtual void PreventOpponentRunAway([[maybe_unused]] bool* result){}; }; }