diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp new file mode 100644 index 0000000000..ae9d4c111c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp @@ -0,0 +1,138 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/Notifications/ProgramInfo.h" +#include "CommonFramework/Recording/StreamHistorySession.h" +#include "CommonFramework/Tools/ErrorDumper.h" +#include "CommonFramework/Tools/VideoStream.h" +#include "CommonTools/ImageMatch/CroppedImageDictionaryMatcher.h" +#include "CommonTools/ImageMatch/ImageCropper.h" +#include "CommonTools/Resources/SpriteDatabase.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_BallReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +constexpr double MAX_ALPHA = 0.40; +constexpr double ALPHA_SPREAD = 0.02; +constexpr double MIN_BACKGROUND_DISTANCE = 100; +const ImageFloatBox BALL_BOX(0.410, 0.507, 0.063, 0.084); + +class PokeballSpriteMatcher : public ImageMatch::CroppedImageDictionaryMatcher{ +public: + PokeballSpriteMatcher() + : CroppedImageDictionaryMatcher({1, 128}) + { + const SpriteDatabase sprites("PokemonFRLG/PokeballSprites.png", "PokemonFRLG/PokeballSprites.json"); + for (const auto& item : sprites){ + add(item.first, item.second.sprite); + } + } + +private: + std::vector get_crop_candidates(const ImageViewRGB32& image) const override{ + const FloatPixel background = image_border_stats(image).average; + const ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( + image, + [&](Color pixel){ + double r = (double)pixel.red() - background.r; + double g = (double)pixel.green() - background.g; + double b = (double)pixel.blue() - background.b; + return r*r + g*g + b*b >= MIN_BACKGROUND_DISTANCE * MIN_BACKGROUND_DISTANCE; + } + ); + return {extract_box_reference(image, box)}; + } +}; + +} + + +BallReader::BallReader(VideoStream& stream) + : m_stream(stream) + , m_box_sprite(stream.overlay(), GameSettings::instance().GAME_BOX.inner_to_outer(BALL_BOX)) +{} + +std::string BallReader::read_ball(const ImageViewRGB32& screen) const{ + if (!screen){ + return ""; + } + + static const PokeballSpriteMatcher matcher; + const ImageViewRGB32 image = extract_box_reference(screen, m_box_sprite); + ImageMatch::ImageMatchResult result = matcher.match(image, ALPHA_SPREAD); + result.log(m_stream.logger(), MAX_ALPHA); + result.clear_beyond_alpha(MAX_ALPHA); + + if (result.results.size() != 1){ + dump_image(m_stream.logger(), ProgramInfo(), "FRLG-BallReader", screen); + } + if (result.results.empty()){ + return ""; + } + return result.results.begin()->second; +} + + +class Test_BallReader : public ImageUnitTest{ +public: + Test_BallReader(const std::string& image, std::string expected) + : ImageUnitTest("PokemonFRLG::BallReader - " + image, {"PokemonFRLG/StatsReader/Page1/" + image}) + , m_expected(std::move(expected)) + {} + +private: + UnitTestResult run_on_images( + Logger& logger, CancellableScope&, + const std::vector& images + ) const override{ + DummyAudioFeed audio_feed; + DummyVideoFeed video_feed; + StreamHistorySession history(logger); + DummyVideoOverlay overlay; + VideoStream stream(logger, audio_feed, video_feed, history, overlay); + BallReader reader(stream); + std::string result = reader.read_ball(images[0]); + if (result == m_expected){ + return true; + } + return "Expected: " + m_expected + ", received: " + result; + } + + std::string m_expected; +}; + +void add_tests_BallReader(UnitTestDatabase& database){ + database.add("abra_1_deu.png", "master-ball"); + database.add("bulbasaur_1_eng.png", "poke-ball"); + database.add("bulbasaur_1_fra.png", "poke-ball"); + database.add("bulbasaur_1_jpn.png", "poke-ball"); + database.add("bulbasaur_2_eng.png", "poke-ball"); + database.add("bulbasaur_3_eng.png", "poke-ball"); + database.add("bulbasaur_4_eng.png", "poke-ball"); + database.add("deoxys_1_jpn.png", "master-ball"); + database.add("horsea_1_deu.png", "master-ball"); + database.add("horsea_2_deu.png", "master-ball"); + database.add("horsea_3_deu.png", "master-ball"); + database.add("magikarp_1_spa.png", "poke-ball"); + database.add("moltres_1_eng.png", "master-ball"); + database.add("nidoran_m_eng.png", "ultra-ball"); + database.add("pidgey_1_ita.png", "poke-ball"); + database.add("venonat_4k_eng.png", "master-ball"); +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h new file mode 100644 index 0000000000..f7e38d75c4 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h @@ -0,0 +1,40 @@ +/* Ball Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_BallReader_H +#define PokemonAutomation_PokemonFRLG_BallReader_H + +#include +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" + +namespace PokemonAutomation{ + +class UnitTestDatabase; +class VideoStream; + +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +class BallReader{ +public: + BallReader(VideoStream& stream); + + // Read the ball slug from the first summary page. Return an empty string if no match is found. + std::string read_ball(const ImageViewRGB32& screen) const; + +private: + VideoStream& m_stream; + OverlayBoxScope m_box_sprite; +}; + +void add_tests_BallReader(UnitTestDatabase& database); + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index 94dc41dd85..31746134b7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -20,6 +20,7 @@ #include "Inference/Menus/PokemonFRLG_PartyEmptySlotDetector.h" #include "Inference/Menus/PokemonFRLG_PartyHeldItemDetector.h" #include "Inference/Menus/PokemonFRLG_PartyMenuDetector.h" +#include "Inference/PokemonFRLG_BallReader.h" #include "Inference/PokemonFRLG_BattleLevelUpReader.h" #include "Inference/PokemonFRLG_BattleSelectionArrowDetector.h" #include "Inference/PokemonFRLG_PartyLevelUpReader.h" @@ -40,6 +41,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AdvanceWhiteDialogDetector(database); add_tests_AdvanceBattleDialogDetector(database); add_tests_BagDetector(database); + add_tests_BallReader(database); add_tests_BattleLevelUpReader(database); add_tests_BattleMenuDetector(database); add_tests_BattleMoveSelectionArrow(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp index bfdbfab3f4..99847df668 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.cpp @@ -23,6 +23,7 @@ #include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxGenderReader.h" #include "VideoGames/PokemonFRLG/Inference/Box/PokemonFRLG_BoxSelectionModeDetector.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h" +#include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_SelectionArrowDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_ShinySymbolDetector.h" #include "VideoGames/PokemonFRLG/Inference/PokemonFRLG_StatsReader.h" @@ -162,8 +163,7 @@ void open_summary_screen(SingleSwitchProgramEnvironment& env, ProControllerConte wait_for_summary_animation(env, context); } -// Scroll through the detected pokemon and read their summary screens. -// Currently only reads shininess, but could be extended to read other summary information. +// Scroll through the detected Pokemon and read their shininess and ball type. void scan_box_summaries( SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language, const BoxSelectionModeDetector& selection_detector, BoxCursor& cursor, @@ -180,7 +180,7 @@ void scan_box_summaries( return; } - env.log("Reading shininess in box " + std::to_string(cursor.box + 1)); + env.log("Reading summaries in box " + std::to_string(cursor.box + 1)); move_cursor_to(env, context, selection_detector, cursor, BoxCursor(occupied.back()), BoxSelectionMode::Normal); open_summary_screen(env, context); @@ -189,6 +189,8 @@ void scan_box_summaries( ShinySymbolDetector shiny_detector(COLOR_YELLOW); VideoOverlaySet overlays(env.console.overlay()); shiny_detector.make_overlays(overlays); + BallReader ball_reader(env.console); + for (size_t remaining = occupied.size(); remaining > 0; remaining--){ const size_t index = occupied[remaining - 1]; auto& pokemon = *boxes_data[index]; @@ -203,6 +205,21 @@ void scan_box_summaries( OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, ss.str(), env.console); } pokemon.shiny = shiny_detector.read(env.logger(), screen); + pokemon.ball_slug = ball_reader.read_ball(screen); + if (pokemon.ball_slug.empty()){ + std::ostringstream ss; + ss << "Box Sorter: Unable to read the ball type at " << BoxCursor(index) << "."; + env.log(ss.str(), COLOR_RED); + } + + std::ostringstream overview; + overview << "index:" << index + << ", name:" << pokemon.name_slug + << ", lvl:" << level_str(pokemon.level) + << ", shiny:" << (pokemon.shiny ? "True" : "False") + << ", ball:" << pokemon.ball_slug; + env.log(overview.str()); + pbf_press_dpad(context, DPAD_UP, 80ms, 300ms); context.wait_for_all_requests(); wait_for_summary_animation(env, context); @@ -421,10 +438,16 @@ std::string make_missing_living_dex_report( BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, - Language language, const std::vector& preferences, size_t num_boxes, bool read_shininess + Language language, const std::vector& preferences, size_t num_boxes, bool force_summary_scan ){ - bool read_summary = read_shininess || std::any_of(preferences.begin(), preferences.end(), [](const SortingRule& rule){ - return rule.sort_type == SortingRuleType::Shiny; + const bool read_summary = force_summary_scan || std::any_of(preferences.begin(), preferences.end(), [](const SortingRule& rule){ + switch (rule.sort_type){ + case SortingRuleType::Shiny: + case SortingRuleType::Ball_Slug: + return true; + default: + return false; + } }); auto& stats = env.current_stats(); const size_t slot_count = num_boxes * BOX_ROWS * BOX_COLS; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h index b2401191e6..6c2e90348c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxOrganization.h @@ -75,11 +75,11 @@ struct BoxOrganizationScan{ BoxSelectionMode mode; }; -// Detecting Shiny requires reading the summary screen, this is much slower than just reading the box data. +// Scan summaries for rules that need summary data or when the caller requests a summary scan. BoxOrganizationScan scan_box_data( SingleSwitchProgramEnvironment& env, ProControllerContext& context, Language language, const std::vector& preferences, size_t num_boxes, - bool read_shininess = false + bool force_summary_scan = false ); void execute_box_sorting_plan( diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp index 6c2f7edbf5..dd725ec99d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter.cpp @@ -65,7 +65,13 @@ BoxSorter::BoxSorter() ) , SORT_TABLE( "Sort Order Rules:
Sort order rules will be applied top to bottom.", - {SortingRuleType::DexNo, SortingRuleType::Level, SortingRuleType::Gender, SortingRuleType::Shiny} + { + SortingRuleType::DexNo, + SortingRuleType::Level, + SortingRuleType::Gender, + SortingRuleType::Shiny, + SortingRuleType::Ball_Slug, + } ) , DRY_RUN( "Dry Run:
Scan boxes and write frlg-box_order.json and " diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp index aa48844633..6035c5c36e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorterLivingDex.cpp @@ -12,7 +12,6 @@ #include "CommonTools/StartupChecks/StartProgramChecks.h" #include "Pokemon/Pokemon_Strings.h" #include "PokemonFRLG_BoxOrganization.h" -#include "PokemonFRLG_BoxSorterLivingDex.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_BoxSorterLivingDex.h" diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp index f529d86d53..e63660ec33 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/BoxOrganization/PokemonFRLG_BoxSorter_Tests.cpp @@ -7,11 +7,14 @@ #include #include #include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" #include "Common/Cpp/TestRunners/UnitTest.h" #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/GlobalAutoPaths.h" #include "Pokemon/Resources/Pokemon_PokemonSlugs.h" #include "PokemonFRLG_BoxOrganization.h" +#include "PokemonFRLG_BoxSorter.h" #include "PokemonFRLG_BoxSorter_Tests.h" namespace PokemonAutomation{ @@ -73,6 +76,29 @@ class Test_BoxSorter_Ordering : public UnitTest{ if (slots != std::vector>{low, high, shiny, std::nullopt}){ return "Reversed shiny sorting must put non-shinies first and empty slots last."; } + auto poke_ball = low; + poke_ball->ball_slug = "poke-ball"; + auto master_ball = poke_ball; + master_ball->ball_slug = "master-ball"; + auto high_poke_ball = high; + high_poke_ball->ball_slug = "poke-ball"; + rules = {{SortingRuleType::Ball_Slug, false}, {SortingRuleType::Level, false}}; + const std::vector> original{ + poke_ball, master_ball, high_poke_ball, std::nullopt + }; + slots = {high_poke_ball, std::nullopt, poke_ball, master_ball}; + std::stable_sort(slots.begin(), slots.end()); + if (slots != std::vector>{master_ball, poke_ball, high_poke_ball, std::nullopt}){ + return "Ball Type sorting must group balls alphabetically and break ties by level."; + } + if (make_box_sorting_plan(original, slots).chains != std::vector>{{0, 1, 0}}){ + return "The swap plan must distinguish Pokemon that differ only by ball type."; + } + rules[0].reverse = true; + std::stable_sort(slots.begin(), slots.end()); + if (slots != std::vector>{poke_ball, high_poke_ball, master_ball, std::nullopt}){ + return "Reversing Ball Type must leave the level tie-breaker and empty slots in order."; + } if (SLUGS_TO_NATIONAL_DEX().at("bulbasaur") != 1 || SLUGS_TO_NATIONAL_DEX().at("deoxys") != 386){ return "Species conversion must use National Dex numbers."; } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 03a76d8719..eb81069fbd 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1731,6 +1731,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SummaryDetector.h Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.cpp Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_TrainerCardDetector.h + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.cpp + Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BallReader.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.cpp Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattleLevelUpReader.h Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_BattlePokemonDetector.cpp