diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 0ace2abb4f..6dd1d9ef43 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -495,6 +495,128 @@ class Test_BBQuestPlan : public UnitTest{ const BBQuestPlanScenario m_scenario; }; + +// A quest panel with a cursor. A reroll replaces the quest under the cursor +// with the next of `replacements`, unless out of BP. +class SimulatedQuestPanel : public BBQRerollActions{ +public: + std::vector quests; + std::vector replacements; + size_t cursor = 0; + bool panel_opens = true; + bool menu_seen = true; + bool out_of_bp = false; + int missed_reads = 0; // Reads that fail OCR on every slot. + + bool panel_open = false; + bool menu_open = false; + std::vector rerolled_slots; + + virtual bool open_panel() override{ + panel_open = panel_opens; + return panel_open; + } + virtual std::optional open_reroll_menu() override{ + menu_open = true; + if (!menu_seen){ + return std::nullopt; + } + return cursor; + } + virtual void close_reroll_menu() override{ + menu_open = false; + } + virtual void move_cursor(int slots) override{ + cursor = (size_t)((int)cursor + slots); + } + virtual void confirm_reroll() override{ + menu_open = false; + if (out_of_bp){ + return; + } + rerolled_slots.push_back(cursor); + quests[cursor] = replacements[rerolled_slots.size() - 1]; + } + virtual void close_panel() override{ + panel_open = false; + menu_open = false; + } + virtual std::vector read_quests() override{ + if (missed_reads > 0){ + missed_reads--; + return std::vector(quests.size(), BBQuests::UnableToDetect); + } + return quests; + } +}; + +struct BBQRerollScenario{ + std::string name; + std::vector quests; + std::vector replacements = {}; + size_t slot; + size_t cursor = 0; + bool panel_opens = true; + bool menu_seen = true; + bool out_of_bp = false; + int missed_reads = 0; + BBQRerollResult expected; + std::vector expected_rerolled_slots = {}; + std::vector expected_quests; +}; + +std::string reroll_result_to_string(BBQRerollResult result){ + switch (result){ + case BBQRerollResult::Rerolled: return "Rerolled"; + case BBQRerollResult::PanelNotOpened: return "PanelNotOpened"; + case BBQRerollResult::MenuNotFound: return "MenuNotFound"; + case BBQRerollResult::NotReplaced: return "NotReplaced"; + } + return "?"; +} + +class Test_BBQReroll : public UnitTest{ +public: + Test_BBQReroll(BBQRerollScenario scenario) + : UnitTest("PokemonSV::BBQReroll - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + SimulatedQuestPanel panel; + panel.quests = m_scenario.quests; + panel.replacements = m_scenario.replacements; + panel.cursor = m_scenario.cursor; + panel.panel_opens = m_scenario.panel_opens; + panel.menu_seen = m_scenario.menu_seen; + panel.out_of_bp = m_scenario.out_of_bp; + panel.missed_reads = m_scenario.missed_reads; + + NullLogger logger; + std::vector quests = m_scenario.quests; + const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot); + if (result != m_scenario.expected){ + return "Expected " + reroll_result_to_string(m_scenario.expected) + + ", got " + reroll_result_to_string(result); + } + if (panel.rerolled_slots != m_scenario.expected_rerolled_slots){ + return "Expected rerolled slots: " + slots_to_string(m_scenario.expected_rerolled_slots) + + "got: " + slots_to_string(panel.rerolled_slots); + } + if (quests != m_scenario.expected_quests){ + return "Expected quests: " + quests_to_string(m_scenario.expected_quests) + + "got: " + quests_to_string(quests); + } + if (panel.panel_open || panel.menu_open){ + return "Panel left open."; + } + return true; + } + +private: + const BBQRerollScenario m_scenario; +}; + } static void add_tests_BBQuestPlan(UnitTestDatabase& database){ @@ -568,6 +690,100 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ } } +static void add_tests_BBQReroll(UnitTestDatabase& database){ + using enum BBQuests; + const BBQRerollScenario scenarios[] = { + { + .name = "cursor on the top quest", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, catch_any}, + }, + { + // Seen on a Switch: the panel can open with the cursor where it + // was left, so moving down from the top rerolls the wrong quest. + .name = "cursor left below the quest moves up", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 1, + .cursor = 3, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {1}, + .expected_quests = {make_tm, catch_any, sneak_up, hatch_egg}, + }, + { + .name = "fourth quest", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 3, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {3}, + .expected_quests = {make_tm, travel_500, sneak_up, catch_any}, + }, + { + .name = "panel not opened spends nothing", + .quests = {make_tm, sneak_up, travel_500}, + .slot = 2, + .panel_opens = false, + .expected = BBQRerollResult::PanelNotOpened, + .expected_quests = {make_tm, sneak_up, travel_500}, + }, + { + .name = "menu not seen spends nothing", + .quests = {make_tm, sneak_up, travel_500}, + .slot = 2, + .menu_seen = false, + .expected = BBQRerollResult::MenuNotFound, + .expected_quests = {make_tm, sneak_up, travel_500}, + }, + { + .name = "out of BP is not replaced", + .quests = {make_tm, sneak_up, travel_500}, + .slot = 2, + .out_of_bp = true, + .expected = BBQRerollResult::NotReplaced, + .expected_quests = {make_tm, sneak_up, travel_500}, + }, + { + .name = "a missed read is read again", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .missed_reads = 2, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, catch_any}, + }, + { + .name = "three missed reads give up", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .missed_reads = 3, + .expected = BBQRerollResult::NotReplaced, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, travel_500}, + }, + { + // Cannot be told apart from out of BP. + .name = "replaced by the same quest is not replaced", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {travel_500}, + .slot = 2, + .expected = BBQRerollResult::NotReplaced, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, travel_500}, + }, + }; + for (const BBQRerollScenario& scenario : scenarios){ + database.add(scenario); + } +} + + void add_tests_BBQ(UnitTestDatabase& database){ database.add(); @@ -580,6 +796,7 @@ void add_tests_BBQ(UnitTestDatabase& database){ database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); + add_tests_BBQReroll(database); add_tests_BBQuestPlan(database); add_tests_BBQFarmerRounds(database); add_tests_TeraEncounterAction(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 41f77717c6..2861283c87 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -22,6 +22,7 @@ #include "VideoGames/PokemonSV/PokemonSV_Settings.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_GameEntry.h" #include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h" +#include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_GradientArrowDetector.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.h" #include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h" @@ -200,28 +201,67 @@ bool plan_from_exclusions( return quest_in_table; } -void reroll_slot( - const ProgramInfo& info, - VideoStream& stream, ProControllerContext& context, - size_t slot -){ - open_quest_panel(stream, context); - - //Move cursor down to quest - for (size_t i = 0; i < slot; i++){ - pbf_press_dpad(context, DPAD_DOWN, 160ms, 160ms); - pbf_wait(context, 800ms); - context.wait_for_all_requests(); +class ConsoleBBQRerollActions : public BBQRerollActions{ +public: + ConsoleBBQRerollActions( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS + ) + : m_info(info), m_stream(stream), m_context(context), m_options(BBQ_OPTIONS) + {} + + virtual bool open_panel() override{ + return open_quest_panel(m_stream, m_context); + } + virtual std::optional open_reroll_menu() override{ + //The menu opens next to the quest, with an arrow on its top choice. Slots are 0.177 + //apart; the boxes for the top 3 slots were checked on a Switch, the 4th is spaced + //the same way. + GradientArrowWatcher slot0(COLOR_RED, GradientArrowType::RIGHT, {0.475, 0.218, 0.070, 0.095}); + GradientArrowWatcher slot1(COLOR_RED, GradientArrowType::RIGHT, {0.475, 0.395, 0.070, 0.095}); + GradientArrowWatcher slot2(COLOR_RED, GradientArrowType::RIGHT, {0.475, 0.572, 0.070, 0.095}); + GradientArrowWatcher slot3(COLOR_RED, GradientArrowType::RIGHT, {0.475, 0.749, 0.070, 0.095}); + pbf_press_button(m_context, BUTTON_A, 160ms, 400ms); + m_context.wait_for_all_requests(); + int ret = wait_until( + m_stream, m_context, std::chrono::seconds(3), + {slot0, slot1, slot2, slot3} + ); + if (ret < 0){ + return std::nullopt; + } + return (size_t)ret; + } + virtual void close_reroll_menu() override{ + pbf_press_button(m_context, BUTTON_B, 160ms, 400ms); + m_context.wait_for_all_requests(); + } + virtual void move_cursor(int slots) override{ + for (int i = 0; i < std::abs(slots); i++){ + pbf_press_dpad(m_context, slots > 0 ? DPAD_DOWN : DPAD_UP, 160ms, 160ms); + pbf_wait(m_context, 800ms); + } + m_context.wait_for_all_requests(); + } + virtual void confirm_reroll() override{ + pbf_press_button(m_context, BUTTON_A, 160ms, 400ms); + pbf_wait(m_context, 800ms); + m_context.wait_for_all_requests(); + } + virtual void close_panel() override{ + press_Bs_to_back_to_overworld(m_info, m_stream, m_context); + } + virtual std::vector read_quests() override{ + return PokemonSV::read_quests(m_info, m_stream, m_context, m_options); } - //Reroll - pbf_press_button(context, BUTTON_A, 160ms, 400ms); - pbf_press_button(context, BUTTON_A, 160ms, 400ms); - pbf_wait(context, 800ms); - context.wait_for_all_requests(); - - press_Bs_to_back_to_overworld(info, stream, context); -} +private: + const ProgramInfo& m_info; + VideoStream& m_stream; + ProControllerContext& m_context; + const BBQOption& m_options; +}; //This does not handle out of BP. void reroll_all_slots( @@ -246,6 +286,87 @@ void reroll_all_slots( } +BBQRerollResult reroll_quest( + Logger& logger, + BBQRerollActions& actions, + std::vector& quests, + size_t slot +){ + if (!actions.open_panel()){ + return BBQRerollResult::PanelNotOpened; + } + + //The cursor is not always on the top quest when the panel opens; it can be left where it + //was. So open the menu wherever the cursor is, see which quest it opened next to, and move + //from there. Moving can drop a press, so this is tried a few times. + bool menu_on_slot = false; + for (int attempt = 0; attempt < 3 && !menu_on_slot; attempt++){ + std::optional menu_slot = actions.open_reroll_menu(); + if (!menu_slot){ + //Without a menu, B would close the panel instead, so stop here. + break; + } + if (*menu_slot == slot){ + menu_on_slot = true; + break; + } + logger.log("Reroll menu opened on quest " + std::to_string(*menu_slot + 1) + ". Moving to quest " + std::to_string(slot + 1) + "."); + actions.close_reroll_menu(); + actions.move_cursor((int)slot - (int)*menu_slot); + } + if (!menu_on_slot){ + actions.close_panel(); + return BBQRerollResult::MenuNotFound; + } + actions.confirm_reroll(); + actions.close_panel(); + + //A read can miss the quest in the slot, so read up to 3 times before deciding it is + //still the old quest. + for (int read = 0; read < 3; read++){ + std::vector after = actions.read_quests(); + if (slot < after.size() && after[slot] != quests[slot] && after[slot] != BBQuests::UnableToDetect){ + logger.log("Quest " + std::to_string(slot + 1) + " rerolled."); + quests[slot] = after[slot]; + return BBQRerollResult::Rerolled; + } + } + return BBQRerollResult::NotReplaced; +} + +void reroll_quest_slot( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS, + std::vector& quest_list, + size_t slot +){ + ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS); + switch (reroll_quest(stream.logger(), actions, quest_list, slot)){ + case BBQRerollResult::Rerolled: + return; + case BBQRerollResult::PanelNotOpened: + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unable to open the quest panel to reroll a quest.", + stream + ); + case BBQRerollResult::MenuNotFound: + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unable to open the reroll menu for quest " + std::to_string(slot + 1) + ". No BP was spent.", + stream + ); + case BBQRerollResult::NotReplaced: + //Most likely out of BP, which is not a program error. + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::NO_ERROR_REPORT, + "Quest " + std::to_string(slot + 1) + " was not replaced after rerolling it. Check that there is enough BP.", + stream + ); + } +} + BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, @@ -289,8 +410,10 @@ std::vector process_quest_list( ){ BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); + //Each reroll updates the panel the next one checks against. + std::vector panel = quest_list; for (size_t slot : plan.reroll_slots){ - reroll_slot(info, stream, context, slot); + reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot); } if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index bb16917266..4b45e65e49 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_PokemonSV_BlueberryQuests_H #define PokemonAutomation_PokemonSV_BlueberryQuests_H +#include #include #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" @@ -74,6 +75,62 @@ BBQuestPlan plan_quests( uint8_t eggs_hatched ); +//The steps of rerolling one quest on the quest panel. They are injected so that the reroll +//(which quest it pays for, when it gives up) runs in tests without a Switch. +class BBQRerollActions{ +public: + virtual ~BBQRerollActions() = default; + + //Open the quest panel from the overworld. Returns false if it did not open. + virtual bool open_panel() = 0; + + //Press A on the quest under the cursor, which opens its reroll menu. Returns the slot of + //the quest the menu opened next to, or nothing if no menu was seen. + virtual std::optional open_reroll_menu() = 0; + + //Close the reroll menu without rerolling. + virtual void close_reroll_menu() = 0; + + //Move the cursor down this many slots, or up if negative. + virtual void move_cursor(int slots) = 0; + + //Choose to reroll in the open menu. This spends BP. + virtual void confirm_reroll() = 0; + + //Close the quest panel and return to the overworld. + virtual void close_panel() = 0; + + //Open the quest panel and read the quests on it, as read_quests() does. + virtual std::vector read_quests() = 0; +}; + +enum class BBQRerollResult{ + Rerolled, //The quest in the slot was replaced. + PanelNotOpened, //Nothing was spent. + MenuNotFound, //Nothing was spent. + NotReplaced, //The reroll was confirmed, but the slot still shows the same quest. +}; + +//Reroll the quest in `slot` of `quests`, the quests last read from the panel. Only pays once +//the reroll menu is open next to that slot, then reads the panel again to check the quest was +//replaced. On Rerolled, `quests[slot]` is updated to the new quest. +BBQRerollResult reroll_quest( + Logger& logger, + BBQRerollActions& actions, + std::vector& quests, + size_t slot +); + +//Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program +//if the reroll fails. +void reroll_quest_slot( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS, + std::vector& quests, + size_t slot +); + //Determine which quests are possible and which quests are not. (ex. no eggs, reroll egg hatcher.) //Quests not possible are removed from the list. If the list is empty, then reroll all items. std::vector process_quest_list(