Make ExecutingAttack a local variable while being used, to ensure it's always cleaned up.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-29 18:49:48 +02:00
parent a9740cb1eb
commit d51919c74f
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 19 additions and 10 deletions

View File

@ -62,9 +62,9 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
auto target = choice->GetUser()->GetBattle()->GetCreature(choice->GetTarget());
ArbUt::List<Creature*> 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) {

View File

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