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 155418cd5a..58d2dabb0b 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 @@ -18,6 +19,7 @@ #include "Utils/PokemonBDSP_AutoStory_RouteWalk.h" #include "Utils/PokemonBDSP_AutoStory_Walk.h" #include "Actions/PokemonBDSP_AutoStory_Talks.h" +#include "Utils/PokemonBDSP_AutoStory_Walk.h" #include "PokemonBDSP_AutoStory_Walk_Tests.h" #include "Utils/PokemonBDSP_AutoStory_WalkResult.h" #include "Utils/PokemonBDSP_AutoStory_WalkScreen.h" @@ -264,6 +266,86 @@ UnitTestResult test_location_banner_showing(Logger& logger){ +// 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; + } + } +} + +UnitTestResult test_view_shift(Logger&){ + 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; +} + // pokemon_center_on_screen(): the Center's warm floor in a room. r79 read // Jubilife's yellow plaza as a Center and called a won battle lost. UnitTestResult test_pokemon_center_on_screen(Logger&){ @@ -405,6 +487,10 @@ const AutoStoryWalkCase AUTO_STORY_WALK_CASES[] = { .input = {"a Pokemon Center is its floor in a room, not a yellow plaza", test_pokemon_center_on_screen}, .expectation = {}, }, + { + .input = {"a step scrolls the view; moving scenery does not", test_view_shift}, + .expectation = {}, + }, { .input = {"a wild battle on an open leg's taps, placed by its screen events", test_leg_battle_place}, .expectation = {}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp index 389302230d..2c4578fadd 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp @@ -162,7 +162,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 e69c7ec902..677d4a32eb 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_Walk.cpp @@ -23,6 +23,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_Location.h" #include "PokemonBDSP_AutoStory_LocationBannerWatcher.h" #include "PokemonBDSP_AutoStory_Walk.h" @@ -113,6 +114,70 @@ WarpFade WarpFadeWatch::push(const Pokemon::TransitionSample& sample){ } +namespace detail{ +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 detail + + namespace{ // The battle's text box, held for 250ms. @@ -442,7 +507,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 @@ -464,12 +533,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; } // What hold()'s watcher `ret` saw (see after_watchers()). @@ -532,7 +600,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++; } } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h index acb7daf3b4..a5b0fe8c4f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WalkScreen.h @@ -36,6 +36,47 @@ 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; +namespace detail{ + +// 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; +}; + +// Calculates the view scroll shift between before and after images. +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); + +} // namespace detail + +// View scroll shift calculation. +using detail::ViewShift; +// Calculates the view scroll shift between before and after images. +using detail::walk_view_shift; +// Whether the player moved between before and after frames. +using detail::tap_moved; + + // Whether two frames show different scenes: a change walking cannot make // (WALK_SCENE_CHANGE). UNKNOWN without both frames. Pokemon::SceneChange walk_scenes_differ(const Pokemon::TransitionSample& before, const Pokemon::TransitionSample& after);