From 675aa5db41ff2607d72617b42fd6d3f851c2e4df Mon Sep 17 00:00:00 2001 From: ghoulslash <41651341+ghoulslash@users.noreply.github.com> Date: Tue, 5 Sep 2023 03:21:14 -0400 Subject: [PATCH] fix GetBattleAnimMoveTargets logic (#3278) * fix GetBattleAnimMoveTargets logic * more efficient GetBattleAnimMoveTargets --------- Co-authored-by: ghoulslash --- include/battle_util.h | 3 ++- src/battle_anim.c | 41 ++++++++++++++++++++--------------------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/include/battle_util.h b/include/battle_util.h index d0b34add7..723a5362b 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -54,7 +54,8 @@ #define WEATHER_HAS_EFFECT ((!IsAbilityOnField(ABILITY_CLOUD_NINE) && !IsAbilityOnField(ABILITY_AIR_LOCK))) -#define IS_WHOLE_SIDE_ALIVE(battler)((IsBattlerAlive(battler) && IsBattlerAlive(BATTLE_PARTNER(battler)))) +#define IS_WHOLE_SIDE_ALIVE(battler) ((IsBattlerAlive(battler) && IsBattlerAlive(BATTLE_PARTNER(battler)))) +#define IS_ALIVE_AND_PRESENT(battler) (IsBattlerAlive(battler) && IsBattlerSpritePresent(battler)) // for Natural Gift and Fling struct TypePower diff --git a/src/battle_anim.c b/src/battle_anim.c index 808676c2e..6b48d38da 100644 --- a/src/battle_anim.c +++ b/src/battle_anim.c @@ -435,32 +435,27 @@ static void Cmd_unloadspritegfx(void) static u8 GetBattleAnimMoveTargets(u8 battlerArgIndex, u8 *targets) { - u8 numTargets = 1; + u8 numTargets = 0; + int idx = 0; + u32 battler = gBattleAnimArgs[battlerArgIndex]; switch (GetBattlerMoveTargetType(gBattleAnimAttacker, gAnimMoveIndex)) { - case MOVE_TARGET_BOTH: - targets[0] = gBattleAnimArgs[battlerArgIndex]; - numTargets = 1; - if (IsBattlerAlive(BATTLE_PARTNER(targets[0]))) - { - targets[1] = BATTLE_PARTNER(targets[0]); - numTargets = 2; - } - break; case MOVE_TARGET_FOES_AND_ALLY: - targets[0] = gBattleAnimArgs[battlerArgIndex]; - numTargets = 1; - if (IsBattlerAlive(BATTLE_PARTNER(targets[0]))) - { - targets[1] = BATTLE_PARTNER(targets[0]); + if (IS_ALIVE_AND_PRESENT(BATTLE_PARTNER(BATTLE_OPPOSITE(battler)))) { + targets[idx++] = BATTLE_PARTNER(BATTLE_OPPOSITE(battler)); numTargets++; } - - if (IsBattlerAlive(BATTLE_PARTNER(BATTLE_OPPOSITE(targets[0])))) - { - targets[2] = BATTLE_PARTNER(BATTLE_OPPOSITE(targets[0])); + // fallthrough + case MOVE_TARGET_BOTH: + if (IS_ALIVE_AND_PRESENT(battler)) { + targets[idx++] = battler; numTargets++; } + battler = BATTLE_PARTNER(battler); + if (IS_ALIVE_AND_PRESENT(battler)) { + targets[idx++] = battler; + numTargets++; + } break; default: targets[0] = gBattleAnimArgs[battlerArgIndex]; // original @@ -551,7 +546,9 @@ static void CreateSpriteOnTargets(const struct SpriteTemplate *template, u8 argV subpriority = GetSubpriorityForMoveAnim(argVar); ntargets = GetBattleAnimMoveTargets(battlerArgIndex, targets); - + if (ntargets == 0) + return; + for (i = 0; i < ntargets; i++) { if (overwriteAnimTgt) @@ -676,7 +673,9 @@ static void Cmd_createvisualtaskontargets(void) } numArgs = GetBattleAnimMoveTargets(battlerArgIndex, targets); - + if (numArgs == 0) + return; + for (i = 0; i < numArgs; i++) { gBattleAnimArgs[battlerArgIndex] = targets[i];