From ea359ba3d021daf9d2e472c06f6db929c03ca35a Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 20 Oct 2026 21:08:30 -0700 Subject: [PATCH 1/2] 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 053e59b375ab3bdcf06c5ed167820e5d844ab070 Mon Sep 17 00:00:00 2001 From: dayfine Date: Wed, 21 Oct 2026 10:51:21 -0700 Subject: [PATCH 2/2] 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);