diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp index cca86e5ca0..757fe08d62 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp @@ -13,6 +13,7 @@ #include "Common/Cpp/Filesystem/Filesystem.h" #include "Common/Cpp/Filesystem/FileIO.h" #include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageDiff.h" #include "CommonFramework/ImageTypes/ImageRGB32.h" #include "CommonFramework/Exceptions/OperationFailedException.h" #include "CommonFramework/VideoPipeline/VideoFeed.h" @@ -42,6 +43,13 @@ constexpr Milliseconds STICK_RELEASE = 160ms; constexpr Milliseconds SHOTS_INTERVAL = 500ms; constexpr Milliseconds WAIT_FOR_TIMEOUT = 5000ms; +// "walk": the stick is held in slices this long, and the frames between +// slices are compared (RMSD at 192x108). Two slices in a row under the +// threshold count as standing still against something. +constexpr Milliseconds WALK_SLICE = 200ms; +constexpr Milliseconds WALK_LIMIT = 5000ms; +constexpr double WALK_STILL_RMSD = 10; + // How often "wait_for" and "expect" check a new frame. constexpr Milliseconds DETECTOR_POLL_INTERVAL = 100ms; @@ -160,6 +168,7 @@ class LineRunner{ {"dpad", {"dpad UP|DOWN|LEFT|RIGHT [hold_ms] [release_ms]", &LineRunner::dpad}}, {"stick", {"stick L|R x y [hold_ms] [release_ms] (x, y from -1 to 1)", &LineRunner::stick}}, {"wait", {"wait ms", &LineRunner::wait}}, + {"walk", {"walk x y [max_ms] [still_rmsd] (left stick until the screen holds still)", &LineRunner::walk}}, {"shot", {"shot label", &LineRunner::shot}}, {"shots", {"shots label count [interval_ms]", &LineRunner::shots}}, {"detect", {"detect detector [image] (image: a path, or a file in the outbox)", &LineRunner::detect}}, @@ -218,6 +227,45 @@ class LineRunner{ void wait(const std::vector& words){ pbf_wait(context(), optional_duration(words, 1, 0ms)); } + // Holds the left stick until the screen stops changing (a wall, a + // sign, someone in the way) or max_ms passes, and reports how long it + // moved. Measuring a map with walks that stop on their own: a timed + // hold moves a different number of tiles from one try to the next. + void walk(const std::vector& words){ + const JoystickPosition position{optional_number(words, 1, 0), optional_number(words, 2, 0)}; + const Milliseconds limit = optional_duration(words, 3, WALK_LIMIT); + const double still = optional_number(words, 4, WALK_STILL_RMSD); + VideoSnapshot before = current_frame(); + if (!before){ + throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "walk: no video frame"); + } + Milliseconds walked = 0ms; + Milliseconds moving = 0ms; + int quiet = 0; + std::ostringstream changes; + while (walked < limit && quiet < 2){ + pbf_move_left_joystick(context(), position, WALK_SLICE, 0ms); + VideoSnapshot after = current_frame(); + if (!after){ + break; + } + const double change = ImageMatch::pixel_RMSD( + before.frame->scale_to(192, 108), after.frame->scale_to(192, 108) + ); + walked += WALK_SLICE; + changes << " " << (int)change; + if (change < still){ + quiet++; + }else{ + quiet = 0; + moving = walked; + } + before = std::move(after); + } + pbf_wait(context(), STICK_RELEASE); + out() << "walk " << (quiet >= 2 ? "still" : "limit") << " after " << walked.count() + << "ms, moving " << moving.count() << "ms; changes" << changes.str() << "\n"; + } void shot(const std::vector& words){ out() << "shot " << save_screenshot(required_arg(words, 1)) << "\n"; } diff --git a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md index c5b038015a..88adb99c76 100644 --- a/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md +++ b/SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/README.md @@ -74,6 +74,7 @@ Any other line comes from a command, such as `detect home yes` or the path of a | `dpad UP\|DOWN\|LEFT\|RIGHT [hold] [release]` | Presses the D-pad. | hold 100, release 200 | | `stick L\|R x y [hold] [release]` | Moves the left or right stick. `x` and `y` run from -1 to 1; `y` is +1 for up. | hold 160, release 160 | | `wait ms` | Waits. | | +| `walk x y [max] [still]` | Holds the left stick in 200 ms slices until two slices in a row change the screen less than `still` (RMSD at 192x108): a wall or someone in the way. Prints `walk still\|limit after N ms, moving M ms` and each slice's change. For measuring maps: a timed `stick` hold moves a different number of tiles from try to try. | max 5000, still 10 | ### Screen