From 6bd08e24da2e685229b6d3d2b837198da46b98dd Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 2 Nov 2026 07:27:02 -0800 Subject: [PATCH 1/2] BDSP AutoStory: count a tap as a step only if the view scrolled The tap counters read any screen change as our own step, so cloud shadows (r83, r84), TVs and conveyors counted as tiles walked. Outside, the camera follows the player: walk_view_shift() finds the scroll that lines up two frames around the player, and tap_moved() counts a tap only if a scroll lines up at least twice as well as none. Inside, the camera can stay put while the player crosses a room, so the old screen-change line still decides there. WalkUntil's tap walks and dpad_tiles() use it. Measured offline on r83/r84's Floaroma video: standing frames with screen changes up to 57 never read as scrolled; walking read a scroll 2 to 6 times better than none. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01AKYzhrMnPK61XHWEch4a6E --- .../PokemonBDSP_AutoStory_Walk_Tests.cpp | 89 +++++++++++++++++++ .../AutoStory/PokemonBDSP_AutoStory_Walks.cpp | 2 +- .../Utils/PokemonBDSP_AutoStory_Walk.cpp | 74 ++++++++++++++- .../Utils/PokemonBDSP_AutoStory_Walk.h | 27 ++++++ 4 files changed, 187 insertions(+), 5 deletions(-) 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 c38ff46daf..3176624408 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.cpp @@ -5,6 +5,7 @@ */ #include +#include #include #include #include "Common/Cpp/Filesystem/Filesystem.h" @@ -14,6 +15,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 "PokemonBDSP_AutoStory_Walk_Tests.h" namespace PokemonAutomation{ @@ -237,6 +239,92 @@ class Test_LocationBannerShowing : public UnitTest{ } }; +// walk_view_shift(): a step scrolls the whole view; scenery that moves on +// its own does not. Synthetic 384x216 screens of 8-pixel blocks in mixed +// colours, as grass and flowers are, with one change each. +uint32_t scenery_at(int x, int y){ + const uint32_t block = (uint32_t)((x >> 3) * 73856093u) ^ (uint32_t)((y >> 3) * 19349663u); + const uint32_t hashed = block * 2654435761u; + return 0xff000000 | (hashed & 0x00ffffff); +} +ImageRGB32 scenery(int shift_x, int shift_y){ + ImageRGB32 screen(384, 216); + for (int y = 0; y < 216; y++){ + for (int x = 0; x < 384; x++){ + screen.pixel(x, y) = scenery_at(x + shift_x + 1000, y + shift_y + 1000); + } + } + return screen; +} +// Each colour channel of the pixels in [x0, x1) x [y0, y1) times `scale`, +// or set to `fill` if given. +void paint(ImageRGB32& screen, int x0, int y0, int x1, int y1, double scale, uint32_t fill = 0){ + for (int y = y0; y < y1; y++){ + for (int x = x0; x < x1; x++){ + uint32_t p = screen.pixel(x, y); + if (fill != 0){ + screen.pixel(x, y) = fill; + continue; + } + uint32_t out = 0xff000000; + for (int shift = 0; shift < 24; shift += 8){ + out |= (uint32_t)(((p >> shift) & 0xff) * scale) << shift; + } + screen.pixel(x, y) = out; + } + } +} + +class Test_ViewShift : public UnitTest{ +public: + Test_ViewShift() + : UnitTest("PokemonBDSP::AutoStory - a step scrolls the view; moving scenery does not") + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const ImageRGB32 before = scenery(0, 0); + struct Case{ + std::string name; + ImageRGB32 after; + bool scrolled; + }; + std::vector cases; + cases.push_back({"the same view", scenery(0, 0), false}); + cases.push_back({"a step east: the view scrolls 16 pixels west", scenery(16, 0), true}); + cases.push_back({"a step south", scenery(0, 12), true}); + { + // r83, r84: a cloud's shadow over the top of the view. + ImageRGB32 shadow = scenery(0, 0); + paint(shadow, 0, 0, 384, 90, 0.6); + cases.push_back({"a cloud shadow over the top third", std::move(shadow), false}); + } + { + // Jubilife's TV screens, the mine's conveyors. + ImageRGB32 tv = scenery(0, 0); + paint(tv, 20, 20, 110, 80, 0, 0xffffffff); + cases.push_back({"a TV lights up in a corner", std::move(tv), false}); + } + { + // A turn changes only the player's sprite. + ImageRGB32 turned = scenery(0, 0); + paint(turned, 175, 75, 210, 150, 0, 0xffff4040); + cases.push_back({"the player turns", std::move(turned), false}); + } + for (const Case& item : cases){ + const ViewShift shift = walk_view_shift(before, item.after); + if (shift.scrolled() != item.scrolled){ + return std::format("{}: {} (shift {}, {}; difference {:.1f} unshifted, {:.1f} shifted).", + item.name, item.scrolled ? "did not read as a scroll" : "read as a scroll", + shift.dx, shift.dy, shift.unshifted, shift.shifted); + } + } + const ViewShift east = walk_view_shift(before, scenery(16, 0)); + if (east.dx != -8 || east.dy != 0){ + return std::format("A 16-pixel scroll west read as ({}, {}) on the half-size copy, expected (-8, 0).", east.dx, east.dy); + } + return true; + } +}; + } @@ -248,6 +336,7 @@ void add_tests_AutoStory_Walk(UnitTestDatabase& database){ database.add(); database.add(); database.add(); + database.add(); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp index f3423bede7..39b5fa595d 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp @@ -161,7 +161,7 @@ void dpad_tiles(VideoStream& stream, ProControllerContext& context, DpadPosition context.wait_for_all_requests(); const VideoSnapshot after = stream.video().snapshot(); const double change = before && after ? walk_screen_change(*before.frame, *after.frame) : 100; - if (change > 30){ + if (!before || !after || tap_moved(*before.frame, *after.frame, 30)){ moved++; continue; } 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 53d7a461e0..b333d339dd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp @@ -20,6 +20,7 @@ #include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h" #include "PokemonBDSP/Inference/PokemonBDSP_TransitionWatchers.h" #include "PokemonBDSP/Inference/Battles/PokemonBDSP_BattleMenuDetector.h" +#include "PokemonBDSP_AutoStory_Indoors.h" #include "PokemonBDSP_AutoStory_Walk.h" namespace PokemonAutomation{ @@ -46,6 +47,68 @@ double walk_screen_change(const ImageViewRGB32& before, const ImageViewRGB32& af } +bool ViewShift::scrolled() const{ + constexpr double LEAST_DIFFERENCE = 3; // standing still in the video: 0 to 1.5 + return (dx != 0 || dy != 0) && unshifted >= LEAST_DIFFERENCE && unshifted >= 2 * shifted; +} + +ViewShift walk_view_shift(const ImageViewRGB32& before, const ImageViewRGB32& after){ + constexpr int W = 192; + constexpr int H = 108; + // A step scrolls 2 to 10 pixels of the copy in 300 ms. + constexpr int REACH = 12; + const ImageRGB32 a = before.scale_to(W, H); + const ImageRGB32 b = after.scale_to(W, H); + // The player's box, which moves with the camera, not with the scenery. + auto in_player_box = [](int x, int y){ + return x >= W * 42 / 100 && x < W * 58 / 100 && y >= H * 30 / 100 && y < H * 75 / 100; + }; + auto difference = [&](int dx, int dy){ + uint64_t sum = 0; + size_t count = 0; + for (int y = REACH; y < H - REACH; y++){ + for (int x = REACH; x < W - REACH; x++){ + if (in_player_box(x, y)){ + continue; + } + const uint32_t p = a.pixel(x - dx, y - dy); + const uint32_t q = b.pixel(x, y); + for (int shift = 0; shift < 24; shift += 8){ + const int c = (int)((p >> shift) & 0xff) - (int)((q >> shift) & 0xff); + sum += c < 0 ? -c : c; + } + count += 3; + } + } + return (double)sum / (double)count; + }; + ViewShift best; + best.unshifted = difference(0, 0); + best.shifted = best.unshifted; + for (int dy = -REACH; dy <= REACH; dy++){ + for (int dx = -REACH; dx <= REACH; dx++){ + if (dx == 0 && dy == 0){ + continue; + } + const double d = difference(dx, dy); + if (d < best.shifted){ + best.shifted = d; + best.dx = dx; + best.dy = dy; + } + } + } + return best; +} + +bool tap_moved(const ImageViewRGB32& before, const ImageViewRGB32& after, double line){ + if (indoors(after)){ + return walk_screen_change(before, after) >= line; + } + return walk_view_shift(before, after).scrolled(); +} + + namespace{ // The battle's text box, held for 250ms. @@ -327,7 +390,11 @@ class WalkUntil{ m_before = std::move(after); return {ChunkNext::NEXT_CHUNK, WalkStop::LIMIT}; } - const bool still = still_chunk(change); + // Taps count what scrolled the view, not scenery that moved (r84: + // cloud shadows read as steps). + const bool still = m_spec.input == WalkInput::TAPS + ? !tap_moved(*m_before.frame, *after.frame, still_line(change)) + : change < still_line(change); if (!still){ // A quiet tap and then a moving one: the first moved too. What // blocks a tap still blocks the next; the view barely changed @@ -347,12 +414,11 @@ class WalkUntil{ // NPC measured 17.2 after steps of 63 to 65 (r250a, Floaroma's old // woman), over the fixed line of 16, so for taps "still" is also // anything under 40% of the walk's own steps. - bool still_chunk(double change){ + double still_line(double change){ m_largest = std::max(m_largest, change); - const double line = m_spec.input == WalkInput::TAPS + return m_spec.input == WalkInput::TAPS ? std::max(WALK_TAP_BLOCKED_CHANGE, 0.4 * m_largest) : WALK_BLOCKED_CHANGE; - return change < line; } void escape_battle(const std::string& what){ 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 504edccfe8..08e118c61f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.h @@ -160,6 +160,33 @@ constexpr double WALK_TAP_BLOCKED_CHANGE = 16.0; // More than walking changes in 400ms: a new scene, such as a battle opening. constexpr double WALK_SCENE_CHANGE = 120.0; +// How the view scrolled between two frames, on 192x108 copies: the shift +// (dx, dy) of `before` that best lines up with `after`, leaving out the +// middle where the player stands, and the mean difference per colour +// channel (0 to 255) with and without it. Outside, the camera follows the +// player, so a step scrolls everything one way; a TV, a conveyor, a +// passer-by or a cloud shadow changes part of the view with no common +// scroll. In r83's and r84's Floaroma video, frames 300 ms apart while +// standing (screen changes up to 57, cloud shadows, text boxes) never read +// as scrolled; walking read a shift 2 to 6 times better than none. +struct ViewShift{ + int dx = 0; + int dy = 0; + double unshifted = 0; + double shifted = 0; + + // A shift lines up at least twice as well as none, and none leaves a + // real difference. + bool scrolled() const; +}; +ViewShift walk_view_shift(const ImageViewRGB32& before, const ImageViewRGB32& after); + +// Whether the player moved between `before` and `after`, frames around a +// D-pad tap. Outside: only if the view scrolled. Inside, the camera can +// stay put while the player crosses a room (the house's 1F), so there a +// screen change of at least `line` counts, as it did before. +bool tap_moved(const ImageViewRGB32& before, const ImageViewRGB32& after, double line); + // One straight walk timed finely, to check a map against the game: holds the // stick at `stick` and samples the screen every 100ms until the view has been From d73147dbfeb0be45d575d3f7f44620da71b2d547 Mon Sep 17 00:00:00 2001 From: dayfine Date: Mon, 9 Nov 2026 00:45:48 -0800 Subject: [PATCH 2/2] BDSP walk: the late-tap check counts a scrolled view, as look() does The merge of bdsp/autostory-v4 brought back a call to still_chunk(), which this branch replaced with tap_moved(); use tap_moved() there too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 375f4d29dd..eca572b3ba 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp @@ -478,7 +478,7 @@ class WalkUntil{ ){ VideoSnapshot after = snapshot_or_fail(m_stream, m_spec.label); const double change = walk_screen_change(*m_before_still.frame, *after.frame); - if (!still_chunk(change)){ + if (tap_moved(*m_before_still.frame, *after.frame, still_line(change))){ m_result.moved_chunks++; } }