diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.cpp new file mode 100644 index 0000000000..c2f548577c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.cpp @@ -0,0 +1,29 @@ +/* Tutorial Map Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "PokemonLZA_TutorialMapDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +TutorialMapDetector::TutorialMapDetector(Color color, VideoOverlay* overlay) + : m_mission_list(color, ButtonType::ButtonX, {0.005, 0.15, 0.025, 0.11}, overlay) + , m_close(color, ButtonType::ButtonB, {0.9, 0.94, 0.04, 0.04}, overlay) +{} + +void TutorialMapDetector::make_overlays(VideoOverlaySet& items) const{ + m_mission_list.make_overlays(items); + m_close.make_overlays(items); +} + +bool TutorialMapDetector::detect(const ImageViewRGB32& screen){ + return m_mission_list.detect(screen) && m_close.detect(screen); +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h new file mode 100644 index 0000000000..ff906721dc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h @@ -0,0 +1,42 @@ +/* Tutorial Map Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_TutorialMapDetector_H +#define PokemonAutomation_PokemonLZA_TutorialMapDetector_H + +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// The early map has lacks Travel Spots so we need a separate detector +class TutorialMapDetector : public StaticScreenDetector{ +public: + TutorialMapDetector(Color color = COLOR_RED, VideoOverlay* overlay = nullptr); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + ButtonDetector m_mission_list; + ButtonDetector m_close; +}; + +class TutorialMapWatcher : public DetectorToFinder{ +public: + TutorialMapWatcher( + Color color = COLOR_RED, VideoOverlay* overlay = nullptr, + std::chrono::milliseconds hold_duration = std::chrono::milliseconds(100) + ) + : DetectorToFinder("TutorialMapWatcher", hold_duration, color, overlay) + {} +}; + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.cpp new file mode 100644 index 0000000000..15b50e2fb7 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.cpp @@ -0,0 +1,121 @@ +/* Booth Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Kernels/Waterfill/Kernels_Waterfill.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Types.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "PokemonLZA_BoothDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +BoothDetector::BoothDetector(Color color) + : m_color(color) + , m_search_box(0.18, 0.12, 0.78, 0.55) +{} + +void BoothDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_search_box); +} + +bool BoothDetector::detect(const ImageViewRGB32& screen){ + m_roof_box = {}; + const ImagePixelBox search = floatbox_to_pixelbox(screen, m_search_box); + const ImageViewRGB32 crop = extract_box_reference(screen, search); + // Keep the bright cyan strip, excluding beige walls and the green zone gate. + ImageRGB32 cyan = crop.copy(); + for (size_t y = 0; y < cyan.height(); y++){ + for (size_t x = 0; x < cyan.width(); x++){ + const uint32_t pixel = cyan.pixel(x, y); + const uint32_t r = (pixel >> 16) & 0xff; + const uint32_t g = (pixel >> 8) & 0xff; + const uint32_t b = pixel & 0xff; + const bool is_cyan = g >= 180 && b >= 180 && r + 25 <= std::min(g, b) && + std::max(g, b) - std::min(g, b) <= 60; + cyan.pixel(x, y) = is_cyan ? 0xffffffff : 0xff000000; + } + } + PackedBinaryMatrix mask = compress_rgb32_to_binary_range(cyan, 0xffffffff, 0xffffffff); + const double scale = screen.height() / 1080.0; + const auto objects = Kernels::Waterfill::find_objects_inplace(mask, size_t(100 * scale * scale)); + bool found = false; + for (const auto& object : objects){ + const double width = static_cast(object.width()); + const double height = static_cast(object.height()); + if (width < 70 * scale || width > 600 * scale || height < 2 * scale || + width / height < 10 || width / height > 120 || object.area < width * height * 0.3){ + continue; + } + const ImagePixelBox roof( + search.min_x + object.min_x, search.min_y + object.min_y, + search.min_x + object.max_x, search.min_y + object.max_y + ); + // Require the gray roof above the strip to reject cyan battle boundaries. + if (roof.min_y < width * 0.1){ + continue; + } + const ImagePixelBox roof_top( + roof.min_x + size_t(width * 0.15), roof.min_y - size_t(width * 0.1), + roof.max_x - size_t(width * 0.15), roof.min_y - size_t(width * 0.025) + ); + size_t gray = 0; + for (size_t y = roof_top.min_y; y < roof_top.max_y; y++){ + for (size_t x = roof_top.min_x; x < roof_top.max_x; x++){ + const uint32_t pixel = screen.pixel(x, y); + const uint32_t r = (pixel >> 16) & 0xff; + const uint32_t g = (pixel >> 8) & 0xff; + const uint32_t b = pixel & 0xff; + if (r >= 90 && r <= 235 && g >= 100 && b >= 100 && + std::max(g, b) - std::min(g, b) <= 40 && + std::max(r, g) - std::min(r, g) <= 60){ + gray++; + } + } + } + if (gray < roof_top.area() * 0.5){ + continue; + } + // The cyan strip must sit above a substantial dark wall, not sky or HUD. + const ImagePixelBox wall( + roof.min_x + size_t(width * 0.15), roof.max_y + size_t(width * 0.08), + roof.max_x - size_t(width * 0.15), roof.max_y + size_t(width * 0.5) + ); + if (wall.max_y > screen.height()){ + continue; + } + size_t dark = 0; + for (size_t y = wall.min_y; y < wall.max_y; y++){ + for (size_t x = wall.min_x; x < wall.max_x; x++){ + const uint32_t pixel = screen.pixel(x, y); + const uint32_t r = (pixel >> 16) & 0xff; + const uint32_t g = (pixel >> 8) & 0xff; + const uint32_t b = pixel & 0xff; + if (r <= 110 && g <= 145 && b <= 145){ + dark++; + } + } + } + if (dark < wall.area() * 0.4){ + continue; + } + // Ambiguous views must not steer toward an arbitrary candidate. + if (found){ + m_roof_box = {}; + return false; + } + m_roof_box = pixelbox_to_floatbox(screen, roof); + found = true; + } + return found; +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h new file mode 100644 index 0000000000..16fef20a61 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h @@ -0,0 +1,46 @@ +/* Booth Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_BoothDetector_H +#define PokemonAutomation_PokemonLZA_BoothDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// Locate the Wild Zone 1 booth by its cyan roof strip and dark wall below it. +// No template is used: the booth's perspective and position can change. +class BoothDetector : public StaticScreenDetector{ +public: + BoothDetector(Color color = COLOR_CYAN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + + // Valid only after a successful detection. Coordinates are screen-relative. + const ImageFloatBox& roof_box() const { return m_roof_box; } + +private: + Color m_color; + ImageFloatBox m_search_box; + ImageFloatBox m_roof_box; +}; + +class BoothWatcher : public DetectorToFinder{ +public: + BoothWatcher(Color color = COLOR_CYAN) + : DetectorToFinder("BoothWatcher", std::chrono::milliseconds(250), color) + {} +}; + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.cpp new file mode 100644 index 0000000000..5b053e2450 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.cpp @@ -0,0 +1,268 @@ +/* Door Entry Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Types.h" +#include "CommonTools/ImageMatch/ExactImageMatcher.h" +#include "CommonTools/ImageMatch/WaterfillTemplateMatcher.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/Images/WaterfillUtilities.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "PokemonLZA_DoorEntryDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class DoorEntryArrowMatcher : public ImageMatch::WaterfillTemplateMatcher{ +public: + DoorEntryArrowMatcher(const char* path) + : WaterfillTemplateMatcher(path, Color(0xffc0c0c0), COLOR_WHITE, 100) + { + // Compare isolated strokes against black to verify gaps + // Preserve antialiasing on the white edges + m_matcher = std::make_unique(filter_rgb32_range( + image_template(), 0xffa0a0a0, 0xffffffff, COLOR_BLACK, false + )); + } + + virtual double rmsd_original( + Resolution resolution, const ImageViewRGB32& image, + const Kernels::Waterfill::WaterfillObject& object + ) const override{ + ImageRGB32 component = filter_rgb32_range( + extract_box_reference(image, object), 0xffa0a0a0, 0xffffffff, COLOR_BLACK, false + ); + return WaterfillTemplateMatcher::rmsd_precropped(resolution, component, object); + } +}; + +class DoorEntryMatcher : public ImageMatch::WaterfillTemplateMatcher{ +public: + DoorEntryMatcher() + : WaterfillTemplateMatcher("PokemonLZA/DoorEntry.png", Color(0xffc0c0c0), COLOR_WHITE, 100) + , m_arrow("PokemonLZA/DoorEntryArrow.png") + { + // Match the bright doorway strokes and ignore the background and soft edges + // ExactImageMatcher excludes zero-alpha template pixels from the comparison + m_matcher = std::make_unique(filter_rgb32_range( + image_template(), 0xffe0e0e0, 0xffffffff, Color(0), false + )); + m_aspect_ratio_lower = 0.85; + m_aspect_ratio_upper = 1.15; + m_area_ratio_lower = 0.75; + m_area_ratio_upper = 1.25; + } + + virtual bool check_image(Resolution resolution, const ImageViewRGB32& image) const override{ + return image.width() >= 40 * resolution.width / 3840 + && image.height() >= 32 * resolution.height / 2160 + && image.width() <= 160 * resolution.width / 3840 + && image.height() <= 140 * resolution.height / 2160; + } + + virtual double rmsd_original( + Resolution resolution, const ImageViewRGB32& image, + const Kernels::Waterfill::WaterfillObject& object + ) const override{ + const double outline_rmsd = WaterfillTemplateMatcher::rmsd_original(resolution, image, object); + if (outline_rmsd >= 90){ + return outline_rmsd; + } + + // The template's doorway is 25 x 34; the arrow starts 19 pixels right and 9 pixels down + // Allow a small margin for scaling/rounding + const double scale_x = object.width() / 25.0; + const double scale_y = object.height() / 34.0; + const size_t x = object.min_x + static_cast(16 * scale_x + 0.5); + const size_t y = object.min_y + static_cast(6 * scale_y + 0.5); + const size_t width = static_cast(30 * scale_x + 0.5); + const size_t height = static_cast(24 * scale_y + 0.5); + if (x + width > image.width() || y + height > image.height()){ + return 99999; + } + const size_t min_area = std::max(1, static_cast(80 * scale_x * scale_y)); + const size_t max_area = static_cast(1000 * scale_x * scale_y); + const bool arrow_found = match_template_by_waterfill( + resolution, image.sub_image(x, y, width, height), m_arrow, + { + {0xffa0a0a0, 0xffffffff}, + {0xffc0c0c0, 0xffffffff}, + {0xffe0e0e0, 0xffffffff}, + }, + {min_area, max_area}, 100, + [](Kernels::Waterfill::WaterfillObject&){ + return true; + } + ); + return arrow_found ? outline_rmsd : 99999; + } + +private: + DoorEntryArrowMatcher m_arrow; +}; + +DoorEntryDetector::DoorEntryDetector(Color color, VideoOverlay* overlay, const ImageFloatBox& box) + : m_color(color) + , m_overlay(overlay) + , m_box(box) +{} + +void DoorEntryDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_box); +} + +bool DoorEntryDetector::detect(const ImageViewRGB32& screen){ + static const DoorEntryMatcher matcher; + const double scale = screen.height() / 1080.0; + const size_t min_area = std::max(1, static_cast(100 * scale * scale)); + const size_t max_area = static_cast(3000 * scale * scale); + const bool found = match_template_by_waterfill( + screen.size(), extract_box_reference(screen, m_box), matcher, + { + {0xffa0a0a0, 0xffffffff}, + {0xffc0c0c0, 0xffffffff}, + {0xffe0e0e0, 0xffffffff}, + // Bright capture-card edge pixels can widen the candidate bounds. + {0xfff0f0f0, 0xffffffff}, + {0xfff8f8f8, 0xffffffff}, + }, + {min_area, max_area}, 90, + [&](Kernels::Waterfill::WaterfillObject& object){ + m_last_detected = translate_to_parent(screen, m_box, object); + return true; + } + ); + if (!found){ + reset_state(); + }else if (m_overlay){ + m_last_detected_box.emplace(*m_overlay, m_last_detected, COLOR_GREEN); + } + return found; +} + +void DoorEntryDetector::reset_state(){ + m_last_detected = {}; + m_last_detected_box.reset(); +} + +class Test_DoorEntryDetector : public ImageUnitTest{ +public: + Test_DoorEntryDetector(const std::string& image, bool expected, size_t icon_x = 948, size_t icon_y = 323) + : ImageUnitTest("PokemonLZA::DoorEntryDetector - " + image, {image}) + , m_expected(expected) + , m_icon_x(icon_x) + , m_icon_y(icon_y) + { + m_memory += MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + DoorEntryDetector detector(COLOR_RED); + const bool detected = detector.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "door entry at height " + std::to_string(height)); + if (m_expected){ + DoorEntryDetector outside(COLOR_RED, nullptr, {0.0, 0.0, 0.25, 0.5}); + const bool outside_detected = outside.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(outside_detected, false, "search region excludes door"); + + // Require a sustained match and verify that reset starts a new hold. + DoorEntryWatcher watcher(COLOR_RED); + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + std::chrono::milliseconds(200)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial watcher frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained watcher match"); + watcher.reset_state(); + const bool after_reset = watcher.process_frame(scaled, start + std::chrono::milliseconds(300)); + TEST_RESULT_COMPONENT_EQUAL_STR(after_reset, false, "watcher reset"); + + // A doorway outline without its separate arrow must not match. + const ImagePixelBox arrow_box = floatbox_to_pixelbox( + scaled, {(m_icon_x + 24.0) / 1920, (m_icon_y + 13.0) / 1080, 24.0 / 1920, 18.0 / 1080} + ); + ImageRGB32 arrow = extract_box_reference(scaled, arrow_box).copy(); + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(arrow_box.min_x + x, arrow_box.min_y + y) = 0xff202020; + } + } + const bool without_arrow = detector.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(without_arrow, false, "entry arrow removed"); + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(arrow_box.min_x + x, arrow_box.min_y + y) = arrow.pixel(x, y); + } + } + + // Remove only the entry icon: the quest marker above it must not match. + const ImagePixelBox icon_box = floatbox_to_pixelbox( + scaled, {m_icon_x / 1920.0, m_icon_y / 1080.0, 52.0 / 1920, 42.0 / 1080} + ); + ImageRGB32 icon = extract_box_reference(scaled, icon_box).copy(); + for (size_t y = 0; y < icon.height(); y++){ + for (size_t x = 0; x < icon.width(); x++){ + scaled.pixel(icon_box.min_x + x, icon_box.min_y + y) = 0xff202020; + } + } + const bool erased = detector.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(erased, false, "entry icon removed"); + + // The separate arrow alone must not match a doorway. + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(arrow_box.min_x + x, arrow_box.min_y + y) = arrow.pixel(x, y); + } + } + const bool arrow_only = detector.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(arrow_only, false, "entry arrow without doorway"); + + // The same icon must also match elsewhere in the search region. + const size_t target_x = scaled.width() * 3 / 5; + const size_t target_y = scaled.height() / 2; + for (size_t y = 0; y < icon.height(); y++){ + for (size_t x = 0; x < icon.width(); x++){ + scaled.pixel(target_x + x, target_y + y) = icon.pixel(x, y); + } + } + const bool shifted = detector.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(shifted, true, "entry icon moved"); + } + } + return true; + } + +private: + bool m_expected; + size_t m_icon_x; + size_t m_icon_y; +}; + +void add_tests_DoorEntryDetector(UnitTestDatabase& database){ + database.add("PokemonLZA/DoorEntryDetector/pokemon_center_entry.png", true); + database.add("PokemonLZA/DoorEntryDetector/hotel_elevator_entry.png", true, 1317, 605); + database.add("PokemonLZA/DoorEntryDetector/hotel_room_alternate_card.png", true, 848, 389); + database.add("PokemonLZA/DoorEntryDetector/hotel_room_bright_outline.png", true, 848, 389); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_False.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); + database.add("PokemonLZA/ButtonDetector/french_fossil_1_ButtonA_True.png", false); + database.add("PokemonLZA/OverworldPartySelectionDetector/mac_zone_10_0_5.png", false); +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.h new file mode 100644 index 0000000000..4eb547fad5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.h @@ -0,0 +1,57 @@ +/* Door Entry Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_DoorEntryDetector_H +#define PokemonAutomation_PokemonLZA_DoorEntryDetector_H + +#include +#include +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +class UnitTestDatabase; +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class DoorEntryDetector : public StaticScreenDetector{ +public: + DoorEntryDetector( + Color color, VideoOverlay* overlay = nullptr, + const ImageFloatBox& box = {0.2, 0.15, 0.65, 0.65} + ); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + virtual void reset_state() override; + + const ImageFloatBox& last_detected() const{ return m_last_detected; } + +private: + Color m_color; + VideoOverlay* m_overlay; + ImageFloatBox m_box; + ImageFloatBox m_last_detected; + std::optional m_last_detected_box; +}; + +class DoorEntryWatcher : public DetectorToFinder{ +public: + DoorEntryWatcher( + Color color, VideoOverlay* overlay = nullptr, + const ImageFloatBox& box = {0.2, 0.15, 0.65, 0.65}, + std::chrono::milliseconds hold_duration = std::chrono::milliseconds(200) + ) + : DetectorToFinder("DoorEntryWatcher", hold_duration, color, overlay, box) + {} +}; + +void add_tests_DoorEntryDetector(UnitTestDatabase& database); + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.cpp new file mode 100644 index 0000000000..6b498d9f5a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.cpp @@ -0,0 +1,93 @@ +/* Event Marker Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Kernels/Waterfill/Kernels_Waterfill.h" +#include "Kernels/Waterfill/Kernels_Waterfill_Types.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/Images/BinaryImage_FilterRgb32.h" +#include "PokemonLZA_EventMarkerDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +EventMarkerDetector::EventMarkerDetector(Color color) + : m_color(color) + // Camera can put the event marker against the top edge + , m_search_box(0.02, 0.0, 0.96, 0.73) +{} + +void EventMarkerDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_search_box); +} + +bool EventMarkerDetector::detect(const ImageViewRGB32& screen){ + m_marker_box = {}; + const ImagePixelBox search = floatbox_to_pixelbox(screen, m_search_box); + ImageRGB32 crop = extract_box_reference(screen, search).copy(); + // Hide the minimap, so we don't accidentally match the marker on it + const size_t minimap_right = size_t(screen.width() * 0.18); + const size_t minimap_bottom = size_t(screen.height() * 0.30); + for (size_t y = search.min_y; y < std::min(search.max_y, minimap_bottom); y++){ + for (size_t x = search.min_x; x < std::min(search.max_x, minimap_right); x++){ + crop.pixel(x - search.min_x, y - search.min_y) = 0xff000000; + } + } + PackedBinaryMatrix mask = compress_rgb32_to_binary_range(crop, 200, 255, 140, 255, 0, 140); + const double scale = screen.height() / 1080.0; + const auto objects = Kernels::Waterfill::find_objects_inplace(mask, size_t(120 * scale * scale)); + bool found = false; + for (const auto& object : objects){ + const double width = static_cast(object.width()); + const double height = static_cast(object.height()); + const double aspect = width / height; + if (width < 15 * scale || width > 100 * scale || height < 20 * scale || height > 120 * scale || + aspect < 0.5 || aspect > 1.1 || object.area < width * height * 0.45){ + continue; + } + const ImagePixelBox badge( + search.min_x + object.min_x, search.min_y + object.min_y, + search.min_x + object.max_x, search.min_y + object.max_y + ); + // Check the shape of !, ignoring the white border + const auto white_fraction = [&](double x0, double y0, double x1, double y1){ + const ImagePixelBox box( + badge.min_x + size_t(width * x0), badge.min_y + size_t(height * y0), + badge.min_x + size_t(width * x1), badge.min_y + size_t(height * y1) + ); + size_t white = 0; + for (size_t y = box.min_y; y < box.max_y; y++){ + for (size_t x = box.min_x; x < box.max_x; x++){ + const uint32_t pixel = screen.pixel(x, y); + if (((pixel >> 16) & 0xff) >= 200 && ((pixel >> 8) & 0xff) >= 200 && (pixel & 0xff) >= 180){ + white++; + } + } + } + return white / static_cast(box.area()); + }; + // Sample the shape of !, ignoring the white border + if (white_fraction(0.4, 0.2, 0.6, 0.55) < 0.3 || + white_fraction(0.35, 0.65, 0.65, 0.85) < 0.1 || + white_fraction(0.35, 0.55, 0.65, 0.65) > 0.65){ + continue; + } + // Off-center markers omit the green flag while the yellow ! badge is shared. + if (found){ + m_marker_box = {}; + return false; + } + m_marker_box = pixelbox_to_floatbox(screen, badge); + found = true; + } + return found; +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h new file mode 100644 index 0000000000..b050fbc5a8 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h @@ -0,0 +1,61 @@ +/* Event Marker Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_EventMarkerDetector_H +#define PokemonAutomation_PokemonLZA_EventMarkerDetector_H + +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class EventMarkerDetector : public StaticScreenDetector{ +public: + EventMarkerDetector(Color color = COLOR_YELLOW); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + + // Valid only after a successful detection, excludes the flag to its right + const ImageFloatBox& marker_box() const { return m_marker_box; } + +private: + Color m_color; + ImageFloatBox m_search_box; + ImageFloatBox m_marker_box; +}; + +class EventMarkerWatcher : public DetectorToFinder{ +public: + EventMarkerWatcher(Color color = COLOR_YELLOW) + : DetectorToFinder("EventMarkerWatcher", std::chrono::milliseconds(200), color) + {} +}; + +// Require the marker to remain centered while its flag/flagless version settles +class CenteredEventMarkerWatcher : public DetectorToFinder{ +public: + CenteredEventMarkerWatcher(Color color = COLOR_YELLOW) + : DetectorToFinder("CenteredEventMarkerWatcher", std::chrono::milliseconds(400), color) + {} + + virtual bool detect(const ImageViewRGB32& screen) override{ + if (!EventMarkerDetector::detect(screen)){ + return false; + } + const ImageFloatBox& box = marker_box(); + const double center = box.x + box.width / 2; + return center >= 0.48 && center <= 0.52; + } +}; + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.cpp new file mode 100644 index 0000000000..9e9d80c90c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.cpp @@ -0,0 +1,43 @@ +/* Tutorial Window Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" +#include "CommonTools/ImageMatch/ExactImageMatcher.h" +#include "CommonTools/Images/SolidColorTest.h" +#include "PokemonLZA_TutorialWindowDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +TutorialWindowDetector::TutorialWindowDetector(Color color) + : m_color(color) + , m_book(523.0 / 1920, 104.0 / 1080, 33.0 / 1920, 39.0 / 1080) + , m_top_border(0.29, 0.165, 0.42, 0.012) + , m_left_border(0.265, 0.18, 0.018, 0.68) +{} + +void TutorialWindowDetector::make_overlays(VideoOverlaySet& items) const{ + items.add(m_color, m_book); + items.add(m_color, m_top_border); + items.add(m_color, m_left_border); +} + +bool TutorialWindowDetector::detect(const ImageViewRGB32& screen){ + if (!is_white(extract_box_reference(screen, m_top_border), 600, 20) + || !is_white(extract_box_reference(screen, m_left_border), 600, 20)){ + return false; + } + // Only the fixed book badge and its green backing are in the template + // Tutorial text, illustrations, and the game behind the window can vary + static const ImageMatch::ExactImageMatcher book(RESOURCE_PATH() + "PokemonLZA/TutorialBook.png"); + return book.rmsd(extract_box_reference(screen, m_book)) <= 45; +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h new file mode 100644 index 0000000000..01e5a64e4d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h @@ -0,0 +1,48 @@ +/* Tutorial Window Detector + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_TutorialWindowDetector_H +#define PokemonAutomation_PokemonLZA_TutorialWindowDetector_H + +#include +#include "Common/Cpp/Color.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonTools/VisualDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// Match the question-book badge and white frame shared by tutorial windows +// Includes single-page windows and windows with navigation arrows +class TutorialWindowDetector : public StaticScreenDetector{ +public: + TutorialWindowDetector(Color color = COLOR_CYAN); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_book; + ImageFloatBox m_top_border; + ImageFloatBox m_left_border; +}; + +class TutorialWindowWatcher : public DetectorToFinder{ +public: + TutorialWindowWatcher( + Color color = COLOR_CYAN, + std::chrono::milliseconds hold_duration = std::chrono::milliseconds(100) + ) + : DetectorToFinder("TutorialWindowWatcher", hold_duration, color) + {} +}; + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp index bd5e98adae..2922ef5498 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Panels.cpp @@ -49,6 +49,9 @@ // Non-Shiny Hunting #include "Programs/NonShinyHunting/PokemonLZA_StatsReset.h" +// Story Automation +#include "Programs/AutoStory/PokemonLZA_AutoStory.h" + // Developer #include "Programs/TestPrograms/PokemonLZA_OverworldWatcher.h" #include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h" @@ -115,6 +118,11 @@ std::vector PanelListFactory::make_panels() const{ ret.emplace_back(make_SingleSwitchProgram()); } + ret.emplace_back("---- Story Automation ----"); + if (IS_BETA_VERSION){ + ret.emplace_back(make_SingleSwitchProgram()); + } + // ret.emplace_back("---- Non-Shiny Hunting ----"); if (IS_BETA_VERSION){ } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index ead2ea2d67..eb0fc93720 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -5,6 +5,7 @@ */ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Programs/AutoStory/PokemonLZA_AutoStory_Tests.h" #include "Inference/Donuts/PokemonLZA_DonutBerriesDetector.h" #include "Inference/Donuts/PokemonLZA_FlavorPowerScreenDetector.h" #include "Inference/PokemonLZA_AlertEyeDetector.h" @@ -12,6 +13,7 @@ #include "Inference/PokemonLZA_ButtonDetector.h" #include "Inference/Map/PokemonLZA_DirectionArrowDetector.h" #include "Inference/PokemonLZA_DialogDetector.h" +#include "Inference/PokemonLZA_DoorEntryDetector.h" #include "Inference/PokemonLZA_WeatherDetector.h" #include "Inference/PokemonLZA_HyperspaceCalorieDetector.h" #include "Inference/PokemonLZA_MainMenuDetector.h" @@ -28,10 +30,12 @@ namespace PokemonLZA{ void add_tests(UnitTestDatabase& database){ add_tests_AlertEyeDetector(database); + add_tests_AutoStory(database); add_tests_BoxCellInfoDetector(database); add_tests_ButtonDetector(database); add_tests_DirectionArrowDetector(database); add_tests_DialogDetector(database); + add_tests_DoorEntryDetector(database); add_tests_WeatherDetector(database); add_tests_DonutBerriesReader(database); add_tests_FlavorPowerScreenDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp new file mode 100644 index 0000000000..c3ce2d6831 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp @@ -0,0 +1,212 @@ +/* Auto Story + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Common/Cpp/Exceptions.h" +#include "CommonFramework/Notifications/ProgramNotifications.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/StartupChecks/VideoResolutionCheck.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "Pokemon/Pokemon_Strings.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "PokemonLZA_AutoStory_Segment_01.h" +#include "PokemonLZA_AutoStory.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +const std::vector>& ALL_AUTO_STORY_SEGMENT_LIST(){ + static const std::vector> segments = []{ + std::vector> ret; + ret.emplace_back(std::make_unique()); + return ret; + }(); + return segments; +} + +const std::vector>& ALL_AUTO_STORY_CHECKPOINT_LIST(){ + static const std::vector> checkpoints = []{ + std::vector> ret; + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + ret.emplace_back(std::make_unique()); + return ret; + }(); + return checkpoints; +} + +const StringSelectDatabase& AUTO_STORY_SEGMENTS_SELECT_DATABASE(){ + static const StringSelectDatabase database = Pokemon::make_story_select_database( + ALL_AUTO_STORY_SEGMENT_LIST(), 0, ALL_AUTO_STORY_SEGMENT_LIST().size() + ); + return database; +} + +const StringSelectDatabase& AUTO_STORY_CHECKPOINTS_SELECT_DATABASE(){ + static const StringSelectDatabase database = Pokemon::make_story_select_database( + ALL_AUTO_STORY_CHECKPOINT_LIST(), 0, ALL_AUTO_STORY_CHECKPOINT_LIST().size() + ); + return database; +} + +AutoStory_Descriptor::AutoStory_Descriptor() + : SingleSwitchProgramDescriptor( + "PokemonLZA:AutoStory", + Pokemon::STRING_POKEMON + " LZA", "Auto Story", + "Programs/PokemonLZA/AutoStory.html", + "Progress through the main story of LZA (currently only tutorial)", + PanelQualityStanding::OFFICIAL_BETA, + ProgramControllerClass::StandardController_NoRestrictions, + FeedbackType::REQUIRED, + AllowCommandsWhenRunning::DISABLE_COMMANDS + ) +{} + +std::unique_ptr AutoStory_Descriptor::make_stats() const{ + return std::make_unique(); +} + +AutoStory::AutoStory() + : GO_HOME_WHEN_DONE(true) + , STORY_SECTION( + "Story Section:", + {{StorySection::TUTORIAL, "tutorial", "Tutorial"}}, + LockMode::LOCK_WHILE_RUNNING, + StorySection::TUTORIAL + ) + , ENABLE_ADVANCED_MODE( + "Advanced mode:
Select start/end checkpoints instead of a starting segment", + LockMode::LOCK_WHILE_RUNNING, + false + ) + , START_SEGMENT( + "Start Segment:", AUTO_STORY_SEGMENTS_SELECT_DATABASE(), + LockMode::LOCK_WHILE_RUNNING, "0" + ) + , START_CHECKPOINT( + "Start Checkpoint:", AUTO_STORY_CHECKPOINTS_SELECT_DATABASE(), + LockMode::LOCK_WHILE_RUNNING, "0" + ) + , END_CHECKPOINT( + "End Checkpoint:", AUTO_STORY_CHECKPOINTS_SELECT_DATABASE(), + LockMode::LOCK_WHILE_RUNNING, "4" + ) + , START_DESCRIPTION("") + , END_DESCRIPTION("") + , SETUP_NOTE( + "Place the player at the exact position described for the selected start point" + "Later checkpoints assume the preceding story segments are complete with the Text Speed on High" + ) + , STARTER( + "Starter:
Choose the starter Pokemon to receive", + { + {StarterChoice::CHIKORITA, "chikorita", "Chikorita (Grass)"}, + {StarterChoice::TOTODILE, "totodile", "Totodile (Water)"}, + {StarterChoice::TEPIG, "tepig", "Tepig (Fire)"}, + }, + LockMode::LOCK_WHILE_RUNNING, + StarterChoice::TOTODILE + ) + , NOTIFICATIONS({ + &NOTIFICATION_PROGRAM_FINISH, + &NOTIFICATION_ERROR_FATAL, + }) +{ + PA_ADD_OPTION(GO_HOME_WHEN_DONE); + PA_ADD_OPTION(STORY_SECTION); + PA_ADD_OPTION(ENABLE_ADVANCED_MODE); + PA_ADD_OPTION(START_SEGMENT); + PA_ADD_OPTION(START_CHECKPOINT); + PA_ADD_OPTION(END_CHECKPOINT); + PA_ADD_OPTION(START_DESCRIPTION); + PA_ADD_OPTION(END_DESCRIPTION); + PA_ADD_OPTION(SETUP_NOTE); + PA_ADD_OPTION(STARTER); + PA_ADD_OPTION(BATTLE_AI); + PA_ADD_OPTION(NOTIFICATIONS); + + on_config_value_changed(this); + STORY_SECTION.add_listener(*this); + ENABLE_ADVANCED_MODE.add_listener(*this); + START_SEGMENT.add_listener(*this); + START_CHECKPOINT.add_listener(*this); + END_CHECKPOINT.add_listener(*this); +} + +AutoStory::~AutoStory(){ + STORY_SECTION.remove_listener(*this); + ENABLE_ADVANCED_MODE.remove_listener(*this); + START_SEGMENT.remove_listener(*this); + START_CHECKPOINT.remove_listener(*this); + END_CHECKPOINT.remove_listener(*this); +} + +void AutoStory::on_config_value_changed(void*){ + const bool advanced = ENABLE_ADVANCED_MODE; + START_SEGMENT.set_visibility(advanced ? ConfigOptionState::HIDDEN : ConfigOptionState::ENABLED); + START_CHECKPOINT.set_visibility(advanced ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + END_CHECKPOINT.set_visibility(advanced ? ConfigOptionState::ENABLED : ConfigOptionState::HIDDEN); + if (advanced){ + START_DESCRIPTION.set_text("Start: " + ALL_AUTO_STORY_CHECKPOINT_LIST()[START_CHECKPOINT.index()]->start_text()); + END_DESCRIPTION.set_text("End: " + ALL_AUTO_STORY_CHECKPOINT_LIST()[END_CHECKPOINT.index()]->end_text()); + }else{ + START_DESCRIPTION.set_text(ALL_AUTO_STORY_SEGMENT_LIST()[START_SEGMENT.index()]->start_text()); + END_DESCRIPTION.set_text(ALL_AUTO_STORY_SEGMENT_LIST().back()->end_text()); + } +} + +std::string AutoStory::check_validity() const{ + std::string error = SingleSwitchProgramInstance::check_validity(); + if (!error.empty()){ + return error; + } + if (ENABLE_ADVANCED_MODE && START_CHECKPOINT.index() > END_CHECKPOINT.index()){ + return "The start checkpoint cannot be later than the end checkpoint."; + } + return ""; +} + +void AutoStory::program(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + const std::string error = check_validity(); + if (!error.empty()){ + throw UserSetupError(env.console, error); + } + assert_16_9_720p_min(env.logger(), env.console); + require_player(env.console, context, BUTTON_L); + + AutoStoryOptions options{ + STARTER, + BATTLE_AI, + }; + AutoStoryStats& stats = env.current_stats(); + if (ENABLE_ADVANCED_MODE){ + const auto& checkpoints = ALL_AUTO_STORY_CHECKPOINT_LIST(); + for (size_t index = START_CHECKPOINT.index(); index <= END_CHECKPOINT.index(); index++){ + context.throw_if_cancelled(); + checkpoints[index]->run_checkpoint(env, context, options, stats); + } + }else{ + const auto& segments = ALL_AUTO_STORY_SEGMENT_LIST(); + for (size_t index = START_SEGMENT.index(); index < segments.size(); index++){ + context.throw_if_cancelled(); + segments[index]->run_segment(env, context, options, stats); + } + } + + env.log("Story input sequence finished."); + context.wait_for_all_requests(); + send_program_finished_notification( + env, NOTIFICATION_PROGRAM_FINISH, "Auto Story finished", + env.console.video().snapshot() + ); + GO_HOME_WHEN_DONE.run_end_of_program(context); +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h new file mode 100644 index 0000000000..5778a8fa8c --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h @@ -0,0 +1,66 @@ +/* Auto Story + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_H +#define PokemonAutomation_PokemonLZA_AutoStory_H + +#include +#include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/StaticTextOption.h" +#include "CommonFramework/Notifications/EventNotificationsTable.h" +#include "CommonTools/Options/StringSelectOption.h" +#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h" +#include "VideoGames/PokemonLZA/Options/PokemonLZA_BattleAIOption.h" +#include "PokemonLZA_AutoStoryTools.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class AutoStory_Descriptor : public SingleSwitchProgramDescriptor{ +public: + AutoStory_Descriptor(); + virtual std::unique_ptr make_stats() const override; +}; + +class AutoStory : public SingleSwitchProgramInstance, public ConfigOption::Listener{ +public: + using Descriptor = AutoStory_Descriptor; + AutoStory(); + virtual ~AutoStory(); + virtual std::string check_validity() const override; + virtual void program(SingleSwitchProgramEnvironment& env, ProControllerContext& context) override; + +private: + virtual void on_config_value_changed(void* object) override; + + enum class StorySection{ + TUTORIAL, + }; + + GoHomeWhenDoneOption GO_HOME_WHEN_DONE; + EnumDropdownOption STORY_SECTION; + BooleanCheckBoxOption ENABLE_ADVANCED_MODE; + StringSelectOption START_SEGMENT; + StringSelectOption START_CHECKPOINT; + StringSelectOption END_CHECKPOINT; + StaticTextOption START_DESCRIPTION; + StaticTextOption END_DESCRIPTION; + StaticTextOption SETUP_NOTE; + EnumDropdownOption STARTER; + BattleAIOption BATTLE_AI; + EventNotificationsOption NOTIFICATIONS; +}; + +const std::vector>& ALL_AUTO_STORY_SEGMENT_LIST(); +const std::vector>& ALL_AUTO_STORY_CHECKPOINT_LIST(); + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp new file mode 100644 index 0000000000..8da9581644 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp @@ -0,0 +1,732 @@ +/* Auto Story Tools + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h" +#include "VideoGames/PokemonLZA/Programs/PokemonLZA_TrainerBattle.h" +#include "PokemonLZA_AutoStoryTools.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +void clear_dialog( + ConsoleHandle& console, ProControllerContext& context, + ClearDialogMode mode, Button button, Milliseconds timeout +){ + if (timeout <= 0ms){ + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Dialog timeout must be positive."); + } + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + constexpr Milliseconds choice_release = 1500ms; + + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &console.overlay()); + BlackScreenOverWatcher black_screen_over(COLOR_BLUE); + DetectorToFinder white_screen("WhiteScreenWatcher", 100ms, COLOR_WHITE); + auto battle_watchers = make_trainer_battle_watchers(100ms); + SelectionArrowWatcher dialog_option( + COLOR_GREEN, &console.overlay(), SelectionArrowType::RIGHT, + {0.5, 0.2, 0.45, 0.6}, 100ms + ); + TutorialPageWatcher tutorial_page(COLOR_CYAN, &console.overlay()); + TutorialWindowWatcher tutorial_window(COLOR_CYAN); + std::vector callbacks; + const char* stop_description = nullptr; + switch (mode){ + case ClearDialogMode::STOP_OVERWORLD: + callbacks.emplace_back(overworld); + stop_description = "overworld"; + break; + case ClearDialogMode::STOP_BLACK_SCREEN: + callbacks.emplace_back(black_screen_over); + stop_description = "end of black-screen transition"; + break; + case ClearDialogMode::STOP_WHITE_SCREEN: + callbacks.emplace_back(white_screen); + stop_description = "white screen"; + break; + case ClearDialogMode::STOP_BATTLE: + for (auto& watcher : battle_watchers){ + callbacks.emplace_back(watcher); + } + stop_description = "opponent party indicators"; + break; + case ClearDialogMode::STOP_CHOICES: + stop_description = "dialog choices"; + break; + default: + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Unknown dialog stop condition."); + } + + const int option_index = static_cast(callbacks.size()); + callbacks.emplace_back(dialog_option); + callbacks.emplace_back(tutorial_page); + callbacks.emplace_back(tutorial_window); + // Keep the stop watchers alive across choices, including black-screen state + while (current_time() < deadline){ + context.throw_if_cancelled(); + const Milliseconds remaining = std::chrono::duration_cast(deadline - current_time()); + if (remaining <= 0ms){ + break; + } + dialog_option.reset_state(); + tutorial_page.reset_state(); + tutorial_window.reset_state(); + int ret = run_until( + console, context, + [=](ProControllerContext& context){ + if (mode == ClearDialogMode::STOP_CHOICES){ + // Check for choices before the first press as well as between presses + const Milliseconds settle = std::min(choice_release, remaining); + pbf_wait(context, settle); + for (Milliseconds left = remaining - settle; left > 0ms;){ + context.throw_if_cancelled(); + const Milliseconds hold = std::min(100ms, left); + const Milliseconds release = std::min(choice_release, left - hold); + pbf_press_button(context, button, hold, release); + left -= hold + release; + } + }else{ + pbf_mash_button(context, button, remaining); + } + }, + callbacks + ); + if (ret < 0){ + break; + } + if (ret < option_index || (ret == option_index && mode == ClearDialogMode::STOP_CHOICES)){ + context.wait_for_all_requests(); + console.log(std::string("clear_dialog(): Detected ") + stop_description + "."); + return; + } + console.log(ret == option_index + ? "clear_dialog(): Confirming dialog option." + : "clear_dialog(): Advancing tutorial window." + ); + pbf_press_button(context, BUTTON_A, 100ms, + mode == ClearDialogMode::STOP_CHOICES ? choice_release : 200ms); + context.wait_for_all_requests(); + } + + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "clear_dialog(): Did not detect the requested stop condition within " + + std::to_string(timeout.count()) + " milliseconds.", + console + ); +} + +void press_button_until_dialog( + ConsoleHandle& console, ProControllerContext& context, Button button, + Milliseconds hold, Milliseconds release, Milliseconds timeout +){ + if (hold <= 0ms || release < 0ms){ + throw InternalProgramError( + &console.logger(), PA_CURRENT_FUNCTION, + "Button hold must be positive and release must be nonnegative." + ); + } + context.wait_for_all_requests(); + + FlatWhiteDialogWatcher white_dialog(COLOR_WHITE, &console.overlay()); + BlueDialogWatcher blue_dialog(COLOR_BLUE, &console.overlay()); + ItemReceiveWatcher item_receive(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal_dialog(COLOR_CYAN, &console.overlay()); + LightBlueDialogWatcher light_blue_dialog(COLOR_BLUE, &console.overlay()); + TransparentBattleDialogWatcher battle_dialog(COLOR_YELLOW, &console.overlay()); + int ret = run_until( + console, context, + [=](ProControllerContext& context){ + for (Milliseconds remaining = timeout; remaining > 0ms;){ + context.throw_if_cancelled(); + const Milliseconds press_hold = std::min(hold, remaining); + const Milliseconds press_release = std::min(release, remaining - press_hold); + pbf_press_button(context, button, press_hold, press_release); + remaining -= press_hold + press_release; + } + }, + { + white_dialog, + blue_dialog, + item_receive, + teal_dialog, + light_blue_dialog, + battle_dialog, + } + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "press_button_until_dialog(): No dialog detected within " + + std::to_string(timeout.count()) + " milliseconds.", + console + ); + } + + context.wait_for_all_requests(); + console.log("press_button_until_dialog(): Detected dialog."); +} + +void mash_button_until_button( + ConsoleHandle& console, ProControllerContext& context, + Button mash_button, ButtonType button_type, const ImageFloatBox& button_box, + Milliseconds timeout +){ + context.wait_for_all_requests(); + + ButtonWatcher button(COLOR_RED, button_type, button_box, &console.overlay(), 200ms); + int ret = run_until( + console, context, + [=](ProControllerContext& context){ + pbf_mash_button(context, mash_button, timeout); + }, + {button} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "mash_button_until_button(): Requested button not detected within " + + std::to_string(timeout.count()) + " milliseconds.", + console + ); + } + + context.wait_for_all_requests(); + console.log("mash_button_until_button(): Detected requested button."); +} + +void mash_button_until_door_entry( + ConsoleHandle& console, ProControllerContext& context, + Button mash_button, Milliseconds timeout +){ + context.wait_for_all_requests(); + + DoorEntryWatcher door_entry(COLOR_RED, &console.overlay()); + int ret = run_until( + console, context, + [=](ProControllerContext& context){ + pbf_mash_button(context, mash_button, timeout); + }, + {door_entry} + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "mash_button_until_door_entry(): No door-entry icon detected within " + + std::to_string(timeout.count()) + " milliseconds.", + console + ); + } + + context.wait_for_all_requests(); + console.log("mash_button_until_door_entry(): Detected door-entry icon."); +} + +std::array make_trainer_battle_watchers(Milliseconds hold_duration){ + // ImageMatchWatcher crops the same normalized box in the reference and video + // The reference repeats a filled opponent-party icon at all six HUD slots + static const auto reference = std::make_shared( + RESOURCE_PATH() + "PokemonLZA/AutoStory/TrainerParty.png" + ); + const auto box = [](size_t slot){ + return ImageFloatBox((846.0 + 40.8 * slot) / 1920, 60.0 / 1080, 23.0 / 1920, 24.0 / 1080); + }; + // Watch each slot independently to disregard fainted Pokemon + return {{ + {reference, box(0), 65, false, hold_duration, COLOR_GREEN}, + {reference, box(1), 65, false, hold_duration, COLOR_GREEN}, + {reference, box(2), 65, false, hold_duration, COLOR_GREEN}, + {reference, box(3), 65, false, hold_duration, COLOR_GREEN}, + {reference, box(4), 65, false, hold_duration, COLOR_GREEN}, + {reference, box(5), 65, false, hold_duration, COLOR_GREEN}, + }}; +} + +ImageFloatBox tutorial_pager_box(){ + return {0.25, 0.5, 0.5, 0.47}; +} + +TutorialPageWatcher::TutorialPageWatcher(Color color, VideoOverlay* overlay, Milliseconds hold_duration) + : DetectorToFinder( + "TutorialPageWatcher", hold_duration, color, + ButtonType::ButtonRight, tutorial_pager_box(), overlay + ) +{} + +bool TutorialPageWatcher::detect(const ImageViewRGB32& screen){ + if (ButtonDetector::detect(screen)){ + return true; + } + // The dark tutorial pager is the inverse of the standard button template + // Keep the full frame dimensions so ButtonDetector's size checks and overlays stay in the original screen coordinates + ImageRGB32 inverted = screen.copy(); + const ImagePixelBox box = floatbox_to_pixelbox(screen, tutorial_pager_box()); + for (size_t y = box.min_y; y < box.max_y; y++){ + for (size_t x = box.min_x; x < box.max_x; x++){ + inverted.pixel(x, y) ^= 0x00ffffff; + } + } + return ButtonDetector::detect(inverted); +} + +void run_battle_until_condition( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai, BattleStopCondition stop_condition, Milliseconds timeout +){ + if (timeout <= 0ms){ + throw InternalProgramError(&env.console.logger(), PA_CURRENT_FUNCTION, "Battle timeout must be positive."); + } + const char* stop_description = nullptr; + switch (stop_condition){ + case BattleStopCondition::STOP_OVERWORLD: + stop_description = "overworld"; + break; + case BattleStopCondition::STOP_BLACK_SCREEN: + stop_description = "black screen"; + break; + default: + throw InternalProgramError(&env.console.logger(), PA_CURRENT_FUNCTION, "Unknown battle stop condition."); + } + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + TrainerBattleState battle_state(battle_ai); + + while (current_time() < deadline){ + context.throw_if_cancelled(); + + OverworldPartySelectionWatcher overworld(COLOR_WHITE, &env.console.overlay()); + BlackScreenWatcher black_screen(COLOR_BLUE); + MapWatcher map(COLOR_RED, &env.console.overlay(), 100ms); + TutorialMapWatcher tutorial_map(COLOR_RED, &env.console.overlay()); + TutorialPageWatcher tutorial(COLOR_CYAN, &env.console.overlay()); + TutorialWindowWatcher tutorial_window(COLOR_CYAN); + SelectionArrowWatcher dialog_option( + COLOR_GREEN, &env.console.overlay(), SelectionArrowType::RIGHT, + {0.5, 0.2, 0.45, 0.6}, 100ms + ); + FlatWhiteDialogWatcher white_dialog(COLOR_WHITE, &env.console.overlay(), 100ms); + BlueDialogWatcher blue_dialog(COLOR_BLUE, &env.console.overlay(), 100ms); + ItemReceiveWatcher item_receive(COLOR_BLUE, &env.console.overlay(), 100ms); + TealDialogWatcher teal_dialog(COLOR_CYAN, &env.console.overlay(), 100ms); + LightBlueDialogWatcher light_blue_dialog(COLOR_BLUE, &env.console.overlay(), 100ms); + TransparentBattleDialogWatcher battle_dialog(COLOR_YELLOW, &env.console.overlay(), 100ms); + // Let dialogue detection settle before acting on the battle HUD behind it + auto battle_watchers = make_trainer_battle_watchers(250ms); + + std::vector callbacks; + if (stop_condition == BattleStopCondition::STOP_OVERWORLD){ + callbacks.emplace_back(overworld); + }else{ + callbacks.emplace_back(black_screen); + } + callbacks.insert(callbacks.end(), { + map, + tutorial_map, + tutorial, + tutorial_window, + dialog_option, + white_dialog, + blue_dialog, + item_receive, + teal_dialog, + light_blue_dialog, + battle_dialog, + }); + for (auto& watcher : battle_watchers){ + callbacks.emplace_back(watcher); + } + int ret = wait_until(env.console, context, deadline, callbacks); + switch (ret){ + case 0: + env.log(std::string("run_battle_until_condition(): Detected ") + stop_description + "."); + return; + case 1: + case 2: + env.log("run_battle_until_condition(): Closing map."); + pbf_press_button(context, BUTTON_B, 100ms, 300ms); + break; + case 3: + case 4: + env.log("run_battle_until_condition(): Advancing tutorial page."); + pbf_press_button(context, BUTTON_A, 100ms, 200ms); + break; + case 5: + env.log("run_battle_until_condition(): Confirming dialog option."); + pbf_press_button(context, BUTTON_A, 100ms, 200ms); + break; + case 6: + case 7: + case 8: + case 9: + case 10: + case 11: + env.log("run_battle_until_condition(): Advancing dialog."); + pbf_press_button(context, BUTTON_A, 100ms, 200ms); + break; + case 12: + case 13: + case 14: + case 15: + case 16: + case 17: + env.log("run_battle_until_condition(): Detected opponent party indicators."); + if (!battle_state.attempt_one_attack(env, env.console, context)){ + // Send out the selected Pokemon if no attack could be launched + pbf_press_button(context, BUTTON_UP, 500ms, 1s); + } + break; + default: + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::string("run_battle_until_condition(): Did not detect ") + stop_description + " within " + + std::to_string(timeout.count()) + " milliseconds.", + env.console + ); + } + context.wait_for_all_requests(); + } + + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::string("run_battle_until_condition(): Did not detect ") + stop_description + " within " + + std::to_string(timeout.count()) + " milliseconds.", + env.console + ); +} + +void run_wild_battle_until_condition( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai, WildBattleStopCondition stop_condition, Milliseconds timeout +){ + if (timeout <= 0ms){ + throw InternalProgramError(&env.console.logger(), PA_CURRENT_FUNCTION, "Battle timeout must be positive."); + } + switch (stop_condition){ + case WildBattleStopCondition::STOP_DIALOG: + break; + default: + throw InternalProgramError(&env.console.logger(), PA_CURRENT_FUNCTION, "Unknown wild battle stop condition."); + } + context.wait_for_all_requests(); + const WallClock deadline = current_time() + timeout; + TrainerBattleState battle_state(battle_ai); + // Require sustained white dialogue to ignore brief background matches + FlatWhiteDialogWatcher white_dialog(COLOR_WHITE, &env.console.overlay(), 1500ms); + run_until( + env.console, context, + [&](ProControllerContext& context){ + while (current_time() < deadline){ + context.throw_if_cancelled(); + if (!battle_state.attempt_one_attack(env, env.console, context)){ + pbf_wait(context, 250ms); + } + context.wait_for_all_requests(); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "run_wild_battle_until_condition(): No white dialogue detected within " + + std::to_string(timeout.count()) + " milliseconds.", + env.console + ); + }, + {white_dialog} + ); + context.wait_for_all_requests(); + env.log("run_wild_battle_until_condition(): Detected white dialogue."); +} + +void align_camera_left_of_booth(ConsoleHandle& console, ProControllerContext& context){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + 20s; + BoothWatcher booth; + // The roof overhang is wider than the wall, leaving clearance on its left + constexpr double target_left = 0.60; + constexpr double tolerance = 0.025; + while (current_time() < deadline){ + context.throw_if_cancelled(); + booth.reset_state(); + if (wait_until(console, context, std::min(deadline, current_time() + 2s), {booth}) < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "align_camera_left_of_booth(): Unable to locate the booth's cyan roof strip.", + console + ); + } + const ImageFloatBox roof = booth.roof_box(); + OverlayBoxScope roof_overlay(console.overlay(), COLOR_CYAN, roof, "Booth roof"); + console.log("Booth roof left edge: " + std::to_string(roof.x)); + if (roof.x >= target_left - tolerance && roof.x <= target_left + tolerance){ + console.log("Booth aligned to the right of the forward path."); + return; + } + // Looking left shifts the booth right; looking right shifts it left + const double direction = roof.x < target_left ? -0.35 : 0.35; + pbf_move_right_joystick(context, {direction, 0}, 80ms, 200ms); + context.wait_for_all_requests(); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "align_camera_left_of_booth(): Unable to align the booth within 20 seconds.", + console + ); +} + +void center_event_marker(ConsoleHandle& console, ProControllerContext& context){ + context.wait_for_all_requests(); + const WallClock deadline = current_time() + 30s; + EventMarkerWatcher marker; + CenteredEventMarkerWatcher centered; + constexpr double tolerance = 0.02; + bool seen_marker = false; + double last_center = 0.5; + double last_direction = 1; + size_t recovery_turns = 0; + while (current_time() < deadline){ + context.throw_if_cancelled(); + marker.reset_state(); + if (wait_until(console, context, std::min(deadline, current_time() + 750ms), {marker}) < 0){ + if (seen_marker){ + if (recovery_turns >= 4){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "center_event_marker(): Unable to recover the event marker after four camera turns.", + console + ); + } + // The flag transition can move the badge off the screen edge + // Keep turning toward the last known target to bring it back + recovery_turns++; + const bool far_from_center = std::abs(last_center - 0.5) > 0.25; + const double strength = far_from_center ? 0.65 : 0.35; + const Milliseconds hold = far_from_center ? 160ms : 60ms; + console.log("Recovering event marker in the last known direction (" + + std::to_string(recovery_turns) + " of 4)."); + pbf_move_right_joystick(context, {last_direction * strength, 0}, hold, 250ms); + }else{ + // Search around the player when the gate starts outside the view + pbf_move_right_joystick(context, {0.65, 0}, 250ms, 250ms); + } + }else{ + seen_marker = true; + recovery_turns = 0; + const ImageFloatBox box = marker.marker_box(); + OverlayBoxScope marker_overlay(console.overlay(), COLOR_YELLOW, box, "Event marker"); + const double center = box.x + box.width / 2; + last_center = center; + last_direction = center < 0.5 ? -1 : 1; + console.log("Event marker center: " + std::to_string(center)); + if (center >= 0.5 - tolerance && center <= 0.5 + tolerance){ + centered.reset_state(); + if (wait_until(console, context, std::min(deadline, current_time() + 1s), {centered}) >= 0){ + console.log("Event marker held at center. Ready to walk forward."); + return; + } + // Re-read its position if the graphic moves during confirmation + continue; + } + // Large turns at the edge, then progressively finer corrections + const double error = std::abs(center - 0.5); + double strength; + Milliseconds hold; + if (error > 0.25){ + strength = 0.65; + hold = 220ms; + }else if (error > 0.10){ + strength = 0.5; + hold = 120ms; + }else{ + strength = 0.35; + hold = 40ms; + } + pbf_move_right_joystick(context, {last_direction * strength, 0}, hold, 250ms); + } + context.wait_for_all_requests(); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "center_event_marker(): Unable to find and center the event marker within 30 seconds.", + console + ); +} + +bool party_has_at_least(ConsoleHandle& console, ProControllerContext& context, uint8_t count){ + if (count == 0 || count > 6){ + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Party size must be between 1 and 6."); + } + context.wait_for_all_requests(); + OverworldPartySelectionWatcher party(COLOR_WHITE, &console.overlay(), 250ms); + if (wait_until(console, context, 5s, {party}) < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "party_has_at_least(): Unable to read the selected party Pokemon.", + console + ); + } + const uint8_t original = party.selected_party_idx(); + bool found = original >= count - 1; + // Cycle back to the original selection so later inputs use the same Pokemon + for (size_t step = 0; step < 6; step++){ + pbf_press_dpad(context, DPAD_RIGHT, 100ms, 300ms); + context.wait_for_all_requests(); + party.reset_state(); + if (wait_until(console, context, 5s, {party}) < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "party_has_at_least(): Unable to read the party after switching Pokemon.", + console + ); + } + const uint8_t selected = party.selected_party_idx(); + found |= selected >= count - 1; + if (selected == original){ + console.log("party_has_at_least(): " + std::string(found ? "Found" : "Did not find") + + " at least " + std::to_string(count) + " party Pokemon."); + return found; + } + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "party_has_at_least(): Party selection did not return to its starting slot.", + console + ); +} + +void run_catch(ProControllerContext& context, Milliseconds post_throw_wait){ + if (post_throw_wait < 0ms){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Post-throw wait must be nonnegative."); + } + ssf_press_button(context, BUTTON_ZL | BUTTON_ZR, 500ms, 500ms, 0ms); + pbf_wait(context, post_throw_wait); + context.wait_for_all_requests(); +} + +void overworld_navigation( + ConsoleHandle& console, ProControllerContext& context, + NavigationStopCondition stop_condition, NavigationMovementMode movement_mode, + double direction_x, double direction_y, Milliseconds timeout, bool sprint, + ButtonType button_type, const ImageFloatBox& button_box +){ + if (timeout <= 0ms){ + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Navigation timeout must be positive."); + } + switch (movement_mode){ + case NavigationMovementMode::DIRECTIONAL_ONLY: + case NavigationMovementMode::DIRECTIONAL_SPAM_A: + break; + default: + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Unknown navigation movement mode."); + } + context.wait_for_all_requests(); + + FlatWhiteDialogWatcher white_dialog(COLOR_WHITE, &console.overlay()); + BlueDialogWatcher blue_dialog(COLOR_BLUE, &console.overlay()); + ItemReceiveWatcher item_receive(COLOR_BLUE, &console.overlay()); + TealDialogWatcher teal_dialog(COLOR_CYAN, &console.overlay()); + LightBlueDialogWatcher light_blue_dialog(COLOR_BLUE, &console.overlay()); + TransparentBattleDialogWatcher battle_dialog(COLOR_YELLOW, &console.overlay()); + ButtonWatcher button(COLOR_RED, button_type, button_box, &console.overlay(), 200ms); + DoorEntryWatcher door_entry(COLOR_RED, &console.overlay()); + auto battle_watchers = make_trainer_battle_watchers(100ms); + BlackScreenOverWatcher black_screen_over(COLOR_BLUE); + std::vector callbacks; + const char* stop_description = nullptr; + switch (stop_condition){ + case NavigationStopCondition::STOP_DIALOG: + callbacks = { + white_dialog, + blue_dialog, + item_receive, + teal_dialog, + light_blue_dialog, + battle_dialog, + }; + stop_description = "dialog"; + break; + case NavigationStopCondition::STOP_BUTTON: + callbacks.emplace_back(button); + stop_description = "requested button"; + break; + case NavigationStopCondition::STOP_DOOR_ENTRY: + callbacks.emplace_back(door_entry); + stop_description = "door-entry icon"; + break; + case NavigationStopCondition::STOP_TIME: + stop_description = "elapsed time"; + break; + case NavigationStopCondition::STOP_BATTLE: + for (auto& watcher : battle_watchers){ + callbacks.emplace_back(watcher); + } + stop_description = "opponent party indicators"; + break; + case NavigationStopCondition::STOP_BLACK_SCREEN: + callbacks.emplace_back(black_screen_over); + stop_description = "end of black-screen transition"; + break; + default: + throw InternalProgramError(&console.logger(), PA_CURRENT_FUNCTION, "Unknown navigation stop condition."); + } + + int ret = run_until( + console, context, + [=](ProControllerContext& context){ + if (sprint){ + ssf_press_button(context, BUTTON_B, 0ms, timeout, 0ms); + } + ssf_press_left_joystick(context, {direction_x, direction_y}, 0ms, timeout, 0ms); + if (movement_mode == NavigationMovementMode::DIRECTIONAL_ONLY){ + pbf_wait(context, timeout); + }else{ + for (Milliseconds remaining = timeout; remaining > 0ms;){ + context.throw_if_cancelled(); + const Milliseconds hold = std::min(160ms, remaining); + const Milliseconds release = std::min(40ms, remaining - hold); + pbf_press_button(context, BUTTON_A, hold, release); + remaining -= hold + release; + } + } + }, + callbacks + ); + if (ret < 0 && stop_condition != NavigationStopCondition::STOP_TIME){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + std::string("overworld_navigation(): Did not detect ") + stop_description + " within " + + std::to_string(timeout.count()) + " milliseconds.", + console + ); + } + + context.wait_for_all_requests(); + if (stop_condition == NavigationStopCondition::STOP_TIME){ + console.log("overworld_navigation(): Finished timed movement."); + }else{ + console.log(std::string("overworld_navigation(): Detected ") + stop_description + "."); + } +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h new file mode 100644 index 0000000000..b72d2d99c5 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h @@ -0,0 +1,232 @@ +/* Auto Story Tools + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryTools_H +#define PokemonAutomation_PokemonLZA_AutoStoryTools_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ProgramStats/StatsTracking.h" +#include "CommonTools/VisualDetectors/ImageMatchDetector.h" +#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class BattleAIOption; + +enum class StarterChoice{ + CHIKORITA, + TOTODILE, + TEPIG, +}; + +struct AutoStoryOptions{ + StarterChoice starter_choice; + BattleAIOption& battle_ai; +}; + +struct AutoStoryStats : public StatsTracker{ + AutoStoryStats() + : m_segment(m_stats["Segment"]) + , m_checkpoint(m_stats["Checkpoint"]) + { + m_display_order.emplace_back("Segment"); + m_display_order.emplace_back("Checkpoint"); + } + + std::atomic& m_segment; + std::atomic& m_checkpoint; +}; + +class AutoStory_Segment{ +public: + virtual ~AutoStory_Segment() = default; + virtual std::string name() const = 0; + virtual std::string start_text() const = 0; + virtual std::string end_text() const = 0; + virtual void run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const = 0; +}; + + +class AutoStory_Checkpoint{ +public: + virtual ~AutoStory_Checkpoint() = default; + virtual std::string name() const = 0; + virtual std::string start_text() const = 0; + virtual std::string end_text() const = 0; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const = 0; +}; + +// STOP_BLACK_SCREEN waits for a black screen to appear and then finish +// STOP_WHITE_SCREEN stops when a white screen appears, before it finishes +// STOP_BATTLE waits for the opponent-party icons to appear in a trainer battle +// STOP_CHOICES stops at a right-pointing choice arrow without confirming it +// OverworldPartySelectionWatcher detects the overworld +enum class ClearDialogMode{ + STOP_OVERWORLD, + STOP_BLACK_SCREEN, + STOP_BATTLE, + STOP_CHOICES, + STOP_WHITE_SCREEN, +}; + +// Mash A (or B) until the selected stop condition and throws on timeout +// STOP_CHOICES waits 1500 ms before the first press, then uses 100 ms presses with 1500 ms releases to wait on choice arrows +// All modes except STOP_CHOICES confirm highlighted choices with A, even when mashing B through other dialogue +// All modes advance tutorial windows with A, with or without page arrows +void clear_dialog( + ConsoleHandle& console, ProControllerContext& context, + ClearDialogMode mode = ClearDialogMode::STOP_OVERWORLD, + Button button = BUTTON_A, + Milliseconds timeout = std::chrono::minutes(4) +); + +// Hold the requested button continuously until any supported dialogue appears +// Releases the button when detection stops the hold, throws on timeout +void hold_button_until_dialog( + ConsoleHandle& console, ProControllerContext& context, Button button, + Milliseconds timeout = std::chrono::minutes(2) +); + +// Repeat the requested press/release cadence until any supported dialog appear +void press_button_until_dialog( + ConsoleHandle& console, ProControllerContext& context, Button button, + Milliseconds hold = std::chrono::milliseconds(100), + Milliseconds release = std::chrono::milliseconds(100), + Milliseconds timeout = std::chrono::minutes(2) +); + +// Mash the requested button until the requested on-screen icon appears, throws on timeout. +void mash_button_until_button( + ConsoleHandle& console, ProControllerContext& context, + Button mash_button, ButtonType button_type, const ImageFloatBox& button_box, + Milliseconds timeout = std::chrono::minutes(2) +); + +// Mash the requested button until a doorway/entry-arrow icon is detected, throws on timeout. +void mash_button_until_door_entry( + ConsoleHandle& console, ProControllerContext& context, + Button mash_button = BUTTON_A, + Milliseconds timeout = std::chrono::minutes(2) +); + +// Match filled opponent-party icons at the six top HUD slots +// Any slot can signal a trainer battle independent of whether we can run from the battle +std::array make_trainer_battle_watchers(Milliseconds hold_duration); + +// Tutorial pagers use the right-button icon in either light or dark colors +// Search across the lower half of the screen to cover different page lengths +class TutorialPageWatcher : public DetectorToFinder{ +public: + TutorialPageWatcher( + Color color, VideoOverlay* overlay = nullptr, + Milliseconds hold_duration = std::chrono::milliseconds(100) + ); + + virtual bool detect(const ImageViewRGB32& screen) override; +}; + +enum class BattleStopCondition{ + STOP_OVERWORLD, + STOP_BLACK_SCREEN, +}; + +// Advance dialogue with A and use the existing battle AI until the selected condition +// STOP_BLACK_SCREEN stops when black appears, before it finishes +// Advance tutorial windows with A, including pages without navigation arrows +// Close normal/tutorial maps with B when detected +// Confirm highlighted dialogue options with A when their selection arrow appears +// Keep one battle state throughout the fight, including lock recovery +// Throws if the entire sequence exceeds timeout +void run_battle_until_condition( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai, BattleStopCondition stop_condition, + Milliseconds timeout = std::chrono::minutes(5) +); + +enum class WildBattleStopCondition{ + STOP_DIALOG, +}; + +// Use the configured battle AI with a Pokemon already out until the selected condition, throws on timeout +// STOP_DIALOG requires white dialogue for 1.5 continuous seconds to prevent matching with the background or moving Pokemon +void run_wild_battle_until_condition( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai, WildBattleStopCondition stop_condition, + Milliseconds timeout = std::chrono::minutes(1) +); + +// Turn the camera in short increments until facing slightly left of the news booth +// Requires the booth to be visible; throws on lost detection or alignment timeout +void align_camera_left_of_booth(ConsoleHandle& console, ProControllerContext& context); + +// Search for the event marker, with or without its green flag, and turn the camera with corrections until centered for 400 ms +// Recover a temporarily clipped marker in its last known direction (up to 4 turns) +// Detection excludes the minimap, throws on failed recovery or a 30-second timeout +void center_event_marker(ConsoleHandle& console, ProControllerContext& context); + +// Cycle through the overworld party with the existing selection watcher +// Returns whether slot count (1-6) is selectable, restoring the starting selection +// Throws if party selection cannot be read +bool party_has_at_least(ConsoleHandle& console, ProControllerContext& context, uint8_t count); + +// Throw the currently selected ball using Stats Reset's ZL + ZR sequence +void run_catch( + ProControllerContext& context, + Milliseconds post_throw_wait = std::chrono::seconds(6) +); + +enum class NavigationMovementMode{ + DIRECTIONAL_ONLY, + DIRECTIONAL_SPAM_A, +}; + +enum class NavigationStopCondition{ + STOP_DIALOG, + STOP_BUTTON, + STOP_DOOR_ENTRY, + STOP_TIME, + STOP_BATTLE, + STOP_BLACK_SCREEN, +}; + +// Move in the requested direction, optionally pressing A with 160 ms hold and 40 ms release +// All inputs stop when the selected condition is met +// STOP_BUTTON uses button_type and button_box +// STOP_DOOR_ENTRY matches the doorway/entry-arrow icon in the central screen area +// STOP_BATTLE matches the opponent-party icons at the top of the screen +// STOP_BLACK_SCREEN waits for a black screen to appear +// STOP_TIME completes normally after timeout +void overworld_navigation( + ConsoleHandle& console, ProControllerContext& context, + NavigationStopCondition stop_condition, NavigationMovementMode movement_mode, + double direction_x, double direction_y, + Milliseconds timeout = std::chrono::seconds(60), + bool sprint = false, + ButtonType button_type = ButtonType::ButtonA, + const ImageFloatBox& button_box = {0.3, 0.2, 0.4, 0.7} +); + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp new file mode 100644 index 0000000000..d6a7417cab --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp @@ -0,0 +1,594 @@ +/* Auto Story Segment 01 + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "PokemonLZA_AutoStory_Segment_01.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +// Opening sequences +void checkpoint_00(SingleSwitchProgramEnvironment& env, ProControllerContext& context){ + // Move to the Start Game button + context.wait_for_all_requests(); + SelectionArrowWatcher begin_arrow( + COLOR_GREEN, &env.console.overlay(), SelectionArrowType::RIGHT, + {0.05, 0.73, 0.045, 0.12}, 100ms + ); + const WallClock deadline = current_time() + 30s; + + while (wait_until(env.console, context, std::min(deadline, current_time() + 300ms), {begin_arrow}) < 0){ + if (current_time() >= deadline){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Unable to select Begin on the profile confirmation screen within 30 seconds.", + env.console + ); + } + pbf_press_dpad(context, DPAD_DOWN, 100ms, 300ms); + context.wait_for_all_requests(); + } + + // Opening cutscene + pbf_press_button(context, BUTTON_A, 100ms, 15000ms); + press_button_until_dialog(env.console, context, BUTTON_PLUS, 2000ms, 100ms, 60s); + pbf_press_button(context, BUTTON_A, 100ms, 100ms); + + // Wait out the train as it can false positive on the button detector + pbf_wait(context, 13000ms); + mash_button_until_button( + env.console, context, BUTTON_B, ButtonType::RightStickUpDown, + {0.35, 0.70, 0.30, 0.30}, 60s + ); + + // Change text speed to fast (consider turn off rumble too if it causes issues in bluetooth traffic) + pbf_press_button(context, BUTTON_X, 100ms, 2600ms); + pbf_press_button(context, BUTTON_A, 100ms, 600ms); + pbf_press_button(context, BUTTON_L, 100ms, 600ms); + pbf_press_dpad(context, DPAD_UP, 100ms, 300ms); + pbf_press_button(context, BUTTON_A, 100ms, 1100ms); + pbf_press_dpad(context, DPAD_DOWN, 100ms, 300ms); + pbf_press_dpad(context, DPAD_RIGHT, 100ms, 300ms); + mash_button_until_button( + env.console, context, BUTTON_B, ButtonType::RightStickUpDown, + {0.35, 0.70, 0.30, 0.30}, 60s + ); + + // Exit station and mash through the NPC introduction cutscene + pbf_move_left_joystick(context, {0.25, 1}, 4000ms, 100ms); + pbf_move_left_joystick(context, {-0.8, 0.7}, 500ms, 100ms); + pbf_move_left_joystick(context, {0.95, 0.35}, 5000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0.95, -0.4, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_BLACK_SCREEN); + press_button_until_dialog(env.console, context, BUTTON_PLUS, 2000ms, 100ms, 60s); + clear_dialog(env.console, context, ClearDialogMode::STOP_BLACK_SCREEN); + pbf_wait(context, 2000ms); +} + +// Starter selection and battle tutorial +void checkpoint_01( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai, const JoystickPosition& starter_direction +){ + // Chase Pancham + pbf_move_left_joystick(context, {-0.3, 1}, 5500ms, 100ms); + pbf_move_left_joystick(context, {-0.95, -0.15}, 4000ms, 100ms); + pbf_move_left_joystick(context, {0.07, 1}, 6000ms, 100ms); + // Small pause to avoid colliding with the starters + pbf_wait(context, 800ms); + pbf_move_left_joystick(context, {0.07, 1}, 4000ms, 100ms); + pbf_move_left_joystick(context, {-1, 0.2}, 2000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + -0.9, 0.5, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_BLACK_SCREEN); + pbf_wait(context, 2000ms); + + // Starter selection and tutorial battle + pbf_move_left_joystick(context, starter_direction, 500ms, 0ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BLACK_SCREEN, NavigationMovementMode::DIRECTIONAL_SPAM_A, + starter_direction.x, starter_direction.y, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_BATTLE); + run_battle_until_condition(env, context, battle_ai, BattleStopCondition::STOP_BLACK_SCREEN); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); +} + +// First battle zone and Hotel Z introduction +void checkpoint_02( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai +){ + // NPC battle + pbf_move_left_joystick(context, {0, 1}, 5000ms, 100ms); + pbf_move_left_joystick(context, {1, 0.2}, 4000ms, 100ms); + pbf_move_left_joystick(context, {0.6, -0.8}, 800ms, 100ms); + pbf_move_left_joystick(context, {1, 0}, 3000ms, 100ms); + pbf_move_left_joystick(context, {1, 0.25}, 2000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_SPAM_A, + 0, -1, 60s + ); + run_battle_until_condition(env, context, battle_ai, BattleStopCondition::STOP_OVERWORLD); + + // Floette cutscene and exiting the battle zone + pbf_move_left_joystick(context, {-0.4, 0.95}, 3000ms, 100ms); + pbf_move_left_joystick(context, {-1, -0.15}, 4500ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BLACK_SCREEN, NavigationMovementMode::DIRECTIONAL_ONLY, + 0.5, 0.85, 60s + ); + press_button_until_dialog(env.console, context, BUTTON_PLUS, 2000ms, 100ms, 60s); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, +1, 30s, true, ButtonType::ButtonA + ); + pbf_press_button(context, BUTTON_A, 100ms, 500ms); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Head to Hotel Z + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BLACK_SCREEN, NavigationMovementMode::DIRECTIONAL_ONLY, + +1, 0, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + pbf_move_left_joystick(context, {0, 1}, 4000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, +1, 30s, true, ButtonType::ButtonA + ); + pbf_press_button(context, BUTTON_A, 100ms, 500ms); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Enter hotel room + pbf_move_left_joystick(context, {-0.13, 1}, 4000ms, 100ms); + pbf_move_left_joystick(context, {-1, -0.1}, 300ms, 100ms); + mash_button_until_door_entry(env.console, context); + pbf_move_left_joystick(context, {0, 1}, 1800ms, 100ms); + pbf_mash_button(context, BUTTON_A, 1s); + mash_button_until_door_entry(env.console, context); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + -0.2, 0.95, 60s, false, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 1s); + clear_dialog(env.console, context, ClearDialogMode::STOP_BLACK_SCREEN); + pbf_wait(context, 2000ms); + + // Approach the bed, then leave for the rooftop conversation + pbf_move_left_joystick(context, {0.8, 0.8}, 8000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, false, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 2s); + mash_button_until_door_entry(env.console, context); + pbf_move_left_joystick(context, {0, 1}, 800ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + -0.2, 0.8, 60s, false, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 2s); + mash_button_until_door_entry(env.console, context); +} + +// First rival battle and Pokemon Center introduction +void checkpoint_03( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai +){ + // Z-A Royale introduction + pbf_move_left_joystick(context, {0, 1}, 800ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + -0.8, 0.8, 60s, false, ButtonType::ButtonA + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_BLACK_SCREEN); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + -0.3, 0.95, 60s, true, ButtonType::ButtonA + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD, BUTTON_A, 6min); + + // Rival battle + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true, ButtonType::ButtonA + ); + pbf_press_button(context, BUTTON_A, 100ms, 500ms); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, false, ButtonType::ButtonA + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_BATTLE); + run_battle_until_condition(env, context, battle_ai, BattleStopCondition::STOP_OVERWORLD); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Climb the ladder and finish the movement tutorial + pbf_move_left_joystick(context, {0.3, 1}, 9000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + -1, 1, 60s, false, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 1s); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Move to the Pokemon Center and finish the tutorial + pbf_move_left_joystick(context, {0, 1}, 8000ms, 100ms); + pbf_move_left_joystick(context, {1, 0}, 3000ms, 100ms); + pbf_move_left_joystick(context, {0, 1}, 3400ms, 100ms); + pbf_move_left_joystick(context, {-1, 0}, 6000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, false, ButtonType::ButtonA + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_WHITE_SCREEN); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD, BUTTON_B); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); +} + +// Catching tutorials and Mable's Research introduction +void checkpoint_04( + SingleSwitchProgramEnvironment& env, ProControllerContext& context, + BattleAIOption& battle_ai +){ + // Enter Wild Zone 1 + pbf_move_left_joystick(context, {0, 1}, 4000ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 2s); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // First Fletchling tutorial, always fails + run_catch(context); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Second Fletchling tutorial, always succeeds + run_catch(context); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Bunnelby tutorial, faint first then catch + pbf_press_button(context, BUTTON_UP, 500ms, 2s); + context.wait_for_all_requests(); + pbf_wait(context, 1000ms); + run_wild_battle_until_condition(env, context, battle_ai, WildBattleStopCondition::STOP_DIALOG); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + run_catch(context); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Catch Scatterbug + pbf_move_left_joystick(context, {-1, 0}, 1000ms, 100ms); + pbf_move_left_joystick(context, {0, 1}, 5500ms, 100ms); + ssf_press_right_joystick(context, {1, 0}, 0ms, 300ms, 0ms); + pbf_move_left_joystick(context, {0, 1}, 300ms, 100ms); + pbf_move_left_joystick(context, {0, 1}, 400ms, 100ms); + run_catch(context, 9000ms); + + // Use the news booth as a camera landmark, then pass it on the left toward Weedle + pbf_move_left_joystick(context, {0, 1}, 500ms, 100ms); + align_camera_left_of_booth(env.console, context); + pbf_move_left_joystick(context, {0, 1}, 5000ms, 100ms); + + // Attempt 5 Weedle catches, rotating camera angle and checking party count + bool caught = false; + for (size_t attempt = 0; attempt < 5; attempt++){ + context.throw_if_cancelled(); + env.log("Weedle catch attempt " + std::to_string(attempt + 1) + " of 5."); + if (attempt > 0){ + // Each retry adds another small rightward camera turn, without moving. + pbf_move_right_joystick(context, {0.35, 0}, 1000ms, 200ms); + } + run_catch(context); + if (party_has_at_least(env.console, context, 5)){ + caught = true; + break; + } + } + if (!caught){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Fewer than five party Pokemon after five Weedle catch attempts.", + env.console + ); + } + + // Adjust direction based on the event marker and exit Wild Zone 1 + center_event_marker(env.console, context); + pbf_move_left_joystick(context, {0.2, 1}, 3000ms, 100ms); + center_event_marker(env.console, context); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 2s); + + // Adjust direction based on the event marker and enter research lab + center_event_marker(env.console, context); + pbf_move_left_joystick(context, {0.4, 1}, 6000ms, 100ms); + center_event_marker(env.console, context); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, true, ButtonType::ButtonA + ); + pbf_mash_button(context, BUTTON_A, 2s); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Enter elevator and go to the 3rd floor + center_event_marker(env.console, context); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_BUTTON, NavigationMovementMode::DIRECTIONAL_ONLY, + 0, 1, 60s, false, ButtonType::ButtonA + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_CHOICES); + pbf_press_dpad(context, DPAD_DOWN, 100ms, 300ms); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD); + + // Marble's Research introduction + pbf_move_left_joystick(context, {-0.6, 0.9}, 1500ms, 100ms); + overworld_navigation( + env.console, context, + NavigationStopCondition::STOP_DIALOG, NavigationMovementMode::DIRECTIONAL_ONLY, + 1, -0.2, 60s + ); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD, BUTTON_B); + + press_button_until_dialog(env.console, context, BUTTON_X); + clear_dialog(env.console, context, ClearDialogMode::STOP_OVERWORLD, BUTTON_B); +} + +std::string AutoStory_Segment_01::name() const{ + return "01: Tutorial (Until unlocking Mystery Gift)"; +} + +std::string AutoStory_Segment_01::start_text() const{ + return "Start: Profile confirmation screen after choosing the game language, player appearance, and player name"; +} + +std::string AutoStory_Segment_01::end_text() const{ + return "End: Introduction to Mable's Research and unlocking Mystery Gift etc."; +} + +void AutoStory_Segment_01::run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + env.update_stats(); + context.wait_for_all_requests(); + env.console.log("Start Segment " + name(), COLOR_ORANGE); + env.log(start_text()); + + AutoStory_Checkpoint_00().run_checkpoint(env, context, options, stats); + AutoStory_Checkpoint_01().run_checkpoint(env, context, options, stats); + AutoStory_Checkpoint_02().run_checkpoint(env, context, options, stats); + AutoStory_Checkpoint_03().run_checkpoint(env, context, options, stats); + AutoStory_Checkpoint_04().run_checkpoint(env, context, options, stats); + + env.log(end_text()); + env.console.log("End Segment " + name(), COLOR_GREEN); +} + +std::string AutoStory_Checkpoint_00::name() const{ + return "00: Opening sequences"; +} + +std::string AutoStory_Checkpoint_00::start_text() const{ + return "Profile confirmation screen after choosing the language, appearance, and name"; +} + +std::string AutoStory_Checkpoint_00::end_text() const{ + return "Outside the station before chasing Pancham"; +} + +void AutoStory_Checkpoint_00::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + stats.m_checkpoint = 0; + env.update_stats(); + context.wait_for_all_requests(); + env.log("Checkpoint " + name()); + env.log("Start: " + start_text()); + checkpoint_00(env, context); + context.wait_for_all_requests(); +} + +std::string AutoStory_Checkpoint_01::name() const{ + return "01: Starter selection and battle tutorial"; +} + +std::string AutoStory_Checkpoint_01::start_text() const{ + return "Outside the station before chasing Pancham"; +} + +std::string AutoStory_Checkpoint_01::end_text() const{ + return "Finished first battle tutorial, battle zone started"; +} + +void AutoStory_Checkpoint_01::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + stats.m_checkpoint = 1; + env.update_stats(); + context.wait_for_all_requests(); + env.log("Checkpoint " + name()); + env.log("Start: " + start_text()); + JoystickPosition starter_direction; + switch (options.starter_choice){ + case StarterChoice::CHIKORITA: + starter_direction = {-0.9, +0.4}; + break; + case StarterChoice::TOTODILE: + starter_direction = {-0.9, +0.75}; + break; + case StarterChoice::TEPIG: + starter_direction = {-0.6, +0.9}; + break; + } + checkpoint_01(env, context, options.battle_ai, starter_direction); + context.wait_for_all_requests(); +} + +std::string AutoStory_Checkpoint_02::name() const{ + return "02: First battle zone and Hotel Z introduction"; +} + +std::string AutoStory_Checkpoint_02::start_text() const{ + return "Finished first battle tutorial, battle zone started"; +} + +std::string AutoStory_Checkpoint_02::end_text() const{ + return "Hotel Z night sequence finished, next morning"; +} + +void AutoStory_Checkpoint_02::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + stats.m_checkpoint = 2; + env.update_stats(); + context.wait_for_all_requests(); + env.log("Checkpoint " + name()); + env.log("Start: " + start_text()); + checkpoint_02(env, context, options.battle_ai); + context.wait_for_all_requests(); +} + +std::string AutoStory_Checkpoint_03::name() const{ + return "03: First rival battle and Pokemon Center introduction"; +} + +std::string AutoStory_Checkpoint_03::start_text() const{ + return "Hotel Z night sequence finished, next morning"; +} + +std::string AutoStory_Checkpoint_03::end_text() const{ + return "Pokemon Center introduction completed, before entering Wild Zone 1"; +} + +void AutoStory_Checkpoint_03::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + stats.m_checkpoint = 3; + env.update_stats(); + context.wait_for_all_requests(); + env.log("Checkpoint " + name()); + env.log("Start: " + start_text()); + checkpoint_03(env, context, options.battle_ai); + context.wait_for_all_requests(); +} + +std::string AutoStory_Checkpoint_04::name() const{ + return "04: Catching tutorials and Mable's Research introduction"; +} + +std::string AutoStory_Checkpoint_04::start_text() const{ + return "Pokemon Center introduction completed, before entering Wild Zone 1"; +} + +std::string AutoStory_Checkpoint_04::end_text() const{ + return "Mable's Research introduction completed, inside research lab"; +} + +void AutoStory_Checkpoint_04::run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats +) const{ + stats.m_segment = 1; + stats.m_checkpoint = 4; + env.update_stats(); + context.wait_for_all_requests(); + env.log("Checkpoint " + name()); + env.log("Start: " + start_text()); + checkpoint_04(env, context, options.battle_ai); + context.wait_for_all_requests(); +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h new file mode 100644 index 0000000000..76934c9643 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h @@ -0,0 +1,97 @@ +/* Auto Story Segment 01 + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H +#define PokemonAutomation_PokemonLZA_AutoStory_Segment_01_H + +#include "PokemonLZA_AutoStoryTools.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class AutoStory_Segment_01 : public AutoStory_Segment{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_segment( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +class AutoStory_Checkpoint_00 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +class AutoStory_Checkpoint_01 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +class AutoStory_Checkpoint_02 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +class AutoStory_Checkpoint_03 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +class AutoStory_Checkpoint_04 : public AutoStory_Checkpoint{ +public: + virtual std::string name() const override; + virtual std::string start_text() const override; + virtual std::string end_text() const override; + virtual void run_checkpoint( + SingleSwitchProgramEnvironment& env, + ProControllerContext& context, + AutoStoryOptions options, + AutoStoryStats& stats + ) const override; +}; + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.cpp new file mode 100644 index 0000000000..b11fd0422a --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.cpp @@ -0,0 +1,765 @@ +/* Auto Story Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Options/BatchOption.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Tests/ImageUnitTest.h" +#include "Tests/TestUtils.h" +#include "VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h" +#include "VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h" +#include "PokemonLZA_AutoStory.h" +#include "PokemonLZA_AutoStoryTools.h" +#include "PokemonLZA_AutoStory_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +class Test_AutoStoryOptions : public UnitTest{ +public: + Test_AutoStoryOptions() + : UnitTest("PokemonLZA::AutoStoryOptions") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + AutoStory program; + StringSelectOption* segment = nullptr; + StringSelectOption* start = nullptr; + StringSelectOption* end = nullptr; + BooleanCheckBoxOption* advanced = nullptr; + IntegerEnumDropdownOption* section = nullptr; + StaticTextOption* start_description = nullptr; + StaticTextOption* end_description = nullptr; + for (ConfigOption* option : dynamic_cast(program.options()).options()){ + if (auto* select = dynamic_cast(option)){ + if (select->label() == "Start Segment:"){ + segment = select; + }else if (select->label() == "Start Checkpoint:"){ + start = select; + }else if (select->label() == "End Checkpoint:"){ + end = select; + } + }else if (auto* toggle = dynamic_cast(option)){ + if (toggle->label().find("Advanced mode:") == 0){ + advanced = toggle; + } + }else if (auto* dropdown = dynamic_cast(option)){ + if (dropdown->label() == "Story Section:"){ + section = dropdown; + } + }else if (auto* text = dynamic_cast(option)){ + if (text->text().find("Start:") == 0){ + start_description = text; + }else if (text->text().find("End:") == 0){ + end_description = text; + } + } + } + if (!segment || !start || !end || !advanced || !section || !start_description || !end_description){ + return "Missing story-selection controls or descriptions."; + } + TEST_RESULT_COMPONENT_EQUAL_STR(section->database().all_values().size(), size_t(1), "one story section"); + TEST_RESULT_COMPONENT_EQUAL_STR(segment->database().size(), size_t(1), "one starting segment"); + TEST_RESULT_COMPONENT_EQUAL_STR(start->database().size(), size_t(5), "five starting checkpoints"); + TEST_RESULT_COMPONENT_EQUAL_STR(end->database().size(), size_t(5), "five ending checkpoints"); + TEST_RESULT_COMPONENT_EQUAL_STR(start->slug(), std::string("0"), "default start checkpoint"); + TEST_RESULT_COMPONENT_EQUAL_STR(end->slug(), std::string("4"), "default end checkpoint"); + if (segment->visibility() != ConfigOptionState::ENABLED || + start->visibility() != ConfigOptionState::HIDDEN || end->visibility() != ConfigOptionState::HIDDEN){ + return "Normal mode should display only the segment selector."; + } + if (!program.check_validity().empty()){ + return "Default selection should be valid."; + } + *advanced = true; + if (segment->visibility() != ConfigOptionState::HIDDEN || + start->visibility() != ConfigOptionState::ENABLED || end->visibility() != ConfigOptionState::ENABLED){ + return "Advanced mode should display the checkpoint selectors."; + } + start->set_by_slug("3"); + end->set_by_slug("3"); + TEST_RESULT_COMPONENT_EQUAL_STR(start_description->text(), + "Start: " + ALL_AUTO_STORY_CHECKPOINT_LIST()[3]->start_text(), "selected checkpoint start description"); + TEST_RESULT_COMPONENT_EQUAL_STR(end_description->text(), + "End: " + ALL_AUTO_STORY_CHECKPOINT_LIST()[3]->end_text(), "selected checkpoint end description"); + if (!program.check_validity().empty()){ + return "A single-checkpoint range should be valid."; + } + const JsonValue saved = program.to_json(); + AutoStory restored; + restored.load_json(saved); + const JsonValue restored_settings = restored.to_json(); + const JsonObject& json = restored_settings.to_object_throw(); + TEST_RESULT_COMPONENT_EQUAL_STR(json.get_boolean_throw("ENABLE_ADVANCED_MODE"), true, "persist advanced mode"); + TEST_RESULT_COMPONENT_EQUAL_STR(json.get_string_throw("STORY_SECTION"), std::string("tutorial"), "persist section"); + TEST_RESULT_COMPONENT_EQUAL_STR(json.get_string_throw("START_SEGMENT"), std::string("0"), "persist segment"); + TEST_RESULT_COMPONENT_EQUAL_STR(json.get_string_throw("START_CHECKPOINT"), std::string("3"), "persist starting checkpoint"); + TEST_RESULT_COMPONENT_EQUAL_STR(json.get_string_throw("END_CHECKPOINT"), std::string("3"), "persist ending checkpoint"); + end->set_by_slug("2"); + if (program.check_validity().empty()){ + return "A reversed checkpoint range must be rejected."; + } + *advanced = false; + if (!program.check_validity().empty()){ + return "Hidden checkpoint selections must not affect normal mode."; + } + TEST_RESULT_COMPONENT_EQUAL_STR(start_description->text(), ALL_AUTO_STORY_SEGMENT_LIST()[0]->start_text(), + "normal mode restores the segment description"); + return true; + } +}; + +class Test_AutoStoryEventMarker : public ImageUnitTest{ +public: + Test_AutoStoryEventMarker(const std::string& image, bool expected, double expected_center = 0) + : ImageUnitTest("PokemonLZA::AutoStoryEventMarker - " + image, {image}) + , m_expected(expected) + , m_expected_center(expected_center) + { + m_memory += 5 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + const auto fill_box = [](ImageRGB32& image, const ImageFloatBox& box, uint32_t color){ + const ImagePixelBox pixels = floatbox_to_pixelbox(image, box); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + image.pixel(x, y) = color; + } + } + }; + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + EventMarkerWatcher watcher; + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "event marker at height " + std::to_string(height)); + if (!m_expected){ + continue; + } + const ImageFloatBox box = watcher.marker_box(); + const double center = box.x + box.width / 2; + const bool correct_location = center > m_expected_center - 0.01 && center < m_expected_center + 0.01; + TEST_RESULT_COMPONENT_EQUAL_STR(correct_location, true, "world marker coordinates"); + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(200)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial event marker frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained event marker"); + + ImageRGB32 missing_symbol = scaled.copy(); + fill_box(missing_symbol, {box.x + box.width * 0.3, box.y + box.height * 0.15, + box.width * 0.4, box.height * 0.73}, 0xffffc800); + const bool without_symbol = watcher.detect(missing_symbol); + TEST_RESULT_COMPONENT_EQUAL_STR(without_symbol, false, "yellow badge without exclamation mark"); + + const ImageFloatBox full_marker{box.x - 0.008, box.y - 0.015, box.width + 0.025, box.height + 0.025}; + ImageRGB32 marker = extract_box_reference(scaled, full_marker).copy(); + ImageRGB32 removed = scaled.copy(); + fill_box(removed, full_marker, 0xff283850); + const bool only_minimap = watcher.detect(removed); + TEST_RESULT_COMPONENT_EQUAL_STR(only_minimap, false, "world marker removed with minimap unchanged"); + watcher.reset_state(); + watcher.process_frame(scaled, start); + watcher.process_frame(removed, start + Milliseconds(100)); + const bool interrupted = watcher.process_frame(scaled, start + Milliseconds(200)); + TEST_RESULT_COMPONENT_EQUAL_STR(interrupted, false, "interrupted marker detection"); + + // Reposition the same marker to prove coordinates follow the icon + const size_t target_x = scaled.width() * 3 / 4; + const size_t target_y = scaled.height() / 2; + for (size_t y = 0; y < marker.height(); y++){ + for (size_t x = 0; x < marker.width(); x++){ + removed.pixel(target_x + x, target_y + y) = marker.pixel(x, y); + } + } + const bool relocated = watcher.detect(removed); + TEST_RESULT_COMPONENT_EQUAL_STR(relocated, true, "relocated event marker"); + const bool moved_right = watcher.marker_box().x > 0.74; + TEST_RESULT_COMPONENT_EQUAL_STR(moved_right, true, "relocated marker coordinates"); + } + return true; + } + +private: + bool m_expected; + double m_expected_center; +}; + +class Test_AutoStoryEventMarkerTransition : public ImageUnitTest{ +public: + Test_AutoStoryEventMarkerTransition( + const std::string& flagged_image = "PokemonLZA/AutoStory/wild_zone_1_no_booth.png" + ) + : ImageUnitTest("PokemonLZA::AutoStoryEventMarkerTransition - " + flagged_image, { + "PokemonLZA/AutoStory/wild_zone_1_event_no_flag.png", + "PokemonLZA/AutoStory/wild_zone_1_event_clipped.png", + flagged_image, + }) + { + m_memory += 4 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 flagless = images[0].scale_to(height * 16 / 9, height); + ImageRGB32 clipped = images[1].scale_to(height * 16 / 9, height); + ImageRGB32 flagged = images[2].scale_to(height * 16 / 9, height); + const WallClock start = current_time(); + EventMarkerWatcher watcher; + const bool first = watcher.process_frame(flagless, start); + const bool initial = watcher.process_frame(flagless, start + Milliseconds(200)); + const bool lost = watcher.process_frame(clipped, start + Milliseconds(250)); + const bool reacquiring = watcher.process_frame(flagged, start + Milliseconds(300)); + const bool recovered = watcher.process_frame(flagged, start + Milliseconds(500)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial flagless frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(initial, true, "flagless marker acquired"); + TEST_RESULT_COMPONENT_EQUAL_STR(lost, false, "clipped transition frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(reacquiring, false, "reacquisition hold restarts"); + TEST_RESULT_COMPONENT_EQUAL_STR(recovered, true, "flagged marker recovered"); + + const ImageFloatBox box = watcher.marker_box(); + const ImageFloatBox full_marker{box.x - 0.008, box.y - 0.015, box.width + 0.025, box.height + 0.025}; + const ImagePixelBox pixels = floatbox_to_pixelbox(flagged, full_marker); + ImageRGB32 icon = extract_box_reference(flagged, pixels).copy(); + ImageRGB32 centered = flagged.copy(); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + centered.pixel(x, y) = 0xff283850; + } + } + const ptrdiff_t shift = std::lround((0.5 - box.x - box.width / 2) * flagged.width()); + const size_t target_x = size_t(ptrdiff_t(pixels.min_x) + shift); + for (size_t y = 0; y < icon.height(); y++){ + for (size_t x = 0; x < icon.width(); x++){ + centered.pixel(target_x + x, pixels.min_y + y) = icon.pixel(x, y); + } + } + CenteredEventMarkerWatcher centered_watcher; + const bool off_center = centered_watcher.process_frame(flagged, start); + const bool still_off_center = centered_watcher.process_frame(flagged, start + Milliseconds(500)); + TEST_RESULT_COMPONENT_EQUAL_STR(off_center, false, "off-center flagged marker"); + TEST_RESULT_COMPONENT_EQUAL_STR(still_off_center, false, "off-center presence is insufficient"); + const bool centering = centered_watcher.process_frame(centered, start + Milliseconds(600)); + const bool too_brief = centered_watcher.process_frame(centered, start + Milliseconds(800)); + TEST_RESULT_COMPONENT_EQUAL_STR(centering, false, "first centered frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(too_brief, false, "brief alignment must not start walking"); + const bool interrupted = centered_watcher.process_frame(clipped, start + Milliseconds(900)); + TEST_RESULT_COMPONENT_EQUAL_STR(interrupted, false, "graphic transition interrupts alignment"); + const bool restarting = centered_watcher.process_frame(centered, start + Milliseconds(1000)); + const bool not_yet = centered_watcher.process_frame(centered, start + Milliseconds(1399)); + const bool stable = centered_watcher.process_frame(centered, start + Milliseconds(1400)); + TEST_RESULT_COMPONENT_EQUAL_STR(restarting, false, "alignment hold restarts after loss"); + TEST_RESULT_COMPONENT_EQUAL_STR(not_yet, false, "alignment held for less than 400 ms"); + TEST_RESULT_COMPONENT_EQUAL_STR(stable, true, "alignment held for 400 ms"); + } + return true; + } +}; + +class Test_AutoStoryBooth : public ImageUnitTest{ +public: + Test_AutoStoryBooth(const std::string& image, bool expected) + : ImageUnitTest("PokemonLZA::AutoStoryBooth - " + image, {image}) + , m_expected(expected) + { + m_memory += 6 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + const auto fill_box = [](ImageRGB32& image, const ImageFloatBox& box, uint32_t color){ + const ImagePixelBox pixels = floatbox_to_pixelbox(image, box); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + image.pixel(x, y) = color; + } + } + }; + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + BoothWatcher watcher; + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "booth at height " + std::to_string(height)); + if (!m_expected){ + continue; + } + const ImageFloatBox roof = watcher.roof_box(); + const bool correct_roof = roof.x > 0.60 && roof.x < 0.62 && roof.y > 0.28 && roof.y < 0.31; + TEST_RESULT_COMPONENT_EQUAL_STR(correct_roof, true, "cyan roof location"); + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(250)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial booth frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained booth"); + + ImageRGB32 removed_strip = scaled.copy(); + fill_box(removed_strip, {0.595, 0.28, 0.14, 0.025}, 0xff806040); + const bool without_strip = watcher.detect(removed_strip); + TEST_RESULT_COMPONENT_EQUAL_STR(without_strip, false, "roof strip removed"); + watcher.reset_state(); + watcher.process_frame(scaled, start); + watcher.process_frame(removed_strip, start + Milliseconds(100)); + const bool interrupted = watcher.process_frame(scaled, start + Milliseconds(250)); + TEST_RESULT_COMPONENT_EQUAL_STR(interrupted, false, "interrupted booth detection"); + + ImageRGB32 removed_wall = scaled.copy(); + fill_box(removed_wall, {0.60, 0.30, 0.13, 0.13}, 0xffffffff); + const bool without_wall = watcher.detect(removed_wall); + TEST_RESULT_COMPONENT_EQUAL_STR(without_wall, false, "cyan strip without dark wall"); + + const ImageFloatBox booth_box{0.595, 0.255, 0.14, 0.17}; + ImageRGB32 booth = extract_box_reference(scaled, booth_box).copy(); + ImageRGB32 moved = scaled.copy(); + fill_box(moved, booth_box, 0xff806040); + const size_t target_x = scaled.width() / 3; + const size_t target_y = scaled.height() / 3; + for (size_t y = 0; y < booth.height(); y++){ + for (size_t x = 0; x < booth.width(); x++){ + moved.pixel(target_x + x, target_y + y) = booth.pixel(x, y); + } + } + const bool relocated = watcher.detect(moved); + TEST_RESULT_COMPONENT_EQUAL_STR(relocated, true, "booth at another screen position"); + const bool moved_left = watcher.roof_box().x < 0.4; + TEST_RESULT_COMPONENT_EQUAL_STR(moved_left, true, "relocated roof coordinates"); + + ImageRGB32 ambiguous = scaled.copy(); + for (size_t y = 0; y < booth.height(); y++){ + for (size_t x = 0; x < booth.width(); x++){ + ambiguous.pixel(target_x + x, target_y + y) = booth.pixel(x, y); + } + } + const bool multiple = watcher.detect(ambiguous); + TEST_RESULT_COMPONENT_EQUAL_STR(multiple, false, "ambiguous booth candidates"); + } + return true; + } + +private: + bool m_expected; +}; + +class Test_AutoStoryBeginArrow : public ImageUnitTest{ +public: + Test_AutoStoryBeginArrow(const std::string& image) + : ImageUnitTest("PokemonLZA::AutoStoryBeginArrow - " + image, {image}) + { + m_memory += MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + const ImageFloatBox box{0.05, 0.73, 0.045, 0.12}; + SelectionArrowWatcher watcher(COLOR_GREEN, nullptr, SelectionArrowType::RIGHT, box, Milliseconds(100)); + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, true, "Begin arrow at height " + std::to_string(height)); + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial Begin arrow frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained Begin arrow"); + + const ImagePixelBox pixels = floatbox_to_pixelbox(scaled, box); + ImageRGB32 arrow = extract_box_reference(scaled, pixels).copy(); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + scaled.pixel(x, y) = 0xff102030; + } + } + const bool removed = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(removed, false, "Begin arrow removed"); + + // A highlight on the options above Begin must not start the game + const size_t target_y = scaled.height() * 53 / 100; + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(pixels.min_x + x, target_y + y) = arrow.pixel(x, y); + } + } + const bool other_option = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(other_option, false, "arrow above Begin"); + } + return true; + } +}; + +class Test_AutoStoryTrainerBattle : public ImageUnitTest{ +public: + Test_AutoStoryTrainerBattle(const std::string& image, bool expected, bool check_second_slot = false) + : ImageUnitTest("PokemonLZA::AutoStoryTrainerBattle - " + image, {image}) + , m_expected(expected) + , m_check_second_slot(check_second_slot) + { + m_memory += MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + auto watchers = make_trainer_battle_watchers(Milliseconds(0)); + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + bool detected = false; + for (auto& watcher : watchers){ + detected |= watcher.detect(scaled); + } + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "trainer battle at height " + std::to_string(height)); + if (m_check_second_slot){ + // The second opponent must be detectable independently of slot 0 + const bool second_slot = watchers[1].detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(second_slot, true, "second opponent at height " + std::to_string(height)); + } + } + return true; + } + +private: + bool m_expected; + bool m_check_second_slot; +}; + +class Test_AutoStoryTutorialPage : public ImageUnitTest{ +public: + Test_AutoStoryTutorialPage(const std::string& image, bool expected) + : ImageUnitTest("PokemonLZA::AutoStoryTutorialPage - " + image, {image}) + , m_expected(expected) + { + // The watcher keeps an inverted frame while the resized test is alive + m_memory += 2 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + TutorialPageWatcher watcher(COLOR_CYAN); + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "tutorial at height " + std::to_string(height)); + if (m_expected){ + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial tutorial frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained tutorial match"); + + // Leave the left arrow and page dots in place, removing only Next + const ImagePixelBox source = floatbox_to_pixelbox( + scaled, {998.0 / 1920, 957.0 / 1080, 35.0 / 1920, 34.0 / 1080} + ); + ImageRGB32 pager = extract_box_reference(scaled, source).copy(); + for (size_t y = 0; y < pager.height(); y++){ + for (size_t x = 0; x < pager.width(); x++){ + scaled.pixel(source.min_x + x, source.min_y + y) = 0xffffffff; + } + } + const bool without_next = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(without_next, false, "right arrow removed"); + + // Longer pages may push the pager farther right and down + const size_t target_x = scaled.width() * 13 / 20; + const size_t target_y = scaled.height() * 93 / 100; + for (size_t y = 0; y < pager.height(); y++){ + for (size_t x = 0; x < pager.width(); x++){ + scaled.pixel(target_x + x, target_y + y) = pager.pixel(x, y); + } + } + const bool moved = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(moved, true, "pager moved right and down"); + + // Accept the standard light button colors as well + for (size_t y = 0; y < pager.height(); y++){ + for (size_t x = 0; x < pager.width(); x++){ + scaled.pixel(target_x + x, target_y + y) ^= 0x00ffffff; + } + } + const bool light = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(light, true, "light tutorial pager"); + } + } + return true; + } + +private: + bool m_expected; +}; + +class Test_AutoStoryTutorialWindow : public ImageUnitTest{ +public: + Test_AutoStoryTutorialWindow(const std::string& image, bool expected) + : ImageUnitTest("PokemonLZA::AutoStoryTutorialWindow - " + image, {image}) + , m_expected(expected) + { + m_memory += 2 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + TutorialWindowWatcher watcher; + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "tutorial window at height " + std::to_string(height)); + if (m_expected){ + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial tutorial window frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained tutorial window"); + + // Reject a missing book, a solid green badge, or a missing frame + for (const ImageFloatBox& box : { + ImageFloatBox{523.0 / 1920, 104.0 / 1080, 33.0 / 1920, 39.0 / 1080}, + ImageFloatBox{0.29, 0.165, 0.42, 0.012}, + ImageFloatBox{0.265, 0.18, 0.018, 0.68}, + }){ + for (uint32_t color : {0xff102030, 0xffb8f52e}){ + ImageRGB32 missing_component = scaled.copy(); + const ImagePixelBox pixels = floatbox_to_pixelbox(missing_component, box); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + missing_component.pixel(x, y) = color; + } + } + const bool missing = watcher.detect(missing_component); + TEST_RESULT_COMPONENT_EQUAL_STR(missing, false, "tutorial window component removed"); + watcher.reset_state(); + watcher.process_frame(scaled, start); + watcher.process_frame(missing_component, start + Milliseconds(50)); + const bool interrupted = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(interrupted, false, "interrupted tutorial window match"); + } + } + + // Illustrations, text, and bottom arrows are not required + const ImagePixelBox content = floatbox_to_pixelbox(scaled, {0.29, 0.185, 0.42, 0.735}); + for (size_t y = content.min_y; y < content.max_y; y++){ + for (size_t x = content.min_x; x < content.max_x; x++){ + scaled.pixel(x, y) = 0xffffffff; + } + } + const bool without_content = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(without_content, true, "tutorial content replaced"); + } + } + return true; + } + +private: + bool m_expected; +}; + +class Test_AutoStoryDialogOptions : public ImageUnitTest{ +public: + Test_AutoStoryDialogOptions(const std::string& image, bool expected) + : ImageUnitTest("PokemonLZA::AutoStoryDialogOptions - " + image, {image}) + , m_expected(expected) + { + m_memory += MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + SelectionArrowWatcher watcher( + COLOR_GREEN, nullptr, SelectionArrowType::RIGHT, + {0.5, 0.2, 0.45, 0.6}, Milliseconds(100) + ); + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "dialog option at height " + std::to_string(height)); + if (m_expected){ + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial option frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained option match"); + + const ImagePixelBox source = floatbox_to_pixelbox( + scaled, {1390.0 / 1920, 650.0 / 1080, 55.0 / 1920, 72.0 / 1080} + ); + ImageRGB32 arrow = extract_box_reference(scaled, source).copy(); + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(source.min_x + x, source.min_y + y) = 0xff102030; + } + } + const bool removed = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(removed, false, "selection arrow removed"); + + // More choices or longer labels can move the highlighted row + const size_t target_x = scaled.width() * 13 / 20; + const size_t target_y = scaled.height() * 3 / 10; + for (size_t y = 0; y < arrow.height(); y++){ + for (size_t x = 0; x < arrow.width(); x++){ + scaled.pixel(target_x + x, target_y + y) = arrow.pixel(x, y); + } + } + const bool moved = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(moved, true, "selection arrow moved"); + } + } + return true; + } + +private: + bool m_expected; +}; + +class Test_AutoStoryTutorialMap : public ImageUnitTest{ +public: + Test_AutoStoryTutorialMap(const std::string& image, bool expected) + : ImageUnitTest("PokemonLZA::AutoStoryTutorialMap - " + image, {image}) + , m_expected(expected) + { + m_memory += 2 * MAX_TEST_IMAGE_RGB32_BYTES; + } + + virtual UnitTestResult run_on_images( + Logger&, CancellableScope&, + const std::vector& images + ) const override{ + for (size_t height : {720, 1080, 2160}){ + ImageRGB32 scaled = images[0].scale_to(height * 16 / 9, height); + TutorialMapWatcher watcher; + const bool detected = watcher.detect(scaled); + TEST_RESULT_COMPONENT_EQUAL_STR(detected, m_expected, "tutorial map at height " + std::to_string(height)); + if (m_expected){ + const WallClock start = current_time(); + const bool first = watcher.process_frame(scaled, start); + const bool held = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(first, false, "initial map frame"); + TEST_RESULT_COMPONENT_EQUAL_STR(held, true, "sustained map match"); + + // One prompt alone must not be treated as a map + for (const ImageFloatBox& box : { + ImageFloatBox{0.005, 0.15, 0.025, 0.11}, + ImageFloatBox{0.9, 0.94, 0.04, 0.04}, + }){ + ImageRGB32 missing_prompt = scaled.copy(); + const ImagePixelBox pixels = floatbox_to_pixelbox(missing_prompt, box); + for (size_t y = pixels.min_y; y < pixels.max_y; y++){ + for (size_t x = pixels.min_x; x < pixels.max_x; x++){ + missing_prompt.pixel(x, y) = 0xff102030; + } + } + const bool missing = watcher.detect(missing_prompt); + TEST_RESULT_COMPONENT_EQUAL_STR(missing, false, "map prompt removed"); + watcher.reset_state(); + watcher.process_frame(scaled, start); + watcher.process_frame(missing_prompt, start + Milliseconds(50)); + const bool interrupted = watcher.process_frame(scaled, start + Milliseconds(100)); + TEST_RESULT_COMPONENT_EQUAL_STR(interrupted, false, "interrupted map match"); + } + } + } + return true; + } + +private: + bool m_expected; +}; + +void add_tests_AutoStory(UnitTestDatabase& database){ + database.add(); + database.add(); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_high_062357.png"); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_high_062932.png"); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_high_062357.png", true, 0.476); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_high_062932.png", true, 0.529); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_clipped.png", false); + database.add("PokemonLZA/AutoStory/wild_zone_1_event_no_flag.png", true, 0.055); + database.add("PokemonLZA/AutoStory/wild_zone_1_no_booth.png", true, 0.525); + database.add("PokemonLZA/AutoStory/wild_zone_1_booth.png", true, 0.05); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", false); + database.add("PokemonLZA/AutoStory/rival_dialog_options.png", false); + database.add("PokemonLZA/DoorEntryDetector/hotel_room_alternate_card.png", false); + + database.add("PokemonLZA/AutoStory/wild_zone_1_booth.png", true); + database.add("PokemonLZA/AutoStory/wild_zone_1_no_booth.png", false); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", false); + database.add("PokemonLZA/AutoStory/rival_dialog_options.png", false); + database.add("PokemonLZA/DoorEntryDetector/hotel_room_alternate_card.png", false); + + database.add("PokemonLZA/AutoStory/profile_confirmation.png"); + database.add("PokemonLZA/AutoStory/required_pancham.png", true); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", true, true); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_False.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); + database.add("PokemonLZA/OverworldPartySelectionDetector/mac_zone_10_0_5.png", false); + database.add("PokemonLZA/OverworldPartySelectionDetector/mac_zone_10_6_2.png", false); + + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", true); + database.add("PokemonLZA/AutoStory/main_mission_tutorial.png", false); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/MapDetector/hyperspace_2_True.png", false); + database.add("PokemonLZA/MapDetector/hyperspace_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_False.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); + + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", true); + database.add("PokemonLZA/AutoStory/main_mission_tutorial.png", true); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/AutoStory/rival_dialog_options.png", false); + database.add("PokemonLZA/AutoStory/tutorial_map.png", false); + database.add("PokemonLZA/MapDetector/hyperspace_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); + + database.add("PokemonLZA/AutoStory/rival_dialog_options.png", true); + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", false); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/MapDetector/hyperspace_2_True.png", false); + database.add("PokemonLZA/MapDetector/hyperspace_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_False.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); + + database.add("PokemonLZA/AutoStory/tutorial_map.png", true); + database.add("PokemonLZA/MapDetector/hyperspace_True.png", true); + database.add("PokemonLZA/MapDetector/hyperspace_2_True.png", true); + database.add("PokemonLZA/AutoStory/rival_dialog_options.png", false); + database.add("PokemonLZA/AutoStory/type_matchup_tutorial.png", false); + database.add("PokemonLZA/AutoStory/required_pancham.png", false); + database.add("PokemonLZA/AutoStory/required_cleffa_no_instructions.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_True.png", false); + database.add("PokemonLZA/TransparentBattleDialogDetector/mac_jacinthe_overworld_False.png", false); +} + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.h new file mode 100644 index 0000000000..f9e2a90556 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.h @@ -0,0 +1,20 @@ +/* Auto Story Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStory_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStory_Tests_H + +namespace PokemonAutomation{ +class UnitTestDatabase; +namespace NintendoSwitch{ +namespace PokemonLZA{ + +void add_tests_AutoStory(UnitTestDatabase& database); + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ce09e48a85..28a5933b15 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2190,10 +2190,14 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_LocationNameReader.h Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.cpp Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapDetector.h + Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.cpp + Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_TutorialMapDetector.h Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapIconDetector.cpp Source/VideoGames/PokemonLZA/Inference/Map/PokemonLZA_MapIconDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_AlertEyeDetector.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_BoothDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_ButtonDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DayNightChangeDetector.cpp @@ -2202,16 +2206,22 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DayNightStateDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DialogDetector.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_DoorEntryDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceCalorieDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceCalorieDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_HyperspaceRewardNameReader.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_EventMarkerDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_MainMenuDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_OverworldPartySelectionDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_SelectionArrowDetector.h + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.cpp + Source/VideoGames/PokemonLZA/Inference/PokemonLZA_TutorialWindowDetector.h Source/VideoGames/PokemonLZA/Inference/PokemonLZA_WeatherDetector.cpp Source/VideoGames/PokemonLZA/Inference/PokemonLZA_WeatherDetector.h Source/VideoGames/PokemonLZA/InferenceTraining/PokemonLZA_GenerateLocationNameOCR.cpp @@ -2232,6 +2242,14 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/PokemonLZA_Settings.h Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp Source/VideoGames/PokemonLZA/PokemonLZA_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryTools.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Segment_01.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStory_Tests.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.cpp Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_DonutMaker.h Source/VideoGames/PokemonLZA/Programs/Farming/PokemonLZA_FriendshipFarmer.cpp