From 2779423d94539cf2674b7aafe0a4ed733e4201cd Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 26 Oct 2026 22:51:24 -0700 Subject: [PATCH 1/2] 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 70cf43c724dd7fa91cc0eb32063e777999fb52b6 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 08:20:02 -0700 Subject: [PATCH 2/2] BBQ Farmer: catch the target the battle watcher race used to flee quest_catch() watches for the battle menu twice: an inner wait_until() and the outer EncounterWatcher. When the inner one saw the menu first, run_until() returned -1, the battle was treated as "no battle", and return_to_plaza() fled from the target. Logs show 7 of 22 catch-quest battles lost this way; no battle ever started after the 25 s timeout. - Count the inner watcher's battle menu as a battle. - Catch Any: catch a battle that starts on the way back to Central Plaza until this attempt has battled (return_to_plaza_unless_battle(), opt-in; return_to_plaza() is unchanged). Typed catch quests still flee. - Skip the Pokecenter heal and re-fly after an attempt with no battle: the player is still on the Central Plaza fly point (~28 s per attempt). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Programs/Farming/PokemonSV_BBQTests.cpp | 75 +++++++++++++++++++ .../Farming/PokemonSV_BlueberryCatchPhoto.cpp | 37 ++++++++- .../Farming/PokemonSV_BlueberryCatchPhoto.h | 5 ++ .../PokemonSV/Programs/PokemonSV_Terarium.cpp | 23 +++++- .../PokemonSV/Programs/PokemonSV_Terarium.h | 4 + 5 files changed, 140 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index 999661c313..75e4a8452a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "PokemonSV_BlueberryCatchPhoto.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQTests.h" @@ -66,8 +67,82 @@ static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ } +namespace{ + +// A catch quest, whether this attempt already battled, and whether a wild +// battle on the way back to Central Plaza should be caught instead of fled. +struct BBQCatchOnReturnScenario{ + std::string name; + BBQuests quest; + bool battled_this_attempt = false; + bool expected_catch = false; +}; + +class Test_BBQCatchOnReturn : public UnitTest{ +public: + Test_BBQCatchOnReturn(BBQCatchOnReturnScenario scenario) + : UnitTest("PokemonSV::BBQCatchOnReturn - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool got = catch_quest_catches_on_return(m_scenario.quest, m_scenario.battled_this_attempt); + if (got != m_scenario.expected_catch){ + return std::string("Expected ") + (m_scenario.expected_catch ? "catch" : "flee") + + ", got " + (got ? "catch" : "flee"); + } + return true; + } + +private: + const BBQCatchOnReturnScenario m_scenario; +}; + +} + +static void add_tests_BBQCatchOnReturn(UnitTestDatabase& database){ + using enum BBQuests; + const BBQCatchOnReturnScenario scenarios[] = { + { + // The target spawned late or not at all: any wild Pokemon completes the quest. + .name = "catch any without a battle catches", + .quest = catch_any, + .expected_catch = true, + }, + { + // Already caught this attempt: fleeing is faster than a second catch. + .name = "catch any after a battle flees", + .quest = catch_any, + .battled_this_attempt = true, + }, + { + // The type of a wild Pokemon is not read in battle. + .name = "catch normal without a battle flees", + .quest = catch_normal, + }, + { + .name = "catch fire without a battle flees", + .quest = catch_fire, + }, + { + .name = "catch psychic without a battle flees", + .quest = catch_psychic, + }, + { + .name = "catch fighting after a battle flees", + .quest = catch_fighting, + .battled_this_attempt = true, + }, + }; + for (const BBQCatchOnReturnScenario& scenario : scenarios){ + database.add(scenario); + } +} + + void add_tests_BBQ(UnitTestDatabase& database){ add_tests_TeraEncounterAction(database); + add_tests_BBQCatchOnReturn(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp index 3a24e1d187..f357faf0ac 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp @@ -893,6 +893,10 @@ void quest_catch_handle_battle( } } +bool catch_quest_catches_on_return(BBQuests current_quest, bool battled_this_attempt){ + return current_quest == BBQuests::catch_any && !battled_this_attempt; +} + void quest_catch( const ProgramInfo& info, ConsoleHandle& console, ProControllerContext& context, @@ -900,6 +904,7 @@ void quest_catch( BBQuests current_quest ){ EncounterWatcher encounter_watcher(console, COLOR_RED); + bool battle_menu_seen = false; //Navigate to target and start battle int ret = run_until( @@ -915,7 +920,11 @@ void quest_catch( std::chrono::seconds(25), { battle_menu } ); - if (ret2 != 0){ + if (ret2 == 0){ + //encounter_watcher looks for the same battle menu. If this watcher + //sees it first, run_until() returns -1 even though a battle started. + battle_menu_seen = true; + }else{ console.log("Did not enter battle. Did target spawn?"); } }, @@ -926,7 +935,8 @@ void quest_catch( ); encounter_watcher.throw_if_no_sound(); - if (ret >= 0){ + bool battled = false; + if (ret >= 0 || battle_menu_seen){ console.log("Battle menu detected."); bool is_shiny = (bool)encounter_watcher.shiny_screenshot(); @@ -936,10 +946,22 @@ void quest_catch( throw ProgramFinishedException(); }else{ quest_catch_handle_battle(info, console, context, BBQ_OPTIONS, current_quest); + battled = true; } } - return_to_plaza(info, console, context); + while (true){ + if (!catch_quest_catches_on_return(current_quest, battled)){ + return_to_plaza(info, console, context); + break; + } + if (!return_to_plaza_unless_battle(info, console, context)){ + break; + } + console.log("Battle on the way back to Central Plaza. Any wild Pokemon counts: catching it."); + quest_catch_handle_battle(info, console, context, BBQ_OPTIONS, current_quest); + battled = true; + } //Day skip and attempt to respawn fixed encounters pbf_press_button(context, BUTTON_HOME, 160ms, GameSettings::instance().GAME_TO_HOME_DELAY1); @@ -948,6 +970,15 @@ void quest_catch( pbf_press_button(context, BUTTON_HOME, 160ms, ConsoleSettings::instance().SETTINGS_TO_HOME_DELAY0); resume_game_from_home(console, context); + //Nothing to heal without a battle (return_to_plaza() flees with a Smoke Ball). + //The player is still on the Central Plaza fly point, where return_to_plaza() + //landed, so the central_to_* routes need no re-fly either. + if (!battled){ + console.log("No battle this attempt. Skipping the Pokecenter heal."); + context.wait_for_all_requests(); + return; + } + //Heal up and then reset position again. OverworldWatcher done_healing(console.logger(), COLOR_BLUE); pbf_move_left_joystick(context, {0, +1}, 800ms, 160ms); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h index 862285c3c3..4390d29a74 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h @@ -66,6 +66,11 @@ void quest_catch_handle_battle( BBQuests current_quest ); +//Whether quest_catch() catches a wild battle that starts on the way back to Central Plaza +//instead of fleeing it. Any wild pokemon completes catch_any, so until this attempt has +//battled, the battle is a catch. Typed catch quests flee: the type is not read in battle. +bool catch_quest_catches_on_return(BBQuests current_quest, bool battled_this_attempt); + //Catch a pokemon void quest_catch( const ProgramInfo& info, diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp index aae34299d3..a0e7f7fadd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp @@ -23,7 +23,14 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ -void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ +namespace{ + +// Returns true if it stopped at a wild battle's menu (only when stop_at_battle +// is set). Otherwise it flees each battle and returns false once at the plaza. +bool return_to_plaza_impl( + const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, + bool stop_at_battle +){ stream.log("Attempting to return to Central Plaza."); //Modified version of handle_battles_and_back_to_pokecenter() bool returned_to_pokecenter = false; @@ -91,6 +98,9 @@ void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProController stream.log("Shiny detected!"); pbf_press_button(context, BUTTON_CAPTURE, 2000ms, 5000ms); throw ProgramFinishedException(); + }else if (stop_at_battle){ + stream.log("Detected battle. Stopping at the battle menu."); + return true; }else{ stream.log("Detected battle. Running from battle."); try{ @@ -110,6 +120,17 @@ void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProController } } context.wait_for_all_requests(); + return false; +} + +} + +void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ + return_to_plaza_impl(info, stream, context, false); +} + +bool return_to_plaza_unless_battle(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ + return return_to_plaza_impl(info, stream, context, true); } void map_move_cursor_fly( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h index 8c21530cea..546193bc79 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h @@ -18,6 +18,10 @@ namespace PokemonSV{ // Return to Central Plaza from anywhere in the terarium void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context); +// Same as return_to_plaza(), but stops at the first wild battle instead of fleeing it. +// Returns true with the battle menu open, or false once at Central Plaza. +bool return_to_plaza_unless_battle(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context); + // Open map from overworld, move cursor to fly location, and fly to location void map_move_cursor_fly( const ProgramInfo& info,