diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp index fb9e7a89f5..ae35f4b51b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include "Common/Cpp/Exceptions.h" #include "PokemonBDSP_AutoStory_StoryWorld.h" @@ -294,6 +295,37 @@ std::optional step(const StoryWorld& world, const TilePos& from, Direct return std::nullopt; } +std::vector leg_path(const StoryWorld& grid, TilePos from, Direction direction, size_t tiles){ + std::vector path; + size_t travelled = 0; + while (travelled < tiles){ + const std::optional next = step(grid, from, direction); + if (!next){ + break; + } + travelled += std::abs(next->x - from.x) + std::abs(next->y - from.y); + from = *next; + path.push_back(from); + } + return path; +} + +bool warp_within(const StoryWorld& world, const std::vector& from, Direction direction, size_t reach){ + for (TilePos pos : from){ + for (size_t i = 0; i < reach; i++){ + if (world.tile(pos.next(direction)) == TileKind::DOOR){ + return true; + } + const std::optional next = step(world, pos, direction); + if (!next){ + break; + } + pos = *next; + } + } + return false; +} + // Someone standing in the way stops us too, but is no wall to stop against: // they may turn and talk, or walk off (r29 ran east into Mom, who talked). bool at_wall(const StoryWorld& world, const TilePos& pos, Direction direction){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h index 9cf790e24f..6940a5be4d 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_StoryWorld.h @@ -128,6 +128,13 @@ std::optional step(const StoryWorld& world, const Pokemon::Til // in place. Doors and tiles off the maps are not walls: walking into a // door enters the building, and ground off the maps may be an exit. bool at_wall(const StoryWorld& world, const Pokemon::TilePos& pos, Pokemon::Direction direction); +// The tiles the player passes when walking `tiles` tiles from `from`, the +// first one after `from` first. Stops early where the player is stopped. +std::vector leg_path(const StoryWorld& grid, Pokemon::TilePos from, Pokemon::Direction direction, size_t tiles); +// Whether a warp (a door's tile) lies within `reach` tiles `direction` of +// one of `from`, nothing stopping the player before it: where a walk that +// ran on past its leg's end may have gone through (#268 slice 2). +bool warp_within(const StoryWorld& world, const std::vector& from, Pokemon::Direction direction, size_t reach); // "Route 201 (45, 3)", or "world (x, y)" if no map holds it. std::string describe(const StoryWorld& world, const Pokemon::TilePos& pos); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp index 5af1245470..64f2ec71dc 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp @@ -86,9 +86,9 @@ class RecoveryStepIO : public StoryStepIO{ virtual void fight(const std::string&) override{ throw GridSimFailure{"unexpected trainer"}; } virtual void walk_until_stopped(Direction, size_t, const std::string&) override{ throw GridSimFailure{"unexpected blind hold"}; } virtual bool run_until_map(Direction, Milliseconds, const std::string&, const std::string&) override{ throw GridSimFailure{"unexpected banner walk"}; } - virtual bool run_into_door(Direction, bool, Milliseconds = Milliseconds(12000), Milliseconds = Milliseconds(0)) override{ throw GridSimFailure{"unexpected door"}; } + virtual WarpSeen run_into_door(Direction, bool, Milliseconds = Milliseconds(12000), Milliseconds = Milliseconds(0), const std::string& = "") override{ throw GridSimFailure{"unexpected door"}; } virtual std::optional tap_to_banner(Direction, const std::string&, size_t) override{ throw GridSimFailure{"unexpected banner"}; } - virtual void settle(Button, const std::string&, bool) override{ throw GridSimFailure{"unexpected scene"}; } + virtual void settle(Button, const std::string&, bool, const std::string&) override{ throw GridSimFailure{"unexpected scene"}; } private: RouteIO& m_route; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp index 80e5d6bbe8..47c25de07f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.cpp @@ -241,9 +241,9 @@ Outcome StoryExecutor::heal(RunState&, const StoryStep& step){ // Out the same way: south until the fade. The walk out's indoor check // read "not a room" a step from the counter and stopped inside (r50, r59). route_turn(stream(), context(), Direction::SOUTH); - const bool out = m_io.run_into_door(Direction::SOUTH, m_walks.can_run()); + const bool out = m_io.run_into_door(Direction::SOUTH, m_walks.can_run()).any(); stream().log(std::format("[{}] {}: south out of the Center: {}.", m_checkpoint, step.name, out ? "faded" : "no fade yet")); - m_io.settle(BUTTON_B, label_of(step), !out); + m_io.settle(BUTTON_B, label_of(step), !out, ""); // r143a's heal said it had walked out while still inside. context().wait_for_all_requests(); VideoSnapshot screen = stream().video().snapshot(); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp index 72d05f3667..1493ad8daa 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.cpp @@ -230,6 +230,25 @@ const std::string& story_banner_name(const std::string& map){ return banner_name(map); } +std::string story_arrival_banner(const std::string& from, const std::string& to){ + // The named places, by the place each map is part of: the mine's two floors are one. + static const std::map caves{ + {"OreburghGate", "OreburghGate"}, + {"OreburghMine", "OreburghMine"}, + {"OreburghMineB1F", "OreburghMine"}, + {"RavagedPath", "RavagedPath"}, + }; + const auto place = [](const std::string& map) -> std::string{ + if (banner_names().contains(map)){ + return banner_name(map); + } + const auto cave = caves.find(map); + return cave == caves.end() ? "" : cave->second; + }; + const std::string there = place(to); + return there.empty() || there == place(from) ? "" : story_banner_name(to); +} + const StoryWorld* story_plan_world(const std::string& from, const std::string& to){ for (const PlanWorld& world : plan_worlds()){ if (world.maps.contains(from) && world.maps.contains(to)){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h index 62006d2cf3..a6f0dbbfd0 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryPlaces.h @@ -33,6 +33,11 @@ const StoryWorld* story_plan_world(const std::string& from, const std::string& t // The location banner of the JSON map `map` ("Route 204" for both of its // halves), or `map` itself for one no banner list names. const std::string& story_banner_name(const std::string& map); +// The banner (story_banner_name()) a warp from the JSON map `from` onto +// `to` shows, or "" if none: arriving in another town, route or cave shows +// its name (out of the Gym, Oreburgh City's), arriving in a house, a lab, +// a school or another floor of the same cave shows none. +std::string story_arrival_banner(const std::string& from, const std::string& to); // The tile `local` of the map `map` in `world`. Pokemon::TilePos story_to_world(const StoryWorld& world, const std::string& map, const Pokemon::TilePos& local); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp index 43df0cf73f..46b0279e63 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp @@ -6,6 +6,7 @@ #include #include +#include #include "Common/Cpp/PrettyPrint.h" #include "Common/Cpp/Filesystem/Filesystem.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" @@ -50,16 +51,41 @@ namespace{ // A step or scene that ends on another map (`fade`) must fade on the way, // and only the quiet after the fade counts: r30's scene went quiet for 5 s // before the fade home, and r28 "went out" a tile beside the door and -// walked a whole route in the house. No fade is a map mismatch. +// walked a whole route in the house. No fade is a map mismatch. The far +// map's banner `banner`, if it has one, counts as the fade: a fade the run +// missed is over, but its banner comes after it (#268 slice 1). constexpr double FADE_DARKNESS = 75; // brightness 0 to 255; cave fades reach ~45..64, r24's dim fade goes darker -void settle(VideoStream& stream, ProControllerContext& context, Button button, const std::string& label, bool fade){ +// What settle() has seen of the warp so far: the darkest frame, and the banner. +class SettleWarp{ +public: + explicit SettleWarp(const std::string& banner) + : m_banner(banner) + {} + void look(const ImageViewRGB32& frame, WallClock now){ + m_darkest = std::min(m_darkest, image_average(frame).sum() / 3); + m_banner_seen |= !m_banner.empty() && location_banner_matches(m_reads.read(frame, now), m_banner); + } + bool seen() const{ + return m_darkest < FADE_DARKNESS || m_banner_seen; + } + std::string describe() const{ + return std::format("darkest frame {:.0f}{}", m_darkest, m_banner_seen ? ", banner read" : ""); + } + +private: + const std::string& m_banner; + double m_darkest = 255; + LocationBannerReads m_reads; + bool m_banner_seen = false; +}; +void settle(VideoStream& stream, ProControllerContext& context, Button button, const std::string& label, bool fade, const std::string& banner){ const WallClock none = WallClock::max(); const WallClock deadline = current_time() + 120s; ShortDialogDetector dialog; OverworldDetector overworld; BattleMenuDetector battle_menu(BattleType::STANDARD); WallClock quiet_since = none, calm_since = none; - double darkest = 255; + SettleWarp warp(banner); VideoSnapshot previous; while (current_time() < deadline){ context.wait_for_all_requests(); @@ -71,21 +97,22 @@ void settle(VideoStream& stream, ProControllerContext& context, Button button, c continue; } if (frame){ - darkest = std::min(darkest, image_average(*frame.frame).sum() / 3); + warp.look(*frame.frame, now); } + const bool warped = !fade || warp.seen(); const bool talking = frame && dialog.detect(frame); const bool still = frame && previous && walk_screen_change(*previous.frame, *frame.frame) < WALK_TAP_BLOCKED_CHANGE; previous = frame; - const bool calm = !talking && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS); + const bool calm = !talking && overworld.detect(frame) && warped; calm_since = calm ? (calm_since == none ? now : calm_since) : none; if (calm && now - calm_since >= 15000ms){ - stream.log(std::format("[Story] {}: settled on calm overworld; darkest frame {:.0f}.", label, darkest)); + stream.log(std::format("[Story] {}: settled on calm overworld; {}.", label, warp.describe())); return; } - if (!talking && still && overworld.detect(frame) && (!fade || darkest < FADE_DARKNESS)){ + if (!talking && still && overworld.detect(frame) && warped){ quiet_since = quiet_since == none ? now : quiet_since; if (now - quiet_since >= 5000ms){ - stream.log(std::format("[Story] {}: settled; darkest frame {:.0f}.", label, darkest)); + stream.log(std::format("[Story] {}: settled; {}.", label, warp.describe())); return; } }else{ @@ -94,8 +121,8 @@ void settle(VideoStream& stream, ProControllerContext& context, Button button, c pbf_press_button(context, talking ? button : BUTTON_B, 80ms, 1200ms); } OperationFailedExceptionWithScreenshot::fire(ErrorReportMode::SEND_ERROR_REPORT, - fade && darkest >= FADE_DARKNESS - ? label + ": no fade within 120 s, so we are not where the step goes (map mismatch)." + fade && !warp.seen() + ? label + ": no fade or banner within 120 s, so we are not where the step goes (map mismatch)." : label + ": not back on a quiet overworld within 120 s.", stream); } @@ -140,25 +167,32 @@ class DarkFrom : public VisualInferenceCallback{ }; // How one hold toward a door ended: run_until()'s index (0 dark, 1 white, -// 2 a line, 3 a wild battle; -1 time up), and when the screen last went dark. +// 2 a line, 3 a wild battle, 4 the far map's banner; -1 time up), when the +// screen last went dark, and the banner read. struct DoorHoldEnd{ int ret; WallClock went_dark; + std::string banner; }; -DoorHoldEnd hold_into_door(VideoStream& stream, ProControllerContext& context, DpadPosition dpad, bool run, Milliseconds hold, Milliseconds dark_from){ +DoorHoldEnd hold_into_door(VideoStream& stream, ProControllerContext& context, DpadPosition dpad, bool run, Milliseconds hold, Milliseconds dark_from, const std::string& banner){ BlackScreenWatcher dark(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, 3 * FADE_DARKNESS, 60, BlackScreenWatcher::FinderType::PRESENT, 50ms); DarkFrom late_dark(dark, current_time() + dark_from); WhiteScreenWatcher white(COLOR_BLUE, {0.1, 0.1, 0.8, 0.8}, WhiteScreenWatcher::FinderType::PRESENT, 60ms); ShortDialogWatcher dialog(COLOR_YELLOW); BattleMenuWatcher battle(BattleType::STANDARD, COLOR_YELLOW); + LocationBannerWatcher far_banner(banner); + std::vector watchers{{late_dark}, {white}, {dialog}, {battle}}; + if (!banner.empty()){ + watchers.emplace_back(far_banner); + } const int ret = run_until(stream, context, [&](ProControllerContext& context){ if (run){ ssf_press_button(context, BUTTON_B, 0ms, hold, 0ms); } pbf_press_dpad(context, dpad, hold, 0ms); - }, {{late_dark}, {white}, {dialog}, {battle}}); - return {.ret = ret, .went_dark = late_dark.went_dark()}; + }, watchers); + return {.ret = ret, .went_dark = late_dark.went_dark(), .banner = ret == 4 ? far_banner.seen() : ""}; } // Wild battles a run into a door flees and runs on after. @@ -171,11 +205,13 @@ constexpr size_t DOOR_RUN_BATTLES = 3; // 2026-11-08: dark at 1.8 s, halfway down the mine's entrance hall). A wild // battle on the way is fled, and the run goes on for the time it had left: // only the running counts, up to the battle's fade. -bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from){ +// The far map's banner `banner` shows after the fade, so it ends the run +// only when the fade went unseen (#268 slice 1). +WarpSeen run_into_door(VideoStream& stream, ProControllerContext& context, Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from, const std::string& banner){ Milliseconds left = timeout; for (size_t battles = 0;; battles++){ const WallClock start = current_time(); - const DoorHoldEnd end = hold_into_door(stream, context, dpad_toward(direction), run, left, dark_from); + const DoorHoldEnd end = hold_into_door(stream, context, dpad_toward(direction), run, left, dark_from, banner); if (end.ret == 3){ const WallClock stopped = std::min(end.went_dark, current_time()); const Milliseconds ran = std::chrono::duration_cast(stopped - start) - ROUTE_START_DELAY; @@ -187,7 +223,7 @@ bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction if (battles < DOOR_RUN_BATTLES && left > 0ms){ continue; } - return false; + return {}; } if (end.ret == 0){ stream.log("[Story] run_into_door: black screen fade detected."); @@ -195,6 +231,8 @@ bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction stream.log("[Story] run_into_door: white screen fade detected."); }else if (end.ret == 2){ stream.log("[Story] run_into_door: dialog detected."); + }else if (end.ret == 4){ + stream.log("[Story] run_into_door: no fade seen, but the banner \"" + end.banner + "\" read."); }else if (end.went_dark != WallClock::max()){ // Dark only before dark_from: early, or the door's fade itself. stream.log(std::format("[Story] run_into_door: timed out; dark {} ms in, before dark_from {} ms.", @@ -202,7 +240,10 @@ bool run_into_door(VideoStream& stream, ProControllerContext& context, Direction }else{ stream.log("[Story] run_into_door: timed out without detecting fade."); } - return end.ret == 0 || end.ret == 1; + return { + .fade = end.ret == 0 ? WarpFade::BLACK : end.ret == 1 ? WarpFade::WHITE : WarpFade::NONE, + .banner = end.banner, + }; } } @@ -232,14 +273,15 @@ void log_new_banner_read(VideoStream& stream, const std::string& read, std::stri } // Looks for the location banner `banner` for up to `wait`, and with -// `before` (the brightness before the tap) for the fade (tap_faded()). -// Logs each distinct banner read and how the wait ended: the Oreburgh Gate's -// entry missed its banner twice (lab-B, 2026-11-08 15:31 and 15:39) though -// a hand probe saw it 1.5 s after the fade, and nothing said why. -TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, std::optional before){ +// `fade` for the fade (fade_of_screen()). Logs each distinct banner read +// and how the wait ended: the Oreburgh Gate's entry missed its banner twice +// (lab-B, 2026-11-08 15:31 and 15:39) though a hand probe saw it 1.5 s +// after the fade, and nothing said why. +TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, bool fade){ const WallClock start = current_time(); const WallClock deadline = start + wait; std::string last_read; + LocationBannerReads reads; double darkest = 255; // Every 500 ms of a long wait (the one after a fade) a frame goes to // Screenshots/AutoStory/banner/, to see what the game showed. @@ -248,18 +290,17 @@ TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const while (true){ context.wait_for_all_requests(); const VideoSnapshot screen = stream.video().snapshot(); - if (!before && screen && current_time() >= next_frame){ + if (!fade && screen && current_time() >= next_frame){ keep_banner_frame(*screen.frame, folder, banner); next_frame += 500ms; } - const std::string read = screen ? read_location_banner(*screen.frame) : ""; + const std::string read = screen ? reads.read(*screen.frame, current_time()) : ""; log_new_banner_read(stream, read, last_read, banner, start); if (screen && location_banner_matches(read, banner)){ return TapLook::BANNER; } - const double brightness = brightness_of(screen); - darkest = std::min(darkest, brightness); - if (before && tap_faded(*before, brightness)){ + darkest = std::min(darkest, brightness_of(screen)); + if (fade && screen && fade_of_screen(*screen.frame) != WarpFade::NONE){ return TapLook::DARK; } if (current_time() >= deadline){ @@ -274,19 +315,6 @@ TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const } // namespace -// Black as the Gym's door fades (< 25), or a drop to dark: a look under -// FADE_DARKNESS is not a fade by itself, since a cave's floor is that dim -// (the Oreburgh Gate's about 46, lab-B 2026-11-08), but a fade into a cave -// from a lit street goes from about 120 to 45..64. -// TODO(#268 slice 3): stopgap. A uniform-screen fade watcher (the shared -// Black/WhiteScreen stddev test, as in upstream #1717) should replace this -// brightness drop; remove it once that watcher passes the Gate frames. -constexpr double TAP_FADE_BLACK = 25; -constexpr double TAP_FADE_DROP = 0.7; -bool tap_faded(double before, double after){ - return after < TAP_FADE_BLACK || (after < FADE_DARKNESS && after < before * TAP_FADE_DROP); -} - ConsoleStoryStepIO::ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context) : m_stream(stream) , m_context(context) @@ -316,48 +344,40 @@ bool ConsoleStoryStepIO::run_until_map(Direction direction, Milliseconds limit, return walk.stop == WalkStop::ARRIVED; } -bool ConsoleStoryStepIO::run_into_door(Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from){ - return PokemonBDSP::run_into_door(m_stream, m_context, direction, run, timeout, dark_from); +WarpSeen ConsoleStoryStepIO::run_into_door(Direction direction, bool run, Milliseconds timeout, Milliseconds dark_from, const std::string& banner){ + return PokemonBDSP::run_into_door(m_stream, m_context, direction, run, timeout, dark_from, banner); } // A held walk can carry on past a fade that goes unseen: out of the Gym it // walked on south (r219g), and through the Oreburgh Gate's west mouth on // west into a pocket by Route 203's pond (r75). Taps during the fade are -// eaten. The banner before the first tap means we are through already: -// the walk before slid onto the warp (lab-B 2026-11-08 15:31). -// TODO(#268 slice 2): stopgap. The "through already" check sees only a -// banner still on screen; once walkers report the fade or banner they saw -// (WalkResult::warp), read that instead and remove this check. +// eaten, so a fade ends the taps; the banner after it is evidence, not +// required (#268 slice 1). The watch starts before the first tap: a walk +// before that slid onto the warp unseen may have left its banner up +// (lab-B 2026-11-08 15:31). std::optional ConsoleStoryStepIO::tap_to_banner(Direction direction, const std::string& map, size_t max_taps){ const std::string& banner = story_banner_name(map); m_context.wait_for_all_requests(); - VideoSnapshot screen = m_stream.video().snapshot(); - if (screen && location_banner_matches(read_location_banner(*screen.frame), banner)){ + if (look_after_tap(m_stream, m_context, banner, 0ms, true) == TapLook::BANNER){ m_stream.log("[Banner] \"" + banner + "\" shows before the first tap: through already."); return 0; } for (size_t tap = 1; tap <= max_taps; tap++){ - const double before = brightness_of(screen); pbf_press_dpad(m_context, dpad_toward(direction), 160ms, 0ms); - TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, before); - const bool faded = look == TapLook::DARK; - if (faded){ - look = look_after_tap(m_stream, m_context, banner, 8000ms, std::nullopt); + const TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, true); + if (look == TapLook::DARK && look_after_tap(m_stream, m_context, banner, 8000ms, false) != TapLook::BANNER){ + m_stream.log("[Banner] a fade, no \"" + banner + "\" banner after it: counting the fade.", COLOR_ORANGE); } - if (look == TapLook::BANNER){ + if (look != TapLook::NOTHING){ m_context.wait_for(1000ms); return tap; } - if (faded){ - break; - } - screen = m_stream.video().snapshot(); } return std::nullopt; } -void ConsoleStoryStepIO::settle(Button button, const std::string& label, bool fade){ - PokemonBDSP::settle(m_stream, m_context, button, label, fade); +void ConsoleStoryStepIO::settle(Button button, const std::string& label, bool fade, const std::string& banner){ + PokemonBDSP::settle(m_stream, m_context, button, label, fade, banner); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h index a48007ee2f..6aa734d890 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h @@ -44,28 +44,27 @@ class StoryStepIO{ // JSON map's name) shows, for at most `limit`. Returns whether it showed. virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) = 0; // Holds the D-pad toward a door (running with `run`) for `timeout` until - // the screen goes dark or a line shows. A dark screen in the first - // `dark_from` of running is not the door's fade, and a wild battle on the - // way is fled, the run going on for the time left. Returns whether it went dark. - virtual bool run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), Milliseconds dark_from = Milliseconds(0)) = 0; + // the screen goes dark or white, `banner` (a map's banner name, "" for + // none) reads, or a line shows. A dark screen in the first `dark_from` + // of running is not the door's fade, and a wild battle on the way is + // fled, the run going on for the time left. Returns what it saw of the + // warp: the fade or the banner, either one alone (#268 slice 1). + virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), + Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") = 0; // D-pad taps `direction` one at a time, each looked at, toward a door - // or cave mouth, until the fade (then no more taps) and the location - // banner of `map` (a JSON map's name), for at most `max_taps`. Returns - // the taps it took (0 if the banner shows before the first: through - // already), or nothing if no banner showed. + // or cave mouth, until its fade (fade_of(): uniform, so a dim cave floor + // is none) or the location banner of `map` (a JSON map's name), for at + // most `max_taps`; after a fade, no more taps. Either one alone is the + // way through. The watch starts before the first tap: a banner up then + // is 0 taps. Returns the taps it took, or nothing if neither showed. virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) = 0; // Presses through any text until the overworld is quiet; with `fade`, a - // fade must come first (a map change). Fails otherwise. - virtual void settle(Button button, const std::string& label, bool fade) = 0; + // warp must come first (a map change): a fade, or the banner `banner` + // ("" for a fade only). Fails otherwise. + virtual void settle(Button button, const std::string& label, bool fade, const std::string& banner) = 0; }; -// Whether a look after a tap toward a door or a cave mouth shows its fade, -// from its brightness `after` and that of the look before the tap, -// `before` (the mean of r, g and b, 0 to 255). -bool tap_faded(double before, double after); - - // The console. class ConsoleStoryStepIO : public StoryStepIO{ public: @@ -77,9 +76,10 @@ class ConsoleStoryStepIO : public StoryStepIO{ virtual void fight(const std::string& trainer) override; virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string& label) override; - virtual bool run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), Milliseconds dark_from = Milliseconds(0)) override; + virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), + Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") override; virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) override; - virtual void settle(Button button, const std::string& label, bool fade) override; + virtual void settle(Button button, const std::string& label, bool fade, const std::string& banner) override; private: VideoStream& m_stream; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp index 8a0d50e0b9..f79162b485 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -7,12 +7,14 @@ #include #include #include +#include #include "Common/Cpp/Color.h" #include "Common/Cpp/Exceptions.h" #include "Pokemon/AutoStory/Pokemon_TilePose.h" #include "Pokemon/Navigation/Pokemon_MapData.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_Maps.h" #include "PokemonBDSP/Programs/AutoStory/Data/PokemonBDSP_AutoStory_WallRecovery.h" +#include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h" #include "PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "PokemonBDSP_AutoStory_StoryExecutor.h" @@ -52,7 +54,7 @@ std::string label_of(const StoryStep& step){ CenterEntry run_into_center(StoryStepIO& io, bool run, const std::function& inside, const std::function& line_up){ - if (io.run_into_door(Direction::NORTH, run)){ + if (io.run_into_door(Direction::NORTH, run).any()){ return CenterEntry::FADED; } if (inside()){ @@ -67,6 +69,7 @@ void StoryWalkSteps::begin_attempt(const std::string& row){ // A row starts from its save: after the shoes' row, the save has them. m_can_run = row > std::string(RUNNING_SHOES_ROW); m_trainers.clear(); + m_warp = {}; } void StoryWalkSteps::turn(Direction direction){ @@ -105,10 +108,11 @@ Pose StoryWalkSteps::walk_to(const Pose& from, const Pose& goal, const StoryStep [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); }, label_of(step), {}, *costs, m_can_run ? MoveSpeed::RUN : MoveSpeed::WALK }; - const std::vector at = walk_route(m_io.route(), *maps, start, trainers, plan); + RouteEnd end = walk_route_to_warp(m_io.route(), *maps, start, trainers, plan); sights = std::move(trainers.sights); + m_warp = std::move(end.warp); std::vector local; - for (const TilePos& pos : at){ + for (const TilePos& pos : end.tiles){ const std::optional tile = story_to_local(*maps, goal.map, pos); if (!tile){ fail(m_io.route(), std::format("{}: the walk ended off {}.", step.name, goal.map)); @@ -119,6 +123,7 @@ Pose StoryWalkSteps::walk_to(const Pose& from, const Pose& goal, const StoryStep } Outcome StoryWalkSteps::route(RunState& state, const StoryStep& step){ + m_warp = {}; if (Pokemon::tiles_of(state.pose).empty()){ return Pokemon::outcome_failed("no_pose", Pokemon::to_string(state.pose)); } @@ -193,20 +198,38 @@ size_t taps_to_banner(const Pose& from, Direction direction){ // there; a banner before any tap means the walk before took us through. Outcome StoryWalkSteps::tap_to_banner(const Pose& from, const StoryStep& step){ const std::string map = step.pose_after ? step.pose_after->map : ""; + if (through_already(step)){ + return Pokemon::outcome_ok(); + } const size_t limit = taps_to_banner(from, step.direction); const std::optional taps = m_io.tap_to_banner(step.direction, map, limit); if (!taps){ - return Pokemon::outcome_failed("no_banner", std::format("{} in {} taps {}: through already, its banner gone?", + return Pokemon::outcome_failed("no_banner", std::format("no fade and no {} banner in {} taps {}", map, limit, direction_name(step.direction))); } if (*taps == 0){ log(std::format("[{}] {}: {}'s banner before any tap: through already.", m_row, step.name, map)); }else{ - log(std::format("[{}] {}: {}'s banner after {} taps {}.", m_row, step.name, map, *taps, direction_name(step.direction))); + log(std::format("[{}] {}: through to {} after {} taps {}.", m_row, step.name, map, *taps, direction_name(step.direction))); } return Pokemon::outcome_ok(); } +// The route before ran on through this step's warp (walk_route_to_warp()): +// the step is done, unless the banner it read names another map. +bool StoryWalkSteps::through_already(const StoryStep& step){ + const WarpSeen through = std::exchange(m_warp, {}); + if (!through.any()){ + return false; + } + const std::string map = step.pose_after ? step.pose_after->map : ""; + if (!through.banner.empty() && !location_banner_matches(through.banner, story_banner_name(map))){ + fail(m_io.route(), std::format("{}: the walk before went through a warp to \"{}\", not {}.", step.name, through.banner, map)); + } + log(std::format("[{}] {}: through already; the walk before saw {}.", m_row, step.name, describe(through))); + return true; +} + namespace{ // A run into a door: how long to hold, and for how long of it a dark screen // is not yet the door's fade (run_into_door()). @@ -352,6 +375,7 @@ Outcome StoryWalkSteps::counted_wall(const Pose& from, const StoryStep& step){ } Outcome StoryWalkSteps::legs(RunState& state, const StoryStep& step){ + m_warp = {}; if (step.variant == "counted_wall"){ return counted_wall(state.pose, step); } @@ -385,7 +409,7 @@ Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ if (walk.wild_battles > 0){ return Pokemon::outcome_failed("interrupted_step", step.name); } - m_io.settle(BUTTON_B, label_of(step), false); + m_io.settle(BUTTON_B, label_of(step), false, ""); return Pokemon::outcome_ok(); } // A step through a door: walk (or run) on until the screen goes dark, @@ -394,12 +418,19 @@ Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ // 00.01; Dawn's tour, 00.04). Without the fade, settle must see it itself // (r33 was outside, "no fade"). No separate turn tap first: at 1 tile // from the door, a 50 ms turn tap can step in before run_into_door() watches. + // A fade or the far map's banner is the way through, either one (#268 + // slice 1); with neither yet, settle() waits for one. if (step.pose_after && step.pose_after->map != state.pose.map){ + if (through_already(step)){ + m_io.settle(BUTTON_B, label_of(step), false, ""); + return Pokemon::outcome_ok(); + } + const std::string banner = story_arrival_banner(state.pose.map, step.pose_after->map); const DoorRun door = door_run(state.pose.map, state.pose, step.direction, m_can_run); - const bool faded = m_io.run_into_door(step.direction, m_can_run, door.timeout, door.dark_from); + const WarpSeen seen = m_io.run_into_door(step.direction, m_can_run, door.timeout, door.dark_from, banner); log(std::format("[{}] {}: {} through the door (timeout {} ms, dark from {} ms): {}.", m_row, step.name, - direction_name(step.direction), door.timeout.count(), door.dark_from.count(), faded ? "faded" : "no fade yet")); - m_io.settle(BUTTON_B, label_of(step), !faded); + direction_name(step.direction), door.timeout.count(), door.dark_from.count(), seen.any() ? describe(seen) : "nothing yet")); + m_io.settle(BUTTON_B, label_of(step), !seen.any(), banner); return Pokemon::outcome_ok(); } turn(step.direction); @@ -416,6 +447,7 @@ Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ } Outcome StoryWalkSteps::talk(RunState& state, const StoryStep& step){ + m_warp = {}; // Face whom we talk to: a route ends facing its last leg, not its goal // (r40 reached Mom facing north). They block the tile, so the tap only turns. if (const std::optional facing = Pokemon::facing_of(state.pose)){ @@ -432,6 +464,7 @@ Outcome StoryWalkSteps::talk(RunState& state, const StoryStep& step){ } Outcome StoryWalkSteps::mash(RunState& state, const StoryStep& step){ + m_warp = {}; // The step before may have played the scene already (a door step settles // its arrival scene), so its first line is not required. if (!m_io.route().dialog_within(15000ms, COLOR_CYAN)){ @@ -439,7 +472,7 @@ Outcome StoryWalkSteps::mash(RunState& state, const StoryStep& step){ } m_io.settle(step.variant == "a" ? BUTTON_A : BUTTON_B, label_of(step), step.pose_after && step.pose_after->map != state.pose.map && - story_plan_world(state.pose.map, step.pose_after->map) == nullptr); + story_plan_world(state.pose.map, step.pose_after->map) == nullptr, ""); if (m_row == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP){ m_can_run = true; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h index a7c4aad024..c284f97065 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -76,6 +76,7 @@ class StoryWalkSteps{ Pokemon::Outcome leg_to_dialogue(const Pokemon::StoryStep& step); Pokemon::Outcome leg_to_map(const Pokemon::StoryStep& step); Pokemon::Outcome tap_to_banner(const Pokemon::Pose& from, const Pokemon::StoryStep& step); + bool through_already(const Pokemon::StoryStep& step); void turn(Pokemon::Direction direction); void log(const std::string& text); @@ -86,6 +87,9 @@ class StoryWalkSteps{ // in its tiles, from its first walk this attempt; all of them again at // each attempt. std::map> m_trainers; + // The warp the last route ran on through at its end (walk_route_to_warp()), + // for the step after it, which goes through that warp. + WarpSeen m_warp; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp index 4bacc207a3..fd86f221cc 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp @@ -14,6 +14,7 @@ #include "Utils/PokemonBDSP_AutoStory_Indoors.h" #include "Utils/PokemonBDSP_AutoStory_Location.h" #include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" +#include "Utils/PokemonBDSP_AutoStory_Walk.h" #include "Actions/PokemonBDSP_AutoStory_Talks.h" #include "PokemonBDSP_AutoStory_StoryStepIO.h" #include "PokemonBDSP_AutoStory_Walk_Tests.h" @@ -121,29 +122,33 @@ UnitTestResult test_door_fade(Logger&){ return true; } -// A tap step's fade is a real one: black, or a drop to dark from the look -// before the tap. The Oreburgh Gate's floor, about 46 before and after a -// tap inside it, is no fade: read as one, the tap into the Gate from -// Oreburgh, already through, waited 8 s for a banner gone (lab-B, -// 2026-11-08 15:31 to 15:54). -UnitTestResult test_tap_fade(Logger&){ +// A fade is uniform (fade_of(): the shared watchers' stddev test), dark or +// white; a floor is textured. The Oreburgh Gate's floor, about 46, is no +// fade: read as one, the tap into the Gate from Oreburgh, already +// through, waited 8 s for a banner gone (lab-B, 2026-11-08 15:31 to +// 15:54). A cave mouth's dim fade (45 to 64 measured) is one. The stddevs +// are a model; the Gate's frames on the console are to check them. +UnitTestResult test_fade_of(Logger&){ struct Case{ std::string name; - double before; - double after; - bool faded; + double brightness; + double stddev; + WarpFade fade; }; const std::vector cases{ - {.name = "inside the Gate, its floor", .before = 46, .after = 46, .faded = false}, - {.name = "inside the Gate, a darker patch", .before = 52, .after = 44, .faded = false}, - {.name = "on a street", .before = 120, .after = 112, .faded = false}, - {.name = "the street into the Gate's mouth, to black", .before = 120, .after = 8, .faded = true}, - {.name = "the street into a cave, a dim fade (45 to 64 measured)", .before = 120, .after = 60, .faded = true}, - {.name = "out of the Gate, from its floor to black", .before = 46, .after = 12, .faded = true}, + {.name = "inside the Gate, its floor", .brightness = 46, .stddev = 20, .fade = WarpFade::NONE}, + {.name = "Oreburgh's street at night", .brightness = 88, .stddev = 20, .fade = WarpFade::NONE}, + {.name = "a door's black", .brightness = 7, .stddev = 1, .fade = WarpFade::BLACK}, + {.name = "a cave mouth's dim fade", .brightness = 64, .stddev = 2, .fade = WarpFade::BLACK}, + {.name = "a grey screen, neither dark nor white", .brightness = 120, .stddev = 1, .fade = WarpFade::NONE}, + {.name = "a cave exit's white", .brightness = 250, .stddev = 1, .fade = WarpFade::WHITE}, + {.name = "a bright textured screen", .brightness = 250, .stddev = 20, .fade = WarpFade::NONE}, }; for (const Case& item : cases){ - if (tap_faded(item.before, item.after) != item.faded){ - return item.name + (item.faded ? ": no fade read." : ": read as a fade."); + const ImageStats stats(FloatPixel(item.brightness, item.brightness, item.brightness), FloatPixel(item.stddev, item.stddev, item.stddev), 1); + const WarpFade fade = fade_of(stats); + if (fade != item.fade){ + return item.name + ": " + warp_fade_name(fade) + ", expected " + warp_fade_name(item.fade) + "."; } } return true; @@ -315,7 +320,7 @@ const AutoStoryWalkCase AUTO_STORY_WALK_CASES[] = { .expectation = {}, }, { - .input = {"a tap into a door fades to black or drops to dark, not a dim floor (lab-B, 2026-11-08)", test_tap_fade}, + .input = {"a fade is uniform, dark or white; a dim floor is none (lab-B, 2026-11-08)", test_fade_of}, .expectation = {}, }, { diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp index 38630ad729..2be33f0f4c 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp @@ -441,7 +441,9 @@ WalkResult GridSimRouteIO::walk_to_wall(const RouteWallWalk& walk){ const SimMove move = m_sim.hold(walk.direction, walk.limit, pace); m_notes.push_back(std::format("[Sim] {}: held {} to {} at {}.", walk.label, direction_name(walk.direction), sim_stop_name(move.stop), to_string(m_sim.player().pos))); - return {.stop = walk_stop(move.stop), .walked = move.time, .wild_battles = wild_battles, .moved_chunks = (size_t)(move.distance / SIM_TILE)}; + WalkResult result{.stop = walk_stop(move.stop), .walked = move.time, .wild_battles = wild_battles, .moved_chunks = (size_t)(move.distance / SIM_TILE)}; + result.warp.fade = move.stop == SimStop::WARP ? WarpFade::BLACK : WarpFade::NONE; + return result; } std::optional GridSimRouteIO::take_encounter(Direction direction, size_t tiles, size_t tap, const std::string& label){ @@ -484,6 +486,7 @@ WalkResult GridSimRouteIO::walk_taps(Direction direction, size_t tiles, const st result.moved_chunks += move.distance > 0 ? 1 : 0; if (move.stop == SimStop::DIALOG || move.stop == SimStop::WARP){ result.stop = walk_stop(move.stop); + result.warp.fade = move.stop == SimStop::WARP ? WarpFade::BLACK : WarpFade::NONE; }else if (still >= 2){ result.stop = WalkStop::BLOCKED; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp index 6901c3c2e5..30b7c55788 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp @@ -427,15 +427,15 @@ class WalkStepsIO : public StoryStepIO{ unsupported("a run onto " + map); return false; } - virtual bool run_into_door(Direction, bool, Milliseconds, Milliseconds) override{ + virtual WarpSeen run_into_door(Direction, bool, Milliseconds, Milliseconds, const std::string&) override{ unsupported("a run into a door"); - return false; + return {}; } virtual std::optional tap_to_banner(Direction, const std::string& map, size_t) override{ unsupported("taps to " + map + "'s banner"); return std::nullopt; } - virtual void settle(Button, const std::string& label, bool) override{ + virtual void settle(Button, const std::string& label, bool, const std::string&) override{ unsupported(label + "'s settle"); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp index 9dd3fd9b47..a0968abb0f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include "Common/Cpp/Exceptions.h" @@ -44,14 +45,22 @@ using Pokemon::TilePos; // Cases the program fails today, with what is to fix each. const std::map KNOWN_FAILING{ - {"01.05 old walk slides onto the Gate's west mouth, dim fade, banner gone", "#268 slice 2: walkers report warps"}, - {"01.05 old walk slides onto the Gate's west mouth, black fade", "#268 slice 2: walkers report warps"}, - {"01.05 old walk slides onto the Gate's west mouth, white fade", "#268 slices 2 and 3: a fade watcher that tells white from a battle"}, {"01.05 through_the_gate is one run west", "planner: goal kinds and ms costs (PLANNER_DESIGN.md)"}, - {"any one missed fade or banner from 00.00 to 01.11: every row still passes", "#268 slice 1: door arrival = fade or banner"}, + {"any one missed fade or banner from 00.00 to 01.11: every row still passes", + "a third signal where a warp gives one (ONE_SIGNAL_STEPS): position (PLANNER_DESIGN.md 2, 4)"}, {"01.08 with Route 204's youngster wandering (12..14, 2)", "planner: one recovery rule, a sidestep round a wanderer (PLANNER_DESIGN.md 4, 7)"}, }; +// Warps that give one signal only (#268 slice 1: door = fade or banner), so +// a miss there leaves nothing to go on: a fade and no banner (stairs, into +// a building, between the mine's floors), or a banner and no fade (an open +// border crossed by run_until_map()). +const std::set ONE_SIGNAL_STEPS{ + "00.01 down_the_stairs", "00.01 into_barrys_house", "00.01 up_barrys_stairs", "00.01 down_barrys_stairs", + "00.04 into_the_house", "00.05 into_the_school", "01.03 into_the_coal_room", "01.03 up_to_the_hall", + "00.04 to_route_201", "01.10 into_floaroma", +}; + // The table's step `name` in row `row`. const StoryStep& table_step(const std::string& row, const std::string& name){ for (const StoryCheckpoint& checkpoint : story_checkpoints()){ @@ -117,10 +126,10 @@ UnitTestResult gate_plan_one_run(Logger& logger){ } // Each fade and each banner the clean run looks for, missed alone: the -// row it falls in must still pass. A door step accepts only its fade -// today, so a missed fade there fails a real arrival (#268's Monte Carlo, -// 19/20 on 01.05). -UnitTestResult every_single_miss(Logger& logger){ +// row it falls in must still pass, but for a miss in one of `skip`'s steps. +// A door step once accepted only its fade, so a missed fade there failed a +// real arrival (#268's Monte Carlo, 19/20 on 01.05). +UnitTestResult every_single_miss(Logger& logger, const std::set& skip){ SimExecutor clean(logger); if (!run_rows_on(clean, logger, {.from = "00.00", .to = "01.11"}).completed){ return "the run fails without misses: " + clean.result().error; @@ -134,9 +143,12 @@ UnitTestResult every_single_miss(Logger& logger){ } SimMisses misses{.fade = {}, .banner = {}}; (fade ? misses.fade : misses.banner) = index - (fade ? rows[k].fades : rows[k].banners); - const std::string stopped = run_case(logger, rows[k].row, rows[k].row, misses, {}); - if (!stopped.empty()){ - failed.push_back(std::format("{} {} missed: {}", fade ? "fade" : "banner", index, stopped)); + SimExecutor executor(logger, misses); + if (!run_rows_on(executor, logger, {.from = rows[k].row, .to = rows[k].row}).completed + && !skip.contains(executor.result().failed_step) + ){ + failed.push_back(std::format("{} {} missed: stopped in {}: {}", fade ? "fade" : "banner", index, + executor.result().failed_step, executor.result().error)); } }; for (size_t i = 0; i < clean.misses().fades; i++){ @@ -154,8 +166,9 @@ UnitTestResult every_single_miss(Logger& logger){ // Oreburgh's street at night (88, game clock 00:25; 104 by day, r74), the // Gate's dim floor (46) and a cave mouth's dim fade (45 to 64): the tap -// steps judge their fades with tap_faded(), which took the Gate's floor -// for a fade under the old "brightness under 75" (lab-B 2026-11-08). +// steps judge their fades with fade_of(): uniform, so a textured floor +// is none. The old "brightness under 75" took the Gate's floor for a fade +// (lab-B 2026-11-08). UnitTestResult night_and_dim_floors(Logger& logger){ SimWorldKnobs knobs{ .floor = {{"OreburghCity", 88}, {"Route203", 88}, {"OreburghGate", 46}}, @@ -253,7 +266,12 @@ const FailureCase AUTO_STORY_FAILURE_CASES[] = { return gate_old_walk_slides(logger, SimFade::WHITE, true); }}, .expectation = {}}, {.input = {"01.05 through_the_gate is one run west", gate_plan_one_run}, .expectation = {}}, - {.input = {"any one missed fade or banner from 00.00 to 01.11: every row still passes", every_single_miss}, .expectation = {}}, + {.input = {"any one missed fade or banner from 00.00 to 01.11: every row still passes", [](Logger& logger){ + return every_single_miss(logger, {}); + }}, .expectation = {}}, + {.input = {"any one missed fade or banner at a warp that shows both: its row still passes", [](Logger& logger){ + return every_single_miss(logger, ONE_SIGNAL_STEPS); + }}, .expectation = {}}, {.input = {"01.02 to 01.05 at night, the Gate's dim floor", night_and_dim_floors}, .expectation = {}}, {.input = {"01.03 Roark's first line 7 s after the A press", roark_speaks_late}, .expectation = {}}, {.input = {"01.03 with Oreburgh's people wandering (the pokefan on 45, 37..39)", [](Logger& logger){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp index c535b38c60..835ee14d1f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp @@ -29,6 +29,10 @@ using Pokemon::StoryStepKind; using Pokemon::TilePos; +ImageStats sim_screen(double brightness, double stddev){ + return ImageStats(FloatPixel(brightness, brightness, brightness), FloatPixel(stddev, stddev, stddev), 1); +} + // A hold until something stops us. A door stops it at the fade, which // fails the step; a fade missed lets the hold go on past the mouth (r81's // timed walk south out of the Ravaged Path, onto Route 204's cliff). @@ -74,12 +78,12 @@ double SimStoryStepIO::floor_here() const{ return SIM_FLOOR; } -// D-pad taps, each looked at, until the fade and then the banner (the -// console's ConsoleStoryStepIO::tap_to_banner()): none if the banner shows -// before the first. Each look stands still, so a banner on the far side -// reads at once. A look the program's tap_faded() takes for the fade ends -// the taps, banner or not: a warp's fade, or a dim floor read as one; with -// the fade missed the next look still sees the banner. +// D-pad taps, each looked at, until the fade or the banner (the console's +// ConsoleStoryStepIO::tap_to_banner()): none if the banner shows before +// the first. Each look stands still, so a banner on the far side reads at +// once. A look the program's fade_of() takes for a fade ends the taps: a +// warp's fade, or a floor read as one; with the fade missed the look still +// sees the banner. std::optional SimStoryStepIO::tap_to_banner(Direction direction, const std::string& map, size_t max_taps){ if (m_misses.through == m_misses.tap_steps++){ m_misses.missed = true; @@ -92,23 +96,22 @@ std::optional SimStoryStepIO::tap_to_banner(Direction direction, const s } bool crossed = false; for (size_t tap = 1; tap <= max_taps; tap++){ - const double before = floor_here(); const SimMove move = m_sim.tap(direction, 160ms, false); if (move.stop == SimStop::DIALOG){ return std::nullopt; } - double look = floor_here(); + ImageStats look = sim_screen(floor_here(), SIM_FLOOR_STDDEV); if (move.stop == SimStop::WARP){ const auto fade = m_knobs.fade_into.find(map); - look = fade_missed() ? look : fade == m_knobs.fade_into.end() ? SIM_DOOR_FADE : fade->second; + if (!fade_missed()){ + look = sim_screen(fade == m_knobs.fade_into.end() ? SIM_DOOR_FADE : fade->second, SIM_FADE_STDDEV); + } crossed = m_far_side(); } - if (crossed && on_map(map) && !banner_missed()){ + const bool banner = crossed && on_map(map) && !banner_missed(); + if (banner || fade_of(look) != WarpFade::NONE){ return tap; } - if (tap_faded(before, look)){ - return std::nullopt; - } } return std::nullopt; } @@ -127,9 +130,11 @@ WalkResult SimKnobRouteIO::walk_taps(Direction direction, size_t tiles, const st return result; } -// The taps past the leg's end. Onto a warp the walk sees what the fade -// looks like to its watchers: a black fade ends it (TRANSITION), a white -// one is a wild battle to it, a dim one nothing. +// The taps past the leg's end. Onto a warp the walk sees the fade as the +// program's fade_of() judges it, the walk's watchers' test: a fade ends +// it (TRANSITION), a white one too, as no battle follows it. The walk ends +// at the fade, before the banner shows (about 1.5 s after it), so it +// reads none. WalkResult SimKnobRouteIO::slide_on(const SimSlide& slide, WalkResult result){ m_misses.slid = true; for (size_t tap = 0; tap < slide.tiles; tap++){ @@ -141,10 +146,10 @@ WalkResult SimKnobRouteIO::slide_on(const SimSlide& slide, WalkResult result){ note("[Sim] slid onto a warp to " + Pokemon::to_string(slide.far) + ".", RouteNote::LOG); m_cross(slide.far); m_misses.banner_up = slide.banner_up ? slide.far.map : ""; - if (slide.fade == SimFade::BLACK){ + const double brightness = slide.fade == SimFade::BLACK ? SIM_DOOR_FADE : slide.fade == SimFade::DIM ? SIM_DIM_FADE : SIM_WHITE_FADE; + result.warp.fade = fade_of(sim_screen(brightness, SIM_FADE_STDDEV)); + if (result.warp.fade != WarpFade::NONE){ result.stop = WalkStop::TRANSITION; - }else if (slide.fade == SimFade::WHITE){ - result.wild_battles++; } return result; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h index ec681f290e..e20575b083 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h @@ -23,6 +23,7 @@ #include #include "Common/Cpp/Logging/AbstractLogger.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTools/ImageStats.h" #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryExecutor.h" #include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h" @@ -63,16 +64,17 @@ struct SimMisses{ std::string banner_up = {}; }; -// How a warp's fade looks to the console's watchers (#268's design note). +// How a warp's fade looks on the screen (#268's design note); the +// program's fade_of() judges it, as the walk's watchers do. enum class SimFade{ - // A door's: near black. Every watcher of a black screen sees it. + // A door's: near black (SIM_DOOR_FADE). BLACK, - // A cave mouth's: dim, not black. A walk's strict black watcher (rgb - // sum 100, stddev 10; WalkRunner::hold()) misses it and the walk goes - // on (the Oreburgh Gate, lab-B 2026-11-08 16:03). + // A cave mouth's: dim, not black (SIM_DIM_FADE). The walk's old strict + // black watcher (rgb sum 100) missed it and the walk went on (the + // Oreburgh Gate, lab-B 2026-11-08 16:03). DIM, - // A cave exit's white: a walk's encounter-flash watcher reads it as a - // wild battle. + // A cave exit's white. The walk's old encounter-flash watcher read it + // as a wild battle. WHITE, }; @@ -96,8 +98,9 @@ struct SimWorldKnobs{ std::vector slides = {}; // The mean brightness (0 to 255) of a map's floor, by JSON map name, and // of the fade into a map; a tap step judges its fade from these with the - // program's tap_faded(). Unset: 104 (Oreburgh's street by day) and 7 - // (a door's black). + // program's fade_of(), a floor textured, a fade uniform (SIM_FLOOR_STDDEV, + // SIM_FADE_STDDEV). Unset: 104 (Oreburgh's street by day) and 7 (a + // door's black). std::map floor = {}; std::map fade_into = {}; // People with a wander box on their map walk it, a step tried every 2 s. @@ -150,6 +153,17 @@ constexpr Milliseconds SIM_FALSE_DARK = Milliseconds(1800); constexpr double SIM_FLOOR = 104; // Brightness of a fade where SimWorldKnobs does not say: a door's black (by hand: 7). constexpr double SIM_DOOR_FADE = 7; +// Brightness of a cave mouth's dim fade (45 to 64 measured). +constexpr double SIM_DIM_FADE = 55; +// Brightness of a cave exit's white fade. +constexpr double SIM_WHITE_FADE = 250; +// The spread (stddev a channel) of a fade: uniform. A model, not measured +// on the console. +constexpr double SIM_FADE_STDDEV = 1; +// The spread of a floor, which the game draws with texture. A model too. +constexpr double SIM_FLOOR_STDDEV = 20; +// The screen's middle as fade_of() reads it: `brightness` a channel, `stddev` a channel. +ImageStats sim_screen(double brightness, double stddev); // What a run on the simulation did. struct SimRun{ @@ -198,24 +212,32 @@ class SimStoryStepIO : public StoryStepIO{ } virtual void walk_until_stopped(Pokemon::Direction direction, size_t tiles, const std::string& label) override; virtual bool run_until_map(Pokemon::Direction direction, Milliseconds limit, const std::string& map, const std::string&) override; - virtual bool run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), Milliseconds dark_from = Milliseconds(0)) override{ + // A door's fade stops the run; with the fade missed, the run goes on + // holding and sees the far map's `banner` show after it, if it has one + // (story_arrival_banner(): the sim's maps have no banners of their own). + virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), + Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") override{ const SimPace pace = run ? SimPace::RUN : SimPace::WALK; if (false_dark() && dark_from <= SIM_FALSE_DARK && SIM_FALSE_DARK < timeout){ m_sim.hold(direction, SIM_FALSE_DARK, pace); - return true; + return {.fade = WarpFade::BLACK}; } - const bool warped = m_sim.hold(direction, timeout, pace).stop == SimStop::WARP; - if (warped && fade_missed()){ - m_misses.fade_unseen = true; - return false; + if (m_sim.hold(direction, timeout, pace).stop != SimStop::WARP){ + return {}; } - return warped; + if (!fade_missed()){ + return {.fade = WarpFade::BLACK}; + } + m_misses.fade_unseen = true; + return {.banner = !banner.empty() && !banner_missed() ? banner : ""}; } // A scene may take us to another map (Mom walks us out, 00.04): with a // dialogue open the stub scene plays it. Otherwise only a door fades. // A fade missed on the way in stays missed: it was over before settle() - // looked, which then fails as it does on the console after 120 s. - virtual void settle(Button, const std::string& label, bool fade) override{ + // looked, which then fails as it does on the console after 120 s. The + // run before looked for the banner (run_into_door()), so settle() sees + // none here. + virtual void settle(Button, const std::string& label, bool fade, const std::string&) override{ const bool scene_fades = m_sim.player().dialog && m_far_side(); if (fade && ((!m_sim.player().warped && !scene_fades) || m_misses.fade_unseen)){ m_route.note(label + ": no fade, so we are not where the step goes (map mismatch).", RouteNote::FAILURE); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.cpp index df31791552..9556ec5018 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.cpp @@ -6,11 +6,13 @@ #include #include +#include #include #include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Language.h" +#include "CommonFramework/VideoPipeline/VideoOverlayScopes.h" #include "CommonTools/Images/ImageFilter.h" #include "CommonTools/OCR/OCR_RawTesseractOCR.h" #include "PokemonBDSP_AutoStory_Location.h" @@ -143,6 +145,47 @@ bool location_banner_matches(const std::string& read, const std::string& expecte } +std::string LocationBannerReads::read(const ImageViewRGB32& screen, WallClock now){ + if (!location_banner_showing(screen)){ + m_reads = 0; + return ""; + } + if (m_reads >= 2 || (m_reads == 1 && now - m_first < std::chrono::milliseconds(300))){ + return ""; + } + if (m_reads == 0){ + m_first = now; + } + m_reads++; + return read_location_banner(screen); +} + +LocationBannerWatcher::LocationBannerWatcher(std::string expected) + : VisualInferenceCallback("LocationBannerWatcher") + , m_expected(std::move(expected)) +{} +void LocationBannerWatcher::make_overlays(VideoOverlaySet& items) const{ + items.add(COLOR_CYAN, LOCATION_BANNER_WHITE); +} +bool LocationBannerWatcher::process_frame(const ImageViewRGB32& frame, WallClock timestamp){ + const std::string read = m_reads.read(frame, timestamp); + if (read.empty() || m_matched){ + return m_matched; + } + m_last_read = read; + if (!m_expected.empty() && location_banner_matches(read, m_expected)){ + m_seen = read; + m_matched = true; + return true; + } + if (m_seen.empty() && m_expected.empty()){ + m_seen = read; + } + return false; +} + + + } // namespace PokemonBDSP } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h index cb4e4c8626..d2baff6d70 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Location.h @@ -11,8 +11,10 @@ #define PokemonAutomation_PokemonBDSP_AutoStory_Location_H #include +#include "Common/Cpp/Time.h" #include "CommonFramework/Language.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" +#include "CommonTools/InferenceCallbacks/VisualInferenceCallback.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -63,6 +65,47 @@ std::string read_save_screen_location(const ImageViewRGB32& screen); // read as Route 202. bool location_banner_matches(const std::string& read, const std::string& expected); +// Reads the location banner at most twice per showing: when its box shows, +// and once more 300 ms later in case the first frame caught it sliding in. +// The box test runs on every frame; OCR (tens of ms) only then (#268's cost +// rule: the old look read every poll while the box showed, 30 to 40 reads). +class LocationBannerReads{ +public: + // The banner's text if this frame was read, "" if not. + std::string read(const ImageViewRGB32& screen, WallClock now); + +private: + size_t m_reads = 0; + WallClock m_first = WallClock::min(); +}; + +// Watches the location banner through an action. With `expected` (a map's +// banner name) it stops the action when that banner reads; without, it +// only keeps the first banner read. +class LocationBannerWatcher : public VisualInferenceCallback{ +public: + explicit LocationBannerWatcher(std::string expected = ""); + + virtual void make_overlays(VideoOverlaySet& items) const override; + virtual bool process_frame(const ImageViewRGB32& frame, WallClock timestamp) override; + + // The expected banner if it read, else the first banner read; "" if none. + const std::string& seen() const{ + return m_seen; + } + // The last banner read, for the log. + const std::string& last_read() const{ + return m_last_read; + } + +private: + std::string m_expected; + LocationBannerReads m_reads; + std::string m_seen; + std::string m_last_read; + bool m_matched = false; +}; + } // namespace PokemonBDSP } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp index b2f2d0354f..5a8263eeb2 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.cpp @@ -69,23 +69,6 @@ void log_leg_done(RouteIO& io, const StoryWorld& maps, const std::string& label, assumed ? EndBasis::ASSUMED : EndBasis::WALKED, assumed ? std::to_string(wild_battles) + " wild battle(s) on the way" : ""), RouteNote::LOG); } -// The tiles the player passes when walking `tiles` tiles from `from`, the -// first one after `from` first. Stops early where the player is stopped. -std::vector leg_path(const StoryWorld& grid, TilePos from, Direction direction, size_t tiles){ - std::vector path; - size_t travelled = 0; - while (travelled < tiles){ - const std::optional next = step(grid, from, direction); - if (!next){ - break; - } - travelled += std::abs(next->x - from.x) + std::abs(next->y - from.y); - from = *next; - path.push_back(from); - } - return path; -} - // The map all of `tiles` are on, or empty if they are on several or none. std::string map_of(const StoryWorld& maps, const std::vector& tiles){ std::string name; @@ -195,7 +178,10 @@ size_t min_tiles_at_wall(size_t expected_tiles){ enum class LegNext{ NEXT_LEG, REPLAN, + ARRIVED_EARLY, // through a warp at the leg's end (walk_route_to_warp()) }; +// How far past a leg's end a walk that ran on may have gone. +constexpr size_t WARP_SLIDE_TILES = 2; // One planned leg as it is walked: where it goes, and the location // banners read on the way. @@ -246,7 +232,7 @@ class RouteWalker{ , m_plan(plan) {} - std::vector run(std::vector from); + RouteEnd run(std::vector from); private: std::vector plan_legs() const; @@ -290,11 +276,12 @@ class RouteWalker{ // Every banner read on the way, to check the map we arrive on against. std::vector m_all_banners; std::string m_map_start; + WarpSeen m_warp; // Dialogue the map does not explain, so far on this route. size_t m_unexplained_talks = 0; }; -std::vector RouteWalker::run(std::vector from){ +RouteEnd RouteWalker::run(std::vector from){ // Tiles to avoid are ground the game lets us walk on (a trainer's line // of sight), so they are off the map, not walls: a leg must not end // "at" one, or the walk would run on through it. @@ -309,12 +296,12 @@ std::vector RouteWalker::run(std::vector from){ const std::vector legs = plan_legs(); m_io.note(m_plan.label + ": " + std::to_string(legs.size()) + " legs planned", RouteNote::STATUS); log_plan(m_io, m_maps, m_plan.label, m_from, legs); - bool replanned = false; - for (size_t number = 1; number <= legs.size() && !replanned; number++){ - replanned = walk_leg(legs[number - 1], number, legs.size()) == LegNext::REPLAN; + LegNext next = LegNext::NEXT_LEG; + for (size_t number = 1; number <= legs.size() && next == LegNext::NEXT_LEG; number++){ + next = walk_leg(legs[number - 1], number, legs.size()); } - if (!replanned){ - return arrive(); + if (next != LegNext::REPLAN){ + return {next == LegNext::ARRIVED_EARLY ? m_from : arrive(), m_warp}; } } fail(m_io, m_plan.label + ": too many plans."); @@ -356,10 +343,15 @@ LegNext RouteWalker::walk_leg(const PlannedLeg& planned, size_t number, size_t c return next; } m_facing = planned.leg.direction; - check_banners(walk); if (walk.result.stop == WalkStop::TRANSITION){ - fail(m_io, walk.label + ": unexpected transition."); + if (!warp_within(m_grid, m_from, planned.leg.direction, planned.leg.tiles + WARP_SLIDE_TILES)){ + fail(m_io, walk.label + ": unexpected transition."); + } + m_io.note("[Route] " + walk.label + ": through the warp at its end (" + describe(walk.result.warp) + "); handing over.", RouteNote::WARNING); + m_warp = walk.result.warp; + return LegNext::ARRIVED_EARLY; } + check_banners(walk); return after_leg(walk); } @@ -857,6 +849,17 @@ std::vector walk_route( std::vector walk_route( RouteIO& io, const StoryWorld& maps, std::vector from, RouteTrainers& trainers, const RouteWalkPlan& plan +){ + RouteEnd end = walk_route_to_warp(io, maps, std::move(from), trainers, plan); + if (end.warp.any()){ + fail(io, plan.label + ": unexpected transition."); + } + return std::move(end.tiles); +} + +RouteEnd walk_route_to_warp( + RouteIO& io, const StoryWorld& maps, std::vector from, + RouteTrainers& trainers, const RouteWalkPlan& plan ){ return RouteWalker(io, maps, trainers, plan).run(std::move(from)); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h index 0f6efccc0a..777019cfdd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_PlannedRoute.h @@ -71,6 +71,21 @@ std::vector walk_route( RouteTrainers& trainers, const RouteWalkPlan& plan ); +// Where walk_route_to_warp() ended: on `tiles`, or through a warp it saw +// (`warp`), with `tiles` where the leg that went through began. +struct RouteEnd{ + std::vector tiles; + WarpSeen warp = {}; +}; +// walk_route(), but a fade on a leg with a warp at most a tile or two past +// its end ends the walk there, as arrived early: the walk ran on onto the +// warp (01.05's Oreburgh Gate, lab-B 2026-11-08), and the step after goes +// through it (#268 slice 2). Any other fade fails as in walk_route(). +RouteEnd walk_route_to_warp( + RouteIO& io, const StoryWorld& maps, std::vector from, + RouteTrainers& trainers, const RouteWalkPlan& plan +); + // The corners of a line a human walked, and the costs to plan each // stretch with. struct WaypointWalkPlan{ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp index fdc6357556..f1a18078fb 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp @@ -9,6 +9,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "CommonFramework/ImageTools/ImageDiff.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" @@ -20,6 +21,7 @@ #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_TransitionWatchers.h" #include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP_AutoStory_Location.h" #include "PokemonBDSP_AutoStory_Walk.h" namespace PokemonAutomation{ @@ -40,6 +42,37 @@ const char* walk_stop_name(WalkStop stop){ return "?"; } +const char* warp_fade_name(WarpFade fade){ + switch (fade){ + case WarpFade::NONE: return "no fade"; + case WarpFade::BLACK: return "black fade"; + case WarpFade::WHITE: return "white fade"; + } + return "?"; +} + +WarpFade fade_of(const ImageStats& middle){ + if (middle.stddev.sum() > FADE_MAX_STDDEV_SUM){ + return WarpFade::NONE; + } + const double sum = middle.average.sum(); + return sum <= FADE_MAX_DARK_SUM ? WarpFade::BLACK : sum >= FADE_MIN_WHITE_SUM ? WarpFade::WHITE : WarpFade::NONE; +} +WarpFade fade_of_screen(const ImageViewRGB32& screen){ + return fade_of(image_stats(extract_box_reference(screen, ImageFloatBox(0.1, 0.1, 0.8, 0.8)))); +} + +std::string describe(const WarpSeen& seen){ + if (!seen.any()){ + return "nothing"; + } + std::string out = seen.fade == WarpFade::NONE ? "" : warp_fade_name(seen.fade); + if (!seen.banner.empty()){ + out += (out.empty() ? "banner \"" : ", banner \"") + seen.banner + "\""; + } + return out; +} + double walk_screen_change(const ImageViewRGB32& before, const ImageViewRGB32& after){ // Small copies: only the whole-view scroll matters, not detail. return ImageMatch::pixel_RMSD(before.scale_to(192, 108), after.scale_to(192, 108)); @@ -80,7 +113,7 @@ DpadPosition dpad_toward(JoystickPosition stick){ bool black_screen_was_battle(VideoStream& stream, ProControllerContext& context){ BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); BattleDialogWatcher battle_dialog(COLOR_YELLOW); - BlackScreenWatcher gone(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, 100, 10, + BlackScreenWatcher gone(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, FADE_MAX_DARK_SUM, FADE_MAX_STDDEV_SUM, BlackScreenWatcher::FinderType::GONE, 500ms); const int ret = wait_until(stream, context, 10s, {{battle_menu}, {battle_dialog}, {gone}}); if (ret == 2){ @@ -95,12 +128,16 @@ bool black_screen_was_battle(VideoStream& stream, ProControllerContext& context) // A grass encounter opens with a leaf animation, neither white nor black, and // shows the battle's text box a few seconds later. After a change of the // whole screen that walking cannot make, wait for the battle to show. -bool battle_follows(VideoStream& stream, ProControllerContext& context){ +bool battle_follows(VideoStream& stream, ProControllerContext& context, Milliseconds wait = 5000ms){ BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); BattleDialogWatcher battle_dialog(COLOR_YELLOW); - return wait_until(stream, context, 5s, {{battle_menu}, {battle_dialog}}) >= 0; + return wait_until(stream, context, wait, {{battle_menu}, {battle_dialog}}) >= 0; } +// After a white flash: an encounter's, if its battle shows; else a cave +// exit's white fade (#268 slice 3), which the walk once fled as a battle. +constexpr Milliseconds WHITE_FLASH_BATTLE_WAIT = 8000ms; + } // namespace // A step in tall grass that was started before the stick was released still @@ -250,19 +287,24 @@ class WalkUntil{ } private: + // The banner watcher only looks (m_banner keeps the first read); a + // fade ends the chunk: dark (fade_of()'s uniform test, a cave's dim + // fade too) or the white of an encounter or a cave exit. int hold(Milliseconds chunk){ WhiteScreenOverWatcher encounter_flash(COLOR_YELLOW, {0.1, 0.1, 0.8, 0.8}); - BlackScreenWatcher black(COLOR_RED); + BlackScreenWatcher black(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, FADE_MAX_DARK_SUM, FADE_MAX_STDDEV_SUM); BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); ShortDialogWatcher dialog(COLOR_YELLOW); BattleDialogWatcher battle_dialog(COLOR_YELLOW); - return run_until( + const int ret = run_until( m_stream, m_context, [&](ProControllerContext& context){ press(context, chunk); }, - {{encounter_flash}, {black}, {battle_menu}, {dialog}, {battle_dialog}} + {{encounter_flash}, {black}, {battle_menu}, {dialog}, {battle_dialog}, {m_banner}} ); + m_result.warp.banner = m_banner.seen(); + return ret; } void press(ProControllerContext& context, Milliseconds chunk) const{ @@ -286,10 +328,13 @@ class WalkUntil{ // The watchers: 0 the encounter flash, 1 black, 2 the battle menu, 3 a // dialogue, 4 the battle's text box. ChunkVerdict after_watchers(int ret){ - bool battle = ret == 0 || ret == 2 || ret == 4; - if (ret == 1){ - battle = black_screen_was_battle(m_stream, m_context); + bool battle = ret == 2 || ret == 4; + if (ret == 0 || ret == 1){ + battle = ret == 0 + ? battle_follows(m_stream, m_context, WHITE_FLASH_BATTLE_WAIT) + : black_screen_was_battle(m_stream, m_context); if (!battle){ + m_result.warp.fade = ret == 0 ? WarpFade::WHITE : WarpFade::BLACK; return {ChunkNext::STOP, WalkStop::TRANSITION}; } } @@ -422,6 +467,7 @@ class WalkUntil{ size_t m_unchanged = 0; size_t m_chunks = 0; double m_largest = 0; + LocationBannerWatcher m_banner; }; WalkResult walk_until_spec( @@ -487,7 +533,7 @@ WalkResult walk_for( WalkResult result{WalkStop::LIMIT, duration, 0}; context.wait_for_all_requests(); WhiteScreenOverWatcher encounter_flash(COLOR_YELLOW, {0.1, 0.1, 0.8, 0.8}); - BlackScreenWatcher black(COLOR_RED); + BlackScreenWatcher black(COLOR_RED, {0.1, 0.1, 0.8, 0.8}, FADE_MAX_DARK_SUM, FADE_MAX_STDDEV_SUM); BattleMenuWatcher battle_menu(BattleType::STANDARD, COLOR_YELLOW); ShortDialogWatcher dialog(COLOR_YELLOW); BattleDialogWatcher battle_dialog(COLOR_YELLOW); @@ -507,6 +553,7 @@ WalkResult walk_for( battle = black_screen_was_battle(stream, context); if (!battle){ result.stop = WalkStop::TRANSITION; + result.warp.fade = WarpFade::BLACK; } } if (battle){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h index f83f45ce07..311e3c784e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h @@ -14,6 +14,7 @@ #include #include #include "Common/Cpp/Time.h" +#include "CommonFramework/ImageTools/ImageStats.h" #include "CommonFramework/ImageTypes/ImageViewRGB32.h" #include "CommonFramework/Tools/VideoStream.h" #include "Controllers/Joystick.h" @@ -33,7 +34,7 @@ using WildBattleEscape = std::function; // hold where we are. enum class WalkOrigin{ CURRENT_LEG, MAY_PRECEDE_LEG }; +// How a screen's fade looks (#268's design note, slice 3). +enum class WarpFade{ + NONE, + BLACK, // a door's black, or a cave mouth's dim fade + WHITE, // a cave exit's white, with no battle after it +}; +// "black fade", for the log. +const char* warp_fade_name(WarpFade fade); + +// A fade is uniform over the middle 80% of the screen, the shared +// Black/WhiteScreenWatcher's stddev test (upstream #1717): a dim cave floor +// is textured, so it is no fade (the Oreburgh Gate's, mean 46, lab-B +// 2026-11-08). Check against the Gate's frames on the console before +// tightening. +constexpr double FADE_MAX_STDDEV_SUM = 10; +// Dark goes to an rgb sum of 225, as a cave mouth's fade is dim, 45 to 64 +// a channel, not black. +constexpr double FADE_MAX_DARK_SUM = 225; +// White from an rgb sum of 500, as the shared WhiteScreenWatcher's. +constexpr double FADE_MIN_WHITE_SUM = 500; +// The fade `middle` (the stats of the screen's middle 80%) shows, if any. +WarpFade fade_of(const ImageStats& middle); +// fade_of() for a screen. +WarpFade fade_of_screen(const ImageViewRGB32& screen); + +// What a walk or a step saw of a warp (#268): a fade or the location +// banner of the map arrived on. Either one alone is the warp. +struct WarpSeen{ + WarpFade fade = WarpFade::NONE; + // The banner read, "" if none. + std::string banner = {}; + + bool any() const{ + return fade != WarpFade::NONE || !banner.empty(); + } +}; +// How it was seen, for the log: a black fade, banner "Route 203". +std::string describe(const WarpSeen& seen); + // Result metrics and terminal state returned by automated walking execution. struct WalkResult{ WalkStop stop; @@ -59,6 +99,9 @@ struct WalkResult{ // taps into Taylor's freeze). size_t moved_chunks = 0; WalkOrigin origin = WalkOrigin::CURRENT_LEG; + // The fade that ended a TRANSITION, and any location banner read on + // the way (#268 slice 2: walkers report warps). + WarpSeen warp = {}; }; // Holds the left stick at `stick` until one of the WalkStop events. Wild battles on