Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,32 @@ DpadPosition parse_dpad(const std::string& text){
return iter->second;
}

TapsArguments parse_taps(const std::vector<std::string>& words){
// 160 ms is a whole step in BDSP; 640 ms lets the step animation finish
// before the next frame is compared. A step there measures 21 to 50, a
// bump into a wall or an NPC brushing past 4 to 13.
constexpr Milliseconds TAP_HOLD(160);
constexpr Milliseconds TAP_RELEASE(640);
constexpr double TAP_STILL_RMSD = 16;

TapsArguments taps;
taps.direction = parse_dpad(required_arg(words, 1));
const double count = optional_number(words, 2, 0);
if (words.size() < 3 || count < 1 || count != (size_t)count){
throw ParseException(words[0] + ": argument 2 must be a whole number of taps, got " + (words.size() < 3 ? "nothing" : words[2]));
}
taps.count = (size_t)count;
size_t next = 3;
if (words.size() > next && words[next] == "all"){
taps.all = true;
next++;
}
taps.hold = optional_duration(words, next, TAP_HOLD);
taps.release = optional_duration(words, next + 1, TAP_RELEASE);
taps.still = optional_number(words, next + 2, TAP_STILL_RMSD);
return taps;
}

std::optional<Filesystem::Path> next_command_file(const std::string& inbox){
std::optional<Filesystem::Path> next;
for (const Filesystem::Path& path : Filesystem::list_directory(Filesystem::Path(inbox))){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ Button parse_buttons(const std::string& text);
// "UP", "DOWN", "LEFT" or "RIGHT". Throws ParseException otherwise.
DpadPosition parse_dpad(const std::string& text);

// The arguments of "taps DIR N [all] [hold_ms] [release_ms] [still_rmsd]":
// N D-pad taps, each measured against the frame before it.
struct TapsArguments{
DpadPosition direction = DPAD_NONE;
size_t count = 0;
bool all = false; // Keep tapping after two still taps in a row.
Milliseconds hold = Milliseconds(0);
Milliseconds release = Milliseconds(0);
double still = 0; // A tap changing the screen less than this (RMSD at 192x108) didn't move.
};
TapsArguments parse_taps(const std::vector<std::string>& words);

// The ".txt" file in `inbox` whose name sorts first, if any.
std::optional<Filesystem::Path> next_command_file(const std::string& inbox);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Command File Runner

Check warning on line 1 in SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=591 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -50,6 +50,15 @@
constexpr Milliseconds WALK_LIMIT = 5000ms;
constexpr double WALK_STILL_RMSD = 10;

// "walk" and "taps" compare frames scaled to this size, so the change
// measures movement rather than noise.
constexpr size_t CHANGE_WIDTH = 192;
constexpr size_t CHANGE_HEIGHT = 108;

// "taps": two taps in a row that don't move the screen mean a wall, unless
// the line says "all".
constexpr int TAPS_STILL_LIMIT = 2;

// How often "wait_for" and "expect" check a new frame.
constexpr Milliseconds DETECTOR_POLL_INTERVAL = 100ms;

Expand Down Expand Up @@ -100,6 +109,13 @@
return iter->second.second;
}

// How much the screen changed between two frames: RMSD at 192x108.
double screen_change(const VideoSnapshot& before, const VideoSnapshot& after){
return ImageMatch::pixel_RMSD(
before.frame->scale_to(CHANGE_WIDTH, CHANGE_HEIGHT), after.frame->scale_to(CHANGE_WIDTH, CHANGE_HEIGHT)
);
}

}


Expand All @@ -122,9 +138,8 @@
// Runs the lines of one command file.
class LineRunner{
public:
LineRunner(CommandFileSession& session, std::string name)
explicit LineRunner(CommandFileSession& session)
: m_session(session)
, m_name(std::move(name))
{}

// Runs one line. Returns false for "stop". Throws to fail the line.
Expand All @@ -151,7 +166,7 @@
if (!screen){
return "(no video frame)";
}
const std::string path = m_session.outbox + m_name + "_" + label + ".png";
const std::string path = m_session.outbox + m_session.name + "_" + label + ".png";
screen.frame->save(path);
return path;
}
Expand All @@ -169,6 +184,7 @@
{"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}},
{"taps", {"taps DIR N [all] [hold_ms] [release_ms] [still_rmsd] (D-pad taps, each reported moved or still)", &LineRunner::taps}},
{"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}},
Expand Down Expand Up @@ -231,7 +247,7 @@
// 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<std::string>& words){

Check warning on line 250 in SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::LineRunner::walk( const std :: vector<std::string> & words): function_lines=33 (advisory above 25, limit 50)
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);
Expand All @@ -249,9 +265,7 @@
if (!after){
break;
}
const double change = ImageMatch::pixel_RMSD(
before.frame->scale_to(192, 108), after.frame->scale_to(192, 108)
);
const double change = screen_change(before, after);
walked += WALK_SLICE;
changes << " " << (int)change;
if (change < still){
Expand All @@ -266,6 +280,43 @@
out() << "walk " << (quiet >= 2 ? "still" : "limit") << " after " << walked.count()
<< "ms, moving " << moving.count() << "ms; changes" << changes.str() << "\n";
}
// Taps the D-pad N times and reports, for each tap, whether the screen
// changed more than `still` between the frame before and after it: a step
// moves the screen, a bump into a wall doesn't. Stops after two still
// taps in a row unless the line says "all". A tap toward a new direction
// turns and steps, so every tap is a step unless something blocks it.
void taps(const std::vector<std::string>& words){

Check warning on line 288 in SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::LineRunner::taps( const std :: vector<std::string> & words): function_lines=32 (advisory above 25, limit 50)
const TapsArguments args = parse_taps(words);
VideoSnapshot before = current_frame();
if (!before){
throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "taps: no video frame");
}
size_t moved = 0;
size_t tapped = 0;
int quiet = 0;
while (tapped < args.count && (args.all || quiet < TAPS_STILL_LIMIT)){
pbf_press_dpad(context(), args.direction, args.hold, args.release);
VideoSnapshot after = current_frame();
if (!after){
throw OperationFailedException(ErrorReportMode::NO_ERROR_REPORT, "taps: lost the video frame");
}
const double change = screen_change(before, after);
tapped++;
if (change < args.still){
quiet++;
}else{
quiet = 0;
moved++;
}
out() << "tap " << tapped << ": change " << (int)change << (change < args.still ? " still" : " moved") << "\n";
before = std::move(after);
}
out() << "moved " << moved << " of " << args.count;
if (tapped < args.count){
out() << ", stopped after " << tapped << " taps: " << TAPS_STILL_LIMIT << " still in a row";
}
out() << "\n";
}
void shot(const std::vector<std::string>& words){
out() << "shot " << save_screenshot(required_arg(words, 1)) << "\n";
}
Expand Down Expand Up @@ -358,7 +409,6 @@
}

CommandFileSession& m_session;
std::string m_name;
};


Expand Down Expand Up @@ -422,8 +472,8 @@
env.log("Command File Runner: running " + name);

std::ostringstream out;
CommandFileSession session{env, context, out, folder + "outbox/"};
LineRunner runner(session, name);
CommandFileSession session{env, context, out, folder + "outbox/", name};
LineRunner runner(session);
const std::string out_path = session.outbox + name;

std::istringstream lines(script);
Expand All @@ -450,6 +500,15 @@



void run_command_file_line(CommandFileSession& session, const std::vector<std::string>& words){
if (words.empty()){
throw ParseException("run_command_file_line: empty line");
}
LineRunner(session).run(words);
}



CommandFileRunner_Descriptor::CommandFileRunner_Descriptor()
: SingleSwitchProgramDescriptor(
"NintendoSwitch:CommandFileRunner",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct CommandFileSession{
ProControllerContext& context;
std::ostream& out; // Text printed here goes into the .out file.
std::string outbox; // Folder for files a command saves, ending in '/'.
std::string name; // The command file's name: the prefix of the screenshots it saves.
};

// A command. `words` is its line split on spaces; words[0] is the command name.
Expand All @@ -66,6 +67,10 @@ using CommandFileDetectorFactory = std::function<std::unique_ptr<StaticScreenDet
void register_command_file_command(const std::string& name, const std::string& usage, CommandFileCommand command);
void register_command_file_detector(const std::string& name, CommandFileDetectorFactory factory);

// Runs one command line, built-in or registered, from inside a command: a
// game command can start with {"close_game"} and end with {"state"}.
void run_command_file_line(CommandFileSession& session, const std::vector<std::string>& words);



class CommandFileRunner_Descriptor : public SingleSwitchProgramDescriptor{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@
return true;
}

UnitTestResult test_taps(){

Check warning on line 114 in SerialPrograms/Source/NintendoSwitch/Programs/CommandFileRunner/NintendoSwitch_CommandFileRunner_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::test_taps(): function_lines=29 (advisory above 25, limit 50)
struct Case{
std::string line;
TapsArguments expected;
};
const Case CASES[] = {
{.line = "taps UP 3", .expected = {.direction = DPAD_UP, .count = 3, .all = false, .hold = 160ms, .release = 640ms, .still = 16}},
{.line = "taps LEFT 4 all", .expected = {.direction = DPAD_LEFT, .count = 4, .all = true, .hold = 160ms, .release = 640ms, .still = 16}},
{.line = "taps RIGHT 2 100 500", .expected = {.direction = DPAD_RIGHT, .count = 2, .all = false, .hold = 100ms, .release = 500ms, .still = 16}},
{.line = "taps DOWN 1 all 200 700 20", .expected = {.direction = DPAD_DOWN, .count = 1, .all = true, .hold = 200ms, .release = 700ms, .still = 20}},
};
for (const Case& c : CASES){
const TapsArguments taps = parse_taps(split_command_line(c.line));
if (taps.direction != c.expected.direction || taps.count != c.expected.count || taps.all != c.expected.all){
return c.line + ": wrong direction, count or all flag.";
}
if (taps.hold != c.expected.hold || taps.release != c.expected.release || taps.still != c.expected.still){
return c.line + ": expected hold " + std::to_string(c.expected.hold.count())
+ ", release " + std::to_string(c.expected.release.count()) + ", still " + std::to_string(c.expected.still)
+ "; read " + std::to_string(taps.hold.count()) + ", " + std::to_string(taps.release.count()) + ", " + std::to_string(taps.still);
}
}
for (const char* line : {"taps UP", "taps UP x", "taps UP 0", "taps UP 2.5", "taps NORTH 2", "taps UP 2 all soon"}){
if (!throws_parse_error([&]{ parse_taps(split_command_line(line)); })){
return std::string(line) + " should be a parse error.";
}
}
return true;
}

UnitTestResult test_next_file(){
const Filesystem::Path inbox = Filesystem::unique_temp_path(
Filesystem::application_scratch_path() / "CommandFileRunnerTest"
Expand Down Expand Up @@ -163,6 +193,7 @@
database.add<Test_CommandFileRunner>("argument errors", test_argument_errors);
database.add<Test_CommandFileRunner>("buttons", test_buttons);
database.add<Test_CommandFileRunner>("D-pad", test_dpad);
database.add<Test_CommandFileRunner>("taps arguments", test_taps);
database.add<Test_CommandFileRunner>("next inbox file", test_next_file);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Any other line comes from a command, such as `detect home yes` or the path of a
| --- | --- | --- |
| `press BTN[+BTN] [hold] [release]` | Presses one button, or several together, joined with `+` (e.g. `L+R`). Buttons: `A B X Y L R ZL ZR PLUS MINUS LCLICK RCLICK HOME CAPTURE`. | hold 160, release 160 |
| `dpad UP\|DOWN\|LEFT\|RIGHT [hold] [release]` | Presses the D-pad. | hold 100, release 200 |
| `taps DIR N [all] [hold] [release] [still]` | Taps the D-pad `N` times and measures the screen change (RMSD at 192x108, as `walk`) between the frame before and after each tap. Prints `tap k: change X moved\|still` per tap, `moved` when the change is at least `still`, then `moved M of N`. Stops after two still taps in a row (a wall) unless `N` is followed by `all`. Every tap is a step unless something blocks it: a tap toward a new direction turns and steps within the same 160 ms (measured 2026-10-31 in Floaroma: a left tap from facing north moved one tile, with a screen change of about 37, the same as a plain step). On BDSP a step measures 21 to 50, a bump into a wall or an NPC brushing past 4 to 13. | hold 160, release 640, still 16 |
| `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 |
Expand Down Expand Up @@ -111,7 +112,34 @@ These work on any game:
| `black` | A black screen. |
| `white` | A white screen. |

Games can add their own commands and detectors with `register_command_file_command()` and `register_command_file_detector()` (see `NintendoSwitch_CommandFileRunner.h`); `help` lists them.
### Pokemon Brilliant Diamond and Shining Pearl

| Name | Detects |
| --- | --- |
| `bdsp_dialog` | A short dialog box: talking to someone, signs, item pickups, the EXP text after a battle. |
| `bdsp_battle_text` | The long dialog box that only appears in battle. |
| `bdsp_battle_menu` | The Fight / Pokemon / Bag / Run menu of a standard (wild) battle. |
| `bdsp_exp_gain` | The experience gain screen after a battle. |
| `bdsp_menu` | The X menu. |
| `bdsp_map` | The town map. |

These are the detectors a lab operator checks between steps while measuring maps and story routes: dialog and the battle menu say what stopped a walk, the menu and map say where a key press landed. They are all cheap box checks, so `state` stays fast.

## Game-specific commands

### Pokemon Brilliant Diamond and Shining Pearl

| Command | What it does |
| --- | --- |
| `bdsp_load_save` | From anywhere: `close_game`, launch the tile the Home menu has highlighted (A, A, wait 35 s, A, wait 3 s, A, A held 6 s, wait 8 s) and print `state`. |

**`bdsp_load_save` launches whatever Home has highlighted, and Home highlights the last game played, not BDSP.** Before sending it, take a `shot` of the Home menu and move the highlight to BDSP with `dpad`; a wrong tile launches another game and the command reports its `state` as if nothing were wrong. The console has been sent into Pokemon Violet this way.

### How a game registers them

A game adds commands and detectors with `register_command_file_command()` and `register_command_file_detector()` from `NintendoSwitch_CommandFileRunner.h`, and `help` lists them. The registration has to run at startup, before any command file: put it in a function in the game's own module (BDSP: `register_command_file_commands()` in `PokemonBDSP/Programs/PokemonBDSP_CommandFileCommands.cpp`, guarded so a second call does nothing) and call it from the game's `PanelListFactory` constructor, which the program list builds when the app starts. A static registrar object would not do: the game code is in a static library, and the linker drops an object file nothing references.

A registered command gets a `CommandFileSession` (the environment, controller context, output stream and outbox) and its line's words, and can run built-in commands with `run_command_file_line()`, as `bdsp_load_save` does for `close_game` and `state`. Detector names are prefixed with the game (`bdsp_`) so `state` output reads at a glance.

## Options

Expand Down
7 changes: 6 additions & 1 deletion SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Panels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include "PokemonBDSP_Panels.h"
#include "PokemonBDSP_Settings.h"
#include "Programs/PokemonBDSP_CommandFileCommands.h"

#include "Programs/General/PokemonBDSP_MassRelease.h"
#include "Programs/General/PokemonBDSP_AutonomousBallThrower.h"
Expand Down Expand Up @@ -55,7 +56,11 @@ PanelListFactory::PanelListFactory()
Pokemon::STRING_POKEMON + " Brilliant Diamond and Shining Pearl",
RESOURCE_PATH() + "CategoryIcons/PokemonBDSP.png"
)
{}
{
// At startup, so the Command File Runner (under Nintendo Switch) can
// probe BDSP screens without BDSP's program list being opened first.
register_command_file_commands();
}

std::vector<PanelEntry> PanelListFactory::make_panels() const{
std::vector<PanelEntry> ret;
Expand Down
Loading
Loading