From 2779423d94539cf2674b7aafe0a4ed733e4201cd Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 26 Oct 2026 22:51:24 -0700 Subject: [PATCH 1/8] 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 0181f814d01e8679d92e340d87f997d5bf411118 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 02:46:20 -0700 Subject: [PATCH 2/8] BBQ: run the Auto Battle loop against injectable game steps Move the decisions of quest_auto_battle() into run_auto_battle(), with the game steps (fly to Chargestone, clear, return to Central Plaza, heal) behind BBQAutoBattleActions. The console version makes today's calls with today's buttons and timings, so behavior is unchanged. The interface is the one the follow-up policies need, so they change only the loop: clear() takes a kill count to stop at and returns the lead's last known HP, and run_auto_battle() returns why it stopped. For the stop count, use_lets_go_to_clear_in_front() takes an optional stop_early predicate; its default is none, so its other callers are unchanged. The loop passes no stop count yet, so every clear still runs until no kill has been heard for 6 seconds. Log one line per pass: its kills, the running total, the lead's HP, the time spent and what happens next. Native tests in PokemonSV_BBQTests.cpp use a scripted fake that charges each step the seconds measured on one live pass, and pin today's behavior: the final pass still heals, and with no kills the loop never ends. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 196 ++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 187 +++++++++++++---- .../Farming/PokemonSV_BlueberryQuests.h | 49 +++++ .../ShinyHunting/PokemonSV_LetsGoTools.cpp | 13 +- .../ShinyHunting/PokemonSV_LetsGoTools.h | 5 +- 5 files changed, 404 insertions(+), 46 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 999661c313..541582b6cc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -4,6 +4,8 @@ * */ +#include +#include #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQTests.h" @@ -66,8 +68,202 @@ static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ } +namespace{ + +// Keeps every line logged, so a test can check the pass log. +class RecordingLogger : public Logger{ +public: + std::vector lines; + + virtual void log(const std::string& msg, Color) override{ + lines.push_back(msg); + } +}; + +std::string join(const std::vector& items, const std::string& separator){ + std::string ret; + for (const std::string& item : items){ + if (!ret.empty()){ + ret += separator; + } + ret += item; + } + return ret; +} + +// Auto Battle steps that report scripted kills, and charge each step the +// seconds measured on one live pass (bbq-m5 log, 2026-10-21). +class ScriptedAutoBattle : public BBQAutoBattleActions{ +public: + static constexpr int FLY_SECONDS = 20; // Fly to Chargestone, wait 3 s, walk in (~19.5 s). + static constexpr int CLEAR_SECONDS = 26; // Battling, then 6 s with no kill. + static constexpr int CLEAR_TAIL_SECONDS = 6; // Skipped when the clear stops at `stop_at_kills`. + static constexpr int RETURN_SECONDS = 28; // Fly back to Central Plaza. + static constexpr int HEAL_SECONDS = 28; // Walk to the nurse and heal (14 s), then re-fly (14 s). + + // Kills heard in each clear, in turn. Running past the end throws, which + // also stops a loop that would never end. + std::vector kills_per_clear; + + std::vector steps; + int seconds = 0; + + virtual uint64_t kills() const override{ + return m_kills; + } + virtual WallClock now() const override{ + return WallClock{} + std::chrono::seconds(seconds); + } + virtual void fly_to_battle_area() override{ + steps.push_back("fly"); + seconds += FLY_SECONDS; + } + virtual std::optional clear(uint64_t stop_at_kills) override{ + if (m_clears >= kills_per_clear.size()){ + throw std::runtime_error("Cleared more often than scripted."); + } + steps.push_back("clear"); + seconds += CLEAR_SECONDS; + m_kills += kills_per_clear[m_clears++]; + if (m_kills >= stop_at_kills){ + seconds -= CLEAR_TAIL_SECONDS; + } + return std::nullopt; + } + virtual void return_to_plaza() override{ + steps.push_back("return"); + seconds += RETURN_SECONDS; + } + virtual void heal() override{ + steps.push_back("heal"); + seconds += HEAL_SECONDS; + } + +private: + uint64_t m_kills = 0; + size_t m_clears = 0; +}; + +// One pass as it shows in the expected steps. +const std::string PASS = "fly clear return heal"; + +std::string passes(int count){ + std::vector ret(count, PASS); + return join(ret, " "); +} + +struct BBQAutoBattleScenario{ + std::string name; + uint64_t kill_target = 10; + std::vector kills_per_clear; + bool expected_endless = false; // Runs past the script without stopping. + std::string expected_steps; + int expected_seconds = 0; + std::vector expected_log = {}; // Checked when not empty. +}; + +class Test_BBQAutoBattle : public UnitTest{ +public: + Test_BBQAutoBattle(BBQAutoBattleScenario scenario) + : UnitTest("PokemonSV::BBQAutoBattle - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScriptedAutoBattle actions; + actions.kills_per_clear = m_scenario.kills_per_clear; + + RecordingLogger logger; + BBQAutoBattlePolicy policy; + policy.kill_target = m_scenario.kill_target; + bool endless = false; + try{ + run_auto_battle(logger, actions, policy); + }catch (const std::runtime_error&){ + endless = true; + } + if (endless != m_scenario.expected_endless){ + return endless + ? "Expected the loop to stop, but it cleared more often than scripted." + : "Expected the loop to run past the script, but it stopped."; + } + const std::string steps = join(actions.steps, " "); + if (steps != m_scenario.expected_steps){ + return "Expected steps: " + m_scenario.expected_steps + "\nActual steps: " + steps; + } + if (actions.seconds != m_scenario.expected_seconds){ + return "Expected " + std::to_string(m_scenario.expected_seconds) + " seconds, got " + + std::to_string(actions.seconds); + } + if (!m_scenario.expected_log.empty() && logger.lines != m_scenario.expected_log){ + return "Expected log:\n" + join(m_scenario.expected_log, "\n") + + "\nActual log:\n" + join(logger.lines, "\n"); + } + return true; + } + +private: + const BBQAutoBattleScenario m_scenario; +}; + +} + +static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ + constexpr int PASS_SECONDS = ScriptedAutoBattle::FLY_SECONDS + ScriptedAutoBattle::CLEAR_SECONDS + + ScriptedAutoBattle::RETURN_SECONDS + ScriptedAutoBattle::HEAL_SECONDS; + const BBQAutoBattleScenario scenarios[] = { + { + // The final pass still heals and re-flies, though the quest is done. + .name = "auto battle 10 at 2 kills per pass", + .kills_per_clear = {2, 2, 2, 2, 2}, + .expected_steps = passes(5), + .expected_seconds = 5 * PASS_SECONDS, + .expected_log = { + "Auto Battle pass 1: 2 kills, 2/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 2: 2 kills, 4/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 3: 2 kills, 6/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 4: 2 kills, 8/10 in total, HP ?, 352 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 5: 2 kills, 10/10 in total, HP ?, 454 s elapsed. Target reached: return and heal.", + }, + }, + { + .name = "auto battle 30 at 3 kills per pass", + .kill_target = 30, + .kills_per_clear = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + .expected_steps = passes(10), + .expected_seconds = 10 * PASS_SECONDS, + }, + { + // A clear keeps going while kills are heard, past the target. + .name = "kills past the target", + .kills_per_clear = {4, 4, 4}, + .expected_steps = passes(3), + .expected_seconds = 3 * PASS_SECONDS, + }, + { + .name = "passes with no kill go again", + .kills_per_clear = {0, 0, 5, 0, 5}, + .expected_steps = passes(5), + .expected_seconds = 5 * PASS_SECONDS, + }, + { + // Nothing bounds the loop: with no kills it never ends. + .name = "no kills never stops", + .kills_per_clear = {0, 0, 0, 0, 0, 0}, + .expected_endless = true, + .expected_steps = passes(6) + " fly", + .expected_seconds = 6 * PASS_SECONDS + ScriptedAutoBattle::FLY_SECONDS, + }, + }; + for (const BBQAutoBattleScenario& scenario : scenarios){ + database.add(scenario); + } +} + + void add_tests_BBQ(UnitTestDatabase& database){ add_tests_TeraEncounterAction(database); + add_tests_BBQAutoBattle(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..8dd448ca4c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -4,6 +4,9 @@ * */ +#include +#include +#include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/ErrorDumper.h" @@ -27,6 +30,7 @@ #include "VideoGames/PokemonSV/Inference/Dialogs/PokemonSV_DialogDetector.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldSensors.h" +#include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_LetsGoHpReader.h" #include "VideoGames/PokemonSV/Inference/Boxes/PokemonSV_BoxDetection.h" #include "VideoGames/PokemonSV/Programs/Boxes/PokemonSV_BoxRoutines.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_MenuNavigation.h" @@ -1248,75 +1252,170 @@ void quest_tera_raid( day_skip_from_overworld(console, context); } -void quest_auto_battle( - ProgramEnvironment& env, - VideoStream& stream, ProControllerContext& context, - const BBQOption& BBQ_OPTIONS, - BBQuests current_quest +BBQAutoBattleResult run_auto_battle( + Logger& logger, + BBQAutoBattleActions& actions, + const BBQAutoBattlePolicy& policy ){ - stream.log("Quest: Auto Battle 10/30"); - - - OverworldSensors sensors( - env.logger(), stream, context - ); - - LetsGoEncounterBotStats stats; - LetsGoEncounterBotTracker tracker( - env, stream, - stats, - sensors.lets_go_kill - ); - - uint64_t target_number = 10; + const WallClock start = actions.now(); + uint64_t pass = 0; + while (actions.kills() < policy.kill_target){ + pass++; + const uint64_t kills_before = actions.kills(); + + actions.fly_to_battle_area(); + //Clear until no kill has been heard for 6 seconds, even past the target. + const std::optional hp = actions.clear(std::numeric_limits::max()); + + const uint64_t kills = actions.kills(); + const auto elapsed = std::chrono::duration_cast(actions.now() - start); + const std::string hp_text = hp ? std::to_string((int)std::floor(*hp * 100)) + "%" : "?"; + + std::optional end; + std::string next = "Below the target: return, heal and go again."; + if (kills >= policy.kill_target){ + end = BBQAutoBattleResult::TargetReached; + next = "Target reached: return and heal."; + } + logger.log( + "Auto Battle pass " + std::to_string(pass) + ": " + + std::to_string(kills - kills_before) + " kills, " + + std::to_string(kills) + "/" + std::to_string(policy.kill_target) + " in total, " + + "HP " + hp_text + ", " + + std::to_string(elapsed.count()) + " s elapsed. " + + next + ); - if (current_quest == BBQuests::auto_30){ - target_number = 30; + actions.return_to_plaza(); + actions.heal(); + if (end){ + return *end; + } } + return BBQAutoBattleResult::TargetReached; +} - while (stats.m_kills < target_number){ - - central_to_chargestone(env.program_info(), stream, context); +namespace{ + +class ConsoleBBQAutoBattleActions : public BBQAutoBattleActions{ +public: + ConsoleBBQAutoBattleActions( + ProgramEnvironment& env, + VideoStream& stream, ProControllerContext& context, + LetsGoEncounterBotStats& stats, + LetsGoEncounterBotTracker& tracker, + LetsGoHpWatcher& hp_watcher + ) + : m_env(env), m_stream(stream), m_context(context) + , m_stats(stats), m_tracker(tracker), m_hp_watcher(hp_watcher) + {} + + virtual uint64_t kills() const override{ + return m_stats.m_kills; + } + virtual WallClock now() const override{ + return current_time(); + } + virtual void fly_to_battle_area() override{ + central_to_chargestone(m_env.program_info(), m_stream, m_context); //Wait for spawns - pbf_wait(context, 3000ms); - context.wait_for_all_requests(); + pbf_wait(m_context, 3000ms); + m_context.wait_for_all_requests(); //Forward and right, stay in the battle court - safe zone - pbf_press_button(context, BUTTON_L, 160ms, 400ms); - pbf_move_left_joystick(context, {0, +1}, 2000ms, 400ms); - pbf_move_left_joystick(context, {+1, 0}, 1440ms, 400ms); - pbf_press_button(context, BUTTON_L, 160ms, 400ms); + pbf_press_button(m_context, BUTTON_L, 160ms, 400ms); + pbf_move_left_joystick(m_context, {0, +1}, 2000ms, 400ms); + pbf_move_left_joystick(m_context, {+1, 0}, 1440ms, 400ms); + pbf_press_button(m_context, BUTTON_L, 160ms, 400ms); + } + virtual std::optional clear(uint64_t stop_at_kills) override{ + //The watcher runs for the whole quest. Forget what it read before this clear, so a reading + //from an earlier pass is never reported as this one's. + m_hp_watcher.clear(); - use_lets_go_to_clear_in_front(stream, context, tracker, false, [&](ProControllerContext& context){ + use_lets_go_to_clear_in_front(m_stream, m_context, m_tracker, false, [&](ProControllerContext& context){ pbf_move_left_joystick(context, {0, -1}, 1440ms, 400ms); pbf_wait(context, 12000ms); context.wait_for_all_requests(); - }); + }, + [&]{ return m_stats.m_kills >= stop_at_kills; } + ); - context.wait_for_all_requests(); - return_to_plaza(env.program_info(), stream, context); + m_context.wait_for_all_requests(); - OverworldWatcher done_healing(stream.logger(), COLOR_BLUE); - pbf_move_left_joystick(context, {0, +1}, 800ms, 160ms); + //Read before leaving: the HP bar is only on screen in the overworld with the lead out. + const double hp = m_hp_watcher.last_known_value(); + if (hp <= 0){ + return std::nullopt; + } + return hp; + } + virtual void return_to_plaza() override{ + PokemonSV::return_to_plaza(m_env.program_info(), m_stream, m_context); + } + virtual void heal() override{ + OverworldWatcher done_healing(m_stream.logger(), COLOR_BLUE); + pbf_move_left_joystick(m_context, {0, +1}, 800ms, 160ms); - pbf_mash_button(context, BUTTON_A, 2400ms); - context.wait_for_all_requests(); + pbf_mash_button(m_context, BUTTON_A, 2400ms); + m_context.wait_for_all_requests(); int exit = run_until( - stream, context, + m_stream, m_context, [&](ProControllerContext& context){ pbf_mash_button(context, BUTTON_B, 16000ms); }, { { done_healing } } ); if (exit == 0){ - stream.log("Overworld detected."); + m_stream.log("Overworld detected."); } - open_map_from_overworld(env.program_info(), stream, context); - pbf_press_button(context, BUTTON_ZL, 320ms, 800ms); - fly_to_overworld_from_map(env.program_info(), stream, context); + open_map_from_overworld(m_env.program_info(), m_stream, m_context); + pbf_press_button(m_context, BUTTON_ZL, 320ms, 800ms); + fly_to_overworld_from_map(m_env.program_info(), m_stream, m_context); + } + +private: + ProgramEnvironment& m_env; + VideoStream& m_stream; + ProControllerContext& m_context; + LetsGoEncounterBotStats& m_stats; + LetsGoEncounterBotTracker& m_tracker; + LetsGoHpWatcher& m_hp_watcher; +}; + +} + +void quest_auto_battle( + ProgramEnvironment& env, + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS, + BBQuests current_quest +){ + stream.log("Quest: Auto Battle 10/30"); + + + OverworldSensors sensors( + env.logger(), stream, context + ); + + LetsGoEncounterBotStats stats; + LetsGoEncounterBotTracker tracker( + env, stream, + stats, + sensors.lets_go_kill + ); + + BBQAutoBattlePolicy policy; + policy.kill_target = 10; + + if (current_quest == BBQuests::auto_30){ + policy.kill_target = 30; } + + ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp); + run_auto_battle(stream.logger(), actions, policy); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 21ad16aa71..d643fa3ce3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -7,7 +7,10 @@ #ifndef PokemonAutomation_PokemonSV_BlueberryQuests_H #define PokemonAutomation_PokemonSV_BlueberryQuests_H +#include #include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/Logging/AbstractLogger.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h" @@ -159,6 +162,52 @@ void quest_auto_battle( BBQuests current_quest ); +//The game steps of one Auto Battle pass. They are injected so that the loop's decisions (when +//to go again, when to stop) run in tests without a Switch. +class BBQAutoBattleActions{ +public: + virtual ~BBQAutoBattleActions() = default; + + //Kills heard since the quest started. Kills are counted as they are heard, in any step. + virtual uint64_t kills() const = 0; + + //The current time, to time the passes. + virtual WallClock now() const = 0; + + //Fly from Central Plaza to Chargestone Cavern, wait for spawns, and walk into the battle court. + virtual void fly_to_battle_area() = 0; + + //Send the lead out with Let's Go until no kill has been heard for 6 seconds, or until + //kills() reaches `stop_at_kills`, whichever comes first. Returns the lead's last known HP, + //from 0 to 1, or nothing if it could not be read. + virtual std::optional clear(uint64_t stop_at_kills) = 0; + + //Fly back to Central Plaza. Leaving the cave respawns its Pokemon. + virtual void return_to_plaza() = 0; + + //Walk to the nurse and heal, then fly to the Central Plaza fly point again: the flight to + //Chargestone moves the map cursor from there. + virtual void heal() = 0; +}; + +struct BBQAutoBattlePolicy{ + uint64_t kill_target = 10; +}; + +//Why run_auto_battle() returned. +enum class BBQAutoBattleResult{ + TargetReached, +}; + +//Clear Chargestone Cavern, one pass at a time, until the kills reach the target, and return why +//it stopped. It ends at Central Plaza, where the next quest starts. Logs one line per pass: its +//kills, the total, the lead's HP, the time spent, and what happens next. +BBQAutoBattleResult run_auto_battle( + Logger& logger, + BBQAutoBattleActions& actions, + const BBQAutoBattlePolicy& policy +); + } } } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.cpp index d21bb3b405..e3c7070f7c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.cpp @@ -176,7 +176,8 @@ bool use_lets_go_to_clear_in_front( VideoStream& stream, ProControllerContext& context, LetsGoEncounterBotTracker& tracker, bool throw_ball_if_bubble, - std::function&& command + std::function&& command, + std::function stop_early ){ // ShinyHuntAreaZeroPlatform_Descriptor::Stats& stats = env.current_stats(); @@ -213,12 +214,22 @@ bool use_lets_go_to_clear_in_front( command(context); context.wait_for_all_requests(); command = nullptr; + }else if (stop_early){ + // Wait in short steps, so the clear ends soon after `stop_early` is met. + while (current_time() < tracker.last_kill() + timeout && !stop_early()){ + context.wait_until(std::min(tracker.last_kill() + timeout, current_time() + Milliseconds(200))); + } }else{ // cout << "Waiting out... " << timeout.count() << " seconds" << endl; context.wait_until(tracker.last_kill() + timeout); } // timeout = std::chrono::seconds(3); + if (stop_early && stop_early()){ + stream.log("Stopping the clear early."); + break; + } + WallClock now = current_time(); WallClock last_kill_time = tracker.last_kill(); WallDuration last_kill; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h index 3e06d2019a..bb2c73ec98 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/ShinyHunting/PokemonSV_LetsGoTools.h @@ -165,11 +165,14 @@ class LetsGoEncounterBotTracker{ // chain has ended. // The function tracks kill chain by sound detection from `tracker`. The function // does not handle any pokemon battle encounters (turn-based battles). +// If `stop_early` is set, it is checked after the command and while waiting +// for the kill chain to end, and the function returns once it is true. bool use_lets_go_to_clear_in_front( VideoStream& stream, ProControllerContext& context, LetsGoEncounterBotTracker& tracker, bool throw_ball_if_bubble, - std::function&& command + std::function&& command, + std::function stop_early = nullptr ); From fda42de2195cc135117a749c136047b67d281867 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 23:45:54 -0700 Subject: [PATCH 3/8] BBQ: stop an Auto Battle quest once its kills reach the target run_auto_battle() passes the kill target to clear(), so the clear ends as soon as the kills reach it, and on the pass that reaches it returns to Central Plaza without healing or re-flying. In the tests' model this saves 34 s per quest (the 6 s tail, the heal and the re-fly). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 25 +++++++++++-------- .../Farming/PokemonSV_BlueberryQuests.cpp | 9 +++---- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 541582b6cc..197bd57cb6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -146,6 +146,8 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ // One pass as it shows in the expected steps. const std::string PASS = "fly clear return heal"; +// A pass that returns without healing. +const std::string LAST_PASS = "fly clear return"; std::string passes(int count){ std::vector ret(count, PASS); @@ -211,40 +213,41 @@ class Test_BBQAutoBattle : public UnitTest{ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ constexpr int PASS_SECONDS = ScriptedAutoBattle::FLY_SECONDS + ScriptedAutoBattle::CLEAR_SECONDS + ScriptedAutoBattle::RETURN_SECONDS + ScriptedAutoBattle::HEAL_SECONDS; + constexpr int LAST_PASS_SAVED_SECONDS = ScriptedAutoBattle::CLEAR_TAIL_SECONDS + ScriptedAutoBattle::HEAL_SECONDS; const BBQAutoBattleScenario scenarios[] = { { - // The final pass still heals and re-flies, though the quest is done. + // The final pass stops clearing at the target, and skips the heal and re-fly. .name = "auto battle 10 at 2 kills per pass", .kills_per_clear = {2, 2, 2, 2, 2}, - .expected_steps = passes(5), - .expected_seconds = 5 * PASS_SECONDS, + .expected_steps = passes(4) + " " + LAST_PASS, + .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, .expected_log = { "Auto Battle pass 1: 2 kills, 2/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", "Auto Battle pass 2: 2 kills, 4/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", "Auto Battle pass 3: 2 kills, 6/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.", "Auto Battle pass 4: 2 kills, 8/10 in total, HP ?, 352 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 5: 2 kills, 10/10 in total, HP ?, 454 s elapsed. Target reached: return and heal.", + "Auto Battle pass 5: 2 kills, 10/10 in total, HP ?, 448 s elapsed. Target reached: return without healing.", }, }, { .name = "auto battle 30 at 3 kills per pass", .kill_target = 30, .kills_per_clear = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, - .expected_steps = passes(10), - .expected_seconds = 10 * PASS_SECONDS, + .expected_steps = passes(9) + " " + LAST_PASS, + .expected_seconds = 10 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, { - // A clear keeps going while kills are heard, past the target. + // Kills heard before the clear can stop still count. .name = "kills past the target", .kills_per_clear = {4, 4, 4}, - .expected_steps = passes(3), - .expected_seconds = 3 * PASS_SECONDS, + .expected_steps = passes(2) + " " + LAST_PASS, + .expected_seconds = 3 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, { .name = "passes with no kill go again", .kills_per_clear = {0, 0, 5, 0, 5}, - .expected_steps = passes(5), - .expected_seconds = 5 * PASS_SECONDS, + .expected_steps = passes(4) + " " + LAST_PASS, + .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, { // Nothing bounds the loop: with no kills it never ends. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 8dd448ca4c..62d0fd2de5 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -5,7 +5,6 @@ */ #include -#include #include #include #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" @@ -1264,8 +1263,7 @@ BBQAutoBattleResult run_auto_battle( const uint64_t kills_before = actions.kills(); actions.fly_to_battle_area(); - //Clear until no kill has been heard for 6 seconds, even past the target. - const std::optional hp = actions.clear(std::numeric_limits::max()); + const std::optional hp = actions.clear(policy.kill_target); const uint64_t kills = actions.kills(); const auto elapsed = std::chrono::duration_cast(actions.now() - start); @@ -1275,7 +1273,7 @@ BBQAutoBattleResult run_auto_battle( std::string next = "Below the target: return, heal and go again."; if (kills >= policy.kill_target){ end = BBQAutoBattleResult::TargetReached; - next = "Target reached: return and heal."; + next = "Target reached: return without healing."; } logger.log( "Auto Battle pass " + std::to_string(pass) + ": " @@ -1286,11 +1284,12 @@ BBQAutoBattleResult run_auto_battle( + next ); + //The next quest starts from Central Plaza, so return even when done. actions.return_to_plaza(); - actions.heal(); if (end){ return *end; } + actions.heal(); } return BBQAutoBattleResult::TargetReached; } From a536fda46864a9adf51ef77a14cae15352adab4d Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 23:47:15 -0700 Subject: [PATCH 4/8] BBQ: give up an Auto Battle quest after 3 empty passes or a time limit run_auto_battle() had no bound: if kills stopped registering (wrong lead, no spawns, no audio) it never returned, and the farmer's retry handling could not step in. It now stops after 3 passes in a row with no kill, or after the pass that ends past the time limit (35 minutes for Auto Battle 10, 60 for Auto Battle 30, about 3x the slowest attempts seen). It returns to Central Plaza without healing, logs which limit ended it, and leaves the outcome to the board check. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 73 +++++++++++++++---- .../Farming/PokemonSV_BlueberryQuests.cpp | 11 +++ .../Farming/PokemonSV_BlueberryQuests.h | 8 ++ 3 files changed, 78 insertions(+), 14 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 197bd57cb6..3cb0254e9b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -154,11 +154,21 @@ std::string passes(int count){ return join(ret, " "); } +std::string auto_battle_result_to_string(BBQAutoBattleResult result){ + switch (result){ + case BBQAutoBattleResult::TargetReached: return "TargetReached"; + case BBQAutoBattleResult::NoKills: return "NoKills"; + case BBQAutoBattleResult::TimeLimit: return "TimeLimit"; + } + return "?"; +} + struct BBQAutoBattleScenario{ std::string name; uint64_t kill_target = 10; + int time_limit_minutes = 35; std::vector kills_per_clear; - bool expected_endless = false; // Runs past the script without stopping. + BBQAutoBattleResult expected_result = BBQAutoBattleResult::TargetReached; std::string expected_steps; int expected_seconds = 0; std::vector expected_log = {}; // Checked when not empty. @@ -178,16 +188,16 @@ class Test_BBQAutoBattle : public UnitTest{ RecordingLogger logger; BBQAutoBattlePolicy policy; policy.kill_target = m_scenario.kill_target; - bool endless = false; + policy.time_limit = std::chrono::minutes(m_scenario.time_limit_minutes); + BBQAutoBattleResult result; try{ - run_auto_battle(logger, actions, policy); + result = run_auto_battle(logger, actions, policy); }catch (const std::runtime_error&){ - endless = true; + return "Expected the loop to stop, but it cleared more often than scripted."; } - if (endless != m_scenario.expected_endless){ - return endless - ? "Expected the loop to stop, but it cleared more often than scripted." - : "Expected the loop to run past the script, but it stopped."; + if (result != m_scenario.expected_result){ + return "Expected " + auto_battle_result_to_string(m_scenario.expected_result) + + ", got " + auto_battle_result_to_string(result); } const std::string steps = join(actions.steps, " "); if (steps != m_scenario.expected_steps){ @@ -250,12 +260,47 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, { - // Nothing bounds the loop: with no kills it never ends. - .name = "no kills never stops", - .kills_per_clear = {0, 0, 0, 0, 0, 0}, - .expected_endless = true, - .expected_steps = passes(6) + " fly", - .expected_seconds = 6 * PASS_SECONDS + ScriptedAutoBattle::FLY_SECONDS, + .name = "gives up after 3 passes with no kill", + .kills_per_clear = {0, 0, 0}, + .expected_result = BBQAutoBattleResult::NoKills, + .expected_steps = passes(2) + " " + LAST_PASS, + .expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + .expected_log = { + "Auto Battle pass 1: 0 kills, 0/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 2: 0 kills, 0/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 3: 0 kills, 0/10 in total, HP ?, 250 s elapsed. No kill in 3 passes in a row: return and give up.", + }, + }, + { + // Only passes in a row count. + .name = "a kill resets the empty passes", + .kills_per_clear = {0, 0, 1, 0, 0, 0}, + .expected_result = BBQAutoBattleResult::NoKills, + .expected_steps = passes(5) + " " + LAST_PASS, + .expected_seconds = 6 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + }, + { + // The pass that ends past the limit is the last. + .name = "gives up at the time limit", + .time_limit_minutes = 5, + .kills_per_clear = {1, 1, 1, 1}, + .expected_result = BBQAutoBattleResult::TimeLimit, + .expected_steps = passes(3) + " " + LAST_PASS, + .expected_seconds = 4 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + .expected_log = { + "Auto Battle pass 1: 1 kills, 1/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 2: 1 kills, 2/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 3: 1 kills, 3/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 4: 1 kills, 4/10 in total, HP ?, 352 s elapsed. Time limit of 5 min reached: return and give up.", + }, + }, + { + // Reaching the target counts as reached, even past the time limit. + .name = "target reached past the time limit", + .time_limit_minutes = 5, + .kills_per_clear = {3, 3, 3, 3}, + .expected_steps = passes(3) + " " + LAST_PASS, + .expected_seconds = 4 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, }; for (const BBQAutoBattleScenario& scenario : scenarios){ diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 62d0fd2de5..42d7f27044 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -1258,6 +1258,7 @@ BBQAutoBattleResult run_auto_battle( ){ const WallClock start = actions.now(); uint64_t pass = 0; + uint64_t empty_passes = 0; while (actions.kills() < policy.kill_target){ pass++; const uint64_t kills_before = actions.kills(); @@ -1266,6 +1267,7 @@ BBQAutoBattleResult run_auto_battle( const std::optional hp = actions.clear(policy.kill_target); const uint64_t kills = actions.kills(); + empty_passes = kills == kills_before ? empty_passes + 1 : 0; const auto elapsed = std::chrono::duration_cast(actions.now() - start); const std::string hp_text = hp ? std::to_string((int)std::floor(*hp * 100)) + "%" : "?"; @@ -1274,6 +1276,12 @@ BBQAutoBattleResult run_auto_battle( if (kills >= policy.kill_target){ end = BBQAutoBattleResult::TargetReached; next = "Target reached: return without healing."; + }else if (empty_passes >= policy.max_empty_passes){ + end = BBQAutoBattleResult::NoKills; + next = "No kill in " + std::to_string(empty_passes) + " passes in a row: return and give up."; + }else if (elapsed >= policy.time_limit){ + end = BBQAutoBattleResult::TimeLimit; + next = "Time limit of " + std::to_string(policy.time_limit.count()) + " min reached: return and give up."; } logger.log( "Auto Battle pass " + std::to_string(pass) + ": " @@ -1406,11 +1414,14 @@ void quest_auto_battle( sensors.lets_go_kill ); + //The time limits are about 3x the slowest attempts seen with a correct lead. BBQAutoBattlePolicy policy; policy.kill_target = 10; + policy.time_limit = std::chrono::minutes(35); if (current_quest == BBQuests::auto_30){ policy.kill_target = 30; + policy.time_limit = std::chrono::minutes(60); } ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index d643fa3ce3..6cad2bd92b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -192,16 +192,24 @@ class BBQAutoBattleActions{ struct BBQAutoBattlePolicy{ uint64_t kill_target = 10; + //Give up after this many passes in a row with no kill. + uint64_t max_empty_passes = 3; + //Give up after the pass that ends past this time. + std::chrono::minutes time_limit = std::chrono::minutes(35); }; //Why run_auto_battle() returned. enum class BBQAutoBattleResult{ TargetReached, + NoKills, //max_empty_passes passes in a row had no kill. + TimeLimit, }; //Clear Chargestone Cavern, one pass at a time, until the kills reach the target, and return why //it stopped. It ends at Central Plaza, where the next quest starts. Logs one line per pass: its //kills, the total, the lead's HP, the time spent, and what happens next. +//It gives up early when a limit in `policy` is reached; the board check then decides the quest +//failed. BBQAutoBattleResult run_auto_battle( Logger& logger, BBQAutoBattleActions& actions, From 3a0b28e7df56139a396a368dce8885c9ed86b4c3 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 23:50:26 -0700 Subject: [PATCH 5/8] BBQ: heal between Auto Battle passes only when HP is low (behavior change) Every Auto Battle pass used to end with a nurse heal and a re-fly to the Central Plaza fly point, about 28 s, even when the lead was barely hurt. Skip the heal and re-fly when the lead's HP, which clear() already reads at the end of each clear, is at or above the new "Auto Battle Quest - Heal Below HP %" option (default 50, like the Material Farmer's Auto-Heal %). Unknown HP still heals. Skipping the re-fly is safe: return_to_plaza() lands on the Central Plaza fly point, the same place the first pass and quest_tera_raid()'s retry fly out from. The per-pass log now says why the pass heals or not. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonSV/Options/PokemonSV_BBQOption.cpp | 6 + .../PokemonSV/Options/PokemonSV_BBQOption.h | 3 + .../Programs/Farming/PokemonSV_BBQTests.cpp | 116 ++++++++++++++++-- .../Farming/PokemonSV_BlueberryQuests.cpp | 21 +++- .../Farming/PokemonSV_BlueberryQuests.h | 2 + 5 files changed, 134 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..7cccadcb26 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.cpp @@ -185,6 +185,11 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) "Hatch Quest - Fix time for eggs:
Fix the time before doing a hatch egg quest.", LockMode::UNLOCK_WHILE_RUNNING, false ) + , AUTO_BATTLE_HEAL_PERCENT( + "Auto Battle Quest - Heal Below HP %:
Between passes, heal at the Pokemon Center only if your lead's HP is below this percentage. " + "If its HP could not be read, heal anyway.", + LockMode::UNLOCK_WHILE_RUNNING, 50, 0, 100 + ) , CATCH_ON_WIN(true) , NUM_RETRIES( "Number of Retries:
Reattempt a quest this many times if it fails.", @@ -207,6 +212,7 @@ BBQOption::BBQOption(OCR::LanguageOCROption* language_option) PA_ADD_OPTION(NUM_EGGS); PA_ADD_OPTION(OUT_OF_EGGS); PA_ADD_OPTION(FIX_TIME_FOR_HATCH); + PA_ADD_OPTION(AUTO_BATTLE_HEAL_PERCENT); PA_ADD_OPTION(BATTLE_AI); PA_ADD_OPTION(CATCH_ON_WIN); PA_ADD_OPTION(NUM_RETRIES); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h index f5715a10c7..ab37cc67c1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Options/PokemonSV_BBQOption.h @@ -93,6 +93,9 @@ class BBQOption : public GroupOption{ EnumDropdownOption OUT_OF_EGGS; BooleanCheckBoxOption FIX_TIME_FOR_HATCH; + //Auto battle quest + SimpleIntegerOption AUTO_BATTLE_HEAL_PERCENT; + //Tera raid TeraAIOption BATTLE_AI; TeraFarmerCatchOnWin CATCH_ON_WIN; diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 3cb0254e9b..e5e7fad2a0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -104,6 +104,8 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ // Kills heard in each clear, in turn. Running past the end throws, which // also stops a loop that would never end. std::vector kills_per_clear; + // The lead's HP read in each clear, in turn. Past the end, it is unknown. + std::vector> hp_per_clear; std::vector steps; int seconds = 0; @@ -124,11 +126,12 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ } steps.push_back("clear"); seconds += CLEAR_SECONDS; + const std::optional hp = m_clears < hp_per_clear.size() ? hp_per_clear[m_clears] : std::nullopt; m_kills += kills_per_clear[m_clears++]; if (m_kills >= stop_at_kills){ seconds -= CLEAR_TAIL_SECONDS; } - return std::nullopt; + return hp; } virtual void return_to_plaza() override{ steps.push_back("return"); @@ -149,8 +152,8 @@ const std::string PASS = "fly clear return heal"; // A pass that returns without healing. const std::string LAST_PASS = "fly clear return"; -std::string passes(int count){ - std::vector ret(count, PASS); +std::string passes(int count, const std::string& pass = PASS){ + std::vector ret(count, pass); return join(ret, " "); } @@ -168,6 +171,8 @@ struct BBQAutoBattleScenario{ uint64_t kill_target = 10; int time_limit_minutes = 35; std::vector kills_per_clear; + std::vector> hp_per_clear = {}; // Unknown when not given. + int heal_below_hp_percent = 50; BBQAutoBattleResult expected_result = BBQAutoBattleResult::TargetReached; std::string expected_steps; int expected_seconds = 0; @@ -184,11 +189,13 @@ class Test_BBQAutoBattle : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ ScriptedAutoBattle actions; actions.kills_per_clear = m_scenario.kills_per_clear; + actions.hp_per_clear = m_scenario.hp_per_clear; RecordingLogger logger; BBQAutoBattlePolicy policy; policy.kill_target = m_scenario.kill_target; policy.time_limit = std::chrono::minutes(m_scenario.time_limit_minutes); + policy.heal_below_hp_percent = (uint8_t)m_scenario.heal_below_hp_percent; BBQAutoBattleResult result; try{ result = run_auto_battle(logger, actions, policy); @@ -232,10 +239,10 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_steps = passes(4) + " " + LAST_PASS, .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, .expected_log = { - "Auto Battle pass 1: 2 kills, 2/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 2: 2 kills, 4/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 3: 2 kills, 6/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 4: 2 kills, 8/10 in total, HP ?, 352 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 1: 2 kills, 2/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 2 kills, 4/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 3: 2 kills, 6/10 in total, HP ?, 250 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 4: 2 kills, 8/10 in total, HP ?, 352 s elapsed. Below the target and HP unknown: return, heal and go again.", "Auto Battle pass 5: 2 kills, 10/10 in total, HP ?, 448 s elapsed. Target reached: return without healing.", }, }, @@ -266,8 +273,8 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_steps = passes(2) + " " + LAST_PASS, .expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, .expected_log = { - "Auto Battle pass 1: 0 kills, 0/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 2: 0 kills, 0/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 1: 0 kills, 0/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 0 kills, 0/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", "Auto Battle pass 3: 0 kills, 0/10 in total, HP ?, 250 s elapsed. No kill in 3 passes in a row: return and give up.", }, }, @@ -288,9 +295,9 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_steps = passes(3) + " " + LAST_PASS, .expected_seconds = 4 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, .expected_log = { - "Auto Battle pass 1: 1 kills, 1/10 in total, HP ?, 46 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 2: 1 kills, 2/10 in total, HP ?, 148 s elapsed. Below the target: return, heal and go again.", - "Auto Battle pass 3: 1 kills, 3/10 in total, HP ?, 250 s elapsed. Below the target: return, heal and go again.", + "Auto Battle pass 1: 1 kills, 1/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 1 kills, 2/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 3: 1 kills, 3/10 in total, HP ?, 250 s elapsed. Below the target and HP unknown: return, heal and go again.", "Auto Battle pass 4: 1 kills, 4/10 in total, HP ?, 352 s elapsed. Time limit of 5 min reached: return and give up.", }, }, @@ -302,6 +309,91 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_steps = passes(3) + " " + LAST_PASS, .expected_seconds = 4 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, }, + { + // Same kills as "auto battle 10 at 2 kills per pass", 4 heals (112 s) faster. + .name = "high HP skips the heal", + .kills_per_clear = {2, 2, 2, 2, 2}, + .hp_per_clear = {0.8, 0.8, 0.8, 0.8, 0.8}, + .expected_steps = passes(5, LAST_PASS), + .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - 4 * ScriptedAutoBattle::HEAL_SECONDS, + .expected_log = { + "Auto Battle pass 1: 2 kills, 2/10 in total, HP 80%, 46 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 2: 2 kills, 4/10 in total, HP 80%, 120 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 3: 2 kills, 6/10 in total, HP 80%, 194 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 4: 2 kills, 8/10 in total, HP 80%, 268 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 5: 2 kills, 10/10 in total, HP 80%, 336 s elapsed. Target reached: return without healing.", + }, + }, + { + // Same kills as "auto battle 30 at 3 kills per pass", 9 heals (252 s) faster. + .name = "high HP skips the heal in auto battle 30", + .kill_target = 30, + .kills_per_clear = {3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, + .hp_per_clear = {0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9, 0.9}, + .expected_steps = passes(10, LAST_PASS), + .expected_seconds = 10 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - 9 * ScriptedAutoBattle::HEAL_SECONDS, + }, + { + .name = "low HP heals", + .kills_per_clear = {2, 2, 2, 2, 2}, + .hp_per_clear = {0.3, 0.3, 0.3, 0.3, 0.3}, + .expected_steps = passes(4) + " " + LAST_PASS, + .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, + .expected_log = { + "Auto Battle pass 1: 2 kills, 2/10 in total, HP 30%, 46 s elapsed. Below the target and HP below 50%: return, heal and go again.", + "Auto Battle pass 2: 2 kills, 4/10 in total, HP 30%, 148 s elapsed. Below the target and HP below 50%: return, heal and go again.", + "Auto Battle pass 3: 2 kills, 6/10 in total, HP 30%, 250 s elapsed. Below the target and HP below 50%: return, heal and go again.", + "Auto Battle pass 4: 2 kills, 8/10 in total, HP 30%, 352 s elapsed. Below the target and HP below 50%: return, heal and go again.", + "Auto Battle pass 5: 2 kills, 10/10 in total, HP 30%, 448 s elapsed. Target reached: return without healing.", + }, + }, + { + // Unknown HP heals, as every pass did before the threshold. + .name = "unknown HP heals", + .kills_per_clear = {2, 2, 2, 2, 2}, + .hp_per_clear = {0.8, std::nullopt, 0.8, std::nullopt, 0.8}, + .expected_steps = LAST_PASS + " " + PASS + " " + LAST_PASS + " " + PASS + " " + LAST_PASS, + .expected_seconds = 5 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - 2 * ScriptedAutoBattle::HEAL_SECONDS, + }, + { + // HP at the threshold skips the heal; just below heals. + .name = "heal threshold boundary", + .kills_per_clear = {3, 3, 3, 3}, + .hp_per_clear = {0.5, 0.49, 0.5, 0.5}, + .expected_steps = LAST_PASS + " " + PASS + " " + LAST_PASS + " " + LAST_PASS, + .expected_seconds = 4 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - 2 * ScriptedAutoBattle::HEAL_SECONDS, + .expected_log = { + "Auto Battle pass 1: 3 kills, 3/10 in total, HP 50%, 46 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 2: 3 kills, 6/10 in total, HP 49%, 120 s elapsed. Below the target and HP below 50%: return, heal and go again.", + "Auto Battle pass 3: 3 kills, 9/10 in total, HP 50%, 222 s elapsed. Below the target and HP at or above 50%: return and go again without healing.", + "Auto Battle pass 4: 3 kills, 12/10 in total, HP 50%, 290 s elapsed. Target reached: return without healing.", + }, + }, + { + .name = "a threshold of 100% heals unless HP is full", + .kills_per_clear = {4, 4, 4}, + .hp_per_clear = {1.0, 0.99, 1.0}, + .heal_below_hp_percent = 100, + .expected_steps = LAST_PASS + " " + PASS + " " + LAST_PASS, + .expected_seconds = 3 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + }, + { + .name = "a threshold of 0% heals only when HP is unknown", + .kills_per_clear = {4, 4, 4}, + .hp_per_clear = {0.01, std::nullopt, 0.01}, + .heal_below_hp_percent = 0, + .expected_steps = LAST_PASS + " " + PASS + " " + LAST_PASS, + .expected_seconds = 3 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + }, + { + // The limits still end the attempt when no pass heals. + .name = "gives up after 3 passes with no kill and high HP", + .kills_per_clear = {0, 0, 0}, + .hp_per_clear = {1.0, 1.0, 1.0}, + .expected_result = BBQAutoBattleResult::NoKills, + .expected_steps = passes(3, LAST_PASS), + .expected_seconds = 3 * (PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS), + }, }; for (const BBQAutoBattleScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 42d7f27044..679b833db9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -1269,10 +1269,22 @@ BBQAutoBattleResult run_auto_battle( const uint64_t kills = actions.kills(); empty_passes = kills == kills_before ? empty_passes + 1 : 0; const auto elapsed = std::chrono::duration_cast(actions.now() - start); + + //Unknown HP heals, as every pass did before the threshold existed. The log rounds HP + //down, so it shows below the threshold exactly when it is. + const bool heal = !hp || *hp * 100 < policy.heal_below_hp_percent; const std::string hp_text = hp ? std::to_string((int)std::floor(*hp * 100)) + "%" : "?"; + const std::string threshold_text = std::to_string(policy.heal_below_hp_percent) + "%"; std::optional end; - std::string next = "Below the target: return, heal and go again."; + std::string next; + if (!hp){ + next = "Below the target and HP unknown: return, heal and go again."; + }else if (heal){ + next = "Below the target and HP below " + threshold_text + ": return, heal and go again."; + }else{ + next = "Below the target and HP at or above " + threshold_text + ": return and go again without healing."; + } if (kills >= policy.kill_target){ end = BBQAutoBattleResult::TargetReached; next = "Target reached: return without healing."; @@ -1297,7 +1309,11 @@ BBQAutoBattleResult run_auto_battle( if (end){ return *end; } - actions.heal(); + //Skipping the heal also skips its re-fly: return_to_plaza() already lands on the Central + //Plaza fly point, which is where the flight to Chargestone starts from. + if (heal){ + actions.heal(); + } } return BBQAutoBattleResult::TargetReached; } @@ -1423,6 +1439,7 @@ void quest_auto_battle( policy.kill_target = 30; policy.time_limit = std::chrono::minutes(60); } + policy.heal_below_hp_percent = BBQ_OPTIONS.AUTO_BATTLE_HEAL_PERCENT; ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp); run_auto_battle(stream.logger(), actions, policy); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 6cad2bd92b..94e73c6d1a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -196,6 +196,8 @@ struct BBQAutoBattlePolicy{ uint64_t max_empty_passes = 3; //Give up after the pass that ends past this time. std::chrono::minutes time_limit = std::chrono::minutes(35); + //Between passes, heal only if the lead's HP is below this percentage or unknown. + uint8_t heal_below_hp_percent = 50; }; //Why run_auto_battle() returned. From 0667c79ac18ab37cce6fc0c896d849cbb6c55317 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 23:51:13 -0700 Subject: [PATCH 6/8] BBQ: count kills heard on the way back in the Auto Battle pass log The kill sound is heard in every step, not only while clearing: the lead can finish a kill after the clear ends, as the flight back starts. Each pass counted kills from its own start, so such a kill went into the total but into no pass ("pass 2: 1 kills, 4/10" after "2/10" in R3b). Each pass now counts the kills since the previous pass's line, so the passes add up to the total. Behavior change: when kills heard on the way back reach the target, stop without healing instead of healing and then stopping. 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 | 14 +++++- 2 files changed, 60 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 e5e7fad2a0..196e65e8e3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -106,6 +106,9 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ std::vector kills_per_clear; // The lead's HP read in each clear, in turn. Past the end, it is unknown. std::vector> hp_per_clear; + // Kills heard on each flight back, in turn: the lead finishing a kill + // after the clear ends. Past the end, none. + std::vector kills_per_return; std::vector steps; int seconds = 0; @@ -136,6 +139,10 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ virtual void return_to_plaza() override{ steps.push_back("return"); seconds += RETURN_SECONDS; + if (m_returns < kills_per_return.size()){ + m_kills += kills_per_return[m_returns]; + } + m_returns++; } virtual void heal() override{ steps.push_back("heal"); @@ -145,6 +152,7 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ private: uint64_t m_kills = 0; size_t m_clears = 0; + size_t m_returns = 0; }; // One pass as it shows in the expected steps. @@ -172,6 +180,7 @@ struct BBQAutoBattleScenario{ int time_limit_minutes = 35; std::vector kills_per_clear; std::vector> hp_per_clear = {}; // Unknown when not given. + std::vector kills_per_return = {}; // None when not given. int heal_below_hp_percent = 50; BBQAutoBattleResult expected_result = BBQAutoBattleResult::TargetReached; std::string expected_steps; @@ -190,6 +199,7 @@ class Test_BBQAutoBattle : public UnitTest{ ScriptedAutoBattle actions; actions.kills_per_clear = m_scenario.kills_per_clear; actions.hp_per_clear = m_scenario.hp_per_clear; + actions.kills_per_return = m_scenario.kills_per_return; RecordingLogger logger; BBQAutoBattlePolicy policy; @@ -394,6 +404,43 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ .expected_steps = passes(3, LAST_PASS), .expected_seconds = 3 * (PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS), }, + { + // R3b: "pass 2: 1 kills, 4/10 in total" after "2/10". A kill heard after a pass's + // line counts in the next one, so the passes add up to the total. + .name = "a kill heard on the way back counts in the next pass", + .kills_per_clear = {2, 1, 3, 4}, + .kills_per_return = {1}, + .expected_steps = passes(3) + " " + LAST_PASS, + .expected_seconds = 4 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, + .expected_log = { + "Auto Battle pass 1: 2 kills, 2/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 2 kills, 4/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 3: 3 kills, 7/10 in total, HP ?, 250 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 4: 4 kills, 11/10 in total, HP ?, 346 s elapsed. Target reached: return without healing.", + }, + }, + { + // A pass whose only kill was heard on the previous flight back is not empty. + .name = "a kill heard on the way back resets the empty passes", + .kills_per_clear = {0, 0, 0, 0, 0, 0}, + .kills_per_return = {0, 1}, + .expected_result = BBQAutoBattleResult::NoKills, + .expected_steps = passes(5) + " " + LAST_PASS, + .expected_seconds = 6 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + }, + { + .name = "a kill heard on the way back that reaches the target skips the heal", + .kills_per_clear = {3, 3, 3}, + .kills_per_return = {0, 0, 1}, + .expected_steps = passes(2) + " " + LAST_PASS, + .expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS, + .expected_log = { + "Auto Battle pass 1: 3 kills, 3/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 3 kills, 6/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 3: 3 kills, 9/10 in total, HP ?, 250 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle: kills heard on the way back reached the target, 10/10. Stop without healing.", + }, + }, }; for (const BBQAutoBattleScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index 679b833db9..f23f8e1d88 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -1259,9 +1259,12 @@ BBQAutoBattleResult run_auto_battle( const WallClock start = actions.now(); uint64_t pass = 0; uint64_t empty_passes = 0; + //The kill sound is heard in every step, not only while clearing: the lead can finish a kill + //after the clear ends, as the flight back starts. Each pass counts the kills since the last + //pass log, so a kill heard after it goes to the next pass and the passes add up to the total. + uint64_t kills_before = actions.kills(); while (actions.kills() < policy.kill_target){ pass++; - const uint64_t kills_before = actions.kills(); actions.fly_to_battle_area(); const std::optional hp = actions.clear(policy.kill_target); @@ -1303,12 +1306,21 @@ BBQAutoBattleResult run_auto_battle( + std::to_string(elapsed.count()) + " s elapsed. " + next ); + kills_before = kills; //The next quest starts from Central Plaza, so return even when done. actions.return_to_plaza(); if (end){ return *end; } + if (actions.kills() >= policy.kill_target){ + logger.log( + "Auto Battle: kills heard on the way back reached the target, " + + std::to_string(actions.kills()) + "/" + std::to_string(policy.kill_target) + + ". Stop without healing." + ); + return BBQAutoBattleResult::TargetReached; + } //Skipping the heal also skips its re-fly: return_to_plaza() already lands on the Central //Plaza fly point, which is where the flight to Chargestone starts from. if (heal){ From a48d5419566483598435f3e34733bbfc23e26a60 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 27 Oct 2026 23:52:44 -0700 Subject: [PATCH 7/8] BBQ: near the Auto Battle target, stop when the quest leaves the board (C10) The heard kill count runs about one short of the game's: a kill whose sound is missed still counts in game. In R3b, 9 kills were heard after pass 4 but the game had completed the quest, so pass 5 and its heal (~100 s) were wasted. After a pass that ends within 2 kills of the target, read the quest panel at Central Plaza (the reader the post-quest check uses, ~5 s) before healing. If the quest is gone, stop. Far from the target nothing changes. Behavior change: a board read after passes that end near the target; an attempt can end before the heard kills reach the target. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 70 +++++++++++++++++++ .../Farming/PokemonSV_BlueberryQuests.cpp | 24 ++++++- .../Farming/PokemonSV_BlueberryQuests.h | 8 +++ 3 files changed, 100 insertions(+), 2 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 196e65e8e3..c506ec8843 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -100,6 +100,7 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ static constexpr int CLEAR_TAIL_SECONDS = 6; // Skipped when the clear stops at `stop_at_kills`. static constexpr int RETURN_SECONDS = 28; // Fly back to Central Plaza. static constexpr int HEAL_SECONDS = 28; // Walk to the nurse and heal (14 s), then re-fly (14 s). + static constexpr int BOARD_SECONDS = 5; // Open the quest panel, read it, close it. // Kills heard in each clear, in turn. Running past the end throws, which // also stops a loop that would never end. @@ -109,6 +110,8 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ // Kills heard on each flight back, in turn: the lead finishing a kill // after the clear ends. Past the end, none. std::vector kills_per_return; + // Whether each board read finds the quest, in turn. Past the end, it does. + std::vector quest_on_board_per_read; std::vector steps; int seconds = 0; @@ -144,6 +147,13 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ } m_returns++; } + virtual bool quest_on_board() override{ + steps.push_back("board"); + seconds += BOARD_SECONDS; + const bool on_board = m_reads >= quest_on_board_per_read.size() || quest_on_board_per_read[m_reads]; + m_reads++; + return on_board; + } virtual void heal() override{ steps.push_back("heal"); seconds += HEAL_SECONDS; @@ -153,6 +163,7 @@ class ScriptedAutoBattle : public BBQAutoBattleActions{ uint64_t m_kills = 0; size_t m_clears = 0; size_t m_returns = 0; + size_t m_reads = 0; }; // One pass as it shows in the expected steps. @@ -170,6 +181,7 @@ std::string auto_battle_result_to_string(BBQAutoBattleResult result){ case BBQAutoBattleResult::TargetReached: return "TargetReached"; case BBQAutoBattleResult::NoKills: return "NoKills"; case BBQAutoBattleResult::TimeLimit: return "TimeLimit"; + case BBQAutoBattleResult::QuestDone: return "QuestDone"; } return "?"; } @@ -181,6 +193,8 @@ struct BBQAutoBattleScenario{ std::vector kills_per_clear; std::vector> hp_per_clear = {}; // Unknown when not given. std::vector kills_per_return = {}; // None when not given. + uint64_t check_board_within_kills = 0; // Off unless a scenario tests it. + std::vector quest_on_board_per_read = {}; // On the board when not given. int heal_below_hp_percent = 50; BBQAutoBattleResult expected_result = BBQAutoBattleResult::TargetReached; std::string expected_steps; @@ -200,12 +214,14 @@ class Test_BBQAutoBattle : public UnitTest{ actions.kills_per_clear = m_scenario.kills_per_clear; actions.hp_per_clear = m_scenario.hp_per_clear; actions.kills_per_return = m_scenario.kills_per_return; + actions.quest_on_board_per_read = m_scenario.quest_on_board_per_read; RecordingLogger logger; BBQAutoBattlePolicy policy; policy.kill_target = m_scenario.kill_target; policy.time_limit = std::chrono::minutes(m_scenario.time_limit_minutes); policy.heal_below_hp_percent = (uint8_t)m_scenario.heal_below_hp_percent; + policy.check_board_within_kills = m_scenario.check_board_within_kills; BBQAutoBattleResult result; try{ result = run_auto_battle(logger, actions, policy); @@ -441,6 +457,60 @@ static void add_tests_BBQAutoBattle(UnitTestDatabase& database){ "Auto Battle: kills heard on the way back reached the target, 10/10. Stop without healing.", }, }, + { + // R3b: 9 kills heard, but the game had completed the quest. The board read + // replaces the 5th pass and its heal. + .name = "near the target, a board without the quest ends it", + .kills_per_clear = {3, 3, 3}, + .check_board_within_kills = 2, + .quest_on_board_per_read = {false}, + .expected_result = BBQAutoBattleResult::QuestDone, + .expected_steps = passes(2) + " " + LAST_PASS + " board", + .expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS + ScriptedAutoBattle::BOARD_SECONDS, + .expected_log = { + "Auto Battle pass 1: 3 kills, 3/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 3 kills, 6/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 3: 3 kills, 9/10 in total, HP ?, 250 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle: the quest is no longer on the board at 9/10 kills heard. Stop: the game completed it.", + }, + }, + { + // Each pass near the target reads the board until the quest is gone. + .name = "near the target, a board with the quest goes again", + .kills_per_clear = {4, 4, 1, 1}, + .check_board_within_kills = 2, + .quest_on_board_per_read = {true, false}, + .expected_result = BBQAutoBattleResult::QuestDone, + .expected_steps = PASS + " " + LAST_PASS + " board heal " + LAST_PASS + " board", + .expected_seconds = 3 * PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS + 2 * ScriptedAutoBattle::BOARD_SECONDS, + .expected_log = { + "Auto Battle pass 1: 4 kills, 4/10 in total, HP ?, 46 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle pass 2: 4 kills, 8/10 in total, HP ?, 148 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle: the quest is still on the board at 8/10 kills heard. Go again.", + "Auto Battle pass 3: 1 kills, 9/10 in total, HP ?, 255 s elapsed. Below the target and HP unknown: return, heal and go again.", + "Auto Battle: the quest is no longer on the board at 9/10 kills heard. Stop: the game completed it.", + }, + }, + { + // Far from the target the board is never read, and reaching the target + // needs no read. + .name = "far from the target, no board read", + .kills_per_clear = {5, 5}, + .check_board_within_kills = 2, + .quest_on_board_per_read = {false}, + .expected_steps = PASS + " " + LAST_PASS, + .expected_seconds = 2 * PASS_SECONDS - LAST_PASS_SAVED_SECONDS, + }, + { + // High HP skips the heal but not the read. + .name = "near the target with high HP, read the board without healing", + .kill_target = 30, + .kills_per_clear = {10, 10, 9, 1}, + .hp_per_clear = {0.9, 0.9, 0.9, 0.9}, + .check_board_within_kills = 2, + .expected_steps = passes(2, LAST_PASS) + " " + LAST_PASS + " board " + LAST_PASS, + .expected_seconds = 4 * (PASS_SECONDS - ScriptedAutoBattle::HEAL_SECONDS) - ScriptedAutoBattle::CLEAR_TAIL_SECONDS + ScriptedAutoBattle::BOARD_SECONDS, + }, }; for (const BBQAutoBattleScenario& scenario : scenarios){ database.add(scenario); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index f23f8e1d88..45e10cf76f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -1321,6 +1321,17 @@ BBQAutoBattleResult run_auto_battle( ); return BBQAutoBattleResult::TargetReached; } + //The heard count can fall short of the game's: a kill whose sound was missed still counts + //in game. Near the target, read the board before going again. The read (~5 s) costs much + //less than a pass and its heal. + if (actions.kills() + policy.check_board_within_kills >= policy.kill_target){ + const std::string count_text = std::to_string(actions.kills()) + "/" + std::to_string(policy.kill_target); + if (!actions.quest_on_board()){ + logger.log("Auto Battle: the quest is no longer on the board at " + count_text + " kills heard. Stop: the game completed it."); + return BBQAutoBattleResult::QuestDone; + } + logger.log("Auto Battle: the quest is still on the board at " + count_text + " kills heard. Go again."); + } //Skipping the heal also skips its re-fly: return_to_plaza() already lands on the Central //Plaza fly point, which is where the flight to Chargestone starts from. if (heal){ @@ -1339,10 +1350,13 @@ class ConsoleBBQAutoBattleActions : public BBQAutoBattleActions{ VideoStream& stream, ProControllerContext& context, LetsGoEncounterBotStats& stats, LetsGoEncounterBotTracker& tracker, - LetsGoHpWatcher& hp_watcher + LetsGoHpWatcher& hp_watcher, + const BBQOption& options, + BBQuests quest ) : m_env(env), m_stream(stream), m_context(context) , m_stats(stats), m_tracker(tracker), m_hp_watcher(hp_watcher) + , m_options(options), m_quest(quest) {} virtual uint64_t kills() const override{ @@ -1389,6 +1403,10 @@ class ConsoleBBQAutoBattleActions : public BBQAutoBattleActions{ virtual void return_to_plaza() override{ PokemonSV::return_to_plaza(m_env.program_info(), m_stream, m_context); } + virtual bool quest_on_board() override{ + const std::vector quests = read_quests(m_env.program_info(), m_stream, m_context, m_options); + return std::find(quests.begin(), quests.end(), m_quest) != quests.end(); + } virtual void heal() override{ OverworldWatcher done_healing(m_stream.logger(), COLOR_BLUE); pbf_move_left_joystick(m_context, {0, +1}, 800ms, 160ms); @@ -1418,6 +1436,8 @@ class ConsoleBBQAutoBattleActions : public BBQAutoBattleActions{ LetsGoEncounterBotStats& m_stats; LetsGoEncounterBotTracker& m_tracker; LetsGoHpWatcher& m_hp_watcher; + const BBQOption& m_options; + BBQuests m_quest; }; } @@ -1453,7 +1473,7 @@ void quest_auto_battle( } policy.heal_below_hp_percent = BBQ_OPTIONS.AUTO_BATTLE_HEAL_PERCENT; - ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp); + ConsoleBBQAutoBattleActions actions(env, stream, context, stats, tracker, sensors.lets_go_hp, BBQ_OPTIONS, current_quest); run_auto_battle(stream.logger(), actions, policy); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 94e73c6d1a..daca511d04 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -185,6 +185,10 @@ class BBQAutoBattleActions{ //Fly back to Central Plaza. Leaving the cave respawns its Pokemon. virtual void return_to_plaza() = 0; + //At Central Plaza, read the quest panel. Returns whether this quest is still on it: the game + //removes a quest once it is completed. + virtual bool quest_on_board() = 0; + //Walk to the nurse and heal, then fly to the Central Plaza fly point again: the flight to //Chargestone moves the map cursor from there. virtual void heal() = 0; @@ -198,6 +202,9 @@ struct BBQAutoBattlePolicy{ std::chrono::minutes time_limit = std::chrono::minutes(35); //Between passes, heal only if the lead's HP is below this percentage or unknown. uint8_t heal_below_hp_percent = 50; + //After a pass that ends this close to the target, read the board before going again, and + //stop if the quest is gone. 0 never reads it. + uint64_t check_board_within_kills = 2; }; //Why run_auto_battle() returned. @@ -205,6 +212,7 @@ enum class BBQAutoBattleResult{ TargetReached, NoKills, //max_empty_passes passes in a row had no kill. TimeLimit, + QuestDone, //The quest left the board before the heard kills reached the target. }; //Clear Chargestone Cavern, one pass at a time, until the kills reach the target, and return why From daa1de34f820ff5feb1e34a266014dc246b7a067 Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 26 Oct 2026 22:51:24 -0700 Subject: [PATCH 8/8] 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