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 @@ -36,6 +36,7 @@
#include "Pokemon/Navigation/Pokemon_LegPlanner_Tests.h"
#include "Pokemon/Navigation/Pokemon_LegWalker_Tests.h"
#include "Pokemon/Navigation/Pokemon_MapData_Tests.h"
#include "Pokemon/Navigation/Pokemon_MoveMechanics_Tests.h"
#include "Pokemon/Navigation/Pokemon_RouteLog_Tests.h"
#include "Pokemon/Navigation/Pokemon_RuntimeMap_Tests.h"
#include "Pokemon/Navigation/Pokemon_WallRecovery_Tests.h"
Expand All @@ -58,7 +59,7 @@



UnitTestDatabase make_UNIT_TESTS_ALL(){

Check warning on line 62 in SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::ComputerPrograms::make_UNIT_TESTS_ALL(): function_lines=43 (advisory above 25, limit 50)
UnitTestDatabase ret;

add_tests_QtEventThreadPool(ret, GlobalThreadPools::computation_normal());
Expand All @@ -82,6 +83,7 @@
Pokemon::add_tests_LegWalker(ret);
Pokemon::add_tests_WallRecovery(ret);
Pokemon::add_tests_MapData(ret);
Pokemon::add_tests_MoveMechanics(ret);
Pokemon::add_tests_RuntimeMap(ret);
Pokemon::add_tests_RouteLog(ret);
Pokemon::add_tests_AutoStory(ret);
Expand Down
119 changes: 119 additions & 0 deletions SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/* Move Mechanics

Check warning on line 1 in SerialPrograms/Source/Pokemon/Navigation/Pokemon_MoveMechanics.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<header>: header_symbols=7 (advisory above 6, limit 8)
*
* From: https://github.com/PokemonAutomation/
*
* How each game moves the player for a press, as measured on the console:
* one entry per game, read by that game's walker and by its sim.
*
*/

#ifndef PokemonAutomation_Pokemon_MoveMechanics_H
#define PokemonAutomation_Pokemon_MoveMechanics_H

#include <optional>
#include "Common/Cpp/Time.h"
#include "Pokemon_Grid.h"

namespace PokemonAutomation{
namespace Pokemon{


// How the walker presses an open leg, one that ends short of a wall.
enum class OpenLegPress{
STEP_TAPS, // a step tap a tile: the count is exact
TIMED_HOLD, // one hold, timed by the tile time
};

// Time per tile along each axis of the screen.
struct TileTime{
Milliseconds across{}; // east or west
Milliseconds up_down{}; // north or south

constexpr Milliseconds along(Direction direction) const{
return direction == Direction::EAST || direction == Direction::WEST ? across : up_down;
}
};

// A tap that takes one step: the press, and the release before the next
// press. Their sum is the time per tapped tile.
struct StepTap{
Milliseconds hold{};
Milliseconds release{};
};

// One way of moving: walking, running or riding.
struct Gait{
TileTime tile;
// The step tap in this gait; a game's taps differ by gait (FRLG's
// Bicycle taps far faster than its walk).
StepTap tap;
OpenLegPress open_legs = OpenLegPress::STEP_TAPS;
};

// How one game moves the player: its turn tap, and a tile time and step
// tap per gait.
struct MoveMechanics{
// A tap that turns a player facing another way, without a step.
Milliseconds turn_tap{};
// From pressing, standing still, to the first step under way.
Milliseconds start_delay{};
Gait walk;
// B held, once the player has the Running Shoes.
Gait run;
// None where the walker does not ride yet.
std::optional<Gait> bike;
};


// Brilliant Diamond and Shining Pearl, measured on console B. The camera
// tilts, so a row takes longer than a column. D-pad taps move exactly a
// tile each, while a held run overshoots about 3 tiles in 10 (2026-11-09,
// Jubilife), so open legs are tapped. Taps: PokemonBDSP_AutoStory_RouteInput.h;
// tile times: PokemonBDSP_AutoStory_RouteTiming.h.
inline constexpr MoveMechanics BDSP_MOVE_MECHANICS{
.turn_tap = Milliseconds(50),
.start_delay = Milliseconds(250),
.walk = {
.tile = {.across = Milliseconds(160), .up_down = Milliseconds(240)},
.tap = {.hold = Milliseconds(160), .release = Milliseconds(640)},
.open_legs = OpenLegPress::STEP_TAPS,
},
.run = {
.tile = {.across = Milliseconds(100), .up_down = Milliseconds(150)},
.tap = {.hold = Milliseconds(160), .release = Milliseconds(640)},
.open_legs = OpenLegPress::STEP_TAPS,
},
.bike = std::nullopt,
};

// FireRed and LeafGreen, measured on console B: a walking tile is 16
// frames, running 8, and a held leg stops where its time says, so open
// legs on foot are timed holds; on the Bicycle they are counted in taps,
// 40 ms at one per 100 ms (BIKE_TAP_PERIOD_WORKED; at a 60 ms period one
// tap in 12 drops). The walker's holds count no start delay.
// Measurements: PokemonFRLG_MovementMechanics.h and
// PokemonFRLG_BikeSpinMechanics.h.
inline constexpr MoveMechanics FRLG_MOVE_MECHANICS{
.turn_tap = Milliseconds(40),
.start_delay = Milliseconds(0),
.walk = {
.tile = {.across = Milliseconds(267), .up_down = Milliseconds(267)},
.tap = {.hold = Milliseconds(66), .release = Milliseconds(700)},
.open_legs = OpenLegPress::TIMED_HOLD,
},
.run = {
.tile = {.across = Milliseconds(133), .up_down = Milliseconds(133)},
.tap = {.hold = Milliseconds(66), .release = Milliseconds(700)},
.open_legs = OpenLegPress::TIMED_HOLD,
},
.bike = Gait{
.tile = {.across = Milliseconds(100), .up_down = Milliseconds(100)},
.tap = {.hold = Milliseconds(40), .release = Milliseconds(60)},
.open_legs = OpenLegPress::STEP_TAPS,
},
};


} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/* Move Mechanics Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon_MoveMechanics.h"
#include "Pokemon_MoveMechanics_Tests.h"

namespace PokemonAutomation{
namespace Pokemon{
namespace{


struct MoveMechanicsInput{
std::string_view game;
MoveMechanics mechanics;
};
struct MoveMechanicsExpectation{
// How the walker presses an open leg on foot (the user's pick,
// 2026-11-11: BDSP taps, FRLG timed holds).
OpenLegPress open_legs_on_foot;
};
struct MoveMechanicsCase{
MoveMechanicsInput input;
MoveMechanicsExpectation expectation;
};

// Why `faster` is not faster than `slower` on both axes, if it is not.
std::optional<std::string> not_faster(std::string_view faster_name, const Gait& faster, std::string_view slower_name, const Gait& slower){
if (faster.tile.across >= slower.tile.across || faster.tile.up_down >= slower.tile.up_down){
return std::format("{} ({}/{} ms) is not faster than {} ({}/{} ms).",
faster_name, faster.tile.across.count(), faster.tile.up_down.count(),
slower_name, slower.tile.across.count(), slower.tile.up_down.count());
}
return std::nullopt;
}

// Why the turn tap would step on foot in `gait`, if it would: a turn tap
// must be shorter than the gait's step tap.
std::optional<std::string> turn_steps(std::string_view gait_name, const Gait& gait, Milliseconds turn_tap){
if (turn_tap >= gait.tap.hold){
return std::format("The turn tap ({} ms) is not shorter than the {} step tap ({} ms).",
turn_tap.count(), gait_name, gait.tap.hold.count());
}
return std::nullopt;
}

// Why a gait's step tap is unusable, if it is: both its press and its
// release must take time.
std::optional<std::string> empty_tap(std::string_view gait_name, const Gait& gait){
if (gait.tap.hold <= Milliseconds(0) || gait.tap.release <= Milliseconds(0)){
return std::format("The {} step tap is {} ms pressed, {} ms released; both must be over 0.",
gait_name, gait.tap.hold.count(), gait.tap.release.count());
}
return std::nullopt;
}

std::string_view open_leg_press_name(OpenLegPress press){
return press == OpenLegPress::STEP_TAPS ? "step taps" : "a timed hold";
}

// Why `gait` presses its open legs other than `expected`, if it does.
std::optional<std::string> wrong_open_legs(std::string_view gait_name, const Gait& gait, OpenLegPress expected){
if (gait.open_legs != expected){
return std::format("Open legs {} are pressed as {}, expected {}.",
gait_name, open_leg_press_name(gait.open_legs), open_leg_press_name(expected));
}
return std::nullopt;
}

class Test_MoveMechanics : public UnitTest{
public:
explicit Test_MoveMechanics(MoveMechanicsCase scenario)
: UnitTest(std::format("Pokemon::MoveMechanics - {}", scenario.input.game)), m_scenario(std::move(scenario))
{}
virtual UnitTestResult run(Logger&, CancellableScope&) const override{
const MoveMechanics& game = m_scenario.input.mechanics;
const OpenLegPress expected = m_scenario.expectation.open_legs_on_foot;
const std::optional<std::string> failures[] = {
empty_tap("walking", game.walk),
empty_tap("running", game.run),
game.bike ? empty_tap("bike", *game.bike) : std::nullopt,
turn_steps("walking", game.walk, game.turn_tap),
turn_steps("running", game.run, game.turn_tap),
not_faster("running", game.run, "walking", game.walk),
game.bike ? not_faster("the bike", *game.bike, "running", game.run) : std::nullopt,
wrong_open_legs("walking", game.walk, expected),
wrong_open_legs("running", game.run, expected),
};
for (const std::optional<std::string>& failure : failures){
if (failure){
return *failure;
}
}
return true;
}
private:
MoveMechanicsCase m_scenario;
};

const MoveMechanicsCase MOVE_MECHANICS_CASES[] = {
{
.input = {.game = "BDSP", .mechanics = BDSP_MOVE_MECHANICS},
.expectation = {.open_legs_on_foot = OpenLegPress::STEP_TAPS},
},
{
.input = {.game = "FRLG", .mechanics = FRLG_MOVE_MECHANICS},
.expectation = {.open_legs_on_foot = OpenLegPress::TIMED_HOLD},
},
};


} // namespace

void add_tests_MoveMechanics(UnitTestDatabase& database){
for (const MoveMechanicsCase& scenario : MOVE_MECHANICS_CASES){
database.add<Test_MoveMechanics>(scenario);
}
}


} // namespace Pokemon
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Move Mechanics Tests
*
* From: https://github.com/PokemonAutomation/
*
*/

#ifndef PokemonAutomation_Pokemon_MoveMechanics_Tests_H
#define PokemonAutomation_Pokemon_MoveMechanics_Tests_H

#include "Common/Cpp/TestRunners/UnitTestDatabase.h"

namespace PokemonAutomation{
namespace Pokemon{


// Registers the checks on each game's movement table.
void add_tests_MoveMechanics(UnitTestDatabase& database);


} // namespace Pokemon
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ size_t RouteWalker::unstick_wall_leg(LegWalk& walk, size_t walked_tiles, size_t
if (walk.result.stop == WalkStop::BLOCKED && walked_tiles + 2 < nearest){
m_io.note("[Route] " + walk.label + ": stopped after about " + std::to_string(walked_tiles) +
" tiles, short of the wall; two D-pad taps, then on to the wall.", RouteNote::WARNING);
m_io.press({.kind = RouteInputKind::DPAD, .direction = walk.planned.leg.direction, .hold = 160ms, .release = 640ms,
m_io.press({.kind = RouteInputKind::DPAD, .direction = walk.planned.leg.direction, .hold = BDSP_MOVE_MECHANICS.walk.tap.hold, .release = BDSP_MOVE_MECHANICS.walk.tap.release,
.count = 2, .end = RouteInputEnd::DONE});
walk.result = m_io.walk_to_wall({.direction = walk.planned.leg.direction, .limit = limit, .label = walk.label, .options = run_watching(walk)});
walked_tiles += tiles_moved(walk.result, per_tile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "CommonFramework/Tools/VideoStream.h"
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon/Navigation/Pokemon_MoveMechanics.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -25,7 +26,7 @@ namespace PokemonBDSP{
// stick tap stepped about 0.6 tile, off the tile grid, and a 160 ms D-pad
// tap steps a whole tile (r49: a stick "turn" put us a tile west of
// Sandgem's Center door; r248a).
constexpr Milliseconds ROUTE_TURN_TAP = Milliseconds(50);
constexpr Milliseconds ROUTE_TURN_TAP = Pokemon::BDSP_MOVE_MECHANICS.turn_tap;

// What a RouteInput presses.
enum class RouteInputKind{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Route timing

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Utils/PokemonBDSP_AutoStory_RouteTiming.h

View workflow job for this annotation

GitHub Actions / code-health

Code health (bypass)

<header>: header_symbols=9 over limit 8, bypassed by the tag at line 8: "one table of measured walking and running times per tile; split, the walk and run rows lose their pairing"
*
* From: https://github.com/PokemonAutomation/
*
Expand All @@ -14,6 +14,7 @@
#include "Common/Cpp/Time.h"
#include "Controllers/Joystick.h"
#include "Pokemon/Navigation/Pokemon_Grid.h"
#include "Pokemon/Navigation/Pokemon_MoveMechanics.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -22,11 +23,11 @@

// Walking time per tile, measured on the console from the post-catch save:
// a row is deeper than a column is wide under the tilted camera.
constexpr Milliseconds ROUTE_MS_PER_TILE_ACROSS = Milliseconds(160); // east or west
constexpr Milliseconds ROUTE_MS_PER_TILE_ACROSS = Pokemon::BDSP_MOVE_MECHANICS.walk.tile.across;
// Walking time per vertical tile (north or south).
constexpr Milliseconds ROUTE_MS_PER_TILE_UP_DOWN = Milliseconds(240); // north or south
constexpr Milliseconds ROUTE_MS_PER_TILE_UP_DOWN = Pokemon::BDSP_MOVE_MECHANICS.walk.tile.up_down;
// From pressing the stick to the first step, when standing still.
constexpr Milliseconds ROUTE_START_DELAY = Milliseconds(250);
constexpr Milliseconds ROUTE_START_DELAY = Pokemon::BDSP_MOVE_MECHANICS.start_delay;
// How fast the player moves on a leg to a wall (B held with the D-pad).
// B only runs once Mom has given the Running Shoes (00.03); before that
// it walks. The bike comes later, with its own measured times.
Expand All @@ -40,9 +41,9 @@
// tile) where walking took 240. Across is scaled the same until measured.
// Rounded down: a count from too fast a pace runs over, which a wall leg
// forgives, where one from too slow a pace stops short of the wall.
constexpr Milliseconds ROUTE_RUN_MS_PER_TILE_ACROSS = Milliseconds(100);
constexpr Milliseconds ROUTE_RUN_MS_PER_TILE_ACROSS = Pokemon::BDSP_MOVE_MECHANICS.run.tile.across;
// Running time per vertical tile (north or south).
constexpr Milliseconds ROUTE_RUN_MS_PER_TILE_UP_DOWN = Milliseconds(150);
constexpr Milliseconds ROUTE_RUN_MS_PER_TILE_UP_DOWN = Pokemon::BDSP_MOVE_MECHANICS.run.tile.up_down;

// Time per tile in `direction` at `speed`.
Milliseconds route_ms_per_tile(Pokemon::Direction direction, MoveSpeed speed = MoveSpeed::WALK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "CommonTools/Async/InferenceRoutines.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "Pokemon/Navigation/Pokemon_MoveMechanics.h"
#include "Pokemon/Navigation/Pokemon_RouteLog.h"
#include "PokemonBDSP/Inference/PokemonBDSP_DialogDetector.h"
#include "PokemonBDSP/Programs/AutoStory/Detect/PokemonBDSP_AutoStory_OverworldDetector.h"
Expand All @@ -34,19 +35,9 @@ using namespace std::chrono_literals;
using namespace Pokemon;


namespace{

bool across(Direction direction){
return direction == Direction::EAST || direction == Direction::WEST;
}

} // namespace

Milliseconds route_ms_per_tile(Direction direction, MoveSpeed speed){
if (speed == MoveSpeed::RUN){
return across(direction) ? ROUTE_RUN_MS_PER_TILE_ACROSS : ROUTE_RUN_MS_PER_TILE_UP_DOWN;
}
return across(direction) ? ROUTE_MS_PER_TILE_ACROSS : ROUTE_MS_PER_TILE_UP_DOWN;
const MoveMechanics& game = BDSP_MOVE_MECHANICS;
return (speed == MoveSpeed::RUN ? game.run : game.walk).tile.along(direction);
}

JoystickPosition route_stick(Direction direction){
Expand Down
Loading
Loading