diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp new file mode 100644 index 0000000000..c57672d043 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp @@ -0,0 +1,246 @@ +/* Overworld Recovery Walk + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTools/ImageDiff.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "CommonTools/Async/InferenceRoutines.h" +#include "CommonTools/VisualDetectors/BlackScreenDetector.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_OverworldRecoveryWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace OverworldWalkSteps; + + +namespace{ + +// An open leg stops at its first interruption: walking on from how far it +// got went wrong too often on the console. The timing only narrows where +// the player is, and the route finds them again at a wall. See +// walk_overworld_route_with_recovery(). +Pokemon::LegSpec leg_spec(const RouteStep& step, MoveSpeed speed){ + Pokemon::LegSpec leg; + leg.direction = step.direction; + leg.tiles = step.tiles; + leg.to_wall = step.to_wall; + leg.tile_time = tile_time(step, speed); + leg.stop_on_interrupt = !step.to_wall; + leg.stop_if_blocked = step.to_wall; + return leg; +} + +// One step of a route, for a player facing `facing`. If an open leg +// stopped part-way, returns how far the timing puts the player along it. +std::optional walk_step( + ConsoleHandle& console, ProControllerContext& context, + const RouteStep& step, std::optional facing, + const OverworldInterruptSet& interrupts, MoveSpeed speed +){ + switch (step.action){ + case RouteAction::WALK:{ + // Turning first, with a tap, makes the hold start from a standstill + // facing the right way: holding through a turn lost a tile on the + // console, while holds from a standstill were exact over 15 tiles. + if (facing != step.direction){ + pbf_press_dpad(context, dpad(step.direction), TURN_TAP, TURN_SETTLE); + } + if (const std::string note = speed_note(step, speed); !note.empty()){ + console.log(note); + } + const Pokemon::LegResult result = Pokemon::walk_leg( + console, context, leg_mover(step, speed), step.wild ? interrupts.wild : interrupts.other, + leg_spec(step, speed), MAX_INTERRUPTIONS_PER_LEG + ); + if (result.stopped){ + return walked_near(step, result.tiles, tile_time(step, speed)); + } + if (step.warps){ + wait_out_warp(console, context, step); + static_assert(AFTER_WARP_BATTLE_WATCH >= AFTER_WARP); + check_warp_was_no_battle(console, context, step); + } + break; + } + case RouteAction::SURF: + start_surf(console, context, step.direction, facing == step.direction); + break; + case RouteAction::FACE: + pbf_press_dpad(context, dpad(step.direction), FACE_HOLD, STEP_RELEASE); + break; + } + return std::nullopt; +} + +// walk_step(), then a watch for what its last step set off. Not DONE if +// the player may have stopped short of the step's end: an open leg +// stopped, or something seen after the hold, which may have stopped it +// before the end (after a warp, it was checked for already). +StepOutcome walk_and_watch( + ConsoleHandle& console, ProControllerContext& context, + const RouteStep& step, std::optional facing, + const OverworldInterruptSet& interrupts, MoveSpeed speed +){ + if (const std::optional walked = walk_step(console, context, step, facing, interrupts, speed)){ + return StepOutcome{.end = StepEnd::STOPPED, .walked = walked}; + } + if (step.action == RouteAction::FACE){ + return StepOutcome{}; + } + const Milliseconds watch = step.wild ? AFTER_WILD_LEG_WATCH : AFTER_LEG_WATCH; + const size_t late = Pokemon::clear_late_interrupts(console, context, step.wild ? interrupts.wild : interrupts.other, watch, MAX_INTERRUPTIONS_PER_LEG); + // After a warp it was checked for already. + const bool done = late == 0 || step.warps || step.action != RouteAction::WALK; + return StepOutcome{.end = done ? StepEnd::DONE : StepEnd::LATE, .walked = std::nullopt}; +} + +// Holds on along `step`, stopped part-way off the grass, until its warp's +// fade. Off the grass only a message stops a leg (a trainer who saw the +// player, a Repel wearing off), and the player then stands on the leg's +// line, so holding on reaches the warp however far they got (console B, +// 2026-11-01: Camper Liam stopped "south 9 (warp)" out of Pewter Gym). +// After a trainer the player faces them, so this turns first. +void hold_to_warp( + ConsoleHandle& console, ProControllerContext& context, + const RouteStep& step, const OverworldInterrupts& interrupts, MoveSpeed speed +){ + const Pokemon::LegMover mover = leg_mover(step, speed); + for (size_t count = 0; count <= MAX_INTERRUPTIONS_PER_LEG; count++){ + BlackScreenWatcher fade(COLOR_RED); + std::vector> watchers; + std::vector callbacks{fade}; + for (const auto& interrupt : interrupts){ + watchers.emplace_back(interrupt->make_watcher()); + callbacks.emplace_back(*watchers.back()); + } + context.wait_for_all_requests(); + const int ret = run_until( + console, context, + [&](ProControllerContext& context){ + pbf_press_dpad(context, dpad(step.direction), TURN_TAP, TURN_SETTLE); + mover(context, step.direction, tile_time(step, speed) * (step.tiles + 2)); + }, + callbacks + ); + if (ret == 0){ + wait_out_warp(console, context, step); + check_warp_was_no_battle(console, context, step); + return; + } + if (ret < 0){ + break; + } + console.log("Route: " + interrupts[ret - 1]->name() + " on the way to the warp.", COLOR_ORANGE); + interrupts[ret - 1]->clear(console, context); + } + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "walk_overworld_route(): no fade holding on toward the warp to " + step.end.map + ".", + console + ); +} + +// A trainer who sees the player stops them, a "!" shows over the trainer, +// the trainer walks up, then speaks. The leg's hold may run on past the +// tile the player was stopped on, so the watch counts from the hold's end, +// with room to spare: on Route 3 (console B, 2026-11-02) Lass Robin's +// message showed after the 500ms watch after the leg had ended. Not +// measured on the console yet. +constexpr Milliseconds TRAINER_NOTICE(1500); +constexpr Milliseconds TRAINER_SPEAKS(1000); + +// After `step`, which crosses a trainer's sight: watches for the trainer +// to walk up `approach_tiles` tiles and speak, and clears what shows. +StepEnd watch_for_trainer( + ConsoleHandle& console, ProControllerContext& context, + const RouteStep& step, int approach_tiles, const OverworldInterruptSet& interrupts +){ + const Milliseconds watch = TRAINER_NOTICE + TILE_TIME * approach_tiles + TRAINER_SPEAKS; + console.log(std::format("Route: {} crosses a trainer's sight; watching {} ms for them.", to_string(step), watch.count())); + const size_t seen = Pokemon::clear_late_interrupts( + console, context, step.wild ? interrupts.wild : interrupts.other, watch, MAX_INTERRUPTIONS_PER_LEG + ); + return seen == 0 ? StepEnd::DONE : StepEnd::LATE; +} + +// TODO(forest drift, 2026-11-01): diagnostic; remove or turn into a recovery once the cause is known. +// How much the game screen's top quarter, clear of the player in the +// middle, may change when a tap toward a wall only bumps the player (the +// player's own animation, an animated tile). A step scrolls the whole map +// by a tile. Not yet measured on the console: a guess on the safe side. +constexpr double WALL_TAP_MOVED_RMSD = 20.0; + +// Taps once toward the wall `step` ended at (the player faces it, so a tap +// steps if it can) and says whether the map scrolled: whether the player +// moved instead of bumping into the wall. +bool moved_past_wall(ConsoleHandle& console, ProControllerContext& context, const RouteStep& step){ + context.wait_for_all_requests(); + VideoSnapshot before = console.video().snapshot(); + pbf_press_dpad(context, dpad(step.direction), TURN_TAP, TILE_TIME + TURN_SETTLE); + context.wait_for_all_requests(); + VideoSnapshot after = console.video().snapshot(); + if (!before || !after){ + return false; + } + const ImageFloatBox game = GameSettings::instance().GAME_BOX; + const ImageFloatBox band(game.x, game.y, game.width, game.height * 0.25); + const double change = ImageMatch::pixel_RMSD(extract_box_reference(before, band), extract_box_reference(after, band)); + console.log(std::format("Position check: a tap toward the wall of {} changed the screen by {:.1f}.", to_string(step), change)); + return change > WALL_TAP_MOVED_RMSD; +} + +} // namespace + + +void walk_overworld_route_with_recovery( + ConsoleHandle& console, ProControllerContext& context, + const std::vector& route, + const RoutePlanning& planning, + std::optional facing, + const OverworldInterruptSet& interrupts, + MoveSpeed speed +){ + if (route.empty()){ + return; + } + if (!facing){ + facing = read_facing(console, context); + } + RouteCalls calls; + calls.walk = [&](const RouteStep& step, std::optional from_facing){ + clear_open_message(console, context, step.wild ? interrupts.wild : interrupts.other, step); + check_overworld(console, context, step); + return walk_and_watch(console, context, step, from_facing, interrupts, speed); + }; + calls.hold_to_warp = [&](const RouteStep& step){ + hold_to_warp(console, context, step, interrupts.other, speed); + }; + calls.log = [&](const std::string& message){ console.log(message, COLOR_ORANGE); }; + calls.fail = [&](const std::string& message){ + OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, message, console); + }; + calls.moved_past_wall = [&](const RouteStep& step){ + return moved_past_wall(console, context, step); + }; + calls.watch_for_trainer = [&](const RouteStep& step, int approach_tiles){ + return watch_for_trainer(console, context, step, approach_tiles, interrupts); + }; + run_route(route, facing, &planning, calls); + context.wait_for_all_requests(); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h new file mode 100644 index 0000000000..5f24a07949 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h @@ -0,0 +1,69 @@ +/* Overworld Recovery Walk + * + * From: https://github.com/PokemonAutomation/ + * + * The console side of run_route() (PokemonFRLG_OverworldRecovery.h): walks + * a route with the walker's steps (PokemonFRLG_OverworldWalk.h), except + * that an open leg stops at its first interruption, and the route finds + * the player again around where the timing puts them, instead of walking + * on from a count. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldRecoveryWalk_H +#define PokemonAutomation_PokemonFRLG_OverworldRecoveryWalk_H + +#include +#include +#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h" +#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" +#include "PokemonFRLG_OverworldRecovery.h" +#include "PokemonFRLG_OverworldRoute.h" +#include "PokemonFRLG_OverworldWalk.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// Walks `route`, planned with `planning`, at `speed`, as +// walk_overworld_route() does, from the same start: standing still on the +// overworld, not on the bike, facing `facing` (read from the screen if not +// given). +// +// When a battle or a message stops an open leg part-way, where the player +// is isn't guessed: the stop's timing narrows it to a few tiles of the leg +// (walked_near()), from which run_route() finds them again with +// `planning`'s maps and costs, and walks the rest of the route as planned. +// +// After a leg that crosses a trainer's sight it watches for the trainer, +// long enough for them to walk up and speak, and hands what shows to +// `interrupts`; where the maps place the stop, the route goes on from +// there (run_route()). +// +// After each leg to a wall it taps once more toward the wall, and logs +// whether the screen moved (a diagnostic; RoutePlanning::wall_check SKIP +// turns it off, as the Fast checks level does). +// +// A message open before a step is handed to `interrupts` once +// (clear_open_message()). Throws OperationFailedException if a menu or +// message is still open before a step, a warp's fade or the Surf prompt +// doesn't show, a warp's fade turns out to be a wild battle's, a leg is +// interrupted too many times, or the player can't be found again from +// every tile they may be on. A wild battle that starts as the last leg +// ends is cleared before it returns. +void walk_overworld_route_with_recovery( + ConsoleHandle& console, ProControllerContext& context, + const std::vector& route, + const RoutePlanning& planning, + std::optional facing = std::nullopt, + const OverworldInterruptSet& interrupts = default_overworld_interrupts(), + MoveSpeed speed = MoveSpeed::WALK +); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 29c518b22f..4fa57e85d2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -13,6 +13,7 @@ #include "Pokemon/AutoStory/Pokemon_TilePose.h" #include "Pokemon/Navigation/Pokemon_RouteLog.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldMaps.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h" #include "PokemonFRLG_AutoStoryBattleData.h" @@ -93,6 +94,12 @@ void log_arrival(StoryEnv& env, const std::string& label, const std::string& map env.console.log(std::string(Pokemon::ROUTE_LOG_TAG) + Pokemon::route_arrived_line(label, {{map, tile}})); } +// The walker's check that each leg to a wall ended at it (a diagnostic): +// with Thorough checks only, as Fast trusts the walk. +WallCheck wall_check(const StoryEnv& env){ + return checks_screen(env.checks) ? WallCheck::TAP : WallCheck::SKIP; +} + Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step){ const std::vector tiles = Pokemon::tiles_of(state.pose); if (tiles.size() != 1 || state.pose.certainty != Pokemon::PoseCertainty::TILE){ @@ -119,7 +126,10 @@ Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step){ return Pokemon::outcome_failed("no_route", to_string(start) + " to " + to_string(goal.position)); } const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); - walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step)); + // Planning lets the walker recover a stopped open leg: it plans back to + // the leg's end, then walks the rest as given. + const RoutePlanning planning{.maps = &story_maps(), .costs = default_route_costs(), .start = start, .label = step.name, .wall_check = wall_check(env)}; + walk_overworld_route_with_recovery(env.console, env.context, *route, planning, facing, interrupts, move_speed(step)); log_arrival(env, step.name, target->map, target->tile); const std::optional end_facing = route->empty() ? facing : std::optional(route->back().facing_after); return Pokemon::outcome_ok(to_pose(StoryPose{target->map, target->tile, end_facing})); @@ -140,7 +150,10 @@ Outcome walk_legs(StoryEnv& env, RunState& state, const StoryStep& step){ return Pokemon::outcome_failed("bad_legs", to_string(start)); } const OverworldInterruptSet interrupts = story_interrupts(env.supplies, arg(step, "fight_wild"), env.checks, env.language); - walk_overworld_route(env.console, env.context, *route, facing, interrupts, move_speed(step)); + // Planning lets the walker recover a stopped open leg: it plans back to + // the leg's end, then walks the rest as given. + const RoutePlanning planning{.maps = &story_maps(), .costs = default_route_costs(), .start = start, .label = step.name, .wall_check = wall_check(env)}; + walk_overworld_route_with_recovery(env.console, env.context, *route, planning, facing, interrupts, move_speed(step)); log_arrival(env, step.name, route->back().end.map, route->back().end.point); return Pokemon::outcome_ok(to_pose(StoryPose{route->back().end.map, route->back().end.point, route->back().facing_after})); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 647ca0f7e2..41849b07a5 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2010,6 +2010,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor_Tests.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h