Ensure Attack script is deleted when not taken by ExecutingAttack.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-05-29 20:05:05 +02:00
parent 20be2815ce
commit a7069a5960
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 10 additions and 0 deletions

View File

@ -63,6 +63,7 @@ void TurnHandler::ExecuteAttackChoice(AttackTurnChoice* choice) {
ArbUt::List<Creature*> targets = {target};
auto attack = ExecutingAttack(targets, 1, choice->GetUser(), choice->GetAttack(), choice->GetAttackScript());
choice->MarkScriptAsTaken();
bool prevented = false;
HOOK_LOCAL(PreventAttack, attack, &attack, &prevented);
if (prevented) {

View File

@ -12,6 +12,7 @@ namespace CreatureLib::Battling {
LearnedAttack* _attack;
CreatureIndex _target;
Script* _attackScript = nullptr;
bool _scriptIsTaken = false;
void ResolveScript() {
if (_attackScript != nullptr)
@ -41,6 +42,12 @@ namespace CreatureLib::Battling {
AttackTurnChoice(Creature* user, LearnedAttack* attack, const CreatureIndex& target, Script* script) noexcept
: BaseTurnChoice(user), _attack(attack), _target(target), _attackScript(script) {}
~AttackTurnChoice() {
if (!_scriptIsTaken) {
delete _attackScript;
}
}
inline LearnedAttack* GetAttack() const noexcept { return _attack; }
TurnChoiceKind GetKind() const noexcept override { return TurnChoiceKind ::Attack; }
@ -50,6 +57,8 @@ namespace CreatureLib::Battling {
return _attack->GetAttack()->GetPriority();
}
void MarkScriptAsTaken() { _scriptIsTaken = true; }
const CreatureIndex& GetTarget() const noexcept { return _target; }
Script* GetAttackScript() const noexcept { return _attackScript; }