From ea359ba3d021daf9d2e472c06f6db929c03ca35a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 21:08:30 -0700 Subject: [PATCH 01/50] Run the BBQ farmer's rounds against an injectable BBQGameActions BBQSoloFarmer::program() runs its quest rounds inline against the console, so the round logic (which quests count, when to stop, when to save) can only be exercised by running the farmer on a Switch. Move the round loop into run_bbq_rounds(), which takes the game actions it depends on as a BBQGameActions interface: read the quests, process them, do a quest, report a completed quest, and end the round (saving if due). BBQSoloFarmer implements these with the calls they replace, in the same order; the date fix, save/reset, stats and notification move together into end_round(). Startup, flying to the plaza and end-of-program handling stay in program(). The commented-out single-quest debug block declares its own egg counter now that the loop locals have moved. process_quest_list() only reads its quest list and egg count, so take them as const& and by value. Add PokemonSV::BBQFarmerRounds, a table of scenarios run against a simulated singleplayer quest panel: 3 blue quests, completed quests are replaced, impossible quests are skipped, and a panel with nothing doable is rerolled. Each scenario checks the number of completed quests and the exact action sequence: finishing the round that reaches the goal, a quest already gone, a failed quest, a panel with nothing doable, saving at exact multiples, and stopping when out of eggs. No behavior change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 222 ++++++++++++++++++ .../Farming/PokemonSV_BBQSoloFarmer.cpp | 166 ++++++++----- .../Farming/PokemonSV_BBQSoloFarmer.h | 42 ++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 4 +- .../Farming/PokemonSV_BlueberryQuests.h | 4 +- 5 files changed, 375 insertions(+), 63 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index d966bfe133..f894861d68 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Inference/PokemonSV_WhiteButtonDetector.h" #include "Inference/Boxes/PokemonSV_BoxDetection.h" @@ -25,15 +26,236 @@ #include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h" #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" +#include "Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "Programs/Farming/PokemonSV_BBQSoloFarmer.h" #include "PokemonSV_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ +namespace{ +class NullLogger : public Logger{ +public: + virtual void log(const std::string&, Color) override{} +}; + +// A singleplayer quest panel that behaves like the game's: 3 blue quests, +// and a completed quest is replaced by a new one. process_quest_list() +// follows the two rules of the real one that the round loop depends on: +// quests in not_possible_quests are skipped, and if nothing is doable all +// quests are rerolled and nothing is returned. Red quests and the +// exclusions table are not modeled. Records the game actions taken. +class SimulatedBBQGameActions : public BBQGameActions{ +public: + std::vector panel; + // Each attempt returns the next outcome; once exhausted, attempts succeed. + std::vector outcomes; + // Completing the first quest also completes the second, e.g. walking + // 500 meters while navigating to a photo target. + std::vector> also_completes; + + std::vector events; + + virtual std::vector read_quests() override{ + events.push_back("read"); + if (++m_reads > 1000){ + throw std::runtime_error("Too many quest panel reads."); + } + return panel; + } + virtual std::vector process_quest_list( + const std::vector& quests, uint8_t + ) override{ + events.push_back("process"); + std::vector doable; + for (BBQuests quest : quests){ + if (!not_possible_quests.contains(quest)){ + doable.push_back(quest); + } + } + if (doable.empty()){ + for (BBQuests& slot : panel){ + slot = next_new_quest(); + } + } + return doable; + } + virtual bool do_quest(BBQuests quest, uint8_t&) override{ + events.push_back("do"); + const bool success = m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true; + if (success){ + complete(quest); + for (const auto& [done, extra] : also_completes){ + if (done == quest){ + complete(extra); + } + } + } + return success; + } + virtual void quest_completed() override{ + events.push_back("completed"); + } + virtual void end_round(bool save) override{ + events.push_back(save ? "end(save)" : "end"); + } + +private: + void complete(BBQuests quest){ + auto iter = std::find(panel.begin(), panel.end(), quest); + if (iter != panel.end()){ + *iter = next_new_quest(); + } + } + + // New quests come from a fixed rotation of doable blue quests, skipping + // any that are already on the panel. + BBQuests next_new_quest(){ + static const BBQuests ROTATION[] = { + BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, + BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, + BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, + }; + while (true){ + BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)]; + if (std::find(panel.begin(), panel.end(), quest) == panel.end()){ + return quest; + } + } + } + + size_t m_reads = 0; + size_t m_next_outcome = 0; + size_t m_next_new_quest = 0; +}; + +std::string join_events(const std::vector& events){ + std::string ret; + for (const std::string& event : events){ + if (!ret.empty()) ret += ' '; + ret += event; + } + return ret; +} + +// One simulated farmer run: option values, the panel it starts with and how +// quests turn out, and the game actions it must take. +struct BBQRoundsScenario{ + std::string name; + uint64_t num_quests; + uint64_t save_num_quests = 0; + bool stop_when_out_of_eggs = false; + std::vector panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up}; + std::vector outcomes; + std::vector> also_completes; + uint64_t expected_completed = 0; + std::string expected; +}; + +class Test_BBQFarmerRounds : public UnitTest{ +public: + Test_BBQFarmerRounds(BBQRoundsScenario scenario) + : UnitTest("PokemonSV::BBQFarmerRounds - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + BBQOption options; + options.NUM_QUESTS.set(m_scenario.num_quests); + options.SAVE_NUM_QUESTS.set(m_scenario.save_num_quests); + if (m_scenario.stop_when_out_of_eggs){ + options.NUM_EGGS.set(0); + options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop); + } + + SimulatedBBQGameActions game; + game.panel = m_scenario.panel; + game.outcomes = m_scenario.outcomes; + game.also_completes = m_scenario.also_completes; + + NullLogger logger; + run_bbq_rounds(logger, options, game); + const uint64_t completed = (uint64_t)std::count(game.events.begin(), game.events.end(), "completed"); + if (completed != m_scenario.expected_completed){ + return "Expected " + std::to_string(m_scenario.expected_completed) + + " completed quests, counted " + std::to_string(completed); + } + const std::string actual = join_events(game.events); + if (actual != m_scenario.expected){ + return "Expected: " + m_scenario.expected + "\nActual: " + actual; + } + return true; + } + +private: + const BBQRoundsScenario m_scenario; +}; + +void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ + const std::string round_of_3 = "read process read do completed read do completed read do completed"; + const BBQRoundsScenario scenarios[] = { + { + // 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 + // complete. + .name = "finishes the round that reaches the quest goal", + .num_quests = 4, + .expected_completed = 6, + .expected = round_of_3 + " end " + round_of_3 + " end", + }, + { + // Doing photo_swim also completes travel_500, so travel_500 is + // gone when the loop reads the panel before attempting it. + .name = "quest already gone counts as completed", + .num_quests = 3, + .panel = {BBQuests::photo_swim, BBQuests::travel_500, BBQuests::make_tm}, + .also_completes = {{BBQuests::photo_swim, BBQuests::travel_500}}, + .expected_completed = 3, + .expected = "read process read do completed read completed read do completed end", + }, + { + // The first attempt fails: 3 attempts, 2 completions. + .name = "failed quest does not count", + .num_quests = 2, + .outcomes = {false}, + .expected_completed = 2, + .expected = "read process read do read do completed read do completed end", + }, + { + // Nothing on the panel is doable, so process_quest_list() rerolls + // all of it and returns nothing; the loop reads the panel again. + .name = "panel with nothing doable is rerolled", + .num_quests = 3, + .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, + .expected_completed = 3, + .expected = "read process " + round_of_3 + " end", + }, + { + .name = "saves at exact multiples", + .num_quests = 6, + .save_num_quests = 3, + .expected_completed = 6, + .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", + }, + { + .name = "stops when out of eggs", + .num_quests = 5, + .stop_when_out_of_eggs = true, + .expected_completed = 0, + .expected = "", + }, + }; + for (const BBQRoundsScenario& scenario : scenarios){ + database.add(scenario); + } +} + +} void add_tests(UnitTestDatabase& database){ + add_tests_BBQFarmerRounds(database); add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 1c874e32a1..c6deaccca9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -74,48 +74,22 @@ BBQSoloFarmer::BBQSoloFarmer() PA_ADD_OPTION(NOTIFICATIONS); } -void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - StartProgramChecks::check_performance_class_wired_or_wireless(context); - assert_16_9_720p_min(env.logger(), env.console); - - BBQSoloFarmer_Descriptor::Stats& stats = env.current_stats(); - - //Make sure console type is set - if (env.console.state().console_type() == ConsoleType::Unknown){ - throw UserSetupError(env.console, "Console Type (Switch 1 or 2) must be specified."); - } - - // Connect the controller. - require_player(env.console, context, BUTTON_LCLICK); - - //Fly to plaza - open_map_from_overworld(env.program_info(), env.console, context); - fly_to_overworld_from_map(env.program_info(), env.console, context); - +void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs uint64_t num_completed_quests = 0; - //Test a specific quest - /* - BBQuests test_quest = BBQuests::catch_water; - bool questTest = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, test_quest, eggs_hatched); - if (questTest){ - env.log("Finished quest."); - } - */ - - while (num_completed_quests < BBQ_OPTIONS.NUM_QUESTS){ - if (BBQ_OPTIONS.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ - env.log("Stop when out of eggs selected. Stopping program."); + while (num_completed_quests < options.NUM_QUESTS){ + if (options.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= options.NUM_EGGS){ + logger.log("Stop when out of eggs selected. Stopping program."); break; } //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - quest_list = read_quests(env.program_info(), env.console, context, BBQ_OPTIONS); - quests_to_do = process_quest_list(env.program_info(), env.console, context, BBQ_OPTIONS, quest_list, eggs_hatched); + quest_list = game.read_quests(); + quests_to_do = game.process_quest_list(quest_list, eggs_hatched); //Clear out the regular quest list. quest_list.clear(); @@ -123,23 +97,21 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo 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_quests(env.program_info(), env.console, context, BBQ_OPTIONS); + quest_list = game.read_quests(); if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){ - env.log("Current quest exists on list. Doing quest."); - bool questSuccess = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, current_quest, eggs_hatched); + logger.log("Current quest exists on list. Doing quest."); + bool questSuccess = game.do_quest(current_quest, eggs_hatched); if (questSuccess){ - env.log("Quest completed successfully."); - stats.questsCompleted++; - env.update_stats(); + logger.log("Quest completed successfully."); + game.quest_completed(); num_completed_quests++; }else{ - env.log("Quest did not complete successfully."); + logger.log("Quest did not complete successfully."); } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest - env.log("Current quest does not exist on list. Quest completed at some point."); - stats.questsCompleted++; - env.update_stats(); + logger.log("Current quest does not exist on list. Quest completed at some point."); + game.quest_completed(); num_completed_quests++; } quest_list.clear(); @@ -147,27 +119,103 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo //Clear out the todo list quests_to_do.clear(); - //Fix the time to prevent running out of years - pbf_wait(context, 2000ms); - context.wait_for_all_requests(); - go_home(env.console, context); - home_to_date_time(env.console, context, false); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); - resume_game_from_home(env.console, context); + uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; + game.end_round(temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0); + } +} + - uint64_t temp_save_num_option = BBQ_OPTIONS.SAVE_NUM_QUESTS; - if (temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0){ - env.log("Saving and resetting."); - save_game_from_overworld(env.program_info(), env.console, context); - reset_game(env.program_info(), env.console, context); - stats.saves++; +namespace{ + +class BBQSoloFarmerGameActions : public BBQGameActions{ +public: + BBQSoloFarmerGameActions( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BBQOption& options, BBQSoloFarmer_Descriptor::Stats& stats, + EventNotificationOption& status_update + ) + : m_env(env), m_context(context) + , m_options(options), m_stats(stats) + , m_status_update(status_update) + {} + + 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( + 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); + } + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ + return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); + } + virtual void quest_completed() override{ + m_stats.questsCompleted++; + m_env.update_stats(); + } + virtual void end_round(bool save) override{ + //Fix the time to prevent running out of years + pbf_wait(m_context, 2000ms); + m_context.wait_for_all_requests(); + go_home(m_env.console, m_context); + home_to_date_time(m_env.console, m_context, false); + pbf_press_button(m_context, BUTTON_A, 160ms, 840ms); + pbf_press_button(m_context, BUTTON_A, 160ms, 840ms); + pbf_press_button(m_context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); + resume_game_from_home(m_env.console, m_context); + + if (save){ + m_env.log("Saving and resetting."); + save_game_from_overworld(m_env.program_info(), m_env.console, m_context); + reset_game(m_env.program_info(), m_env.console, m_context); + m_stats.saves++; } - env.update_stats(); - send_program_status_notification(env, NOTIFICATION_STATUS_UPDATE); + m_env.update_stats(); + send_program_status_notification(m_env, m_status_update); + } + +private: + SingleSwitchProgramEnvironment& m_env; + ProControllerContext& m_context; + BBQOption& m_options; + BBQSoloFarmer_Descriptor::Stats& m_stats; + EventNotificationOption& m_status_update; +}; + +} + +void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + StartProgramChecks::check_performance_class_wired_or_wireless(context); + assert_16_9_720p_min(env.logger(), env.console); + + BBQSoloFarmer_Descriptor::Stats& stats = env.current_stats(); + + //Make sure console type is set + if (env.console.state().console_type() == ConsoleType::Unknown){ + throw UserSetupError(env.console, "Console Type (Switch 1 or 2) must be specified."); + } + + // Connect the controller. + require_player(env.console, context, BUTTON_LCLICK); + + //Fly to plaza + open_map_from_overworld(env.program_info(), env.console, context); + fly_to_overworld_from_map(env.program_info(), env.console, context); + + //Test a specific quest + /* + uint8_t eggs_hatched = 0; + BBQuests test_quest = BBQuests::catch_water; + bool questTest = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, test_quest, eggs_hatched); + if (questTest){ + env.log("Finished quest."); } + */ + + BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE); + run_bbq_rounds(env.logger(), BBQ_OPTIONS, game); env.update_stats(); if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 9917606334..1ac8535caf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -7,6 +7,9 @@ #ifndef PokemonAutomation_PokemonSV_BBQSoloFarmer_H #define PokemonAutomation_PokemonSV_BBQSoloFarmer_H +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" @@ -16,6 +19,45 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ + +// The game actions that run_bbq_rounds() depends on. They are injected so +// that the round loop (which quests to attempt, which ones count, when to +// stop, when to save) runs in tests without a Switch: BBQSoloFarmer performs +// them on the console, and tests replace them with scripted results. +class BBQGameActions{ +public: + virtual ~BBQGameActions() = default; + + // Open the quest panel and read the quests on it. + virtual std::vector read_quests() = 0; + + // 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( + const std::vector& quests, uint8_t eggs_hatched + ) = 0; + + // Attempt one quest that is on the panel, after process_quest_list() + // chose it. Returns true if it is no longer on the panel afterwards. + // Increments `eggs_hatched` when the quest hatches eggs. + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; + + // Progress reporting only: a quest was counted as completed. Lets stats + // update after every quest instead of once per round. + virtual void quest_completed() = 0; + + // A round has ended. The loop decides whether a save is due, because + // that depends on how many quests it has counted; how to fix the date, + // save and reset, and report the round is up to the game side. + virtual void end_round(bool save) = 0; +}; + +// Run rounds of quests until NUM_QUESTS have been completed, or until +// eggs run out when OUT_OF_EGGS is Stop. +void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); + + class BBQSoloFarmer_Descriptor : public SingleSwitchProgramDescriptor{ public: BBQSoloFarmer_Descriptor(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 43740d2c1c..fda3441a39 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -225,8 +225,8 @@ std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, - std::vector& quest_list, - uint8_t& eggs_hatched + const std::vector& quest_list, + uint8_t eggs_hatched ){ std::vector quests_to_do; bool rerolled = false; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 957a3c50e9..8ab92394f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -71,8 +71,8 @@ std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, - std::vector& quest_list, - uint8_t& eggs_hatched + const std::vector& quest_list, + uint8_t eggs_hatched ); //Take the current quest and calls the function to do it, then checks the quest was successful. Returns true if so. From 548ac951c4aaf65d30f189bd1cfbcae233f6f09b Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 21:42:30 -0700 Subject: [PATCH 02/50] Fix BBQ save/reset milestones across stalled and batched rounds The farmer saved and reset when the completion count was an exact multiple of SAVE_NUM_QUESTS. A round that completed nothing while the count sat at 0 or 50 saved again, and a round going from 49 to 52 skipped the 50 milestone entirely. Compare the count before and after each round and ask end_round() to save once when it crosses a multiple of the interval. Stalled rounds and an interval of 0 never save. Fix the option label to say "completing", not "attempting". Add two PokemonSV::BBQFarmerRounds scenarios that fail with the old rule: a round that skips past a milestone, and rounds with no completions. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 2 +- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 20 ++++++++++++++++++- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 15 +++++++++++++- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 4067d75fa3..4758cdd47b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -151,7 +151,7 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) 1000 ) , SAVE_NUM_QUESTS( - "Save and reset the game after attempting this many quests:
This preserves progress and prevents potential game lags from long runs.
0 disables this option.", + "Save and reset the game after completing this many quests:
This preserves progress and prevents potential game lags from long runs.
0 disables this option.", LockMode::UNLOCK_WHILE_RUNNING, 50 ) , INVERTED_FLIGHT( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index f894861d68..16b905a61f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -233,12 +233,30 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = "read process " + round_of_3 + " end", }, { - .name = "saves at exact multiples", + .name = "saves at each milestone", .num_quests = 6, .save_num_quests = 3, .expected_completed = 6, .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", }, + { + // 0 -> 3 crosses the milestone at 2 without landing on it. + .name = "saves when a round skips past a milestone", + .num_quests = 6, + .save_num_quests = 2, + .expected_completed = 6, + .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", + }, + { + // Round 1 completes nothing. It must not save, even though the + // count (0) is a multiple of the interval. + .name = "round without completions does not save", + .num_quests = 3, + .save_num_quests = 2, + .outcomes = {false, false, false}, + .expected_completed = 3, + .expected = "read process read do read do read do end " + round_of_3 + " end(save)", + }, { .name = "stops when out of eggs", .num_quests = 5, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index c6deaccca9..6686ada964 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -74,6 +74,17 @@ BBQSoloFarmer::BBQSoloFarmer() PA_ADD_OPTION(NOTIFICATIONS); } +namespace{ + +// Whether a round that took the completed count from previous_completed +// to completed crossed a nonzero multiple of the save interval. +bool bbq_save_due(uint64_t previous_completed, uint64_t completed, uint64_t interval){ + return interval != 0 && completed > previous_completed + && completed / interval > previous_completed / interval; +} + +} + void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests @@ -95,6 +106,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga quest_list.clear(); } + 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 = game.read_quests(); @@ -120,7 +132,8 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga quests_to_do.clear(); uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; - game.end_round(temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0); + // A round can cross a milestone without ending at an exact multiple. + game.end_round(bbq_save_due(completed_before_round, num_completed_quests, temp_save_num_option)); } } From 102957f131d20d503526da3ceaa108b8670549f8 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 22:39:54 -0700 Subject: [PATCH 03/50] Separate the BBQ quest plan from the controller actions that carry it out process_quest_list() decided what to do with each quest and pressed the buttons for it in the same loop, so the decisions could only be checked on a Switch. plan_quests() now makes the decisions from the options alone: which quests to do, which panel slots to reroll, and whether to reroll all. process_quest_list() carries out the plan, and opening the panel is a shared helper instead of three copies. All rerolls now move the cursor to the quest's slot. The out-of-eggs reroll used to reroll the top slot; the egg quest is a red quest and red quests are listed at the top, so this is the same in practice. OOEggs::Stop reaching the planner is now an InternalProgramError, since the farmer stops before that can happen. Unit tests cover the plan for exclusions, duplicate rows, quests that are not possible, and eggs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 150 +++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 255 ++++++++---------- .../Farming/PokemonSV_BlueberryQuests.h | 17 ++ 3 files changed, 279 insertions(+), 143 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index d966bfe133..8c9563ff9a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -25,15 +25,165 @@ #include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h" #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" +#include "Programs/Farming/PokemonSV_BlueberryQuests.h" #include "PokemonSV_Tests.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ +namespace{ + +class NullLogger : public Logger{ +public: + virtual void log(const std::string&, Color) override{} +}; + +// 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; +}; + +} + +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(UnitTestDatabase& database){ + add_tests_BBQuestPlan(database); add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 43740d2c1c..dab292b71a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -221,6 +221,97 @@ std::vector read_quests( return quest_list; } +BBQuestPlan plan_quests( + Logger& logger, + const BBQOption& BBQ_OPTIONS, + const std::vector& quest_list, + uint8_t eggs_hatched +){ + BBQuestPlan plan; + std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); + + 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)){ + 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."); + 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()."); + } + continue; + } + bool quest_in_table = false; + for (const std::unique_ptr& row : exclusions_table){ + if (n != 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(n); + 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; + } + } + if (!quest_in_table){ + logger.log("Quest not in inclusion/exclusions table. Adding to list."); + plan.to_do.push_back(n); + } + } + + //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 + plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + return plan; +} + +namespace{ + +//Open the quest panel from the overworld, with the cursor on the top quest. +void 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(); +} + +} + std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -228,155 +319,33 @@ std::vector process_quest_list( std::vector& quest_list, uint8_t& eggs_hatched ){ - std::vector quests_to_do; - bool rerolled = false; - 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){ - 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(); + BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); - rerolled = true; - press_Bs_to_back_to_overworld(info, stream, context); + for (size_t slot : plan.reroll_slots){ + open_quest_panel(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); - } - } + //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(); } - questpos++; + + //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); } - - //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){ + + if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); //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(); + open_quest_panel(stream, context); for (string::size_type i = 0; i < quest_list.size(); i++){ pbf_press_button(context, BUTTON_A, 160ms, 400ms); @@ -391,7 +360,7 @@ std::vector process_quest_list( press_Bs_to_back_to_overworld(info, stream, context); } /* - if (!rerolled && quests_to_do.size() == 0){ + if (plan.reroll_all){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, "No possible quests! Check language selection.", @@ -399,7 +368,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 957a3c50e9..92270cf374 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -65,6 +65,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( From 053e59b375ab3bdcf06c5ed167820e5d844ab070 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 10:51:21 -0700 Subject: [PATCH 04/50] Do not retry a failed BBQ quest every round while it stays on the panel process_and_do_quest() gives up on a quest after NUM_RETRIES retries, but the quest stays on the panel, so the next round attempts it again with a fresh set of retries. A quest that cannot be done, such as a sneak-up whose target does not spawn, is retried forever. run_bbq_rounds() now remembers quests that failed all their attempts and leaves them out of later rounds. A quest is forgotten once a read of 4 known quests shows it gone, so it gets its attempts again if it comes back. If only failed quests are doable, the farmer stops instead of spending BP to reroll them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 78 ++++++++++++++++--- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 45 ++++++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 3 +- 3 files changed, 114 insertions(+), 12 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index f894861d68..f98872d0ba 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Inference/PokemonSV_WhiteButtonDetector.h" #include "Inference/Boxes/PokemonSV_BoxDetection.h" @@ -55,6 +56,15 @@ class SimulatedBBQGameActions : public BBQGameActions{ // Completing the first quest also completes the second, e.g. walking // 500 meters while navigating to a photo target. std::vector> also_completes; + // Attempts at these always fail, e.g. a target that never spawns. + std::set never_completes; + // New quests are drawn from this list in turn, skipping any that are + // already on the panel. + std::vector new_quests = { + BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, + BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, + BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, + }; std::vector events; @@ -84,7 +94,8 @@ class SimulatedBBQGameActions : public BBQGameActions{ } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); - const bool success = m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true; + const bool success = !never_completes.contains(quest) + && (m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true); if (success){ complete(quest); for (const auto& [done, extra] : also_completes){ @@ -110,16 +121,9 @@ class SimulatedBBQGameActions : public BBQGameActions{ } } - // New quests come from a fixed rotation of doable blue quests, skipping - // any that are already on the panel. BBQuests next_new_quest(){ - static const BBQuests ROTATION[] = { - BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, - BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, - BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, - }; while (true){ - BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)]; + BBQuests quest = new_quests[m_next_new_quest++ % new_quests.size()]; if (std::find(panel.begin(), panel.end(), quest) == panel.end()){ return quest; } @@ -150,6 +154,8 @@ struct BBQRoundsScenario{ std::vector panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up}; std::vector outcomes; std::vector> also_completes; + std::set never_completes; + std::vector new_quests; uint64_t expected_completed = 0; std::string expected; }; @@ -174,6 +180,10 @@ class Test_BBQFarmerRounds : public UnitTest{ game.panel = m_scenario.panel; game.outcomes = m_scenario.outcomes; game.also_completes = m_scenario.also_completes; + game.never_completes = m_scenario.never_completes; + if (!m_scenario.new_quests.empty()){ + game.new_quests = m_scenario.new_quests; + } NullLogger logger; run_bbq_rounds(logger, options, game); @@ -246,6 +256,56 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected_completed = 0, .expected = "", }, + { + // sneak-up fails in round 1 and is still on the panel in round 2, + // where only the two new quests are attempted. + .name = "failed quest is not attempted again", + .num_quests = 4, + .never_completes = {BBQuests::sneak_up}, + .expected_completed = 4, + .expected = + "read process read do completed read do completed read do end " + "read process read do completed read do completed end", + }, + { + .name = "stops when only failed quests are doable", + .num_quests = 3, + .panel = {BBQuests::sneak_up, BBQuests::pickup_10, BBQuests::catch_flying}, + .never_completes = {BBQuests::sneak_up}, + .expected_completed = 0, + .expected = "read process read do end read process", + }, + { + // sneak-up fails, then leaves the panel along with make-tm. Round 2 + // reads a full panel without it, and its replacement for + // photo-canyon brings it back. Round 3 attempts it again. + .name = "failed quest that leaves a full panel is attempted again", + .num_quests = 5, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10, BBQuests::catch_flying}, + .outcomes = {false}, + .also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}}, + .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, + .expected_completed = 5, + .expected = + "read process read do read do completed end " + "read process read do completed read do completed end " + "read process read do completed read do completed end", + }, + { + // As above with 3 slots. A 4th quest could have been missed by + // OCR, so the read does not prove sneak-up left: it stays failed. + .name = "failed quest missing from a 3-slot panel stays failed", + .num_quests = 4, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10}, + .outcomes = {false}, + .also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}}, + .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, + .expected_completed = 4, + .expected = + "read process read do read do completed end " + "read process read do completed read do completed end " + "read process read do completed end", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index c6deaccca9..ee63a9b792 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/ProgramStats/StatsTracking.h" @@ -74,12 +75,40 @@ BBQSoloFarmer::BBQSoloFarmer() PA_ADD_OPTION(NOTIFICATIONS); } +namespace{ + +// Forget the failed quests that are gone from the panel, so a quest that +// comes back later gets its attempts again. The first 3 slots are always +// read, as UnableToDetect when OCR fails, but a 4th slot is only there when +// OCR reads it. So only a read of 4 known quests proves a quest is gone; a +// missed read must not give a failing quest new attempts. +void forget_quests_gone_from_panel(std::set& failed_quests, const std::vector& panel){ + if (panel.size() < 4 || std::find(panel.begin(), panel.end(), BBQuests::UnableToDetect) != panel.end()){ + return; + } + std::erase_if(failed_quests, [&](BBQuests quest){ + return std::find(panel.begin(), panel.end(), quest) == panel.end(); + }); +} + +} + void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs uint64_t num_completed_quests = 0; + // Quests that failed every attempt do_quest() made. A failed quest stays + // 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; + auto read_panel = [&](){ + std::vector panel = game.read_quests(); + forget_quests_gone_from_panel(failed_quests, panel); + return panel; + }; + while (num_completed_quests < options.NUM_QUESTS){ if (options.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= options.NUM_EGGS){ logger.log("Stop when out of eggs selected. Stopping program."); @@ -88,16 +117,27 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - quest_list = game.read_quests(); + quest_list = read_panel(); quests_to_do = game.process_quest_list(quest_list, eggs_hatched); + // Rerolling costs BP and the failed quests may be doable another + // time, so stop instead of spending BP to replace them. + const bool found_doable = !quests_to_do.empty(); + std::erase_if(quests_to_do, [&](BBQuests quest){ + return failed_quests.contains(quest); + }); + if (found_doable && quests_to_do.empty()){ + logger.log("Every doable quest on the panel failed all its attempts. Stopping program.", COLOR_RED); + return; + } + //Clear out the regular quest list. quest_list.clear(); } 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 = game.read_quests(); + 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); @@ -107,6 +147,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; }else{ logger.log("Quest did not complete successfully."); + failed_quests.insert(current_quest); } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 1ac8535caf..1afa74e6dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -54,7 +54,8 @@ class BBQGameActions{ }; // Run rounds of quests until NUM_QUESTS have been completed, or until -// eggs run out when OUT_OF_EGGS is Stop. +// eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the +// panel is one that already failed all its attempts. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); From fe5055a40c7de35885fb4d248123ee7347111ea1 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 10:59:00 -0700 Subject: [PATCH 05/50] Break the BBQ quest planner and its controller steps into helpers Move the out-of-eggs choice, the exclusions table rows, a single-slot reroll and the reroll of every slot into file-local helpers, so plan_quests() and process_quest_list() read as one level of steps. Explain why the language-selection error is commented out. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BlueberryQuests.cpp | 217 ++++++++++-------- 1 file changed, 125 insertions(+), 92 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index dab292b71a..f960282969 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -221,6 +221,122 @@ std::vector read_quests( return 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; +} + +//Open the quest panel from the overworld, with the cursor on the top quest. +void 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(); +} + +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, @@ -240,43 +356,10 @@ BBQuestPlan plan_quests( //Check eggs remaining. if (n == BBQuests::hatch_egg && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ logger.log("Out of eggs! Quest not possible."); - 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()."); - } + plan_out_of_eggs(logger, BBQ_OPTIONS, slot, plan); continue; } - bool quest_in_table = false; - for (const std::unique_ptr& row : exclusions_table){ - if (n != 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(n); - 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; - } - } - if (!quest_in_table){ + 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); } @@ -288,30 +371,6 @@ BBQuestPlan plan_quests( return plan; } -namespace{ - -//Open the quest panel from the overworld, with the cursor on the top quest. -void 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(); -} - -} - std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -322,43 +381,17 @@ std::vector process_quest_list( BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); for (size_t slot : plan.reroll_slots){ - 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); + reroll_slot(info, stream, context, slot); } - if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); - - //Open quest panel and reroll all quests. - //This does not handle out of BP. - open_quest_panel(stream, context); - - 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); + 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 (plan.reroll_all){ OperationFailedExceptionWithScreenshot::fire( From 5b86beecb19588dadffddbdaffc40035b9937dde Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:05:54 -0700 Subject: [PATCH 06/50] Check which quest a BBQ reroll pays for, and that it was replaced reroll_slot() opened the quest panel, pressed DOWN once per slot from where it assumed the cursor was, and pressed A twice. The panel does not always open with the cursor on the top quest: on a Switch it opened where the cursor was left, so the reroll paid for a different quest than planned. Nothing checked the menu opened or the quest changed, so running out of BP went unnoticed and the same quest was rerolled every round. reroll_quest() now does a reroll through injectable BBQRerollActions: open the reroll menu wherever the cursor is, see which quest the menu opened next to from its arrow, move to the planned quest, and only then confirm. Afterwards it reads the panel (up to 3 times, as OCR can miss a read) to check the quest was replaced. Panel or menu not found stops before spending BP; a quest not replaced stops without an error report, as it usually means out of BP. The arrow boxes for the top three slots were checked on a Switch; the fourth uses the same spacing. Reroll-all is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 216 ++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 158 +++++++++++-- .../Farming/PokemonSV_BlueberryQuests.h | 47 ++++ 3 files changed, 403 insertions(+), 18 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 8c9563ff9a..610a5bb935 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -107,6 +107,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; +}; + } void add_tests_BBQuestPlan(UnitTestDatabase& database){ @@ -180,10 +302,104 @@ void add_tests_BBQuestPlan(UnitTestDatabase& database){ } } +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(UnitTestDatabase& database){ add_tests_BBQuestPlan(database); + add_tests_BBQReroll(database); add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index f960282969..28e5481912 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/PokemonSV_MainMenuDetector.h" @@ -271,8 +272,8 @@ bool plan_from_exclusions( return quest_in_table; } -//Open the quest panel from the overworld, with the cursor on the top quest. -void open_quest_panel(VideoStream& stream, ProControllerContext& context){ +//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, @@ -289,29 +290,102 @@ void open_quest_panel(VideoStream& stream, ProControllerContext& context){ stream.log("Found quest panel."); } context.wait_for_all_requests(); + return result == 0; } +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); + } + +private: + const ProgramInfo& m_info; + VideoStream& m_stream; + ProControllerContext& m_context; + const BBQOption& m_options; +}; + void reroll_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS, + std::vector& quest_list, 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(); + 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 + ); } - - //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. @@ -337,6 +411,54 @@ 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; +} + BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, @@ -381,7 +503,7 @@ std::vector process_quest_list( BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); for (size_t slot : plan.reroll_slots){ - reroll_slot(info, stream, context, slot); + reroll_slot(info, stream, context, BBQ_OPTIONS, quest_list, 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 92270cf374..25a847c154 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" @@ -82,6 +83,52 @@ 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 +); + //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( From f7e3ba6f0743b1e72286c3a8ee214d0f6494fd17 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:07:34 -0700 Subject: [PATCH 07/50] Let the BBQ exclusions table reroll quests the farmer cannot do plan_quests() dropped quests the farmer cannot do (pickup, flying photo and catch) before looking at the exclusions table, so a reroll row for one of them did nothing: the quest stayed on the panel and blocked the slot. Pickup could not be picked in the table at all. The table is now applied first. Reroll and skip rows work for quests the farmer cannot do; a run row for one is ignored, since there is no route for it. Pickup is listed in the table. With nothing to do and nothing rerolled, the first quest the farmer cannot do that has no table row is rerolled instead of every quest, which costs one reroll instead of three or four. Every quest is still rerolled when there is no such quest. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 7 ++-- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 36 ++++++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 39 +++++++++++++++---- .../Farming/PokemonSV_BlueberryQuests.h | 8 ++-- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 4067d75fa3..4b50f2fcdf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -17,7 +17,7 @@ const EnumDropdownDatabase& BBQuests_database(){ static EnumDropdownDatabase database{ {BBQuests::auto_10, "auto-10", "Defeat 10 wild Pokemon using Auto Battle!"}, {BBQuests::make_tm, "make-tm", "Make yourself a TM!"}, - //{BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, + {BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, {BBQuests::sneak_up, "sneak-up", "Successfully sneak up on 1 Pokemon and surprise them with a battle!"}, {BBQuests::photo_fly, "photo-fly", "Take a photo of a wild Pokemon in flight!"}, {BBQuests::photo_swim, "photo-swim", "Take a photo of a wild Pokemon that is swimming!"}, @@ -110,8 +110,9 @@ BBQuestTable::BBQuestTable() "Exclude the quests in the table. If you want to skip a quest, select it below. " "(Quests can be explicitly included, but this is not necessary.) " "Do not exclude too many quests, as rerolling costs BP. " - "The program will automatically reroll all quests if none are possible, but will not handle being out of BP. " - "Does not include egg hatching or pickup quests, as eggs are handled in other options and pickup is not possible. ", + "If no quest is possible, the program rerolls one quest it cannot do, or all quests if there is none. " + "Quests the program cannot do, such as pickup, are never run, but can be rerolled or skipped here. " + "Does not include egg hatching, as eggs are handled in other options. ", //"Duplicates of the same quest", //duplicates will work in table order. might lead to weird behavior like rerolling and then marking a quest as complete. //won't break the program though. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 610a5bb935..1a4eeda810 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -245,10 +245,42 @@ void add_tests_BBQuestPlan(UnitTestDatabase& database){ .expected_to_do = {make_tm}, }, { - .name = "nothing doable rerolls all", - .quests = {pickup_10, auto_10, catch_flying}, + // One quest costs less BP to reroll than all of them. + .name = "nothing doable rerolls the first quest not possible", + .quests = {auto_10, pickup_10, catch_flying}, + .expected_reroll_slots = {1}, + }, + { + .name = "nothing doable and nothing not possible rerolls all", + .quests = {auto_10, sneak_up}, + .extra_exclusions = {{sneak_up, BBQAction::skip}}, .expected_reroll_all = true, }, + { + // An undetected quest is not known to be impossible. + .name = "an undetected quest is not rerolled alone", + .quests = {UnableToDetect, auto_10}, + .expected_reroll_all = true, + }, + { + .name = "a skipped quest not possible is not rerolled", + .quests = {pickup_10, auto_10, catch_flying}, + .extra_exclusions = {{pickup_10, BBQAction::skip}}, + .expected_reroll_slots = {2}, + }, + { + .name = "a reroll row rerolls a quest not possible", + .quests = {make_tm, sneak_up, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::reroll}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "a run row does not run a quest not possible", + .quests = {make_tm, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::run}}, + .expected_to_do = {make_tm}, + }, { // The reroll replaces a quest, so the next read has something new. .name = "a reroll row rerolls that slot", diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 28e5481912..3fa394a421 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -242,10 +242,11 @@ void plan_out_of_eggs(Logger& logger, const BBQOption& BBQ_OPTIONS, size_t slot, } //Apply each exclusions table row for the quest, in table order. Returns false if no row is for it. +//A quest the program cannot do is never run, but can still be rerolled or skipped. bool plan_from_exclusions( Logger& logger, const std::vector>& exclusions_table, - BBQuests quest, size_t slot, + BBQuests quest, size_t slot, bool possible, BBQuestPlan& plan ){ bool quest_in_table = false; @@ -257,6 +258,10 @@ bool plan_from_exclusions( quest_in_table = true; switch (row->action){ case BBQAction::run: + if (!possible){ + logger.log("Run selected, but this quest is not possible. Skipping quest."); + break; + } logger.log("Run selected. Adding quest to list."); plan.to_do.push_back(quest); break; @@ -468,11 +473,14 @@ BBQuestPlan plan_quests( BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); + //The first quest not possible that has no table row, to reroll if nothing else can be done. + std::optional not_possible_slot; + 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)){ - logger.log("Quest not possible"); + if (n == BBQuests::UnableToDetect){ + logger.log("Quest not detected."); continue; } //Check eggs remaining. @@ -481,15 +489,32 @@ BBQuestPlan plan_quests( 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); + const bool possible = !not_possible_quests.contains(n); + if (plan_from_exclusions(logger, exclusions_table, n, slot, possible, plan)){ + continue; + } + if (!possible){ + logger.log("Quest not possible."); + if (!not_possible_slot){ + not_possible_slot = slot; + } + continue; } + logger.log("Quest not in inclusion/exclusions table. Adding to list."); + plan.to_do.push_back(n); } //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 - plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + if (plan.reroll_slots.empty() && plan.to_do.empty()){ + if (not_possible_slot){ + //Rerolling one quest costs less BP than rerolling all of them. + logger.log("No possible quests! Rerolling a quest that is not possible."); + plan.reroll_slots.push_back(*not_possible_slot); + }else{ + plan.reroll_all = true; + } + } return plan; } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 25a847c154..d32d9fb5cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -73,9 +73,11 @@ struct BBQuestPlan{ 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. +//the decision can be tested. Quests not possible are left out, unless the exclusions table +//rerolls them. 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. With +//nothing to do and nothing rerolled, the first quest not possible and not in the table is +//rerolled, or else every quest. BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, From a1b685f9e255bd8de34eed566a998322900da271 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:10:16 -0700 Subject: [PATCH 08/50] Let the BBQ farmer reroll or stop on a quest that fails all its retries A quest that fails all its retries is skipped while it stays on the panel. That keeps the farmer going, but the quest blocks its slot, and if every doable quest is one that failed the farmer stops. OUT_OF_RETRIES now chooses what happens: - Skip (default): as before. - Reroll: reroll the failed quest, so its slot gets a new quest. Uses reroll_quest_slot(), which checks which quest it pays for and that it was replaced. - Stop: end the program there, through the normal end of program. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 11 +++ .../PokemonSV/Options/PokemonSV_BBQOption.h | 7 ++ .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 27 ++++++++ .../Farming/PokemonSV_BBQSoloFarmer.cpp | 22 +++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 7 +- .../Farming/PokemonSV_BlueberryQuests.cpp | 67 ++++++++++--------- .../Farming/PokemonSV_BlueberryQuests.h | 10 +++ 7 files changed, 116 insertions(+), 35 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 4067d75fa3..a15034162b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -192,6 +192,16 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) "Number of Retries:
Reattempt a quest this many times if it fails.", LockMode::UNLOCK_WHILE_RUNNING, 1, 0, 5 ) + , OUT_OF_RETRIES( + "Out of Retries:
When a quest fails all its retries, do the selected option.", + { + {OutOfRetries::Skip, "skip", "Skip Quest - Not attempted again while it stays on the panel"}, + {OutOfRetries::Reroll, "reroll", "Reroll Quest - Costs BP"}, + {OutOfRetries::Stop, "stop", "Stop Program"}, + }, + LockMode::UNLOCK_WHILE_RUNNING, + OutOfRetries::Skip + ) , FIX_TIME_WHEN_DONE( "Fix Time when Done:
Fix the time after the program finishes.", LockMode::UNLOCK_WHILE_RUNNING, false @@ -212,6 +222,7 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) PA_ADD_OPTION(BATTLE_AI); PA_ADD_OPTION(CATCH_ON_WIN); PA_ADD_OPTION(NUM_RETRIES); + PA_ADD_OPTION(OUT_OF_RETRIES); PA_ADD_OPTION(FIX_TIME_WHEN_DONE); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h index 48db02d5f9..c93cb83a21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h @@ -85,6 +85,12 @@ class BBQOption : public GroupOption{ KeepGoing, }; + enum class OutOfRetries{ + Skip, + Reroll, + Stop, + }; + OCR::LanguageOCROption& LANGUAGE; SimpleIntegerOption NUM_QUESTS; @@ -109,6 +115,7 @@ class BBQOption : public GroupOption{ TeraFarmerCatchOnWin CATCH_ON_WIN; SimpleIntegerOption NUM_RETRIES; + EnumDropdownOption OUT_OF_RETRIES; BooleanCheckBoxOption FIX_TIME_WHEN_DONE; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 0e25fd09c3..2325d9b069 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -465,6 +465,13 @@ class SimulatedBBQGameActions : public BBQGameActions{ } return success; } + virtual void reroll_quest(BBQuests quest) override{ + events.push_back("reroll"); + auto iter = std::find(panel.begin(), panel.end(), quest); + if (iter != panel.end()){ + *iter = next_new_quest(); + } + } virtual void quest_completed() override{ events.push_back("completed"); } @@ -515,6 +522,7 @@ struct BBQRoundsScenario{ std::vector> also_completes; std::set never_completes; std::vector new_quests; + BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; uint64_t expected_completed = 0; std::string expected; }; @@ -534,6 +542,7 @@ class Test_BBQFarmerRounds : public UnitTest{ options.NUM_EGGS.set(0); options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop); } + options.OUT_OF_RETRIES.set(m_scenario.out_of_retries); SimulatedBBQGameActions game; game.panel = m_scenario.panel; @@ -665,6 +674,24 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ "read process read do completed read do completed end " "read process read do completed end", }, + { + // sneak-up is rerolled to photo-polar, which round 2 attempts. + .name = "out of retries with reroll replaces the failed quest", + .num_quests = 3, + .never_completes = {BBQuests::sneak_up}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 5, + .expected = "read process read do completed read do completed read do reroll end " + round_of_3 + " end", + }, + { + .name = "out of retries with stop stops at once", + .num_quests = 3, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::auto_10}, + .never_completes = {BBQuests::sneak_up}, + .out_of_retries = BBQOption::OutOfRetries::Stop, + .expected_completed = 0, + .expected = "read process read do", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index ee63a9b792..d67906f0ff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -147,7 +147,18 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; }else{ logger.log("Quest did not complete successfully."); - failed_quests.insert(current_quest); + switch (options.OUT_OF_RETRIES){ + case BBQOption::OutOfRetries::Skip: + failed_quests.insert(current_quest); + break; + case BBQOption::OutOfRetries::Reroll: + logger.log("Reroll selected. Rerolling the failed quest."); + game.reroll_quest(current_quest); + break; + case BBQOption::OutOfRetries::Stop: + logger.log("Stop selected. Stopping program.", COLOR_RED); + return; + } } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest @@ -191,6 +202,15 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); } + virtual void reroll_quest(BBQuests quest) override{ + std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); + auto iter = std::find(quests.begin(), quests.end(), quest); + if (iter == quests.end()){ + m_env.log("Failed quest is no longer on the panel. Not rerolling."); + return; + } + reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); + } virtual void quest_completed() override{ m_stats.questsCompleted++; m_env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 1afa74e6dc..c9c32d591c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -43,6 +43,10 @@ class BBQGameActions{ // Increments `eggs_hatched` when the quest hatches eggs. virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; + // Reroll a quest that failed all its attempts, if it is still on the + // panel. Spends BP. + virtual void reroll_quest(BBQuests quest) = 0; + // Progress reporting only: a quest was counted as completed. Lets stats // update after every quest instead of once per round. virtual void quest_completed() = 0; @@ -55,7 +59,8 @@ class BBQGameActions{ // Run rounds of quests until NUM_QUESTS have been completed, or until // eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the -// panel is one that already failed all its attempts. +// panel is one that already failed all its attempts, or until a quest fails +// all its attempts when OUT_OF_RETRIES is Stop. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 59d5656a8a..2f5514dbc6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -355,38 +355,6 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ const BBQOption& m_options; }; -void reroll_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 - ); - } -} //This does not handle out of BP. void reroll_all_slots( @@ -459,6 +427,39 @@ BBQRerollResult reroll_quest( 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, @@ -505,7 +506,7 @@ std::vector process_quest_list( //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, BBQ_OPTIONS, panel, 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 5939c326e6..a7044e73fe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -129,6 +129,16 @@ BBQRerollResult reroll_quest( 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( From 3f1f79f0a32cfb625860668eb683c99a7f4104bf Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 18:30:38 -0700 Subject: [PATCH 09/50] BBQ Farmer: reuse complete reroll observations before new game actions --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 84 +++++++++++++++++-- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 33 +++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 6 +- .../Farming/PokemonSV_BlueberryQuests.cpp | 31 +++++-- .../Farming/PokemonSV_BlueberryQuests.h | 19 +++-- 5 files changed, 147 insertions(+), 26 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 089a1c50cf..0570376d93 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -120,8 +120,8 @@ class SimulatedQuestPanel : public BBQRerollActions{ 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. + std::optional> read_override; bool panel_open = false; bool menu_open = false; @@ -161,7 +161,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ missed_reads--; return std::vector(quests.size(), BBQuests::UnableToDetect); } - return quests; + return read_override.value_or(quests); } }; @@ -173,11 +173,12 @@ struct BBQRerollScenario{ size_t cursor = 0; bool panel_opens = true; 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){ @@ -206,10 +207,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); @@ -222,6 +225,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."; } @@ -340,6 +347,47 @@ void add_tests_BBQuestPlan(UnitTestDatabase& database){ 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}, @@ -360,6 +408,7 @@ 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", @@ -369,6 +418,7 @@ 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", @@ -456,6 +506,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; @@ -466,7 +517,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"); @@ -476,12 +527,13 @@ class SimulatedBBQGameActions : public BBQGameActions{ doable.push_back(quest); } } - if (doable.empty()){ + const bool rerolled = doable.empty(); + 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"); @@ -555,6 +607,7 @@ struct BBQRoundsScenario{ std::set never_completes; std::vector new_quests; BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; + bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; std::string expected; }; @@ -584,6 +637,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; run_bbq_rounds(logger, options, game); @@ -606,6 +660,22 @@ class Test_BBQFarmerRounds : public UnitTest{ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ const std::string round_of_3 = "read process read do completed read do completed read do completed"; 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 diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index d67906f0ff..6c3c2fcf83 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -103,6 +103,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); @@ -117,8 +119,22 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - 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. @@ -135,9 +151,18 @@ 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(); 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); @@ -194,7 +219,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 c9c32d591c..17701b0fe5 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_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 0c9b2c2ce7..4e5425c6dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -350,6 +350,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); } @@ -388,8 +389,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; } @@ -426,13 +429,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, @@ -440,9 +449,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, @@ -519,7 +529,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, @@ -530,8 +540,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."); @@ -551,7 +562,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( @@ -664,6 +680,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 4f036ed73a..e4e614e2a2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -85,6 +85,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{ @@ -123,17 +130,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, @@ -143,7 +152,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, From 0ec332559dd7d24afb21e895d8505dcb18c0ddd8 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 18:29:36 -0700 Subject: [PATCH 10/50] lab: adapt save regression to persistent failed-quest handling --- SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index b1980077a0..7f6843989f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -734,8 +734,9 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 3, - .expected = "read process read do read do read do end " + round_of_3 + " end(save)", + .expected = "read process read do reroll read do reroll read do reroll end " + round_of_3 + " end(save)", }, { .name = "stops when out of eggs", From 2ff0fdd65cb835fbabc3b5cad7b47940234c964d Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 21:01:07 -0700 Subject: [PATCH 11/50] BBQ Farmer: replan after rerolling an exhausted quest --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 58 ++++++++++++++++++- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 8 +++ 2 files changed, 63 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 7f6843989f..023ef0e9a7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -509,6 +509,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ bool reuse_rerolled_panel = false; std::vector events; + std::vector attempted_quests; virtual std::vector read_quests() override{ events.push_back("read"); @@ -537,6 +538,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); + attempted_quests.push_back(quest); const bool success = !never_completes.contains(quest) && (m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true); if (success){ @@ -609,6 +611,7 @@ struct BBQRoundsScenario{ BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; + std::vector expected_attempted; std::string expected; }; @@ -646,6 +649,18 @@ class Test_BBQFarmerRounds : public UnitTest{ return "Expected " + std::to_string(m_scenario.expected_completed) + " completed quests, counted " + std::to_string(completed); } + if (!m_scenario.expected_attempted.empty() && game.attempted_quests != m_scenario.expected_attempted){ + std::vector expected_quests; + std::vector actual_quests; + for (BBQuests quest : m_scenario.expected_attempted){ + expected_quests.push_back(std::to_string((int)quest)); + } + for (BBQuests quest : game.attempted_quests){ + actual_quests.push_back(std::to_string((int)quest)); + } + return "Expected quest order: " + join_events(expected_quests) + + "\nActual quest order: " + join_events(actual_quests); + } const std::string actual = join_events(game.events); if (actual != m_scenario.expected){ return "Expected: " + m_scenario.expected + "\nActual: " + actual; @@ -660,6 +675,41 @@ class Test_BBQFarmerRounds : public UnitTest{ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ const std::string round_of_3 = "read process read do completed read do completed read do completed"; const BBQRoundsScenario scenarios[] = { + { + .name = "failed reroll replans before queued photo and TM", + .num_quests = 1, + .panel = {BBQuests::catch_grass, BBQuests::photo_savanna, BBQuests::make_tm}, + .outcomes = {false}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 3, + .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm}, + .expected = "read process read do reroll end " + round_of_3 + " end", + }, + { + .name = "failed reroll on full board reads fresh after round actions", + .num_quests = 1, + .panel = {BBQuests::catch_grass, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, + .outcomes = {false}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .reuse_rerolled_panel = true, + .expected_completed = 4, + .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, + .expected = "read process read do reroll end " + round_of_3 + " read do completed end", + }, + { + .name = "replanning after failure preserves a crossed save milestone", + .num_quests = 2, + .save_num_quests = 1, + .panel = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_savanna}, + .outcomes = {true, false}, + .new_quests = {BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 4, + .expected_attempted = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_savanna}, + .expected = "read process read do completed read do reroll end(save) " + round_of_3 + " end(save)", + }, { .name = "reuses reroll observation for selection and first dispatch only", .num_quests = 1, @@ -728,15 +778,17 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", }, { - // Round 1 completes nothing. It must not save, even though the - // count (0) is a multiple of the interval. + // Three partial rounds each fail and reroll their first quest. + // None may save while the completed count is still zero. .name = "round without completions does not save", .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 3, - .expected = "read process read do reroll read do reroll read do reroll end " + round_of_3 + " end(save)", + .expected = + "read process read do reroll end read process read do reroll end " + "read process read do reroll end " + round_of_3 + " end(save)", }, { .name = "stops when out of eggs", diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index ffe2b25e20..277caaa7bf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -161,6 +161,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //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; + bool replan_after_reroll = false; for (auto current_quest : quests_to_do){ //Check if quest was already completed (ex. 500 meters completed while navigating to take a photo) if (dispatch_panel){ @@ -187,6 +188,8 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga case BBQOption::OutOfRetries::Reroll: logger.log("Reroll selected. Rerolling the failed quest."); game.reroll_quest(current_quest); + logger.log("Reroll finished. Discarding the remaining batch before scheduling."); + replan_after_reroll = true; break; case BBQOption::OutOfRetries::Stop: logger.log("Stop selected. Stopping program.", COLOR_RED); @@ -200,6 +203,11 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; } quest_list.clear(); + if (replan_after_reroll){ + //A replacement belongs in the next plan, before more old queued + //work. Finish normal round actions so save milestones are kept. + break; + } } //Clear out the todo list quests_to_do.clear(); From 8dbace6c74a0a833737996229d8fa9d467dcc8bb Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 21:45:20 -0700 Subject: [PATCH 12/50] Preserve existing out-of-BP reroll fixtures during extraction --- SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 0570376d93..ec3632f72e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -120,6 +120,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ 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. std::optional> read_override; @@ -173,6 +174,7 @@ struct BBQRerollScenario{ size_t cursor = 0; bool panel_opens = true; bool menu_seen = true; + bool out_of_bp = false; int missed_reads = 0; std::optional> read_override; BBQRerollResult expected; From 50a3704bedd5ad9b825ebb638039fbf42a701980 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:21:52 -0700 Subject: [PATCH 13/50] BBQ tests: default-initialize scenario fields Give BBQRoundsScenario's `never_completes`, `new_quests` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index f98872d0ba..85b1b13f57 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -154,8 +154,8 @@ struct BBQRoundsScenario{ std::vector panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up}; std::vector outcomes; std::vector> also_completes; - std::set never_completes; - std::vector new_quests; + std::set never_completes = {}; + std::vector new_quests = {}; uint64_t expected_completed = 0; std::string expected; }; From 84800b286577344eb5f308bec8b15f5fc2a943b0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:22:12 -0700 Subject: [PATCH 14/50] BBQ tests: default-initialize scenario fields Give BBQRerollScenario's `replacements`, `expected_rerolled_slots` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 610a5bb935..af191304cb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -165,7 +165,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ struct BBQRerollScenario{ std::string name; std::vector quests; - std::vector replacements; + std::vector replacements = {}; size_t slot; size_t cursor = 0; bool panel_opens = true; @@ -173,7 +173,7 @@ struct BBQRerollScenario{ bool out_of_bp = false; int missed_reads = 0; BBQRerollResult expected; - std::vector expected_rerolled_slots; + std::vector expected_rerolled_slots = {}; std::vector expected_quests; }; From bbc231861bc66e080e6fb3a680e4cf8d7fc33bc5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:23:22 -0700 Subject: [PATCH 15/50] BBQ tests: default-initialize scenario fields Give BBQRerollScenario's `read_override`, `expected_observed_panel` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index aa954ded76..c65bac1cf3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -176,11 +176,11 @@ struct BBQRerollScenario{ bool menu_seen = true; bool out_of_bp = false; int missed_reads = 0; - std::optional> read_override; + std::optional> read_override = {}; BBQRerollResult expected; std::vector expected_rerolled_slots = {}; std::vector expected_quests; - std::optional> expected_observed_panel; + std::optional> expected_observed_panel = {}; }; std::string reroll_result_to_string(BBQRerollResult result){ From e4c5cc4aeac36c1638533529d0e5c4418b13bd47 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:23:58 -0700 Subject: [PATCH 16/50] BBQ tests: default-initialize scenario fields Give BBQRoundsScenario's `expected_attempted` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index c6ab24bb79..b18e526680 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -613,7 +613,7 @@ struct BBQRoundsScenario{ BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; - std::vector expected_attempted; + std::vector expected_attempted = {}; std::string expected; }; From 7dfc056e4e8b5c68a08883f533fa414e06bd341f Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 08:02:57 -0700 Subject: [PATCH 17/50] BBQ Farmer: reuse verified quest panel reads after a failed-quest reroll After a failed quest was rerolled (OUT_OF_RETRIES = Reroll), the round loop read the panel 3 times before the next attempt, each right after a read of the same unchanged panel: - The out-of-retries reroll now returns its verified read, and the next plan starts from it unless the round ended with a save and reset. - reroll_quest() also reuses a 3-slot read when the panel used for the reroll had 3 slots, every slot is known and the other slots agree. A 3-slot read after a 4-slot panel (a missed 4th quest) is still not reused. - The first dispatch of a round reuses the panel its work was chosen from: process_quest_list() only returns work when it changed nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 183 +++++++++++++++--- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 26 ++- .../Farming/PokemonSV_BBQSoloFarmer.h | 8 +- .../Farming/PokemonSV_BlueberryQuests.cpp | 11 +- .../Farming/PokemonSV_BlueberryQuests.h | 5 +- 5 files changed, 186 insertions(+), 47 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index b18e526680..66b178b083 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -379,6 +379,49 @@ void add_tests_BBQReroll(UnitTestDatabase& database){ .expected_rerolled_slots = {2}, .expected_quests = {make_tm, travel_500, catch_any, hatch_egg}, }, + { + // The panel used for the reroll had 3 slots, so a 3-slot read is + // not a missed fourth quest. + .name = "three-slot read of a three-slot panel is reusable", + .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}, + .expected_observed_panel = std::vector{make_tm, sneak_up, catch_any}, + }, + { + .name = "three-slot read with an unknown slot is not reusable", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{make_tm, UnableToDetect, catch_any}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, catch_any}, + }, + { + .name = "three-slot read with another changed quest is not reusable", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{photo_canyon, sneak_up, catch_any}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, catch_any}, + }, + { + // The panel used for the reroll missed this fourth quest. + .name = "fourth quest not on the three-slot panel is not reusable", + .quests = {make_tm, sneak_up, travel_500}, + .replacements = {catch_any}, + .slot = 2, + .read_override = std::vector{make_tm, sneak_up, catch_any, hatch_egg}, + .expected = BBQRerollResult::Rerolled, + .expected_rerolled_slots = {2}, + .expected_quests = {make_tm, sneak_up, catch_any}, + }, { .name = "complete observation after missed reads is reusable", .quests = {make_tm, travel_500, sneak_up, hatch_egg}, @@ -398,6 +441,7 @@ void add_tests_BBQReroll(UnitTestDatabase& database){ .expected = BBQRerollResult::Rerolled, .expected_rerolled_slots = {2}, .expected_quests = {make_tm, sneak_up, catch_any}, + .expected_observed_panel = std::vector{make_tm, sneak_up, catch_any}, }, { // Seen on a Switch: the panel can open with the cursor where it @@ -455,6 +499,7 @@ void add_tests_BBQReroll(UnitTestDatabase& database){ .expected = BBQRerollResult::Rerolled, .expected_rerolled_slots = {2}, .expected_quests = {make_tm, sneak_up, catch_any}, + .expected_observed_panel = std::vector{make_tm, sneak_up, catch_any}, }, { .name = "three missed reads give up", @@ -487,10 +532,11 @@ namespace{ // A singleplayer quest panel that behaves like the game's: 3 blue quests, // and a completed quest is replaced by a new one. process_quest_list() -// follows the two rules of the real one that the round loop depends on: -// quests in not_possible_quests are skipped, and if nothing is doable all -// quests are rerolled and nothing is returned. Red quests and the -// exclusions table are not modeled. Records the game actions taken. +// follows the rules of the real one that the round loop depends on: quests +// in not_possible_quests are skipped, quests in table_rerolls are rerolled +// in their slot as an exclusions table reroll row does, and if nothing is +// doable all quests are rerolled. After any reroll it returns nothing to do. +// Red quests are not modeled. Records the game actions taken. class SimulatedBBQGameActions : public BBQGameActions{ public: std::vector panel; @@ -508,6 +554,10 @@ class SimulatedBBQGameActions : public BBQGameActions{ BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, }; + // Quests that process_quest_list() rerolls in their slot. + std::set table_rerolls; + // Rerolls return the panel they read to verify the reroll, as + // reroll_quest() does when that read is reusable. bool reuse_rerolled_panel = false; std::vector events; @@ -524,19 +574,29 @@ class SimulatedBBQGameActions : public BBQGameActions{ const std::vector& quests, uint8_t ) override{ events.push_back("process"); + bool rerolled = false; + for (BBQuests& slot : panel){ + if (table_rerolls.contains(slot)){ + slot = next_new_quest(); + rerolled = true; + } + } std::vector doable; for (BBQuests quest : quests){ if (!not_possible_quests.contains(quest)){ doable.push_back(quest); } } - const bool rerolled = doable.empty(); - if (rerolled){ + if (!rerolled && doable.empty()){ for (BBQuests& slot : panel){ slot = next_new_quest(); } + rerolled = true; + } + if (rerolled){ + return {{}, reuse_rerolled_panel ? std::optional(panel) : std::nullopt}; } - return {std::move(doable), rerolled && reuse_rerolled_panel && quests.size() == 4 ? std::optional(panel) : std::nullopt}; + return {std::move(doable), std::nullopt}; } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); @@ -553,12 +613,14 @@ class SimulatedBBQGameActions : public BBQGameActions{ } return success; } - virtual void reroll_quest(BBQuests quest) override{ + virtual std::optional> reroll_quest(BBQuests quest) override{ events.push_back("reroll"); auto iter = std::find(panel.begin(), panel.end(), quest); - if (iter != panel.end()){ - *iter = next_new_quest(); + if (iter == panel.end()){ + return std::nullopt; } + *iter = next_new_quest(); + return reuse_rerolled_panel ? std::optional(panel) : std::nullopt; } virtual void quest_completed() override{ events.push_back("completed"); @@ -611,9 +673,11 @@ struct BBQRoundsScenario{ std::set never_completes = {}; std::vector new_quests = {}; BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; + std::set table_rerolls; bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; std::vector expected_attempted = {}; + std::optional expected_reads; std::string expected; }; @@ -642,6 +706,7 @@ class Test_BBQFarmerRounds : public UnitTest{ if (!m_scenario.new_quests.empty()){ game.new_quests = m_scenario.new_quests; } + game.table_rerolls = m_scenario.table_rerolls; game.reuse_rerolled_panel = m_scenario.reuse_rerolled_panel; NullLogger logger; @@ -663,6 +728,11 @@ class Test_BBQFarmerRounds : public UnitTest{ return "Expected quest order: " + join_events(expected_quests) + "\nActual quest order: " + join_events(actual_quests); } + const size_t reads = (size_t)std::count(game.events.begin(), game.events.end(), "read"); + if (m_scenario.expected_reads && reads != *m_scenario.expected_reads){ + return "Expected " + std::to_string(*m_scenario.expected_reads) + + " quest panel reads, counted " + std::to_string(reads); + } const std::string actual = join_events(game.events); if (actual != m_scenario.expected){ return "Expected: " + m_scenario.expected + "\nActual: " + actual; @@ -675,7 +745,7 @@ class Test_BBQFarmerRounds : public UnitTest{ }; void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ - const std::string round_of_3 = "read process read do completed read do completed read do completed"; + const std::string round_of_3 = "read process do completed read do completed read do completed"; const BBQRoundsScenario scenarios[] = { { .name = "failed reroll replans before queued photo and TM", @@ -686,10 +756,10 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 3, .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm}, - .expected = "read process read do reroll end " + round_of_3 + " end", + .expected = "read process do reroll end " + round_of_3 + " end", }, { - .name = "failed reroll on full board reads fresh after round actions", + .name = "failed reroll on full board reuses its verified read after round actions", .num_quests = 1, .panel = {BBQuests::catch_grass, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, .outcomes = {false}, @@ -698,7 +768,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .reuse_rerolled_panel = true, .expected_completed = 4, .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, - .expected = "read process read do reroll end " + round_of_3 + " read do completed end", + .expected = "read process do reroll end process do completed read do completed read do completed read do completed end", }, { .name = "replanning after failure preserves a crossed save milestone", @@ -710,7 +780,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 4, .expected_attempted = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_savanna}, - .expected = "read process read do completed read do reroll end(save) " + round_of_3 + " end(save)", + .expected = "read process do completed read do reroll end(save) " + round_of_3 + " end(save)", }, { .name = "reuses reroll observation for selection and first dispatch only", @@ -721,12 +791,61 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .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", + // reroll_quest() decides whether a 3-slot read is reusable. + .name = "reusable three-slot reroll observation is not read 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", + .expected = "read process process do completed read do completed read do completed end", + }, + { + // Seen in a live run: catch-fighting fails and is rerolled into + // pickup-10, which the exclusions table rerolls into travel-500. + // Each reroll's verified read is reused, and so is the last one + // for the first dispatch. The round loop used to read the panel + // 3 times between the failed reroll and the next attempt. + .name = "failed reroll then table reroll reuse verified reads", + .num_quests = 1, + .panel = {BBQuests::catch_fighting, BBQuests::make_tm, BBQuests::photo_savanna}, + .outcomes = {false}, + .new_quests = {BBQuests::pickup_10, BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .table_rerolls = {BBQuests::pickup_10}, + .reuse_rerolled_panel = true, + .expected_completed = 3, + .expected_attempted = {BBQuests::catch_fighting, BBQuests::travel_500, BBQuests::make_tm, BBQuests::photo_savanna}, + .expected_reads = 3, + .expected = "read process do reroll end process process do completed read do completed read do completed end", + }, + { + // As above, but neither reroll's read is reusable. Only the + // first dispatch skips a read: 2 reads before the next attempt + // instead of 3. + .name = "failed reroll then table reroll without reusable reads", + .num_quests = 1, + .panel = {BBQuests::catch_fighting, BBQuests::make_tm, BBQuests::photo_savanna}, + .outcomes = {false}, + .new_quests = {BBQuests::pickup_10, BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .table_rerolls = {BBQuests::pickup_10}, + .expected_completed = 3, + .expected_attempted = {BBQuests::catch_fighting, BBQuests::travel_500, BBQuests::make_tm, BBQuests::photo_savanna}, + .expected_reads = 5, + .expected = "read process do reroll end read process read process do completed read do completed read do completed end", + }, + { + // The save resets the game, so the reroll's read is not reused. + .name = "failed reroll before a save reads the panel after it", + .num_quests = 2, + .save_num_quests = 1, + .panel = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_savanna}, + .outcomes = {true, false}, + .new_quests = {BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .reuse_rerolled_panel = true, + .expected_completed = 4, + .expected = "read process do completed read do reroll end(save) " + round_of_3 + " end(save)", }, { // The goal (4) is reached in the middle of round 2. The loop only @@ -745,7 +864,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .panel = {BBQuests::photo_swim, BBQuests::travel_500, BBQuests::make_tm}, .also_completes = {{BBQuests::photo_swim, BBQuests::travel_500}}, .expected_completed = 3, - .expected = "read process read do completed read completed read do completed end", + .expected = "read process do completed read completed read do completed end", }, { // The first attempt fails: 3 attempts, 2 completions. @@ -753,7 +872,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .num_quests = 2, .outcomes = {false}, .expected_completed = 2, - .expected = "read process read do read do completed read do completed end", + .expected = "read process do read do completed read do completed end", }, { // Nothing on the panel is doable, so process_quest_list() rerolls @@ -789,8 +908,8 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 3, .expected = - "read process read do reroll end read process read do reroll end " - "read process read do reroll end " + round_of_3 + " end(save)", + "read process do reroll end read process do reroll end " + "read process do reroll end " + round_of_3 + " end(save)", }, { .name = "stops when out of eggs", @@ -807,8 +926,8 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .never_completes = {BBQuests::sneak_up}, .expected_completed = 4, .expected = - "read process read do completed read do completed read do end " - "read process read do completed read do completed end", + "read process do completed read do completed read do end " + "read process do completed read do completed end", }, { .name = "stops when only failed quests are doable", @@ -816,7 +935,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .panel = {BBQuests::sneak_up, BBQuests::pickup_10, BBQuests::catch_flying}, .never_completes = {BBQuests::sneak_up}, .expected_completed = 0, - .expected = "read process read do end read process", + .expected = "read process do end read process", }, { // sneak-up fails, then leaves the panel along with make-tm. Round 2 @@ -830,9 +949,9 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, .expected_completed = 5, .expected = - "read process read do read do completed end " - "read process read do completed read do completed end " - "read process read do completed read do completed end", + "read process do read do completed end " + "read process do completed read do completed end " + "read process do completed read do completed end", }, { // As above with 3 slots. A 4th quest could have been missed by @@ -845,9 +964,9 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, .expected_completed = 4, .expected = - "read process read do read do completed end " - "read process read do completed read do completed end " - "read process read do completed end", + "read process do read do completed end " + "read process do completed read do completed end " + "read process do completed end", }, { // sneak-up is rerolled to photo-polar, which round 2 attempts. @@ -856,7 +975,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .never_completes = {BBQuests::sneak_up}, .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 5, - .expected = "read process read do completed read do completed read do reroll end " + round_of_3 + " end", + .expected = "read process do completed read do completed read do reroll end " + round_of_3 + " end", }, { .name = "out of retries with stop stops at once", @@ -865,7 +984,7 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .never_completes = {BBQuests::sneak_up}, .out_of_retries = BBQOption::OutOfRetries::Stop, .expected_completed = 0, - .expected = "read process read do", + .expected = "read process do", }, }; for (const BBQRoundsScenario& scenario : scenarios){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 277caaa7bf..45faa1c628 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -126,7 +126,6 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - 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); @@ -139,7 +138,9 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga 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){ + //Work is only returned when nothing changed in game, so the panel + //it was chosen from is still current for the first dispatch. + if (!quests_to_do.empty() && !observed_panel){ dispatch_panel = quest_list; } @@ -158,10 +159,11 @@ 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. + //Never carry an observation across quest actions or a save and reset. observed_panel.reset(); const uint64_t completed_before_round = num_completed_quests; bool replan_after_reroll = false; + std::optional> rerolled_panel; for (auto current_quest : quests_to_do){ //Check if quest was already completed (ex. 500 meters completed while navigating to take a photo) if (dispatch_panel){ @@ -187,7 +189,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga break; case BBQOption::OutOfRetries::Reroll: logger.log("Reroll selected. Rerolling the failed quest."); - game.reroll_quest(current_quest); + rerolled_panel = game.reroll_quest(current_quest); logger.log("Reroll finished. Discarding the remaining batch before scheduling."); replan_after_reroll = true; break; @@ -214,7 +216,15 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; // A round can cross a milestone without ending at an exact multiple. - game.end_round(bbq_save_due(completed_before_round, num_completed_quests, temp_save_num_option)); + const bool save = bbq_save_due(completed_before_round, num_completed_quests, temp_save_num_option); + game.end_round(save); + + //The reroll was the last game action of the round. Fixing the date + //does not change the quests, so its verified read can seed the next + //plan. A save resets the game, so the panel is read again after one. + if (rerolled_panel && !save){ + observed_panel = std::move(rerolled_panel); + } } } @@ -244,14 +254,14 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); } - virtual void reroll_quest(BBQuests quest) override{ + virtual std::optional> reroll_quest(BBQuests quest) override{ std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); auto iter = std::find(quests.begin(), quests.end(), quest); if (iter == quests.end()){ m_env.log("Failed quest is no longer on the panel. Not rerolling."); - return; + return std::nullopt; } - reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); + return reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); } virtual void quest_completed() override{ m_stats.questsCompleted++; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 17701b0fe5..c7e9c1fbeb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -8,6 +8,7 @@ #define PokemonAutomation_PokemonSV_BBQSoloFarmer_H #include +#include #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" @@ -34,6 +35,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; a complete post-reroll observation can seed selection. + // Only returns quests to attempt when it changed nothing in game, so + // `quests` still shows the panel. virtual BBQuestBatch process_quest_list( const std::vector& quests, uint8_t eggs_hatched ) = 0; @@ -44,8 +47,9 @@ class BBQGameActions{ virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; // Reroll a quest that failed all its attempts, if it is still on the - // panel. Spends BP. - virtual void reroll_quest(BBQuests quest) = 0; + // panel. Spends BP. Returns the panel read to verify the reroll when + // reroll_quest() found it reusable, so scheduling can skip a read. + virtual std::optional> reroll_quest(BBQuests quest) = 0; // Progress reporting only: a quest was counted as completed. Lets stats // update after every quest instead of once per round. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 4e5425c6dc..92bc9a990f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -429,9 +429,14 @@ 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 + //Reuse only a read whose slots are all known and, apart from the + //rerolled one, agree with the panel used for this reroll. Three + //slots can mean a missed fourth quest, so a 3-slot read is only + //reused when that panel had 3 slots too. Both reads could have + //missed the same fourth quest, as a fresh read also could. That + //quest is then left out of one plan, which costs no BP, and a + //3-slot read never proves a failed quest left the panel. + if (after.size() >= 3 && after == quests && std::find(after.begin(), after.end(), BBQuests::UnableToDetect) == after.end()){ observed_panel = std::move(after); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index e4e614e2a2..eec8eb1034 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -131,7 +131,8 @@ 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. observed_panel is populated -//only for four known slots with no other changes, so scheduling can reuse it. +//only when every slot is known and no other slot changed, so scheduling can +//reuse it. A 3-slot read is only reused when `quests` had 3 slots. BBQRerollResult reroll_quest( Logger& logger, BBQRerollActions& actions, @@ -141,7 +142,7 @@ BBQRerollResult reroll_quest( ); //Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program -//if the reroll fails. Returns a complete observed panel when reusable. +//if the reroll fails. Returns the observed panel when reusable. std::optional> reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, From f4d4c473a61fa40cbe88be6c880676587e38b87f Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:24:17 -0700 Subject: [PATCH 18/50] BBQ tests: default-initialize scenario fields Give BBQRoundsScenario's `table_rerolls`, `expected_reads` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 66b178b083..11c07336c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -673,11 +673,11 @@ struct BBQRoundsScenario{ std::set never_completes = {}; std::vector new_quests = {}; BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; - std::set table_rerolls; + std::set table_rerolls = {}; bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; std::vector expected_attempted = {}; - std::optional expected_reads; + std::optional expected_reads = {}; std::string expected; }; From 778fe05f6e011c50f357ae030eea1ae42473109b Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 14:17:14 -0700 Subject: [PATCH 19/50] BBQ Farmer: chain no-trip quests without round actions between Make TM and Travel 500 m start at the Central Plaza fly point and fly back to it. After one completes, the panel read that checked it is reused: - for the next quest of the round when that is also a no-trip quest, and - to plan the next round when it was the last quest, skipping the round actions (date fix) unless a save is due or the quest goal is reached. do_quest() now hands back the panel read that checked the last attempt. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 84 ++++++++++++++++--- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 40 ++++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 13 ++- .../Farming/PokemonSV_BlueberryQuests.cpp | 6 +- .../Farming/PokemonSV_BlueberryQuests.h | 4 +- 5 files changed, 127 insertions(+), 20 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 11c07336c3..b9f9dbbfb7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -598,7 +598,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ } return {std::move(doable), std::nullopt}; } - virtual bool do_quest(BBQuests quest, uint8_t&) override{ + virtual bool do_quest(BBQuests quest, uint8_t&, std::vector& panel_after) override{ events.push_back("do"); attempted_quests.push_back(quest); const bool success = !never_completes.contains(quest) @@ -611,6 +611,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ } } } + panel_after = panel; return success; } virtual std::optional> reroll_quest(BBQuests quest) override{ @@ -815,13 +816,13 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .reuse_rerolled_panel = true, .expected_completed = 3, .expected_attempted = {BBQuests::catch_fighting, BBQuests::travel_500, BBQuests::make_tm, BBQuests::photo_savanna}, - .expected_reads = 3, - .expected = "read process do reroll end process process do completed read do completed read do completed end", + .expected_reads = 2, + .expected = "read process do reroll end process process do completed do completed read do completed end", }, { // As above, but neither reroll's read is reusable. Only the // first dispatch skips a read: 2 reads before the next attempt - // instead of 3. + // instead of 3. make-tm starts from travel-500's read. .name = "failed reroll then table reroll without reusable reads", .num_quests = 1, .panel = {BBQuests::catch_fighting, BBQuests::make_tm, BBQuests::photo_savanna}, @@ -831,8 +832,8 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .table_rerolls = {BBQuests::pickup_10}, .expected_completed = 3, .expected_attempted = {BBQuests::catch_fighting, BBQuests::travel_500, BBQuests::make_tm, BBQuests::photo_savanna}, - .expected_reads = 5, - .expected = "read process do reroll end read process read process do completed read do completed read do completed end", + .expected_reads = 4, + .expected = "read process do reroll end read process read process do completed do completed read do completed end", }, { // The save resets the game, so the reroll's read is not reused. @@ -939,8 +940,9 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ }, { // sneak-up fails, then leaves the panel along with make-tm. Round 2 - // reads a full panel without it, and its replacement for - // photo-canyon brings it back. Round 3 attempts it again. + // plans round 2 from a full panel without it (make-tm's read, no + // round actions), and its replacement for photo-canyon brings it + // back. Round 3 attempts it again. .name = "failed quest that leaves a full panel is attempted again", .num_quests = 5, .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10, BBQuests::catch_flying}, @@ -949,8 +951,8 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, .expected_completed = 5, .expected = - "read process do read do completed end " - "read process do completed read do completed end " + "read process do read do completed " + "process do completed read do completed end " "read process do completed read do completed end", }, { @@ -964,8 +966,8 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, .expected_completed = 4, .expected = - "read process do read do completed end " - "read process do completed read do completed end " + "read process do read do completed " + "process do completed read do completed end " "read process do completed end", }, { @@ -986,6 +988,64 @@ void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected_completed = 0, .expected = "read process do", }, + { + // make-tm ends round 1 back at Central Plaza: its read plans + // round 2 with no round actions between it and travel-500. + .name = "no-trip quests chain with no round actions between", + .num_quests = 5, + .panel = {BBQuests::auto_10, BBQuests::sneak_up, BBQuests::make_tm}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .expected_completed = 6, + .expected_attempted = { + BBQuests::auto_10, BBQuests::sneak_up, BBQuests::make_tm, + BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, + }, + .expected_reads = 5, + .expected = + "read process do completed read do completed read do completed " + "process do completed read do completed read do completed end", + }, + { + // make-tm, travel-500 and photo-canyon on one board: only + // travel-500 starts from make-tm's read. The trip quest reads + // the panel and ends the round as usual. + .name = "no-trip then trip quest keeps the trip's usual steps", + .num_quests = 3, + .panel = {BBQuests::make_tm, BBQuests::travel_500, BBQuests::photo_canyon}, + .new_quests = {BBQuests::photo_coastal, BBQuests::photo_polar, BBQuests::photo_savanna}, + .expected_completed = 3, + .expected_reads = 2, + .expected = "read process do completed do completed read do completed end", + }, + { + .name = "failed no-trip quest ends the round as usual", + .num_quests = 3, + .panel = {BBQuests::auto_10, BBQuests::sneak_up, BBQuests::make_tm}, + .outcomes = {true, true, false}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .expected_completed = 4, + .expected = + "read process do completed read do completed read do end " + "read process do completed read do completed end", + }, + { + .name = "no-trip quest does not chain past a save", + .num_quests = 6, + .save_num_quests = 3, + .panel = {BBQuests::auto_10, BBQuests::sneak_up, BBQuests::make_tm}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .expected_completed = 6, + .expected = + "read process do completed read do completed read do completed end(save) " + "read process do completed read do completed read do completed end(save)", + }, + { + .name = "no-trip quest that reaches the goal ends the round", + .num_quests = 3, + .panel = {BBQuests::auto_10, BBQuests::sneak_up, BBQuests::make_tm}, + .expected_completed = 3, + .expected = "read process do completed read do completed read do completed end", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 45faa1c628..2eeed27353 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -91,6 +91,17 @@ void forget_quests_gone_from_panel(std::set& failed_quests, const std: }); } +} + +// Quests done at Central Plaza with no flight out: they start at its fly +// point and fly back to it, so the player is where the round actions and the +// next quest expect. Every other route flies to another plaza or area. +bool bbq_quest_is_no_trip(BBQuests quest){ + return quest == BBQuests::make_tm || quest == BBQuests::travel_500; +} + +namespace{ + // Whether a round that took the completed count from previous_completed // to completed crossed a nonzero multiple of the save interval. bool bbq_save_due(uint64_t previous_completed, uint64_t completed, uint64_t interval){ @@ -164,23 +175,37 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga const uint64_t completed_before_round = num_completed_quests; bool replan_after_reroll = false; std::optional> rerolled_panel; + //The panel read that checked a completed no-trip quest. Nothing runs + //between it and the next dispatch or plan, so a no-trip quest next or + //the next plan starts from it instead of reading again. + std::optional> no_trip_panel; for (auto current_quest : quests_to_do){ //Check if quest was already completed (ex. 500 meters completed while navigating to take a photo) if (dispatch_panel){ logger.log("Reusing verified quest panel for first dispatch."); quest_list = std::move(*dispatch_panel); dispatch_panel.reset(); + }else if (no_trip_panel && bbq_quest_is_no_trip(current_quest)){ + logger.log("Reusing quest panel read after a no-trip quest for dispatch."); + quest_list = std::move(*no_trip_panel); + no_trip_panel.reset(); + forget_quests_gone_from_panel(failed_quests, quest_list); }else{ + no_trip_panel.reset(); 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); + std::vector panel_after; + bool questSuccess = game.do_quest(current_quest, eggs_hatched, panel_after); if (questSuccess){ logger.log("Quest completed successfully."); game.quest_completed(); num_completed_quests++; + if (bbq_quest_is_no_trip(current_quest)){ + no_trip_panel = std::move(panel_after); + } }else{ logger.log("Quest did not complete successfully."); switch (options.OUT_OF_RETRIES){ @@ -217,6 +242,15 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; // A round can cross a milestone without ending at an exact multiple. const bool save = bbq_save_due(completed_before_round, num_completed_quests, temp_save_num_option); + + //A no-trip quest ended the round at Central Plaza, and fixing the date + //does not change the quests, so plan the next quests from its read + //at once. Round actions still run before a save or the last stop. + if (no_trip_panel && !save && num_completed_quests < options.NUM_QUESTS){ + logger.log("Chaining after a no-trip quest. Skipping round actions."); + observed_panel = std::move(no_trip_panel); + continue; + } game.end_round(save); //The reroll was the last game action of the round. Fixing the date @@ -251,8 +285,8 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ ) override{ return PokemonSV::process_quest_list(m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched); } - virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ - return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched, std::vector& panel_after) override{ + return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched, &panel_after); } virtual std::optional> reroll_quest(BBQuests quest) override{ std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index c7e9c1fbeb..07902e50e9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -43,8 +43,9 @@ class BBQGameActions{ // Attempt one quest that is on the panel, after process_quest_list() // chose it. Returns true if it is no longer on the panel afterwards. - // Increments `eggs_hatched` when the quest hatches eggs. - virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; + // Increments `eggs_hatched` when the quest hatches eggs. Sets + // `panel_after` to the quest panel read that checked the last attempt. + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched, std::vector& panel_after) = 0; // Reroll a quest that failed all its attempts, if it is still on the // panel. Spends BP. Returns the panel read to verify the reroll when @@ -61,10 +62,16 @@ class BBQGameActions{ virtual void end_round(bool save) = 0; }; +// Whether a quest is done at Central Plaza with no flight out. +bool bbq_quest_is_no_trip(BBQuests quest); + // Run rounds of quests until NUM_QUESTS have been completed, or until // eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the // panel is one that already failed all its attempts, or until a quest fails -// all its attempts when OUT_OF_RETRIES is Stop. +// all its attempts when OUT_OF_RETRIES is Stop. No-trip quests (see +// bbq_quest_is_no_trip()) are chained: the panel read that checked one is +// reused for a no-trip quest next or for the next plan, without the round +// actions between. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 92bc9a990f..152d352525 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -580,7 +580,8 @@ bool process_and_do_quest( ConsoleHandle& console, ProControllerContext& context, BBQOption& BBQ_OPTIONS, BBQuests current_quest, - uint8_t& eggs_hatched + uint8_t& eggs_hatched, + std::vector* panel_after ){ bool quest_completed = false; int quest_attempts = 0; @@ -687,6 +688,9 @@ 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 (panel_after){ + *panel_after = quest_list; + } 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."); quest_attempts++; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index eec8eb1034..53144fc6a8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -162,12 +162,14 @@ BBQuestBatch process_quest_list( ); //Take the current quest and calls the function to do it, then checks the quest was successful. Returns true if so. +//If panel_after is given, it receives the quest panel read that checked the last attempt. bool process_and_do_quest( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, BBQOption& BBQ_OPTIONS, BBQuests current_quest, - uint8_t& eggs_hatched + uint8_t& eggs_hatched, + std::vector* panel_after = nullptr ); //Iterate through TMs until a craftable one is found. Make the TM and return to position. From 2aedcd0bd8fc8cdd5664cea5dd67a5d7d0efc4d4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 18:54:38 -0700 Subject: [PATCH 20/50] Fix BP OCR crop including the "BP" suffix The Blueberry Points crop ended at x=0.957, which included the left stroke of the "B" in the "BP" suffix. PaddleOCR read it as an extra digit, so a balance of 6750 came back as 67501. Narrow the crop to end at x=0.954 and move the reader into Inference/PokemonSV_BlueberryPointsReader so it can be tested from a captured frame without depending on the BBQ program code. read_BP() still opens and closes the panel and delegates to the shared reader. Register four PokemonSV::BlueberryPoints unit tests at 1920x1080 and 1280x720 on two full-frame reference screenshots (6750 and 6740 BP, taken one reroll apart). The images go to the CommandLineTests repo under PokemonSV/BlueberryPoints/. With the old crop restored, all four fail. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV_BlueberryPointsReader.cpp | 39 ++++++++++++++++++ .../PokemonSV_BlueberryPointsReader.h | 25 ++++++++++++ .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 40 +++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 8 +--- SerialPrograms/cmake/SourceFiles.cmake | 2 + 5 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp new file mode 100644 index 0000000000..4d5f5fc4ba --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp @@ -0,0 +1,39 @@ +/* Blueberry Points Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_NumberReader.h" +#include "PokemonSV_BlueberryPointsReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +int read_BP_from_image(Logger& logger, const ImageViewRGB32& screen){ + // Normalized coordinates for the upper-right balance on a 16:9 quest panel. + // Keep the right edge at 0.954: the old 0.957 edge included the left stroke + // of the B in "BP", making 6750 read as 67501. See the full-frame reference + // UnitTestResources/PokemonSV/BlueberryPoints/6750.png and its README. + const ImageFloatBox BP_NUMBER_BOX(0.866, 0.019, 0.088, 0.041); + // Isolate the near-white digits from the blue panel background. These are + // the existing quest-panel text thresholds, inclusive on all RGB channels. + const uint32_t BP_TEXT_MIN = combine_rgb(198, 198, 198); + const uint32_t BP_TEXT_MAX = combine_rgb(255, 255, 255); + ImageRGB32 number = to_blackwhite_rgb32_range( + extract_box_reference(screen, BP_NUMBER_BOX), true, BP_TEXT_MIN, BP_TEXT_MAX + ); + return OCR::read_number(logger, number); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h new file mode 100644 index 0000000000..d11517de2a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h @@ -0,0 +1,25 @@ +/* Blueberry Points Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BlueberryPointsReader_H +#define PokemonAutomation_PokemonSV_BlueberryPointsReader_H + +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Read the numeric balance from a captured 16:9 quest panel; -1 on OCR failure. +int read_BP_from_image(Logger& logger, const ImageViewRGB32& screen); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 7788036545..02e94b05c7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -28,6 +28,9 @@ #include "Options/PokemonSV_BBQuests.h" #include "Programs/Farming/PokemonSV_BlueberryQuests.h" #include "PokemonSV_Tests.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "Inference/PokemonSV_BlueberryPointsReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -116,8 +119,45 @@ class Test_BBQuestsSlugs : public UnitTest{ } +namespace{ + +class Test_BlueberryPoints : public UnitTest{ +public: + Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) + : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_expected(expected), m_width(width), m_height(height) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(m_image); + ImageRGB32 image = reference.scale_to(m_width, m_height); + const int actual = read_BP_from_image(logger, image); + if (actual != m_expected){ + return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); + } + return true; + } + +private: + const std::string m_image; + const int m_expected; + const size_t m_width, m_height; +}; + +} + void add_tests(UnitTestDatabase& database){ database.add(); + + // Full-frame references, in the CommandLineTests repo: they exercise the production + // crop, filter and OCR together. + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); + + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); + add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 8bec917a63..c90bae7a04 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -32,6 +32,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_MenuNavigation.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h" +#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" #include "VideoGames/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.h" #include "VideoGames/PokemonSV/Programs/Boxes/PokemonSV_BoxRelease.h" #include "VideoGames/PokemonSV/Inference/Boxes/PokemonSV_BoxEggDetector.h" @@ -67,17 +68,12 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context.wait_for_all_requests(); VideoSnapshot screen = stream.video().snapshot(); - ImageRGB32 BP_value = to_blackwhite_rgb32_range( - extract_box_reference(screen, ImageFloatBox(0.866, 0.019, 0.091, 0.041)), - true, - combine_rgb(198, 198, 198), combine_rgb(255, 255, 255) - ); //Close panel pbf_mash_button(context, BUTTON_B, 800ms); context.wait_for_all_requests(); - return OCR::read_number(stream.logger(), BP_value); + return read_BP_from_image(stream.logger(), screen); } std::vector read_quests( diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 39c48d531e..251ef0cf6b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2386,6 +2386,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Inference/PokemonSV_AuctionItemNameReader.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BagDetector.cpp Source/VideoGames/PokemonSV/Inference/PokemonSV_BagDetector.h + Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.cpp + Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.cpp Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h Source/VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestReader.cpp From 2779423d94539cf2674b7aafe0a4ed733e4201cd Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 26 Oct 2026 22:51:24 -0700 Subject: [PATCH 21/50] PokemonSV: move the BBQ farmer's tests into their own file PokemonSV_Tests.cpp registers every SV test, and each BBQ change adds its controller tests there, so parallel BBQ changes all conflict in it. Move the Tera encounter tests unchanged into Programs/Farming/PokemonSV_BBQTests.cpp, registered through one add_tests_BBQ() call, following the add_tests_() pattern the detectors use. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 56 +------------- .../Programs/Farming/PokemonSV_BBQTests.cpp | 77 +++++++++++++++++++ .../Programs/Farming/PokemonSV_BBQTests.h | 26 +++++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 4 files changed, 107 insertions(+), 54 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 7788036545..eeb891a1cb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -26,7 +26,7 @@ #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" #include "Options/PokemonSV_BBQuests.h" -#include "Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "Programs/Farming/PokemonSV_BBQTests.h" #include "PokemonSV_Tests.h" namespace PokemonAutomation{ @@ -35,58 +35,6 @@ namespace PokemonSV{ namespace{ -struct TeraEncounterScenario{ - std::string name; - bool shiny = false; - int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first - bool battle_menu_detected = false; // The route's own wait saw the battle menu. - TeraEncounterAction expected; -}; - -class Test_TeraEncounterAction : public UnitTest{ -public: - Test_TeraEncounterAction(TeraEncounterScenario scenario) - : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const TeraEncounterAction actual = tera_encounter_action( - m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected - ); - if (actual != m_scenario.expected){ - return "Expected action " + std::to_string((int)m_scenario.expected) - + ", got " + std::to_string((int)actual) + "."; - } - return true; - } - -private: - const TeraEncounterScenario m_scenario; -}; - -} - -void add_tests_TeraEncounterAction(UnitTestDatabase& database){ - using enum TeraEncounterAction; - const std::vector scenarios{ - {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, - {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, - {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, - {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, - {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, - {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, - {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, - {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, - {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, - }; - for (const TeraEncounterScenario& scenario : scenarios){ - database.add(scenario); - } -} - -namespace{ - class Test_BBQuestsSlugs : public UnitTest{ public: Test_BBQuestsSlugs() @@ -138,7 +86,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_TeraTypeReader(database); add_tests_NormalBattleMenus(database); add_tests_LetsGoKillDetector(database); - add_tests_TeraEncounterAction(database); + add_tests_BBQ(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp new file mode 100644 index 0000000000..999661c313 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -0,0 +1,77 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQTests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +namespace{ + +struct TeraEncounterScenario{ + std::string name; + bool shiny = false; + int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first + bool battle_menu_detected = false; // The route's own wait saw the battle menu. + TeraEncounterAction expected; +}; + +class Test_TeraEncounterAction : public UnitTest{ +public: + Test_TeraEncounterAction(TeraEncounterScenario scenario) + : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TeraEncounterAction actual = tera_encounter_action( + m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected + ); + if (actual != m_scenario.expected){ + return "Expected action " + std::to_string((int)m_scenario.expected) + + ", got " + std::to_string((int)actual) + "."; + } + return true; + } + +private: + const TeraEncounterScenario m_scenario; +}; + +} + +static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ + using enum TeraEncounterAction; + const std::vector scenarios{ + {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, + {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, + {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, + {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, + {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, + {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, + {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, + {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, + {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, + }; + for (const TeraEncounterScenario& scenario : scenarios){ + database.add(scenario); + } +} + + +void add_tests_BBQ(UnitTestDatabase& database){ + add_tests_TeraEncounterAction(database); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h new file mode 100644 index 0000000000..64317f45aa --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h @@ -0,0 +1,26 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQTests_H +#define PokemonAutomation_PokemonSV_BBQTests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + + +// Blueberry Quest farmer tests: controller decisions run against scripted inputs. +void add_tests_BBQ(UnitTestDatabase& database); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 39c48d531e..ce54742834 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2597,6 +2597,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp From 272e7fe62228d5d98c086ab2567e22ba9452cd14 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 21:08:30 -0700 Subject: [PATCH 22/50] Run the BBQ farmer's rounds against an injectable BBQGameActions BBQSoloFarmer::program() runs its quest rounds inline against the console, so the round logic (which quests count, when to stop, when to save) can only be exercised by running the farmer on a Switch. Move the round loop into run_bbq_rounds(), which takes the game actions it depends on as a BBQGameActions interface: read the quests, process them, do a quest, report a completed quest, and end the round (saving if due). BBQSoloFarmer implements these with the calls they replace, in the same order; the date fix, save/reset, stats and notification move together into end_round(). Startup, flying to the plaza and end-of-program handling stay in program(). The commented-out single-quest debug block declares its own egg counter now that the loop locals have moved. process_quest_list() only reads its quest list and egg count, so take them as const& and by value. Add PokemonSV::BBQFarmerRounds, a table of scenarios run against a simulated singleplayer quest panel: 3 blue quests, completed quests are replaced, impossible quests are skipped, and a panel with nothing doable is rerolled. Each scenario checks the number of completed quests and the exact action sequence: finishing the round that reaches the goal, a quest already gone, a failed quest, a panel with nothing doable, saving at exact multiples, and stopping when out of eggs. No behavior change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 166 ++++++++----- .../Farming/PokemonSV_BBQSoloFarmer.h | 42 ++++ .../Programs/Farming/PokemonSV_BBQTests.cpp | 224 ++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 4 +- .../Farming/PokemonSV_BlueberryQuests.h | 4 +- 5 files changed, 377 insertions(+), 63 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index fcfa3182fa..e71f27ac80 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -74,48 +74,22 @@ BBQSoloFarmer::BBQSoloFarmer() PA_ADD_OPTION(NOTIFICATIONS); } -void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ - StartProgramChecks::check_performance_class_wired_or_wireless(context); - assert_16_9_720p_min(env.logger(), env.console); - - BBQSoloFarmer_Descriptor::Stats& stats = env.current_stats(); - - //Make sure console type is set - if (env.console.state().console_type() == ConsoleType::Unknown){ - throw UserSetupError(env.console, "Console Type (Switch 1 or 2) must be specified."); - } - - // Connect the controller. - require_player(env.console, context, BUTTON_LCLICK); - - //Fly to plaza - open_map_from_overworld(env.program_info(), env.console, context); - fly_to_overworld_from_map(env.program_info(), env.console, context); - +void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs uint64_t num_completed_quests = 0; - //Test a specific quest - /* - BBQuests test_quest = BBQuests::catch_water; - bool questTest = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, test_quest, eggs_hatched); - if (questTest){ - env.log("Finished quest."); - } - */ - - while (num_completed_quests < BBQ_OPTIONS.NUM_QUESTS){ - if (BBQ_OPTIONS.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ - env.log("Stop when out of eggs selected. Stopping program."); + while (num_completed_quests < options.NUM_QUESTS){ + if (options.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= options.NUM_EGGS){ + logger.log("Stop when out of eggs selected. Stopping program."); break; } //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - quest_list = read_quests(env.program_info(), env.console, context, BBQ_OPTIONS); - quests_to_do = process_quest_list(env.program_info(), env.console, context, BBQ_OPTIONS, quest_list, eggs_hatched); + quest_list = game.read_quests(); + quests_to_do = game.process_quest_list(quest_list, eggs_hatched); //Clear out the regular quest list. quest_list.clear(); @@ -123,23 +97,21 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo 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_quests(env.program_info(), env.console, context, BBQ_OPTIONS); + quest_list = game.read_quests(); if (std::find(quest_list.begin(), quest_list.end(), current_quest) != quest_list.end()){ - env.log("Current quest exists on list. Doing quest."); - bool questSuccess = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, current_quest, eggs_hatched); + logger.log("Current quest exists on list. Doing quest."); + bool questSuccess = game.do_quest(current_quest, eggs_hatched); if (questSuccess){ - env.log("Quest completed successfully."); - stats.questsCompleted++; - env.update_stats(); + logger.log("Quest completed successfully."); + game.quest_completed(); num_completed_quests++; }else{ - env.log("Quest did not complete successfully."); + logger.log("Quest did not complete successfully."); } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest - env.log("Current quest does not exist on list. Quest completed at some point."); - stats.questsCompleted++; - env.update_stats(); + logger.log("Current quest does not exist on list. Quest completed at some point."); + game.quest_completed(); num_completed_quests++; } quest_list.clear(); @@ -147,27 +119,103 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo //Clear out the todo list quests_to_do.clear(); - //Fix the time to prevent running out of years - pbf_wait(context, 2000ms); - context.wait_for_all_requests(); - go_home(env.console, context); - home_to_date_time(env.console, context, false); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - pbf_press_button(context, BUTTON_A, 160ms, 840ms); - pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); - resume_game_from_home(env.console, context); + uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; + game.end_round(temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0); + } +} + - uint64_t temp_save_num_option = BBQ_OPTIONS.SAVE_NUM_QUESTS; - if (temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0){ - env.log("Saving and resetting."); - save_game_from_overworld(env.program_info(), env.console, context); - reset_game(env.program_info(), env.console, context); - stats.saves++; +namespace{ + +class BBQSoloFarmerGameActions : public BBQGameActions{ +public: + BBQSoloFarmerGameActions( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BBQOption& options, BBQSoloFarmer_Descriptor::Stats& stats, + EventNotificationOption& status_update + ) + : m_env(env), m_context(context) + , m_options(options), m_stats(stats) + , m_status_update(status_update) + {} + + 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( + 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); + } + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ + return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); + } + virtual void quest_completed() override{ + m_stats.questsCompleted++; + m_env.update_stats(); + } + virtual void end_round(bool save) override{ + //Fix the time to prevent running out of years + pbf_wait(m_context, 2000ms); + m_context.wait_for_all_requests(); + go_home(m_env.console, m_context); + home_to_date_time(m_env.console, m_context, false); + pbf_press_button(m_context, BUTTON_A, 160ms, 840ms); + pbf_press_button(m_context, BUTTON_A, 160ms, 840ms); + pbf_press_button(m_context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); + resume_game_from_home(m_env.console, m_context); + + if (save){ + m_env.log("Saving and resetting."); + save_game_from_overworld(m_env.program_info(), m_env.console, m_context); + reset_game(m_env.program_info(), m_env.console, m_context); + m_stats.saves++; } - env.update_stats(); - send_program_status_notification(env, NOTIFICATION_STATUS_UPDATE); + m_env.update_stats(); + send_program_status_notification(m_env, m_status_update); + } + +private: + SingleSwitchProgramEnvironment& m_env; + ProControllerContext& m_context; + BBQOption& m_options; + BBQSoloFarmer_Descriptor::Stats& m_stats; + EventNotificationOption& m_status_update; +}; + +} + +void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + StartProgramChecks::check_performance_class_wired_or_wireless(context); + assert_16_9_720p_min(env.logger(), env.console); + + BBQSoloFarmer_Descriptor::Stats& stats = env.current_stats(); + + //Make sure console type is set + if (env.console.state().console_type() == ConsoleType::Unknown){ + throw UserSetupError(env.console, "Console Type (Switch 1 or 2) must be specified."); + } + + // Connect the controller. + require_player(env.console, context, BUTTON_LCLICK); + + //Fly to plaza + open_map_from_overworld(env.program_info(), env.console, context); + fly_to_overworld_from_map(env.program_info(), env.console, context); + + //Test a specific quest + /* + uint8_t eggs_hatched = 0; + BBQuests test_quest = BBQuests::catch_water; + bool questTest = process_and_do_quest(env, env.console, context, BBQ_OPTIONS, test_quest, eggs_hatched); + if (questTest){ + env.log("Finished quest."); } + */ + + BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE); + run_bbq_rounds(env.logger(), BBQ_OPTIONS, game); env.update_stats(); if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 9917606334..1ac8535caf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -7,6 +7,9 @@ #ifndef PokemonAutomation_PokemonSV_BBQSoloFarmer_H #define PokemonAutomation_PokemonSV_BBQSoloFarmer_H +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" #include "CommonFramework/Notifications/EventNotificationsTable.h" #include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" #include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" @@ -16,6 +19,45 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ + +// The game actions that run_bbq_rounds() depends on. They are injected so +// that the round loop (which quests to attempt, which ones count, when to +// stop, when to save) runs in tests without a Switch: BBQSoloFarmer performs +// them on the console, and tests replace them with scripted results. +class BBQGameActions{ +public: + virtual ~BBQGameActions() = default; + + // Open the quest panel and read the quests on it. + virtual std::vector read_quests() = 0; + + // 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( + const std::vector& quests, uint8_t eggs_hatched + ) = 0; + + // Attempt one quest that is on the panel, after process_quest_list() + // chose it. Returns true if it is no longer on the panel afterwards. + // Increments `eggs_hatched` when the quest hatches eggs. + virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; + + // Progress reporting only: a quest was counted as completed. Lets stats + // update after every quest instead of once per round. + virtual void quest_completed() = 0; + + // A round has ended. The loop decides whether a save is due, because + // that depends on how many quests it has counted; how to fix the date, + // save and reset, and report the round is up to the game side. + virtual void end_round(bool save) = 0; +}; + +// Run rounds of quests until NUM_QUESTS have been completed, or until +// eggs run out when OUT_OF_EGGS is Stop. +void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); + + class BBQSoloFarmer_Descriptor : public SingleSwitchProgramDescriptor{ public: BBQSoloFarmer_Descriptor(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 999661c313..147d8ad558 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -4,8 +4,10 @@ * */ +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQSoloFarmer.h" #include "PokemonSV_BBQTests.h" namespace PokemonAutomation{ @@ -66,7 +68,229 @@ static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ } +namespace{ + +class NullLogger : public Logger{ +public: + virtual void log(const std::string&, Color) override{} +}; + +// A singleplayer quest panel that behaves like the game's: 3 blue quests, +// and a completed quest is replaced by a new one. process_quest_list() +// follows the two rules of the real one that the round loop depends on: +// quests in not_possible_quests are skipped, and if nothing is doable all +// quests are rerolled and nothing is returned. Red quests and the +// exclusions table are not modeled. Records the game actions taken. +class SimulatedBBQGameActions : public BBQGameActions{ +public: + std::vector panel; + // Each attempt returns the next outcome; once exhausted, attempts succeed. + std::vector outcomes = {}; + // Completing the first quest also completes the second, e.g. walking + // 500 meters while navigating to a photo target. + std::vector> also_completes = {}; + + std::vector events; + + virtual std::vector read_quests() override{ + events.push_back("read"); + if (++m_reads > 1000){ + throw std::runtime_error("Too many quest panel reads."); + } + return panel; + } + virtual std::vector process_quest_list( + const std::vector& quests, uint8_t + ) override{ + events.push_back("process"); + std::vector doable; + for (BBQuests quest : quests){ + if (!not_possible_quests.contains(quest)){ + doable.push_back(quest); + } + } + if (doable.empty()){ + for (BBQuests& slot : panel){ + slot = next_new_quest(); + } + } + return doable; + } + virtual bool do_quest(BBQuests quest, uint8_t&) override{ + events.push_back("do"); + const bool success = m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true; + if (success){ + complete(quest); + for (const auto& [done, extra] : also_completes){ + if (done == quest){ + complete(extra); + } + } + } + return success; + } + virtual void quest_completed() override{ + events.push_back("completed"); + } + virtual void end_round(bool save) override{ + events.push_back(save ? "end(save)" : "end"); + } + +private: + void complete(BBQuests quest){ + auto iter = std::find(panel.begin(), panel.end(), quest); + if (iter != panel.end()){ + *iter = next_new_quest(); + } + } + + // New quests come from a fixed rotation of doable blue quests, skipping + // any that are already on the panel. + BBQuests next_new_quest(){ + static const BBQuests ROTATION[] = { + BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, + BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, + BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, + }; + while (true){ + BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)]; + if (std::find(panel.begin(), panel.end(), quest) == panel.end()){ + return quest; + } + } + } + + size_t m_reads = 0; + size_t m_next_outcome = 0; + size_t m_next_new_quest = 0; +}; + +std::string join_events(const std::vector& events){ + std::string ret; + for (const std::string& event : events){ + if (!ret.empty()) ret += ' '; + ret += event; + } + return ret; +} + +// One simulated farmer run: option values, the panel it starts with and how +// quests turn out, and the game actions it must take. +struct BBQRoundsScenario{ + std::string name; + uint64_t num_quests; + uint64_t save_num_quests = 0; + bool stop_when_out_of_eggs = false; + std::vector panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up}; + std::vector outcomes = {}; + std::vector> also_completes = {}; + uint64_t expected_completed = 0; + std::string expected; +}; + +class Test_BBQFarmerRounds : public UnitTest{ +public: + Test_BBQFarmerRounds(BBQRoundsScenario scenario) + : UnitTest("PokemonSV::BBQFarmerRounds - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + BBQOption options; + options.NUM_QUESTS.set(m_scenario.num_quests); + options.SAVE_NUM_QUESTS.set(m_scenario.save_num_quests); + if (m_scenario.stop_when_out_of_eggs){ + options.NUM_EGGS.set(0); + options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop); + } + + SimulatedBBQGameActions game; + game.panel = m_scenario.panel; + game.outcomes = m_scenario.outcomes; + game.also_completes = m_scenario.also_completes; + + NullLogger logger; + run_bbq_rounds(logger, options, game); + const uint64_t completed = (uint64_t)std::count(game.events.begin(), game.events.end(), "completed"); + if (completed != m_scenario.expected_completed){ + return "Expected " + std::to_string(m_scenario.expected_completed) + + " completed quests, counted " + std::to_string(completed); + } + const std::string actual = join_events(game.events); + if (actual != m_scenario.expected){ + return "Expected: " + m_scenario.expected + "\nActual: " + actual; + } + return true; + } + +private: + const BBQRoundsScenario m_scenario; +}; + +static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ + const std::string round_of_3 = "read process read do completed read do completed read do completed"; + const BBQRoundsScenario scenarios[] = { + { + // 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 + // complete. + .name = "finishes the round that reaches the quest goal", + .num_quests = 4, + .expected_completed = 6, + .expected = round_of_3 + " end " + round_of_3 + " end", + }, + { + // Doing photo_swim also completes travel_500, so travel_500 is + // gone when the loop reads the panel before attempting it. + .name = "quest already gone counts as completed", + .num_quests = 3, + .panel = {BBQuests::photo_swim, BBQuests::travel_500, BBQuests::make_tm}, + .also_completes = {{BBQuests::photo_swim, BBQuests::travel_500}}, + .expected_completed = 3, + .expected = "read process read do completed read completed read do completed end", + }, + { + // The first attempt fails: 3 attempts, 2 completions. + .name = "failed quest does not count", + .num_quests = 2, + .outcomes = {false}, + .expected_completed = 2, + .expected = "read process read do read do completed read do completed end", + }, + { + // Nothing on the panel is doable, so process_quest_list() rerolls + // all of it and returns nothing; the loop reads the panel again. + .name = "panel with nothing doable is rerolled", + .num_quests = 3, + .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, + .expected_completed = 3, + .expected = "read process " + round_of_3 + " end", + }, + { + .name = "saves at exact multiples", + .num_quests = 6, + .save_num_quests = 3, + .expected_completed = 6, + .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", + }, + { + .name = "stops when out of eggs", + .num_quests = 5, + .stop_when_out_of_eggs = true, + .expected_completed = 0, + .expected = "", + }, + }; + for (const BBQRoundsScenario& scenario : scenarios){ + database.add(scenario); + } +} + +} + + void add_tests_BBQ(UnitTestDatabase& 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 8bec917a63..4401172ccf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -133,8 +133,8 @@ std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, - std::vector& quest_list, - uint8_t& eggs_hatched + const std::vector& quest_list, + uint8_t eggs_hatched ){ std::vector quests_to_do; bool rerolled = false; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 21ad16aa71..8e4bac1bf2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -63,8 +63,8 @@ std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, - std::vector& quest_list, - uint8_t& eggs_hatched + const std::vector& quest_list, + uint8_t eggs_hatched ); //Take the current quest and calls the function to do it, then checks the quest was successful. Returns true if so. From c976129a2f8b3d478505f24cc4d042b1eeca38e7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 22:39:54 -0700 Subject: [PATCH 23/50] Separate the BBQ quest plan from the controller actions that carry it out process_quest_list() decided what to do with each quest and pressed the buttons for it in the same loop, so the decisions could only be checked on a Switch. plan_quests() now makes the decisions from the options alone: which quests to do, which panel slots to reroll, and whether to reroll all. process_quest_list() carries out the plan, and opening the panel is a shared helper instead of three copies. All rerolls now move the cursor to the quest's slot. The out-of-eggs reroll used to reroll the top slot; the egg quest is a red quest and red quests are listed at the top, so this is the same in practice. OOEggs::Stop reaching the planner is now an InternalProgramError, since the farmer stops before that can happen. Unit tests cover the plan for exclusions, duplicate rows, quests that are not possible, and eggs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 145 ++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 253 ++++++++---------- .../Farming/PokemonSV_BlueberryQuests.h | 17 ++ 3 files changed, 273 insertions(+), 142 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 147d8ad558..c82fc7101c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -289,7 +289,152 @@ 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){ + 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 4401172ccf..41fa293c7d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -129,162 +129,131 @@ std::vector read_quests( return quest_list; } -std::vector process_quest_list( - const ProgramInfo& info, - VideoStream& stream, ProControllerContext& 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."); + 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()."); } + continue; + } + bool quest_in_table = false; + for (const std::unique_ptr& row : exclusions_table){ + if (n != 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(n); + 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; + } + } + if (!quest_in_table){ + 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){ + plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + return plan; +} + +namespace{ + +//Open the quest panel from the overworld, with the cursor on the top quest. +void 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(); +} + +} + +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 (size_t slot : plan.reroll_slots){ + 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); + } + + if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); //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(); + open_quest_panel(stream, context); for (string::size_type i = 0; i < quest_list.size(); i++){ pbf_press_button(context, BUTTON_A, 160ms, 400ms); @@ -299,7 +268,7 @@ std::vector process_quest_list( press_Bs_to_back_to_overworld(info, stream, context); } /* - if (!rerolled && quests_to_do.size() == 0){ + if (plan.reroll_all){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, "No possible quests! Check language selection.", @@ -307,7 +276,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( From ab57d3ea1eb867d62ffccde9edc9e3af5b86c4e4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 10:59:00 -0700 Subject: [PATCH 24/50] Break the BBQ quest planner and its controller steps into helpers Move the out-of-eggs choice, the exclusions table rows, a single-slot reroll and the reroll of every slot into file-local helpers, so plan_quests() and process_quest_list() read as one level of steps. Explain why the language-selection error is commented out. open_quest_panel() sits next to read_quests(), which opens the same panel, and returns whether the panel opened, so later changes can use it without moving it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 6 +- .../Farming/PokemonSV_BlueberryQuests.cpp | 222 ++++++++++-------- 2 files changed, 133 insertions(+), 95 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index c82fc7101c..68798b9a7a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -297,12 +297,12 @@ namespace{ struct BBQuestPlanScenario{ std::string name; std::vector quests; - std::vector> extra_exclusions; + 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; + std::vector expected_to_do = {}; + std::vector expected_reroll_slots = {}; bool expected_reroll_all = false; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 41fa293c7d..5e09272ea2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -80,6 +80,31 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& return OCR::read_number(stream.logger(), BP_value); } +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, @@ -129,6 +154,102 @@ std::vector read_quests( return 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, @@ -148,43 +269,10 @@ BBQuestPlan plan_quests( //Check eggs remaining. if (n == BBQuests::hatch_egg && eggs_hatched >= BBQ_OPTIONS.NUM_EGGS){ logger.log("Out of eggs! Quest not possible."); - 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()."); - } + plan_out_of_eggs(logger, BBQ_OPTIONS, slot, plan); continue; } - bool quest_in_table = false; - for (const std::unique_ptr& row : exclusions_table){ - if (n != 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(n); - 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; - } - } - if (!quest_in_table){ + 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); } @@ -196,30 +284,6 @@ BBQuestPlan plan_quests( return plan; } -namespace{ - -//Open the quest panel from the overworld, with the cursor on the top quest. -void 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(); -} - -} - std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -230,43 +294,17 @@ std::vector process_quest_list( BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); for (size_t slot : plan.reroll_slots){ - 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); + reroll_slot(info, stream, context, slot); } - if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); - - //Open quest panel and reroll all quests. - //This does not handle out of BP. - open_quest_panel(stream, context); - - 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); + 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 (plan.reroll_all){ OperationFailedExceptionWithScreenshot::fire( From 037549e0079f345fa6a8497b826c94c41b46dbf7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 12:09:24 -0700 Subject: [PATCH 25/50] Pin the ways the BBQ farmer's round loop gets stuck today The round loop has several ways to stop making progress or lose time, and the PRs that fix them each change its behavior. Without a test of the current behavior, a reviewer has to infer from the code what each fix changes. Add BBQFarmerRounds scenarios that state what the loop does today: a failed quest is attempted again every round; a panel whose only doable quest keeps failing is retried forever; rerolls that change nothing (out of BP) repeat forever; a round that skips past a save milestone does not save; and a round with no completions saves because 0 is a multiple of the interval. Each fix then shows up as a change to one of these expectations. The simulated panel gains quests that never complete, an out-of-BP mode where rerolls do nothing, and a read limit per scenario; a run that exceeds it is recorded as "stuck". Test-only; no behavior change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 88 ++++++++++++++++++- 1 file changed, 84 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 68798b9a7a..716bc8a158 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -5,6 +5,7 @@ */ #include +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQSoloFarmer.h" @@ -81,6 +82,9 @@ class NullLogger : public Logger{ // quests in not_possible_quests are skipped, and if nothing is doable all // quests are rerolled and nothing is returned. Red quests and the // exclusions table are not modeled. Records the game actions taken. +// With out_of_bp, rerolls do nothing, as in the game when BP runs out. +// A run that reads the panel more than max_reads times is stuck: the read +// throws, and the test records "stuck". class SimulatedBBQGameActions : public BBQGameActions{ public: std::vector panel; @@ -89,12 +93,16 @@ class SimulatedBBQGameActions : public BBQGameActions{ // Completing the first quest also completes the second, e.g. walking // 500 meters while navigating to a photo target. std::vector> also_completes = {}; + // Attempts at these always fail, e.g. a target that never spawns. + std::set never_completes = {}; + bool out_of_bp = false; + size_t max_reads = 1000; std::vector events; virtual std::vector read_quests() override{ events.push_back("read"); - if (++m_reads > 1000){ + if (++m_reads > max_reads){ throw std::runtime_error("Too many quest panel reads."); } return panel; @@ -109,7 +117,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ doable.push_back(quest); } } - if (doable.empty()){ + if (doable.empty() && !out_of_bp){ for (BBQuests& slot : panel){ slot = next_new_quest(); } @@ -118,7 +126,8 @@ class SimulatedBBQGameActions : public BBQGameActions{ } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); - const bool success = m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true; + const bool success = !never_completes.contains(quest) + && (m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true); if (success){ complete(quest); for (const auto& [done, extra] : also_completes){ @@ -184,6 +193,9 @@ struct BBQRoundsScenario{ std::vector panel = {BBQuests::make_tm, BBQuests::auto_10, BBQuests::sneak_up}; std::vector outcomes = {}; std::vector> also_completes = {}; + std::set never_completes = {}; + bool out_of_bp = false; + size_t max_reads = 1000; uint64_t expected_completed = 0; std::string expected; }; @@ -208,9 +220,16 @@ class Test_BBQFarmerRounds : public UnitTest{ game.panel = m_scenario.panel; game.outcomes = m_scenario.outcomes; game.also_completes = m_scenario.also_completes; + game.never_completes = m_scenario.never_completes; + game.out_of_bp = m_scenario.out_of_bp; + game.max_reads = m_scenario.max_reads; NullLogger logger; - run_bbq_rounds(logger, options, game); + try{ + run_bbq_rounds(logger, options, game); + }catch (std::runtime_error&){ + game.events.push_back("stuck"); + } const uint64_t completed = (uint64_t)std::count(game.events.begin(), game.events.end(), "completed"); if (completed != m_scenario.expected_completed){ return "Expected " + std::to_string(m_scenario.expected_completed) @@ -280,6 +299,67 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected_completed = 0, .expected = "", }, + + // The ways the loop gets stuck or loses time. Scenarios named + // "today:" state the current behavior, so that each fix shows up as a + // change to one of them. + { + // Today: sneak-up never completes and is attempted again in every + // round it stays on the panel. + .name = "today: a failed quest is attempted every round", + .num_quests = 4, + .never_completes = {BBQuests::sneak_up}, + .expected_completed = 4, + .expected = + "read process read do completed read do completed read do end " + "read process read do completed read do completed read do end", + }, + { + // Today: the only doable quest never completes, so the farmer + // attempts it every round and never finishes. + .name = "today: a panel of failed quests is retried forever", + .num_quests = 3, + .panel = {BBQuests::sneak_up, BBQuests::pickup_10, BBQuests::catch_flying}, + .never_completes = {BBQuests::sneak_up}, + .max_reads = 6, + .expected_completed = 0, + .expected = + "read process read do end " + "read process read do end " + "read process read do end read stuck", + }, + { + // Today: rerolls change nothing (out of BP), so the farmer reads + // and rerolls the panel forever. + .name = "today: rerolls that do not help repeat forever", + .num_quests = 3, + .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, + .out_of_bp = true, + .max_reads = 4, + .expected_completed = 0, + .expected = "read process read process read process read process read stuck", + }, + { + // Today: the save check only looks for exact multiples, so going + // from 0 to 3 skips the milestone at 2 without saving. + .name = "today: a round that skips past a milestone does not save", + .num_quests = 6, + .save_num_quests = 2, + .expected_completed = 6, + .expected = round_of_3 + " end " + round_of_3 + " end(save)", + }, + { + // Today: round 1 completes nothing, but 0 is a multiple of 2, so + // it saves anyway. + .name = "today: a round without completions saves", + .num_quests = 3, + .save_num_quests = 2, + .outcomes = {false, false, false}, + .expected_completed = 3, + .expected = + "read process read do read do read do end(save) " + + round_of_3 + " end", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); From e6afd5ea9fa91ace3bd3c5ab60112e585938b774 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:05:54 -0700 Subject: [PATCH 26/50] Check which quest a BBQ reroll pays for, and that it was replaced reroll_slot() opened the quest panel, pressed DOWN once per slot from where it assumed the cursor was, and pressed A twice. The panel does not always open with the cursor on the top quest: on a Switch it opened where the cursor was left, so the reroll paid for a different quest than planned. Nothing checked the menu opened or the quest changed, so running out of BP went unnoticed and the same quest was rerolled every round. reroll_quest() now does a reroll through injectable BBQRerollActions: open the reroll menu wherever the cursor is, see which quest the menu opened next to from its arrow, move to the planned quest, and only then confirm. Afterwards it reads the panel (up to 3 times, as OCR can miss a read) to check the quest was replaced. Panel or menu not found stops before spending BP; a quest not replaced stops without an error report, as it usually means out of BP. The arrow boxes for the top three slots were checked on a Switch; the fourth uses the same spacing. Reroll-all is unchanged. reroll_slot() becomes reroll_quest_slot(), declared next to reroll_quest(): it runs reroll_quest() on the console and stops the program if the reroll fails. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 223 +++++++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 165 +++++++++++-- .../Farming/PokemonSV_BlueberryQuests.h | 57 +++++ 3 files changed, 421 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 716bc8a158..03433af297 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -377,12 +377,12 @@ namespace{ struct BBQuestPlanScenario{ std::string name; std::vector quests; - std::vector> extra_exclusions = {}; + 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 = {}; + std::vector expected_to_do; + std::vector expected_reroll_slots; bool expected_reroll_all = false; }; @@ -439,6 +439,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){ @@ -512,8 +634,103 @@ 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){ + 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 5e09272ea2..091a6374f4 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" @@ -204,28 +205,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( @@ -250,6 +290,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, @@ -293,8 +414,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( From 6e412fc1c3ac11d06223ca1c38ed0df41d1521ee Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 10:51:21 -0700 Subject: [PATCH 27/50] Do not retry a failed BBQ quest every round while it stays on the panel process_and_do_quest() gives up on a quest after NUM_RETRIES retries, but the quest stays on the panel, so the next round attempts it again with a fresh set of retries. A quest that cannot be done, such as a sneak-up whose target does not spawn, is retried forever. run_bbq_rounds() now remembers quests that failed all their attempts and leaves them out of later rounds. A quest is forgotten once a read of 4 known quests shows it gone, so it gets its attempts again if it comes back. If only failed quests are doable, the farmer stops instead of spending BP to reroll them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 45 ++++++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 3 +- .../Programs/Farming/PokemonSV_BBQTests.cpp | 75 +++++++++++++------ 3 files changed, 96 insertions(+), 27 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index e71f27ac80..891221b0d3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/ProgramStats/StatsTracking.h" @@ -74,12 +75,40 @@ BBQSoloFarmer::BBQSoloFarmer() PA_ADD_OPTION(NOTIFICATIONS); } +namespace{ + +// Forget the failed quests that are gone from the panel, so a quest that +// comes back later gets its attempts again. The first 3 slots are always +// read, as UnableToDetect when OCR fails, but a 4th slot is only there when +// OCR reads it. So only a read of 4 known quests proves a quest is gone; a +// missed read must not give a failing quest new attempts. +void forget_quests_gone_from_panel(std::set& failed_quests, const std::vector& panel){ + if (panel.size() < 4 || std::find(panel.begin(), panel.end(), BBQuests::UnableToDetect) != panel.end()){ + return; + } + std::erase_if(failed_quests, [&](BBQuests quest){ + return std::find(panel.begin(), panel.end(), quest) == panel.end(); + }); +} + +} + void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs uint64_t num_completed_quests = 0; + // Quests that failed every attempt do_quest() made. A failed quest stays + // 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; + auto read_panel = [&](){ + std::vector panel = game.read_quests(); + forget_quests_gone_from_panel(failed_quests, panel); + return panel; + }; + while (num_completed_quests < options.NUM_QUESTS){ if (options.OUT_OF_EGGS == BBQOption::OOEggs::Stop && eggs_hatched >= options.NUM_EGGS){ logger.log("Stop when out of eggs selected. Stopping program."); @@ -88,16 +117,27 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //Get and reroll quests until we can at least one while (quests_to_do.size() < 1){ - quest_list = game.read_quests(); + quest_list = read_panel(); quests_to_do = game.process_quest_list(quest_list, eggs_hatched); + // Rerolling costs BP and the failed quests may be doable another + // time, so stop instead of spending BP to replace them. + const bool found_doable = !quests_to_do.empty(); + std::erase_if(quests_to_do, [&](BBQuests quest){ + return failed_quests.contains(quest); + }); + if (found_doable && quests_to_do.empty()){ + logger.log("Every doable quest on the panel failed all its attempts. Stopping program.", COLOR_RED); + return; + } + //Clear out the regular quest list. quest_list.clear(); } 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 = game.read_quests(); + 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); @@ -107,6 +147,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; }else{ logger.log("Quest did not complete successfully."); + failed_quests.insert(current_quest); } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 1ac8535caf..1afa74e6dc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -54,7 +54,8 @@ class BBQGameActions{ }; // Run rounds of quests until NUM_QUESTS have been completed, or until -// eggs run out when OUT_OF_EGGS is Stop. +// eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the +// panel is one that already failed all its attempts. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 03433af297..9adeb555f5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -97,6 +97,13 @@ class SimulatedBBQGameActions : public BBQGameActions{ std::set never_completes = {}; bool out_of_bp = false; size_t max_reads = 1000; + // New quests are drawn from this list in turn, skipping any that are + // already on the panel. + std::vector new_quests = { + BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, + BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, + BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, + }; std::vector events; @@ -153,16 +160,9 @@ class SimulatedBBQGameActions : public BBQGameActions{ } } - // New quests come from a fixed rotation of doable blue quests, skipping - // any that are already on the panel. BBQuests next_new_quest(){ - static const BBQuests ROTATION[] = { - BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar, - BBQuests::photo_savanna, BBQuests::catch_any, BBQuests::catch_normal, - BBQuests::catch_fire, BBQuests::catch_water, BBQuests::catch_grass, - }; while (true){ - BBQuests quest = ROTATION[m_next_new_quest++ % std::size(ROTATION)]; + BBQuests quest = new_quests[m_next_new_quest++ % new_quests.size()]; if (std::find(panel.begin(), panel.end(), quest) == panel.end()){ return quest; } @@ -196,6 +196,7 @@ struct BBQRoundsScenario{ std::set never_completes = {}; bool out_of_bp = false; size_t max_reads = 1000; + std::vector new_quests = {}; uint64_t expected_completed = 0; std::string expected; }; @@ -223,6 +224,9 @@ class Test_BBQFarmerRounds : public UnitTest{ game.never_completes = m_scenario.never_completes; game.out_of_bp = m_scenario.out_of_bp; game.max_reads = m_scenario.max_reads; + if (!m_scenario.new_quests.empty()){ + game.new_quests = m_scenario.new_quests; + } NullLogger logger; try{ @@ -304,29 +308,23 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ // "today:" state the current behavior, so that each fix shows up as a // change to one of them. { - // Today: sneak-up never completes and is attempted again in every - // round it stays on the panel. - .name = "today: a failed quest is attempted every round", + // sneak-up fails in round 1 and is still on the panel in round 2, + // where only the two new quests are attempted. + .name = "failed quest is not attempted again", .num_quests = 4, .never_completes = {BBQuests::sneak_up}, .expected_completed = 4, .expected = "read process read do completed read do completed read do end " - "read process read do completed read do completed read do end", + "read process read do completed read do completed end", }, { - // Today: the only doable quest never completes, so the farmer - // attempts it every round and never finishes. - .name = "today: a panel of failed quests is retried forever", + .name = "stops when only failed quests are doable", .num_quests = 3, .panel = {BBQuests::sneak_up, BBQuests::pickup_10, BBQuests::catch_flying}, .never_completes = {BBQuests::sneak_up}, - .max_reads = 6, .expected_completed = 0, - .expected = - "read process read do end " - "read process read do end " - "read process read do end read stuck", + .expected = "read process read do end read process", }, { // Today: rerolls change nothing (out of BP), so the farmer reads @@ -350,15 +348,44 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ }, { // Today: round 1 completes nothing, but 0 is a multiple of 2, so - // it saves anyway. + // it saves anyway. Every quest failed, so the farmer then stops. .name = "today: a round without completions saves", .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, - .expected_completed = 3, + .expected_completed = 0, + .expected = "read process read do read do read do end(save) read process", + }, + { + // sneak-up fails, then leaves the panel along with make-tm. Round 2 + // reads a full panel without it, and its replacement for + // photo-canyon brings it back. Round 3 attempts it again. + .name = "failed quest that leaves a full panel is attempted again", + .num_quests = 5, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10, BBQuests::catch_flying}, + .outcomes = {false}, + .also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}}, + .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, + .expected_completed = 5, + .expected = + "read process read do read do completed end " + "read process read do completed read do completed end " + "read process read do completed read do completed end", + }, + { + // As above with 3 slots. A 4th quest could have been missed by + // OCR, so the read does not prove sneak-up left: it stays failed. + .name = "failed quest missing from a 3-slot panel stays failed", + .num_quests = 4, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::pickup_10}, + .outcomes = {false}, + .also_completes = {{BBQuests::make_tm, BBQuests::sneak_up}}, + .new_quests = {BBQuests::auto_10, BBQuests::photo_canyon, BBQuests::sneak_up}, + .expected_completed = 4, .expected = - "read process read do read do read do end(save) " - + round_of_3 + " end", + "read process read do read do completed end " + "read process read do completed read do completed end " + "read process read do completed end", }, }; for (const BBQRoundsScenario& scenario : scenarios){ From e3ef02c3d1aa3e97da479fdfa608fafdcc59391d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 22:33:37 -0700 Subject: [PATCH 28/50] Stop the BBQ Farmer instead of rerolling the quest panel forever When nothing on the panel is doable the farmer rerolls it and reads it again. If nothing can ever become doable, the loop never ends. Check at start that the exclusions table leaves at least one doable blue quest, and give up with a UserSetupError after 30 reads in a row with nothing doable (wrong game language, or no BP left to reroll). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 44 ++++++++++++++++ .../Farming/PokemonSV_BBQSoloFarmer.h | 2 + .../Programs/Farming/PokemonSV_BBQTests.cpp | 52 +++++++++++++++++-- 3 files changed, 93 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 891221b0d3..3a4fd0dce2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -91,9 +91,45 @@ void forget_quests_gone_from_panel(std::set& failed_quests, const std: }); } +// When nothing on the panel is doable, the farmer rerolls it and reads it +// again. If no blue quest can ever be doable, that loop never ends, so the +// settings are checked before starting. +// A quest is done if any exclusion row says run, matching process_quest_list(). +bool exclusions_leave_a_blue_quest(const BBQOption& options){ + const std::vector> exclusions = options.QUEST_EXCLUSIONS.copy_snapshot(); + for (BBQuests quest : blue_quests){ + if (not_possible_quests.contains(quest)){ + continue; + } + bool in_table = false; + bool run = false; + for (const std::unique_ptr& row : exclusions){ + if (row->quest == quest){ + in_table = true; + run = run || row->action == BBQAction::run; + } + } + if (!in_table || run){ + return true; + } + } + return false; +} + +// Valid settings can still produce a panel with nothing doable, which the +// next reroll fixes. If only 3 of the 31 blue quests are doable, a panel has +// nothing doable 73% of the time, and 30 such panels in a row happen with +// probability 0.008%. More than that means rerolling does not help: the game +// language is wrong so no quest is read, or there is no BP left to reroll. +const size_t MAX_READS_WITHOUT_DOABLE_QUEST = 30; + } void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ + if (!exclusions_leave_a_blue_quest(options)){ + throw UserSetupError(logger, "The quest exclusions table skips or rerolls every blue quest the program can do."); + } + std::vector quest_list; //all quests std::vector quests_to_do; //do-able quests uint8_t eggs_hatched = 0; //Track eggs @@ -116,7 +152,15 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga } //Get and reroll quests until we can at least one + size_t reads_without_doable_quest = 0; while (quests_to_do.size() < 1){ + if (reads_without_doable_quest++ == MAX_READS_WITHOUT_DOABLE_QUEST){ + throw UserSetupError( + logger, + "No doable quest after " + std::to_string(MAX_READS_WITHOUT_DOABLE_QUEST) + + " 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); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 1afa74e6dc..40daefa8bf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -56,6 +56,8 @@ class BBQGameActions{ // Run rounds of quests until NUM_QUESTS have been completed, or until // eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the // panel is one that already failed all its attempts. +// Throws UserSetupError if the exclusions leave no blue quest to do, or if +// the panel shows nothing doable for many reads in a row. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 9adeb555f5..62d531a54c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -6,6 +6,7 @@ #include #include +#include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQSoloFarmer.h" @@ -197,6 +198,7 @@ struct BBQRoundsScenario{ bool out_of_bp = false; size_t max_reads = 1000; std::vector new_quests = {}; + std::vector> exclusions = {}; uint64_t expected_completed = 0; std::string expected; }; @@ -216,6 +218,12 @@ class Test_BBQFarmerRounds : public UnitTest{ options.NUM_EGGS.set(0); options.OUT_OF_EGGS.set(BBQOption::OOEggs::Stop); } + for (const auto& [quest, action] : m_scenario.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)); + } SimulatedBBQGameActions game; game.panel = m_scenario.panel; @@ -231,6 +239,8 @@ class Test_BBQFarmerRounds : public UnitTest{ NullLogger logger; try{ run_bbq_rounds(logger, options, game); + }catch (UserSetupError&){ + game.events.push_back("error"); }catch (std::runtime_error&){ game.events.push_back("stuck"); } @@ -250,8 +260,25 @@ class Test_BBQFarmerRounds : public UnitTest{ const BBQRoundsScenario m_scenario; }; +// Every blue quest the program can do, each with the given action. +std::vector> every_doable_blue_quest(BBQAction action){ + std::vector> ret; + for (BBQuests quest : blue_quests){ + if (!not_possible_quests.contains(quest)){ + ret.emplace_back(quest, action); + } + } + return ret; +} + static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ const std::string round_of_3 = "read process read do completed read do completed read do completed"; + std::string read_30_times; + for (int c = 0; c < 30; c++){ + read_30_times += "read process "; + } + 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[] = { { // The goal (4) is reached in the middle of round 2. The loop only @@ -327,15 +354,13 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = "read process read do end read process", }, { - // Today: rerolls change nothing (out of BP), so the farmer reads - // and rerolls the panel forever. - .name = "today: rerolls that do not help repeat forever", + // Rerolls change nothing, so the panel never becomes doable. + .name = "gives up when rerolling does not help", .num_quests = 3, .panel = {BBQuests::pickup_10, BBQuests::catch_flying, BBQuests::UnableToDetect}, .out_of_bp = true, - .max_reads = 4, .expected_completed = 0, - .expected = "read process read process read process read process read stuck", + .expected = read_30_times + "error", }, { // Today: the save check only looks for exact multiples, so going @@ -387,6 +412,23 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ "read process read do completed read do completed end " "read process read do completed end", }, + { + // Rerolling could never produce a doable panel, so the run + // fails before reading the panel. + .name = "fails at start when every blue quest is excluded", + .num_quests = 3, + .exclusions = every_doable_blue_quest(BBQAction::reroll), + .expected_completed = 0, + .expected = "error", + }, + { + // A run row keeps the quest doable even when another row skips it. + .name = "a quest with a run row is not excluded", + .num_quests = 3, + .exclusions = all_skipped_but_make_tm, + .expected_completed = 3, + .expected = round_of_3 + " end", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); From 30063b30d454009af601c54d3bce9371af25ef4c Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 21:42:30 -0700 Subject: [PATCH 29/50] Fix BBQ save/reset milestones across stalled and batched rounds The farmer saved and reset when the completion count was an exact multiple of SAVE_NUM_QUESTS. A round that completed nothing while the count sat at 0 or 50 saved again, and a round going from 49 to 52 skipped the 50 milestone entirely. Compare the count before and after each round and ask end_round() to save once when it crosses a multiple of the interval. Stalled rounds and an interval of 0 never save. Fix the option label to say "completing", not "attempting". Add two PokemonSV::BBQFarmerRounds scenarios that fail with the old rule: a round that skips past a milestone, and rounds with no completions. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 2 +- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 11 ++++++++- .../Programs/Farming/PokemonSV_BBQTests.cpp | 23 +++++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 57986e80d2..da09d46a59 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -149,7 +149,7 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) 1000 ) , SAVE_NUM_QUESTS( - "Save and reset the game after attempting this many quests:
This preserves progress and prevents potential game lags from long runs.
0 disables this option.", + "Save and reset the game after completing this many quests:
This preserves progress and prevents potential game lags from long runs.
0 disables this option.", LockMode::UNLOCK_WHILE_RUNNING, 50 ) , INVERTED_FLIGHT( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 3a4fd0dce2..7cd2c4c15e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -123,6 +123,13 @@ bool exclusions_leave_a_blue_quest(const BBQOption& options){ // language is wrong so no quest is read, or there is no BP left to reroll. const size_t MAX_READS_WITHOUT_DOABLE_QUEST = 30; +// Whether a round that took the completed count from previous_completed +// to completed crossed a nonzero multiple of the save interval. +bool bbq_save_due(uint64_t previous_completed, uint64_t completed, uint64_t interval){ + return interval != 0 && completed > previous_completed + && completed / interval > previous_completed / interval; +} + } void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game){ @@ -179,6 +186,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga quest_list.clear(); } + 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(); @@ -205,7 +213,8 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga quests_to_do.clear(); uint64_t temp_save_num_option = options.SAVE_NUM_QUESTS; - game.end_round(temp_save_num_option != 0 && num_completed_quests % temp_save_num_option == 0); + // A round can cross a milestone without ending at an exact multiple. + game.end_round(bbq_save_due(completed_before_round, num_completed_quests, temp_save_num_option)); } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 62d531a54c..c35ca22e4f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -317,7 +317,7 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = "read process " + round_of_3 + " end", }, { - .name = "saves at exact multiples", + .name = "saves at each milestone", .num_quests = 6, .save_num_quests = 3, .expected_completed = 6, @@ -331,9 +331,7 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = "", }, - // The ways the loop gets stuck or loses time. Scenarios named - // "today:" state the current behavior, so that each fix shows up as a - // change to one of them. + // The ways the loop used to get stuck or lose time. { // sneak-up fails in round 1 and is still on the panel in round 2, // where only the two new quests are attempted. @@ -363,23 +361,24 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = read_30_times + "error", }, { - // Today: the save check only looks for exact multiples, so going - // from 0 to 3 skips the milestone at 2 without saving. - .name = "today: a round that skips past a milestone does not save", + // 0 -> 3 crosses the milestone at 2 without landing on it. + .name = "saves when a round skips past a milestone", .num_quests = 6, .save_num_quests = 2, .expected_completed = 6, - .expected = round_of_3 + " end " + round_of_3 + " end(save)", + .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", }, { - // Today: round 1 completes nothing, but 0 is a multiple of 2, so - // it saves anyway. Every quest failed, so the farmer then stops. - .name = "today: a round without completions saves", + // Round 1 completes nothing. It must not save, even though the + // count (0) is a multiple of the interval. + // Every quest fails, so nothing doable is left and the farmer + // stops, but the round with no completions doesn't save. + .name = "round without completions does not save", .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, .expected_completed = 0, - .expected = "read process read do read do read do end(save) read process", + .expected = "read process read do read do read do end read process", }, { // sneak-up fails, then leaves the panel along with make-tm. Round 2 From 01e50592748981c2ec9303837b2a2d49ec7287d9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:10:16 -0700 Subject: [PATCH 30/50] Let the BBQ farmer reroll or stop on a quest that fails all its retries A quest that fails all its retries is skipped while it stays on the panel. That keeps the farmer going, but the quest blocks its slot, and if every doable quest is one that failed the farmer stops. OUT_OF_RETRIES now chooses what happens: - Skip (default): as before. - Reroll: reroll the failed quest, so its slot gets a new quest. Uses reroll_quest_slot(), which checks which quest it pays for and that it was replaced. - Stop: end the program there, through the normal end of program. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 11 ++++++ .../PokemonSV/Options/PokemonSV_BBQOption.h | 7 ++++ .../Farming/PokemonSV_BBQSoloFarmer.cpp | 22 +++++++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 7 +++- .../Programs/Farming/PokemonSV_BBQTests.cpp | 36 ++++++++++++++++--- 5 files changed, 77 insertions(+), 6 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index da09d46a59..008307024b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -190,6 +190,16 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) "Number of Retries:
Reattempt a quest this many times if it fails.", LockMode::UNLOCK_WHILE_RUNNING, 1, 0, 5 ) + , OUT_OF_RETRIES( + "Out of Retries:
When a quest fails all its retries, do the selected option.", + { + {OutOfRetries::Skip, "skip", "Skip Quest - Not attempted again while it stays on the panel"}, + {OutOfRetries::Reroll, "reroll", "Reroll Quest - Costs BP"}, + {OutOfRetries::Stop, "stop", "Stop Program"}, + }, + LockMode::UNLOCK_WHILE_RUNNING, + OutOfRetries::Skip + ) , FIX_TIME_WHEN_DONE( "Fix Time when Done:
Fix the time after the program finishes.", LockMode::UNLOCK_WHILE_RUNNING, false @@ -210,6 +220,7 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) PA_ADD_OPTION(BATTLE_AI); PA_ADD_OPTION(CATCH_ON_WIN); PA_ADD_OPTION(NUM_RETRIES); + PA_ADD_OPTION(OUT_OF_RETRIES); PA_ADD_OPTION(FIX_TIME_WHEN_DONE); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h index f5715a10c7..0f45903656 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h @@ -74,6 +74,12 @@ class BBQOption : public GroupOption{ KeepGoing, }; + enum class OutOfRetries{ + Skip, + Reroll, + Stop, + }; + OCR::LanguageOCROption& LANGUAGE; SimpleIntegerOption NUM_QUESTS; @@ -98,6 +104,7 @@ class BBQOption : public GroupOption{ TeraFarmerCatchOnWin CATCH_ON_WIN; SimpleIntegerOption NUM_RETRIES; + EnumDropdownOption OUT_OF_RETRIES; BooleanCheckBoxOption FIX_TIME_WHEN_DONE; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 7cd2c4c15e..84fd26495c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -199,7 +199,18 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; }else{ logger.log("Quest did not complete successfully."); - failed_quests.insert(current_quest); + switch (options.OUT_OF_RETRIES){ + case BBQOption::OutOfRetries::Skip: + failed_quests.insert(current_quest); + break; + case BBQOption::OutOfRetries::Reroll: + logger.log("Reroll selected. Rerolling the failed quest."); + game.reroll_quest(current_quest); + break; + case BBQOption::OutOfRetries::Stop: + logger.log("Stop selected. Stopping program.", COLOR_RED); + return; + } } }else{ //Note: This doesn't account for case such as "sneak up" being added and then completed alongside the next quest @@ -244,6 +255,15 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); } + virtual void reroll_quest(BBQuests quest) override{ + std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); + auto iter = std::find(quests.begin(), quests.end(), quest); + if (iter == quests.end()){ + m_env.log("Failed quest is no longer on the panel. Not rerolling."); + return; + } + reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); + } virtual void quest_completed() override{ m_stats.questsCompleted++; m_env.update_stats(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h index 40daefa8bf..86c7865151 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h @@ -43,6 +43,10 @@ class BBQGameActions{ // Increments `eggs_hatched` when the quest hatches eggs. virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) = 0; + // Reroll a quest that failed all its attempts, if it is still on the + // panel. Spends BP. + virtual void reroll_quest(BBQuests quest) = 0; + // Progress reporting only: a quest was counted as completed. Lets stats // update after every quest instead of once per round. virtual void quest_completed() = 0; @@ -55,7 +59,8 @@ class BBQGameActions{ // Run rounds of quests until NUM_QUESTS have been completed, or until // eggs run out when OUT_OF_EGGS is Stop, or until every doable quest on the -// panel is one that already failed all its attempts. +// panel is one that already failed all its attempts, or until a quest fails +// all its attempts when OUT_OF_RETRIES is Stop. // Throws UserSetupError if the exclusions leave no blue quest to do, or if // the panel shows nothing doable for many reads in a row. void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& game); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index c35ca22e4f..569af8ee21 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -146,6 +146,13 @@ class SimulatedBBQGameActions : public BBQGameActions{ } return success; } + virtual void reroll_quest(BBQuests quest) override{ + events.push_back("reroll"); + auto iter = std::find(panel.begin(), panel.end(), quest); + if (iter != panel.end()){ + *iter = next_new_quest(); + } + } virtual void quest_completed() override{ events.push_back("completed"); } @@ -199,6 +206,7 @@ struct BBQRoundsScenario{ size_t max_reads = 1000; std::vector new_quests = {}; std::vector> exclusions = {}; + BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; uint64_t expected_completed = 0; std::string expected; }; @@ -224,6 +232,7 @@ class Test_BBQFarmerRounds : public UnitTest{ row->action.set(action); options.QUEST_EXCLUSIONS.append_row(std::move(row)); } + options.OUT_OF_RETRIES.set(m_scenario.out_of_retries); SimulatedBBQGameActions game; game.panel = m_scenario.panel; @@ -371,14 +380,15 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ { // Round 1 completes nothing. It must not save, even though the // count (0) is a multiple of the interval. - // Every quest fails, so nothing doable is left and the farmer - // stops, but the round with no completions doesn't save. + // Every quest fails and is rerolled, so round 1 completes + // nothing and doesn't save; round 2 reaches the milestone. .name = "round without completions does not save", .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, - .expected_completed = 0, - .expected = "read process read do read do read do end read process", + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 3, + .expected = "read process read do reroll read do reroll read do reroll end " + round_of_3 + " end(save)", }, { // sneak-up fails, then leaves the panel along with make-tm. Round 2 @@ -428,6 +438,24 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected_completed = 3, .expected = round_of_3 + " end", }, + { + // sneak-up is rerolled to photo-polar, which round 2 attempts. + .name = "out of retries with reroll replaces the failed quest", + .num_quests = 3, + .never_completes = {BBQuests::sneak_up}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 5, + .expected = "read process read do completed read do completed read do reroll end " + round_of_3 + " end", + }, + { + .name = "out of retries with stop stops at once", + .num_quests = 3, + .panel = {BBQuests::sneak_up, BBQuests::make_tm, BBQuests::auto_10}, + .never_completes = {BBQuests::sneak_up}, + .out_of_retries = BBQOption::OutOfRetries::Stop, + .expected_completed = 0, + .expected = "read process read do", + }, }; for (const BBQRoundsScenario& scenario : scenarios){ database.add(scenario); From 2548fd262cdc960f98e5c420e35db272fbb8af61 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 13:07:34 -0700 Subject: [PATCH 31/50] Let the BBQ exclusions table reroll quests the farmer cannot do plan_quests() dropped quests the farmer cannot do (pickup, flying photo and catch) before looking at the exclusions table, so a reroll row for one of them did nothing: the quest stayed on the panel and blocked the slot. Pickup could not be picked in the table at all. The table is now applied first. Reroll and skip rows work for quests the farmer cannot do; a run row for one is ignored, since there is no route for it. Pickup is listed in the table. With nothing to do and nothing rerolled, the first quest the farmer cannot do that has no table row is rerolled instead of every quest, which costs one reroll instead of three or four. Every quest is still rerolled when there is no such quest. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 7 ++-- .../Programs/Farming/PokemonSV_BBQTests.cpp | 36 ++++++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 39 +++++++++++++++---- .../Farming/PokemonSV_BlueberryQuests.h | 8 ++-- 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp index 008307024b..66e94e4ce4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -15,7 +15,7 @@ const EnumDropdownDatabase& BBQuests_database(){ static EnumDropdownDatabase database{ {BBQuests::auto_10, "auto-10", "Defeat 10 wild Pokemon using Auto Battle!"}, {BBQuests::make_tm, "make-tm", "Make yourself a TM!"}, - //{BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, + {BBQuests::pickup_10, "pickup-10", "Pick up items on the ground 10 times!"}, {BBQuests::sneak_up, "sneak-up", "Successfully sneak up on 1 Pokemon and surprise them with a battle!"}, {BBQuests::photo_fly, "photo-fly", "Take a photo of a wild Pokemon in flight!"}, {BBQuests::photo_swim, "photo-swim", "Take a photo of a wild Pokemon that is swimming!"}, @@ -108,8 +108,9 @@ BBQuestTable::BBQuestTable() "Exclude the quests in the table. If you want to skip a quest, select it below. " "(Quests can be explicitly included, but this is not necessary.) " "Do not exclude too many quests, as rerolling costs BP. " - "The program will automatically reroll all quests if none are possible, but will not handle being out of BP. " - "Does not include egg hatching or pickup quests, as eggs are handled in other options and pickup is not possible. ", + "If no quest is possible, the program rerolls one quest it cannot do, or all quests if there is none. " + "Quests the program cannot do, such as pickup, are never run, but can be rerolled or skipped here. " + "Does not include egg hatching, as eggs are handled in other options. ", //"Duplicates of the same quest", //duplicates will work in table order. might lead to weird behavior like rerolling and then marking a quest as complete. //won't break the program though. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 569af8ee21..367b0326c2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -673,10 +673,42 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .expected_to_do = {make_tm}, }, { - .name = "nothing doable rerolls all", - .quests = {pickup_10, auto_10, catch_flying}, + // One quest costs less BP to reroll than all of them. + .name = "nothing doable rerolls the first quest not possible", + .quests = {auto_10, pickup_10, catch_flying}, + .expected_reroll_slots = {1}, + }, + { + .name = "nothing doable and nothing not possible rerolls all", + .quests = {auto_10, sneak_up}, + .extra_exclusions = {{sneak_up, BBQAction::skip}}, .expected_reroll_all = true, }, + { + // An undetected quest is not known to be impossible. + .name = "an undetected quest is not rerolled alone", + .quests = {UnableToDetect, auto_10}, + .expected_reroll_all = true, + }, + { + .name = "a skipped quest not possible is not rerolled", + .quests = {pickup_10, auto_10, catch_flying}, + .extra_exclusions = {{pickup_10, BBQAction::skip}}, + .expected_reroll_slots = {2}, + }, + { + .name = "a reroll row rerolls a quest not possible", + .quests = {make_tm, sneak_up, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::reroll}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "a run row does not run a quest not possible", + .quests = {make_tm, pickup_10}, + .extra_exclusions = {{pickup_10, BBQAction::run}}, + .expected_to_do = {make_tm}, + }, { // The reroll replaces a quest, so the next read has something new. .name = "a reroll row rerolls that slot", diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 091a6374f4..0d2fad01b4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -175,10 +175,11 @@ void plan_out_of_eggs(Logger& logger, const BBQOption& BBQ_OPTIONS, size_t slot, } //Apply each exclusions table row for the quest, in table order. Returns false if no row is for it. +//A quest the program cannot do is never run, but can still be rerolled or skipped. bool plan_from_exclusions( Logger& logger, const std::vector>& exclusions_table, - BBQuests quest, size_t slot, + BBQuests quest, size_t slot, bool possible, BBQuestPlan& plan ){ bool quest_in_table = false; @@ -190,6 +191,10 @@ bool plan_from_exclusions( quest_in_table = true; switch (row->action){ case BBQAction::run: + if (!possible){ + logger.log("Run selected, but this quest is not possible. Skipping quest."); + break; + } logger.log("Run selected. Adding quest to list."); plan.to_do.push_back(quest); break; @@ -380,11 +385,14 @@ BBQuestPlan plan_quests( BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); + //The first quest not possible that has no table row, to reroll if nothing else can be done. + std::optional not_possible_slot; + 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)){ - logger.log("Quest not possible"); + if (n == BBQuests::UnableToDetect){ + logger.log("Quest not detected."); continue; } //Check eggs remaining. @@ -393,15 +401,32 @@ BBQuestPlan plan_quests( 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); + const bool possible = !not_possible_quests.contains(n); + if (plan_from_exclusions(logger, exclusions_table, n, slot, possible, plan)){ + continue; + } + if (!possible){ + logger.log("Quest not possible."); + if (!not_possible_slot){ + not_possible_slot = slot; + } + continue; } + logger.log("Quest not in inclusion/exclusions table. Adding to list."); + plan.to_do.push_back(n); } //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 - plan.reroll_all = plan.reroll_slots.empty() && plan.to_do.empty(); + if (plan.reroll_slots.empty() && plan.to_do.empty()){ + if (not_possible_slot){ + //Rerolling one quest costs less BP than rerolling all of them. + logger.log("No possible quests! Rerolling a quest that is not possible."); + plan.reroll_slots.push_back(*not_possible_slot); + }else{ + plan.reroll_all = true; + } + } return plan; } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 4b45e65e49..2c0b948209 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -65,9 +65,11 @@ struct BBQuestPlan{ 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. +//the decision can be tested. Quests not possible are left out, unless the exclusions table +//rerolls them. 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. With +//nothing to do and nothing rerolled, the first quest not possible and not in the table is +//rerolled, or else every quest. BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, From ba751e5b165fde3715e3f5c6d41a4b450f946d96 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 13:53:13 -0700 Subject: [PATCH 32/50] Reroll BBQ quests the farmer cannot do as soon as they appear plan_quests() left a quest it cannot do (pickup, photo flying, catch flying) on the panel unless the exclusions table had a row for it, and rerolled one only when nothing else was doable. A red photo-flying and a blue catch-flying then held 2 of 4 slots for over 20 minutes, and no new red quest could appear. Reroll each such quest when it is read: 10 BP for a blue, 50 BP for a red, which pays 100-150 BP and blocks the next red while it stays. A table row for the quest still decides instead. With every one of them rerolled, the one-slot fallback has nothing left to pick, so it is removed; reroll-all still covers a panel with nothing doable and nothing rerolled. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 62 +++++++++++++++++-- .../Farming/PokemonSV_BlueberryQuests.cpp | 20 ++---- .../Farming/PokemonSV_BlueberryQuests.h | 8 +-- 3 files changed, 66 insertions(+), 24 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 367b0326c2..e3a7005dea 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -668,15 +668,55 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .expected_to_do = {make_tm, sneak_up}, }, { - .name = "quests not possible are left out", + // Left alone, it would hold its slot until something else is + // rerolled. A blue reroll costs 10 BP. + .name = "a blue quest not possible is rerolled while others are doable", + .quests = {make_tm, catch_flying, sneak_up}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {1}, + }, + { + // An undetected quest is neither run nor rerolled. + .name = "a quest not possible is rerolled, an undetected one is not", .quests = {pickup_10, make_tm, UnableToDetect}, .expected_to_do = {make_tm}, + .expected_reroll_slots = {0}, }, { - // One quest costs less BP to reroll than all of them. - .name = "nothing doable rerolls the first quest not possible", - .quests = {auto_10, pickup_10, catch_flying}, - .expected_reroll_slots = {1}, + // A red quest costs 50 BP to reroll, but pays 100-150 BP, and + // the next red quest cannot appear while it stays. + .name = "a red quest not possible is rerolled while the blues are doable", + .quests = {photo_flying, make_tm, sneak_up, travel_500}, + .expected_to_do = {make_tm, sneak_up, travel_500}, + .expected_reroll_slots = {0}, + }, + { + // The board that held 2 of 4 slots for 20 minutes. + .name = "a red and a blue quest not possible are both rerolled", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0, 2}, + }, + { + // There is no longer a single slot to fall back on: every quest + // not possible was rerolled already, so not every quest is. + .name = "nothing doable rerolls each quest not possible, not all", + .quests = {photo_flying, auto_10, pickup_10, catch_flying}, + .expected_reroll_slots = {0, 2, 3}, + }, + { + .name = "a skip row keeps a red quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .extra_exclusions = {{photo_flying, BBQAction::skip}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "a run row keeps a blue quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .extra_exclusions = {{catch_flying, BBQAction::run}}, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0}, }, { .name = "nothing doable and nothing not possible rerolls all", @@ -696,6 +736,16 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .extra_exclusions = {{pickup_10, BBQAction::skip}}, .expected_reroll_slots = {2}, }, + { + // Only skipped quests left: nothing changes the panel otherwise. + .name = "skip rows on every quest not possible reroll all when nothing is doable", + .quests = {photo_flying, auto_10, catch_flying}, + .extra_exclusions = { + {photo_flying, BBQAction::skip}, + {catch_flying, BBQAction::skip}, + }, + .expected_reroll_all = true, + }, { .name = "a reroll row rerolls a quest not possible", .quests = {make_tm, sneak_up, pickup_10}, @@ -722,7 +772,7 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .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}, + .expected_reroll_slots = {0, 1}, }, { // Each row for a quest applies, in table order. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 0d2fad01b4..49d4aeea10 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -385,9 +385,6 @@ BBQuestPlan plan_quests( BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); - //The first quest not possible that has no table row, to reroll if nothing else can be done. - std::optional not_possible_slot; - logger.log("Processing quests."); for (size_t slot = 0; slot < quest_list.size(); slot++){ const BBQuests n = quest_list[slot]; @@ -406,10 +403,11 @@ BBQuestPlan plan_quests( continue; } if (!possible){ - logger.log("Quest not possible."); - if (!not_possible_slot){ - not_possible_slot = slot; - } + //Left on the panel, it holds a slot until something else is rerolled. A red quest + //costs 50 BP to reroll instead of 10, but pays 100-150 BP and keeps the next red + //quest from appearing, so it is rerolled too. + logger.log("Quest not possible. Rerolling quest. New quest will be run in the next batch of quests."); + plan.reroll_slots.push_back(slot); continue; } logger.log("Quest not in inclusion/exclusions table. Adding to list."); @@ -419,13 +417,7 @@ BBQuestPlan plan_quests( //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 (plan.reroll_slots.empty() && plan.to_do.empty()){ - if (not_possible_slot){ - //Rerolling one quest costs less BP than rerolling all of them. - logger.log("No possible quests! Rerolling a quest that is not possible."); - plan.reroll_slots.push_back(*not_possible_slot); - }else{ - plan.reroll_all = true; - } + plan.reroll_all = true; } return plan; } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 2c0b948209..280b82b149 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -65,11 +65,11 @@ struct BBQuestPlan{ 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, unless the exclusions table -//rerolls them. Out-of-egg hatch quests and quests set to reroll in the exclusions table are +//the decision can be tested. Quests not possible are never run. Those not in the exclusions +//table are rerolled, red or blue, so they do not hold a slot; a table row for one decides +//instead. 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. With -//nothing to do and nothing rerolled, the first quest not possible and not in the table is -//rerolled, or else every quest. +//nothing to do and nothing rerolled, every quest is rerolled. BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, From a45c9dedfd709acb0adfbd3a15f889eb7581c42f Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 17:31:15 -0700 Subject: [PATCH 33/50] Reroll a BBQ quest not possible only when BP covers the reroll Rerolling a quest not possible on sight (C15) spent BP the player might not have: out of BP the reroll did not happen, and the program stopped with "Check that there is enough BP" before the doable quests ran. plan_quests() now takes the BP balance. While other quests are doable, a quest not possible is rerolled only if the BP left after the other planned rerolls covers it (red 50, blue 10); otherwise it is skipped as before C15 and the doable quests run. Unknown BP skips it too. With nothing doable, every quest not possible is still rerolled. process_quest_list() reads BP (one more panel open) only when the board has a quest not possible. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 47 +++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 64 +++++++++++++++++-- .../Farming/PokemonSV_BlueberryQuests.h | 16 +++-- 3 files changed, 114 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index e3a7005dea..4f7dc9eb05 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -474,6 +474,7 @@ struct BBQuestPlanScenario{ std::string name; std::vector quests; std::vector> extra_exclusions; + std::optional bp = 1000; uint8_t num_eggs = 1; uint8_t eggs_hatched = 0; BBQOption::OOEggs out_of_eggs = BBQOption::OOEggs::Reroll; @@ -516,7 +517,7 @@ class Test_BBQuestPlan : public UnitTest{ } NullLogger logger; - const BBQuestPlan plan = plan_quests(logger, options, m_scenario.quests, m_scenario.eggs_hatched); + const BBQuestPlan plan = plan_quests(logger, options, m_scenario.quests, m_scenario.eggs_hatched, m_scenario.bp); 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); @@ -718,6 +719,50 @@ static void add_tests_BBQuestPlan(UnitTestDatabase& database){ .expected_to_do = {make_tm, sneak_up}, .expected_reroll_slots = {0}, }, + { + // A red reroll costs 50 BP, a blue one 10. + .name = "BP for a blue reroll but not a red one skips the red quest not possible", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 49, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {2}, + }, + { + .name = "BP for exactly a red reroll rerolls it and skips the blue one", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 55, + .expected_to_do = {make_tm, sneak_up}, + .expected_reroll_slots = {0}, + }, + { + // Before, the reroll did not happen and the program stopped. + .name = "BP under a blue reroll skips quests not possible and runs the doable ones", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = 9, + .expected_to_do = {make_tm, sneak_up}, + }, + { + .name = "unknown BP skips quests not possible and runs the doable ones", + .quests = {photo_flying, make_tm, catch_flying, sneak_up}, + .bp = std::nullopt, + .expected_to_do = {make_tm, sneak_up}, + }, + { + // A reroll row is paid for first. + .name = "BP left after a reroll row decides the quests not possible", + .quests = {make_tm, catch_flying, sneak_up, pickup_10}, + .extra_exclusions = {{sneak_up, BBQAction::reroll}}, + .bp = 25, + .expected_to_do = {make_tm}, + .expected_reroll_slots = {1, 2}, + }, + { + // Nothing else changes the panel, so BP does not hold it back. + .name = "nothing doable rerolls each quest not possible whatever the BP", + .quests = {photo_flying, auto_10, catch_flying}, + .bp = 0, + .expected_reroll_slots = {0, 2}, + }, { .name = "nothing doable and nothing not possible rerolls all", .quests = {auto_10, sneak_up}, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 49d4aeea10..5ea663bd75 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/ErrorDumper.h" @@ -376,15 +377,23 @@ void reroll_quest_slot( } } +int reroll_cost(BBQuests quest){ + return red_quests.contains(quest) ? 50 : 10; +} + BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + std::optional bp ){ BBQuestPlan plan; std::vector> exclusions_table = BBQ_OPTIONS.QUEST_EXCLUSIONS.copy_snapshot(); + //Quests not possible and not in the table, in panel order. Rerolled below if BP allows. + std::vector not_possible_slots; + logger.log("Processing quests."); for (size_t slot = 0; slot < quest_list.size(); slot++){ const BBQuests n = quest_list[slot]; @@ -403,17 +412,47 @@ BBQuestPlan plan_quests( continue; } if (!possible){ - //Left on the panel, it holds a slot until something else is rerolled. A red quest - //costs 50 BP to reroll instead of 10, but pays 100-150 BP and keeps the next red - //quest from appearing, so it is rerolled too. - logger.log("Quest not possible. Rerolling quest. New quest will be run in the next batch of quests."); - plan.reroll_slots.push_back(slot); + logger.log("Quest not possible."); + not_possible_slots.push_back(slot); continue; } logger.log("Quest not in inclusion/exclusions table. Adding to list."); plan.to_do.push_back(n); } + if (plan.to_do.empty()){ + //Nothing else to do: a quest not possible only changes by rerolling it. + for (size_t slot : not_possible_slots){ + logger.log("No possible quests! Rerolling quest " + std::to_string(slot + 1) + ", which is not possible."); + plan.reroll_slots.push_back(slot); + } + }else{ + //Left on the panel, a quest not possible holds a slot until something else is rerolled. + //A red quest costs 50 BP to reroll instead of 10, but pays 100-150 BP and keeps the + //next red quest from appearing, so it is rerolled too. Without the BP for it, it is + //skipped and the doable quests run, rather than stopping the program on a reroll + //that does not happen. + int budget = bp.value_or(0); + for (size_t slot : plan.reroll_slots){ + budget -= reroll_cost(quest_list[slot]); + } + for (size_t slot : not_possible_slots){ + const int cost = reroll_cost(quest_list[slot]); + if (!bp || budget < cost){ + logger.log( + "Quest " + std::to_string(slot + 1) + " not possible, but BP (" + + (bp ? std::to_string(*bp) : "unknown") + ") does not cover its " + + std::to_string(cost) + " BP reroll. Skipping it." + ); + continue; + } + logger.log("Quest " + std::to_string(slot + 1) + " not possible. Rerolling quest. New quest will be run in the next batch of quests."); + budget -= cost; + plan.reroll_slots.push_back(slot); + } + } + std::sort(plan.reroll_slots.begin(), plan.reroll_slots.end()); + //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 (plan.reroll_slots.empty() && plan.to_do.empty()){ @@ -429,7 +468,18 @@ std::vector process_quest_list( const std::vector& quest_list, uint8_t eggs_hatched ){ - BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched); + //Read BP only when a quest not possible may be rerolled while others are doable. + std::optional bp; + if (std::any_of(quest_list.begin(), quest_list.end(), [](BBQuests quest){ + return quest != BBQuests::UnableToDetect && not_possible_quests.contains(quest); + })){ + const int read = read_BP(info, stream, context); + if (read >= 0){ + bp = read; + } + stream.log("BP: " + (bp ? std::to_string(*bp) : "unable to read")); + } + BBQuestPlan plan = plan_quests(stream.logger(), BBQ_OPTIONS, quest_list, eggs_hatched, bp); //Each reroll updates the panel the next one checks against. std::vector panel = quest_list; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 280b82b149..f3f91cd04d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -64,17 +64,23 @@ struct BBQuestPlan{ std::vector reroll_slots; //Slots to reroll, in panel order. bool reroll_all = false; //Nothing to do and nothing rerolled: reroll every slot. }; +//BP it costs to reroll one quest: 50 for a red quest, 10 for a blue one. +int reroll_cost(BBQuests quest); //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 never run. Those not in the exclusions -//table are rerolled, red or blue, so they do not hold a slot; a table row for one decides -//instead. 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. With -//nothing to do and nothing rerolled, every quest is rerolled. +//table are rerolled, red or blue, so they do not hold a slot, as long as BP (the balance +//read from the panel, if any) still covers their cost after the other planned rerolls; the +//rest are skipped, and the doable quests run. A table row for one decides instead. +//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. With nothing to +//do, every quest not possible and not in the table is rerolled whatever the BP, or else +//every quest. BBQuestPlan plan_quests( Logger& logger, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + std::optional bp ); //The steps of rerolling one quest on the quest panel. They are injected so that the reroll From 57c015324f3baccea7e84d43ed2af0129ccbd241 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 15:10:14 -0700 Subject: [PATCH 34/50] BBQ Farmer: show BP earned, spent, net and BP/hour in the stats The BBQ Farmer counts quests but not what they are worth, so a change to the program can't be judged by the BP it makes per hour. read_quests() already screenshots the quest panel, which shows the BP balance in its corner. It now takes an optional out-parameter and reads the balance from that same screenshot with the reader from PokemonAutomation/Arduino-Source#1628, so no button presses are added. BBQSoloFarmerGameActions passes it at every quest read, including the reads inside a reroll, and feeds each balance into a small BBQPointsLedger: a rise between two reads is earned, a drop (a reroll) is spent, and OCR failures (-1) are skipped. The BP read that decides whether a quest not possible can be rerolled is recorded too, and one extra read_BP() after the last batch picks up its reward. The stats show "BP Earned" and "BP Spent" as saved counters, plus a derived "Net BP" and, for the current run, "BP/hour" over the span between the first and latest reads. The derived values are not written to the stats file. read_quests() now opens the panel with open_quest_panel(), which presses the same buttons and waits for the same watcher, instead of its own copy, to stay under the function length limit. Add data-driven PokemonSV::BBQPointsLedger unit tests, with the other BBQ tests in PokemonSV_BBQTests.cpp. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQPointsLedger.cpp | 41 +++++++ .../Farming/PokemonSV_BBQPointsLedger.h | 46 ++++++++ .../Farming/PokemonSV_BBQSoloFarmer.cpp | 68 +++++++++++- .../Programs/Farming/PokemonSV_BBQTests.cpp | 105 ++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 51 +++++---- .../Farming/PokemonSV_BlueberryQuests.h | 14 ++- SerialPrograms/cmake/SourceFiles.cmake | 2 + 7 files changed, 295 insertions(+), 32 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp new file mode 100644 index 0000000000..0fb3c9cc5d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp @@ -0,0 +1,41 @@ +/* BBQ Points Ledger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonSV_BBQPointsLedger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +void BBQPointsLedger::record(int read, WallClock when){ + if (read < 0){ + return; + } + if (starting_balance < 0){ + starting_balance = read; + first_read = when; + }else if (read > balance){ + earned += read - balance; + }else{ + spent += balance - read; + } + balance = read; + last_read = when; +} + +int64_t BBQPointsLedger::net_per_hour() const{ + const int64_t span_ms = std::chrono::duration_cast(last_read - first_read).count(); + if (starting_balance < 0 || span_ms <= 0){ + return 0; + } + return net() * 3600000 / span_ms; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h new file mode 100644 index 0000000000..48937d1fae --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h @@ -0,0 +1,46 @@ +/* BBQ Points Ledger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQPointsLedger_H +#define PokemonAutomation_PokemonSV_BBQPointsLedger_H + +#include +#include "Common/Cpp/Time.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// BP accounting from successive balance reads of the quest panel. +// A rise between two reads is income (quest rewards) and a drop is an +// expense (rerolls), so income and expense are only split correctly when +// they don't fall between the same two reads. The net is always exact. +struct BBQPointsLedger{ + int starting_balance = -1; // -1 until the first successful read. + int balance = -1; + WallClock first_read; + WallClock last_read; + uint64_t earned = 0; + uint64_t spent = 0; + + // Record a balance read at "when". Negative reads (OCR failures) are ignored. + void record(int read, WallClock when); + + int64_t net() const{ + return (int64_t)earned - (int64_t)spent; + } + + // Net BP per hour between the first and the latest read. 0 until two reads + // are at least a millisecond apart. + int64_t net_per_hour() const; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 84fd26495c..36a22e8dd0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -6,6 +6,7 @@ #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/PrettyPrint.h" #include "CommonFramework/Notifications/ProgramNotifications.h" #include "CommonFramework/ProgramStats/StatsTracking.h" #include "CommonTools/StartupChecks/StartProgramChecks.h" @@ -21,6 +22,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" //#include "VideoGames/PokemonSV/PokemonSV_Settings.h" #include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h" #include "PokemonSV_BBQSoloFarmer.h" namespace PokemonAutomation{ @@ -49,14 +51,45 @@ struct BBQSoloFarmer_Descriptor::Stats : public StatsTracker{ : questsCompleted(m_stats["Quests Completed"]) , saves(m_stats["Saves"]) , errors(m_stats["Errors"]) + , bp_earned(m_stats["BP Earned"]) + , bp_spent(m_stats["BP Spent"]) { m_display_order.emplace_back("Quests Completed"); m_display_order.emplace_back("Saves"); m_display_order.emplace_back("Errors", HIDDEN_IF_ZERO); + m_display_order.emplace_back("BP Earned"); + m_display_order.emplace_back("BP Spent"); } + + void update_BP(const BBQPointsLedger& ledger){ + bp_earned.store(ledger.earned, std::memory_order_relaxed); + bp_spent.store(ledger.spent, std::memory_order_relaxed); + bp_per_hour.store(ledger.net_per_hour(), std::memory_order_relaxed); + has_bp_per_hour.store(true, std::memory_order_relaxed); + } + + // Net and BP/hour are derived, so they are shown but not saved. Past runs + // have no BP/hour since their time span isn't saved. + virtual std::string to_str(PrintMode mode) const override{ + std::string str = StatsTracker::to_str(mode); + if (mode == SAVE_TO_STATS_FILE){ + return str; + } + const int64_t net = (int64_t)bp_earned.load(std::memory_order_relaxed) - (int64_t)bp_spent.load(std::memory_order_relaxed); + str += " - Net BP: " + tostr_u_commas(net); + if (has_bp_per_hour.load(std::memory_order_relaxed)){ + str += " - BP/hour: " + tostr_u_commas(bp_per_hour.load(std::memory_order_relaxed)); + } + return str; + } + std::atomic& questsCompleted; std::atomic& saves; std::atomic& errors; + std::atomic& bp_earned; + std::atomic& bp_spent; + std::atomic bp_per_hour{0}; + std::atomic has_bp_per_hour{false}; }; std::unique_ptr BBQSoloFarmer_Descriptor::make_stats() const{ return std::unique_ptr(new Stats()); @@ -245,24 +278,32 @@ 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); + int BP = -1; + std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options, &BP); + record_BP(BP); + return quests; } virtual std::vector 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); + std::vector to_do = PokemonSV::process_quest_list( + m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched, &m_bp_ledger + ); + update_BP_stats(); + return to_do; } virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched); } virtual void reroll_quest(BBQuests quest) override{ - std::vector quests = PokemonSV::read_quests(m_env.program_info(), m_env.console, m_context, m_options); + std::vector quests = read_quests(); auto iter = std::find(quests.begin(), quests.end(), quest); if (iter == quests.end()){ m_env.log("Failed quest is no longer on the panel. Not rerolling."); return; } - reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin()); + reroll_quest_slot(m_env.program_info(), m_env.console, m_context, m_options, quests, iter - quests.begin(), &m_bp_ledger); + update_BP_stats(); } virtual void quest_completed() override{ m_stats.questsCompleted++; @@ -290,12 +331,28 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ send_program_status_notification(m_env, m_status_update); } + //BP is read from the quest panel screenshots the program already takes. + void record_BP(int BP){ + m_bp_ledger.record(BP, current_time()); + update_BP_stats(); + } + private: + void update_BP_stats(){ + m_env.log( + "BP: " + std::to_string(m_bp_ledger.balance) + " (start " + std::to_string(m_bp_ledger.starting_balance) + + ", earned " + std::to_string(m_bp_ledger.earned) + ", spent " + std::to_string(m_bp_ledger.spent) + ")" + ); + m_stats.update_BP(m_bp_ledger); + m_env.update_stats(); + } + SingleSwitchProgramEnvironment& m_env; ProControllerContext& m_context; BBQOption& m_options; BBQSoloFarmer_Descriptor::Stats& m_stats; EventNotificationOption& m_status_update; + BBQPointsLedger m_bp_ledger; }; } @@ -330,6 +387,9 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE); run_bbq_rounds(env.logger(), BBQ_OPTIONS, game); + + //No quest panel is read after the last batch, so read the balance once more. + game.record_BP(read_BP(env.program_info(), env.console, context)); env.update_stats(); if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 4f7dc9eb05..2663f3fc44 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -9,6 +9,7 @@ #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQPointsLedger.h" #include "PokemonSV_BBQSoloFarmer.h" #include "PokemonSV_BBQTests.h" @@ -952,10 +953,114 @@ static void add_tests_BBQReroll(UnitTestDatabase& database){ +namespace{ + +// Balance reads in order, each at a minute offset from the first, and the +// ledger they must produce. A negative read is an OCR failure. +struct BBQPointsLedgerScenario{ + struct Read{ + int balance; + int minute; + }; + std::string name; + std::vector reads = {}; + int expected_start = -1; + uint64_t expected_earned = 0; + uint64_t expected_spent = 0; + int64_t expected_net = 0; + int64_t expected_per_hour = 0; +}; + +class Test_BBQPointsLedger : public UnitTest{ +public: + Test_BBQPointsLedger(BBQPointsLedgerScenario scenario) + : UnitTest("PokemonSV::BBQPointsLedger - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const WallClock start = current_time(); + BBQPointsLedger ledger; + for (const BBQPointsLedgerScenario::Read& read : m_scenario.reads){ + ledger.record(read.balance, start + std::chrono::minutes(read.minute)); + } + const std::pair> checks[] = { + {"starting balance", {m_scenario.expected_start, ledger.starting_balance}}, + {"earned", {(int64_t)m_scenario.expected_earned, (int64_t)ledger.earned}}, + {"spent", {(int64_t)m_scenario.expected_spent, (int64_t)ledger.spent}}, + {"net", {m_scenario.expected_net, ledger.net()}}, + {"net per hour", {m_scenario.expected_per_hour, ledger.net_per_hour()}}, + }; + for (const auto& [field, values] : checks){ + if (values.first != values.second){ + return std::string("Expected ") + field + " " + std::to_string(values.first) + + ", got " + std::to_string(values.second); + } + } + return true; + } + +private: + const BBQPointsLedgerScenario m_scenario; +}; + +} + +static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ + const BBQPointsLedgerScenario scenarios[] = { + { + .name = "no reads", + }, + { + .name = "one read sets the start", + .reads = {{6750, 0}}, + .expected_start = 6750, + }, + { + // A quest reward, then a reroll. + .name = "rises are earned and drops are spent", + .reads = {{6750, 0}, {6780, 10}, {6730, 20}}, + .expected_start = 6750, + .expected_earned = 30, + .expected_spent = 50, + .expected_net = -20, + .expected_per_hour = -60, + }, + { + .name = "an unchanged balance adds nothing", + .reads = {{6750, 0}, {6750, 30}}, + .expected_start = 6750, + }, + { + .name = "OCR failures are skipped", + .reads = {{6750, 0}, {-1, 5}, {6800, 30}}, + .expected_start = 6750, + .expected_earned = 50, + .expected_net = 50, + .expected_per_hour = 100, + }, + { + // The hour starts at the first successful read. + .name = "a failed first read does not set the start", + .reads = {{-1, 0}, {6750, 10}, {6760, 70}}, + .expected_start = 6750, + .expected_earned = 10, + .expected_net = 10, + .expected_per_hour = 10, + }, + }; + for (const BBQPointsLedgerScenario& scenario : scenarios){ + database.add(scenario); + } +} + + + void add_tests_BBQ(UnitTestDatabase& database){ add_tests_BBQReroll(database); add_tests_BBQuestPlan(database); add_tests_BBQFarmerRounds(database); + add_tests_BBQPointsLedger(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 83145f1e77..6af83bb490 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -106,27 +106,12 @@ bool open_quest_panel(VideoStream& stream, ProControllerContext& context){ std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + int* BP ){ std::vector quest_list; - //Open quest list. Wait for it to open. - 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++){ //try 6 times - 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(); + open_quest_panel(stream, context); //Read in the initial 4 quests. stream.log("Reading quests."); @@ -145,6 +130,11 @@ std::vector read_quests( quest_list.push_back(BBQuests_string_to_enum(fourth_quest)); } + //The balance is on the same panel, so reading it costs no extra button presses. + if (BP != nullptr){ + *BP = read_BP_from_image(stream.logger(), screen); + } + //Close quest list pbf_mash_button(context, BUTTON_B, 800ms); context.wait_for_all_requests(); @@ -212,9 +202,11 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ ConsoleBBQRerollActions( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + BBQPointsLedger* ledger ) : m_info(info), m_stream(stream), m_context(context), m_options(BBQ_OPTIONS) + , m_ledger(ledger) {} virtual bool open_panel() override{ @@ -259,7 +251,12 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ 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); + int BP = -1; + std::vector quests = PokemonSV::read_quests(m_info, m_stream, m_context, m_options, &BP); + if (m_ledger != nullptr){ + m_ledger->record(BP, current_time()); + } + return quests; } private: @@ -267,6 +264,7 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ VideoStream& m_stream; ProControllerContext& m_context; const BBQOption& m_options; + BBQPointsLedger* m_ledger; }; //This does not handle out of BP. @@ -345,9 +343,10 @@ void reroll_quest_slot( VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, std::vector& quest_list, - size_t slot + size_t slot, + BBQPointsLedger* ledger ){ - ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS); + ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger); switch (reroll_quest(stream.logger(), actions, quest_list, slot)){ case BBQRerollResult::Rerolled: return; @@ -462,7 +461,8 @@ std::vector process_quest_list( VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, const std::vector& quest_list, - uint8_t eggs_hatched + uint8_t eggs_hatched, + BBQPointsLedger* ledger ){ //Read BP only when a quest not possible may be rerolled while others are doable. std::optional bp; @@ -470,6 +470,9 @@ std::vector process_quest_list( return quest != BBQuests::UnableToDetect && not_possible_quests.contains(quest); })){ const int read = read_BP(info, stream, context); + if (ledger != nullptr){ + ledger->record(read, current_time()); + } if (read >= 0){ bp = read; } @@ -480,7 +483,7 @@ std::vector process_quest_list( //Each reroll updates the panel the next one checks against. std::vector panel = quest_list; for (size_t slot : plan.reroll_slots){ - reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot); + reroll_quest_slot(info, stream, context, BBQ_OPTIONS, panel, slot, ledger); } 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 f3f91cd04d..5c6c776f06 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -13,6 +13,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h" #include "VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h" +#include "PokemonSV_BBQPointsLedger.h" namespace PokemonAutomation{ struct ProgramInfo; @@ -52,10 +53,12 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& //Open the quest panel and read in the current quests. //Currently only supports singleplayer. //For multiplayer, we will want keep track of which quest are gold/red and scroll cursor down until all of the current player's blue quests are in. +//If BP is not null, also read the BP balance from the same screenshot (-1 on OCR failure). std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS + const BBQOption& BBQ_OPTIONS, + int* BP = nullptr ); //What to do with the quests read from the panel. Slot 0 is the top quest on the panel. @@ -130,23 +133,26 @@ BBQRerollResult reroll_quest( ); //Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program -//if the reroll fails. +//if the reroll fails. If ledger is not null, it records the BP of each panel read. void reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, std::vector& quests, - size_t slot + size_t slot, + BBQPointsLedger* ledger = nullptr ); //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. +//If ledger is not null, it records every BP read here. 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 + uint8_t eggs_hatched, + BBQPointsLedger* ledger = nullptr ); //Take the current quest and calls the function to do it, then checks the quest was successful. Returns true if so. diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index a2a6c186d9..2a4c51a0f1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2597,6 +2597,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp From 8d5cb4884e5f9d7a8defda46cec73f9a46cbdc79 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 15:10:59 -0700 Subject: [PATCH 35/50] BBQ Farmer: check the BP stats against the rerolls at the end of a run The BP stats are only as good as the balance reads behind them, and a misread or a reroll that cost more than expected would go unnoticed. Each reroll now tells the BBQPointsLedger what it should cost (50 BP for a red quest, 10 for a blue one): a single-slot reroll when it confirms the reroll, and a whole-panel reroll per quest it rerolls. The next successful read must show exactly that drop. A failed read, or an unreadable slot in a whole-panel reroll, skips the check for that window instead of reporting a false mismatch. At the end of a run the farmer logs one line, in red on a mismatch: "BP check: OK. Balance 13694 -> 14044 (change 350), stats net 350. Rerolls expected to cost 30 BP; 3/3 reroll windows matched the reads." If the run stops early, from an error or the Stop button, the line is still logged, as of the last panel read. Add ledger unit tests for matching, mismatched and skipped windows. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQPointsLedger.cpp | 44 ++++++++++ .../Farming/PokemonSV_BBQPointsLedger.h | 23 ++++++ .../Farming/PokemonSV_BBQSoloFarmer.cpp | 29 ++++++- .../Programs/Farming/PokemonSV_BBQTests.cpp | 82 ++++++++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 25 ++++-- .../Farming/PokemonSV_BlueberryQuests.h | 5 +- 6 files changed, 195 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp index 0fb3c9cc5d..9c3fd32ed3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.cpp @@ -13,8 +13,22 @@ namespace PokemonSV{ void BBQPointsLedger::record(int read, WallClock when){ if (read < 0){ + // The next read may include quest income, so this window can't be checked. + m_pending_unchecked |= m_pending_spend > 0; return; } + if (m_pending_spend > 0 && starting_balance >= 0 && !m_pending_unchecked){ + checked_windows++; + const int change = read - balance; + if (change != -m_pending_spend){ + mismatched_windows++; + last_mismatch = "rerolls should have cost " + std::to_string(m_pending_spend) + + " BP, but the balance went from " + std::to_string(balance) + + " to " + std::to_string(read); + } + } + m_pending_spend = 0; + m_pending_unchecked = false; if (starting_balance < 0){ starting_balance = read; first_read = when; @@ -27,6 +41,36 @@ void BBQPointsLedger::record(int read, WallClock when){ last_read = when; } +void BBQPointsLedger::expect_spend(int bp){ + m_pending_spend += bp; + expected_spent += bp; +} +void BBQPointsLedger::skip_spend_check(){ + m_pending_unchecked = true; +} + +bool BBQPointsLedger::reconciled() const{ + return starting_balance >= 0 + && (int64_t)balance - starting_balance == net() + && mismatched_windows == 0; +} +std::string BBQPointsLedger::reconciliation() const{ + if (starting_balance < 0){ + return "BP check: no balance was read."; + } + std::string str = "BP check: " + std::string(reconciled() ? "OK" : "MISMATCH") + + ". Balance " + std::to_string(starting_balance) + " -> " + std::to_string(balance) + + " (change " + std::to_string((int64_t)balance - starting_balance) + + "), stats net " + std::to_string(net()) + + ". Rerolls expected to cost " + std::to_string(expected_spent) + + " BP; " + std::to_string(checked_windows - mismatched_windows) + "/" + + std::to_string(checked_windows) + " reroll windows matched the reads."; + if (mismatched_windows > 0){ + str += " Last mismatch: " + last_mismatch + "."; + } + return str; +} + int64_t BBQPointsLedger::net_per_hour() const{ const int64_t span_ms = std::chrono::duration_cast(last_read - first_read).count(); if (starting_balance < 0 || span_ms <= 0){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h index 48937d1fae..eb65cd9c98 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQPointsLedger.h @@ -8,6 +8,7 @@ #define PokemonAutomation_PokemonSV_BBQPointsLedger_H #include +#include #include "Common/Cpp/Time.h" namespace PokemonAutomation{ @@ -30,6 +31,24 @@ struct BBQPointsLedger{ // Record a balance read at "when". Negative reads (OCR failures) are ignored. void record(int read, WallClock when); + // The program paid "bp" for a reroll it believes went through. The next + // successful read must show exactly that drop, since the farmer reads the + // panel again before doing any quest. A mismatch means a reroll that didn't + // happen, an unexpected BP change or a misread balance. + void expect_spend(int bp); + // A reroll whose cost is unknown (an unreadable slot): don't check this window. + void skip_spend_check(); + + uint64_t expected_spent = 0; // Sum of every expect_spend(). + uint64_t checked_windows = 0; + uint64_t mismatched_windows = 0; + std::string last_mismatch; + + // End-of-run reconciliation, one line for the log: the balance change + // against the stats' net, and the reroll spends against the reads. + bool reconciled() const; + std::string reconciliation() const; + int64_t net() const{ return (int64_t)earned - (int64_t)spent; } @@ -37,6 +56,10 @@ struct BBQPointsLedger{ // Net BP per hour between the first and the latest read. 0 until two reads // are at least a millisecond apart. int64_t net_per_hour() const; + +private: + int m_pending_spend = 0; + bool m_pending_unchecked = false; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 36a22e8dd0..044a2bbb32 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -336,6 +336,7 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ m_bp_ledger.record(BP, current_time()); update_BP_stats(); } + const BBQPointsLedger& bp_ledger() const{ return m_bp_ledger; } private: void update_BP_stats(){ @@ -355,6 +356,29 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ BBQPointsLedger m_bp_ledger; }; +void log_BP_check(SingleSwitchProgramEnvironment& env, const BBQPointsLedger& ledger, const std::string& note){ + env.log(ledger.reconciliation() + note, ledger.reconciled() ? COLOR_BLACK : COLOR_RED); +} + +//Run the quest rounds, then check the BP stats against the rerolls. The check is logged even +//when the run stops early. +void run_rounds_and_check_BP( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + const BBQOption& options, BBQSoloFarmerGameActions& game +){ + try{ + run_bbq_rounds(env.logger(), options, game); + }catch (...){ + //The balance isn't read again, since the game may be on any screen. + log_BP_check(env, game.bp_ledger(), " Stopped early, so this is as of the last panel read."); + throw; + } + + //No quest panel is read after the last batch, so read the balance once more. + game.record_BP(read_BP(env.program_info(), env.console, context)); + log_BP_check(env, game.bp_ledger(), ""); +} + } void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ @@ -386,10 +410,7 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo */ BBQSoloFarmerGameActions game(env, context, BBQ_OPTIONS, stats, NOTIFICATION_STATUS_UPDATE); - run_bbq_rounds(env.logger(), BBQ_OPTIONS, game); - - //No quest panel is read after the last batch, so read the balance once more. - game.record_BP(read_BP(env.program_info(), env.console, context)); + run_rounds_and_check_BP(env, context, BBQ_OPTIONS, game); env.update_stats(); if (BBQ_OPTIONS.FIX_TIME_WHEN_DONE){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 2663f3fc44..1787051a4a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -956,11 +956,14 @@ static void add_tests_BBQReroll(UnitTestDatabase& database){ namespace{ // Balance reads in order, each at a minute offset from the first, and the -// ledger they must produce. A negative read is an OCR failure. +// ledger they must produce. A negative read is an OCR failure. "spend_before" +// is what the program paid for rerolls just before the read (-1: a reroll of +// unknown cost). struct BBQPointsLedgerScenario{ struct Read{ int balance; int minute; + int spend_before = 0; }; std::string name; std::vector reads = {}; @@ -969,6 +972,8 @@ struct BBQPointsLedgerScenario{ uint64_t expected_spent = 0; int64_t expected_net = 0; int64_t expected_per_hour = 0; + uint64_t expected_checked = 0; + uint64_t expected_mismatched = 0; }; class Test_BBQPointsLedger : public UnitTest{ @@ -982,6 +987,11 @@ class Test_BBQPointsLedger : public UnitTest{ const WallClock start = current_time(); BBQPointsLedger ledger; for (const BBQPointsLedgerScenario::Read& read : m_scenario.reads){ + if (read.spend_before < 0){ + ledger.skip_spend_check(); + }else if (read.spend_before > 0){ + ledger.expect_spend(read.spend_before); + } ledger.record(read.balance, start + std::chrono::minutes(read.minute)); } const std::pair> checks[] = { @@ -990,6 +1000,10 @@ class Test_BBQPointsLedger : public UnitTest{ {"spent", {(int64_t)m_scenario.expected_spent, (int64_t)ledger.spent}}, {"net", {m_scenario.expected_net, ledger.net()}}, {"net per hour", {m_scenario.expected_per_hour, ledger.net_per_hour()}}, + {"checked reroll windows", {(int64_t)m_scenario.expected_checked, (int64_t)ledger.checked_windows}}, + {"mismatched reroll windows", {(int64_t)m_scenario.expected_mismatched, (int64_t)ledger.mismatched_windows}}, + //Reconciled means a balance was read and no reroll window mismatched. + {"reconciled", {m_scenario.expected_start >= 0 && m_scenario.expected_mismatched == 0, ledger.reconciled()}}, }; for (const auto& [field, values] : checks){ if (values.first != values.second){ @@ -1006,6 +1020,70 @@ class Test_BBQPointsLedger : public UnitTest{ } +// Reroll windows where the next read does not show what the rerolls should have cost. +static void add_tests_BBQPointsLedgerMismatches(UnitTestDatabase& database){ + const BBQPointsLedgerScenario mismatches[] = { + { + // The game didn't take the BP: the reroll didn't happen. + .name = "a reroll that cost nothing is a mismatch", + .reads = {{6750, 0}, {6750, 1, 10}}, + .expected_start = 6750, + .expected_checked = 1, + .expected_mismatched = 1, + }, + { + // 6740 misread as 6140. + .name = "a misread after a reroll is a mismatch", + .reads = {{6750, 0}, {6140, 1, 10}}, + .expected_start = 6750, + .expected_spent = 610, + .expected_net = -610, + .expected_per_hour = -36600, + .expected_checked = 1, + .expected_mismatched = 1, + }, + }; + for (const BBQPointsLedgerScenario& scenario : mismatches){ + database.add(scenario); + } +} + +// Reroll windows: what the rerolls should have cost against the next read. +static void add_tests_BBQPointsLedgerRerolls(UnitTestDatabase& database){ + const BBQPointsLedgerScenario scenarios[] = { + { + // A blue and a red reroll, each followed by a read, as the farmer does. + .name = "rerolls that match the reads reconcile", + .reads = {{6750, 0}, {6740, 1, 10}, {6800, 10}, {6750, 11, 50}}, + .expected_start = 6750, + .expected_earned = 60, + .expected_spent = 60, + .expected_per_hour = 0, + .expected_checked = 2, + }, + { + // The next read may include quest income, so the window isn't checked. + .name = "a failed read after a reroll skips the check", + .reads = {{6750, 0}, {-1, 1, 10}, {6790, 10}}, + .expected_start = 6750, + .expected_earned = 40, + .expected_net = 40, + .expected_per_hour = 240, + }, + { + .name = "a reroll of unknown cost skips the check", + .reads = {{6750, 0}, {6700, 1, -1}}, + .expected_start = 6750, + .expected_spent = 50, + .expected_net = -50, + .expected_per_hour = -3000, + }, + }; + for (const BBQPointsLedgerScenario& scenario : scenarios){ + database.add(scenario); + } +} + static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ const BBQPointsLedgerScenario scenarios[] = { { @@ -1052,6 +1130,8 @@ static void add_tests_BBQPointsLedger(UnitTestDatabase& database){ for (const BBQPointsLedgerScenario& scenario : scenarios){ database.add(scenario); } + add_tests_BBQPointsLedgerRerolls(database); + add_tests_BBQPointsLedgerMismatches(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 6af83bb490..e72ec9a2f1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -203,10 +203,10 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS, - BBQPointsLedger* ledger + BBQPointsLedger* ledger, int reroll_cost ) : m_info(info), m_stream(stream), m_context(context), m_options(BBQ_OPTIONS) - , m_ledger(ledger) + , m_ledger(ledger), m_reroll_cost(reroll_cost) {} virtual bool open_panel() override{ @@ -246,6 +246,9 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ pbf_press_button(m_context, BUTTON_A, 160ms, 400ms); pbf_wait(m_context, 800ms); m_context.wait_for_all_requests(); + if (m_ledger != nullptr){ + m_ledger->expect_spend(m_reroll_cost); + } } virtual void close_panel() override{ press_Bs_to_back_to_overworld(m_info, m_stream, m_context); @@ -265,17 +268,27 @@ class ConsoleBBQRerollActions : public BBQRerollActions{ ProControllerContext& m_context; const BBQOption& m_options; BBQPointsLedger* m_ledger; + int m_reroll_cost; }; //This does not handle out of BP. void reroll_all_slots( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, - size_t slots + const std::vector& quests, + BBQPointsLedger* ledger ){ open_quest_panel(stream, context); - for (size_t i = 0; i < slots; i++){ + for (BBQuests quest : quests){ + if (ledger != nullptr){ + //An unreadable slot may be empty or a quest of unknown cost. + if (quest == BBQuests::UnableToDetect){ + ledger->skip_spend_check(); + }else{ + ledger->expect_spend(reroll_cost(quest)); + } + } pbf_press_button(context, BUTTON_A, 160ms, 400ms); pbf_press_button(context, BUTTON_A, 160ms, 400ms); //Yes. pbf_wait(context, 800ms); @@ -346,7 +359,7 @@ void reroll_quest_slot( size_t slot, BBQPointsLedger* ledger ){ - ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger); + ConsoleBBQRerollActions actions(info, stream, context, BBQ_OPTIONS, ledger, reroll_cost(quest_list[slot])); switch (reroll_quest(stream.logger(), actions, quest_list, slot)){ case BBQRerollResult::Rerolled: return; @@ -487,7 +500,7 @@ std::vector process_quest_list( } if (plan.reroll_all){ stream.log("No possible quests! Rerolling all quests."); - reroll_all_slots(info, stream, context, quest_list.size()); + reroll_all_slots(info, stream, context, quest_list, ledger); } //Disabled since 2025-05, when the exclusions table gained its reroll action. Before that, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 5c6c776f06..fb0bc3238a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -133,7 +133,8 @@ BBQRerollResult reroll_quest( ); //Reroll the quest in `slot` of `quests` on the console, with reroll_quest(). Stops the program -//if the reroll fails. If ledger is not null, it records the BP of each panel read. +//if the reroll fails. If ledger is not null, it records the BP of each panel read and what the +//reroll should cost. void reroll_quest_slot( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -145,7 +146,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. -//If ledger is not null, it records every BP read here. +//If ledger is not null, it records every BP read here and what each reroll should cost. std::vector process_quest_list( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, From d887e93dfb7008d4fc5126af6dfe00e37eaeb467 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 17:32:33 -0700 Subject: [PATCH 36/50] Log one line naming the slot and quest for every BBQ reroll A reroll's decision was logged when the panel was planned ("Reroll selected. Rerolling quest."), with no slot or quest, and the reroll itself logged nothing until it was verified. With two rerolls planned, the second spent 10 BP ~30 s later with no line of its own (R5 12:45:40, 13:01:31), so log scans counted it as a reroll that was never selected. reroll_quest(), which every slot reroll goes through, now logs "Rerolling quest N (slug)." before it opens the panel, whatever the result. The slug comes from BBQuests_enum_to_string(), as in the quest reads, or is "unknown" for an undetected quest. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 18 +++++++++++++++++- .../Farming/PokemonSV_BlueberryQuests.cpp | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 03433af297..2c87ea1bc2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -75,6 +75,13 @@ class NullLogger : public Logger{ public: virtual void log(const std::string&, Color) override{} }; +class RecordingLogger : public Logger{ +public: + std::vector lines; + virtual void log(const std::string& line, Color) override{ + lines.push_back(line); + } +}; // A singleplayer quest panel that behaves like the game's: 3 blue quests, // and a completed quest is replaced by a new one. process_quest_list() @@ -536,9 +543,18 @@ class Test_BBQReroll : public UnitTest{ panel.out_of_bp = m_scenario.out_of_bp; panel.missed_reads = m_scenario.missed_reads; - NullLogger logger; + RecordingLogger logger; std::vector quests = m_scenario.quests; const BBQRerollResult result = reroll_quest(logger, panel, quests, m_scenario.slot); + //Each reroll logs one line naming its slot and quest, whatever the result. + const std::string reroll_line = "Rerolling quest " + std::to_string(m_scenario.slot + 1) + + " (" + BBQuests_enum_to_string(m_scenario.quests[m_scenario.slot]) + ")."; + const size_t reroll_lines = std::count_if(logger.lines.begin(), logger.lines.end(), [](const std::string& line){ + return line.starts_with("Rerolling quest "); + }); + if (reroll_lines != 1 || std::find(logger.lines.begin(), logger.lines.end(), reroll_line) == logger.lines.end()){ + return "Expected one log line \"" + reroll_line + "\", got " + std::to_string(reroll_lines) + " reroll lines."; + } if (result != m_scenario.expected){ return "Expected " + reroll_result_to_string(m_scenario.expected) + ", got " + reroll_result_to_string(result); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 091a6374f4..f61e9e6e7a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -296,6 +296,9 @@ BBQRerollResult reroll_quest( std::vector& quests, size_t slot ){ + //Every slot reroll logs this one line, whatever decided it, so a log shows each BP spent. + const std::string quest = slot < quests.size() ? BBQuests_enum_to_string(quests[slot]) : ""; + logger.log("Rerolling quest " + std::to_string(slot + 1) + " (" + (quest.empty() ? "unknown" : quest) + ")."); if (!actions.open_panel()){ return BBQRerollResult::PanelNotOpened; } From 7cf661bc011b25c159c818607e1180d1733b8c48 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 19:56:32 -0700 Subject: [PATCH 37/50] Log BBQ boards and quest attempts through BBQLogger BBQ failures are hard to attribute to a specific quest and attempt from the existing logs. Add one-line "BBQ ..." events: - board: panel detected and quest slugs in board order, each time read_quests() reads the panel - per attempt: start, route_return and result, with attempt/limit and route, verification and total durations - catch battle-entry timeout, and retry exhaustion BBQLogger depends only on Logger and the Options/PokemonSV_BBQuests leaf, so the quest and catch/photo modules call into it without a dependency back. Durations use explicit steady_clock timestamps so tests can check them without depending on wall-clock time. Add four PokemonSV::BBQDiagnosticLogs tests to PokemonSV_BBQTests.cpp, with a recording logger, covering board formatting, success, failure with retry exhaustion, and long/zero durations. Controller sequences, retry limits and reroll policy are unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQLogger.cpp | 94 +++++++++++++++++++ .../Programs/Farming/PokemonSV_BBQLogger.h | 73 ++++++++++++++ .../Programs/Farming/PokemonSV_BBQTests.cpp | 85 +++++++++++++++++ .../Farming/PokemonSV_BlueberryCatchPhoto.cpp | 2 + .../Farming/PokemonSV_BlueberryQuests.cpp | 8 ++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 264 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp new file mode 100644 index 0000000000..ce408705c0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp @@ -0,0 +1,94 @@ +/* BBQ Diagnostic Logger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/PrettyPrint.h" +#include "PokemonSV_BBQLogger.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +namespace{ + +constexpr std::string_view UNKNOWN = "unknown"; + +std::string_view quest_label(BBQuests quest){ + const std::string& slug = BBQuests_enum_to_string(quest); + return slug.empty() ? UNKNOWN : std::string_view(slug); +} + +void append_elapsed(std::ostream& out, std::string_view field, BBQAttemptLog::Clock::duration elapsed){ + const auto milliseconds = std::chrono::duration_cast(elapsed); + out << ' ' << field << "_ms=" << milliseconds.count() + << ' ' << field << "=\"" << duration_to_string(milliseconds) << '\"'; +} + +} + + +void BBQLogger::board(bool panel_detected, const std::vector& quests){ + std::ostringstream out; + out << "BBQ event=board panel_detected=" << panel_detected << " quests="; + bool first = true; + for (BBQuests quest : quests){ + if (!first) out << ','; + first = false; + out << quest_label(quest); + } + m_logger.log(out.str()); +} + +void BBQLogger::battle_entry_timeout(BBQuests quest){ + std::ostringstream out; + out << "BBQ quest=" << quest_label(quest) << " event=battle_entry outcome=timeout"; + m_logger.log(out.str()); +} + +void BBQLogger::retry_exhausted(BBQuests quest){ + std::ostringstream out; + out << "BBQ quest=" << quest_label(quest) << " event=retry_exhausted recovery=skip_current_batch"; + m_logger.log(out.str()); +} + + +BBQAttemptLog::BBQAttemptLog( + Logger& logger, BBQuests quest, + unsigned attempt, unsigned limit, + Clock::time_point start +) + : m_logger(logger), m_start(start), m_route_end(start) +{ + std::ostringstream out; + out << "BBQ quest=" << quest_label(quest) << " attempt=" << attempt << " limit=" << limit; + m_attempt_description = out.str(); + out << " event=start"; + m_logger.log(out.str()); +} + +void BBQAttemptLog::route_return(Clock::time_point end){ + m_route_end = end; + std::ostringstream out; + out << m_attempt_description << " event=route_return"; + append_elapsed(out, "elapsed", end - m_start); + m_logger.log(out.str()); +} + +void BBQAttemptLog::result(bool completed, Clock::time_point end){ + std::ostringstream out; + out << m_attempt_description << " event=result outcome=" + << (completed ? "absent_from_board" : "still_on_board"); + append_elapsed(out, "verification", end - m_route_end); + append_elapsed(out, "total", end - m_start); + m_logger.log(out.str()); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h new file mode 100644 index 0000000000..cd53ba7a06 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h @@ -0,0 +1,73 @@ +/* BBQ Diagnostic Logger + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQLogger_H +#define PokemonAutomation_PokemonSV_BBQLogger_H + +#include +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Formats one-line "BBQ ..." diagnostic events through an existing Logger. +// Does no image I/O and no controller input. +class BBQLogger{ +public: + explicit BBQLogger(Logger& logger) : m_logger(logger) {} + + // The quest board as read from one panel snapshot: whether the panel was + // detected, and the quests in board order. + void board(bool panel_detected, const std::vector& quests); + + // A catch quest gave up waiting for the battle menu after navigating to its target. + void battle_entry_timeout(BBQuests quest); + + // A quest used all of its attempts and the rest of the batch will be skipped. + void retry_exhausted(BBQuests quest); + +private: + Logger& m_logger; +}; + + +// The event sequence and monotonic timing of one quest attempt. +// Timestamps default to now; tests pass fixed values. +class BBQAttemptLog{ +public: + using Clock = std::chrono::steady_clock; + + // Logs the start of attempt number `attempt` out of `limit`. + BBQAttemptLog( + Logger& logger, BBQuests quest, + unsigned attempt, unsigned limit, + Clock::time_point start = Clock::now() + ); + + // The quest routine returned; logs the time since start. + void route_return(Clock::time_point end = Clock::now()); + + // The board was re-read. `completed` means the quest is no longer on it. + // Logs the verification time since route_return() and the total time. + void result(bool completed, Clock::time_point end = Clock::now()); + +private: + Logger& m_logger; + const Clock::time_point m_start; + Clock::time_point m_route_end; + std::string m_attempt_description; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 999661c313..e66ed40513 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonSV_BBQLogger.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQTests.h" @@ -66,8 +67,92 @@ static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ } +namespace{ + +class BBQTestLogger : public Logger{ +public: + virtual void log(const std::string& message, Color) override{ + messages.push_back(message); + } + std::vector messages; +}; + +class Test_BBQDiagnosticLogs : public UnitTest{ +public: + Test_BBQDiagnosticLogs(const std::string& scenario) + : UnitTest("PokemonSV::BBQDiagnosticLogs - " + scenario), m_scenario(scenario) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + BBQTestLogger logger; + std::vector expected; + const BBQAttemptLog::Clock::time_point start{}; + if (m_scenario == "board"){ + BBQLogger(logger).board(true, + {BBQuests::photo_swim, BBQuests::sneak_up, BBQuests::auto_10}); + BBQLogger(logger).board(false, {BBQuests::UnableToDetect}); + BBQLogger(logger).board(true, {}); + expected = { + "BBQ event=board panel_detected=1 quests=photo-swim,sneak-up,auto-10", + "BBQ event=board panel_detected=0 quests=unknown", + "BBQ event=board panel_detected=1 quests=", + }; + }else if (m_scenario == "success"){ + BBQAttemptLog attempt_log(logger, BBQuests::make_tm, 1, 2, start); + attempt_log.route_return(start + std::chrono::milliseconds(2000)); + attempt_log.result(true, start + std::chrono::milliseconds(2500)); + expected = { + "BBQ quest=make-tm attempt=1 limit=2 event=start", + "BBQ quest=make-tm attempt=1 limit=2 event=route_return elapsed_ms=2000 elapsed=\"2.000 seconds\"", + "BBQ quest=make-tm attempt=1 limit=2 event=result outcome=absent_from_board verification_ms=500 verification=\"0.500 seconds\" total_ms=2500 total=\"2.500 seconds\"", + }; + }else if (m_scenario == "retry-exhausted"){ + BBQLogger(logger).battle_entry_timeout(BBQuests::catch_any); + BBQAttemptLog attempt_log(logger, BBQuests::catch_any, 2, 2, start); + attempt_log.route_return(start + std::chrono::milliseconds(3345)); + attempt_log.result(false, start + std::chrono::milliseconds(4110)); + BBQLogger(logger).retry_exhausted(BBQuests::catch_any); + expected = { + "BBQ quest=catch-any event=battle_entry outcome=timeout", + "BBQ quest=catch-any attempt=2 limit=2 event=start", + "BBQ quest=catch-any attempt=2 limit=2 event=route_return elapsed_ms=3345 elapsed=\"3.345 seconds\"", + "BBQ quest=catch-any attempt=2 limit=2 event=result outcome=still_on_board verification_ms=765 verification=\"0.765 seconds\" total_ms=4110 total=\"4.110 seconds\"", + "BBQ quest=catch-any event=retry_exhausted recovery=skip_current_batch", + }; + }else if (m_scenario == "long-duration"){ + BBQAttemptLog attempt_log(logger, BBQuests::UnableToDetect, 256, 256, start); + attempt_log.route_return(start + std::chrono::milliseconds(120000)); + attempt_log.result(false, start + std::chrono::milliseconds(120000)); + expected = { + "BBQ quest=unknown attempt=256 limit=256 event=start", + "BBQ quest=unknown attempt=256 limit=256 event=route_return elapsed_ms=120000 elapsed=\"2.000 minutes\"", + "BBQ quest=unknown attempt=256 limit=256 event=result outcome=still_on_board verification_ms=0 verification=\"0.000 seconds\" total_ms=120000 total=\"2.000 minutes\"", + }; + }else{ + return "Unknown BBQ log test scenario."; + } + if (logger.messages != expected){ + return "Unexpected BBQ diagnostic log sequence for " + m_scenario; + } + return true; + } + +private: + const std::string m_scenario; +}; + +} + +static void add_tests_BBQDiagnosticLogs(UnitTestDatabase& database){ + for (const char* scenario : {"board", "success", "retry-exhausted", "long-duration"}){ + database.add(scenario); + } +} + + void add_tests_BBQ(UnitTestDatabase& database){ add_tests_TeraEncounterAction(database); + add_tests_BBQDiagnosticLogs(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp index 3a24e1d187..9ca5bf1dd3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp @@ -26,6 +26,7 @@ #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" #include "VideoGames/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" +#include "PokemonSV_BBQLogger.h" #include "PokemonSV_BlueberryCatchPhoto.h" namespace PokemonAutomation{ @@ -917,6 +918,7 @@ void quest_catch( ); if (ret2 != 0){ console.log("Did not enter battle. Did target spawn?"); + BBQLogger(console.logger()).battle_entry_timeout(current_quest); } }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 8bec917a63..c10d614eb8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -41,6 +41,7 @@ #include "VideoGames/PokemonSV/Programs/TeraRaids/PokemonSV_TeraBattler.h" #include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" +#include "PokemonSV_BBQLogger.h" #include "PokemonSV_BlueberryQuests.h" @@ -122,6 +123,8 @@ std::vector read_quests( quest_list.push_back(BBQuests_string_to_enum(fourth_quest)); } + BBQLogger(stream.logger()).board(result == 0, quest_list); + //Close quest list pbf_mash_button(context, BUTTON_B, 800ms); context.wait_for_all_requests(); @@ -321,6 +324,8 @@ bool process_and_do_quest( int quest_attempts = 0; while (!quest_completed){ + BBQAttemptLog attempt_log(console.logger(), current_quest, quest_attempts + 1, + (unsigned)BBQ_OPTIONS.NUM_RETRIES + 1); switch (current_quest){ case BBQuests::make_tm: quest_make_tm(env.program_info(), console, context); @@ -419,6 +424,7 @@ bool process_and_do_quest( break; } + attempt_log.route_return(); //Validate quest was completed by checking list 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()){ @@ -434,7 +440,9 @@ bool process_and_do_quest( quest_completed = true; } + attempt_log.result(quest_completed); if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ + BBQLogger(console.logger()).retry_exhausted(current_quest); console.log("Failed to complete a quest multiple times. Skipping it for now.", COLOR_RED); break; } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ce54742834..7709c22832 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2595,6 +2595,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp From cc69593049975f579de1f457b9611ead5a58fec4 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 22:17:45 -0700 Subject: [PATCH 38/50] Save local screenshots for failed BBQ quest attempts When a catch quest times out entering battle, or a quest is still on the panel after an attempt, the logs say what failed but not what the screen showed. Add save_bbq_snapshot(), which saves the current frame to DEBUG_PATH()/BBQ/ as --.png and logs a "BBQ event=snapshot" line with the outcome. Unlike dump_image(), it stays on the local machine and sends nothing to telemetry. Call it at the battle-entry timeout and at failed quest verification. Add PokemonSV::BBQSnapshot tests that save into a folder that does not exist yet and check the file, path and log line, and that check a save that cannot be written returns no path and logs saved=0. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSnapshot.cpp | 41 +++++++++ .../Programs/Farming/PokemonSV_BBQSnapshot.h | 38 ++++++++ .../Programs/Farming/PokemonSV_BBQTests.cpp | 92 +++++++++++++++++++ .../Farming/PokemonSV_BlueberryCatchPhoto.cpp | 5 + .../Farming/PokemonSV_BlueberryQuests.cpp | 5 + SerialPrograms/cmake/SourceFiles.cmake | 2 + 6 files changed, 183 insertions(+) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp new file mode 100644 index 0000000000..6afb46402c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp @@ -0,0 +1,41 @@ +/* BBQ Failure Snapshots + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/PrettyPrint.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "PokemonSV_BBQSnapshot.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +std::string bbq_snapshot_directory(){ + return DEBUG_PATH() + "BBQ/"; +} + +std::string save_bbq_snapshot( + Logger& logger, const ImageViewRGB32& frame, + const std::string& directory, BBQuests quest, std::string_view reason +){ + const std::string& slug = BBQuests_enum_to_string(quest); + std::ostringstream path; + path << directory << now_to_filestring() << '-' + << (slug.empty() ? "unknown" : slug) << '-' << reason << ".png"; + const std::string filename = path.str(); + + const bool saved = frame.save(filename); + std::ostringstream out; + out << "BBQ event=snapshot saved=" << saved << " path=" << filename; + logger.log(out.str()); + return saved ? filename : std::string(); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h new file mode 100644 index 0000000000..9c9b993e64 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h @@ -0,0 +1,38 @@ +/* BBQ Failure Snapshots + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQSnapshot_H +#define PokemonAutomation_PokemonSV_BBQSnapshot_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Where BBQ failure screenshots are saved: DEBUG_PATH()/BBQ/. +std::string bbq_snapshot_directory(); + +// Save a screenshot of a failed quest attempt into `directory` (created if +// missing) as --.png, and log the outcome as a +// "BBQ event=snapshot" line. Unlike dump_image(), this stays on the local +// machine and sends nothing to telemetry. +// Returns the path written, or an empty string if the image was not saved. +std::string save_bbq_snapshot( + Logger& logger, const ImageViewRGB32& frame, + const std::string& directory, BBQuests quest, std::string_view reason +); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index e66ed40513..f233316335 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -4,8 +4,13 @@ * */ +#include +#include +#include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" #include "PokemonSV_BBQLogger.h" +#include "PokemonSV_BBQSnapshot.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQTests.h" @@ -150,9 +155,96 @@ static void add_tests_BBQDiagnosticLogs(UnitTestDatabase& database){ } +namespace{ + +// A fresh folder under the system temp folder, removed afterwards. +class TempFolder{ +public: + TempFolder() + : path(std::filesystem::temp_directory_path() / ("bbq-snapshot-test-" + now_to_filestring())) + { + std::filesystem::create_directories(path); + } + ~TempFolder(){ + std::error_code error; + std::filesystem::remove_all(path, error); + } + const std::filesystem::path path; +}; + +ImageRGB32 small_frame(){ + ImageRGB32 frame(4, 4); + frame.fill(0xff00ff00); + return frame; +} + +class Test_BBQSnapshotSaved : public UnitTest{ +public: + Test_BBQSnapshotSaved() : UnitTest("PokemonSV::BBQSnapshot - saves into a new folder") {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + TempFolder temp; + const std::string directory = (temp.path / "BBQ").string() + "/"; + BBQTestLogger logger; + + const std::string path = save_bbq_snapshot( + logger, small_frame(), directory, BBQuests::make_tm, "verification-failed" + ); + + const std::string suffix = "-make-tm-verification-failed.png"; + if (path.rfind(directory, 0) != 0 || path.size() < suffix.size() + || path.compare(path.size() - suffix.size(), suffix.size(), suffix) != 0 + ){ + return "Unexpected snapshot path: " + path; + } + if (!std::filesystem::exists(path)){ + return "Snapshot was not written: " + path; + } + if (logger.messages != std::vector{"BBQ event=snapshot saved=1 path=" + path}){ + return "Unexpected snapshot log."; + } + return true; + } +}; + +class Test_BBQSnapshotFailed : public UnitTest{ +public: + Test_BBQSnapshotFailed() : UnitTest("PokemonSV::BBQSnapshot - reports a failed save") {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + // The folder cannot be created because a file already has its name. + TempFolder temp; + std::ofstream(temp.path / "BBQ") << "not a folder"; + const std::string directory = (temp.path / "BBQ").string() + "/"; + BBQTestLogger logger; + + const std::string path = save_bbq_snapshot( + logger, small_frame(), directory, BBQuests::make_tm, "verification-failed" + ); + + if (!path.empty()){ + return "Expected no path for a failed save, got: " + path; + } + const std::string expected_prefix = "BBQ event=snapshot saved=0 path=" + directory; + if (logger.messages.size() != 1 || logger.messages[0].rfind(expected_prefix, 0) != 0){ + return "Unexpected snapshot log."; + } + return true; + } +}; + +} + +static void add_tests_BBQSnapshot(UnitTestDatabase& database){ + database.add(); + database.add(); +} + + void add_tests_BBQ(UnitTestDatabase& database){ add_tests_TeraEncounterAction(database); add_tests_BBQDiagnosticLogs(database); + add_tests_BBQSnapshot(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp index 9ca5bf1dd3..d20ee60d53 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp @@ -27,6 +27,7 @@ #include "VideoGames/PokemonSV/Programs/Battles/PokemonSV_BasicCatcher.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" #include "PokemonSV_BBQLogger.h" +#include "PokemonSV_BBQSnapshot.h" #include "PokemonSV_BlueberryCatchPhoto.h" namespace PokemonAutomation{ @@ -919,6 +920,10 @@ void quest_catch( if (ret2 != 0){ console.log("Did not enter battle. Did target spawn?"); BBQLogger(console.logger()).battle_entry_timeout(current_quest); + save_bbq_snapshot( + console.logger(), console.video().snapshot(), + bbq_snapshot_directory(), current_quest, "battle-entry-timeout" + ); } }, { diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index c10d614eb8..2921ada7d2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -42,6 +42,7 @@ #include "VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h" #include "PokemonSV_BBQLogger.h" +#include "PokemonSV_BBQSnapshot.h" #include "PokemonSV_BlueberryQuests.h" @@ -429,6 +430,10 @@ bool process_and_do_quest( 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."); + save_bbq_snapshot( + console.logger(), console.video().snapshot(), + bbq_snapshot_directory(), current_quest, "verification-failed" + ); quest_attempts++; }else{ console.log("Current quest was not found. Quest completed!"); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7709c22832..dabac28086 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2597,6 +2597,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSnapshot.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp From dcd1f2afc6c2f117051a71796ddf6cdb128b2421 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 18:30:38 -0700 Subject: [PATCH 39/50] BBQ Farmer: reuse complete reroll observations before new game actions After a reroll, reuse a complete verified four-slot board for scheduling and the first dispatch. Discard the old work batch after planner rerolls; use fresh reads after quest actions and for incomplete or inconsistent observations. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 33 +++++++- .../Farming/PokemonSV_BBQSoloFarmer.h | 6 +- .../Programs/Farming/PokemonSV_BBQTests.cpp | 82 +++++++++++++++++-- .../Farming/PokemonSV_BlueberryQuests.cpp | 31 +++++-- .../Farming/PokemonSV_BlueberryQuests.h | 19 +++-- 5 files changed, 147 insertions(+), 24 deletions(-) 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 569af8ee21..529627eecb 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -105,6 +105,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; @@ -115,7 +116,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"); @@ -125,12 +126,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"); @@ -207,6 +209,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; }; @@ -244,6 +247,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{ @@ -289,6 +293,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 @@ -547,6 +567,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; @@ -586,7 +607,7 @@ class SimulatedQuestPanel : public BBQRerollActions{ missed_reads--; return std::vector(quests.size(), BBQuests::UnableToDetect); } - return quests; + return read_override.value_or(quests); } }; @@ -600,9 +621,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){ @@ -631,10 +654,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); @@ -647,6 +672,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."; } @@ -733,6 +762,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}, @@ -753,6 +823,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", @@ -762,6 +833,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 091a6374f4..06776342c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -257,6 +257,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); } @@ -294,8 +295,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; } @@ -332,13 +335,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, @@ -346,9 +355,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, @@ -405,7 +415,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, @@ -416,8 +426,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."); @@ -437,7 +448,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( @@ -550,6 +566,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, From 09982bb45a58708ad4aaf3ec0da73ecd32554ec0 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 21:01:07 -0700 Subject: [PATCH 40/50] BBQ Farmer: replan after rerolling an exhausted quest After an exhausted quest is rerolled, end the current batch and replan before continuing old queued work, so a replacement such as Travel 500m enters the next plan instead of waiting behind the old queue. Normal date and save handling still runs at the end of the shortened round. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 8 +++ .../Programs/Farming/PokemonSV_BBQTests.cpp | 60 +++++++++++++++++-- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 46d2d349d4..715dee88a7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -205,6 +205,7 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga //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; + bool replan_after_reroll = false; for (auto current_quest : quests_to_do){ //Check if quest was already completed (ex. 500 meters completed while navigating to take a photo) if (dispatch_panel){ @@ -231,6 +232,8 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga case BBQOption::OutOfRetries::Reroll: logger.log("Reroll selected. Rerolling the failed quest."); game.reroll_quest(current_quest); + logger.log("Reroll finished. Discarding the remaining batch before scheduling."); + replan_after_reroll = true; break; case BBQOption::OutOfRetries::Stop: logger.log("Stop selected. Stopping program.", COLOR_RED); @@ -244,6 +247,11 @@ void run_bbq_rounds(Logger& logger, const BBQOption& options, BBQGameActions& ga num_completed_quests++; } quest_list.clear(); + if (replan_after_reroll){ + //A replacement belongs in the next plan, before more old queued + //work. Finish normal round actions so save milestones are kept. + break; + } } //Clear out the todo list quests_to_do.clear(); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 529627eecb..d3471c4d14 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -108,6 +108,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ bool reuse_rerolled_panel = false; std::vector events; + std::vector attempted_quests; virtual std::vector read_quests() override{ events.push_back("read"); @@ -136,6 +137,7 @@ class SimulatedBBQGameActions : public BBQGameActions{ } virtual bool do_quest(BBQuests quest, uint8_t&) override{ events.push_back("do"); + attempted_quests.push_back(quest); const bool success = !never_completes.contains(quest) && (m_next_outcome < outcomes.size() ? outcomes[m_next_outcome++] : true); if (success){ @@ -211,6 +213,7 @@ struct BBQRoundsScenario{ BBQOption::OutOfRetries out_of_retries = BBQOption::OutOfRetries::Skip; bool reuse_rerolled_panel = false; uint64_t expected_completed = 0; + std::vector expected_attempted = {}; std::string expected; }; @@ -262,6 +265,18 @@ class Test_BBQFarmerRounds : public UnitTest{ return "Expected " + std::to_string(m_scenario.expected_completed) + " completed quests, counted " + std::to_string(completed); } + if (!m_scenario.expected_attempted.empty() && game.attempted_quests != m_scenario.expected_attempted){ + std::vector expected_quests; + std::vector actual_quests; + for (BBQuests quest : m_scenario.expected_attempted){ + expected_quests.push_back(std::to_string((int)quest)); + } + for (BBQuests quest : game.attempted_quests){ + actual_quests.push_back(std::to_string((int)quest)); + } + return "Expected quest order: " + join_events(expected_quests) + + "\nActual quest order: " + join_events(actual_quests); + } const std::string actual = join_events(game.events); if (actual != m_scenario.expected){ return "Expected: " + m_scenario.expected + "\nActual: " + actual; @@ -293,6 +308,41 @@ 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 = "failed reroll replans before queued photo and TM", + .num_quests = 1, + .panel = {BBQuests::catch_grass, BBQuests::photo_savanna, BBQuests::make_tm}, + .outcomes = {false}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 3, + .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm}, + .expected = "read process read do reroll end " + round_of_3 + " end", + }, + { + .name = "failed reroll on full board reads fresh after round actions", + .num_quests = 1, + .panel = {BBQuests::catch_grass, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, + .outcomes = {false}, + .new_quests = {BBQuests::travel_500, BBQuests::photo_canyon, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .reuse_rerolled_panel = true, + .expected_completed = 4, + .expected_attempted = {BBQuests::catch_grass, BBQuests::travel_500, BBQuests::photo_savanna, BBQuests::make_tm, BBQuests::wash_pokemon}, + .expected = "read process read do reroll end " + round_of_3 + " read do completed end", + }, + { + .name = "replanning after failure preserves a crossed save milestone", + .num_quests = 2, + .save_num_quests = 1, + .panel = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_savanna}, + .outcomes = {true, false}, + .new_quests = {BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_coastal, BBQuests::photo_polar}, + .out_of_retries = BBQOption::OutOfRetries::Reroll, + .expected_completed = 4, + .expected_attempted = {BBQuests::make_tm, BBQuests::catch_grass, BBQuests::photo_canyon, BBQuests::travel_500, BBQuests::photo_savanna}, + .expected = "read process read do completed read do reroll end(save) " + round_of_3 + " end(save)", + }, { .name = "reuses reroll observation for selection and first dispatch only", .num_quests = 1, @@ -398,17 +448,17 @@ static void add_tests_BBQFarmerRounds(UnitTestDatabase& database){ .expected = round_of_3 + " end(save) " + round_of_3 + " end(save)", }, { - // Round 1 completes nothing. It must not save, even though the - // count (0) is a multiple of the interval. - // Every quest fails and is rerolled, so round 1 completes - // nothing and doesn't save; round 2 reaches the milestone. + // Three partial rounds each fail and reroll their first quest. + // None may save while the completed count is still zero. .name = "round without completions does not save", .num_quests = 3, .save_num_quests = 2, .outcomes = {false, false, false}, .out_of_retries = BBQOption::OutOfRetries::Reroll, .expected_completed = 3, - .expected = "read process read do reroll read do reroll read do reroll end " + round_of_3 + " end(save)", + .expected = + "read process read do reroll end read process read do reroll end " + "read process read do reroll end " + round_of_3 + " end(save)", }, { // sneak-up fails, then leaves the panel along with make-tm. Round 2 From daa1de34f820ff5feb1e34a266014dc246b7a067 Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 26 Oct 2026 22:51:24 -0700 Subject: [PATCH 41/50] PokemonSV: move the BBQ farmer's tests into their own file PokemonSV_Tests.cpp registers every SV test, and each BBQ change adds its tests there, so parallel BBQ changes all conflict in it. Move the BBQ tests unchanged into Programs/Farming/PokemonSV_BBQTests.cpp, registered through one add_tests_BBQ() call, following the add_tests_() pattern the detectors use: the Tera encounter tests, the quest slug round trip and the Blueberry Points reader tests. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 129 +--------------- .../Programs/Farming/PokemonSV_BBQTests.cpp | 142 ++++++++++++++++++ .../Programs/Farming/PokemonSV_BBQTests.h | 26 ++++ SerialPrograms/cmake/SourceFiles.cmake | 2 + 4 files changed, 172 insertions(+), 127 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 02e94b05c7..ca43d5a46c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -25,139 +25,14 @@ #include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h" #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" -#include "Options/PokemonSV_BBQuests.h" -#include "Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "Programs/Farming/PokemonSV_BBQTests.h" #include "PokemonSV_Tests.h" -#include "CommonFramework/GlobalAutoPaths.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Inference/PokemonSV_BlueberryPointsReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ -namespace{ - -struct TeraEncounterScenario{ - std::string name; - bool shiny = false; - int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first - bool battle_menu_detected = false; // The route's own wait saw the battle menu. - TeraEncounterAction expected; -}; - -class Test_TeraEncounterAction : public UnitTest{ -public: - Test_TeraEncounterAction(TeraEncounterScenario scenario) - : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const TeraEncounterAction actual = tera_encounter_action( - m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected - ); - if (actual != m_scenario.expected){ - return "Expected action " + std::to_string((int)m_scenario.expected) - + ", got " + std::to_string((int)actual) + "."; - } - return true; - } - -private: - const TeraEncounterScenario m_scenario; -}; - -} - -void add_tests_TeraEncounterAction(UnitTestDatabase& database){ - using enum TeraEncounterAction; - const std::vector scenarios{ - {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, - {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, - {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, - {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, - {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, - {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, - {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, - {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, - {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, - }; - for (const TeraEncounterScenario& scenario : scenarios){ - database.add(scenario); - } -} - -namespace{ - -class Test_BBQuestsSlugs : public UnitTest{ -public: - Test_BBQuestsSlugs() - : UnitTest("PokemonSV::BBQuests - slug round trip") - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - for (int c = 0; c < (int)BBQuests::UnableToDetect; c++){ - const BBQuests quest = (BBQuests)c; - const std::string& slug = BBQuests_enum_to_string(quest); - if (slug.empty()){ - return "Quest " + std::to_string(c) + " has no slug."; - } - if (BBQuests_string_to_enum(slug) != quest){ - return "Slug " + slug + " does not map back to quest " + std::to_string(c) + "."; - } - } - if (!BBQuests_enum_to_string(BBQuests::UnableToDetect).empty()){ - return "UnableToDetect must map to the empty slug."; - } - if (BBQuests_string_to_enum("not-a-quest") != BBQuests::UnableToDetect){ - return "An unrecognized slug must map to UnableToDetect."; - } - return true; - } -}; - -} - -namespace{ - -class Test_BlueberryPoints : public UnitTest{ -public: - Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) - : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) - , m_image(UNIT_TEST_RESOURCE_PATH() + image) - , m_expected(expected), m_width(width), m_height(height) - {} - - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - ImageRGB32 reference(m_image); - ImageRGB32 image = reference.scale_to(m_width, m_height); - const int actual = read_BP_from_image(logger, image); - if (actual != m_expected){ - return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); - } - return true; - } - -private: - const std::string m_image; - const int m_expected; - const size_t m_width, m_height; -}; - -} - void add_tests(UnitTestDatabase& database){ - database.add(); - - // Full-frame references, in the CommandLineTests repo: they exercise the production - // crop, filter and OCR together. - database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); - database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); - - database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); - database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); - add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); @@ -178,7 +53,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_TeraTypeReader(database); add_tests_NormalBattleMenus(database); add_tests_LetsGoKillDetector(database); - add_tests_TeraEncounterAction(database); + add_tests_BBQ(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp new file mode 100644 index 0000000000..772ddfa2e9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -0,0 +1,142 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" +#include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" +#include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQTests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +namespace{ + +class Test_BBQuestsSlugs : public UnitTest{ +public: + Test_BBQuestsSlugs() + : UnitTest("PokemonSV::BBQuests - slug round trip") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (int c = 0; c < (int)BBQuests::UnableToDetect; c++){ + const BBQuests quest = (BBQuests)c; + const std::string& slug = BBQuests_enum_to_string(quest); + if (slug.empty()){ + return "Quest " + std::to_string(c) + " has no slug."; + } + if (BBQuests_string_to_enum(slug) != quest){ + return "Slug " + slug + " does not map back to quest " + std::to_string(c) + "."; + } + } + if (!BBQuests_enum_to_string(BBQuests::UnableToDetect).empty()){ + return "UnableToDetect must map to the empty slug."; + } + if (BBQuests_string_to_enum("not-a-quest") != BBQuests::UnableToDetect){ + return "An unrecognized slug must map to UnableToDetect."; + } + return true; + } +}; + +class Test_BlueberryPoints : public UnitTest{ +public: + Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) + : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_expected(expected), m_width(width), m_height(height) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(m_image); + ImageRGB32 image = reference.scale_to(m_width, m_height); + const int actual = read_BP_from_image(logger, image); + if (actual != m_expected){ + return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); + } + return true; + } + +private: + const std::string m_image; + const int m_expected; + const size_t m_width, m_height; +}; + +struct TeraEncounterScenario{ + std::string name; + bool shiny = false; + int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first + bool battle_menu_detected = false; // The route's own wait saw the battle menu. + TeraEncounterAction expected; +}; + +class Test_TeraEncounterAction : public UnitTest{ +public: + Test_TeraEncounterAction(TeraEncounterScenario scenario) + : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TeraEncounterAction actual = tera_encounter_action( + m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected + ); + if (actual != m_scenario.expected){ + return "Expected action " + std::to_string((int)m_scenario.expected) + + ", got " + std::to_string((int)actual) + "."; + } + return true; + } + +private: + const TeraEncounterScenario m_scenario; +}; + +} + +static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ + using enum TeraEncounterAction; + const std::vector scenarios{ + {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, + {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, + {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, + {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, + {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, + {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, + {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, + {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, + {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, + }; + for (const TeraEncounterScenario& scenario : scenarios){ + database.add(scenario); + } +} + + +void add_tests_BBQ(UnitTestDatabase& database){ + database.add(); + + // Full-frame references, in the CommandLineTests repo: they exercise the production + // crop, filter and OCR together. + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); + + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); + + add_tests_TeraEncounterAction(database); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h new file mode 100644 index 0000000000..64317f45aa --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h @@ -0,0 +1,26 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQTests_H +#define PokemonAutomation_PokemonSV_BBQTests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + + +// Blueberry Quest farmer tests: controller decisions run against scripted inputs. +void add_tests_BBQ(UnitTestDatabase& database); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 80752da16d..9ca5d6ba98 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2599,6 +2599,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp From 21ea2e22fd837429664c4cf1e29702d0adcc1e08 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 22:17:34 -0700 Subject: [PATCH 42/50] BBQ Farmer: fail the egg quest when the box has no egg, and check for one at start With no egg in the current box, quest_hatch_egg() logged "No egg found" and returned with the game still in the box system. The farmer's next quest-panel read then pressed right from the box, read the wrong screen and either counted the quest as done or stopped the program on an unreadable quest. Two Switch runs ended this way. The egg scan is now find_egg_in_box(). When it finds nothing, the quest leaves the box and returns to Central Plaza like a finished attempt, so the panel check sees the quest still there and the usual retry, reroll or stop handling applies. Before the first round, the farmer also checks its setup: - It presses B once and waits. The Switch dims the screen after a few idle minutes, and a dimmed overworld is not detected, so a farmer started after a break stopped within 10 seconds. - With Number of eggs above 0, box_has_egg() opens the box once; if there is no egg it stops with a setup error that names the setting and says how to fix it: put eggs in the current box, or set Number of Eggs to 0 and choose Reroll or Keep Going in Zero Eggs (with Stop, the default, the farmer would stop at once). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Farming/PokemonSV_BBQSoloFarmer.cpp | 21 ++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 64 +++++++++++++------ .../Farming/PokemonSV_BlueberryQuests.h | 3 + 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 84fd26495c..fba6815df5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -298,6 +298,25 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ EventNotificationOption& m_status_update; }; +//Checks before the first round, so that a setup problem stops the program at once instead of +//in the middle of a quest. +void check_before_start(SingleSwitchProgramEnvironment& env, ProControllerContext& context, const BBQOption& options){ + //The Switch dims the screen after a few idle minutes, which hides the overworld. Any button + //wakes it, and B is harmless on the overworld. + pbf_press_button(context, BUTTON_B, 160ms, 1000ms); + context.wait_for_all_requests(); + + if (options.NUM_EGGS > 0 && !box_has_egg(env.program_info(), env.console, context)){ + throw UserSetupError( + env.console, + "\"Hatch Quest - Number of Eggs\" is " + std::to_string((int)options.NUM_EGGS) + + ", but the current box (the one the box system opens on) has no egg. " + "Put eggs in that box, or set it to 0 and choose Reroll or Keep Going in " + "\"Hatch Quest - Zero Eggs\"." + ); + } +} + } void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ @@ -313,6 +332,8 @@ void BBQSoloFarmer::program(SingleSwitchProgramEnvironment& env, ProControllerCo // Connect the controller. require_player(env.console, context, BUTTON_LCLICK); + + check_before_start(env, context, BBQ_OPTIONS); //Fly to plaza open_map_from_overworld(env.program_info(), env.console, context); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 83145f1e77..eac14a6f91 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -1178,6 +1178,43 @@ void quest_wash_pokemon(const ProgramInfo& info, VideoStream& stream, ProControl context.wait_for_all_requests(); } +namespace{ + +//Look through the current box for an egg, from the box system. Leaves the cursor on the egg and +//returns its slot, or returns false if the box has none. +bool find_egg_in_box( + const ProgramInfo& info, ConsoleHandle& console, ProControllerContext& context, + uint8_t& row, uint8_t& col +){ + BoxCurrentEggDetector egg_detector; + SomethingInBoxSlotDetector sth_in_box_detector(COLOR_RED); + for (row = 0; row < 5; row++){ + for (uint8_t j_col = 0; j_col < 6; j_col++){ + col = (row % 2 == 0 ? j_col : 5 - j_col); + move_box_cursor(info, console, context, BoxCursorLocation::SLOTS, row, col); + context.wait_for_all_requests(); + auto snapshot = console.video().snapshot(); + if (sth_in_box_detector.detect(snapshot) && egg_detector.detect(snapshot)){ + console.log("Found egg."); + return true; + } + } + } + return false; +} + +} + +bool box_has_egg(const ProgramInfo& info, ConsoleHandle& console, ProControllerContext& context){ + enter_box_system_from_overworld(info, console, context); + context.wait_for(std::chrono::milliseconds(400)); + uint8_t row = 0; + uint8_t col = 0; + const bool found = find_egg_in_box(info, console, context, row, col); + leave_box_system_to_overworld(info, console, context); + return found; +} + void quest_hatch_egg( ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, @@ -1210,30 +1247,15 @@ void quest_hatch_egg( context.wait_for(std::chrono::milliseconds(400)); //move_to_left_box(context); - BoxCurrentEggDetector egg_detector; - SomethingInBoxSlotDetector sth_in_box_detector(COLOR_RED); - bool egg_found = false; uint8_t row = 0; uint8_t col = 0; - for ( ; row < 5; row++){ - for (uint8_t j_col = 0; j_col < 6; j_col++){ - col = (row % 2 == 0 ? j_col : 5 - j_col); - move_box_cursor(env.program_info(), console, context, BoxCursorLocation::SLOTS, row, col); - context.wait_for_all_requests(); - auto snapshot = console.video().snapshot(); - if (sth_in_box_detector.detect(snapshot) && egg_detector.detect(snapshot)){ - console.log("Found egg."); - egg_found = true; - break; - } - } - if (egg_found){ - break; - } - } - - if (!egg_found){ + if (!find_egg_in_box(env.program_info(), console, context, row, col)){ + //Leave the box and go back, so the quest fails instead of leaving the game in the box. console.log("No egg found during egg hatching quest!", COLOR_RED); + leave_box_system_to_overworld(env.program_info(), console, context); + pbf_press_button(context, BUTTON_PLUS, 160ms, 400ms); + return_to_plaza(env.program_info(), console, context); + context.wait_for_all_requests(); }else{ swap_two_box_slots(env.program_info(), console, context, BoxCursorLocation::SLOTS, row, col, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index f3f91cd04d..64032f11c5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -211,6 +211,9 @@ void quest_wash_pokemon( VideoStream& stream, ProControllerContext& context ); +//Open the box system from the overworld and check that the current box has an egg. +bool box_has_egg(const ProgramInfo& info, ConsoleHandle& console, ProControllerContext& context); + //Withdraw and hatch an egg void quest_hatch_egg( ProgramEnvironment& env, From 1b359317cc613a6420350db4e95c53623bb84d53 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 29 Oct 2026 22:33:45 -0700 Subject: [PATCH 43/50] BBQ tests: default-initialize the quest plan scenario fields again Windows Clang builds with -Werror,-Wmissing-designated-field-initializers, and the scenarios leave these fields out. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 4986bf3c39..b4f65e35ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -432,12 +432,12 @@ namespace{ struct BBQuestPlanScenario{ std::string name; std::vector quests; - std::vector> extra_exclusions; + 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; + std::vector expected_to_do = {}; + std::vector expected_reroll_slots = {}; bool expected_reroll_all = false; }; From ea7728a04ab09c5563c687ecec7812a3cd55ec17 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 30 Oct 2026 08:08:04 -0700 Subject: [PATCH 44/50] BBQ Farmer: throw after the reroll result switch in reroll_quest_slot() Every case of the switch calls the [[noreturn]] fire(), but GCC does not treat a switch over an enum as exhaustive, so it reports "control reaches end of non-void function" (-Werror=return-type) and the Ubuntu build fails. Throw InternalProgramError after the switch, as other code does for an out-of-range enum. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 9d2b9e7466..9d7576046a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -375,6 +375,7 @@ std::optional> reroll_quest_slot( stream ); } + throw InternalProgramError(&stream.logger(), PA_CURRENT_FUNCTION, "Invalid enum."); } BBQuestPlan plan_quests( From 57002fb3aeb785f7cd5c4146f23f30dfe8247bd9 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 14:35:46 -0800 Subject: [PATCH 45/50] Initialize the default expected log color for Windows Clang --- .../PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp index 01728d86c2..a250b58cff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQLogger_Tests.cpp @@ -56,7 +56,7 @@ struct DiagnosticLogInput{ // Exact message order is part of the diagnostics contract. struct DiagnosticLogExpectation{ std::vector messages; - Color final_color; + Color final_color{}; }; // Each case keeps all inputs and expected output visible together. From dbc3c7430bffe1b81a8995551c5c06fee9733aee Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 15:20:49 -0800 Subject: [PATCH 46/50] Separate BBQ quest dispatch and retry execution from quest implementations --- .../Farming/PokemonSV_BlueberryQuests.cpp | 132 ----------- .../PokemonSV_BlueberryQuests_Execution.cpp | 207 ++++++++++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 1 + 3 files changed, 208 insertions(+), 132 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 2861283c87..a7e00414fa 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -436,138 +436,6 @@ std::vector process_quest_list( return plan.to_do; } -bool process_and_do_quest( - ProgramEnvironment& env, - ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, - BBQuests current_quest, - uint8_t& eggs_hatched -){ - bool quest_completed = false; - int quest_attempts = 0; - - while (!quest_completed){ - switch (current_quest){ - case BBQuests::make_tm: - quest_make_tm(env.program_info(), console, context); - break; - case BBQuests::travel_500: - quest_travel_500(env.program_info(), console, context); - break; - case BBQuests::tera_self_defeat: - quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::sneak_up: - quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::wild_tera: - quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::wash_pokemon: - quest_wash_pokemon(env.program_info(), console, context); - break; - case BBQuests::hatch_egg: - quest_hatch_egg(env, console, context, BBQ_OPTIONS); - break; - case BBQuests::bitter_sandwich: - case BBQuests::salty_sandwich: - case BBQuests::sour_sandwich: - case BBQuests::spicy_sandwich: - case BBQuests::sweet_sandwich: - case BBQuests::sandwich_three: - quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); - break; - //case BBQuests::pickup_10: - // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); - // break; - case BBQuests::tera_raid: - quest_tera_raid(env, console, context, BBQ_OPTIONS); - break; - case BBQuests::auto_10: - case BBQuests::auto_30: - quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); - break; - //All involve taking pictures - case BBQuests::photo_fly: - case BBQuests::photo_swim: - case BBQuests::photo_canyon: - case BBQuests::photo_coastal: - case BBQuests::photo_polar: - case BBQuests::photo_savanna: - case BBQuests::photo_normal: - case BBQuests::photo_fighting: - case BBQuests::photo_flying: - case BBQuests::photo_poison: - case BBQuests::photo_ground: - case BBQuests::photo_rock: - case BBQuests::photo_bug: - case BBQuests::photo_ghost: - case BBQuests::photo_steel: - case BBQuests::photo_fire: - case BBQuests::photo_water: - case BBQuests::photo_grass: - case BBQuests::photo_electric: - case BBQuests::photo_psychic: - case BBQuests::photo_ice: - case BBQuests::photo_dragon: - case BBQuests::photo_dark: - case BBQuests::photo_fairy: - quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - break; - //All involve catching a pokemon - case BBQuests::catch_any: - case BBQuests::catch_normal: - case BBQuests::catch_fighting: - case BBQuests::catch_flying: - case BBQuests::catch_poison: - case BBQuests::catch_ground: - case BBQuests::catch_rock: - case BBQuests::catch_bug: - case BBQuests::catch_ghost: - case BBQuests::catch_steel: - case BBQuests::catch_fire: - case BBQuests::catch_water: - case BBQuests::catch_grass: - case BBQuests::catch_electric: - case BBQuests::catch_psychic: - case BBQuests::catch_ice: - case BBQuests::catch_dragon: - case BBQuests::catch_dark: - case BBQuests::catch_fairy: - quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - break; - default: - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "Unknown quest selection.", - console - ); - break; - } - - //Validate quest was completed by checking list - 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."); - quest_attempts++; - }else{ - console.log("Current quest was not found. Quest completed!"); - if (current_quest == BBQuests::hatch_egg){ - eggs_hatched++; - console.log("Eggs hatched: " + std::to_string(eggs_hatched)); - } - - quest_completed = true; - } - - if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ - console.log("Failed to complete a quest multiple times. Skipping it for now.", COLOR_RED); - break; - } - } - return quest_completed; -} - void quest_make_tm(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ stream.log("Quest: Make TM"); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp new file mode 100644 index 0000000000..ecd041fc05 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp @@ -0,0 +1,207 @@ +/* Blueberry Quest Execution + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "PokemonSV_BlueberryCatchPhoto.h" +#include "PokemonSV_BlueberryQuests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ +namespace{ + +// Returns true after executing one supported quest; false performs no action. +bool run_basic_quest( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, BBQuests current_quest +){ + switch (current_quest){ + case BBQuests::make_tm: + quest_make_tm(env.program_info(), console, context); + return true; + case BBQuests::travel_500: + quest_travel_500(env.program_info(), console, context); + return true; + case BBQuests::tera_self_defeat: + quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); + return true; + case BBQuests::sneak_up: + quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); + return true; + case BBQuests::wild_tera: + quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); + return true; + case BBQuests::wash_pokemon: + quest_wash_pokemon(env.program_info(), console, context); + return true; + default: + return false; + } +} + +// Returns true after executing one supported quest; false performs no action. +bool run_event_quest( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, BBQuests current_quest +){ + switch (current_quest){ + case BBQuests::hatch_egg: + quest_hatch_egg(env, console, context, BBQ_OPTIONS); + return true; + case BBQuests::bitter_sandwich: + case BBQuests::salty_sandwich: + case BBQuests::sour_sandwich: + case BBQuests::spicy_sandwich: + case BBQuests::sweet_sandwich: + case BBQuests::sandwich_three: + quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); + return true; + //case BBQuests::pickup_10: + // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); + // break; + case BBQuests::tera_raid: + quest_tera_raid(env, console, context, BBQ_OPTIONS); + return true; + case BBQuests::auto_10: + case BBQuests::auto_30: + quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); + return true; + default: + return false; + } +} + +// Returns true after executing one supported quest; false performs no action. +bool run_photo_quest( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, BBQuests current_quest +){ + switch (current_quest){ + //All involve taking pictures + case BBQuests::photo_fly: + case BBQuests::photo_swim: + case BBQuests::photo_canyon: + case BBQuests::photo_coastal: + case BBQuests::photo_polar: + case BBQuests::photo_savanna: + case BBQuests::photo_normal: + case BBQuests::photo_fighting: + case BBQuests::photo_flying: + case BBQuests::photo_poison: + case BBQuests::photo_ground: + case BBQuests::photo_rock: + case BBQuests::photo_bug: + case BBQuests::photo_ghost: + case BBQuests::photo_steel: + case BBQuests::photo_fire: + case BBQuests::photo_water: + case BBQuests::photo_grass: + case BBQuests::photo_electric: + case BBQuests::photo_psychic: + case BBQuests::photo_ice: + case BBQuests::photo_dragon: + case BBQuests::photo_dark: + case BBQuests::photo_fairy: + quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + return true; + default: + return false; + } +} + +// Returns true after executing one supported quest; false performs no action. +bool run_catch_quest( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, BBQuests current_quest +){ + switch (current_quest){ + //All involve catching a pokemon + case BBQuests::catch_any: + case BBQuests::catch_normal: + case BBQuests::catch_fighting: + case BBQuests::catch_flying: + case BBQuests::catch_poison: + case BBQuests::catch_ground: + case BBQuests::catch_rock: + case BBQuests::catch_bug: + case BBQuests::catch_ghost: + case BBQuests::catch_steel: + case BBQuests::catch_fire: + case BBQuests::catch_water: + case BBQuests::catch_grass: + case BBQuests::catch_electric: + case BBQuests::catch_psychic: + case BBQuests::catch_ice: + case BBQuests::catch_dragon: + case BBQuests::catch_dark: + case BBQuests::catch_fairy: + quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + return true; + default: + return false; + } +} + +// Dispatch to the same quest routines as the original switch, or report an unsupported quest. +void execute_quest( + ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, BBQuests current_quest +){ + if (run_basic_quest(env, console, context, BBQ_OPTIONS, current_quest) + || run_event_quest(env, console, context, BBQ_OPTIONS, current_quest) + || run_photo_quest(env, console, context, BBQ_OPTIONS, current_quest) + || run_catch_quest(env, console, context, BBQ_OPTIONS, current_quest) + ){ + return; + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, "Unknown quest selection.", console + ); +} + +} // namespace + +// Execute/retry from the overworld, verify the board and preserve the caller's egg count. +bool process_and_do_quest( + ProgramEnvironment& env, + ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, + BBQuests current_quest, + uint8_t& eggs_hatched +){ + bool quest_completed = false; + int quest_attempts = 0; + + while (!quest_completed){ + execute_quest(env, console, context, BBQ_OPTIONS, current_quest); + + //Validate quest was completed by checking list + 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."); + quest_attempts++; + }else{ + console.log("Current quest was not found. Quest completed!"); + if (current_quest == BBQuests::hatch_egg){ + eggs_hatched++; + console.log("Eggs hatched: " + std::to_string(eggs_hatched)); + } + + quest_completed = true; + } + + if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ + console.log("Failed to complete a quest multiple times. Skipping it for now.", COLOR_RED); + break; + } + } + return quest_completed; +} + +} // namespace PokemonSV +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 32c8dbf362..16669564bd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2648,6 +2648,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_ClaimMysteryGift.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_ClaimMysteryGift.h From be6feea3204c5056dca313e8cb3b1c56adb64031 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 15:21:15 -0800 Subject: [PATCH 47/50] Include the environment definition used by the extracted dispatcher --- .../Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp index ecd041fc05..1c9a478926 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp @@ -6,6 +6,7 @@ #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/Tools/ProgramEnvironment.h" #include "PokemonSV_BlueberryCatchPhoto.h" #include "PokemonSV_BlueberryQuests.h" From 490c83cfae8dd82af0311b7362cad81e4a03d16f Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 15:28:27 -0800 Subject: [PATCH 48/50] Place the panel-result explanation beside the quest reader --- .../PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index b7cf04869d..1b9924ec9f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -62,7 +62,7 @@ int read_BP(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context.wait_for_all_requests(); } }, - {{ right_panel }} // Detector index 0; a negative run_until result means no detection. + {{ right_panel }} ); if (result == 0){ stream.log("Found quest panel."); @@ -103,6 +103,7 @@ bool open_quest_panel(VideoStream& stream, ProControllerContext& context){ } +// Detector index 0 confirms the quest panel; a negative run_until result means no detection. std::vector read_quests( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, From 722af5ac25a9e0ccf9fba3067a8901cf4142bb13 Mon Sep 17 00:00:00 2001 From: dayfine Date: Fri, 6 Nov 2026 16:02:18 -0800 Subject: [PATCH 49/50] Undo linter-driven quest execution split and retain the existing dispatcher Keep the logging-only scope requested in review. The user explicitly directed retaining and ignoring the existing legacy size: process_and_do_quest grows from 131 to 135 lines, and its file from 1452 to 1463. These are the only two code-health findings; no general linter threshold is changed. --- .../Farming/PokemonSV_BlueberryQuests.cpp | 136 +++++++++++ .../PokemonSV_BlueberryQuests_Execution.cpp | 213 ------------------ SerialPrograms/cmake/SourceFiles.cmake | 1 - 3 files changed, 136 insertions(+), 214 deletions(-) delete mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 1b9924ec9f..afe39c8692 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -443,6 +443,142 @@ std::vector process_quest_list( return plan.to_do; } +bool process_and_do_quest( + ProgramEnvironment& env, + ConsoleHandle& console, ProControllerContext& context, + BBQOption& BBQ_OPTIONS, + BBQuests current_quest, + uint8_t& eggs_hatched +){ + bool quest_completed = false; + int quest_attempts = 0; + + while (!quest_completed){ + BBQAttemptLog attempt_log( + console.logger(), /*quest=*/current_quest, + /*attempt=*/quest_attempts + 1, /*limit=*/static_cast(BBQ_OPTIONS.NUM_RETRIES) + 1 + ); + switch (current_quest){ + case BBQuests::make_tm: + quest_make_tm(env.program_info(), console, context); + break; + case BBQuests::travel_500: + quest_travel_500(env.program_info(), console, context); + break; + case BBQuests::tera_self_defeat: + quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::sneak_up: + quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::wild_tera: + quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::wash_pokemon: + quest_wash_pokemon(env.program_info(), console, context); + break; + case BBQuests::hatch_egg: + quest_hatch_egg(env, console, context, BBQ_OPTIONS); + break; + case BBQuests::bitter_sandwich: + case BBQuests::salty_sandwich: + case BBQuests::sour_sandwich: + case BBQuests::spicy_sandwich: + case BBQuests::sweet_sandwich: + case BBQuests::sandwich_three: + quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); + break; + //case BBQuests::pickup_10: + // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); + // break; + case BBQuests::tera_raid: + quest_tera_raid(env, console, context, BBQ_OPTIONS); + break; + case BBQuests::auto_10: + case BBQuests::auto_30: + quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); + break; + //All involve taking pictures + case BBQuests::photo_fly: + case BBQuests::photo_swim: + case BBQuests::photo_canyon: + case BBQuests::photo_coastal: + case BBQuests::photo_polar: + case BBQuests::photo_savanna: + case BBQuests::photo_normal: + case BBQuests::photo_fighting: + case BBQuests::photo_flying: + case BBQuests::photo_poison: + case BBQuests::photo_ground: + case BBQuests::photo_rock: + case BBQuests::photo_bug: + case BBQuests::photo_ghost: + case BBQuests::photo_steel: + case BBQuests::photo_fire: + case BBQuests::photo_water: + case BBQuests::photo_grass: + case BBQuests::photo_electric: + case BBQuests::photo_psychic: + case BBQuests::photo_ice: + case BBQuests::photo_dragon: + case BBQuests::photo_dark: + case BBQuests::photo_fairy: + quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + break; + //All involve catching a pokemon + case BBQuests::catch_any: + case BBQuests::catch_normal: + case BBQuests::catch_fighting: + case BBQuests::catch_flying: + case BBQuests::catch_poison: + case BBQuests::catch_ground: + case BBQuests::catch_rock: + case BBQuests::catch_bug: + case BBQuests::catch_ghost: + case BBQuests::catch_steel: + case BBQuests::catch_fire: + case BBQuests::catch_water: + case BBQuests::catch_grass: + case BBQuests::catch_electric: + case BBQuests::catch_psychic: + case BBQuests::catch_ice: + case BBQuests::catch_dragon: + case BBQuests::catch_dark: + case BBQuests::catch_fairy: + quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + break; + default: + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unknown quest selection.", + console + ); + break; + } + + attempt_log.route_return(); + //Validate quest was completed by checking list + 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()){ + quest_attempts++; + }else{ + if (current_quest == BBQuests::hatch_egg){ + eggs_hatched++; + console.log("Eggs hatched: " + std::to_string(eggs_hatched)); + } + + quest_completed = true; + } + + attempt_log.result(/*completed=*/quest_completed); + if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ + BBQLogger(console.logger()).retry_exhausted(current_quest); + break; + } + } + return quest_completed; +} + void quest_make_tm(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ stream.log("Quest: Make TM"); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp deleted file mode 100644 index fe538a2560..0000000000 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp +++ /dev/null @@ -1,213 +0,0 @@ -/* Blueberry Quest Execution - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" -#include "CommonFramework/Tools/ProgramEnvironment.h" -#include "PokemonSV_BBQLogger.h" -#include "PokemonSV_BlueberryCatchPhoto.h" -#include "PokemonSV_BlueberryQuests.h" - -namespace PokemonAutomation{ -namespace NintendoSwitch{ -namespace PokemonSV{ -namespace{ - -// Returns true after executing one supported quest; false performs no action. -bool run_basic_quest( - ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, BBQuests current_quest -){ - switch (current_quest){ - case BBQuests::make_tm: - quest_make_tm(env.program_info(), console, context); - return true; - case BBQuests::travel_500: - quest_travel_500(env.program_info(), console, context); - return true; - case BBQuests::tera_self_defeat: - quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); - return true; - case BBQuests::sneak_up: - quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); - return true; - case BBQuests::wild_tera: - quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); - return true; - case BBQuests::wash_pokemon: - quest_wash_pokemon(env.program_info(), console, context); - return true; - default: - return false; - } -} - -// Returns true after executing one supported quest; false performs no action. -bool run_event_quest( - ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, BBQuests current_quest -){ - switch (current_quest){ - case BBQuests::hatch_egg: - quest_hatch_egg(env, console, context, BBQ_OPTIONS); - return true; - case BBQuests::bitter_sandwich: - case BBQuests::salty_sandwich: - case BBQuests::sour_sandwich: - case BBQuests::spicy_sandwich: - case BBQuests::sweet_sandwich: - case BBQuests::sandwich_three: - quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); - return true; - //case BBQuests::pickup_10: - // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); - // break; - case BBQuests::tera_raid: - quest_tera_raid(env, console, context, BBQ_OPTIONS); - return true; - case BBQuests::auto_10: - case BBQuests::auto_30: - quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); - return true; - default: - return false; - } -} - -// Returns true after executing one supported quest; false performs no action. -bool run_photo_quest( - ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, BBQuests current_quest -){ - switch (current_quest){ - //All involve taking pictures - case BBQuests::photo_fly: - case BBQuests::photo_swim: - case BBQuests::photo_canyon: - case BBQuests::photo_coastal: - case BBQuests::photo_polar: - case BBQuests::photo_savanna: - case BBQuests::photo_normal: - case BBQuests::photo_fighting: - case BBQuests::photo_flying: - case BBQuests::photo_poison: - case BBQuests::photo_ground: - case BBQuests::photo_rock: - case BBQuests::photo_bug: - case BBQuests::photo_ghost: - case BBQuests::photo_steel: - case BBQuests::photo_fire: - case BBQuests::photo_water: - case BBQuests::photo_grass: - case BBQuests::photo_electric: - case BBQuests::photo_psychic: - case BBQuests::photo_ice: - case BBQuests::photo_dragon: - case BBQuests::photo_dark: - case BBQuests::photo_fairy: - quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - return true; - default: - return false; - } -} - -// Returns true after executing one supported quest; false performs no action. -bool run_catch_quest( - ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, BBQuests current_quest -){ - switch (current_quest){ - //All involve catching a pokemon - case BBQuests::catch_any: - case BBQuests::catch_normal: - case BBQuests::catch_fighting: - case BBQuests::catch_flying: - case BBQuests::catch_poison: - case BBQuests::catch_ground: - case BBQuests::catch_rock: - case BBQuests::catch_bug: - case BBQuests::catch_ghost: - case BBQuests::catch_steel: - case BBQuests::catch_fire: - case BBQuests::catch_water: - case BBQuests::catch_grass: - case BBQuests::catch_electric: - case BBQuests::catch_psychic: - case BBQuests::catch_ice: - case BBQuests::catch_dragon: - case BBQuests::catch_dark: - case BBQuests::catch_fairy: - quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - return true; - default: - return false; - } -} - -// Dispatch to the same quest routines as the original switch, or report an unsupported quest. -void execute_quest( - ProgramEnvironment& env, ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, BBQuests current_quest -){ - if (run_basic_quest(env, console, context, BBQ_OPTIONS, current_quest) - || run_event_quest(env, console, context, BBQ_OPTIONS, current_quest) - || run_photo_quest(env, console, context, BBQ_OPTIONS, current_quest) - || run_catch_quest(env, console, context, BBQ_OPTIONS, current_quest) - ){ - return; - } - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, "Unknown quest selection.", console - ); -} - -} // namespace - -// Execute/retry from the overworld, verify the board and preserve the caller's egg count. -bool process_and_do_quest( - ProgramEnvironment& env, - ConsoleHandle& console, ProControllerContext& context, - BBQOption& BBQ_OPTIONS, - BBQuests current_quest, - uint8_t& eggs_hatched -){ - bool quest_completed = false; - int quest_attempts = 0; - - while (!quest_completed){ - BBQAttemptLog attempt_log( - console.logger(), /*quest=*/current_quest, - /*attempt=*/quest_attempts + 1, /*limit=*/static_cast(BBQ_OPTIONS.NUM_RETRIES) + 1 - ); - execute_quest(env, console, context, BBQ_OPTIONS, current_quest); - - attempt_log.route_return(); - //Validate quest was completed by checking list - 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()){ - quest_attempts++; - }else{ - if (current_quest == BBQuests::hatch_egg){ - eggs_hatched++; - console.log("Eggs hatched: " + std::to_string(eggs_hatched)); - } - - quest_completed = true; - } - - attempt_log.result(/*completed=*/quest_completed); - if (quest_attempts > BBQ_OPTIONS.NUM_RETRIES){ - BBQLogger(console.logger()).retry_exhausted(current_quest); - break; - } - } - return quest_completed; -} - -} // namespace PokemonSV -} // namespace NintendoSwitch -} // namespace PokemonAutomation diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 759e28029d..011038f97e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2652,7 +2652,6 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp - Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests_Execution.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_ClaimMysteryGift.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_ClaimMysteryGift.h From 61c6aae9824e4087df5e374b3938e9631c873797 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 8 Nov 2026 16:59:54 -0800 Subject: [PATCH 50/50] PokemonSV: fix batch conversion and panel_detected identifier in BBQ planner --- .../PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp | 4 ++-- .../PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp index 72aa5b9dcf..c7226c0cbd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp @@ -363,11 +363,11 @@ class BBQSoloFarmerGameActions : public BBQGameActions{ virtual BBQuestBatch process_quest_list( const std::vector& quests, uint8_t eggs_hatched ) override{ - std::vector to_do = PokemonSV::process_quest_list( + BBQuestBatch batch = PokemonSV::process_quest_list( m_env.program_info(), m_env.console, m_context, m_options, quests, eggs_hatched, &m_bp_ledger ); update_BP_stats(); - return to_do; + return batch; } virtual bool do_quest(BBQuests quest, uint8_t& eggs_hatched, std::vector& panel_after) override{ return process_and_do_quest(m_env, m_env.console, m_context, m_options, quest, eggs_hatched, &panel_after); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index b44c437313..b099b38c71 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -114,7 +114,7 @@ std::vector read_quests( ){ std::vector quest_list; - open_quest_panel(stream, context); + const bool panel_detected = open_quest_panel(stream, context); //Read in the initial 4 quests. stream.log("Reading quests."); @@ -137,7 +137,7 @@ std::vector read_quests( if (BP != nullptr){ *BP = read_BP_from_image(stream.logger(), screen); } - BBQLogger(stream.logger()).board(/*panel_detected=*/result == 0, quest_list); + BBQLogger(stream.logger()).board(/*panel_detected=*/panel_detected, quest_list); //Close quest list pbf_mash_button(context, BUTTON_B, 800ms);