diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp index aa8ef76ecb..3fcd534bcf 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -9,6 +9,7 @@ #include "Tests/ImageUnitTest.h" #include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" #include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" +#include "PokemonSV_BlueberryCatchPhoto.h" #include "PokemonSV_BlueberryQuests.h" #include "PokemonSV_BBQTests.h" @@ -122,6 +123,79 @@ 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){ database.add(); @@ -134,6 +208,7 @@ void add_tests_BBQ(UnitTestDatabase& database){ database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); 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,