From d51919c74f8341c7ccbad72af1c7256965a1f672 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 29 May 2020 18:49:48 +0200 Subject: [PATCH] Make ExecutingAttack a local variable while being used, to ensure it's always cleaned up. --- src/Battling/Flow/TurnHandler.cpp | 17 +++++++---------- src/Battling/ScriptHandling/ScriptMacros.hpp | 12 ++++++++++++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/Battling/Flow/TurnHandler.cpp b/src/Battling/Flow/TurnHandler.cpp index d4fc6cd..0e7de56 100644 --- a/src/Battling/Flow/TurnHandler.cpp +++ b/src/Battling/Flow/TurnHandler.cpp @@ -62,9 +62,9 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) { auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget()); ArbUt::List targets = {target}; - auto attack = new ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); + auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript()); bool prevented = false; - HOOK(PreventAttack, attack, attack, &prevented); + HOOK_LOCAL(PreventAttack, attack, &attack, &prevented); if (prevented) { return; } @@ -77,25 +77,22 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) { // HOOK: check if attack fails bool fail = false; - HOOK(FailAttack, attack, attack, &fail); + HOOK_LOCAL(FailAttack, attack, &attack, &fail); if (fail) { // TODO: Fail handling. return; } bool stopBeforeAttack = false; - HOOK(StopBeforeAttack, attack, attack, &stopBeforeAttack); + HOOK_LOCAL(StopBeforeAttack, attack, &attack, &stopBeforeAttack); if (stopBeforeAttack) { return; } - HOOK(OnBeforeAttack, attack, attack); + HOOK_LOCAL(OnBeforeAttack, attack, &attack); - for (auto& t : attack->GetTargets()) { - HandleAttackForTarget(attack, t); + for (auto& t : attack.GetTargets()) { + HandleAttackForTarget(&attack, t); } - - // TODO: We currently delete this, but we probably want to store this in a log, so scripts can look it up. - delete attack; } void TurnHandler::HandleAttackForTarget(ExecutingAttack* attack, Creature* target) { diff --git a/src/Battling/ScriptHandling/ScriptMacros.hpp b/src/Battling/ScriptHandling/ScriptMacros.hpp index dbd14a4..ac4c6aa 100644 --- a/src/Battling/ScriptHandling/ScriptMacros.hpp +++ b/src/Battling/ScriptHandling/ScriptMacros.hpp @@ -9,3 +9,15 @@ next->hookName(__VA_ARGS__); \ } \ } + +#define HOOK_LOCAL(hookName, source, ...) \ + { \ + auto aggregator = source.GetScriptIterator(); \ + aggregator.Reset(); \ + while (aggregator.HasNext()) { \ + auto next = aggregator.GetNext(); \ + if (next == nullptr) \ + continue; \ + next->hookName(__VA_ARGS__); \ + } \ + }