diff --git a/include/battle.h b/include/battle.h index a43fc65cd..491273b80 100644 --- a/include/battle.h +++ b/include/battle.h @@ -534,7 +534,6 @@ struct BattleStruct u8 wildVictorySong; u8 dynamicMoveType; u8 wrappedBy[MAX_BATTLERS_COUNT]; - u16 assistPossibleMoves[PARTY_SIZE * MAX_MON_MOVES]; // Each of mons can know max 4 moves. u8 focusPunchBattlerId; u8 battlerPreventingSwitchout; u8 moneyMultiplier:6; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 126ca06fd..bd1b248b2 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -23,6 +23,7 @@ #include "main.h" #include "palette.h" #include "money.h" +#include "malloc.h" #include "bg.h" #include "string_util.h" #include "pokemon_icon.h" @@ -14766,38 +14767,40 @@ static void Cmd_assistattackselect(void) s32 chooseableMovesNo = 0; struct Pokemon *party; s32 monId, moveId; - u16 *validMoves = gBattleStruct->assistPossibleMoves; + u16 *validMoves = Alloc(sizeof(u16) * PARTY_SIZE * MAX_MON_MOVES); - if (GET_BATTLER_SIDE(gBattlerAttacker) != B_SIDE_PLAYER) - party = gEnemyParty; - else - party = gPlayerParty; - - for (monId = 0; monId < PARTY_SIZE; monId++) + if (validMoves != NULL) { - if (monId == gBattlerPartyIndexes[gBattlerAttacker]) - continue; - if (GetMonData(&party[monId], MON_DATA_SPECIES_OR_EGG) == SPECIES_NONE) - continue; - if (GetMonData(&party[monId], MON_DATA_SPECIES_OR_EGG) == SPECIES_EGG) - continue; + if (GET_BATTLER_SIDE(gBattlerAttacker) != B_SIDE_PLAYER) + party = gEnemyParty; + else + party = gPlayerParty; - for (moveId = 0; moveId < MAX_MON_MOVES; moveId++) + for (monId = 0; monId < PARTY_SIZE; monId++) { - s32 i = 0; - u16 move = GetMonData(&party[monId], MON_DATA_MOVE1 + moveId); - - if (sForbiddenMoves[move] & FORBIDDEN_ASSIST) + if (monId == gBattlerPartyIndexes[gBattlerAttacker]) + continue; + if (GetMonData(&party[monId], MON_DATA_SPECIES_OR_EGG) == SPECIES_NONE) + continue; + if (GetMonData(&party[monId], MON_DATA_SPECIES_OR_EGG) == SPECIES_EGG) continue; - validMoves[chooseableMovesNo] = move; - chooseableMovesNo++; + for (moveId = 0; moveId < MAX_MON_MOVES; moveId++) + { + u16 move = GetMonData(&party[monId], MON_DATA_MOVE1 + moveId); + + if (sForbiddenMoves[move] & FORBIDDEN_ASSIST) + continue; + + validMoves[chooseableMovesNo++] = move; + } } } + if (chooseableMovesNo) { gHitMarker &= ~HITMARKER_ATTACKSTRING_PRINTED; - gCalledMove = validMoves[((Random() & 0xFF) * chooseableMovesNo) >> 8]; + gCalledMove = validMoves[Random() % chooseableMovesNo]; gBattlerTarget = GetMoveTarget(gCalledMove, NO_TARGET_OVERRIDE); gBattlescriptCurrInstr = cmd->nextInstr; } @@ -14805,6 +14808,8 @@ static void Cmd_assistattackselect(void) { gBattlescriptCurrInstr = cmd->failInstr; } + + TRY_FREE_AND_SET_NULL(validMoves); } static void Cmd_trysetmagiccoat(void) diff --git a/test/move_effect_assist.c b/test/move_effect_assist.c new file mode 100644 index 000000000..e95d84654 --- /dev/null +++ b/test/move_effect_assist.c @@ -0,0 +1,22 @@ +#include "global.h" +#include "test_battle.h" + +ASSUMPTIONS +{ + ASSUME(gBattleMoves[MOVE_ASSIST].effect == EFFECT_ASSIST); +} + +SINGLE_BATTLE_TEST("Assist fails if there are no valid moves to choose from") +{ + GIVEN { + PLAYER(SPECIES_WOBBUFFET) {Moves(MOVE_ASSIST, MOVE_CELEBRATE, MOVE_METRONOME, MOVE_ME_FIRST); } + PLAYER(SPECIES_WOBBUFFET) {Moves(MOVE_ASSIST, MOVE_ENDURE, MOVE_DRAGON_TAIL, MOVE_SPOTLIGHT); } + OPPONENT(SPECIES_WOBBUFFET); + } WHEN { + TURN { MOVE(player, MOVE_ASSIST); } + } SCENE { + MESSAGE("Wobbuffet used Assist!"); + NOT ANIMATION(ANIM_TYPE_MOVE, MOVE_ASSIST, player); + MESSAGE("But it failed!"); + } +} diff --git a/test/test_runner_battle.c b/test/test_runner_battle.c index ec7ba275c..f4b8aa4fd 100644 --- a/test/test_runner_battle.c +++ b/test/test_runner_battle.c @@ -1227,6 +1227,7 @@ void Move(u32 sourceLine, struct BattlePokemon *battler, struct MoveContext ctx) const struct BattleMove *move = &gBattleMoves[moveId]; if (move->target == MOVE_TARGET_RANDOM || move->target == MOVE_TARGET_BOTH + || move->target == MOVE_TARGET_DEPENDS || move->target == MOVE_TARGET_FOES_AND_ALLY || move->target == MOVE_TARGET_OPPONENTS_FIELD || move->target == MOVE_TARGET_ALL_BATTLERS)