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 @@ -213,8 +213,11 @@
BANNER,
};

// Looks for the location banner `banner` (and, if `dark_too`, the fade)
// for up to `wait`.
// The mean of r, g and b over the screen, 0 to 255; 255 with no frame.
double brightness_of(const VideoSnapshot& screen){
return screen ? image_average(*screen.frame).sum() / 3 : 255;
}

void keep_banner_frame(const ImageViewRGB32& frame, const std::string& folder, const std::string& banner){
Filesystem::create_directories(Filesystem::Path(folder));
frame.save(folder + now_to_filestring() + "-" + normalize_location_name(banner) + ".png");
Expand All @@ -228,10 +231,12 @@
last_read = read;
}

// Looks for the location banner `banner` for up to `wait`, and with
// `before` (the brightness before the tap) for the fade (tap_faded()).
// Logs each distinct banner read and how the wait ended: the Oreburgh Gate's
// entry missed its banner twice (lab-B, 2026-11-08 15:31 and 15:39) though
// a hand probe saw it 1.5 s after the fade, and nothing said why.
TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, bool dark_too){
TapLook look_after_tap(VideoStream& stream, ProControllerContext& context, const std::string& banner, Milliseconds wait, std::optional<double> before){
const WallClock start = current_time();
const WallClock deadline = start + wait;
std::string last_read;
Expand All @@ -243,7 +248,7 @@
while (true){
context.wait_for_all_requests();
const VideoSnapshot screen = stream.video().snapshot();
if (!dark_too && screen && current_time() >= next_frame){
if (!before && screen && current_time() >= next_frame){
keep_banner_frame(*screen.frame, folder, banner);
next_frame += 500ms;
}
Expand All @@ -252,9 +257,9 @@
if (screen && location_banner_matches(read, banner)){
return TapLook::BANNER;
}
const double brightness = screen ? image_average(*screen.frame).sum() / 3 : 255;
const double brightness = brightness_of(screen);
darkest = std::min(darkest, brightness);
if (dark_too && brightness < FADE_DARKNESS){
if (before && tap_faded(*before, brightness)){
return TapLook::DARK;
}
if (current_time() >= deadline){
Expand All @@ -269,6 +274,19 @@
} // namespace


// Black as the Gym's door fades (< 25), or a drop to dark: a look under
// FADE_DARKNESS is not a fade by itself, since a cave's floor is that dim
// (the Oreburgh Gate's about 46, lab-B 2026-11-08), but a fade into a cave
// from a lit street goes from about 120 to 45..64.
// TODO(#268 slice 3): stopgap. A uniform-screen fade watcher (the shared
// Black/WhiteScreen stddev test, as in upstream #1717) should replace this
// brightness drop; remove it once that watcher passes the Gate frames.
constexpr double TAP_FADE_BLACK = 25;
constexpr double TAP_FADE_DROP = 0.7;
bool tap_faded(double before, double after){
return after < TAP_FADE_BLACK || (after < FADE_DARKNESS && after < before * TAP_FADE_DROP);
}

ConsoleStoryStepIO::ConsoleStoryStepIO(VideoStream& stream, ProControllerContext& context)
: m_stream(stream)
, m_context(context)
Expand Down Expand Up @@ -305,15 +323,26 @@
// A held walk can carry on past a fade that goes unseen: out of the Gym it
// walked on south (r219g), and through the Oreburgh Gate's west mouth on
// west into a pocket by Route 203's pond (r75). Taps during the fade are
// eaten.
// eaten. The banner before the first tap means we are through already:
// the walk before slid onto the warp (lab-B 2026-11-08 15:31).
// TODO(#268 slice 2): stopgap. The "through already" check sees only a
// banner still on screen; once walkers report the fade or banner they saw
// (WalkResult::warp), read that instead and remove this check.
std::optional<size_t> ConsoleStoryStepIO::tap_to_banner(Direction direction, const std::string& map, size_t max_taps){

Check warning on line 331 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::ConsoleStoryStepIO::tap_to_banner( Direction direction , const std :: string & map , size_t max_taps): function_lines=27 (advisory above 25, limit 50)
const std::string& banner = story_banner_name(map);
m_context.wait_for_all_requests();
VideoSnapshot screen = m_stream.video().snapshot();
if (screen && location_banner_matches(read_location_banner(*screen.frame), banner)){
m_stream.log("[Banner] \"" + banner + "\" shows before the first tap: through already.");
return 0;
}
for (size_t tap = 1; tap <= max_taps; tap++){
const double before = brightness_of(screen);
pbf_press_dpad(m_context, dpad_toward(direction), 160ms, 0ms);
TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, true);
TapLook look = look_after_tap(m_stream, m_context, banner, 1500ms, before);
const bool faded = look == TapLook::DARK;
if (faded){
look = look_after_tap(m_stream, m_context, banner, 8000ms, false);
look = look_after_tap(m_stream, m_context, banner, 8000ms, std::nullopt);
}
if (look == TapLook::BANNER){
m_context.wait_for(1000ms);
Expand All @@ -322,6 +351,7 @@
if (faded){
break;
}
screen = m_stream.video().snapshot();
}
return std::nullopt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ class StoryStepIO{
// D-pad taps `direction` one at a time, each looked at, toward a door
// or cave mouth, until the fade (then no more taps) and the location
// banner of `map` (a JSON map's name), for at most `max_taps`. Returns
// the taps it took, or nothing if no banner showed.
// the taps it took (0 if the banner shows before the first: through
// already), or nothing if no banner showed.
virtual std::optional<size_t> tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) = 0;
// Presses through any text until the overworld is quiet; with `fade`, a
// fade must come first (a map change). Fails otherwise.
virtual void settle(Button button, const std::string& label, bool fade) = 0;
};


// Whether a look after a tap toward a door or a cave mouth shows its fade,
// from its brightness `after` and that of the look before the tap,
// `before` (the mean of r, g and b, 0 to 255).
bool tap_faded(double before, double after);


// The console.
class ConsoleStoryStepIO : public StoryStepIO{
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* BDSP AutoStory - Story table

Check warning on line 1 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

<file>: file_lines=999 (advisory above 500, limit 1000)
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -124,6 +124,8 @@
step.why = std::move(why);
return step;
}
// A route toward a door or a warp stops two tiles short, not beside it: an
// open walk there can slide onto it (01.05, lab-B 2026-11-08).
StoryStep route(std::string name, Pose target, std::string why, std::string costs = ""){
StoryStep step = make(StoryStepKind::ROUTE, std::move(name), std::move(why));
step.target = std::move(target);
Expand Down Expand Up @@ -308,7 +310,7 @@
};
}

std::vector<StoryCheckpoint> lake_and_pokedex(){

Check warning on line 313 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::lake_and_pokedex(): function_lines=46 (advisory above 25, limit 50)
return {
row("00.02", "Lake Verity: the starter and the Starly battle", at(LAKEFRONT, "below_lake_entrance", NORTH), {
legs("to_the_lake_entrance", {{.direction = NORTH, .tiles = 1}}, "a tile short of the trigger"),
Expand Down Expand Up @@ -337,7 +339,8 @@
// Line up on the table's corner west first: a plan from either tile
// ran east into Mom at (7, 8), who talks (r29).
route("by_the_counter", at(HOME, 4, 8, WEST), "west against the table's corner (3, 8)"),
route("above_the_door_again", at(HOME, 6, 9, SOUTH), "to above the door"),
anchored(legs("above_the_door_again", {{.direction = EAST, .tiles = 1}, {.direction = SOUTH, .tiles = 1}, {.direction = EAST, .tiles = 1}},
"east, south past the wall (4, 9), and east to above the door: not south toward it"), at(HOME, 6, 9, EAST)),
step_into("out_of_the_house_again", SOUTH, "onto the door's tile (6, 10), and out", at(TWINLEAF, "house_doorstep", SOUTH)),
// Through the grass now, with a starter: wild battles are run from.
route("short_of_dawn", at(ROUTE_201, 63, 5, EAST), "Twinleaf, then east along Route 201 to its east edge"),
Expand Down Expand Up @@ -637,7 +640,7 @@
// for a banner already gone (lab-B, 2026-11-08 15:31, 15:39, 15:47,
// 15:54; frames in Screenshots/AutoStory/banner/).
step_into("into_oreburgh_gate", WEST, "the Gate's mouth (1, 13)", arrival(GATE, {27, 22}, WEST)),
route("through_the_gate", at(GATE, 4, 22, WEST), "west along row 22 to the warp tile by the west mouth (3, 22)"),
route("through_the_gate", at(GATE, 5, 22, WEST), "west along row 22 to two tiles short of the west mouth's warp (3, 22)"),
// Route 203's banner or a failure here: the old walk west "until the
// banner" went on into the pond's pocket (r75). 01.06 heads south.
tap_into("out_of_oreburgh_gate", WEST, "the west mouth (3, 22)", out_of_oreburgh_gate()),
Expand Down Expand Up @@ -674,7 +677,7 @@
// can come up a moment after them; the scene's B closes it.
scene("the_hiker", at(GATE, 7, 22, EAST), "HM Rock Smash, then the Hidden Moves card"),
route("past_curtis", at(GATE, 20, 22, EAST), "east along row 22 past Camper Curtis's column (17), fighting him"),
route("by_the_east_mouth", at(GATE, 27, 22, EAST), "east past Picnicker Diana's column (24), fighting her"),
route("by_the_east_mouth", at(GATE, 26, 22, EAST), "east past Picnicker Diana's column (24), fighting her, to two short of the mouth"),
tap_into("into_oreburgh", EAST, "the Gate's east mouth (28, 22)",
arrival(OREBURGH, map_point(OREBURGH, "gate_mouth").next(EAST), EAST)),
route("west_of_the_welcome", at(OREBURGH, 9, 13, EAST), "east along row 13"),
Expand Down Expand Up @@ -766,7 +769,7 @@
StoryCheckpoint oreburgh_mine_to_roark(){
const Pose below_roark = next_to(MINE, "roark", SOUTH, NORTH);
return row("01.03", "Oreburgh Mine to Roark", out_of_oreburgh_center(), {
route("above_the_mine", at(OREBURGH, 46, 58, SOUTH), "down the door's column to above the mine's warp tiles", "town"),
route("above_the_mine", at(OREBURGH, 46, 57, SOUTH), "down the door's column to two above the mine's warp tiles (44..48, 59)", "town"),
step_into("into_the_mine", SOUTH, "the warp tile (46, 59)", at(MINE_HALL, 12, 1, SOUTH)),
step_into("into_the_coal_room", SOUTH, "down the entrance hall through the cave mouth", at(MINE, 15, 2, SOUTH)),
anchored(to_walls("south_to_the_rock", {{.direction = SOUTH, .tiles = 8}}, "south against the corridor rock (15, 11)"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
#include "Data/PokemonBDSP_AutoStory_PlanCosts.h"
#include "Data/PokemonBDSP_AutoStory_RoutePlanner.h"
#include "Data/PokemonBDSP_AutoStory_StoryMaps.h"
#include "Data/PokemonBDSP_AutoStory_WallRecovery.h"
#include "Actions/PokemonBDSP_AutoStory_Events.h"
#include "PokemonBDSP_AutoStory_StoryExecutor.h"
#include "PokemonBDSP_AutoStory_StoryPlaces.h"
#include "PokemonBDSP_AutoStory_StoryTable.h"
#include "PokemonBDSP_AutoStory_StoryTable_Tests.h"

Expand Down Expand Up @@ -218,6 +221,80 @@
return true;
}

// The legs a ROUTE step from `from` plans, as walk_route() plans them (the
// step's costs, then the default ones), on `world`; nothing if none.
std::optional<std::vector<PlannedLeg>> route_step_plan(const StoryWorld& world, const Pokemon::Pose& from, const Pokemon::StoryStep& step){
std::vector<TilePos> start;
for (const TilePos& pos : Pokemon::tiles_of(from)){
start.push_back(story_to_world(world, from.map, pos));
}
std::vector<TilePos> targets;
for (const TilePos& pos : Pokemon::tiles_of(*step.target)){
targets.push_back(story_to_world(world, step.target->map, pos));
}
const auto goal = [&](const TilePos& pos){ return std::find(targets.begin(), targets.end(), pos) != targets.end(); };
std::optional<std::vector<PlannedLeg>> legs = plan_recovered_route(world, start, goal, *story_route_costs(step.variant));
return legs ? legs : plan_recovered_route(world, start, goal, RobustPlanCosts());
}

// Why `planned` may slide into a door or a warp, or "": an open leg (not to
// a wall) that may end on the tile before one, walking toward it. A part of
// a tile too far takes the warp (lab-B 2026-11-08 15:31 to 15:54: 01.05's
// open walk west to (2, 13), beside the Oreburgh Gate's mouth, slid into
// the Gate, and the tap after it waited for a banner already gone). The
// maps draw every warp as a door tile, cave mouths too.
std::string slides_into_door(const StoryWorld& world, const PlannedLeg& planned){
if (planned.leg.ends_at_wall){
return "";
}
for (const TilePos& pos : planned.after){
const TilePos ahead = pos.next(planned.leg.direction);
if (world.tile(ahead) == Pokemon::TileKind::DOOR){
return std::format("the open leg {} may end on {}, the tile before the door {}",
to_string(planned.leg), describe(world, pos), describe(world, ahead));
}
}
return "";
}

// No ROUTE step of the table plans an open leg that ends before a door it
// walks toward: a door is gone through by a step that runs or taps into it
// from where the route ends on a wall or off its line. Where a row's steps
// leave the player is followed from its start through each step's target
// or pose_after; a route from an unknown place is not checked.
UnitTestResult test_story_routes_stop_short_of_doors(Logger& logger){

Check warning on line 265 in SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonBDSP::test_story_routes_stop_short_of_doors( Logger & logger): function_lines=32 (advisory above 25, limit 50)
std::vector<std::string> found;
size_t checked = 0;
for (const StoryCheckpoint& row : story_checkpoints()){
std::optional<Pokemon::Pose> at = row.start;
for (const Pokemon::StoryStep& step : row.steps){
const bool known = at && !Pokemon::tiles_of(*at).empty();
const StoryWorld* world = step.kind == StoryStepKind::ROUTE && known
? story_plan_world(at->map, step.target->map) : nullptr;
if (step.kind == StoryStepKind::ROUTE && world == nullptr){
logger.log(std::format("{} {}: not checked, from {}.", row.id, step.name, at ? Pokemon::to_string(*at) : "nowhere known"));
}
const std::optional<std::vector<PlannedLeg>> legs = world ? route_step_plan(*world, *at, step) : std::nullopt;
checked += legs ? 1 : 0;
for (const PlannedLeg& planned : legs ? *legs : std::vector<PlannedLeg>{}){
const std::string why = slides_into_door(*world, planned);
if (!why.empty()){
found.push_back(std::format("{} {}: {}.", row.id, step.name, why));
}
}
at = step.kind == StoryStepKind::ROUTE ? step.target
: step.pose_after ? step.pose_after
: step.kind == StoryStepKind::LEGS || step.kind == StoryStepKind::STEP ? std::nullopt : at;
}
}
logger.log(std::format("{} routes planned.", checked));
std::string out;
for (const std::string& line : found){
out += "\n " + line;
}
return found.empty() ? UnitTestResult(true) : UnitTestResult("Open legs that end before a door they walk toward:" + out);
}


struct AutoStoryStoryTableInput{
std::string name;
Expand Down Expand Up @@ -270,6 +347,10 @@
.input = {"Route 204 North: the line into Floaroma", test_route204_north_line},
.expectation = {},
},
{
.input = {"Story table: no open leg ends before a door it walks toward (lab-B, 2026-11-08)", test_story_routes_stop_short_of_doors},
.expectation = {},
},
};

void add_tests_AutoStory_StoryTable(UnitTestDatabase& database){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,52 @@ Outcome StoryWalkSteps::leg_to_map(const StoryStep& step){
return arrived ? Pokemon::outcome_ok() : Pokemon::outcome_failed("no_banner", map);
}

namespace{
// Taps toward a door with no table door ahead of it.
constexpr size_t TAPS_TO_BANNER = 30;
// How many taps `direction` a tap step from `from` takes at most: the
// tiles to the door its map has ahead of every tile of `from`, and two
// more; TAPS_TO_BANNER where the map has none. A step through already,
// its banner gone, then stops a few tiles on (lab-B 2026-11-08: 01.05's
// walk slid into the Oreburgh Gate, whose row 22 runs 24 tiles on).
size_t taps_to_banner(const Pose& from, Direction direction){
size_t most = 0;
try{
const Pokemon::MapData& map = story_map(from.map);
for (const TilePos& pos : Pokemon::tiles_of(from)){
const size_t tiles = Pokemon::tiles_to_door(map, pos, direction);
TilePos door = pos;
for (size_t i = 0; i < tiles; i++){
door = door.next(direction);
}
if (tiles == 0 || map.tiles.at(door) != Pokemon::TileKind::DOOR){
return TAPS_TO_BANNER;
}
most = std::max(most, tiles);
}
}catch (...){
return TAPS_TO_BANNER;
}
return most == 0 ? TAPS_TO_BANNER : most + 2;
}
} // namespace

// A STEP "to_banner": D-pad taps toward a door or cave mouth until the fade
// and pose_after's map's location banner. No banner fails the step right
// there.
Outcome StoryWalkSteps::tap_to_banner(const StoryStep& step){
// there; a banner before any tap means the walk before took us through.
Outcome StoryWalkSteps::tap_to_banner(const Pose& from, const StoryStep& step){
const std::string map = step.pose_after ? step.pose_after->map : "";
const std::optional<size_t> taps = m_io.tap_to_banner(step.direction, map, 30);
const size_t limit = taps_to_banner(from, step.direction);
const std::optional<size_t> taps = m_io.tap_to_banner(step.direction, map, limit);
if (!taps){
return Pokemon::outcome_failed("no_banner", map);
return Pokemon::outcome_failed("no_banner", std::format("{} in {} taps {}: through already, its banner gone?",
map, limit, direction_name(step.direction)));
}
if (*taps == 0){
log(std::format("[{}] {}: {}'s banner before any tap: through already.", m_row, step.name, map));
}else{
log(std::format("[{}] {}: {}'s banner after {} taps {}.", m_row, step.name, map, *taps, direction_name(step.direction)));
}
log(std::format("[{}] {}: {}'s banner after {} taps {}.", m_row, step.name, map, *taps, direction_name(step.direction)));
return Pokemon::outcome_ok();
}

Expand Down Expand Up @@ -340,7 +376,7 @@ Outcome StoryWalkSteps::legs(RunState& state, const StoryStep& step){
Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){
if (step.variant == "to_banner"){
turn(step.direction);
return tap_to_banner(step);
return tap_to_banner(state.pose, step);
}
if (step.variant == "tap"){
// No separate turn tap first: at 1 tile from the door, a turn tap
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class StoryWalkSteps{
Pokemon::Outcome counted_wall(const Pokemon::Pose& from, const Pokemon::StoryStep& step);
Pokemon::Outcome leg_to_dialogue(const Pokemon::StoryStep& step);
Pokemon::Outcome leg_to_map(const Pokemon::StoryStep& step);
Pokemon::Outcome tap_to_banner(const Pokemon::StoryStep& step);
Pokemon::Outcome tap_to_banner(const Pokemon::Pose& from, const Pokemon::StoryStep& step);
void turn(Pokemon::Direction direction);
void log(const std::string& text);

Expand Down
Loading
Loading