diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 1333bbaf22..2625ceac47 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -270,8 +270,10 @@ class ScriptedPlazaReturnActions : public PlazaReturnActions{ : m_landings(std::move(landings)) {} - virtual void fly_to_plaza(bool overshoot) override{ + virtual bool fly_to_plaza(bool overshoot) override{ + const size_t flight = overshoots.size(); overshoots.push_back(overshoot); + return flight >= fly_results.size() || fly_results[flight]; } virtual std::string read_landing_banner(const std::function& matches) override{ const size_t landing = banner_reads++; @@ -291,6 +293,8 @@ class ScriptedPlazaReturnActions : public PlazaReturnActions{ } public: + // Whether each fly happened; flies past the end do. + std::vector fly_results; std::vector overshoots; size_t banner_reads = 0; @@ -308,6 +312,7 @@ struct PlazaReturnCase{ std::string name; std::string expected_banner; std::vector> landings = {}; + std::vector fly_results = {}; PlazaReturnResult result; std::vector overshoots; size_t banner_reads; @@ -323,6 +328,7 @@ class Test_BBQPlazaReturn : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ NullLogger logger; ScriptedPlazaReturnActions actions(m_case.landings); + actions.fly_results = m_case.fly_results; const PlazaReturnResult result = return_to_plaza_checked(logger, actions, m_case.expected_banner, 3); if (result != m_case.result){ return "Expected result " + plaza_return_result_to_string(m_case.result) @@ -387,6 +393,44 @@ void add_tests_BBQPlazaReturn(UnitTestDatabase& database){ .overshoots = {false}, .banner_reads = 0, }, + { + // No Pokecenter to fly to, or a battle on the way: no landing to + // read, and the next attempt overshoots. + .name = "a fly that didn't happen uses an attempt, then arrives", + .expected_banner = "中心廣場", + .landings = {{"中心廣場"}}, + .fly_results = {false}, + .result = PlazaReturnResult::ARRIVED, + .overshoots = {false, true}, + .banner_reads = 1, + }, + { + // bbq-m5 11:20: the fly kept failing and the return looped for + // minutes. Now it fails after 3 flies. + .name = "flies that never happen fail the return", + .expected_banner = "中心廣場", + .fly_results = {false, false, false}, + .result = PlazaReturnResult::FAILED, + .overshoots = {false, true, true}, + .banner_reads = 0, + }, + { + .name = "a failed fly and a wrong landing both count", + .expected_banner = "Central Plaza", + .landings = {{"Coastal Plaza"}}, + .fly_results = {false, true, false}, + .result = PlazaReturnResult::FAILED, + .overshoots = {false, true, true}, + .banner_reads = 1, + }, + { + .name = "nothing to check retries a fly that didn't happen", + .expected_banner = "", + .fly_results = {false}, + .result = PlazaReturnResult::NOT_CHECKED, + .overshoots = {false, true}, + .banner_reads = 0, + }, }; for (const PlazaReturnCase& test_case : cases){ database.add(test_case); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp index 1440787cff..358b3ba026 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp @@ -29,14 +29,16 @@ namespace PokemonSV{ namespace{ -// Flies to the closest Pokecenter on the map. Nothing here checks where the -// player lands: the closest-Pokecenter search can fall back to any fly point. -void fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, bool laggy){ +// Flies to the closest Pokecenter on the map, once. Nothing here checks where +// the player lands: the closest-Pokecenter search can fall back to any fly +// point. Returns false if the fly failed or a battle interrupted it; the +// caller decides whether to try again, so one return can't loop for minutes. +bool fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, bool laggy){ stream.log("Attempting to return to Central Plaza."); //Modified version of handle_battles_and_back_to_pokecenter() bool returned_to_pokecenter = false; - while(!returned_to_pokecenter){ + { EncounterWatcher encounter_watcher(stream, COLOR_RED); int ret = run_until( stream, context, @@ -74,13 +76,15 @@ void fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControlle pbf_press_button(context, BUTTON_ZR, 320ms, 800ms); try{ - //The only pokecenter on the map is Central Plaza - fly_to_closest_pokecenter_on_map(info, stream, context); + //The only pokecenter on the map is Central Plaza. Each failed search + //costs ~13 s, and repeating the same one rarely helps (bbq-m5 11:20: + //17 identical "no Pokecenter" tries), so give up after 3. + fly_to_closest_pokecenter_on_map(info, stream, context, 3); context.wait_for_all_requests(); returned_to_pokecenter = true; }catch (...){ - stream.log("Failed to return to Pokecenter. Closing map and retrying."); - laggy = true; + //The next try mashes B back to the overworld first. + stream.log("Failed to return to Pokecenter.", COLOR_RED); } context.wait_for_all_requests(); }, @@ -117,6 +121,7 @@ void fly_toward_plaza(const ProgramInfo& info, VideoStream& stream, ProControlle } } context.wait_for_all_requests(); + return returned_to_pokecenter; } class PlazaReturnConsoleActions : public PlazaReturnActions{ @@ -131,8 +136,8 @@ class PlazaReturnConsoleActions : public PlazaReturnActions{ , m_language(language) {} - virtual void fly_to_plaza(bool overshoot) override{ - fly_toward_plaza(m_info, m_stream, m_context, overshoot); + virtual bool fly_to_plaza(bool overshoot) override{ + return fly_toward_plaza(m_info, m_stream, m_context, overshoot); } // The banner fades in shortly after the overworld is back and stays for diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp index 58befc2f7e..fe02f3dad3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp @@ -91,7 +91,10 @@ PlazaReturnResult return_to_plaza_checked( // A wrong landing usually means the cursor stopped short of Central // Plaza, so retries overshoot from the start. - actions.fly_to_plaza(attempt > 1); + if (!actions.fly_to_plaza(attempt > 1)){ + logger.log(prefix + "the fly didn't happen.", COLOR_RED); + continue; + } if (expected_banner.empty()){ logger.log(prefix + "landed. Not checking where: no place name to check in this language.", COLOR_ORANGE); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h index acdb91567a..33b823d6d3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h @@ -40,8 +40,9 @@ class PlazaReturnActions{ // Fly toward Central Plaza the way return_to_plaza() always has: map // cursor to the top-left corner, back toward the center, then the // closest Pokecenter. "overshoot" moves the cursor back further, which - // helps when the game lags. - virtual void fly_to_plaza(bool overshoot) = 0; + // helps when the game lags. Tries once: returns false if it didn't fly + // (no Pokecenter to fly to, or a battle interrupted it). + virtual bool fly_to_plaza(bool overshoot) = 0; // Watch the place-name banner shown after landing. Returns the first // text read that `matches` accepts, else the last non-empty text read @@ -57,7 +58,8 @@ enum class PlazaReturnResult{ std::string plaza_return_result_to_string(PlazaReturnResult result); // Fly toward Central Plaza and check the landing banner, up to -// `max_attempts` times. `expected_banner` comes from +// `max_attempts` times. A fly that didn't happen uses up an attempt too, so +// the return is bounded. `expected_banner` comes from // central_plaza_banner_to_check(); when it is empty the first landing is // accepted unchecked. PlazaReturnResult return_to_plaza_checked( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp index 9867d96edd..5230e09658 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp @@ -2,6 +2,7 @@ * */ +#include #include #include #include @@ -532,8 +533,9 @@ bool fly_to_visible_closest_flypoint_cur_zoom_level( } -void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ +void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, int max_tries){ const int MAX_TRY_COUNT = 17; + const int try_limit = std::clamp(max_tries, 1, MAX_TRY_COUNT); int try_count = 0; // Part 1: Tries to detect a pokecenter that is very close to the player // Zoom in one level onto the map. @@ -557,7 +559,7 @@ void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stre break; }catch (OperationFailedException&){ // pokecenter was detected, but failed to fly there try_count++; - if (try_count >= MAX_TRY_COUNT){ + if (try_count >= try_limit){ OperationFailedExceptionWithScreenshot::fire( ErrorReportMode::SEND_ERROR_REPORT, "fly_to_closest_pokecenter_on_map(): At min warpable map level, pokecenter was detected, but failed to fly there.", @@ -609,7 +611,7 @@ void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stre } }catch (OperationFailedException&){ try_count++; - if (try_count >= MAX_TRY_COUNT){ + if (try_count >= try_limit){ // either: // - pokecenter was detected, but failed to fly there. // - could not find pokecenter icon. diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h index 9c995410ad..fb4226d5ef 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h @@ -119,7 +119,8 @@ bool fly_to_visible_closest_flypoint_cur_zoom_level( // While on map (default zoom), move to the closest PokeCenter and fly there. // The PokeCenter must be already visited before (so having the little wing icon with it) and not occluded // by other map icons on the most zoomed-in level of the map. -void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context); +// Each of its two searches gives up after `max_tries` failed flies (at most 17). +void fly_to_closest_pokecenter_on_map(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, int max_tries = 17); // Attempt to escape being stuck on a wall. // Repeatedly center camera and try to backwards jump off.