diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp index 970080ca26..a7ddecc44a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp @@ -573,10 +573,7 @@ void flee_battle(ConsoleHandle& console, ProControllerContext& context){ } } -WildBattleExitResult exit_wild_battle( - Logger& logger, WildBattleExitActions& actions, - bool stop_on_move_learn, bool prevent_evolution -){ +bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution){ // For move learning, there are two dialog selection boxes in a row // we need to decline the first one and accept the second one, so mashing B won't work // The first one will occur after an Advance Battle Dialog @@ -588,70 +585,113 @@ WildBattleExitResult exit_wild_battle( bool evolved = false; while (true){ if (errors > 5 || loops > 5){ - return WildBattleExitResult::Failed; + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "exit_wild_battle(): Failed to exit battle.", + console + ); } - WildBattleExitEvent event = first_attempt - ? actions.mash_b_until_battle_exited() - : actions.press_until_dialog(rejected_first_box ? BUTTON_A : BUTTON_B); + BlackScreenWatcher battle_exited(COLOR_RED); + AdvanceBattleDialogWatcher advance_dialog(COLOR_RED); + BattleLearnDialogWatcher move_learn_select(COLOR_RED); - switch (event){ - case WildBattleExitEvent::BattleExited: + context.wait_for_all_requests(); + WallClock deadline = current_time() + 30s; + int ret; + if (first_attempt){ + ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_mash_button(context, BUTTON_B, 20000ms); + }, + { battle_exited } + ); + }else{ + ret = run_until( + console, context, + [deadline, rejected_first_box](ProControllerContext& context){ + pbf_wait(context, 1000ms); // give the watchers a chance to detect something + while (current_time() < deadline){ + pbf_press_button(context, rejected_first_box ? BUTTON_A : BUTTON_B, 200ms, 1800ms); + // Presses are queued, so without this the loop queues a minute of them + // before the deadline and keeps pressing long after it. + context.wait_for_all_requests(); + } + }, + { battle_exited, advance_dialog, move_learn_select } + ); + } + + BattleDialogWatcher evolution_started(COLOR_RED); + int ret2; + + switch (ret){ + case 0: // check for the evolution screen - if (actions.evolution_started()){ - logger.log("Evolution detected."); + context.wait_for_all_requests(); + ret2 = run_until( + console, context, + [](ProControllerContext& context){ + pbf_wait(context, 2000ms); + }, + { evolution_started } + ); + + if (ret2 == 0){ + console.log("Evolution detected."); evolved = true; if (!prevent_evolution){ // make sure B isn't pressed too soon, which would cancel the evolution - actions.wait_for_evolution(); + pbf_wait(context, 20000ms); } rejected_first_box = false; continue; // press B as in other cases, and handle any move learning loops that might come up } - logger.log("Battle exited."); - return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited; - case WildBattleExitEvent::AdvanceDialog: - logger.log("Battle Advance arrow detected."); - actions.advance_dialog(); + console.log("Battle exited."); + return move_learned; + case 1: + console.log("Battle Advance arrow detected."); + pbf_press_button(context, BUTTON_B, 200ms, 800ms); rejected_first_box = false; continue; - case WildBattleExitEvent::MoveLearnPrompt: + case 2: if (stop_on_move_learn){ - logger.log("Move learn detected."); - return WildBattleExitResult::StoppedOnMoveLearn; + console.log("Move learn detected."); + return true; }else if (rejected_first_box){ loops++; - logger.log("Declined to learn new move."); - actions.answer_move_prompt(BUTTON_A); + console.log("Declined to learn new move."); + pbf_press_button(context, BUTTON_A, 200ms, 0ms); }else{ - actions.answer_move_prompt(BUTTON_B); + pbf_press_button(context, BUTTON_B, 200ms, 0ms); rejected_first_box = true; move_learned = true; } continue; - case WildBattleExitEvent::None: + default: // An evolution ends on the overworld without another black screen, so nothing above // can see that it is over. Check for the overworld now, instead of pressing B for // another 30 seconds first. if (evolved){ evolved = false; first_attempt = false; - if (actions.return_to_overworld()){ - logger.log("Battle exited."); - return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited; + if (in_overworld(console, context)){ + console.log("Battle exited."); + return move_learned; } continue; } if (first_attempt){ - logger.log("Loop detected."); + console.log("Loop detected."); first_attempt = false; continue; } - logger.log("Failed to detect expected battle dialogs."); + console.log("Failed to detect expected battle dialogs."); errors++; - if (actions.return_to_overworld()){ - logger.log("Battle exited."); - return move_learned ? WildBattleExitResult::ExitedAfterMoveLearn : WildBattleExitResult::Exited; + if (in_overworld(console, context)){ + console.log("Battle exited."); + return move_learned; } rejected_first_box = false; continue; @@ -659,124 +699,6 @@ WildBattleExitResult exit_wild_battle( } } -namespace{ - -class ConsoleWildBattleExitActions : public WildBattleExitActions{ -public: - ConsoleWildBattleExitActions(ConsoleHandle& console, ProControllerContext& context) - : m_console(console), m_context(context) - {} - - virtual WildBattleExitEvent mash_b_until_battle_exited() override{ - BlackScreenWatcher battle_exited(COLOR_RED); - m_context.wait_for_all_requests(); - int ret = run_until( - m_console, m_context, - [](ProControllerContext& context){ - pbf_mash_button(context, BUTTON_B, 20000ms); - }, - { battle_exited } - ); - return ret == 0 ? WildBattleExitEvent::BattleExited : WildBattleExitEvent::None; - } - virtual WildBattleExitEvent press_until_dialog(Button button) override{ - BlackScreenWatcher battle_exited(COLOR_RED); - AdvanceBattleDialogWatcher advance_dialog(COLOR_RED); - BattleLearnDialogWatcher move_learn_select(COLOR_RED); - m_context.wait_for_all_requests(); - WallClock deadline = current_time() + 30s; - int ret = run_until( - m_console, m_context, - [deadline, button](ProControllerContext& context){ - pbf_wait(context, 1000ms); // give the watchers a chance to detect something - while (current_time() < deadline){ - pbf_press_button(context, button, 200ms, 1800ms); - // Presses are queued, so without this the loop queues a minute of them - // before the deadline and keeps pressing long after it. - context.wait_for_all_requests(); - } - }, - { battle_exited, advance_dialog, move_learn_select } - ); - switch (ret){ - case 0: - return WildBattleExitEvent::BattleExited; - case 1: - return WildBattleExitEvent::AdvanceDialog; - case 2: - return WildBattleExitEvent::MoveLearnPrompt; - default: - return WildBattleExitEvent::None; - } - } - virtual bool evolution_started() override{ - BattleDialogWatcher evolution_started(COLOR_RED); - m_context.wait_for_all_requests(); - int ret = run_until( - m_console, m_context, - [](ProControllerContext& context){ - pbf_wait(context, 2000ms); - }, - { evolution_started } - ); - return ret == 0; - } - virtual void wait_for_evolution() override{ - pbf_wait(m_context, 20000ms); - } - virtual void advance_dialog() override{ - pbf_press_button(m_context, BUTTON_B, 200ms, 800ms); - } - virtual void answer_move_prompt(Button button) override{ - pbf_press_button(m_context, button, 200ms, 0ms); - } - virtual bool return_to_overworld() override{ - StartMenuWatcher start_menu_open(COLOR_RED); - // attempt to exit any screen that might be open (party, bag, etc) - pbf_mash_button(m_context, BUTTON_B, 500ms); - // overworld detection: look for the start menu - m_context.wait_for_all_requests(); - int ret = run_until( - m_console, m_context, - [](ProControllerContext& context){ - pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms); - pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms); - pbf_press_button(context, BUTTON_PLUS, 200ms, 800ms); - }, - { start_menu_open } - ); - if (ret == 0){ - pbf_mash_button(m_context, BUTTON_B, 500ms); - } - m_context.wait_for_all_requests(); - return ret == 0; - } - -private: - ConsoleHandle& m_console; - ProControllerContext& m_context; -}; - -} - -bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution){ - ConsoleWildBattleExitActions actions(console, context); - switch (exit_wild_battle(console.logger(), actions, stop_on_move_learn, prevent_evolution)){ - case WildBattleExitResult::Exited: - return false; - case WildBattleExitResult::ExitedAfterMoveLearn: - case WildBattleExitResult::StoppedOnMoveLearn: - return true; - case WildBattleExitResult::Failed: - break; - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "exit_wild_battle(): Failed to exit battle.", - console - ); -} - void open_party_menu_from_overworld(ConsoleHandle& console, ProControllerContext& context, StartMenuContext menu_context){ uint16_t errors = 0; bool start_menu_is_open = false; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h index e802a4be6d..b896431aa1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h @@ -9,7 +9,6 @@ #ifndef PokemonAutomation_PokemonFRLG_Navigation_H #define PokemonAutomation_PokemonFRLG_Navigation_H -#include "Common/Cpp/Logging/AbstractLogger.h" #include "CommonFramework/Language.h" #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -75,57 +74,6 @@ void flee_battle(ConsoleHandle& console, ProControllerContext& context); // Returns true if a move was learned (even if it was rejected) and false otherwise. bool exit_wild_battle(ConsoleHandle& console, ProControllerContext& context, bool stop_on_move_learn, bool prevent_evolution); -// What a step of exit_wild_battle() saw. -enum class WildBattleExitEvent{ - BattleExited, // The screen went black as the battle ended. - AdvanceDialog, // A battle dialog is waiting for a button. - MoveLearnPrompt, // A Pokemon is trying to learn a move. - None, // Nothing was seen before the step timed out. -}; - -// The game steps that exit_wild_battle() takes. They are injected so that its decisions (what to -// press after each dialog, when to give up) run in tests without a Switch. -class WildBattleExitActions{ -public: - virtual ~WildBattleExitActions() = default; - - // Mash B for up to 20 seconds, until the battle ends. - virtual WildBattleExitEvent mash_b_until_battle_exited() = 0; - - // Press `button` every 2 seconds for up to 30 seconds, until the battle ends, a battle dialog - // waits for a button, or a move-learn prompt opens. - virtual WildBattleExitEvent press_until_dialog(Button button) = 0; - - // Right after the battle ends, check for a dialog, which means a Pokemon is evolving. - virtual bool evolution_started() = 0; - - // Wait for an evolution to finish without pressing B, which would cancel it. - virtual void wait_for_evolution() = 0; - - // Close a battle dialog that is waiting for a button. - virtual void advance_dialog() = 0; - - // Answer a move-learn prompt with `button`. - virtual void answer_move_prompt(Button button) = 0; - - // Close any open screen, then check for the overworld by opening the start menu. Returns true - // if it opened; it is closed again. - virtual bool return_to_overworld() = 0; -}; - -enum class WildBattleExitResult{ - Exited, - ExitedAfterMoveLearn, // A move-learn prompt came up and the move was declined. - StoppedOnMoveLearn, // Stopped at a move-learn prompt, as stop_on_move_learn asks. - Failed, -}; - -// The decisions of exit_wild_battle(), taking its game steps from `actions`. -WildBattleExitResult exit_wild_battle( - Logger& logger, WildBattleExitActions& actions, - bool stop_on_move_learn, bool prevent_evolution -); - // Starting from the start menu, a sub-screen of the start menu, or the overworld, navigate to the party screen enum class StartMenuContext { STANDARD, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 49d9b56265..94dc41dd85 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -28,7 +28,6 @@ #include "Inference/PokemonFRLG_TrainerIdReader.h" #include "Inference/PokemonFRLG_WildEncounterReader.h" #include "Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.h" -#include "PokemonFRLG_Navigation.h" #include "Tests/TestUtils.h" #include "PokemonFRLG_Tests.h" @@ -37,233 +36,6 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -namespace{ - -class NullLogger : public Logger{ -public: - virtual void log(const std::string&, Color) override{} -}; - -std::string button_name(Button button){ - return button == BUTTON_A ? "A" : button == BUTTON_B ? "B" : "?"; -} - -// Steps that return scripted events, and record what exit_wild_battle() asked for. -class ScriptedWildBattleExit : public WildBattleExitActions{ -public: - // Returned by the press steps in turn; None once used up. Mashing B only watches for the - // battle to end, so it takes the next event only if that is BattleExited. - std::vector events; - // Returned by evolution_started() in turn; false once used up. - std::vector evolutions; - // How many overworld checks fail before one finds it. - int overworld_misses = 0; - - std::vector steps; - // Seconds spent in steps that ran to their timeout, and waiting for an evolution: time in - // which the player sees nothing happen. - int idle_seconds = 0; - - virtual WildBattleExitEvent mash_b_until_battle_exited() override{ - steps.push_back("mash-B"); - if (m_next_event < events.size() && events[m_next_event] == WildBattleExitEvent::BattleExited){ - return events[m_next_event++]; - } - idle_seconds += 20; - return WildBattleExitEvent::None; - } - virtual WildBattleExitEvent press_until_dialog(Button button) override{ - steps.push_back("press-" + button_name(button)); - WildBattleExitEvent event = next_event(); - if (event == WildBattleExitEvent::None){ - idle_seconds += 30; - } - return event; - } - virtual bool evolution_started() override{ - steps.push_back("evolution?"); - return m_next_evolution < evolutions.size() && evolutions[m_next_evolution++]; - } - virtual void wait_for_evolution() override{ - steps.push_back("wait"); - idle_seconds += 20; - } - virtual void advance_dialog() override{ - steps.push_back("advance"); - } - virtual void answer_move_prompt(Button button) override{ - steps.push_back("answer-" + button_name(button)); - } - virtual bool return_to_overworld() override{ - steps.push_back("overworld?"); - return overworld_misses-- <= 0; - } - -private: - WildBattleExitEvent next_event(){ - return m_next_event < events.size() ? events[m_next_event++] : WildBattleExitEvent::None; - } - - size_t m_next_event = 0; - size_t m_next_evolution = 0; -}; - -struct WildBattleExitScenario{ - std::string name; - std::vector events = {}; - std::vector evolutions = {}; - int overworld_misses = 0; - bool stop_on_move_learn = false; - bool prevent_evolution = false; - WildBattleExitResult expected_result = WildBattleExitResult::Exited; - std::string expected_steps; - int expected_idle_seconds = 0; -}; - -std::string result_name(WildBattleExitResult result){ - switch (result){ - case WildBattleExitResult::Exited: return "Exited"; - case WildBattleExitResult::ExitedAfterMoveLearn: return "ExitedAfterMoveLearn"; - case WildBattleExitResult::StoppedOnMoveLearn: return "StoppedOnMoveLearn"; - case WildBattleExitResult::Failed: return "Failed"; - } - return "?"; -} - -class Test_ExitWildBattle : public UnitTest{ -public: - Test_ExitWildBattle(WildBattleExitScenario scenario) - : UnitTest("PokemonFRLG::ExitWildBattle - " + scenario.name) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - ScriptedWildBattleExit actions; - actions.events = m_scenario.events; - actions.evolutions = m_scenario.evolutions; - actions.overworld_misses = m_scenario.overworld_misses; - - NullLogger logger; - const WildBattleExitResult result = exit_wild_battle( - logger, actions, m_scenario.stop_on_move_learn, m_scenario.prevent_evolution - ); - if (result != m_scenario.expected_result){ - return "Expected " + result_name(m_scenario.expected_result) + ", got " + result_name(result); - } - std::string steps; - for (const std::string& step : actions.steps){ - if (!steps.empty()){ - steps += ' '; - } - steps += step; - } - if (steps != m_scenario.expected_steps){ - return "Expected steps: " + m_scenario.expected_steps + "\nActual steps: " + steps; - } - if (actions.idle_seconds != m_scenario.expected_idle_seconds){ - return "Expected " + std::to_string(m_scenario.expected_idle_seconds) + " idle seconds, got " - + std::to_string(actions.idle_seconds); - } - return true; - } - -private: - const WildBattleExitScenario m_scenario; -}; - -void add_tests_ExitWildBattle(UnitTestDatabase& database){ - using enum WildBattleExitEvent; - const WildBattleExitScenario scenarios[] = { - { - .name = "battle exits", - .events = {BattleExited}, - .expected_steps = "mash-B evolution?", - }, - { - // Mashing B did not end the battle, so it presses B until a dialog shows. - .name = "dialog is advanced", - .events = {AdvanceDialog, BattleExited}, - .expected_steps = "mash-B press-B advance press-B evolution?", - .expected_idle_seconds = 20, - }, - { - // Decline "forget a move?" with B, then confirm "stop learning?" with A. - .name = "new move is declined", - .events = {MoveLearnPrompt, MoveLearnPrompt, BattleExited}, - .expected_result = WildBattleExitResult::ExitedAfterMoveLearn, - .expected_steps = "mash-B press-B answer-B press-A answer-A press-A evolution?", - .expected_idle_seconds = 20, - }, - { - .name = "stops at a new move when asked", - .events = {MoveLearnPrompt}, - .stop_on_move_learn = true, - .expected_result = WildBattleExitResult::StoppedOnMoveLearn, - .expected_steps = "mash-B press-B", - .expected_idle_seconds = 20, - }, - { - // After an evolution nothing ends the battle again, so the overworld is checked - // right after mashing B, without pressing B until the deadline first. - .name = "evolution", - .events = {BattleExited}, - .evolutions = {true}, - .expected_steps = "mash-B evolution? wait mash-B overworld?", - .expected_idle_seconds = 40, - }, - { - // Mashing B right away cancels the evolution. - .name = "prevented evolution", - .events = {BattleExited}, - .evolutions = {true}, - .prevent_evolution = true, - .expected_steps = "mash-B evolution? mash-B overworld?", - .expected_idle_seconds = 20, - }, - { - // A move to learn after the evolution keeps the overworld from being found, so it - // goes back to pressing B, which finds the move-learn prompt. - .name = "evolution then new move is declined", - .events = {BattleExited, MoveLearnPrompt, MoveLearnPrompt}, - .evolutions = {true}, - .overworld_misses = 1, - .expected_result = WildBattleExitResult::ExitedAfterMoveLearn, - .expected_steps = - "mash-B evolution? wait mash-B overworld? " - "press-B answer-B press-A answer-A press-A overworld?", - .expected_idle_seconds = 70, - }, - { - // If the overworld is not found after the evolution, it goes back to pressing B. - .name = "evolution and overworld not found", - .events = {BattleExited}, - .evolutions = {true}, - .overworld_misses = 100, - .expected_result = WildBattleExitResult::Failed, - .expected_steps = - "mash-B evolution? wait mash-B overworld? " - "press-B overworld? press-B overworld? press-B overworld? " - "press-B overworld? press-B overworld? press-B overworld?", - .expected_idle_seconds = 220, - }, - { - .name = "gives up when the overworld is not found", - .overworld_misses = 100, - .expected_result = WildBattleExitResult::Failed, - .expected_steps = - "mash-B press-B overworld? press-B overworld? press-B overworld? " - "press-B overworld? press-B overworld? press-B overworld?", - .expected_idle_seconds = 200, - }, - }; - for (const WildBattleExitScenario& scenario : scenarios){ - database.add(scenario); - } -} - -} - - void add_tests(UnitTestDatabase& database){ add_tests_AdvanceWhiteDialogDetector(database); add_tests_AdvanceBattleDialogDetector(database); @@ -277,7 +49,6 @@ void add_tests(UnitTestDatabase& database){ add_tests_BoxSelectionModeDetector(database); add_tests_BoxSorter(database); add_tests_EliteFourRoomDetector(database); - add_tests_ExitWildBattle(database); add_tests_HallOfFameSavingDetector(database); add_tests_PartyEmptySlotDetector(database); add_tests_PartyHeldItemDetector(database);