diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index a66349069c..bfd0ce5538 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -345,6 +345,150 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ } +namespace{ + +// The quests read from the panel, the options that matter, and the plan +// plan_quests() must make. The default exclusions table has one row: +// auto-10, skip. +struct BBQuestPlanScenario{ + std::string name; + std::vector quests; + std::vector> extra_exclusions = {}; + uint8_t num_eggs = 1; + uint8_t eggs_hatched = 0; + BBQOption::OOEggs out_of_eggs = BBQOption::OOEggs::Reroll; + std::vector expected_to_do = {}; + std::vector expected_reroll_slots = {}; + bool expected_reroll_all = false; +}; + +std::string quests_to_string(const std::vector& quests){ + std::string ret; + for (BBQuests quest : quests){ + ret += std::to_string((int)quest) + " "; + } + return ret; +} +std::string slots_to_string(const std::vector& slots){ + std::string ret; + for (size_t slot : slots){ + ret += std::to_string(slot) + " "; + } + return ret; +} + +class Test_BBQuestPlan : public UnitTest{ +public: + Test_BBQuestPlan(BBQuestPlanScenario scenario) + : UnitTest("PokemonSV::BBQuestPlan - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + BBQOption options; + options.NUM_EGGS.set(m_scenario.num_eggs); + options.OUT_OF_EGGS.set(m_scenario.out_of_eggs); + for (const auto& [quest, action] : m_scenario.extra_exclusions){ + auto row = std::make_unique(options.QUEST_EXCLUSIONS); + row->quest.set(quest); + row->action.set(action); + options.QUEST_EXCLUSIONS.append_row(std::move(row)); + } + + NullLogger logger; + const BBQuestPlan plan = plan_quests(logger, options, m_scenario.quests, m_scenario.eggs_hatched); + if (plan.to_do != m_scenario.expected_to_do){ + return "Expected quests to do: " + quests_to_string(m_scenario.expected_to_do) + + "got: " + quests_to_string(plan.to_do); + } + if (plan.reroll_slots != m_scenario.expected_reroll_slots){ + return "Expected reroll slots: " + slots_to_string(m_scenario.expected_reroll_slots) + + "got: " + slots_to_string(plan.reroll_slots); + } + if (plan.reroll_all != m_scenario.expected_reroll_all){ + return std::string("Expected reroll_all = ") + (m_scenario.expected_reroll_all ? "true" : "false"); + } + return true; + } + +private: + const BBQuestPlanScenario m_scenario; +}; + +} + +static void add_tests_BBQuestPlan(UnitTestDatabase& database){ + using enum BBQuests; + const BBQuestPlanScenario scenarios[] = { + { + .name = "default table skips auto-10", + .quests = {make_tm, auto_10, sneak_up}, + .expected_to_do = {make_tm, sneak_up}, + }, + { + .name = "quests not possible are left out", + .quests = {pickup_10, make_tm, UnableToDetect}, + .expected_to_do = {make_tm}, + }, + { + .name = "nothing doable rerolls all", + .quests = {pickup_10, auto_10, catch_flying}, + .expected_reroll_all = true, + }, + { + // The reroll replaces a quest, so the next read has something new. + .name = "a reroll row rerolls that slot", + .quests = {make_tm, sneak_up, travel_500}, + .extra_exclusions = {{travel_500, BBQAction::reroll}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + // A reroll is already a change, so nothing more is rerolled. + .name = "a rerolled slot with nothing doable does not reroll all", + .quests = {pickup_10, sneak_up, auto_10}, + .extra_exclusions = {{sneak_up, BBQAction::reroll}}, + .expected_reroll_slots = {1}, + }, + { + // Each row for a quest applies, in table order. + .name = "every table row for a quest applies", + .quests = {travel_500, auto_10}, + .extra_exclusions = { + {travel_500, BBQAction::run}, + {auto_10, BBQAction::run}, + {travel_500, BBQAction::run}, + }, + .expected_to_do = {travel_500, travel_500, auto_10}, + }, + { + .name = "egg quest is done while eggs remain", + .quests = {make_tm, hatch_egg, sneak_up}, + .expected_to_do = {make_tm, hatch_egg, sneak_up}, + }, + { + // The egg quest is rerolled where it is on the panel, not always + // in the top slot. + .name = "out of eggs rerolls the egg quest slot", + .quests = {make_tm, hatch_egg, sneak_up}, + .eggs_hatched = 1, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {1}, + }, + { + .name = "out of eggs with keep going leaves the egg quest", + .quests = {make_tm, hatch_egg, sneak_up}, + .eggs_hatched = 1, + .out_of_eggs = BBQOption::OOEggs::KeepGoing, + .expected_to_do = {make_tm, sneak_up}, + }, + }; + for (const BBQuestPlanScenario& scenario : scenarios){ + database.add(scenario); + } +} + + void add_tests_BBQ(UnitTestDatabase& database){ database.add(); @@ -356,6 +500,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_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 280f3869db..41f77717c6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -76,6 +76,31 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& return read_BP_from_image(stream.logger(), screen); } +namespace{ + +//Open the quest panel from the overworld. Returns false if it did not open. +bool open_quest_panel(VideoStream& stream, ProControllerContext& context){ + WhiteButtonWatcher panel(COLOR_BLUE, WhiteButton::ButtonB, {0.484, 0.117, 0.022, 0.037}); + int result = run_until( + stream, context, + [&](ProControllerContext& context){ + for (int i = 0; i < 6; i++){ + pbf_press_dpad(context, DPAD_RIGHT, 400ms, 160ms); + pbf_wait(context, 1600ms); + context.wait_for_all_requests(); + } + }, + {{ panel }} + ); + if (result == 0){ + stream.log("Found quest panel."); + } + context.wait_for_all_requests(); + return result == 0; +} + +} + std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -125,177 +150,159 @@ std::vector read_quests( return quest_list; } -std::vector process_quest_list( +namespace{ + +//Out of eggs, the egg quest cannot be done. OUT_OF_EGGS decides what happens to it. +void plan_out_of_eggs(Logger& logger, const BBQOption& BBQ_OPTIONS, size_t slot, BBQuestPlan& plan){ + switch (BBQ_OPTIONS.OUT_OF_EGGS){ + case BBQOption::OOEggs::Reroll: + logger.log("Reroll selected. Rerolling Egg quest. New quest will be run in the next batch of quests."); + //logger.log("Warning: This does not handle/check being out of BP!", COLOR_RED); + plan.reroll_slots.push_back(slot); + break; + case BBQOption::OOEggs::KeepGoing: + logger.log("Keep Going selected. Ignoring quest."); + break; + default: + //This case is handled in BBQSoloFarmer. + throw InternalProgramError(&logger, PA_CURRENT_FUNCTION, "OOEggs is Stop in process_quest_list()."); + } +} + +//Apply each exclusions table row for the quest, in table order. Returns false if no row is for it. +bool plan_from_exclusions( + Logger& logger, + const std::vector>& exclusions_table, + BBQuests quest, size_t slot, + BBQuestPlan& plan +){ + bool quest_in_table = false; + for (const std::unique_ptr& row : exclusions_table){ + if (quest != row->quest){ + continue; + } + logger.log("Quest found in inclusions/exclusions table."); + quest_in_table = true; + switch (row->action){ + case BBQAction::run: + logger.log("Run selected. Adding quest to list."); + plan.to_do.push_back(quest); + break; + case BBQAction::reroll: + logger.log("Reroll selected. Rerolling quest. New quest will be run in the next batch of quests."); + plan.reroll_slots.push_back(slot); + break; + case BBQAction::skip: + logger.log("Skip selected. Skipping quest."); + break; + } + } + 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(); + } + + //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); +} + +//This does not handle out of BP. +void reroll_all_slots( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + size_t slots +){ + open_quest_panel(stream, context); + + for (size_t i = 0; i < slots; i++){ + pbf_press_button(context, BUTTON_A, 160ms, 400ms); + pbf_press_button(context, BUTTON_A, 160ms, 400ms); //Yes. + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + pbf_press_dpad(context, DPAD_DOWN, 160ms, 160ms); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + } + //Close quest panel - mash b + press_Bs_to_back_to_overworld(info, stream, context); +} + +} + +BBQuestPlan plan_quests( + Logger& logger, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, uint8_t eggs_hatched ){ - std::vector quests_to_do; - bool rerolled = false; + BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); - int questpos = 0; - stream.log("Processing quests."); - //Put all do-able quests into a different list - for (auto n : quest_list){ + logger.log("Processing quests."); + for (size_t slot = 0; slot < quest_list.size(); slot++){ + const BBQuests n = quest_list[slot]; if (not_possible_quests.contains(n)){ - stream.log("Quest not possible"); - }else{ - //Check eggs remaining. - if (n == BBQuests::hatch_egg && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ - stream.log("Out of eggs! Quest not possible."); - - switch (BBQ_OPTIONS.OUT_OF_EGGS){ - case BBQOption::OOEggs::Reroll: - { - stream.log("Reroll selected. Rerolling Egg quest. New quest will be run in the next batch of quests."); - //stream.log("Warning: This does not handle/check being out of BP!", COLOR_RED); - - WhiteButtonWatcher right_panel(COLOR_BLUE, WhiteButton::ButtonB, {0.484, 0.117, 0.022, 0.037}); - int result = run_until( - stream, context, - [&](ProControllerContext& context){ - for (int i = 0; i < 6; i++){ - pbf_press_dpad(context, DPAD_RIGHT, 400ms, 160ms); - pbf_wait(context, 1600ms); - context.wait_for_all_requests(); - } - }, - {{ right_panel }} - ); - if (result == 0){ - stream.log("Found quest panel."); - } - context.wait_for_all_requests(); - - 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(); - - rerolled = true; - press_Bs_to_back_to_overworld(info, stream, context); - - break; - } - case BBQOption::OOEggs::KeepGoing: - stream.log("Keep Going selected. Ignoring quest."); - break; - default: - //This case is handled in BBQSoloFarmer. - stream.log("OOEggs is Stop in process_quest_list()."); - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "OOEggs is Stop in process_quest_list().", - stream - ); - break; - } - }else{ - bool quest_in_table = false; - for (const std::unique_ptr& row : exclusions_table){ - if(n == row->quest){ - stream.log("Quest found in inclusions/exclusions table."); - quest_in_table = true; - - WhiteButtonWatcher rp2(COLOR_BLUE, WhiteButton::ButtonB, {0.484, 0.117, 0.022, 0.037}); - int result2; - switch (row->action){ - case BBQAction::run: - stream.log("Run selected. Adding quest to list."); - quests_to_do.push_back(n); - break; - case BBQAction::reroll: - stream.log("Reroll selected. Rerolling quest. New quest will be run in the next batch of quests."); - result2 = run_until( - stream, context, - [&](ProControllerContext& context){ - for (int i = 0; i < 6; i++){ - pbf_press_dpad(context, DPAD_RIGHT, 400ms, 160ms); - pbf_wait(context, 1600ms); - context.wait_for_all_requests(); - } - }, - {{ rp2 }} - ); - if (result2 == 0){ - stream.log("Found quest panel."); - } - context.wait_for_all_requests(); - - //Move cursor down to quest - for (int i = 0; i < questpos; i++){ - pbf_press_dpad(context, DPAD_DOWN, 160ms, 160ms); - pbf_wait(context, 800ms); - context.wait_for_all_requests(); - } - - //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(); - - //Prevent error/rerolling again at the end (allows program to read the rerolled quests) - rerolled = true; - - press_Bs_to_back_to_overworld(info, stream, context); - break; - case BBQAction::skip: - stream.log("Skip selected. Skipping quest."); - break; - } - } - } - if(!quest_in_table){ - stream.log("Quest not in inclusion/exclusions table. Adding to list."); - quests_to_do.push_back(n); - } - } + logger.log("Quest not possible"); + continue; + } + //Check eggs remaining. + if (n == BBQuests::hatch_egg && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ + logger.log("Out of eggs! Quest not possible."); + plan_out_of_eggs(logger, BBQ_OPTIONS, slot, plan); + continue; + } + if (!plan_from_exclusions(logger, exclusions_table, n, slot, plan)){ + logger.log("Quest not in inclusion/exclusions table. Adding to list."); + plan.to_do.push_back(n); } - questpos++; } - + //Check that quests_to_do is not empty (after completing all quests on the list, be sure to erase it. //Lag might be a problem in multi - look into making slots like menu-left navigation - if (!rerolled && quests_to_do.size() == 0){ - stream.log("No possible quests! Rerolling all quests."); + plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + return plan; +} - //Open quest panel and reroll all quests. - //This does not handle out of BP. - WhiteButtonWatcher panel(COLOR_BLUE, WhiteButton::ButtonB, {0.484, 0.117, 0.022, 0.037}); - int result = run_until( - stream, context, - [&](ProControllerContext& context){ - for (int i = 0; i < 6; i++){ - pbf_press_dpad(context, DPAD_RIGHT, 400ms, 160ms); - pbf_wait(context, 1600ms); - context.wait_for_all_requests(); - } - }, - {{ panel }} - ); - if (result == 0){ - stream.log("Found quest panel."); - } - context.wait_for_all_requests(); +std::vector process_quest_list( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS, + const std::vector& quest_list, + uint8_t eggs_hatched +){ + BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); - for (string::size_type i = 0; i < quest_list.size(); i++){ - pbf_press_button(context, BUTTON_A, 160ms, 400ms); - pbf_press_button(context, BUTTON_A, 160ms, 400ms); //Yes. - pbf_wait(context, 800ms); - context.wait_for_all_requests(); - pbf_press_dpad(context, DPAD_DOWN, 160ms, 160ms); - pbf_wait(context, 800ms); - context.wait_for_all_requests(); - } - //Close quest panel - mash b - press_Bs_to_back_to_overworld(info, stream, context); + for (size_t slot : plan.reroll_slots){ + reroll_slot(info, stream, context, slot); } + if (plan.reroll_all){ + stream.log("No possible quests! Rerolling all quests."); + reroll_all_slots(info, stream, context, quest_list.size()); + } + + //Disabled since 2025-05, when the exclusions table gained its reroll action. Before that, + //nothing doable stopped the program here, because it usually meant the game language + //option did not match the game and no quest could be read. Now all quests are rerolled + //above instead, which also covers a panel of only excluded quests. /* - if (!rerolled && quests_to_do.size() == 0){ + if (plan.reroll_all){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, "No possible quests! Check language selection.", @@ -303,7 +310,7 @@ std::vector process_quest_list( ); } */ - return quests_to_do; + return plan.to_do; } bool process_and_do_quest( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 8e4bac1bf2..bb16917266 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -57,6 +57,23 @@ std::vector read_quests( const BBQOption& BBQ_OPTIONS ); +//What to do with the quests read from the panel. Slot 0 is the top quest on the panel. +struct BBQuestPlan{ + std::vector to_do; //Quests to attempt, in panel order. + std::vector reroll_slots; //Slots to reroll, in panel order. + bool reroll_all = false; //Nothing to do and nothing rerolled: reroll every slot. +}; +//Decide what to do with each quest from the options alone, without touching the game, so +//the decision can be tested. Quests not possible are left out. Out-of-egg hatch quests and +//quests set to reroll in the exclusions table are rerolled. A quest with several table rows +//gets the action of each row, in table order. +BBQuestPlan plan_quests( + Logger& logger, + const BBQOption& BBQ_OPTIONS, + const std::vector& quest_list, + uint8_t eggs_hatched +); + //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(