diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.cpp new file mode 100644 index 0000000000..3d5e9a6ee0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.cpp @@ -0,0 +1,56 @@ +/* Place Name Banner Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "PokemonSV_PlaceNameBannerReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// The banner is centered horizontally. On a lab screenshot (2026-10-22, +// Chinese (Traditional), landing at Central Plaza) the name 中心廣場 spans +// x 0.438-0.562, y 0.167-0.222, with the blue line under it at y 0.233 and +// the smaller "Terarium" subtitle below that. The read area stays above the +// line and is wider than the name, so that longer names fit. +PlaceNameBannerReader::PlaceNameBannerReader() + : m_box(0.300, 0.155, 0.400, 0.070) +{} + +void PlaceNameBannerReader::make_overlays(VideoOverlaySet& items) const{ + items.add(COLOR_BLUE, m_box); +} + +std::vector PlaceNameBannerReader::read(Logger& logger, Language language, const ImageViewRGB32& screen) const{ + // The name is white, drawn straight over the scene. Keep only the + // brightest pixels; the scene behind it changes with the time of day, + // so try a few thresholds. + static const uint32_t MINS[] = {0xffe0e0e0, 0xffc0c0c0}; + + const ImageViewRGB32 cropped = extract_box_reference(screen, m_box); + std::vector ret; + for (uint32_t mins : MINS){ + ImageRGB32 filtered = to_blackwhite_rgb32_range(cropped, true, mins, 0xffffffff); + std::string text; + for (char ch : OCR::ocr_read(language, filtered)){ + if ((unsigned char)ch < 32){ + continue; + } + text += ch; + } + logger.log("Place name banner: \"" + text + "\""); + ret.emplace_back(std::move(text)); + } + return ret; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.h new file mode 100644 index 0000000000..899ad23864 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.h @@ -0,0 +1,45 @@ +/* Place Name Banner Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Read the place name the game shows near the top of the screen after + * landing from a fly, e.g. "Central Plaza" over a blue line. + * + */ + +#ifndef PokemonAutomation_PokemonSV_PlaceNameBannerReader_H +#define PokemonAutomation_PokemonSV_PlaceNameBannerReader_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +class PlaceNameBannerReader{ +public: + PlaceNameBannerReader(); + + void make_overlays(VideoOverlaySet& items) const; + + // OCR the banner's white text, once per brightness threshold. Returns + // the raw OCR text of each read, which is empty (or noise) when there + // is no banner. + std::vector read(Logger& logger, Language language, const ImageViewRGB32& screen) const; + +private: + ImageFloatBox m_box; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 80f1d0b6f8..1333bbaf22 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -27,6 +27,7 @@ #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" #include "Options/PokemonSV_BBQuests.h" #include "Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "Programs/PokemonSV_TerariumPlazaReturn.h" #include "PokemonSV_Tests.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "Tests/ImageUnitTest.h" @@ -148,6 +149,254 @@ class Test_BlueberryPoints : public ImageUnitTest{ } +namespace{ + +std::string join_bools(const std::vector& items){ + std::string ret; + for (bool item : items){ + if (!ret.empty()){ + ret += ", "; + } + ret += item ? "true" : "false"; + } + return "{" + ret + "}"; +} + + +struct PlazaBannerToCheckCase{ + std::string name; + Language language; + bool ocr_available; + std::string expected; +}; + +class Test_BBQPlazaBannerToCheck : public UnitTest{ +public: + Test_BBQPlazaBannerToCheck(PlazaBannerToCheckCase test_case) + : UnitTest("PokemonSV::BBQPlazaBannerToCheck - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string banner = central_plaza_banner_to_check(m_case.language, m_case.ocr_available); + if (banner != m_case.expected){ + return "Expected \"" + m_case.expected + "\", got \"" + banner + "\""; + } + return true; + } + +private: + const PlazaBannerToCheckCase m_case; +}; + +void add_tests_BBQPlazaBannerToCheck(UnitTestDatabase& database){ + const PlazaBannerToCheckCase cases[] = { + {.name = "english", .language = Language::English, .ocr_available = true, .expected = "Central Plaza"}, + {.name = "chinese traditional", .language = Language::ChineseTraditional, .ocr_available = true, .expected = "中心廣場"}, + {.name = "no OCR data", .language = Language::ChineseTraditional, .ocr_available = false, .expected = ""}, + {.name = "name not known yet", .language = Language::Japanese, .ocr_available = true, .expected = ""}, + {.name = "no language", .language = Language::None, .ocr_available = true, .expected = ""}, + }; + for (const PlazaBannerToCheckCase& test_case : cases){ + database.add(test_case); + } +} + + +struct PlazaBannerMatchCase{ + std::string name; + std::string expected; + std::string ocr_text; + bool matches; +}; + +class Test_BBQPlazaBannerMatch : public UnitTest{ +public: + Test_BBQPlazaBannerMatch(PlazaBannerMatchCase test_case) + : UnitTest("PokemonSV::BBQPlazaBannerMatch - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const bool matches = place_name_banner_matches(m_case.expected, m_case.ocr_text); + if (matches != m_case.matches){ + return "Banner \"" + m_case.ocr_text + "\" against \"" + m_case.expected + "\": expected " + + (m_case.matches ? "a match" : "no match") + ", got " + + (matches ? "a match" : "no match"); + } + return true; + } + +private: + const PlazaBannerMatchCase m_case; +}; + +void add_tests_BBQPlazaBannerMatch(UnitTestDatabase& database){ + const PlazaBannerMatchCase cases[] = { + {.name = "chinese as seen in the lab", .expected = "中心廣場", .ocr_text = "中心廣場", .matches = true}, + {.name = "chinese with spaces", .expected = "中心廣場", .ocr_text = "中 心\xE3\x80\x80廣 場", .matches = true}, + {.name = "chinese with noise around it", .expected = "中心廣場", .ocr_text = "|一中心廣場。", .matches = true}, + {.name = "chinese, one character misread", .expected = "中心廣場", .ocr_text = "中心廣塲", .matches = true}, + {.name = "chinese, one character missing", .expected = "中心廣場", .ocr_text = "中心場", .matches = true}, + {.name = "chinese, coastal plaza", .expected = "中心廣場", .ocr_text = "海岸廣場", .matches = false}, + {.name = "chinese, savanna plaza", .expected = "中心廣場", .ocr_text = "草原廣場", .matches = false}, + {.name = "chinese, canyon plaza", .expected = "中心廣場", .ocr_text = "峽谷廣場", .matches = false}, + {.name = "chinese, polar plaza", .expected = "中心廣場", .ocr_text = "極地廣場", .matches = false}, + {.name = "chinese, only half the name", .expected = "中心廣場", .ocr_text = "廣場", .matches = false}, + {.name = "english", .expected = "Central Plaza", .ocr_text = "Central Plaza", .matches = true}, + {.name = "english, case and punctuation", .expected = "Central Plaza", .ocr_text = " CENTRAL plaza.", .matches = true}, + {.name = "english, two characters misread", .expected = "Central Plaza", .ocr_text = "Centra1 P1aza", .matches = true}, + {.name = "english, coastal plaza", .expected = "Central Plaza", .ocr_text = "Coastal Plaza", .matches = false}, + {.name = "english, canyon plaza", .expected = "Central Plaza", .ocr_text = "Canyon Plaza", .matches = false}, + {.name = "english, savanna plaza", .expected = "Central Plaza", .ocr_text = "Savanna Plaza", .matches = false}, + {.name = "english, polar plaza", .expected = "Central Plaza", .ocr_text = "Polar Plaza", .matches = false}, + {.name = "english, chargestone cavern", .expected = "Central Plaza", .ocr_text = "Chargestone Cavern", .matches = false}, + {.name = "nothing read", .expected = "中心廣場", .ocr_text = "", .matches = false}, + {.name = "only punctuation read", .expected = "Central Plaza", .ocr_text = " .|- ", .matches = false}, + {.name = "nothing expected", .expected = "", .ocr_text = "Central Plaza", .matches = false}, + }; + for (const PlazaBannerMatchCase& test_case : cases){ + database.add(test_case); + } +} + + +// Plays back the banner texts read after each landing, in order. Each +// landing is a list of reads (frames x thresholds), handed to `matches` the +// way the console reads them. +class ScriptedPlazaReturnActions : public PlazaReturnActions{ +public: + ScriptedPlazaReturnActions(std::vector> landings) + : m_landings(std::move(landings)) + {} + + virtual void fly_to_plaza(bool overshoot) override{ + overshoots.push_back(overshoot); + } + virtual std::string read_landing_banner(const std::function& matches) override{ + const size_t landing = banner_reads++; + if (landing >= m_landings.size()){ + return ""; + } + std::string last; + for (const std::string& text : m_landings[landing]){ + if (matches(text)){ + return text; + } + if (!text.empty()){ + last = text; + } + } + return last; + } + +public: + std::vector overshoots; + size_t banner_reads = 0; + +private: + const std::vector> m_landings; +}; + +class NullLogger : public Logger{ +public: + virtual void log(const std::string&, Color) override{} +}; + + +struct PlazaReturnCase{ + std::string name; + std::string expected_banner; + std::vector> landings = {}; + PlazaReturnResult result; + std::vector overshoots; + size_t banner_reads; +}; + +class Test_BBQPlazaReturn : public UnitTest{ +public: + Test_BBQPlazaReturn(PlazaReturnCase test_case) + : UnitTest("PokemonSV::BBQPlazaReturn - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + NullLogger logger; + ScriptedPlazaReturnActions actions(m_case.landings); + 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) + + ", got " + plaza_return_result_to_string(result); + } + if (actions.overshoots != m_case.overshoots){ + return "Expected flights with overshoot " + join_bools(m_case.overshoots) + + ", got " + join_bools(actions.overshoots); + } + if (actions.banner_reads != m_case.banner_reads){ + return "Expected " + std::to_string(m_case.banner_reads) + " banner reads, got " + + std::to_string(actions.banner_reads); + } + return true; + } + +private: + const PlazaReturnCase m_case; +}; + +void add_tests_BBQPlazaReturn(UnitTestDatabase& database){ + const PlazaReturnCase cases[] = { + { + .name = "arrives first time", + .expected_banner = "中心廣場", + .landings = {{"", "中心廣場"}}, + .result = PlazaReturnResult::ARRIVED, + .overshoots = {false}, + .banner_reads = 1, + }, + { + // As in the lab on 2026-10-22, when the closest-Pokecenter + // fallback flew to another fly point. + .name = "lands elsewhere, then arrives with overshoot", + .expected_banner = "中心廣場", + .landings = {{"海岸廣場"}, {"中心廣場"}}, + .result = PlazaReturnResult::ARRIVED, + .overshoots = {false, true}, + .banner_reads = 2, + }, + { + .name = "banner not seen, then arrives", + .expected_banner = "Central Plaza", + .landings = {{"", ""}, {"", ""}, {"Central Plaza"}}, + .result = PlazaReturnResult::ARRIVED, + .overshoots = {false, true, true}, + .banner_reads = 3, + }, + { + .name = "never arrives", + .expected_banner = "Central Plaza", + .landings = {{"Coastal Plaza"}, {"Canyon Plaza"}, {""}}, + .result = PlazaReturnResult::FAILED, + .overshoots = {false, true, true}, + .banner_reads = 3, + }, + { + .name = "nothing to check", + .expected_banner = "", + .landings = {{"Coastal Plaza"}}, + .result = PlazaReturnResult::NOT_CHECKED, + .overshoots = {false}, + .banner_reads = 0, + }, + }; + for (const PlazaReturnCase& test_case : cases){ + database.add(test_case); + } +} + +} + + + void add_tests(UnitTestDatabase& database){ database.add(); @@ -179,6 +428,9 @@ void add_tests(UnitTestDatabase& database){ add_tests_TeraTypeReader(database); add_tests_NormalBattleMenus(database); add_tests_LetsGoKillDetector(database); + add_tests_BBQPlazaBannerToCheck(database); + add_tests_BBQPlazaBannerMatch(database); + add_tests_BBQPlazaReturn(database); add_tests_TeraEncounterAction(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..2be97ee526 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp @@ -360,7 +360,7 @@ void quest_photo( battle_menu.move_to_slot(console, context, 3); pbf_press_button(context, BUTTON_A, 80ms, 400ms); press_Bs_to_back_to_overworld(info, console, context); - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); }catch (...){ console.log("Unable to flee."); OperationFailedExceptionWithScreenshot::fire( @@ -373,7 +373,7 @@ void quest_photo( } } context.wait_for_all_requests(); - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); } void quest_catch_navi( @@ -939,7 +939,7 @@ void quest_catch( } } - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); //Day skip and attempt to respawn fixed encounters pbf_press_button(context, BUTTON_HOME, 160ms, GameSettings::instance().GAME_TO_HOME_DELAY1); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp index c90bae7a04..f8223b03d7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp @@ -317,102 +317,109 @@ bool process_and_do_quest( int quest_attempts = 0; while (!quest_completed){ - switch (current_quest){ - case BBQuests::make_tm: - quest_make_tm(env.program_info(), console, context); - break; - case BBQuests::travel_500: - quest_travel_500(env.program_info(), console, context); - break; - case BBQuests::tera_self_defeat: - quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::sneak_up: - quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::wild_tera: - quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); - break; - case BBQuests::wash_pokemon: - quest_wash_pokemon(env.program_info(), console, context); - break; - case BBQuests::hatch_egg: - quest_hatch_egg(env, console, context, BBQ_OPTIONS); - break; - case BBQuests::bitter_sandwich: - case BBQuests::salty_sandwich: - case BBQuests::sour_sandwich: - case BBQuests::spicy_sandwich: - case BBQuests::sweet_sandwich: - case BBQuests::sandwich_three: - quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); - break; - //case BBQuests::pickup_10: - // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); - // break; - case BBQuests::tera_raid: - quest_tera_raid(env, console, context, BBQ_OPTIONS); - break; - case BBQuests::auto_10: - case BBQuests::auto_30: - quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); - break; - //All involve taking pictures - case BBQuests::photo_fly: - case BBQuests::photo_swim: - case BBQuests::photo_canyon: - case BBQuests::photo_coastal: - case BBQuests::photo_polar: - case BBQuests::photo_savanna: - case BBQuests::photo_normal: - case BBQuests::photo_fighting: - case BBQuests::photo_flying: - case BBQuests::photo_poison: - case BBQuests::photo_ground: - case BBQuests::photo_rock: - case BBQuests::photo_bug: - case BBQuests::photo_ghost: - case BBQuests::photo_steel: - case BBQuests::photo_fire: - case BBQuests::photo_water: - case BBQuests::photo_grass: - case BBQuests::photo_electric: - case BBQuests::photo_psychic: - case BBQuests::photo_ice: - case BBQuests::photo_dragon: - case BBQuests::photo_dark: - case BBQuests::photo_fairy: - quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - break; - //All involve catching a pokemon - case BBQuests::catch_any: - case BBQuests::catch_normal: - case BBQuests::catch_fighting: - case BBQuests::catch_flying: - case BBQuests::catch_poison: - case BBQuests::catch_ground: - case BBQuests::catch_rock: - case BBQuests::catch_bug: - case BBQuests::catch_ghost: - case BBQuests::catch_steel: - case BBQuests::catch_fire: - case BBQuests::catch_water: - case BBQuests::catch_grass: - case BBQuests::catch_electric: - case BBQuests::catch_psychic: - case BBQuests::catch_ice: - case BBQuests::catch_dragon: - case BBQuests::catch_dark: - case BBQuests::catch_fairy: - quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); - break; - default: - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "Unknown quest selection.", - console - ); - break; + // A failed fly fails this attempt, not the program: the board + // check below counts it, and the retry limit applies as usual. + try{ + switch (current_quest){ + case BBQuests::make_tm: + quest_make_tm(env.program_info(), console, context); + break; + case BBQuests::travel_500: + quest_travel_500(env.program_info(), console, context); + break; + case BBQuests::tera_self_defeat: + quest_tera_self_defeat(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::sneak_up: + quest_sneak_up(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::wild_tera: + quest_wild_tera(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::wash_pokemon: + quest_wash_pokemon(env.program_info(), console, context, BBQ_OPTIONS); + break; + case BBQuests::hatch_egg: + quest_hatch_egg(env, console, context, BBQ_OPTIONS); + break; + case BBQuests::bitter_sandwich: + case BBQuests::salty_sandwich: + case BBQuests::sour_sandwich: + case BBQuests::spicy_sandwich: + case BBQuests::sweet_sandwich: + case BBQuests::sandwich_three: + quest_sandwich(env, console, context, BBQ_OPTIONS, current_quest); + break; + //case BBQuests::pickup_10: + // quest_pickup(env, env.program_info(), console, context, BBQ_OPTIONS); + // break; + case BBQuests::tera_raid: + quest_tera_raid(env, console, context, BBQ_OPTIONS); + break; + case BBQuests::auto_10: + case BBQuests::auto_30: + quest_auto_battle(env, console, context, BBQ_OPTIONS, current_quest); + break; + //All involve taking pictures + case BBQuests::photo_fly: + case BBQuests::photo_swim: + case BBQuests::photo_canyon: + case BBQuests::photo_coastal: + case BBQuests::photo_polar: + case BBQuests::photo_savanna: + case BBQuests::photo_normal: + case BBQuests::photo_fighting: + case BBQuests::photo_flying: + case BBQuests::photo_poison: + case BBQuests::photo_ground: + case BBQuests::photo_rock: + case BBQuests::photo_bug: + case BBQuests::photo_ghost: + case BBQuests::photo_steel: + case BBQuests::photo_fire: + case BBQuests::photo_water: + case BBQuests::photo_grass: + case BBQuests::photo_electric: + case BBQuests::photo_psychic: + case BBQuests::photo_ice: + case BBQuests::photo_dragon: + case BBQuests::photo_dark: + case BBQuests::photo_fairy: + quest_photo(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + break; + //All involve catching a pokemon + case BBQuests::catch_any: + case BBQuests::catch_normal: + case BBQuests::catch_fighting: + case BBQuests::catch_flying: + case BBQuests::catch_poison: + case BBQuests::catch_ground: + case BBQuests::catch_rock: + case BBQuests::catch_bug: + case BBQuests::catch_ghost: + case BBQuests::catch_steel: + case BBQuests::catch_fire: + case BBQuests::catch_water: + case BBQuests::catch_grass: + case BBQuests::catch_electric: + case BBQuests::catch_psychic: + case BBQuests::catch_ice: + case BBQuests::catch_dragon: + case BBQuests::catch_dark: + case BBQuests::catch_fairy: + quest_catch(env.program_info(), console, context, BBQ_OPTIONS, current_quest); + break; + default: + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unknown quest selection.", + console + ); + break; + } + }catch (TerariumFlyFailedException&){ + console.log("Quest attempt failed: unable to fly. Returning to Central Plaza.", COLOR_RED); + return_to_plaza(env.program_info(), console, context, BBQ_OPTIONS.LANGUAGE); } //Validate quest was completed by checking list @@ -625,7 +632,7 @@ void quest_tera_self_defeat( break; } pbf_press_button(context, BUTTON_PLUS, 160ms, 840ms); - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); //Day skip and attempt to respawn fixed encounters pbf_press_button(context, BUTTON_HOME, 160ms, GameSettings::instance().GAME_TO_HOME_DELAY1); @@ -765,7 +772,7 @@ void quest_sneak_up( } } } - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); //Day skip and attempt to respawn fixed encounters - in case quest failed pbf_press_button(context, BUTTON_HOME, 160ms, GameSettings::instance().GAME_TO_HOME_DELAY1); @@ -848,7 +855,7 @@ void quest_wild_tera( console.log("Battle entry not confirmed. Skipping battle handling."); break; } - return_to_plaza(info, console, context); + return_to_plaza(info, console, context, BBQ_OPTIONS.LANGUAGE); //Day skip and attempt to respawn fixed encounters pbf_press_button(context, BUTTON_HOME, 160ms, GameSettings::instance().GAME_TO_HOME_DELAY1); @@ -879,7 +886,7 @@ void quest_wild_tera( fly_to_overworld_from_map(info, console, context); } -void quest_wash_pokemon(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ +void quest_wash_pokemon(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, const BBQOption& BBQ_OPTIONS){ stream.log("Quest: Give your pokemon a bath!"); //Fly to savanna plaza @@ -977,7 +984,7 @@ void quest_wash_pokemon(const ProgramInfo& info, VideoStream& stream, ProControl leave_picnic(info, stream, context); - return_to_plaza(info, stream, context); + return_to_plaza(info, stream, context, BBQ_OPTIONS.LANGUAGE); context.wait_for_all_requests(); } @@ -1057,7 +1064,7 @@ void quest_hatch_egg( pbf_press_button(context, BUTTON_PLUS, 160ms, 400ms); - return_to_plaza(env.program_info(), console, context); + return_to_plaza(env.program_info(), console, context, BBQ_OPTIONS.LANGUAGE); context.wait_for_all_requests(); } } @@ -1129,7 +1136,7 @@ void quest_sandwich( leave_picnic(env.program_info(), stream, context); - return_to_plaza(env.program_info(), stream, context); + return_to_plaza(env.program_info(), stream, context, BBQ_OPTIONS.LANGUAGE); context.wait_for_all_requests(); } @@ -1189,7 +1196,7 @@ void quest_tera_raid( battle_menu.move_to_slot(console, context, 3); pbf_press_button(context, BUTTON_A, 80ms, 400ms); press_Bs_to_back_to_overworld(env.program_info(), console, context); - return_to_plaza(env.program_info(), console, context); + return_to_plaza(env.program_info(), console, context, BBQ_OPTIONS.LANGUAGE); }catch (...){ console.log("Unable to flee."); OperationFailedExceptionWithScreenshot::fire( @@ -1240,7 +1247,7 @@ void quest_tera_raid( } context.wait_for_all_requests(); - return_to_plaza(env.program_info(), console, context); + return_to_plaza(env.program_info(), console, context, BBQ_OPTIONS.LANGUAGE); day_skip_from_overworld(console, context); } @@ -1291,7 +1298,7 @@ void quest_auto_battle( }); context.wait_for_all_requests(); - return_to_plaza(env.program_info(), stream, context); + return_to_plaza(env.program_info(), stream, context, BBQ_OPTIONS.LANGUAGE); OverworldWatcher done_healing(stream.logger(), COLOR_BLUE); pbf_move_left_joystick(context, {0, +1}, 800ms, 160ms); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h index 21ad16aa71..9ae079948c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.h @@ -126,7 +126,8 @@ void quest_wild_tera( //Give bath void quest_wash_pokemon( const ProgramInfo& info, - VideoStream& stream, ProControllerContext& context + VideoStream& stream, ProControllerContext& context, + const BBQOption& BBQ_OPTIONS ); //Withdraw and hatch an egg diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp index aae34299d3..1440787cff 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp @@ -4,30 +4,37 @@ * */ +#include "Common/Cpp/Time.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/ErrorDumper.h" #include "CommonFramework/Exceptions/ProgramFinishedException.h" #include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_Routines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" +#include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_GameEntry.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_SaveGame.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_MenuNavigation.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h" +#include "PokemonSV_TerariumPlazaReturn.h" #include "PokemonSV_Terarium.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ -void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ +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){ stream.log("Attempting to return to Central Plaza."); //Modified version of handle_battles_and_back_to_pokecenter() bool returned_to_pokecenter = false; - bool laggy = false; while(!returned_to_pokecenter){ EncounterWatcher encounter_watcher(stream, COLOR_RED); @@ -112,6 +119,74 @@ void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProController context.wait_for_all_requests(); } +class PlazaReturnConsoleActions : public PlazaReturnActions{ +public: + PlazaReturnConsoleActions( + const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, + Language language + ) + : m_info(info) + , m_stream(stream) + , m_context(context) + , m_language(language) + {} + + virtual void fly_to_plaza(bool overshoot) override{ + fly_toward_plaza(m_info, m_stream, m_context, overshoot); + } + + // The banner fades in shortly after the overworld is back and stays for + // a few seconds. Read until it matches or the window ends. + virtual std::string read_landing_banner(const std::function& matches) override{ + PlaceNameBannerReader reader; + VideoOverlaySet overlays(m_stream.overlay()); + reader.make_overlays(overlays); + + std::string last; + const WallClock deadline = current_time() + std::chrono::seconds(8); + while (current_time() < deadline){ + m_context.wait_for_all_requests(); + VideoSnapshot screen = m_stream.video().snapshot(); + for (std::string& text : reader.read(m_stream.logger(), m_language, screen)){ + if (matches(text)){ + return text; + } + if (!text.empty()){ + last = std::move(text); + } + } + m_context.wait_for(std::chrono::milliseconds(250)); + } + return last; + } + +private: + const ProgramInfo& m_info; + VideoStream& m_stream; + ProControllerContext& m_context; + const Language m_language; +}; + +} + +void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, Language language){ + const bool ocr_available = OCR::ocr_language_available(language); + if (!ocr_available){ + stream.log("No OCR data for the game language. Central Plaza arrivals won't be checked.", COLOR_ORANGE); + } + const std::string expected_banner = central_plaza_banner_to_check(language, ocr_available); + + PlazaReturnConsoleActions actions(info, stream, context, language); + const PlazaReturnResult result = return_to_plaza_checked(stream.logger(), actions, expected_banner, 3); + if (result == PlazaReturnResult::FAILED){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unable to return to Central Plaza: no landing showed its name.", + stream + ); + } +} + void map_move_cursor_fly( const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, @@ -133,7 +208,8 @@ void map_move_cursor_fly( stream.log("Failed to fly! Closing map and retrying."); press_Bs_to_back_to_overworld(info, stream, context); if (i == 2){ - OperationFailedExceptionWithScreenshot::fire( + throw_and_log( + stream.logger(), ErrorReportMode::SEND_ERROR_REPORT, "Unable to fly to " + location + "!", stream diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h index 8c21530cea..261a662cc4 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h @@ -7,6 +7,8 @@ #ifndef PokemonAutomation_PokemonSV_Terarium_H #define PokemonAutomation_PokemonSV_Terarium_H +#include "CommonFramework/Language.h" +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" @@ -15,8 +17,18 @@ namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ -// Return to Central Plaza from anywhere in the terarium -void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context); +// Return to Central Plaza from anywhere in the terarium. When the language +// has OCR data and a known Central Plaza name, check the place name shown on +// landing and return again if it is wrong. Throws if no return lands there. +void return_to_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, Language language); + +// Thrown by map_move_cursor_fly() when it can't fly to its destination. +// BBQ quests catch it to fail the current attempt instead of the program. +class TerariumFlyFailedException : public OperationFailedExceptionWithScreenshot{ +public: + using OperationFailedExceptionWithScreenshot::OperationFailedExceptionWithScreenshot; + virtual const char* name() const override{ return "TerariumFlyFailedException"; } +}; // Open map from overworld, move cursor to fly location, and fly to location void map_move_cursor_fly( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp new file mode 100644 index 0000000000..58befc2f7e --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp @@ -0,0 +1,127 @@ +/* Terarium Plaza Return + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Color.h" +#include "Common/Cpp/Strings/Unicode.h" +#include "CommonTools/OCR/OCR_TextMatcher.h" +#include "PokemonSV_TerariumPlazaReturn.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// Both names were seen in the lab: 中心廣場 on the landing banner, and +// "Central Plaza" is the English name of the same fly point. Other +// languages aren't checked until their names are confirmed, since a wrong +// name would fail every return. +std::string central_plaza_banner_to_check(Language language, bool ocr_available){ + if (!ocr_available){ + return ""; + } + switch (language){ + case Language::English: + return "Central Plaza"; + case Language::ChineseTraditional: + return "中心廣場"; + default: + return ""; + } +} + + +namespace{ + +// Keep letters (lowercased), digits and CJK characters. Drop whitespace and +// punctuation, including CJK and fullwidth punctuation. +std::u32string normalize_place_name(const std::string& text){ + std::u32string ret; + for (char32_t ch : utf8_to_utf32(text)){ + if ('A' <= ch && ch <= 'Z'){ + ret += ch - 'A' + 'a'; + }else if (('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9')){ + ret += ch; + }else if (ch < 0x80){ + continue; + }else if (0x3000 <= ch && ch <= 0x303f){ + continue; + }else if (0xff00 <= ch && ch <= 0xff0f){ + continue; + }else{ + ret += ch; + } + } + return ret; +} + +} + +bool place_name_banner_matches(const std::string& expected, const std::string& ocr_text){ + const std::u32string target = normalize_place_name(expected); + const std::u32string read = normalize_place_name(ocr_text); + if (target.empty() || read.empty()){ + return false; + } + const size_t max_errors = std::max(1, target.size() / 5); + return OCR::levenshtein_distance_substring(target, read) <= max_errors; +} + + +std::string plaza_return_result_to_string(PlazaReturnResult result){ + switch (result){ + case PlazaReturnResult::ARRIVED: return "arrived"; + case PlazaReturnResult::NOT_CHECKED: return "not checked"; + case PlazaReturnResult::FAILED: return "failed"; + } + return "?"; +} + +PlazaReturnResult return_to_plaza_checked( + Logger& logger, PlazaReturnActions& actions, + const std::string& expected_banner, + size_t max_attempts +){ + for (size_t attempt = 1; attempt <= max_attempts; attempt++){ + const std::string prefix = "Return to Central Plaza, attempt " + + std::to_string(attempt) + "/" + std::to_string(max_attempts) + ": "; + + // 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 (expected_banner.empty()){ + logger.log(prefix + "landed. Not checking where: no place name to check in this language.", COLOR_ORANGE); + return PlazaReturnResult::NOT_CHECKED; + } + + auto matches = [&](const std::string& text){ + return place_name_banner_matches(expected_banner, text); + }; + const std::string banner = actions.read_landing_banner(matches); + if (matches(banner)){ + logger.log(prefix + "landed at Central Plaza (banner \"" + banner + "\")."); + return PlazaReturnResult::ARRIVED; + } + logger.log( + prefix + "landed, but the banner read \"" + banner + "\", not \"" + expected_banner + "\".", + COLOR_RED + ); + } + + logger.log( + "Return to Central Plaza: no landing showed Central Plaza after " + + std::to_string(max_attempts) + " attempts.", + COLOR_RED + ); + return PlazaReturnResult::FAILED; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h new file mode 100644 index 0000000000..acdb91567a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h @@ -0,0 +1,74 @@ +/* Terarium Plaza Return + * + * From: https://github.com/PokemonAutomation/ + * + * Return to Central Plaza and check, from the place name shown on landing, + * that the player really is there. + * + */ + +#ifndef PokemonAutomation_PokemonSV_TerariumPlazaReturn_H +#define PokemonAutomation_PokemonSV_TerariumPlazaReturn_H + +#include +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// The place name to look for on landing at Central Plaza, or "" when the +// arrival can't be checked: the name isn't known in this language, or there +// is no OCR data for it. +std::string central_plaza_banner_to_check(Language language, bool ocr_available); + +// Whether the OCR text of a place-name banner shows `expected`. Case, +// spacing and punctuation don't count, other text around the name is +// ignored, and a few OCR errors are allowed (one per 5 characters, at least +// one), which still tells the plazas apart. +bool place_name_banner_matches(const std::string& expected, const std::string& ocr_text); + + +// The game steps the return-and-check loop drives. +class PlazaReturnActions{ +public: + virtual ~PlazaReturnActions() = default; + + // 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; + + // Watch the place-name banner shown after landing. Returns the first + // text read that `matches` accepts, else the last non-empty text read + // ("" when nothing was read). + virtual std::string read_landing_banner(const std::function& matches) = 0; +}; + +enum class PlazaReturnResult{ + ARRIVED, // The banner showed Central Plaza. + NOT_CHECKED, // Landed, but the banner can't be checked in this language. + FAILED, // Every attempt landed somewhere the banner didn't confirm. +}; +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 +// central_plaza_banner_to_check(); when it is empty the first landing is +// accepted unchecked. +PlazaReturnResult return_to_plaza_checked( + Logger& logger, PlazaReturnActions& actions, + const std::string& expected_banner, + size_t max_attempts +); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index dad3af3e6b..b7c0c37f61 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2410,6 +2410,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldSensors.cpp Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldSensors.h + Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.cpp + Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_PlaceNameBannerReader.h Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.cpp Source/VideoGames/PokemonSV/Inference/Overworld/PokemonSV_StationaryOverworldWatcher.h Source/VideoGames/PokemonSV/Inference/Picnics/PokemonSV_PicnicDetector.cpp @@ -2715,6 +2717,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/PokemonSV_SaveGame.h Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h + Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.cpp + Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumPlazaReturn.h Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h Source/VideoGames/PokemonSV/Programs/Sandwiches/PokemonSV_IngredientSession.cpp