From ea9712356bded80f6f732c459b5dc168ba01fb4d Mon Sep 17 00:00:00 2001 From: thewhitewolfking <337211553+thewhitewolfking@users.noreply.github.com> Date: Thu, 12 Nov 2026 10:11:03 -0800 Subject: [PATCH] PokemonFRLG: navigation helpers for the Sevii Islands, field moves, items and party order --- .../Inference/Map/PokemonFRLG_MapDetector.cpp | 20 ++ .../Inference/Map/PokemonFRLG_MapDetector.h | 23 ++ .../PokemonFRLG/PokemonFRLG_Navigation.cpp | 280 +++++++++++++++++- .../PokemonFRLG/PokemonFRLG_Navigation.h | 41 ++- 4 files changed, 355 insertions(+), 9 deletions(-) diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp index 575e7f806b..9be820ed35 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.cpp @@ -50,6 +50,26 @@ bool KantoMapDetector::detect(const ImageViewRGB32& screen){ } +FlyMapDetector::FlyMapDetector(Color color) + : m_color(color) + , m_top_box(0.010, 0.010, 0.500, 0.080) // blue (0, 122, 255) + , m_left_box(0.069, 0.110, 0.025, 0.880) // white + , m_right_box(0.902, 0.110, 0.025, 0.880) // white +{} +void FlyMapDetector::make_overlays(VideoOverlaySet& items) const{ + const BoxOption& GAME_BOX = GameSettings::instance().GAME_BOX; + items.add(m_color, GAME_BOX.inner_to_outer(m_top_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_left_box)); + items.add(m_color, GAME_BOX.inner_to_outer(m_right_box)); +} +bool FlyMapDetector::detect(const ImageViewRGB32& screen){ + ImageViewRGB32 game_screen = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + return is_solid(extract_box_reference(game_screen, m_top_box), { 0.0000, 0.3236, 0.6764 }, 0.25, 20) + && is_white(extract_box_reference(game_screen, m_left_box)) + && is_white(extract_box_reference(game_screen, m_right_box)); +} + + } } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h index cfde346338..8075da32d1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Map/PokemonFRLG_MapDetector.h @@ -41,6 +41,29 @@ class KantoMapWatcher : public DetectorToFinder{ {} }; +// Detect the Fly map of any region: Kanto or the Sevii Islands. Checks the +// blue title bar and the white side borders. (KantoMapDetector also checks +// for Kanto's land, so it misses the Sevii Islands maps.) +class FlyMapDetector : public StaticScreenDetector{ +public: + FlyMapDetector(Color color); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool detect(const ImageViewRGB32& screen) override; + +private: + Color m_color; + ImageFloatBox m_top_box; + ImageFloatBox m_left_box; + ImageFloatBox m_right_box; +}; +class FlyMapWatcher : public DetectorToFinder{ +public: + FlyMapWatcher(Color color) + : DetectorToFinder("FlyMapWatcher", std::chrono::milliseconds(250), color) + {} +}; + } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp index a7ddecc44a..8f63a34178 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp @@ -7,6 +7,7 @@ */ #include +#include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" #include "CommonTools/Random.h" @@ -950,7 +951,7 @@ void use_teleport_from_overworld(ConsoleHandle& console, ProControllerContext& c } } -void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& context){ +void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& context, bool sevii_islands){ uint16_t errors = 0; while (true){ if (errors > 5){ @@ -985,17 +986,20 @@ void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& c } // select Fly (2nd option, but maybe other HMs could change this) - KantoMapWatcher map_opened(COLOR_RED); + // The Kanto map is checked for Kanto's land; the Sevii Islands maps only for the map's frame. + KantoMapWatcher kanto_map_opened(COLOR_RED); + FlyMapWatcher sevii_map_opened(COLOR_RED); context.wait_for_all_requests(); pbf_move_left_joystick(context, {0, -1}, 200ms, 300ms); pbf_press_button(context, BUTTON_A, 200ms, 0ms); - ret = wait_until( - console, context, 20000ms, - {map_opened} - ); + if (sevii_islands){ + ret = wait_until(console, context, 20000ms, {sevii_map_opened}); + }else{ + ret = wait_until(console, context, 20000ms, {kanto_map_opened}); + } if (ret < 0){ - console.log("Failed to detect Kanto map"); + console.log(sevii_islands ? "Failed to detect Sevii Islands map" : "Failed to detect Kanto map"); errors++; pbf_mash_button(context, BUTTON_B, 4000ms); continue; @@ -1003,7 +1007,7 @@ void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& c pbf_wait(context, 1000ms); context.wait_for_all_requests(); - console.log("Kanto map detected."); + console.log(sevii_islands ? "Sevii Islands map detected." : "Kanto map detected."); return; } } @@ -1112,6 +1116,266 @@ void fly_from_kanto_map(ConsoleHandle& console, ProControllerContext& context, K } +namespace{ + +// A town's cell on its Sevii Islands Fly map page, and the page corner the +// cursor goes to first. Each page is a 22 x 15 grid and the cursor stops at +// its edges (region_map_layout_sevii_*.h in the decomp), so a corner is an +// exact starting point. Each town uses the corner closest to it. +struct SeviiFlyTarget{ + const char* name; + int x; + int y; + bool corner_right; + bool corner_bottom; +}; +constexpr int SEVII_MAP_WIDTH = 22; +constexpr int SEVII_MAP_HEIGHT = 15; + +SeviiFlyTarget sevii_fly_target(SeviiFlyLocation destination){ + switch (destination){ + case SeviiFlyLocation::oneisland: + return {"One Island", 1, 8, false, true}; + case SeviiFlyLocation::twoisland: + return {"Two Island", 9, 9, false, true}; + case SeviiFlyLocation::threeisland: + return {"Three Island", 18, 12, true, true}; + case SeviiFlyLocation::fourisland: + return {"Four Island", 3, 4, false, false}; + case SeviiFlyLocation::fiveisland: + return {"Five Island", 16, 11, true, true}; + case SeviiFlyLocation::sixisland: + return {"Six Island", 17, 5, true, false}; + case SeviiFlyLocation::sevenisland: + return {"Seven Island", 5, 8, false, true}; + } + return {"", 0, 0, false, false}; +} + +// Move the Fly map cursor one cell. (A held direction repeats every 5 +// frames and can overshoot by a cell; taps don't.) +void fly_map_cursor_tap(ProControllerContext& context, DpadPosition direction, int count){ + for (int i = 0; i < count; i++){ + pbf_press_dpad(context, direction, 50ms, 300ms); + } +} + +} + + +bool fly_from_sevii_map(ConsoleHandle& console, ProControllerContext& context, SeviiFlyLocation destination){ + SeviiFlyTarget target = sevii_fly_target(destination); + console.log(std::string("Flying to ") + target.name + "."); + + // Hold into the corner. From the far side of the page that is 21 cells, + // about 1.8 seconds. + pbf_move_left_joystick( + context, + {target.corner_right ? +1.0 : -1.0, target.corner_bottom ? -1.0 : +1.0}, + 2500ms, 300ms + ); + + // Then tap across to the town. + int corner_x = target.corner_right ? SEVII_MAP_WIDTH - 1 : 0; + int corner_y = target.corner_bottom ? SEVII_MAP_HEIGHT - 1 : 0; + int dx = target.x - corner_x; + int dy = target.y - corner_y; + fly_map_cursor_tap(context, dx > 0 ? DPAD_RIGHT : DPAD_LEFT, dx > 0 ? dx : -dx); + fly_map_cursor_tap(context, dy > 0 ? DPAD_DOWN : DPAD_UP, dy > 0 ? dy : -dy); + + // A picks the town, A again confirms. The screen fades to black as the + // flight starts. + BlackScreenWatcher flying(COLOR_RED); + context.wait_for_all_requests(); + int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 200ms, 2800ms); + pbf_press_button(context, BUTTON_A, 200ms, 2800ms); + }, + { flying } + ); + if (ret < 0){ + // A does nothing on a place that can't be flown to: a town on another + // island group's page, or one not visited yet. Close the Fly map, the + // party menu and the start menu. + console.log("Could not start flying. Is the town on this island group, and has it been visited?", COLOR_RED); + pbf_mash_button(context, BUTTON_B, 4000ms); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + return false; + } + + pbf_wait(context, 8000ms); + context.wait_for_all_requests(); + console.log(std::string("Landed on ") + target.name + "."); + return true; +} + + +void use_field_move_from_overworld(ConsoleHandle& console, ProControllerContext& context, int from_last, int field_move){ + uint16_t errors = 0; + + while (true){ + if (errors > 5){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "use_field_move_from_overworld(): Failed to open the Pokemon's menu 5 times in a row.", + console + ); + } + + open_party_menu_from_overworld(console, context); + // From slot 1, up goes to CANCEL, then to the last slot, then upward. + for (int i = 0; i < 2 + from_last; i++){ + pbf_move_left_joystick(context, {0, +1}, 200ms, 300ms); + } + + PartySelectionWatcher menu_opened(COLOR_RED); + context.wait_for_all_requests(); + int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 200ms, 1800ms); + }, + { menu_opened } + ); + if (ret < 0){ + console.log("Failed to open the Pokemon's menu."); + errors++; + pbf_mash_button(context, BUTTON_B, 3000ms); + continue; + } + + // The menu lists SUMMARY, then the field moves in the order the + // Pokemon knows them, then SWITCH, ITEM and CANCEL. + for (int i = 0; i < 1 + field_move; i++){ + pbf_move_left_joystick(context, {0, -1}, 200ms, 300ms); + } + pbf_press_button(context, BUTTON_A, 200ms, 800ms); + context.wait_for_all_requests(); + console.log("Used field move " + std::to_string(field_move + 1) + " of the Pokemon " + std::to_string(from_last) + " from the last."); + return; + } +} + + +void start_surfing(ConsoleHandle& console, ProControllerContext& context){ + for (int attempt = 0; attempt < 2; attempt++){ + WhiteDialogWatcher question(COLOR_RED); + context.wait_for_all_requests(); + pbf_press_button(context, BUTTON_A, 200ms, 0ms); + if (wait_until(console, context, 3000ms, { question }) < 0){ + console.log("No Surf question. Trying again."); + continue; + } + // "The water is dyed a deep blue... Would you like to SURF?", then + // YES / NO with YES selected. + pbf_wait(context, 2000ms); + pbf_press_button(context, BUTTON_A, 200ms, 1500ms); + // "X used SURF!", the Pokemon's picture, then the player hops on. + pbf_press_button(context, BUTTON_B, 200ms, 3500ms); + context.wait_for_all_requests(); + console.log("Started surfing."); + return; + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "start_surfing(): Couldn't start surfing. Is the player facing water, and does a party Pokemon know Surf?", + console + ); +} + + +void use_item_from_items_pocket(ConsoleHandle& console, ProControllerContext& context, uint8_t position){ + open_bag_from_overworld(console, context); + pbf_wait(context, 500ms); + + // The Items pocket is the leftmost and pockets don't wrap. The item list + // doesn't wrap either: holding up reaches the first item. + for (int i = 0; i < 3; i++){ + pbf_press_dpad(context, DPAD_LEFT, 120ms, 400ms); + } + pbf_press_dpad(context, DPAD_UP, 3000ms, 200ms); + for (uint8_t i = 1; i < position; i++){ + pbf_press_dpad(context, DPAD_DOWN, 120ms, 250ms); + } + + // A opens the item's menu with USE first; A again uses it. + pbf_press_button(context, BUTTON_A, 200ms, 800ms); + pbf_press_button(context, BUTTON_A, 200ms, 1500ms); + + // Close the message, the bag and the start menu. + pbf_mash_button(context, BUTTON_B, 4000ms); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + console.log("Used item " + std::to_string(position) + " of the Items pocket."); +} + + +void swap_party_slots(ConsoleHandle& console, ProControllerContext& context, uint8_t first, uint8_t second){ + if (first < 1 || second > 6 || first >= second){ + throw InternalProgramError( + &console.logger(), PA_CURRENT_FUNCTION, + "swap_party_slots(): Invalid slots: " + std::to_string(first) + " and " + std::to_string(second) + ); + } + open_party_menu_from_overworld(console, context); + + // Slot 1 is on the left; slots 2 to 6 are a column on the right. The + // cursor starts on slot 1. + if (first > 1){ + pbf_press_dpad(context, DPAD_RIGHT, 150ms, 400ms); + for (uint8_t i = 2; i < first; i++){ + pbf_press_dpad(context, DPAD_DOWN, 150ms, 400ms); + } + } + + PartySelectionWatcher menu_opened(COLOR_RED); + context.wait_for_all_requests(); + int ret = run_until( + console, context, + [](ProControllerContext& context){ + pbf_press_button(context, BUTTON_A, 200ms, 1800ms); + }, + { menu_opened } + ); + if (ret < 0){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "swap_party_slots(): The Pokemon's menu did not open.", + console + ); + } + + // SWITCH is third from the bottom (SWITCH, ITEM, CANCEL), whatever field + // moves are listed above it, and the menu wraps around: up 3 from SUMMARY. + for (int i = 0; i < 3; i++){ + pbf_move_left_joystick(context, {0, +1}, 200ms, 300ms); + } + pbf_press_button(context, BUTTON_A, 200ms, 800ms); + + // Pick the second Pokemon. + if (first == 1){ + pbf_press_dpad(context, DPAD_RIGHT, 150ms, 400ms); + for (uint8_t i = 2; i < second; i++){ + pbf_press_dpad(context, DPAD_DOWN, 150ms, 400ms); + } + }else{ + for (uint8_t i = first; i < second; i++){ + pbf_press_dpad(context, DPAD_DOWN, 150ms, 400ms); + } + } + pbf_press_button(context, BUTTON_A, 200ms, 2500ms); + + // Close the party menu and the start menu. + pbf_mash_button(context, BUTTON_B, 3000ms); + pbf_wait(context, 800ms); + context.wait_for_all_requests(); + console.log("Swapped party slots " + std::to_string(first) + " and " + std::to_string(second) + "."); +} + + void enter_leave_pokecenter(ConsoleHandle& console, ProControllerContext& context, bool leave){ uint16_t errors = 0; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h index b896431aa1..022ab4b61b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h @@ -47,6 +47,20 @@ enum class KantoFlyLocation{ indigoplateau, }; +// Towns on the Sevii Islands Fly map. The Fly map only shows the island group +// the player is in (it has no SWITCH button), so each can only be reached from +// its own group: One, Two and Three Island; Four and Five Island; Six and +// Seven Island. +enum class SeviiFlyLocation{ + oneisland, + twoisland, + threeisland, + fourisland, + fiveisland, + sixisland, + sevenisland, +}; + // Press A+B+Select+Start at the same time to soft reset, then re-enters the game. // There are two random waits, one before pressing start and another after loading in the game. @@ -97,11 +111,36 @@ void use_sweet_scent_from_overworld(ConsoleHandle& console, ProControllerContext void use_teleport_from_overworld(ConsoleHandle& console, ProControllerContext& context); // Navigates to the fly map. Assumes that Fly is usable and the last member of your party has it learned -void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& context); +// Set sevii_islands to true when the player is on the Sevii Islands, whose Fly map is not the Kanto map. +void open_fly_map_from_overworld(ConsoleHandle& console, ProControllerContext& context, bool sevii_islands = false); // Starting from the Kanto Fly map, fly to a specified location. void fly_from_kanto_map(ConsoleHandle& console, ProControllerContext& context, KantoFlyLocation destination); +// Starting from the Sevii Islands Fly map, fly to a specified town. The town must be on the +// island group the player is in (see SeviiFlyLocation). Returns false if the flight didn't start +// (wrong island group, or a town not visited yet), after closing the menus. +bool fly_from_sevii_map(ConsoleHandle& console, ProControllerContext& context, SeviiFlyLocation destination); + +// From the overworld, open the party menu and use a field move (Cut, Flash, Strength, ...). +// from_last: the user's distance from the last party slot (0 = the last Pokemon). +// field_move: which of the user's field moves (0 = the first one listed under SUMMARY). +// Returns once the move is chosen: the party menu closes and the move's animation or message starts. +void use_field_move_from_overworld(ConsoleHandle& console, ProControllerContext& context, int from_last, int field_move = 0); + +// Facing water from land, start surfing: talk to the water and answer YES. +// Any party Pokemon that knows Surf will use it. +void start_surfing(ConsoleHandle& console, ProControllerContext& context); + +// From the overworld, use the item in the given position of the Items pocket of the bag +// (1 = the first item), e.g. a Max Repel. The item is not checked: the position must be right. +// Ends back in the overworld. +void use_item_from_items_pocket(ConsoleHandle& console, ProControllerContext& context, uint8_t position); + +// From the overworld, swap two party Pokemon (slots 1 to 6, first < second) with SWITCH. +// Ends back in the overworld. +void swap_party_slots(ConsoleHandle& console, ProControllerContext& context, uint8_t first, uint8_t second); + // Enter a PokeCenter. Assumes the player is standing in front of its door void enter_pokecenter(ConsoleHandle& console, ProControllerContext& context);