From 82253db2345e8094e5a2ed06087774cc803ac59a Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 03:00:33 -0700 Subject: [PATCH 1/2] BBQ: fly to a Terarium fly point from anywhere, checking its name central_to_*() only work when the player stands at Central Plaza, since they push the map cursor a fixed amount from where it opens. fly_to_terarium_point() anchors the cursor first, the way return_to_plaza() does (top-left corner, back toward the center), snaps it onto the Central Plaza icon, then applies the same central_to_*() offset. Before flying, it OCRs the fly point's name label on the map; a missing or wrong name closes the map and retries, up to 3 times, then returns false with the map closed. Names are known for English and the Chinese (Traditional) plazas; other names are not checked yet. The offsets now live in one table that central_to_*() also reads, with the same values as before. The retry loop runs against an injectable TerariumFlyActions, so it is unit tested with a scripted fake. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Map/PokemonSV_MapFlyPointLabelReader.cpp | 53 +++ .../Map/PokemonSV_MapFlyPointLabelReader.h | 43 ++ .../VideoGames/PokemonSV/PokemonSV_Tests.cpp | 377 ++++++++++++++++++ .../PokemonSV/Programs/PokemonSV_Terarium.cpp | 115 +++++- .../PokemonSV/Programs/PokemonSV_Terarium.h | 11 + .../Programs/PokemonSV_TerariumFlyPoints.cpp | 227 +++++++++++ .../Programs/PokemonSV_TerariumFlyPoints.h | 124 ++++++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 8 files changed, 945 insertions(+), 9 deletions(-) create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.cpp create mode 100644 SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.h diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.cpp new file mode 100644 index 0000000000..a00867177b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.cpp @@ -0,0 +1,53 @@ +/* Map Fly Point Label Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "CommonTools/Images/ImageFilter.h" +#include "CommonTools/OCR/OCR_Routines.h" +#include "PokemonSV_MapFlyPointLabelReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// The label is a white box centered above the cursor, which sits at the +// middle of the screen. On a lab screenshot (2026-10-22, the zoom level the +// map opens with) the box for 中心廣場 spans x 0.426-0.574, y 0.341-0.408. +// The read area keeps inside it vertically, and is wider so that longer +// names fit. +MapFlyPointLabelReader::MapFlyPointLabelReader() + : m_box(0.250, 0.348, 0.500, 0.054) +{} + +void MapFlyPointLabelReader::make_overlays(VideoOverlaySet& items) const{ + items.add(COLOR_BLUE, m_box); +} + +std::string MapFlyPointLabelReader::read(Logger& logger, Language language, const ImageViewRGB32& screen) const{ + // Keep the label's dark text and blank everything else, including most + // of the map around the label. + ImageRGB32 filtered = to_blackwhite_rgb32_range( + extract_box_reference(screen, m_box), + true, + 0xff000000, 0xff707070 + ); + + std::string ret; + for (char ch : OCR::ocr_read(language, filtered)){ + if ((unsigned char)ch < 32){ + continue; + } + ret += ch; + } + logger.log("Map fly point label: \"" + ret + "\""); + return ret; +} + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h new file mode 100644 index 0000000000..6c65b6d5e1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h @@ -0,0 +1,43 @@ +/* Map Fly Point Label Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Read the name label the map shows above the cursor when the cursor is on + * a fly point. + * + */ + +#ifndef PokemonAutomation_PokemonSV_MapFlyPointLabelReader_H +#define PokemonAutomation_PokemonSV_MapFlyPointLabelReader_H + +#include +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/Language.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +class MapFlyPointLabelReader{ +public: + MapFlyPointLabelReader(); + + void make_overlays(VideoOverlaySet& items) const; + + // OCR the label's dark text. Returns the raw OCR text, which is empty + // (or noise) when there is no label. + std::string read(Logger& logger, Language language, const ImageViewRGB32& screen) const; + +private: + ImageFloatBox m_box; +}; + + +} +} +} +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index d966bfe133..3a5821a6fe 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -25,6 +25,7 @@ #include "Inference/Overworld/PokemonSV_LetsGoKillDetector.h" #include "Inference/Picnics/PokemonSV_SandwichRecipeDetector.h" #include "Inference/Picnics/PokemonSV_SandwichHandDetector.h" +#include "Programs/PokemonSV_TerariumFlyPoints.h" #include "PokemonSV_Tests.h" namespace PokemonAutomation{ @@ -33,7 +34,383 @@ namespace PokemonSV{ +namespace{ + +// Keeps every line logged, so a test can check the fly log. +class TerariumFlyLogger : public Logger{ +public: + std::vector lines; + + virtual void log(const std::string& msg, Color) override{ + lines.push_back(msg); + } +}; + +std::string join_lines(const std::vector& items, const std::string& separator){ + std::string ret; + for (const std::string& item : items){ + if (!ret.empty()){ + ret += separator; + } + ret += item; + } + return ret; +} + + +struct TerariumFlyPointMoveCase{ + TerariumFlyPoint point; + std::string name; + MapCursorMove move; +}; + +// The table must keep the literals central_to_*() passed to +// map_move_cursor_fly(), since central_to_*() now reads them from it. +class Test_TerariumFlyPointMove : public UnitTest{ +public: + Test_TerariumFlyPointMove(TerariumFlyPointMoveCase test_case) + : UnitTest("PokemonSV::TerariumFlyPointMove - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const TerariumFlyPointInfo& info = terarium_fly_point_info(m_case.point); + if (info.name != m_case.name){ + return "Expected name \"" + m_case.name + "\", got \"" + info.name + "\""; + } + const MapCursorMove& move = info.from_central_plaza; + if (move.x != m_case.move.x || move.y != m_case.move.y || move.hold != m_case.move.hold){ + return "Expected move (" + std::to_string(m_case.move.x) + ", " + std::to_string(m_case.move.y) + + ") for " + std::to_string(m_case.move.hold.count()) + " ms, got (" + + std::to_string(move.x) + ", " + std::to_string(move.y) + + ") for " + std::to_string(move.hold.count()) + " ms"; + } + return true; + } + +private: + const TerariumFlyPointMoveCase m_case; +}; + +void add_tests_TerariumFlyPointMove(UnitTestDatabase& database){ + const TerariumFlyPointMoveCase cases[] = { + {.point = TerariumFlyPoint::CENTRAL_PLAZA, .name = "Central Plaza", .move = {}}, + {.point = TerariumFlyPoint::COASTAL_PLAZA, .name = "Coastal Plaza", .move = {.x = +0.409, .y = +1, .hold = Milliseconds(1680)}}, + {.point = TerariumFlyPoint::CANYON_PLAZA, .name = "Canyon Plaza", .move = {.x = -1, .y = -1, .hold = Milliseconds(1720)}}, + {.point = TerariumFlyPoint::SAVANNA_PLAZA, .name = "Savanna Plaza", .move = {.x = +0.291, .y = -1, .hold = Milliseconds(1440)}}, + {.point = TerariumFlyPoint::POLAR_PLAZA, .name = "Polar Plaza", .move = {.x = -0.844, .y = +0.805, .hold = Milliseconds(1960)}}, + {.point = TerariumFlyPoint::POLAR_REST_AREA, .name = "Polar Rest Area", .move = {.x = -0.414, .y = +1, .hold = Milliseconds(1840)}}, + {.point = TerariumFlyPoint::POLAR_CLASSROOM_1, .name = "Polar Classroom 1", .move = {.x = -1, .y = +0.844, .hold = Milliseconds(1200)}}, + {.point = TerariumFlyPoint::CANYON_REST_AREA, .name = "Canyon Rest Area", .move = {.x = -1, .y = -0.094, .hold = Milliseconds(1280)}}, + {.point = TerariumFlyPoint::SAVANNA_CLASSROOM, .name = "Savanna Classroom", .move = {.x = +1, .y = -0.724, .hold = Milliseconds(1120)}}, + {.point = TerariumFlyPoint::CHARGESTONE_CAVERN, .name = "Chargestone Cavern", .move = {.x = -1, .y = -0.055, .hold = Milliseconds(1040)}}, + }; + for (const TerariumFlyPointMoveCase& test_case : cases){ + database.add(test_case); + } +} + + +struct FlyPointLabelCase{ + std::string name; + TerariumFlyPoint expected_point; + Language language; + std::string label; + FlyPointLabelMatch expected; +}; + +class Test_FlyPointLabelMatch : public UnitTest{ +public: + Test_FlyPointLabelMatch(FlyPointLabelCase test_case) + : UnitTest("PokemonSV::FlyPointLabelMatch - " + test_case.name) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const FlyPointLabelMatch match = match_fly_point_label( + m_case.expected_point, m_case.language, m_case.label + ); + if (match != m_case.expected){ + return "Label \"" + m_case.label + "\" (normalized \"" + normalize_fly_point_label(m_case.label) + + "\"): expected " + fly_point_label_match_to_string(m_case.expected) + + ", got " + fly_point_label_match_to_string(match); + } + return true; + } + +private: + const FlyPointLabelCase m_case; +}; + +void add_tests_FlyPointLabelMatch(UnitTestDatabase& database){ + const FlyPointLabelCase cases[] = { + { + .name = "english name", + .expected_point = TerariumFlyPoint::CENTRAL_PLAZA, + .language = Language::English, + .label = "Central Plaza", + .expected = FlyPointLabelMatch::MATCH, + }, + { + .name = "case, spacing and punctuation don't matter", + .expected_point = TerariumFlyPoint::POLAR_PLAZA, + .language = Language::English, + .label = " POLAR plaza.", + .expected = FlyPointLabelMatch::MATCH, + }, + { + .name = "OCR noise around the name", + .expected_point = TerariumFlyPoint::COASTAL_PLAZA, + .language = Language::English, + .label = "| Coastal Plaza -", + .expected = FlyPointLabelMatch::MATCH, + }, + { + // The words in a full in-game name, whatever else it holds. + .name = "longer english name", + .expected_point = TerariumFlyPoint::POLAR_CLASSROOM_1, + .language = Language::English, + .label = "Polar Outdoor Classroom 1", + .expected = FlyPointLabelMatch::MATCH, + }, + { + .name = "same biome, other fly point", + .expected_point = TerariumFlyPoint::POLAR_REST_AREA, + .language = Language::English, + .label = "Polar Plaza", + .expected = FlyPointLabelMatch::MISMATCH, + }, + { + .name = "other biome", + .expected_point = TerariumFlyPoint::CANYON_PLAZA, + .language = Language::English, + .label = "Savanna Plaza", + .expected = FlyPointLabelMatch::MISMATCH, + }, + { + .name = "chinese name as seen in the lab", + .expected_point = TerariumFlyPoint::CENTRAL_PLAZA, + .language = Language::ChineseTraditional, + .label = "中心廣場", + .expected = FlyPointLabelMatch::MATCH, + }, + { + .name = "chinese with spaces between characters", + .expected_point = TerariumFlyPoint::SAVANNA_PLAZA, + .language = Language::ChineseTraditional, + .label = "草 原\xE3\x80\x80廣 場", + .expected = FlyPointLabelMatch::MATCH, + }, + { + .name = "chinese, other plaza", + .expected_point = TerariumFlyPoint::COASTAL_PLAZA, + .language = Language::ChineseTraditional, + .label = "中心廣場", + .expected = FlyPointLabelMatch::MISMATCH, + }, + { + .name = "nothing read", + .expected_point = TerariumFlyPoint::CANYON_PLAZA, + .language = Language::English, + .label = "", + .expected = FlyPointLabelMatch::NO_LABEL, + }, + { + .name = "only punctuation read", + .expected_point = TerariumFlyPoint::CANYON_PLAZA, + .language = Language::English, + .label = " .|- ", + .expected = FlyPointLabelMatch::NO_LABEL, + }, + { + .name = "chinese name not known yet", + .expected_point = TerariumFlyPoint::CHARGESTONE_CAVERN, + .language = Language::ChineseTraditional, + .label = "中心廣場", + .expected = FlyPointLabelMatch::NOT_CHECKED, + }, + { + .name = "language without names", + .expected_point = TerariumFlyPoint::CENTRAL_PLAZA, + .language = Language::Japanese, + .label = "", + .expected = FlyPointLabelMatch::NOT_CHECKED, + }, + }; + for (const FlyPointLabelCase& test_case : cases){ + database.add(test_case); + } +} + + +// Map steps that follow a script, one entry per attempt, and record what ran. +class ScriptedTerariumFly : public TerariumFlyActions{ +public: + std::vector central_plaza_found; + std::vector labels; + std::vector fly_point_under_cursor; + + std::vector steps; + + virtual bool open_map_at_central_plaza(size_t attempt) override{ + steps.push_back("open(" + std::to_string(attempt) + ")"); + return next(central_plaza_found, m_opens, true); + } + virtual void move_cursor(const MapCursorMove& move) override{ + steps.push_back("move(" + std::to_string(move.hold.count()) + "ms)"); + } + virtual std::string read_fly_point_label() override{ + steps.push_back("read"); + return next(labels, m_reads, std::string()); + } + virtual bool fly_here() override{ + steps.push_back("fly"); + return next(fly_point_under_cursor, m_flies, true); + } + virtual void close_map() override{ + steps.push_back("close"); + } + +private: + // Past the end of a script, every attempt gets the default. + template + static Type next(const std::vector& script, size_t& index, Type fallback){ + return index < script.size() ? Type(script[index++]) : fallback; + } + + size_t m_opens = 0; + size_t m_reads = 0; + size_t m_flies = 0; +}; + +struct TerariumFlyScenario{ + std::string name; + TerariumFlyPoint destination = TerariumFlyPoint::COASTAL_PLAZA; + Language language = Language::English; + std::vector central_plaza_found; + std::vector labels; + std::vector fly_point_under_cursor; + bool expected_flew = true; + std::string expected_steps; + std::vector expected_log; // Checked when not empty. +}; + +class Test_TerariumFly : public UnitTest{ +public: + Test_TerariumFly(TerariumFlyScenario scenario) + : UnitTest("PokemonSV::TerariumFly - " + scenario.name) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + ScriptedTerariumFly actions; + actions.central_plaza_found = m_scenario.central_plaza_found; + actions.labels = m_scenario.labels; + actions.fly_point_under_cursor = m_scenario.fly_point_under_cursor; + + TerariumFlyLogger logger; + const bool flew = fly_to_terarium_point(logger, actions, m_scenario.language, m_scenario.destination); + if (flew != m_scenario.expected_flew){ + return std::string("Expected ") + (m_scenario.expected_flew ? "to fly" : "to give up") + + ", but it " + (flew ? "flew" : "gave up"); + } + const std::string steps = join_lines(actions.steps, " "); + if (steps != m_scenario.expected_steps){ + return "Expected steps: " + m_scenario.expected_steps + "\nActual steps: " + steps; + } + if (!m_scenario.expected_log.empty() && logger.lines != m_scenario.expected_log){ + return "Expected log:\n" + join_lines(m_scenario.expected_log, "\n") + + "\nActual log:\n" + join_lines(logger.lines, "\n"); + } + return true; + } + +private: + const TerariumFlyScenario m_scenario; +}; + +void add_tests_TerariumFly(UnitTestDatabase& database){ + const TerariumFlyScenario scenarios[] = { + { + .name = "right name on the first try", + .labels = {"Coastal Plaza"}, + .expected_steps = "open(1) move(1680ms) read fly", + .expected_log = { + "Fly to Coastal Plaza, attempt 1/3: map label \"Coastal Plaza\": match.", + "Fly to Coastal Plaza, attempt 1/3: flew.", + }, + }, + { + .name = "wrong name, then the right one", + .labels = {"Central Plaza", "Coastal Plaza"}, + .expected_steps = "open(1) move(1680ms) read close open(2) move(1680ms) read fly", + .expected_log = { + "Fly to Coastal Plaza, attempt 1/3: map label \"Central Plaza\": mismatch.", + "Fly to Coastal Plaza, attempt 2/3: map label \"Coastal Plaza\": match.", + "Fly to Coastal Plaza, attempt 2/3: flew.", + }, + }, + { + // The cursor missed every fly point: give up, with the map closed. + .name = "no label three times", + .labels = {"", "", ""}, + .expected_flew = false, + .expected_steps = "open(1) move(1680ms) read close open(2) move(1680ms) read close open(3) move(1680ms) read close", + .expected_log = { + "Fly to Coastal Plaza, attempt 1/3: map label \"\": no label.", + "Fly to Coastal Plaza, attempt 2/3: map label \"\": no label.", + "Fly to Coastal Plaza, attempt 3/3: map label \"\": no label.", + "Fly to Coastal Plaza: gave up after 3 attempts.", + }, + }, + { + .name = "central plaza not found, then found", + .central_plaza_found = {false, true}, + .labels = {"Coastal Plaza"}, + .expected_steps = "open(1) close open(2) move(1680ms) read fly", + .expected_log = { + "Fly to Coastal Plaza, attempt 1/3: Central Plaza not found on the map. Closing the map.", + "Fly to Coastal Plaza, attempt 2/3: map label \"Coastal Plaza\": match.", + "Fly to Coastal Plaza, attempt 2/3: flew.", + }, + }, + { + .name = "right name but no fly menu", + .labels = {"Coastal Plaza", "Coastal Plaza"}, + .fly_point_under_cursor = {false, true}, + .expected_steps = "open(1) move(1680ms) read fly close open(2) move(1680ms) read fly", + }, + { + // Without a known name, it flies as central_to_*() does: unchecked. + .name = "name not known in the language", + .language = Language::Japanese, + .expected_steps = "open(1) move(1680ms) fly", + .expected_log = { + "Fly to Coastal Plaza: its name isn't known in this language. Flying without checking it.", + "Fly to Coastal Plaza, attempt 1/3: flew.", + }, + }, + { + // Central Plaza is where the cursor starts: no move. + .name = "central plaza", + .destination = TerariumFlyPoint::CENTRAL_PLAZA, + .language = Language::ChineseTraditional, + .labels = {"中心廣場"}, + .expected_steps = "open(1) read fly", + }, + }; + for (const TerariumFlyScenario& scenario : scenarios){ + database.add(scenario); + } +} + +} + + void add_tests(UnitTestDatabase& database){ + add_tests_TerariumFlyPointMove(database); + add_tests_FlyPointLabelMatch(database); + add_tests_TerariumFly(database); add_tests_WhiteButtonDetector(database); add_tests_BoxInfoDetector(database); add_tests_BoxEggDetector(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp index fb19258610..07962084e9 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp @@ -7,13 +7,16 @@ #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/Tools/ErrorDumper.h" #include "CommonFramework/Exceptions/ProgramFinishedException.h" +#include "CommonFramework/Exceptions/UnexpectedBattleException.h" #include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/OCR/OCR_Routines.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_NormalBattleMenus.h" #include "VideoGames/PokemonSV/Inference/Battles/PokemonSV_EncounterWatcher.h" #include "VideoGames/PokemonSV/Inference/PokemonSV_MainMenuDetector.h" #include "VideoGames/PokemonSV/Inference/Overworld/PokemonSV_OverworldDetector.h" +#include "VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_GameEntry.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_SaveGame.h" #include "VideoGames/PokemonSV/Programs/PokemonSV_MenuNavigation.h" @@ -144,40 +147,134 @@ void map_move_cursor_fly( } } +namespace{ + +// Fly from Central Plaza with the offsets the table holds. Same inputs as +// the literals central_to_*() used before. +void central_to(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, TerariumFlyPoint point){ + const TerariumFlyPointInfo& fly_point = terarium_fly_point_info(point); + const MapCursorMove& move = fly_point.from_central_plaza; + map_move_cursor_fly(info, stream, context, move.x, move.y, move.hold, 160ms, fly_point.name); +} + +} + void central_to_polar_rest(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -0.414, +1, 1840ms, 160ms, "Polar Rest Area"); + central_to(info, stream, context, TerariumFlyPoint::POLAR_REST_AREA); } void central_to_polar_class1(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -1, +0.844, 1200ms, 160ms, "Polar Classroom 1"); + central_to(info, stream, context, TerariumFlyPoint::POLAR_CLASSROOM_1); } void central_to_polar_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -0.844, +0.805, 1960ms, 160ms, "Polar Plaza"); + central_to(info, stream, context, TerariumFlyPoint::POLAR_PLAZA); } void central_to_coastal_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, +0.409, +1, 1680ms, 160ms, "Coastal Plaza"); + central_to(info, stream, context, TerariumFlyPoint::COASTAL_PLAZA); } void central_to_canyon_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -1, -1, 1720ms, 160ms, "Canyon Plaza"); + central_to(info, stream, context, TerariumFlyPoint::CANYON_PLAZA); } void central_to_savanna_plaza(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, +0.291, -1, 1440ms, 160ms, "Savanna Plaza"); + central_to(info, stream, context, TerariumFlyPoint::SAVANNA_PLAZA); } void central_to_canyon_rest(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -1, -0.094, 1280ms, 160ms, "Canyon Rest Area"); + central_to(info, stream, context, TerariumFlyPoint::CANYON_REST_AREA); } void central_to_savanna_class(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, +1, -0.724, 1120ms, 160ms, "Savanna Classroom"); + central_to(info, stream, context, TerariumFlyPoint::SAVANNA_CLASSROOM); } void central_to_chargestone(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context){ - map_move_cursor_fly(info, stream, context, -1, -0.055, 1040ms, 160ms, "Chargestone Cavern"); + central_to(info, stream, context, TerariumFlyPoint::CHARGESTONE_CAVERN); +} + +namespace{ + +class TerariumFlyActionsConsole : public TerariumFlyActions{ +public: + TerariumFlyActionsConsole( + const ProgramInfo& info, VideoStream& stream, ProControllerContext& context, + Language language + ) + : m_info(info), m_stream(stream), m_context(context) + , m_language(language) + {} + + virtual bool open_map_at_central_plaza(size_t attempt) override{ + try{ + open_map_from_overworld(m_info, m_stream, m_context); + }catch (OperationFailedException&){ + return false; + } + + // Same as return_to_plaza(): the top left corner one zoom level out, + // then back toward the center, overshooting a bit after a failure. + pbf_press_button(m_context, BUTTON_ZL, 320ms, 800ms); + pbf_move_left_joystick(m_context, {-1, +1}, 4000ms, 320ms); + pbf_move_left_joystick(m_context, {+1, -1}, attempt <= 1 ? 2000ms : 2400ms, 320ms); + pbf_press_button(m_context, BUTTON_ZR, 320ms, 800ms); + + // Put the cursor on the Central Plaza icon one zoom level in, like + // fly_to_closest_pokecenter_on_map(), then zoom back out to the + // level the central_to_*() offsets are for. + pbf_press_button(m_context, BUTTON_ZR, 320ms, 800ms); + const bool found = detect_closest_flypoint_and_move_map_cursor_there( + m_info, m_stream, m_context, FlyPoint::POKECENTER + ); + pbf_press_button(m_context, BUTTON_ZL, 320ms, 800ms); + return found; + } + virtual void move_cursor(const MapCursorMove& move) override{ + pbf_move_left_joystick(m_context, {move.x, move.y}, move.hold, 160ms); + pbf_press_button(m_context, BUTTON_ZL, 320ms, 800ms); + } + virtual std::string read_fly_point_label() override{ + m_context.wait_for_all_requests(); + MapFlyPointLabelReader reader; + VideoOverlaySet overlays(m_stream.overlay()); + reader.make_overlays(overlays); + return reader.read(m_stream.logger(), m_language, m_stream.video().snapshot()); + } + virtual bool fly_here() override{ + try{ + return fly_to_overworld_from_map(m_info, m_stream, m_context, true); + }catch (UnexpectedBattleException&){ + throw; + }catch (OperationFailedException&){ + return false; + } + } + virtual void close_map() override{ + press_Bs_to_back_to_overworld(m_info, m_stream, m_context); + } + +private: + const ProgramInfo& m_info; + VideoStream& m_stream; + ProControllerContext& m_context; + Language m_language; +}; + +} + +bool fly_to_terarium_point( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + Language language, TerariumFlyPoint destination +){ + if (language != Language::None && !OCR::ocr_language_available(language)){ + stream.log("No OCR data for the game language. Fly point names won't be checked.", COLOR_RED); + language = Language::None; + } + TerariumFlyActionsConsole actions(info, stream, context, language); + return fly_to_terarium_point(stream.logger(), actions, language, destination); } void jump_glide_fly( diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h index 8c21530cea..d61b65aa78 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h @@ -7,8 +7,10 @@ #ifndef PokemonAutomation_PokemonSV_Terarium_H #define PokemonAutomation_PokemonSV_Terarium_H +#include "CommonFramework/Language.h" #include "CommonFramework/Tools/VideoStream.h" #include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "PokemonSV_TerariumFlyPoints.h" namespace PokemonAutomation{ struct ProgramInfo; @@ -27,6 +29,15 @@ void map_move_cursor_fly( std::string location ); +// Fly to a Terarium fly point from anywhere in the Terarium. Before flying, +// checks the fly point's name on the map when that name is known in +// "language". Returns false, back in the overworld, if it couldn't get there. +bool fly_to_terarium_point( + const ProgramInfo& info, + VideoStream& stream, ProControllerContext& context, + Language language, TerariumFlyPoint destination +); + // From central plaza, fly to the polar rest area void central_to_polar_rest(const ProgramInfo& info, VideoStream& stream, ProControllerContext& context); diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.cpp new file mode 100644 index 0000000000..9ffb3bee13 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.cpp @@ -0,0 +1,227 @@ +/* Terarium Fly Points + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonSV_TerariumFlyPoints.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +// The moves from Central Plaza are the ones central_to_*() has always used. +// +// The English words assume the in-game names contain them. The Chinese +// (Traditional) plaza names follow 中心廣場 and 草原廣場, both read off the +// lab's map; the other Chinese names aren't known yet, so those labels +// aren't checked. +const TerariumFlyPointInfo& terarium_fly_point_info(TerariumFlyPoint point){ + static const std::map TABLE{ + {TerariumFlyPoint::CENTRAL_PLAZA, { + .name = "Central Plaza", + .from_central_plaza = {}, + .label_words = { + {Language::English, {"central", "plaza"}}, + {Language::ChineseTraditional, {"中心廣場"}}, + }, + }}, + {TerariumFlyPoint::COASTAL_PLAZA, { + .name = "Coastal Plaza", + .from_central_plaza = {.x = +0.409, .y = +1, .hold = Milliseconds(1680)}, + .label_words = { + {Language::English, {"coastal", "plaza"}}, + {Language::ChineseTraditional, {"海岸廣場"}}, + }, + }}, + {TerariumFlyPoint::CANYON_PLAZA, { + .name = "Canyon Plaza", + .from_central_plaza = {.x = -1, .y = -1, .hold = Milliseconds(1720)}, + .label_words = { + {Language::English, {"canyon", "plaza"}}, + {Language::ChineseTraditional, {"峽谷廣場"}}, + }, + }}, + {TerariumFlyPoint::SAVANNA_PLAZA, { + .name = "Savanna Plaza", + .from_central_plaza = {.x = +0.291, .y = -1, .hold = Milliseconds(1440)}, + .label_words = { + {Language::English, {"savanna", "plaza"}}, + {Language::ChineseTraditional, {"草原廣場"}}, + }, + }}, + {TerariumFlyPoint::POLAR_PLAZA, { + .name = "Polar Plaza", + .from_central_plaza = {.x = -0.844, .y = +0.805, .hold = Milliseconds(1960)}, + .label_words = { + {Language::English, {"polar", "plaza"}}, + {Language::ChineseTraditional, {"極地廣場"}}, + }, + }}, + {TerariumFlyPoint::POLAR_REST_AREA, { + .name = "Polar Rest Area", + .from_central_plaza = {.x = -0.414, .y = +1, .hold = Milliseconds(1840)}, + .label_words = { + {Language::English, {"polar", "rest"}}, + }, + }}, + {TerariumFlyPoint::POLAR_CLASSROOM_1, { + .name = "Polar Classroom 1", + .from_central_plaza = {.x = -1, .y = +0.844, .hold = Milliseconds(1200)}, + .label_words = { + {Language::English, {"polar", "classroom"}}, + }, + }}, + {TerariumFlyPoint::CANYON_REST_AREA, { + .name = "Canyon Rest Area", + .from_central_plaza = {.x = -1, .y = -0.094, .hold = Milliseconds(1280)}, + .label_words = { + {Language::English, {"canyon", "rest"}}, + }, + }}, + {TerariumFlyPoint::SAVANNA_CLASSROOM, { + .name = "Savanna Classroom", + .from_central_plaza = {.x = +1, .y = -0.724, .hold = Milliseconds(1120)}, + .label_words = { + {Language::English, {"savanna", "classroom"}}, + }, + }}, + {TerariumFlyPoint::CHARGESTONE_CAVERN, { + .name = "Chargestone Cavern", + .from_central_plaza = {.x = -1, .y = -0.055, .hold = Milliseconds(1040)}, + .label_words = { + {Language::English, {"chargestone"}}, + }, + }}, + }; + auto iter = TABLE.find(point); + if (iter == TABLE.end()){ + throw std::logic_error("terarium_fly_point_info(): Unknown fly point."); + } + return iter->second; +} + +const std::vector& all_terarium_fly_points(){ + static const std::vector POINTS{ + TerariumFlyPoint::CENTRAL_PLAZA, + TerariumFlyPoint::COASTAL_PLAZA, + TerariumFlyPoint::CANYON_PLAZA, + TerariumFlyPoint::SAVANNA_PLAZA, + TerariumFlyPoint::POLAR_PLAZA, + TerariumFlyPoint::POLAR_REST_AREA, + TerariumFlyPoint::POLAR_CLASSROOM_1, + TerariumFlyPoint::CANYON_REST_AREA, + TerariumFlyPoint::SAVANNA_CLASSROOM, + TerariumFlyPoint::CHARGESTONE_CAVERN, + }; + return POINTS; +} + + +std::string normalize_fly_point_label(const std::string& text){ + // U+3000 IDEOGRAPHIC SPACE in UTF-8. + const std::string IDEOGRAPHIC_SPACE = "\xE3\x80\x80"; + + std::string ret; + for (size_t c = 0; c < text.size(); c++){ + if (text.compare(c, IDEOGRAPHIC_SPACE.size(), IDEOGRAPHIC_SPACE) == 0){ + c += IDEOGRAPHIC_SPACE.size() - 1; + continue; + } + const unsigned char ch = (unsigned char)text[c]; + if (ch >= 0x80){ + ret += (char)ch; + }else if ('A' <= ch && ch <= 'Z'){ + ret += (char)(ch - 'A' + 'a'); + }else if (('a' <= ch && ch <= 'z') || ('0' <= ch && ch <= '9')){ + ret += (char)ch; + } + } + return ret; +} + +std::string fly_point_label_match_to_string(FlyPointLabelMatch match){ + switch (match){ + case FlyPointLabelMatch::MATCH: return "match"; + case FlyPointLabelMatch::MISMATCH: return "mismatch"; + case FlyPointLabelMatch::NO_LABEL: return "no label"; + case FlyPointLabelMatch::NOT_CHECKED: return "not checked"; + } + return "?"; +} + +FlyPointLabelMatch match_fly_point_label( + TerariumFlyPoint expected, Language language, + const std::string& label_text +){ + const TerariumFlyPointInfo& info = terarium_fly_point_info(expected); + auto iter = info.label_words.find(language); + if (iter == info.label_words.end()){ + return FlyPointLabelMatch::NOT_CHECKED; + } + const std::string normalized = normalize_fly_point_label(label_text); + if (normalized.empty()){ + return FlyPointLabelMatch::NO_LABEL; + } + for (const std::string& word : iter->second){ + if (normalized.find(word) == std::string::npos){ + return FlyPointLabelMatch::MISMATCH; + } + } + return FlyPointLabelMatch::MATCH; +} + + +bool fly_to_terarium_point( + Logger& logger, TerariumFlyActions& actions, + Language language, TerariumFlyPoint destination, + size_t max_attempts +){ + const TerariumFlyPointInfo& info = terarium_fly_point_info(destination); + const bool check_label = info.label_words.contains(language); + if (!check_label){ + logger.log("Fly to " + info.name + ": its name isn't known in this language. Flying without checking it."); + } + + for (size_t attempt = 1; attempt <= max_attempts; attempt++){ + const std::string prefix = "Fly to " + info.name + ", attempt " + + std::to_string(attempt) + "/" + std::to_string(max_attempts) + ": "; + + if (!actions.open_map_at_central_plaza(attempt)){ + logger.log(prefix + "Central Plaza not found on the map. Closing the map."); + actions.close_map(); + continue; + } + if (info.from_central_plaza.hold > Milliseconds(0)){ + actions.move_cursor(info.from_central_plaza); + } + + if (check_label){ + const std::string label = actions.read_fly_point_label(); + const FlyPointLabelMatch match = match_fly_point_label(destination, language, label); + logger.log(prefix + "map label \"" + label + "\": " + fly_point_label_match_to_string(match) + "."); + if (match != FlyPointLabelMatch::MATCH){ + actions.close_map(); + continue; + } + } + + if (actions.fly_here()){ + logger.log(prefix + "flew."); + return true; + } + logger.log(prefix + "no fly point under the cursor. Closing the map."); + actions.close_map(); + } + + logger.log("Fly to " + info.name + ": gave up after " + std::to_string(max_attempts) + " attempts."); + return false; +} + + + +} +} +} diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.h b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.h new file mode 100644 index 0000000000..6cdd311d11 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.h @@ -0,0 +1,124 @@ +/* Terarium Fly Points + * + * From: https://github.com/PokemonAutomation/ + * + * Flying to a Terarium fly point from anywhere in the Terarium: where each + * fly point is on the map, how to recognize its name label, and the retry + * loop. The game steps are injected through TerariumFlyActions, so the loop + * can run against a scripted fake in the unit tests. + * + */ + +#ifndef PokemonAutomation_PokemonSV_TerariumFlyPoints_H +#define PokemonAutomation_PokemonSV_TerariumFlyPoints_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "Common/Cpp/Logging/AbstractLogger.h" +#include "CommonFramework/Language.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonSV{ + + +enum class TerariumFlyPoint{ + CENTRAL_PLAZA, + COASTAL_PLAZA, + CANYON_PLAZA, + SAVANNA_PLAZA, + POLAR_PLAZA, + POLAR_REST_AREA, + POLAR_CLASSROOM_1, + CANYON_REST_AREA, + SAVANNA_CLASSROOM, + CHARGESTONE_CAVERN, +}; + +// A left joystick push on the map, at the zoom level the map opens with. +struct MapCursorMove{ + double x = 0; + double y = 0; + Milliseconds hold = Milliseconds(0); +}; + +struct TerariumFlyPointInfo{ + // The name used in logs. + std::string name; + + // Where the fly point is relative to the Central Plaza fly point. + // A hold of 0 means no move. + MapCursorMove from_central_plaza; + + // Words the map's name label must contain, after + // normalize_fly_point_label(), for each language. A language that isn't + // listed has no known name, and the label isn't checked. + std::map> label_words; +}; + +const TerariumFlyPointInfo& terarium_fly_point_info(TerariumFlyPoint point); +const std::vector& all_terarium_fly_points(); + + +// Lowercase ASCII letters and drop ASCII spaces and punctuation and +// ideographic spaces, so OCR spacing doesn't matter. Other characters are +// kept as they are. +std::string normalize_fly_point_label(const std::string& text); + +enum class FlyPointLabelMatch{ + MATCH, + MISMATCH, + NO_LABEL, // Nothing was read: the cursor isn't on a fly point. + NOT_CHECKED, // The fly point's name isn't known in this language. +}; +std::string fly_point_label_match_to_string(FlyPointLabelMatch match); + +FlyPointLabelMatch match_fly_point_label( + TerariumFlyPoint expected, Language language, + const std::string& label_text +); + + +// The game steps fly_to_terarium_point() is made of. +class TerariumFlyActions{ +public: + virtual ~TerariumFlyActions() = default; + + // Open the map and put the cursor on the Central Plaza fly point, + // wherever the player is. "attempt" starts at 1. Return false if the + // Central Plaza fly point wasn't found. The map stays open either way. + virtual bool open_map_at_central_plaza(size_t attempt) = 0; + + // Move the map cursor, then zoom out, as map_move_cursor_fly() does. + virtual void move_cursor(const MapCursorMove& move) = 0; + + // OCR the name label shown next to the cursor. Empty if there is none. + virtual std::string read_fly_point_label() = 0; + + // Fly to the fly point under the cursor. Return false if there was no + // fly point to fly to. + virtual bool fly_here() = 0; + + // Close the map and return to the overworld. + virtual void close_map() = 0; +}; + +// Fly to "destination" from anywhere in the Terarium: open the map, put the +// cursor on Central Plaza, move it to the destination, check the fly point's +// name, then fly. A wrong or missing name closes the map and tries again. +// Return true once flown. Return false, with the map closed, after +// "max_attempts" failures. +bool fly_to_terarium_point( + Logger& logger, TerariumFlyActions& actions, + Language language, TerariumFlyPoint destination, + size_t max_attempts = 3 +); + + + +} +} +} +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 7c0b735017..c1953e57b4 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2342,6 +2342,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_FastTravelDetector.h Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapDetector.cpp Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapDetector.h + Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.cpp + Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapFlyPointLabelReader.h Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapMenuDetector.cpp Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapMenuDetector.h Source/VideoGames/PokemonSV/Inference/Map/PokemonSV_MapPokeCenterIconDetector.cpp @@ -2663,6 +2665,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonSV/Programs/PokemonSV_SaveGame.h Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.cpp Source/VideoGames/PokemonSV/Programs/PokemonSV_Terarium.h + Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.cpp + Source/VideoGames/PokemonSV/Programs/PokemonSV_TerariumFlyPoints.h Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.cpp Source/VideoGames/PokemonSV/Programs/PokemonSV_WorldNavigation.h Source/VideoGames/PokemonSV/Programs/Sandwiches/PokemonSV_IngredientSession.cpp From f6e5ddc42ab31ace745fe4e09dd55e96cb1b5408 Mon Sep 17 00:00:00 2001 From: dayfine Date: Thu, 22 Oct 2026 21:20:13 -0700 Subject: [PATCH 2/2] BBQ tests: default-initialize scenario fields Give TerariumFlyScenario's `central_plaza_found`, `labels`, `fly_point_under_cursor`, `expected_log` a default member initializer (= {}), so test cases that leave them out of a designated initializer compile on the Windows Clang CI job. That job builds with clang-cl /W4 /WX, which turns on -Wmissing-designated-field-initializers: a designated initializer that omits a field without a default member initializer is an error. Apple clang's -Wextra does not enable that warning, so macOS builds miss it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp index 3a5821a6fe..ebb4cc657c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonSV/PokemonSV_Tests.cpp @@ -287,12 +287,12 @@ struct TerariumFlyScenario{ std::string name; TerariumFlyPoint destination = TerariumFlyPoint::COASTAL_PLAZA; Language language = Language::English; - std::vector central_plaza_found; - std::vector labels; - std::vector fly_point_under_cursor; + std::vector central_plaza_found = {}; + std::vector labels = {}; + std::vector fly_point_under_cursor = {}; bool expected_flew = true; std::string expected_steps; - std::vector expected_log; // Checked when not empty. + std::vector expected_log = {}; // Checked when not empty. }; class Test_TerariumFly : public UnitTest{