diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 84fd26495c..46d2d349d4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -146,6 +146,8 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga // on the panel, so without this it gets NUM_RETRIES more attempts every // round, forever, e.g. a sneak-up whose target does not spawn. std::set failed_quests; + std::optional> observed_panel; + std::optional> dispatch_panel; auto read_panel = [&](){ std::vector panel = game.read_quests(); forget_quests_gone_from_panel(failed_quests, panel); @@ -168,8 +170,22 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga " quest panel reads. Check the game language and that you have BP to reroll." ); } - quest_list = read_panel(); - quests_to_do = game.process_quest_list(quest_list, eggs_hatched); + const bool reused_panel = observed_panel.has_value(); + if (observed_panel){ + logger.log("Reusing verified quest panel for scheduling."); + quest_list = std::move(*observed_panel); + observed_panel.reset(); + forget_quests_gone_from_panel(failed_quests, quest_list); + }else{ + logger.log("Reading quest panel for scheduling."); + quest_list = read_panel(); + } + BBQuestBatch batch = game.process_quest_list(quest_list, eggs_hatched); + quests_to_do = std::move(batch.to_do); + observed_panel = std::move(batch.observed_panel); + if (reused_panel && !quests_to_do.empty() && !observed_panel){ + dispatch_panel = quest_list; + } // Rerolling costs BP and the failed quests may be doable another // time, so stop instead of spending BP to replace them. @@ -186,10 +202,19 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga quest_list.clear(); } + //Never carry an observation across quest actions or the round's date/reset work. + observed_panel.reset(); const uint64_t completed_before_round = num_completed_quests; for (auto current_quest : quests_to_do){ //Check if quest was already completed (ex. 500 meters completed while navigating to take a photo) - quest_list = read_panel(); + if (dispatch_panel){ + logger.log("Reusing verified quest panel for first dispatch."); + quest_list = std::move(*dispatch_panel); + dispatch_panel.reset(); + }else{ + logger.log("Reading quest panel before dispatch."); + quest_list = read_panel(); + } if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){ logger.log("Current quest exists on list. Doing quest."); bool questSuccess = game.do_quest(current_quest, eggs_hatched); @@ -247,7 +272,7 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ virtual std::vector read_quests() override{ return PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); } - virtual std::vector process_quest_list( + virtual BBQuestBatch process_quest_list( const std::vector& quests, uint8_t eggs_hatched ) override{ return PokemonSV::process_quest_list(m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 86c7865151..63b396fb00 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -13,7 +13,7 @@ #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" -#include "VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h" +#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -33,8 +33,8 @@ class BBQGameActions{ // Decide which of the quests just read are worth attempting this round. // May reroll quests in game, so the returned list can be empty even when - // `quests` is not; the loop then reads the panel again. - virtual std::vector process_quest_list( + // `quests` is not; a complete post-reroll observation can seed selection. + virtual BBQuestBatch process_quest_list( const std::vector& quests, uint8_t eggs_hatched ) = 0; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 3fdfab30ec..8e2f258bbe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -161,6 +161,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, }; + bool reuse_rerolled_panel = false; std::vector events; @@ -171,7 +172,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ } return panel; } - virtual std::vector process_quest_list( + virtual BBQuestBatch process_quest_list( const std::vector& quests, uint8_t ) override{ events.push_back("process"); @@ -181,12 +182,13 @@ class SimulatedBBQGameActions : public BBQGameActions{ doable.push_back(quest); } } - if (doable.empty() && !out_of_bp){ + const bool rerolled = doable.empty() && !out_of_bp; + if (rerolled){ for (BBQuests& slot : panel){ slot = next_new_quest(); } } - return doable; + return {std::move(doable), rerolled && reuse_rerolled_panel && quests.size() == 4 ? std::optional(panel) : std::nullopt}; } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); @@ -263,6 +265,7 @@ struct BBQRoundsScenario{ std::vector new_quests = {}; std::vector> exclusions = {}; BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; + bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; std::string expected; }; @@ -300,6 +303,7 @@ class Test_BBQFarmerRounds : public UnitTest{ if (!m_scenario.new_quests.empty()){ game.new_quests = m_scenario.new_quests; } + game.reuse_rerolled_panel = m_scenario.reuse_rerolled_panel; NullLogger logger; try{ @@ -345,6 +349,22 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ std::vector> all_skipped_but_make_tm = every_doable_blue_quest(BBQAction::skip); all_skipped_but_make_tm.emplace_back(BBQuests::make_tm, BBQAction::run); const BBQRoundsScenario scenarios[] = { + { + .name = "reuses reroll observation for selection and first dispatch only", + .num_quests = 1, + .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect, BBQuests::photo_flying}, + .reuse_rerolled_panel = true, + .expected_completed = 4, + .expected = "read process process do completed read do completed read do completed read do completed end", + }, + { + .name = "three-slot reroll still reads the panel again", + .num_quests = 1, + .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, + .reuse_rerolled_panel = true, + .expected_completed = 3, + .expected = "read process read process read do completed read do completed read do completed end", + }, { // The goal (4) is reached in the middle of round 2. The loop only // checks the goal between rounds, so round 2 finishes and 6 quests @@ -603,6 +623,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ bool menu_seen = true; bool out_of_bp = false; int missed_reads = 0; // Reads that fail OCR on every slot. + std::optional> read_override; bool panel_open = false; bool menu_open = false; @@ -642,7 +663,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ missed_reads--; return std::vector(quests.size(), BBQuests::UnableToDetect); } - return quests; + return read_override.value_or(quests); } }; @@ -656,9 +677,11 @@ struct BBQRerollScenario{ bool menu_seen = true; bool out_of_bp = false; int missed_reads = 0; + std::optional> read_override = {}; BBQRerollResult expected; std::vector expected_rerolled_slots = {}; std::vector expected_quests; + std::optional> expected_observed_panel = {}; }; std::string reroll_result_to_string(BBQRerollResult result){ @@ -687,10 +710,12 @@ class Test_BBQReroll : public UnitTest{ panel.menu_seen = m_scenario.menu_seen; panel.out_of_bp = m_scenario.out_of_bp; panel.missed_reads = m_scenario.missed_reads; + panel.read_override = m_scenario.read_override; NullLogger logger; std::vector quests = m_scenario.quests; - const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot); + std::optional> observed_panel; + const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot, observed_panel); if (result != m_scenario.expected){ return "Expected " + reroll_result_to_string(m_scenario.expected) + ", got " + reroll_result_to_string(result); @@ -703,6 +728,10 @@ class Test_BBQReroll : public UnitTest{ return "Expected quests: " + quests_to_string(m_scenario.expected_quests) + "got: " + quests_to_string(quests); } + if (observed_panel != m_scenario.expected_observed_panel){ + return "Expected observed panel: " + quests_to_string(m_scenario.expected_observed_panel.value_or(std::vector{})) + + ", got: " + quests_to_string(observed_panel.value_or(std::vector{})); + } if (panel.panel_open || panel.menu_open){ return "Panel left open."; } @@ -789,6 +818,47 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ static void add_tests_BBQReroll(UnitTestDatabase& database){ using enum BBQuests; const BBQRerollScenario scenarios[] = { + { + .name = "unknown fourth slot is not reusable", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{make_tm, travel_500, catch_any, UnableToDetect}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, travel_500, catch_any, hatch_egg}, + }, + { + .name = "missing fourth slot is not reusable", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{make_tm, travel_500, catch_any}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, travel_500, catch_any, hatch_egg}, + }, + { + .name = "another changed quest is not reusable", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{photo_canyon, travel_500, catch_any, hatch_egg}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, travel_500, catch_any, hatch_egg}, + }, + { + .name = "complete observation after missed reads is reusable", + .quests = {make_tm, travel_500, sneak_up, hatch_egg}, + .replacements = {catch_any}, + .slot = 2, + .missed_reads = 2, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, travel_500, catch_any, hatch_egg}, + .expected_observed_panel = std::vector{make_tm, travel_500, catch_any, hatch_egg}, + }, { .name = "cursor on the top quest", .quests = {make_tm, sneak_up, travel_500}, @@ -809,6 +879,7 @@ static void add_tests_BBQReroll(UnitTestDatabase& database){ .expected = BBQRerollResult::Rerolled, .expected_rerolled_slots = {1}, .expected_quests = {make_tm, catch_any, sneak_up, hatch_egg}, + .expected_observed_panel = std::vector{make_tm, catch_any, sneak_up, hatch_egg}, }, { .name = "fourth quest", @@ -818,6 +889,7 @@ static void add_tests_BBQReroll(UnitTestDatabase& database){ .expected = BBQRerollResult::Rerolled, .expected_rerolled_slots = {3}, .expected_quests = {make_tm, travel_500, sneak_up, catch_any}, + .expected_observed_panel = std::vector{make_tm, travel_500, sneak_up, catch_any}, }, { .name = "panel not opened spends nothing", diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 2861283c87..9d7576046a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -253,6 +253,7 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ press_Bs_to_back_to_overworld(m_info, m_stream, m_context); } virtual std::vector read_quests() override{ + m_stream.log("Reading quest panel to verify reroll."); return PokemonSV::read_quests(m_info, m_stream, m_context, m_options); } @@ -290,8 +291,10 @@ BBQRerollResult reroll_quest( Logger& logger, BBQRerollActions& actions, std::vector& quests, - size_t slot + size_t slot, + std::optional>& observed_panel ){ + observed_panel.reset(); if (!actions.open_panel()){ return BBQRerollResult::PanelNotOpened; } @@ -328,13 +331,19 @@ BBQRerollResult reroll_quest( 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]; + //Three slots can mean a missed fourth quest. Reuse only a complete + //read whose other slots agree with the panel used for this reroll. + if (after.size() == 4 && after == quests + && std::find(after.begin(), after.end(), BBQuests::UnableToDetect) == after.end()){ + observed_panel = std::move(after); + } return BBQRerollResult::Rerolled; } } return BBQRerollResult::NotReplaced; } -void reroll_quest_slot( +std::optional> reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, @@ -342,9 +351,10 @@ void reroll_quest_slot( size_t slot ){ ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS); - switch (reroll_quest(stream.logger(), actions, quest_list, slot)){ + std::optional> observed_panel; + switch (reroll_quest(stream.logger(), actions, quest_list, slot, observed_panel)){ case BBQRerollResult::Rerolled: - return; + return observed_panel; case BBQRerollResult::PanelNotOpened: OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, @@ -365,6 +375,7 @@ void reroll_quest_slot( stream ); } + throw InternalProgramError(&stream.logger(), PA_CURRENT_FUNCTION, "Invalid enum."); } BBQuestPlan plan_quests( @@ -401,7 +412,7 @@ BBQuestPlan plan_quests( return plan; } -std::vector process_quest_list( +BBQuestBatch process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, @@ -412,8 +423,9 @@ std::vector process_quest_list( //Each reroll updates the panel the next one checks against. std::vector panel = quest_list; + std::optional> observed_panel; for (size_t slot : plan.reroll_slots){ - reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot); + observed_panel = reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot); } if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); @@ -433,7 +445,12 @@ std::vector process_quest_list( ); } */ - return plan.to_do; + //A mutation invalidates queued work, even when other quests remain runnable. + //A complete replacement observation can seed the next selection instead. + if (!plan.reroll_slots.empty() || plan.reroll_all){ + return {{}, std::move(observed_panel)}; + } + return {std::move(plan.to_do), std::nullopt}; } bool process_and_do_quest( @@ -546,6 +563,7 @@ bool process_and_do_quest( } //Validate quest was completed by checking list + console.log("Reading quest panel after attempt."); std::vector quest_list = read_quests(env.program_info(), console, context, BBQ_OPTIONS); if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){ console.log("Current quest exists on list. Quest did not complete."); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 4b45e65e49..f86490204d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -75,6 +75,13 @@ BBQuestPlan plan_quests( uint8_t eggs_hatched ); +//Work selected from a panel, or a complete observed panel to replan after a reroll. +//The observation is consumed once, before any further game actions. +struct BBQuestBatch{ + std::vector to_do; + std::optional> observed_panel; +}; + //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{ @@ -113,17 +120,19 @@ enum class BBQRerollResult{ //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. +//replaced. On Rerolled, `quests[slot]` is updated. observed_panel is populated +//only for four known slots with no other changes, so scheduling can reuse it. BBQRerollResult reroll_quest( Logger& logger, BBQRerollActions& actions, std::vector& quests, - size_t slot + size_t slot, + std::optional>& observed_panel ); //Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program -//if the reroll fails. -void reroll_quest_slot( +//if the reroll fails. Returns a complete observed panel when reusable. +std::optional> reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, @@ -133,7 +142,7 @@ void reroll_quest_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( +BBQuestBatch process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS,