From 1a7166c2bb86c867d1e7c5b607d0460208f15ada Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Sat, 23 Dec 2023 00:27:40 +0100 Subject: [PATCH] Tests do not allow to use SEND_OUT if the chosen mon is fainted (#3752) * Tests dont allow to send out pokemon with 0 hp * remove test test * handle in-game 0 hp sent out --------- Co-authored-by: Bassoonian --- include/test_runner.h | 2 ++ src/battle_script_commands.c | 29 +++++++++++++++++++++++++++++ test/test_runner_battle.c | 6 ++++++ 3 files changed, 37 insertions(+) diff --git a/include/test_runner.h b/include/test_runner.h index e0df88b0a6..248a0463e5 100644 --- a/include/test_runner.h +++ b/include/test_runner.h @@ -19,6 +19,7 @@ void TestRunner_Battle_CheckSwitch(u32 battlerId, u32 partyIndex); void TestRunner_Battle_CheckAiMoveScores(u32 battlerId); void TestRunner_Battle_AISetScore(const char *file, u32 line, u32 battlerId, u32 moveIndex, s32 score); void TestRunner_Battle_AIAdjustScore(const char *file, u32 line, u32 battlerId, u32 moveIndex, s32 score); +void TestRunner_Battle_InvalidNoHPMon(u32 battlerId, u32 partyIndex); void TestRunner_Battle_CheckBattleRecordActionType(u32 battlerId, u32 recordIndex, u32 actionType); @@ -38,6 +39,7 @@ u32 TestRunner_Battle_GetForcedAbility(u32 side, u32 partyIndex); #define TestRunner_Battle_CheckAiMoveScores(...) (void)0 #define TestRunner_Battle_AISetScore(...) (void)0 #define TestRunner_Battle_AIAdjustScore(...) (void)0 +#define TestRunner_Battle_InvalidNoHPMon(...) (void)0 #define TestRunner_Battle_CheckBattleRecordActionType(...) (void)0 diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 0114bc014a..34a3da0857 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6166,6 +6166,35 @@ static void Cmd_switchindataupdate(void) for (i = 0; i < sizeof(struct BattlePokemon); i++) monData[i] = gBattleResources->bufferB[battler][4 + i]; + // Edge case: the sent out pokemon has 0 HP. This should never happen. + if (gBattleMons[battler].hp == 0) + { + // If it's a test, mark it as invalid. + if (gTestRunnerEnabled) + { + TestRunner_Battle_InvalidNoHPMon(battler, gBattlerPartyIndexes[battler]); + } + // Handle in-game scenario. + else + { + struct Pokemon *party = GetBattlerParty(battler); + // Find the first possible replacement for the not valid pokemon. + for (i = 0; i < PARTY_SIZE; i++) + { + if (IsValidForBattle(&party[i])) + break; + } + // There is valid replacement. + if (i != PARTY_SIZE) + { + gBattlerPartyIndexes[battler] = gBattleStruct->monToSwitchIntoId[battler] = i; + BtlController_EmitGetMonData(battler, BUFFER_A, REQUEST_ALL_BATTLE, gBitTable[gBattlerPartyIndexes[battler]]); + MarkBattlerForControllerExec(battler); + return; + } + } + } + gBattleMons[battler].type1 = gSpeciesInfo[gBattleMons[battler].species].types[0]; gBattleMons[battler].type2 = gSpeciesInfo[gBattleMons[battler].species].types[1]; gBattleMons[battler].type3 = TYPE_MYSTERY; diff --git a/test/test_runner_battle.c b/test/test_runner_battle.c index 5e42057955..1528a3ccf9 100644 --- a/test/test_runner_battle.c +++ b/test/test_runner_battle.c @@ -881,6 +881,12 @@ void TestRunner_Battle_CheckSwitch(u32 battlerId, u32 partyIndex) DATA.aiActionsPlayed[battlerId]++; } +void TestRunner_Battle_InvalidNoHPMon(u32 battlerId, u32 partyIndex) +{ + Test_ExitWithResult(TEST_RESULT_INVALID, "%s: INVALID: %s trying to send out a mon(id: %d) with 0 HP.", + gTestRunnerState.test->filename, BattlerIdentifier(battlerId), gBattlerPartyIndexes[battlerId]); +} + static bool32 CheckComparision(s32 val1, s32 val2, u32 cmp) { switch (cmp)