diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp index 9a615e8b4c..abf5aae895 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.cpp @@ -4,6 +4,7 @@ * */ +#include #include "Common/Cpp/Json/JsonValue.h" #include "Common/Cpp/Json/JsonObject.h" #include "CommonFramework/GlobalAutoPaths.h" @@ -15,7 +16,10 @@ namespace PokemonAutomation{ -SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) +SpriteDatabase::SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally +) : m_backing_image(RESOURCE_PATH() + sprite_path) { std::string path = RESOURCE_PATH() + json_path; @@ -37,6 +41,16 @@ SpriteDatabase::SpriteDatabase(const char* sprite_path, const char* json_path) JsonObject& obj = item.second.to_object_throw(path); int y = (int)obj.get_integer_throw("top", path); int x = (int)obj.get_integer_throw("left", path); + if (mirror_horizontally && mirror_horizontally(slug)){ + if (x < 0 || y < 0 || (size_t)(x + width) > m_backing_image.width() || (size_t)(y + height) > m_backing_image.height()){ + throw FileException(nullptr, PA_CURRENT_FUNCTION, "Sprite out of bounds: " + slug, path); + } + for (int r = y; r < y + (int)height; r++){ + for (int c0 = x, c1 = x + (int)width - 1; c0 < c1; c0++, c1--){ + std::swap(m_backing_image.pixel(c0, r), m_backing_image.pixel(c1, r)); + } + } + } ImageViewRGB32 sprite = extract_box_reference(m_backing_image, ImagePixelBox(x, y, x + width, y + height)); m_database.emplace( diff --git a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h index 3136ffd6ec..3213479227 100644 --- a/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h +++ b/SerialPrograms/Source/CommonTools/Resources/SpriteDatabase.h @@ -7,6 +7,7 @@ #ifndef PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H #define PokemonAutomation_CommonTools_Resources_SpriteCompositeImage_H +#include #include #include "CommonFramework/ImageTypes/ImageRGB32.h" @@ -32,7 +33,14 @@ class SpriteDatabase{ // (next pokemon) ... // } // } - SpriteDatabase(const char* sprite_path, const char* json_path); + // + // If "mirror_horizontally" is set, each sprite for which it returns true + // is flipped left-to-right in place. This avoids storing a separate + // flipped copy of the image. + SpriteDatabase( + const char* sprite_path, const char* json_path, + const std::function& mirror_horizontally = nullptr + ); public: struct Sprite{ diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp index aef20552e7..4f888bd9d1 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.cpp @@ -270,6 +270,18 @@ std::string base_species_slug(const std::string& species){ return species; } +bool species_matches(const std::string& observed, const std::string& candidate){ + const std::string base = base_species_slug(observed); + if (base != base_species_slug(candidate)){ + return false; + } + // Accept matches with the base slug in case form couldn't be read + if (observed == base || candidate == base){ + return true; + } + return observed == candidate; +} + int slot_to_unownform(AdvEncounterSlot slot){ if (slot.species.find("unown") == std::string::npos){ return -1; @@ -521,7 +533,7 @@ bool check_for_match(AdvPokemonResult res, AdvRngFilters target, int16_t gender_ } bool check_for_match(AdvWildPokemonResult res, AdvRngFilters target, int16_t gender_threshold, uint16_t tid_xor_sid){ - return (base_species_slug(target.species) == base_species_slug(res.species)) + return species_matches(target.species, res.species) && (target.level == res.level) && (target.nature == AdvNature::Any || (res.nature == target.nature)) && (target.ability == AdvAbility::Any || (res.ability == target.ability)) diff --git a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h index 85a3ad8707..616063c46d 100644 --- a/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h +++ b/SerialPrograms/Source/Pokemon/Pokemon_AdvRng.h @@ -202,11 +202,14 @@ Pokemon::NatureAdjustments nature_to_adjustment(AdvNature nature); Pokemon::AdvNature string_to_nature(const std::string& nature_string); std::string nature_to_string(const AdvNature& nature); -// Forms that cannot be told apart from an observation (e.g. Unown letters) share a base slug. -// Encounter slots carry the form-specific slug, while an observed species only ever carries -// the base slug, so both sides must be folded before they are compared. +// Forms of the same species (e.g. Unown letters) share a base slug. +// Encounter slots carry the form-specific slug (e.g. "unown-b"). std::string base_species_slug(const std::string& species); +// Return true if an observed species is consistent with a candidate species. +// Species with forms match by base slug, unless both sides name a specific form +bool species_matches(const std::string& observed, const std::string& candidate); + std::string gender_to_string(const AdvGender& gender); AdvGender gender_from_gender_value(uint8_t gender_value, int16_t threshold); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp index 6e2c516038..f4e05933ce 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/PokemonFRLG_PokemonSpriteReader.cpp @@ -4,10 +4,14 @@ * */ +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" -#include "CommonFramework/ImageTools/ImageStats.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/GlobalSettingsPanel.h" -#include "CommonTools/ImageMatch/ImageCropper.h" +#include "Tests/ImageUnitTest.h" +#include "PokemonFRLG_ShinySymbolDetector.h" #include "VideoGames/PokemonFRLG/Resources/PokemonFRLG_PokemonSprites.h" #include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" #include "PokemonFRLG_PokemonSpriteReader.h" @@ -17,78 +21,286 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ -PokemonSpriteMatcherExact::PokemonSpriteMatcherExact(const std::set* subset, bool flipped) - : ExactImageDictionaryMatcher({1, 256}) +namespace{ + +// Positions are in GBA pixels. The GBA screen is 240 x 160. +constexpr int GBA_WIDTH = 240; +constexpr int GBA_HEIGHT = 160; +constexpr int SPRITE_SIZE = 64; + +ImageFloatBox sprite_box(int x, int y){ + return ImageFloatBox( + (double)x / GBA_WIDTH, (double)y / GBA_HEIGHT, + (double)SPRITE_SIZE / GBA_WIDTH, (double)SPRITE_SIZE / GBA_HEIGHT + ); +} + +// When the summary is opened, the sprite bounces up by as much as 9 pixels. +// the following allow 1 extra pixel of slack in each direction. +constexpr int SUMMARY_X_MIN = 27; +constexpr int SUMMARY_X_MAX = 31; +constexpr int SUMMARY_Y_MIN = 23; +constexpr int SUMMARY_Y_MAX = 34; + +// In battle, the sprite's top edge depends on the species and whether it floats. +constexpr int BATTLE_X = 144; +constexpr int BATTLE_Y_MIN = 0; +constexpr int BATTLE_Y_MAX = 36; + +} + + + +PositionedSpriteMatcher::PositionedSpriteMatcher( + const std::set* subset, + bool flipped, // most sprites are flipped on the summary screen + const std::vector& shiny_variants, // {false, true} to match against both normal and shiny sprites + int x_min, int x_max, int y_min, int y_max +) + : m_x_min(x_min), m_x_max(x_max) + , m_y_min(y_min), m_y_max(y_max) { - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite.copy()); + for (bool shiny : shiny_variants){ + for (const auto& item : POKEMON_SPRITES(shiny, flipped)){ + if (subset != nullptr && !sprite_in_species_subset(item.first, *subset)){ + continue; + } + m_templates.emplace_back(new Template{ + sprite_slug_to_species(item.first), + shiny, + ImageMatch::WeightedExactImageMatcher(item.second.sprite.copy(), {1, 256}) + }); } } } +ImageFloatBox PositionedSpriteMatcher::search_box() const{ + return ImageFloatBox( + (double)m_x_min / GBA_WIDTH, (double)m_y_min / GBA_HEIGHT, + (double)(m_x_max - m_x_min + SPRITE_SIZE) / GBA_WIDTH, + (double)(m_y_max - m_y_min + SPRITE_SIZE) / GBA_HEIGHT + ); +} -PokemonSpriteMatcherCropped::PokemonSpriteMatcherCropped(const std::set* subset, double min_euclidean_distance, bool flipped) - : CroppedImageDictionaryMatcher({1, 256}) - , m_min_euclidean_distance_squared(min_euclidean_distance * min_euclidean_distance) -{ - for (const auto& item : (flipped ? FLIPPED_POKEMON_SPRITES() : ALL_POKEMON_SPRITES())){ - if (subset == nullptr || subset->find(item.first) != subset->end()){ - add(item.first, item.second.sprite); +PositionedSpriteMatcher::Result PositionedSpriteMatcher::match( + const ImageViewRGB32& game_screen, double alpha_spread +) const{ + Result ret; + if (!game_screen || m_templates.empty()){ + return ret; + } + + // Every possible sprite position scaled to the template size + std::vector candidates; + for (int y = m_y_min; y <= m_y_max; y++){ + for (int x = m_x_min; x <= m_x_max; x++){ + candidates.emplace_back( + extract_box_reference(game_screen, sprite_box(x, y)).scale_to(SPRITE_SIZE, SPRITE_SIZE) + ); } } -} -auto PokemonSpriteMatcherCropped::get_crop_candidates(const ImageViewRGB32& image) const -> std::vector{ - ImageStats border = image_border_stats(image); - ImagePixelBox box = ImageMatch::enclosing_rectangle_with_pixel_filter( - image, - [&](Color pixel){ - double r = (double)pixel.red() - border.average.r; - double g = (double)pixel.green() - border.average.g; - double b = (double)pixel.blue() - border.average.b; - bool stop = r*r + g*g + b*b >= m_min_euclidean_distance_squared; - return stop; + std::map species_best; + double best_alpha = 10000; + for (const std::unique_ptr