diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 02e94b05c7..ca43d5a46c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -25,139 +25,14 @@ #include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h" #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" -#include "Options/PokemonSV_BBQuests.h" -#include "Programs/Farming/PokemonSV_BlueberryQuests.h" +#include "Programs/Farming/PokemonSV_BBQTests.h" #include "PokemonSV_Tests.h" -#include "CommonFramework/GlobalAutoPaths.h" -#include "CommonFramework/ImageTypes/ImageRGB32.h" -#include "Inference/PokemonSV_BlueberryPointsReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ namespace PokemonSV{ -namespace{ - -struct TeraEncounterScenario{ - std::string name; - bool shiny = false; - int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first - bool battle_menu_detected = false; // The route's own wait saw the battle menu. - TeraEncounterAction expected; -}; - -class Test_TeraEncounterAction : public UnitTest{ -public: - Test_TeraEncounterAction(TeraEncounterScenario scenario) - : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const TeraEncounterAction actual = tera_encounter_action( - m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected - ); - if (actual != m_scenario.expected){ - return "Expected action " + std::to_string((int)m_scenario.expected) - + ", got " + std::to_string((int)actual) + "."; - } - return true; - } - -private: - const TeraEncounterScenario m_scenario; -}; - -} - -void add_tests_TeraEncounterAction(UnitTestDatabase& database){ - using enum TeraEncounterAction; - const std::vector scenarios{ - {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, - {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, - {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, - {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, - {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, - {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, - {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, - {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, - {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, - }; - for (const TeraEncounterScenario& scenario : scenarios){ - database.add(scenario); - } -} - -namespace{ - -class Test_BBQuestsSlugs : public UnitTest{ -public: - Test_BBQuestsSlugs() - : UnitTest("PokemonSV::BBQuests - slug round trip") - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - for (int c = 0; c < (int)BBQuests::UnableToDetect; c++){ - const BBQuests quest = (BBQuests)c; - const std::string& slug = BBQuests_enum_to_string(quest); - if (slug.empty()){ - return "Quest " + std::to_string(c) + " has no slug."; - } - if (BBQuests_string_to_enum(slug) != quest){ - return "Slug " + slug + " does not map back to quest " + std::to_string(c) + "."; - } - } - if (!BBQuests_enum_to_string(BBQuests::UnableToDetect).empty()){ - return "UnableToDetect must map to the empty slug."; - } - if (BBQuests_string_to_enum("not-a-quest") != BBQuests::UnableToDetect){ - return "An unrecognized slug must map to UnableToDetect."; - } - return true; - } -}; - -} - -namespace{ - -class Test_BlueberryPoints : public UnitTest{ -public: - Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) - : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) - , m_image(UNIT_TEST_RESOURCE_PATH() + image) - , m_expected(expected), m_width(width), m_height(height) - {} - - virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ - ImageRGB32 reference(m_image); - ImageRGB32 image = reference.scale_to(m_width, m_height); - const int actual = read_BP_from_image(logger, image); - if (actual != m_expected){ - return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); - } - return true; - } - -private: - const std::string m_image; - const int m_expected; - const size_t m_width, m_height; -}; - -} - void add_tests(UnitTestDatabase& database){ - database.add(); - - // Full-frame references, in the CommandLineTests repo: they exercise the production - // crop, filter and OCR together. - database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); - database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); - - database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); - database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); - add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); @@ -178,7 +53,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_TeraTypeReader(database); add_tests_NormalBattleMenus(database); add_tests_LetsGoKillDetector(database); - add_tests_TeraEncounterAction(database); + add_tests_BBQ(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp new file mode 100644 index 0000000000..fc8b078686 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp @@ -0,0 +1,210 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryPointsReader.h" +#include "VideoGames/PokemonSV/Inference/PokemonSV_BlueberryQuestDetector.h" +#include "VideoGames/PokemonSV/Options/PokemonSV_BBQuests.h" +#include "PokemonSV_BlueberryQuests.h" +#include "PokemonSV_BBQTests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +namespace{ + +class Test_BBQuestsSlugs : public UnitTest{ +public: + Test_BBQuestsSlugs() + : UnitTest("PokemonSV::BBQuests - slug round trip") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + for (int c = 0; c < (int)BBQuests::UnableToDetect; c++){ + const BBQuests quest = (BBQuests)c; + const std::string& slug = BBQuests_enum_to_string(quest); + if (slug.empty()){ + return "Quest " + std::to_string(c) + " has no slug."; + } + if (BBQuests_string_to_enum(slug) != quest){ + return "Slug " + slug + " does not map back to quest " + std::to_string(c) + "."; + } + } + if (!BBQuests_enum_to_string(BBQuests::UnableToDetect).empty()){ + return "UnableToDetect must map to the empty slug."; + } + if (BBQuests_string_to_enum("not-a-quest") != BBQuests::UnableToDetect){ + return "An unrecognized slug must map to UnableToDetect."; + } + return true; + } +}; + +class Test_BlueberryPoints : public UnitTest{ +public: + Test_BlueberryPoints(const std::string& image, int expected, size_t width, size_t height) + : UnitTest("PokemonSV::BlueberryPoints - " + image + " - " + std::to_string(width)) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_expected(expected), m_width(width), m_height(height) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(m_image); + ImageRGB32 image = reference.scale_to(m_width, m_height); + const int actual = read_BP_from_image(logger, image); + if (actual != m_expected){ + return "Expected " + std::to_string(m_expected) + " BP, read " + std::to_string(actual); + } + return true; + } + +private: + const std::string m_image; + const int m_expected; + const size_t m_width, m_height; +}; + +struct TeraEncounterScenario{ + std::string name; + bool shiny = false; + int encounter_watcher_result; // 0: battle menu, 1: audio, -1: route finished first + bool battle_menu_detected = false; // The route's own wait saw the battle menu. + TeraEncounterAction expected; +}; + +class Test_TeraEncounterAction : public UnitTest{ +public: + Test_TeraEncounterAction(TeraEncounterScenario scenario) + : UnitTest("PokemonSV::TeraEncounterAction - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TeraEncounterAction actual = tera_encounter_action( + m_scenario.shiny, m_scenario.encounter_watcher_result, m_scenario.battle_menu_detected + ); + if (actual != m_scenario.expected){ + return "Expected action " + std::to_string((int)m_scenario.expected) + + ", got " + std::to_string((int)actual) + "."; + } + return true; + } + +private: + const TeraEncounterScenario m_scenario; +}; + +// One quest slot on a full frame of the quest panel. +struct BlueberryQuestScenario{ + std::string image; + Language language; + BlueberryQuestDetector::QuestPosition position; + std::string expected_slug; + size_t width; + size_t height; +}; + +class Test_BlueberryQuestDetector : public UnitTest{ +public: + Test_BlueberryQuestDetector(BlueberryQuestScenario scenario) + : UnitTest( + "PokemonSV::BlueberryQuestDetector - " + scenario.image + + " - slot " + std::to_string((int)scenario.position + 1) + + " - " + std::to_string(scenario.width) + ) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + ImageRGB32 reference(UNIT_TEST_RESOURCE_PATH() + m_scenario.image); + ImageRGB32 image = reference.scale_to(m_scenario.width, m_scenario.height); + BlueberryQuestDetector detector(logger, COLOR_RED, m_scenario.language, m_scenario.position); + const std::string actual = detector.detect_quest(image); + if (actual != m_scenario.expected_slug){ + return "Expected quest " + m_scenario.expected_slug + ", read \"" + actual + "\""; + } + return true; + } + +private: + const BlueberryQuestScenario m_scenario; +}; + +} + +static void add_tests_TeraEncounterAction(UnitTestDatabase& database){ + using enum TeraEncounterAction; + const std::vector scenarios{ + {.name = "timeout recovers without battle", .encounter_watcher_result = -1, .expected = Recover}, + {.name = "audio alone is not entry", .encounter_watcher_result = 1, .expected = Recover}, + {.name = "unknown callback is not entry", .encounter_watcher_result = 2, .expected = Recover}, + {.name = "encounter watcher menu battles", .encounter_watcher_result = 0, .expected = Battle}, + {.name = "route wait menu battles", .encounter_watcher_result = -1, .battle_menu_detected = true, .expected = Battle}, + {.name = "audio then route wait menu battles", .encounter_watcher_result = 1, .battle_menu_detected = true, .expected = Battle}, + {.name = "both menus battle", .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = Battle}, + {.name = "shiny stops without entry", .shiny = true, .encounter_watcher_result = -1, .expected = StopForShiny}, + {.name = "shiny stops before battle", .shiny = true, .encounter_watcher_result = 0, .battle_menu_detected = true, .expected = StopForShiny}, + }; + for (const TeraEncounterScenario& scenario : scenarios){ + database.add(scenario); + } +} + + +static void add_tests_BlueberryQuestDetector(UnitTestDatabase& database){ + using enum BlueberryQuestDetector::QuestPosition; + // The second frame is the first after rerolling the first quest. Singleplayer + // without a red quest shows 3 quests, so the fourth slot reads as nothing. + const struct{ + std::string image; + std::string slugs[4]; + } panels[] = { + {"PokemonSV/BlueberryPoints/6750.png", {"catch-any", "sneak-up", "auto-10", ""}}, + {"PokemonSV/BlueberryPoints/6740.png", {"photo-swim", "sneak-up", "auto-10", ""}}, + }; + const BlueberryQuestDetector::QuestPosition positions[] = {FIRST, SECOND, THIRD, FOURTH}; + const std::pair sizes[] = {{1920, 1080}, {1280, 720}}; + for (const auto& panel : panels){ + for (size_t slot = 0; slot < 4; slot++){ + for (const auto& [width, height] : sizes){ + database.add(BlueberryQuestScenario{ + .image = panel.image, + .language = Language::ChineseTraditional, + .position = positions[slot], + .expected_slug = panel.slugs[slot], + .width = width, + .height = height, + }); + } + } + } +} + + +void add_tests_BBQ(UnitTestDatabase& database){ + database.add(); + + // Full-frame references, in the CommandLineTests repo: they exercise the production + // crop, filter and OCR together. + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6750.png", 6750, 1280, 720); + + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1920, 1080); + database.add("PokemonSV/BlueberryPoints/6740.png", 6740, 1280, 720); + + add_tests_TeraEncounterAction(database); + add_tests_BlueberryQuestDetector(database); +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h new file mode 100644 index 0000000000..64317f45aa --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h @@ -0,0 +1,26 @@ +/* Pokemon SV BBQ Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonSV_BBQTests_H +#define PokemonAutomation_PokemonSV_BBQTests_H + +#include "Common/Cpp/TestRunners/UnitTest.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + + +// Blueberry Quest farmer tests: controller decisions run against scripted inputs. +void add_tests_BBQ(UnitTestDatabase& database); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 80752da16d..9ca5d6ba98 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2599,6 +2599,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_AuctionFarmer.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQSoloFarmer.h + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.cpp + Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BBQTests.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.cpp Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryCatchPhoto.h Source/VideoGames/PokemonSV/Programs/Farming/PokemonSV_BlueberryQuests.cpp