Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<ImageViewRGB32> 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<ImageRGB32>& 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<Test_BallReader>("abra_1_deu.png", "master-ball");
database.add<Test_BallReader>("bulbasaur_1_eng.png", "poke-ball");
database.add<Test_BallReader>("bulbasaur_1_fra.png", "poke-ball");
database.add<Test_BallReader>("bulbasaur_1_jpn.png", "poke-ball");
database.add<Test_BallReader>("bulbasaur_2_eng.png", "poke-ball");
database.add<Test_BallReader>("bulbasaur_3_eng.png", "poke-ball");
database.add<Test_BallReader>("bulbasaur_4_eng.png", "poke-ball");
database.add<Test_BallReader>("deoxys_1_jpn.png", "master-ball");
database.add<Test_BallReader>("horsea_1_deu.png", "master-ball");
database.add<Test_BallReader>("horsea_2_deu.png", "master-ball");
database.add<Test_BallReader>("horsea_3_deu.png", "master-ball");
database.add<Test_BallReader>("magikarp_1_spa.png", "poke-ball");
database.add<Test_BallReader>("moltres_1_eng.png", "master-ball");
database.add<Test_BallReader>("nidoran_m_eng.png", "ultra-ball");
database.add<Test_BallReader>("pidgey_1_ita.png", "poke-ball");
database.add<Test_BallReader>("venonat_4k_eng.png", "master-ball");
}


}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* Ball Reader
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_PokemonFRLG_BallReader_H
#define PokemonAutomation_PokemonFRLG_BallReader_H

#include <string>
#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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand All @@ -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];
Expand All @@ -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);
Expand Down Expand Up @@ -421,10 +438,16 @@ std::string make_missing_living_dex_report(

BoxOrganizationScan scan_box_data(
SingleSwitchProgramEnvironment& env, ProControllerContext& context,
Language language, const std::vector<SortingRule>& preferences, size_t num_boxes, bool read_shininess
Language language, const std::vector<SortingRule>& 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<BoxOrganizationStats>();
const size_t slot_count = num_boxes * BOX_ROWS * BOX_COLS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pokemon::SortingRule>& preferences, size_t num_boxes,
bool read_shininess = false
bool force_summary_scan = false
);

void execute_box_sorting_plan(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ BoxSorter::BoxSorter()
)
, SORT_TABLE(
"<b>Sort Order Rules:</b><br>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(
"<b>Dry Run:</b><br>Scan boxes and write frlg-box_order.json and "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#include <algorithm>
#include <map>
#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{
Expand Down Expand Up @@ -73,6 +76,29 @@ class Test_BoxSorter_Ordering : public UnitTest{
if (slots != std::vector<std::optional<CollectedPokemonInfo>>{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<std::optional<CollectedPokemonInfo>> 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<std::optional<CollectedPokemonInfo>>{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<std::vector<size_t>>{{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<std::optional<CollectedPokemonInfo>>{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.";
}
Expand Down
2 changes: 2 additions & 0 deletions SerialPrograms/cmake/SourceFiles.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading