diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp index b322e2b1fb..aa2658e204 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_SaveCardReader.cpp @@ -73,6 +73,9 @@ class PlaceNameMatcher : public OCR::SmallDictionaryMatcher{ {"route-3", "Route 3"}, {"route-4", "Route 4"}, {"cerulean-city", "Cerulean City"}, + {"route-21", "Route 21"}, + {"route-22", "Route 22"}, + {"route-23", "Route 23"}, {"route-24", "Route 24"}, {"route-25", "Route 25"}, {"route-10", "Route 10"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.cpp new file mode 100644 index 0000000000..65693ac2d1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.cpp @@ -0,0 +1,78 @@ +/* Overworld Bicycle + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" +#include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h" +#include "PokemonFRLG_BikeSpinPresses.h" +#include "PokemonFRLG_OverworldBike.h" +#include "PokemonFRLG_PlayerFacingReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ +namespace OverworldWalkSteps{ + +using namespace MovementMechanics; + + +bool select_onto_bike(const BikeSelectCalls& calls){ + for (int press = 1; press <= 2; press++){ + calls.press_select(); + if (calls.riding()){ + return true; + } + if (calls.wait_to_read_again){ + calls.wait_to_read_again(); + } + if (calls.riding()){ + return true; + } + } + return false; +} + +bool screen_shows_riding(const ImageViewRGB32& screen, PlayerGender player){ + if (StartMenuDetector(COLOR_RED).detect(screen) || SelectionDialogDetector(COLOR_RED).detect(screen) + || WhiteDialogDetector(COLOR_RED).detect(screen) + ){ + return false; + } + return !read_player_facing(screen, player).has_value(); +} + +void get_on_bike(ConsoleHandle& console, ProControllerContext& context, PlayerGender player){ + BikeSelectCalls calls; + calls.press_select = [&]{ + pbf_press_button(context, BUTTON_MINUS, BIKE_SELECT_PRESS, BIKE_SELECT_SETTLE); + }; + calls.riding = [&]{ + context.wait_for_all_requests(); + VideoSnapshot screen = console.video().snapshot(); + const bool riding = screen && screen_shows_riding(*screen.frame, player); + console.log(riding ? "Bike: riding." : "Bike: not riding after SELECT.", riding ? COLOR_BLACK : COLOR_ORANGE); + return riding; + }; + calls.wait_to_read_again = [&]{ + pbf_wait(context, BIKE_SELECT_SETTLE); + }; + console.log("Bike: SELECT (MINUS) onto the Bicycle."); + if (!select_onto_bike(calls)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "get_on_bike(): not on the Bicycle after SELECT twice. Is it registered?", + console + ); + } +} + + +} // namespace OverworldWalkSteps +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp index bb0f438d3f..aace2d5321 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp @@ -87,6 +87,19 @@ bool is_wall_hold(const RouteStep& next){ return next.action == RouteAction::WALK && next.to_wall && !next.warps; } +// Whether `step`, an open leg on the water, is followed by `next`, another +// open leg: nothing between them shows where `step` ended (console B, +// 2026-11-13, 12.03: "south 86 (open)" down Route 21 stopped short at a +// sand bar unseen, and "east 7 (open)" went on from there). +// Only a long leg: the sims' shorter ones (Viridian's pond "east 5", +// Route 23's "north 17") have no walls near enough to check them by, and +// the unseen stop was 86 tiles into the leg. +constexpr int OPEN_WATER_CHECKED_TILES = 20; +bool open_water_chain(const RouteStep& step, const RouteStep& next){ + return step.surfing && !step.to_wall && !step.warps && step.tiles > OPEN_WATER_CHECKED_TILES + && next.action == RouteAction::WALK && !next.to_wall && !next.warps && !next.spin_tiles; +} + // Whether `next` steps onto a spinner. From a tile short of its start it // misses the arrow, or walks onto another, and ends elsewhere in the maze: // "south 2" before Rocket Hideout B3F's "east 2" onto the arrow (3, 16) @@ -254,12 +267,9 @@ class RouteRun{ continue; } if (outcome.end == StepEnd::DONE){ - const std::optional start = m_next >= m_unchecked ? m_at : std::nullopt; - remember_walked(step); - m_at = step.end; - m_facing = step.facing_after; - m_lines.done(step, ++m_next, m_holds); - check_open_leg(step, start); + if (!after_done(step)){ + return; + } continue; } const std::optional start = m_next >= m_holds ? m_at : std::nullopt; @@ -282,6 +292,25 @@ class RouteRun{ } private: + // After `step` ended as planned: false, the route failed, if it lands + // the player and they are still on the water. + bool after_done(const RouteStep& step){ + if (step.lands_after > 0 && m_calls.landed_on && !m_calls.landed_on(step.end)){ + m_calls.fail(std::format( + "walk_overworld_route(): still surfing after {}: the player didn't land on {}.", + to_string(step), to_string(step.end) + )); + return false; + } + const std::optional start = m_next >= m_unchecked ? m_at : std::nullopt; + remember_walked(step); + m_at = step.end; + m_facing = step.facing_after; + m_lines.done(step, ++m_next, m_holds); + check_open_leg(step, start); + return true; + } + static bool is_gate_step(const RouteStep& step){ return step.action == RouteAction::OPEN_GATE || step.action == RouteAction::PRESS_SWITCH; } @@ -474,8 +503,11 @@ class RouteRun{ // only that far, to keep the check cheap: in Viridian Forest (r16) it // walks 10 tiles after "west 6", while after Route 1's "west 4" the // only holds walk 30. An open leg after a short one walks on off the - // route, which isn't checked. A leg onto a spinner isn't checked: its - // taps wait out the slide and its settle, and it ends on the stop. The + // route, which isn't checked, except on the water: there an open leg + // followed by another open leg is checked however far the holds walk, + // and stops the route if none find the player (open_water_chain()). + // A leg onto a spinner isn't checked: its taps wait out the slide and + // its settle, and it ends on the stop. The // tiles short of its arrow left out that stop, and the hold west "to // the wall (7, 11)" after Rocket Hideout B2F's "east 2" to the stop // (14, 13) slid from there to (10, 15) (09.02, console B, 2026-11-11). @@ -488,16 +520,28 @@ class RouteRun{ if (maps() == nullptr || m_planning->open_leg_check != OpenLegCheck::REANCHOR || !start || counts_step_taps(step) || step.action != RouteAction::WALK || step.to_wall || step.warps || step.spin_tiles - || m_next >= m_plan.size() || !(is_wall_hold(m_plan[m_next]) || is_slide(m_plan[m_next])) + || m_next >= m_plan.size() ){ return; } + const bool water_chain = open_water_chain(step, m_plan[m_next]); + if (!(is_wall_hold(m_plan[m_next]) || is_slide(m_plan[m_next]) || water_chain)){ + return; + } const std::vector candidates = short_candidates(*maps(), *start, step); if (candidates.size() < 2 || (is_wall_hold(m_plan[m_next]) && hold_finds(*maps(), candidates, m_plan[m_next]))){ return; } const std::optional anchor = plan_reanchor(*maps(), candidates, step, m_planning->costs); - if (!anchor || anchor->holds.empty() || reanchor_tiles(*anchor) > OPEN_LEG_CHECK_TILES_PER_TILE * step.tiles){ + if (water_chain && (!anchor || anchor->holds.empty())){ + m_calls.fail(std::format( + "walk_overworld_route(): {} on the water may have ended short with nothing seen, and {} after it is open too; " + "no holds to walls find the player: stopped rather than go on from a guess.", + to_string(step), to_string(m_plan[m_next]) + )); + return; + } + if (!anchor || anchor->holds.empty() || (!water_chain && reanchor_tiles(*anchor) > OPEN_LEG_CHECK_TILES_PER_TILE * step.tiles)){ m_calls.log("Route: no holds to walls within " + std::to_string(OPEN_LEG_CHECK_TILES_PER_TILE * step.tiles) + " tiles find the player if " + to_string(step) + " ended short; taken as walked."); return; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h index b215e3f524..e5fedf0b71 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h @@ -88,6 +88,11 @@ struct RouteCalls{ // caller's interrupts fight the trainer), and returns LATE if anything // showed, DONE if not. Not set: not watched. std::function watch_for_trainer; + // Whether the player stands on foot on `tile`, off the water: checked + // after a leg that lands there (RouteStep::lands_after), which fails + // if not. The console reads only whether they surf. Not set: not + // checked. + std::function landed_on; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp index d2c4e322db..b7debcaffc 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp @@ -26,6 +26,7 @@ #include "PokemonFRLG_BikeSpinPresses.h" #include "PokemonFRLG_OverworldBike.h" #include "PokemonFRLG_OverworldMaps.h" +#include "PokemonFRLG_PlayerFacingReader.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -452,6 +453,26 @@ bool moved_past_wall(ConsoleHandle& console, ProControllerContext& context, cons return change > WALL_TAP_MOVED_RMSD; } +// Whether the player's walking sprite shows, read up to LANDED_READS +// times LANDED_REREAD apart: the hop off the water and the steps after +// it may still be on. Surfing, the sprite isn't read (PlayerFacingReader). +constexpr size_t LANDED_READS = 4; +constexpr Milliseconds LANDED_REREAD(150); +bool stands_on_foot(ConsoleHandle& console, ProControllerContext& context, PlayerGender player){ + for (size_t read = 1; read <= LANDED_READS; read++){ + if (read > 1){ + pbf_wait(context, LANDED_REREAD); + } + context.wait_for_all_requests(); + const VideoSnapshot screen = console.video().snapshot(); + if (screen && read_player_facing(screen, player)){ + return true; + } + } + console.log("Route: the player's walking sprite doesn't show: still surfing?", COLOR_ORANGE); + return false; +} + } // namespace @@ -501,6 +522,7 @@ void walk_overworld_route_with_recovery( calls.watch_for_trainer = [&](const RouteStep& step, int approach_tiles){ return watch_for_trainer(console, context, step, approach_tiles, interrupts); }; + calls.landed_on = [&](const MapPosition&){ return stands_on_foot(console, context, player); }; run_route(route, facing, &planning, calls); context.wait_for_all_requests(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp index 3b9ba87f71..88105b321b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp @@ -202,6 +202,59 @@ UnitTestResult test_stop_recover_resume(){ }); } +// Console B, 2026-11-13 04:15 (12.03, surf_route_21): Surf from Pallet +// Town's shore down Route 21 to Cinnabar. "south 86 (open)" on the water +// stopped short at a sand bar on Route21_South, unseen, and "east 7 +// (open)" after it was walked from there: the route was taken as walked +// to (14, 12), with the player still on the water, and the Center entry +// and the heal after it were faked. A surfing open leg followed by another +// open leg is now checked: the player is found again at walls, or the +// route stops, never goes on from a guess. +UnitTestResult test_open_water_legs(){ + const std::vector maps = overworld_maps(); + const MapPosition start{"PalletTown", {10, 17}}; + std::optional> route = plan_overworld_route( + maps, start, RouteGoal{{"CinnabarIsland", {14, 12}}, Direction::NORTH}, default_route_costs() + ); + if (!route){ + return "No route."; + } + // Routes now land on the sand bars and the shore instead of stopping + // on the water (RouteCost::OPEN_WATER_STOP). The leg to the + // first sand bar is made an open stop on the water, as legs were then, + // to keep the check covered. + for (RouteStep& step : *route){ + if (step.surfing && step.action == RouteAction::WALK && !step.warps){ + step.to_wall = false; + step.lands_after = 0; + break; + } + } + size_t chained = 0; + while (chained + 1 < route->size() && !( + (*route)[chained].surfing && !(*route)[chained].to_wall && !(*route)[chained].warps + && (*route)[chained + 1].action == RouteAction::WALK && !(*route)[chained + 1].to_wall && !(*route)[chained + 1].warps + )){ + chained++; + } + if (chained + 1 >= route->size()){ + return "Expected an open leg on the water followed by another open leg:" + describe(*route); + } + const RoutePlanning planning{.maps = &maps, .costs = default_route_costs(), .start = start, .label = "Route", .wall_check = WallCheck::SKIP}; + ScriptedRoute script; + const bool failed = script.run(*route, &planning); + const std::string leg = to_string((*route)[chained]); + if (failed && script.failure.find(leg) != std::string::npos){ + return true; + } + const UnitTestResult checked = expect_logged(script.logs, {"Route: position check: " + leg}); + if (checked.result != UnitTestResult::PASSED){ + return "Expected " + leg + " checked or the route stopped, not taken as walked:" + describe(*route) + + (failed ? "\nfailed: " + script.failure : std::string()); + } + return true; +} + // A corridor with a person at each end: nothing finds the player again. const std::string TEST_CORRIDOR_JSON = R"({ "name": "Corridor", "source": {"decomp_file": "", "revision": "test", "date": "2026-10-31"}, @@ -529,7 +582,11 @@ const OverworldRecoveryTestCase OVERWORLD_RECOVERY_CASES[] = { { .input = {.name = "a stop on the grass in a trainer's sight", .body = test_grass_in_sight}, .expectation = {.description = "handles wild encounter in trainer sight line"}, + }, { + .input = {.name = "open legs on the water", .body = test_open_water_legs}, + .expectation = {.description = "checks a surfing open leg before another open leg"}, }, + }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp index 06e4227675..1a8afee745 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.cpp @@ -38,6 +38,7 @@ const RouteCosts& default_route_costs(){ {RouteCost::SIGHT_TILE, 0}, {RouteCost::SIGHT_OVERSHOOT, 0}, {RouteCost::SIGN_OVERSHOOT, 3}, + {RouteCost::OPEN_WATER_STOP, 1000}, }; return COSTS; } @@ -273,6 +274,22 @@ bool stops_at(const std::vector& maps, const Located& player, Direction return true; } +// Whether `player` at the edge of their map faces a wall on the loaded map +// connected there (Cinnabar Island's north wall below Route 21's beach): +// press() finds the step blocked. +bool wall_over_edge(const std::vector& maps, const Located& player, Direction direction){ + const MapData& map = maps[player.map]; + if (in_bounds(map, player.state.position.next(direction)) || press(maps, player, direction)){ + return false; + } + for (const MapConnection& connection : map.connections){ + if (connection.side == direction){ + return find_map(maps, connection.to_map) >= 0; + } + } + return false; +} + // The search state: a player, the way they face (4 stands for "not known // yet", before the first step), and whether they are in the middle of a leg // that could go on, as opposed to just stopped by a warp or Surf. @@ -295,10 +312,11 @@ class StepModel{ Pokemon::Step step; step.next = transition->next; step.action = transition->action == RouteAction::SURF ? ACTION_SURF : 0; - // Leaving the water: the player walks at half the speed from here. - step.starts_leg = transition->dismounted; + // Leaving the water doesn't end the leg: the player held on lands + // and walks on, so a leg over a sand bar to its far bank ends where + // the water stops them (RouteStep::lands_after). // A slide ends the leg: the hold must let go on the spinner. - step.ends_leg = transition->warped || transition->dismounted || transition->spin; + step.ends_leg = transition->warped || transition->spin; step.warped = transition->warped; step.facing = transition->facing.value_or(direction); if (transition->spin){ @@ -327,6 +345,9 @@ class StepModel{ return stops_at(m_maps, state, direction); } int open_stop_risk(const State& state, Direction direction) const{ + if (state.state.surfing){ + return route_cost(m_costs, RouteCost::OPEN_WATER_STOP); + } if (reads_sign_ahead(m_maps[state.map], state.state.position, direction)){ return route_cost(m_costs, RouteCost::SIGN_OVERSHOOT); } @@ -432,6 +453,26 @@ std::optional last_spin_tiles(const std::vector& maps, const Pokem return transition->spin->tiles; } +// The step of WALK `leg` that lands the player off the water, counted +// from 1, or 0 if none does. +int landing_step(const std::vector& maps, const Pokemon::Leg& leg){ + if (!leg.start.state.surfing){ + return 0; + } + Located player = leg.start; + for (int c = 1; c <= leg.tiles; c++){ + const std::optional transition = press(maps, player, leg.direction); + if (!transition){ + return 0; + } + if (transition->dismounted){ + return c; + } + player = transition->next; + } + return 0; +} + // Whether WALK `step` ends on stairs at the side of a room, or an exit // mat, going another way than its warp is taken, and not at a wall. The // press the warp's way finds the warp only if the leg ended on its tile: @@ -466,28 +507,28 @@ void tap_from_known_tile(std::vector& route){ } } -RouteStep to_route_step(const std::vector& maps, const Pokemon::Leg& leg){ - RouteStep step; +RouteAction route_action(const Pokemon::Leg& leg){ switch (leg.kind){ case Pokemon::LegKind::WALK: - step.action = RouteAction::WALK; - break; + return RouteAction::WALK; case Pokemon::LegKind::ACTION: switch (leg.action){ case Pokemon::ACTION_OPEN_GATE: - step.action = RouteAction::OPEN_GATE; - break; + return RouteAction::OPEN_GATE; case Pokemon::ACTION_PRESS_SWITCH: - step.action = RouteAction::PRESS_SWITCH; - break; + return RouteAction::PRESS_SWITCH; default: - step.action = RouteAction::SURF; + return RouteAction::SURF; } - break; case Pokemon::LegKind::FACE: - step.action = RouteAction::FACE; - break; + return RouteAction::FACE; } + return RouteAction::WALK; +} + +RouteStep to_route_step(const std::vector& maps, const Pokemon::Leg& leg){ + RouteStep step; + step.action = route_action(leg); step.direction = leg.direction; const bool gate = step.action == RouteAction::OPEN_GATE || step.action == RouteAction::PRESS_SWITCH; step.tiles = leg.kind == Pokemon::LegKind::FACE || gate ? 0 : leg.tiles; @@ -505,6 +546,13 @@ RouteStep to_route_step(const std::vector& maps, const Pokemon::Leg 0 && !step.warps && wall_over_edge(maps, leg.end, step.direction)){ + step.to_wall = true; + } step.to_wall = step.to_wall && !step.spin_tiles; step.tapped = !step.surfing && onto_warp_sideways(maps[leg.end.map], step); step.sign_ahead = !step.to_wall && !step.warps && reads_sign_ahead(maps[leg.end.map], leg.end.state.position, step.direction); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteCosts.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteCosts.h index 72e7daad3d..007703bbd6 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteCosts.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteCosts.h @@ -54,12 +54,21 @@ enum class RouteCost{ // (console B, 2026-11-12, 11.03: "north 5" to Safari Zone West's // (27, 10) on the Bicycle). A tie-break more than a detour. SIGN_OVERSHOOT, + // Extra for an open leg that ends on the water: open water has hardly + // a wall, so a leg stopped part-way there can't be placed, and the + // player isn't found again (console B, 2026-11-13 06:55 and 07:02, + // 12.03: "south 15 (open)" down Route 21 stopped by a message). Above + // OPEN_WILD_STOP and a few Surfs: a leg that lands on a sand bar and + // is held to its bank finds the player, so Route 21 is crossed sand + // bar to sand bar (the user's design, 2026-11-13). + OPEN_WATER_STOP, }; // Cost mapping table for path planning. using RouteCosts = std::map; // TILE 1, TURN 2, WARP 30, SURF 40, GRASS_TILE 20, OPEN_STOP 8, -// OPEN_WILD_STOP 300, SIGHT_TILE 0, SIGHT_OVERSHOOT 0, SIGN_OVERSHOOT 3. +// OPEN_WILD_STOP 300, SIGHT_TILE 0, SIGHT_OVERSHOOT 0, SIGN_OVERSHOOT 3, +// OPEN_WATER_STOP 1000. // The sight costs are // off by default until beaten trainers and battles a story step wants are // told apart: at 100 and 10 they moved Pewter City's and Pewter Gym's diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h index 60fc968525..b5c926edb2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteSteps.h @@ -57,6 +57,17 @@ struct RouteStep{ bool warps = false; // WALK: on the water, where the player moves twice as fast. bool surfing = false; + // WALK, surfing: the leg lands the player on its tile this many steps + // in (a sand bar, an island, the shore), and the steps after it are + // walked on foot, at walking pace, the same direction held. 0 if it + // doesn't land. The landing tile is the first land tile on the line, + // so a leg that lands finds the player again as a wall does: held on + // to the bank (`to_wall`), where the water stops a player on foot, it + // ends on its tile however long it is held. The walker checks that the + // player is on foot after it (console B, 2026-11-13, 12.03: open legs + // on Route 21's water stopped part-way and the player couldn't be + // found again). + int lands_after = 0; // WALK: on a map where the game doesn't let the player run (indoors, // MapData::allow_running): B held there still walks. bool walk_only = false; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteStrings.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteStrings.cpp index b26cd60248..a57c1d75db 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteStrings.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRouteStrings.cpp @@ -24,6 +24,9 @@ std::string to_string(const RouteStep& step){ if (step.spin_tiles){ text += std::format(" (spins {})", *step.spin_tiles); } + if (step.lands_after > 0){ + text += std::format(" (lands after {})", step.lands_after); + } break; case RouteAction::SURF: text = "surf " + std::string(direction_name(step.direction)); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index 2881dff960..ae9b0e23e1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -387,6 +387,9 @@ bool OverworldSim::after_step(const Step& step, Milliseconds pressed){ note("warped to " + to_string(m_state.position) + "."); return true; } + if (m_player.surfing && message_on_water(pressed)){ + return true; + } // The game looks for trainers who see the player before it rolls for a // wild battle, and doesn't roll once one does (pokefirered // ProcessPlayerFieldInput(): CheckForTrainersWantingBattle() first, @@ -492,39 +495,6 @@ void OverworldSim::walk_hold(Direction direction, Milliseconds duration, int sho take_steps(direction, pressed, end, (int)((end - pressed + tile - 1ms) / tile) - short_by); } -// Up to `left` steps of a hold of `direction` until `end`, the first -// pressed at `pressed`. -void OverworldSim::take_steps(Direction direction, Milliseconds pressed, Milliseconds end, int left){ - const Milliseconds tile = tile_time(); - for (; left > 0 && pressed < end; left--){ - const std::optional step = press(direction); - if (!step){ - bump(direction, pressed); - return; - } - if (m_script.connections_stall && step->map != m_map && !step->warped){ - // The step onto a connected map waits a tile while the game - // loads it: a hold timed by its tiles alone ended a tile short - // (console B, 2026-11-03, rows 04.06, 05.05 and 05.09). - pressed += tile; - if (pressed >= end){ - return; - } - } - if (step->facing){ - m_state.facing = *step->facing; - } - pressed += after_slide(*step); - if (after_step(*step, pressed)){ - return; - } - if (step->spin && m_script.holds_end_at_slide_stop){ - return; - } - pressed += step->ledge ? tile * 2 : tile; - } -} - // A hold the game doesn't move the player for. void OverworldSim::stuck_hold(Direction direction){ if (m_state.screen == SimScreen::START_MENU){ @@ -542,6 +512,9 @@ std::optional OverworldSim::hold( ){ const Milliseconds start = m_now; m_holds++; + if (m_player.surfing){ + m_state.surf_holds.push_back(m_holds); + } m_last_tap.reset(); count_bike_leg(direction, duration); if (m_state.screen != SimScreen::OVERWORLD || m_frozen){ @@ -992,6 +965,8 @@ RouteCalls OverworldSimWalker::calls(){ calls.fail = [this](const std::string& message){ fail(message); }; calls.moved_past_wall = [this](const RouteStep& step){ return moved_past_wall(step); }; calls.watch_for_trainer = [this](const RouteStep& step, int approach_tiles){ return watch_for_trainer(step, approach_tiles); }; + // As the console: the walking sprite shows, not where. + calls.landed_on = [this](const MapPosition&){ return !m_sim.state().surfing; }; return calls; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h index c79eceb693..a5e92ae140 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h @@ -239,6 +239,9 @@ class OverworldSim{ // (SimScript::black_without_warp): its black without the warp. int m_warp_steps = 0; bool black_without_warp(Milliseconds pressed); + // A message after the step on the water just taken + // (SimScript::messages_at_surf_steps). + bool message_on_water(Milliseconds pressed); // The last tap, until a hold. std::optional m_last_tap; // When the last Bicycle tap started, and how many came too soon. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h index a848c4988e..983a69a91f 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimState.h @@ -96,6 +96,12 @@ struct SimScript{ // SELECT took the player off, and every bike tap after only turned // them (console B, 2026-11-12 05:42). std::set off_bike_at_legs; + // Fault injection: steps on the water, by number from 1 among all the + // steps surfed, after which a message shows and stops the player, as + // "a message" did on 12.03's "south 15 (open)" down Route 21 (console + // B, 2026-11-13 06:55 and 07:02, 8 tiles in both times; no box was on + // the leg's screenshot after it was closed). + std::set messages_at_surf_steps; // The player rides the Bicycle: a tap facing another way takes a step // too, a hold through a turn loses no tile // (MovementMechanics::BIKE_TAP_ONLY_TURNS), and no step tap is @@ -154,6 +160,11 @@ struct SimState{ // From each wild battle's step to the overworld after it, together. Milliseconds in_wild_battles{0}; int signs_read = 0; + // Steps taken on the water (SimScript::messages_at_surf_steps). + int surf_steps = 0; + // The holds, by number from 1 as SimScript::short_holds counts them, + // that started on the water. + std::vector surf_holds; int turn_taps_dropped = 0; // Taps that only turned the player, or were dropped turning them. int turn_taps = 0; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimTaps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimTaps.cpp index 16ecfd42be..e87cfd02c3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimTaps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSimTaps.cpp @@ -2,9 +2,9 @@ * * From: https://github.com/PokemonAutomation/ * - * The walker's taps on the sim: turn taps, step taps, and on the Bicycle - * taps dropped when they come too fast, SELECT, and legs counted out a - * tap at a time. + * The walker's taps on the sim: turn taps, step taps, the steps of a + * hold, and on the Bicycle taps dropped when they come too fast, SELECT, + * and legs counted out a tap at a time. * */ @@ -36,6 +36,54 @@ BikeSelectCalls sim_select_calls(OverworldSim& sim){ } // namespace +// Up to `left` steps of a hold of `direction` until `end`, the first +// pressed at `pressed`. +void OverworldSim::take_steps(Direction direction, Milliseconds pressed, Milliseconds end, int left){ + for (; left > 0 && pressed < end; left--){ + // Each step at the pace it starts at: on the water until a step + // lands the player, which takes a tile on foot (the hop). + Milliseconds tile = tile_time(); + const std::optional step = press(direction); + if (!step){ + bump(direction, pressed); + return; + } + if (m_script.connections_stall && step->map != m_map && !step->warped){ + // The step onto a connected map waits a tile while the game + // loads it: a hold timed by its tiles alone ended a tile short + // (console B, 2026-11-03, rows 04.06, 05.05 and 05.09). + pressed += tile; + if (pressed >= end){ + return; + } + } + if (step->facing){ + m_state.facing = *step->facing; + } + pressed += after_slide(*step); + if (after_step(*step, pressed)){ + return; + } + if (step->spin && m_script.holds_end_at_slide_stop){ + return; + } + tile = tile_time(); + pressed += step->ledge ? tile * 2 : tile; + } +} + +// Whether a message shows after this step on the water, as one stopped a +// hold on Route 21 (SimScript::messages_at_surf_steps). +bool OverworldSim::message_on_water(Milliseconds pressed){ + if (!m_script.messages_at_surf_steps.contains(++m_state.surf_steps)){ + return false; + } + m_frozen = true; + schedule(pressed + m_script.timing.message, SimScreen::MESSAGE, false); + note(std::format("a message on the water, surfed step {}.", m_state.surf_steps)); + return true; +} + bool OverworldSim::drops_bike_tap(Milliseconds start){ const bool fast = m_last_bike_tap && start - *m_last_bike_tap < MovementMechanics::BIKE_TAP_DROP_PERIOD; m_last_bike_tap = start; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp index ebf46afd7d..07fa20076c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.cpp @@ -202,6 +202,14 @@ MoveSpeed step_speed(const RouteStep& step, MoveSpeed speed){ } Milliseconds tile_time(const RouteStep& step, MoveSpeed speed){ + if (step.surfing && step.lands_after > 0 && step.tiles > 0){ + // The surfed tiles, then the hop onto land and the walked ones + // (B isn't held on a leg that starts on the water), each hop or + // walked tile taken at walking pace, averaged and rounded up. + const int walked = step.tiles - step.lands_after + 1; + const Milliseconds total = SURF_TILE_TIME * (step.lands_after - 1) + TILE_TIME * walked; + return (total + Milliseconds(step.tiles - 1)) / step.tiles; + } if (step.surfing){ return SURF_TILE_TIME; } @@ -234,56 +242,6 @@ bool gets_on_bike(const RouteStep& step, MoveSpeed speed, bool riding){ && !step.walk_only && !step.surfing; } -bool select_onto_bike(const BikeSelectCalls& calls){ - for (int press = 1; press <= 2; press++){ - calls.press_select(); - if (calls.riding()){ - return true; - } - if (calls.wait_to_read_again){ - calls.wait_to_read_again(); - } - if (calls.riding()){ - return true; - } - } - return false; -} - -bool screen_shows_riding(const ImageViewRGB32& screen, PlayerGender player){ - if (StartMenuDetector(COLOR_RED).detect(screen) || SelectionDialogDetector(COLOR_RED).detect(screen) - || WhiteDialogDetector(COLOR_RED).detect(screen) - ){ - return false; - } - return !read_player_facing(screen, player).has_value(); -} - -void get_on_bike(ConsoleHandle& console, ProControllerContext& context, PlayerGender player){ - BikeSelectCalls calls; - calls.press_select = [&]{ - pbf_press_button(context, BUTTON_MINUS, BIKE_SELECT_PRESS, BIKE_SELECT_SETTLE); - }; - calls.riding = [&]{ - context.wait_for_all_requests(); - VideoSnapshot screen = console.video().snapshot(); - const bool riding = screen && screen_shows_riding(*screen.frame, player); - console.log(riding ? "Bike: riding." : "Bike: not riding after SELECT.", riding ? COLOR_BLACK : COLOR_ORANGE); - return riding; - }; - calls.wait_to_read_again = [&]{ - pbf_wait(context, BIKE_SELECT_SETTLE); - }; - console.log("Bike: SELECT (MINUS) onto the Bicycle."); - if (!select_onto_bike(calls)){ - OperationFailedExceptionWithScreenshot::fire( - ErrorReportMode::SEND_ERROR_REPORT, - "get_on_bike(): not on the Bicycle after SELECT twice. Is it registered?", - console - ); - } -} - Pokemon::LegMover leg_mover(const RouteStep& step, MoveSpeed speed){ if (step_speed(step, speed) == MoveSpeed::RUN && !step.surfing){ return hold_dpad_running; @@ -372,6 +330,21 @@ bool warp_left_scene( return true; } +// How bright the black below a building's map may be, as the sum of the +// average's channels (as a dark cave's corners). +constexpr double BLACK_BELOW_MAX = 45.0; + +// Whether the bottom tile row of the game box `box` is black: the black +// below a building's map, which a door north into it ends on. +bool black_below(const ImageViewRGB32& box){ + const size_t row = box.height() / SCREEN_TILES_Y; + if (row == 0){ + return false; + } + const ImageViewRGB32 bottom = box.sub_image(0, box.height() - row, box.width(), row); + return image_stats(bottom).average.sum() < BLACK_BELOW_MAX; +} + } // namespace bool arrived_without_fade( @@ -389,6 +362,11 @@ bool arrived_without_fade( if (scrolled_rmsd(now_box, later_box, 0, 0) >= SAME_SCENE_RMSD){ return false; } + // Water's waves make any scene after it look like another one: a door + // north has to show the building it leads into. + if (step.direction == Direction::NORTH && !black_below(later_box)){ + return false; + } return warp_left_scene(before, later, game, step.direction, step.tiles + step.warp_slack + 2); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h index 93b700d365..cc614a97f3 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWalk.h @@ -89,7 +89,9 @@ void wait_out_warp(ConsoleHandle& console, ProControllerContext& context, const // Store's lift room is mostly black around its lit floor: a watch for the // fade started once it was over never sees black end (console B, // 2026-11-12 00:39, 10.01 lift_to_1f: two steps more into the lift's back -// wall, then "no fade in and out"). +// wall, then "no fade in and out"). A step north must also show the black +// below the building it enters: on Route 21 the waves after a step "into +// the Center" were taken as it (console B, 2026-11-13, 12.03). bool arrived_without_fade( const ImageViewRGB32& before, const ImageViewRGB32& now, const ImageViewRGB32& later, const ImageFloatBox& game, const RouteStep& step diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWarpFade_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWarpFade_Tests.cpp index a97a0a81bb..aba711b34b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWarpFade_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWarpFade_Tests.cpp @@ -182,6 +182,37 @@ UnitTestResult test_lift_room_arrival(){ return true; } +// Water whose waves are drawn from `phase`: every phase another scene. +ImageRGB32 water(size_t phase){ + ImageRGB32 image(240, 160); + for (size_t y = 0; y < 160; y++){ + for (size_t x = 0; x < 240; x++){ + image.pixel(x, y) = ((x + 3 * y + 7 * phase) % 13 < 6) ? 0xff3060e0 : 0xff80b0f8; + } + } + return image; +} + +// 12.03 cinnabar_nurse: surfing on Route 21, short of Cinnabar, the step +// north "into the Center" saw no fade, and the water after it, still for +// a poll but not the water from before, was taken as the Center (console +// B, 2026-11-13 04:16). A door north into a building ends at the bottom of +// its map, with the black below it on screen; water has none. +UnitTestResult test_water_is_not_the_center(){ + const ImageFloatBox game(0, 0, 1, 1); + RouteStep step; + step.direction = Direction::NORTH; + step.tiles = 1; + step.warps = true; + step.end.map = "the Center"; + const ImageRGB32 before = water(0); + const ImageRGB32 after = water(1); + if (OverworldWalkSteps::arrived_without_fade(before, after, after, game, step)){ + return "Expected still water with no black below not to be taken as the Center."; + } + return true; +} + struct ArrivalScreenInput{ std::string name; std::function body; @@ -211,6 +242,7 @@ class Test_ArrivalScreen : public UnitTest{ const ArrivalScreenCase ARRIVAL_SCREEN_CASES[] = { {.input = {"the lift room after a missed fade, by its screen", test_lift_room_arrival}, .expectation = {}}, + {.input = {"water after a missed fade is not the Center", test_water_is_not_the_center}, .expectation = {}}, }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp index 46a97b8321..2f04f94b25 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp @@ -326,6 +326,14 @@ void add_tests_PlayerFacingReader(UnitTestDatabase& database){ database.add("PokemonFRLG/PlayerFacingReader/CeruleanCave_North.png"); database.add("PokemonFRLG/PlayerFacingReader/Surfing_None.png"); database.add("PokemonFRLG/PlayerFacingReader/Bag_None.png"); + // FireRed on console B, 2026-11-13 (12.03): on foot on Route 21's sand + // bars after a leg that landed, and surfing down Route21_North, where + // a leg that lands is checked to have (RouteCalls::landed_on). + database.add("PokemonFRLG/PlayerFacingReader/Route21SandBar_South.png"); + database.add("PokemonFRLG/PlayerFacingReader/Route21SandBar_East.png"); + database.add("PokemonFRLG/PlayerFacingReader/Route21SandBar_North.png"); + database.add("PokemonFRLG/PlayerFacingReader/Route21Surfing_None.png"); + database.add("PokemonFRLG/PlayerFacingReader/Route21ChannelSurfing_None.png"); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.cpp new file mode 100644 index 0000000000..6c7f921d4f --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.cpp @@ -0,0 +1,160 @@ +/* Pokemon Center Floor Reader + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/GlobalAutoPaths.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Settings.h" +#include "PokemonFRLG_PokemonCenterFloorReader.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +// Measured on console B's 1080p captures (2026-11-12/13): the Fuchsia +// Center's floor reads 245..246, 241..242, 164..167. The range leaves out +// Route 21's sand (248, 233, 120) and Pallet Town's paths. +bool is_floor_colour(uint32_t pixel){ + const int red = (pixel >> 16) & 0xff; + const int green = (pixel >> 8) & 0xff; + const int blue = pixel & 0xff; + const int red_green = red > green ? red - green : green - red; + return red >= 225 && green >= 225 && blue >= 140 && blue <= 200 && red_green <= 20; +} + +// The floor covered 15 to 21% of the screen in the Center's far corner, +// where most of the view is the black outside the room; it covers more +// below the nurse. Water, sand and town frames read under 2%. +constexpr double MIN_FLOOR_SHARE = 0.08; + +} // namespace + + +double pokemon_center_floor_share(const ImageViewRGB32& game){ + const size_t width = game.width(); + const size_t height = game.height(); + if (width == 0 || height == 0){ + return 0; + } + size_t floor = 0; + for (size_t row = 0; row < height; row++){ + for (size_t col = 0; col < width; col++){ + if (is_floor_colour(game.pixel(col, row))){ + floor++; + } + } + } + return (double)floor / (double)(width * height); +} + +bool shows_pokemon_center_floor(const ImageViewRGB32& screen){ + if (screen.width() == 0 || screen.height() == 0){ + return false; + } + const ImageViewRGB32 game = extract_box_reference(screen, GameSettings::instance().GAME_BOX); + return pokemon_center_floor_share(game) >= MIN_FLOOR_SHARE; +} + + +namespace{ + +// A console B frame: _.png, True where the player is +// in a Center. +class Test_PokemonCenterFloorReader : public UnitTest{ +public: + Test_PokemonCenterFloorReader(std::string image, bool expected) + : UnitTest("PokemonFRLG::PokemonCenterFloorReader - " + image) + , m_image(UNIT_TEST_RESOURCE_PATH() + image) + , m_expected(expected) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + bool read = false; + try{ + read = shows_pokemon_center_floor(ImageRGB32(m_image)); + }catch (FileException& e){ + // The frames aren't in CI's CommandLineTests yet. + return UnitTestResult(UnitTestResult::SKIPPED, "File not found: " + e.message()); + } + if (read == m_expected){ + return true; + } + return std::format("Expected {}, read {}.", m_expected ? "a Center" : "no Center", read ? "a Center" : "none"); + } + +private: + std::string m_image; + bool m_expected; +}; + +// Made-up screens, so the rule is tested without the frames: a third of +// the floor colour reads as a Center; sand, water and an empty image don't. +class Test_PokemonCenterFloorFixture : public UnitTest{ +public: + Test_PokemonCenterFloorFixture() + : UnitTest("PokemonFRLG::PokemonCenterFloorReader - fixture") + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + struct Case{ + std::string_view name; + uint32_t colour; + bool expected; + }; + const Case cases[] = { + {.name = "floor", .colour = 0xfff6f2a7, .expected = true}, + {.name = "sand", .colour = 0xfff8e978, .expected = false}, + {.name = "water", .colour = 0xff5890e8, .expected = false}, + }; + for (const Case& c : cases){ + ImageRGB32 game(240, 160); + game.fill(0xff000000); + for (size_t row = 100; row < 160; row++){ + for (size_t col = 0; col < 240; col++){ + game.pixel(col, row) = c.colour; + } + } + if ((pokemon_center_floor_share(game) >= MIN_FLOOR_SHARE) != c.expected){ + return std::format("{}: expected {}.", c.name, c.expected ? "a Center" : "no Center"); + } + } + if (shows_pokemon_center_floor(ImageRGB32()) || pokemon_center_floor_share(ImageRGB32()) != 0){ + return "An empty image read as a Center."; + } + return true; + } +}; + +} // namespace + + +void add_tests_PokemonCenterFloorReader(UnitTestDatabase& database){ + database.add(); + // LeafGreen on the Switch, console B's 1080p card. + const std::string folder = "PokemonFRLG/PokemonCenterFloorReader/"; + database.add(folder + "FuchsiaCenterWestWall_True.png", true); + database.add(folder + "FuchsiaCenterNorthwestCorner_True.png", true); + // 12.03, 2026-11-13 04:15-04:16: the Center entry and heal were faked + // on Route 21's water. + database.add(folder + "Route21WaterSaveCard_False.png", false); + database.add(folder + "Route21SandBar_False.png", false); + database.add(folder + "Route21Water_False.png", false); + database.add(folder + "PalletTownShore_False.png", false); + database.add(folder + "FuchsiaCityCenterDoor_False.png", false); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.h new file mode 100644 index 0000000000..5501fb5293 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.h @@ -0,0 +1,42 @@ +/* Pokemon Center Floor Reader + * + * From: https://github.com/PokemonAutomation/ + * + * Whether the overworld shows a Pokemon Center's ground floor, by its cream + * floor tiles. Every Center's ground floor but the Indigo Plateau's and One + * Island's has the one layout (PokemonFRLG_PokemonCenterRoom.h), and the + * floor fills much of the screen wherever the player stands in it. Water, + * sand bars, streets and grass have none of it: Route 21's sand is a + * darker yellow (about 248, 233, 120 against the floor's 246, 242, 167). + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_PokemonCenterFloorReader_H +#define PokemonAutomation_PokemonFRLG_PokemonCenterFloorReader_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "CommonFramework/ImageTypes/ImageViewRGB32.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The share of `game`'s pixels (the GBA screen at any scale, already cut +// out of the console's) that are a Center's floor colour. 0 for an empty +// image. +double pokemon_center_floor_share(const ImageViewRGB32& game); + +// Whether `screen`, the console's frame, shows a Center's ground floor. +// False for an empty frame. +bool shows_pokemon_center_floor(const ImageViewRGB32& screen); + + +// Registers the reader's unit tests. +void add_tests_PokemonCenterFloorReader(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp index b96f9aeff5..872b2b7501 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/PokemonFRLG_Tests.cpp @@ -44,6 +44,7 @@ #include "Overworld/PokemonFRLG_OverworldSim.h" #include "Overworld/PokemonFRLG_OverworldWalk.h" #include "Overworld/PokemonFRLG_PlayerFacingReader.h" +#include "Overworld/PokemonFRLG_PokemonCenterFloorReader.h" #include "Programs/PokemonFRLG_NamingScreenKeyboard_Tests.h" #include "PokemonFRLG_Navigation.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryAbilities.h" @@ -53,6 +54,7 @@ #include "Programs/AutoStory/PokemonFRLG_AutoStoryPcStorage.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryRngLoop.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h" +#include "Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryHealing.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryRows12_Tests.h" #include "Programs/AutoStory/PokemonFRLG_AutoStoryRows14_Tests.h" @@ -341,6 +343,7 @@ static void add_tests_AutoStoryRows(UnitTestDatabase& database){ add_tests_AutoStoryFuchsiaGym(database); add_tests_AutoStoryHealing(database); add_tests_AutoStoryCeladonGym(database); + add_tests_AutoStorySurf(database); add_tests_AutoStorySaffron(database); add_tests_AutoStoryCinnabarGym(database); add_tests_AutoStorySilph(database); @@ -350,7 +353,8 @@ static void add_tests_AutoStoryRows(UnitTestDatabase& database){ add_tests_AutoStoryBoulders(database); } -void add_tests(UnitTestDatabase& database){ +// The detectors and readers of battle, box, party and dialog screens. +static void add_tests_Screens(UnitTestDatabase& database){ add_tests_AdvanceWhiteDialogDetector(database); add_tests_AdvanceBattleDialogDetector(database); add_tests_BagDetector(database); @@ -379,13 +383,22 @@ void add_tests(UnitTestDatabase& database){ add_tests_PartySlotDetector(database); add_tests_BattleTextBoxDetector(database); add_tests_AutoStoryReaders(database); + add_tests_PlayerFacingReader(database); + add_tests_PokemonCenterFloorReader(database); +} + +// The overworld's maps, routes, walker and sim. +static void add_tests_Overworld(UnitTestDatabase& database){ add_tests_OverworldMaps(database); add_tests_OverworldRoute(database); add_tests_OverworldWalk(database); add_tests_OverworldReanchor(database); add_tests_OverworldRecovery(database); add_tests_OverworldSim(database); - add_tests_PlayerFacingReader(database); +} + +// The story's tables, plans and steps. +static void add_tests_AutoStoryAll(UnitTestDatabase& database){ add_tests_AutoStory(database); add_tests_AutoStoryJson(database); add_tests_AutoStorySimulation(database); @@ -397,6 +410,12 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryCapture(database); add_tests_AutoStoryPcStorage(database); add_tests_AutoStoryAbilities(database); +} + +void add_tests(UnitTestDatabase& database){ + add_tests_Screens(database); + add_tests_Overworld(database); + add_tests_AutoStoryAll(database); add_tests_NamingScreen(database); add_tests_RngPlanning(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp index 8787da3930..5ed6cf5d42 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryActions.cpp @@ -16,8 +16,6 @@ #include "CommonTools/Async/InferenceRoutines.h" #include "CommonTools/Images/SolidColorTest.h" #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" -#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" -#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_BattleDialogs.h" #include "VideoGames/PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h" #include "VideoGames/PokemonFRLG/Inference/Menus/PokemonFRLG_ItemListReader.h" @@ -759,7 +757,10 @@ void run_save_dialog(ConsoleHandle& console, ProControllerContext& context){ } // namespace -void save_game(ConsoleHandle& console, ProControllerContext& context, StartMenu menu){ +void save_game( + ConsoleHandle& console, ProControllerContext& context, StartMenu menu, + Language language, const SavePlaceCheck& check +){ console.log("Saving the game."); open_start_menu_row(console, context, menu, start_menu_save_row(menu)); // The save card and "Would you like to save the game?" @@ -769,7 +770,20 @@ void save_game(ConsoleHandle& console, ProControllerContext& context, StartMenu ); } context.wait_for_all_requests(); - dump_debug_image(console, "AutoStory/save", "save-card", console.video().snapshot()); + VideoSnapshot card = console.video().snapshot(); + dump_debug_image(console, "AutoStory/save", "save-card", card); + const std::string place = card ? read_save_card_location(console, language, card) : ""; + const std::string error = check(place); + if (!error.empty()){ + console.log("Not saving: " + error, COLOR_RED); + // NO, then out of the menu. + press(context, BUTTON_B, 1000ms); + mash_until_overworld(console, context, 20000ms); + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, "save_game(): not saved: " + error, console + ); + } + console.log("Save card: " + (place.empty() ? std::string("(unread)") : place) + "."); run_save_dialog(console, context); } @@ -969,33 +983,6 @@ void play_new_game_to_player_name(ConsoleHandle& console, ProControllerContext& NewGameIntro(console, context, player_name, gender, NewGameIntro::End::PLAYER_NAME_TYPED).run(); } -namespace{ - -// After the RNG program `program`: from HOME, if it went there, back to -// the game, then B until the overworld. -void back_to_overworld_after(ConsoleHandle& console, ProControllerContext& context, const std::string& program){ - context.wait_for_all_requests(); - VideoSnapshot screen = console.video().snapshot(); - if (screen && HomeMenuDetector(console, COLOR_RED).detect(screen)){ - console.log("After " + program + ": on HOME, back to the game."); - resume_game_from_home(console, context); - } - mash_until_overworld(console, context, 20000ms); -} - -} // namespace - -void close_trainer_card(ConsoleHandle& console, ProControllerContext& context){ - // B closes the card, then the START menu. - back_to_overworld_after(console, context, "SID Helper"); -} - -void leave_gift_summary(ConsoleHandle& console, ProControllerContext& context){ - // B closes the summary, the party, then the START menu. - back_to_overworld_after(console, context, "Gift RNG"); -} - - } // namespace PokemonFRLG } // namespace NintendoSwitch diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp index 0f3c486bb0..950957ab7c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp @@ -138,7 +138,10 @@ UnitTestResult test_fuchsia_to_cinnabar(){ if (!(sim.state().position == below_center)){ return std::format("12.03: ended on {}, not below Cinnabar's Center door {}.", to_string(sim.state().position), to_string(below_center)); } - if (!sim.state().trainers_beaten.empty() || !sim.state().violations.empty()){ + // Swimmer Roland, who wanders Route21_South's (8..12, 27..31) and sees 4 + // tiles each way, is in the way of every crossing from sand bar to sand + // bar (PokemonFRLG_AutoStorySurf_Tests.cpp); no one else is met. + if (sim.state().trainers_beaten.size() > 1 || !sim.state().violations.empty()){ return "12.03: met a trainer or broke a rule on the way."; } return true; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCenterHeal.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCenterHeal.h new file mode 100644 index 0000000000..5d7eb39e1d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCenterHeal.h @@ -0,0 +1,31 @@ +/* AutoStory Center Heal + * + * From: https://github.com/PokemonAutomation/ + * + * The story's heal at a Pokemon Center's nurse, counted into the run's + * supplies. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStoryCenterHeal_H +#define PokemonAutomation_PokemonFRLG_AutoStoryCenterHeal_H + +#include "PokemonFRLG_AutoStoryEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The nurse's heal (heal_at_pokecenter()), defined in +// PokemonFRLG_AutoStoryHealingSteps.cpp, with the party's HP and PP +// counted full in env.supplies; only where a Pokemon Center's floor shows +// on screen, else it fails with the screen. +void heal_in_center(StoryEnv& env); + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 0100ed32bf..684d33a2c8 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -24,6 +24,7 @@ #include "PokemonFRLG_AutoStoryAbilities.h" #include "PokemonFRLG_AutoStoryBattleData.h" #include "PokemonFRLG_AutoStoryCapture.h" +#include "PokemonFRLG_AutoStoryCenterHeal.h" #include "PokemonFRLG_AutoStoryEvents.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" @@ -303,8 +304,7 @@ Outcome get_ready(StoryEnv& env, RunState& state, const StoryStep& step){ if (!walked.succeeded()){ return walked; } - heal_at_pokecenter(env.console, env.context); - healed_by_nurse(supplies); + heal_in_center(env); return walked; } @@ -363,6 +363,7 @@ std::string save_card_place(const std::string& map){ {"CeruleanCity", "cerulean-city"}, {"Route24", "route-24"}, {"Route25", "route-25"}, + {"Route21", "route-21"}, {"Route22", "route-22"}, {"Route23", "route-23"}, {"VermilionCity", "vermilion-city"}, @@ -536,9 +537,8 @@ bool grind_heal_trip(StoryEnv& env, RunState& state, const StoryStep& step, cons if (!walked(env, state, walk)){ return false; } - heal_at_pokecenter(env.console, env.context); // PP too: a grind's battles spend more than its attacks have. - healed_by_nurse(env.supplies); + heal_in_center(env); walk.name = step.name + "_back_to_patch"; walk.args["target"] = Pokemon::GridPose{back->map, {back->tile}, back->facing}; return walked(env, state, walk); @@ -792,8 +792,7 @@ ConsoleAction console_action_of(StoryActionId id){ return [](StoryEnv& env, RunState&, const StoryStep& step){ return battle(env, step); }; case StoryActionId::HEAL: return [](StoryEnv& env, RunState&, const StoryStep&){ - heal_at_pokecenter(env.console, env.context); - healed_by_nurse(env.supplies); + heal_in_center(env); return Pokemon::outcome_ok(); }; case StoryActionId::SHOP: @@ -999,7 +998,9 @@ Pokemon::Checkpoint build_checkpoint(const StoryCheckpoint& data, StoryExecutor& const StartMenu menu = data.menu; checkpoint.save = [&env, menu, id = data.id](RunState&){ return guarded([&]{ - save_game(env.console, env.context, menu); + save_game(env.console, env.context, menu, env.language, [end_map = checkpoint_end_map(id)](const std::string& place){ + return save_end_error(end_map, place); + }); keep_next_start_frame(env, id); env.has_save = true; env.saved_supplies = env.supplies; @@ -1024,7 +1025,6 @@ Pokemon::Segment make_story_segment(const std::vector& checkpoi } - } // namespace PokemonFRLG } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealingSteps.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealingSteps.cpp index 1aef3cf777..63dc8f0691 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealingSteps.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryHealingSteps.cpp @@ -7,7 +7,11 @@ #include #include #include "Common/Cpp/Color.h" +#include "CommonFramework/Exceptions/OperationFailedException.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.h" +#include "VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h" #include "PokemonFRLG_AutoStoryBattlePolicy.h" +#include "PokemonFRLG_AutoStoryCenterHeal.h" #include "PokemonFRLG_AutoStoryGameArgs.h" #include "PokemonFRLG_AutoStoryHealing.h" #include "PokemonFRLG_AutoStoryHealingSteps.h" @@ -68,6 +72,25 @@ Outcome bag_check(StoryEnv& env, Pokemon::RunState& state, const StoryStep& step } // namespace +// The nurse's heal, counted only where a Center's floor shows: on 12.03 +// (console B, 2026-11-13 04:16) a route taken as walked left the player +// on Route 21's water, the Center entry after it was taken from a still +// screen, and the heal's A mashing was counted as the nurse's. No frame, +// or no floor: fails with the screen. +void heal_in_center(StoryEnv& env){ + env.context.wait_for_all_requests(); + const VideoSnapshot frame = env.console.video().snapshot(); + if (!frame || !shows_pokemon_center_floor(frame)){ + OperationFailedExceptionWithScreenshot::fire( + ErrorReportMode::SEND_ERROR_REPORT, + "Heal: no Pokemon Center floor on screen; the player is not in a Center.", + env.console + ); + } + heal_at_pokecenter(env.console, env.context); + healed_by_nurse(env.supplies); +} + std::vector healing_action_specs(){ const Pokemon::StoryArgSpec max_hp = required_arg("max_hp", Pokemon::StoryArgType::INTEGER, "the lead's maximum HP when its summary wasn't read"); diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h index d708eb4d51..c5cf72a90e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden.h @@ -512,12 +512,12 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN[] = { "06.01 walk south 5 (warp) -> VermilionCity (15, 7) [walk only]", "06.01 walk east 12 -> VermilionCity (27, 7)", "06.01 walk south 7 (to wall) -> VermilionCity (27, 14)", - "06.01 walk west 4 -> VermilionCity (23, 14)", - "06.01 walk south 8 (to wall) -> VermilionCity (23, 22)", - "06.01 surf south -> VermilionCity (23, 23) [wild]", - "06.01 walk south 4 -> VermilionCity (23, 27) [surfing] [wild]", - "06.01 walk south 1 -> VermilionCity (23, 28) [surfing] [wild]", - "06.01 walk south 4 -> VermilionCity (23, 32)", + "06.01 walk west 2 -> VermilionCity (25, 14)", + "06.01 walk south 11 (to wall) -> VermilionCity (25, 25)", + "06.01 surf south -> VermilionCity (25, 26) [wild]", + "06.01 walk south 4 (to wall) (lands after 2) -> VermilionCity (25, 30) [surfing] [wild]", + "06.01 walk west 2 -> VermilionCity (23, 30)", + "06.01 walk south 2 -> VermilionCity (23, 32)", "06.01 aboard from VermilionCity (23, 33) facing east to SSAnne_1F_Corridor (19, 2) facing south", "06.01 walk south 2 (warp) -> SSAnne_Exterior (32, 5)", "06.01 walk south 10 (warp) -> SSAnne_1F_Corridor (19, 2)", @@ -549,11 +549,11 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN[] = { "06.01 walk north 7 -> SSAnne_1F_Corridor (19, 2)", "06.01 vermilion_nurse from VermilionCity (23, 32) facing north to VermilionCity_PokemonCenter_1F (7, 4) facing north", "06.01 walk north 4 (to wall) -> VermilionCity (23, 28)", - "06.01 surf north -> VermilionCity (23, 27) [wild]", - "06.01 walk north 4 -> VermilionCity (23, 23) [surfing] [wild]", - "06.01 walk north 1 -> VermilionCity (23, 22) [surfing] [wild]", - "06.01 walk north 10 -> VermilionCity (23, 12)", - "06.01 walk east 4 -> VermilionCity (27, 12)", + "06.01 walk east 14 (to wall) -> VermilionCity (37, 28)", + "06.01 walk north 9 -> VermilionCity (37, 19)", + "06.01 walk west 11 -> VermilionCity (26, 19)", + "06.01 walk north 7 -> VermilionCity (26, 12)", + "06.01 walk east 1 -> VermilionCity (27, 12)", "06.01 walk north 5 (to wall) -> VermilionCity (27, 7)", "06.01 walk west 12 -> VermilionCity (15, 7)", "06.01 walk north 1 (warp) -> VermilionCity_PokemonCenter_1F (7, 8)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h index 626b3c2668..e8b63e0352 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden12.h @@ -97,16 +97,19 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_12[] = { "12.03 walk south 6 (to wall) -> PalletTown (10, 16)", "12.03 walk west 2 -> PalletTown (8, 16)", "12.03 face south -> PalletTown (8, 16)", - "12.03 surf_route_21 from PalletTown (8, 17) facing south to CinnabarIsland (14, 12) facing north", - "12.03 walk south 29 -> Route21_North (8, 26) [surfing] [wild] [connection 3]", - "12.03 walk south 1 -> Route21_North (8, 27) [surfing] [wild]", - "12.03 surf north -> Route21_North (8, 26) [wild]", - "12.03 walk east 5 -> Route21_North (13, 26) [surfing] [wild]", - "12.03 walk east 1 -> Route21_North (14, 26) [surfing] [wild]", - "12.03 surf west -> Route21_North (13, 26) [wild]", - "12.03 walk south 68 -> Route21_South (13, 44) [surfing] [wild] [connection 24]", - "12.03 walk south 1 -> Route21_South (13, 45) [surfing] [wild]", - "12.03 walk south 10 -> CinnabarIsland (13, 5) [connection 5]", + "12.03 land_north_sand_bar from PalletTown (8, 17) facing south to Route21_North (5, 31) facing south", + "12.03 walk south 35 (to wall) (lands after 30) -> Route21_North (8, 32) [surfing] [wild] [connection 3]", + "12.03 walk north 1 -> Route21_North (8, 31)", + "12.03 walk west 3 -> Route21_North (5, 31)", + "12.03 face south -> Route21_North (5, 31)", + "12.03 land_middle_sand_bar from Route21_North (5, 32) facing south to Route21_South (5, 23) facing south", + "12.03 walk south 41 (to wall) (lands after 39) -> Route21_South (5, 23) [surfing] [wild] [connection 18]", + "12.03 land_beach from Route21_South (5, 24) facing south to Route21_South (5, 49) facing east", + "12.03 walk south 25 (to wall) (lands after 24) -> Route21_South (5, 49) [surfing] [wild]", + "12.03 face east -> Route21_South (5, 49)", + "12.03 surf_route_21 from Route21_South (5, 49) facing east to CinnabarIsland (14, 12) facing north", + "12.03 walk east 8 (to wall) -> Route21_South (13, 49)", + "12.03 walk south 6 -> CinnabarIsland (13, 5) [connection 1]", "12.03 walk east 8 (to wall) -> CinnabarIsland (21, 5)", "12.03 walk south 7 -> CinnabarIsland (21, 12)", "12.03 walk west 7 -> CinnabarIsland (14, 12)", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h index 872c365fae..5a46f8cc8a 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPlanGolden19.h @@ -39,12 +39,8 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_19[] = { "19.02 walk west 15 -> Route22 (18, 5)", "19.02 walk south 2 -> Route22 (18, 7)", "19.02 below_gate_guard_trigger from Route22 (18, 9) facing south to Route22_NorthEntrance (7, 3) facing north", - "19.02 walk east 3 (to wall) -> Route22 (21, 9) [wild]", - "19.02 surf east -> Route22 (22, 9) [wild]", - "19.02 walk south 2 -> Route22 (22, 11) [surfing] [wild]", - "19.02 walk south 1 -> Route22 (22, 12) [surfing] [wild]", - "19.02 walk south 2 -> Route22 (22, 14)", - "19.02 walk west 18 (to wall) -> Route22 (4, 14)", + "19.02 walk south 5 -> Route22 (18, 14) [wild]", + "19.02 walk west 14 (to wall) -> Route22 (4, 14)", "19.02 walk north 4 -> Route22 (4, 10)", "19.02 walk east 6 -> Route22 (10, 10)", "19.02 walk north 4 (to wall) -> Route22 (10, 6)", @@ -72,16 +68,12 @@ inline constexpr std::string_view FRLG_PLAN_GOLDEN_19[] = { "19.03 soul_guard_approach from Route23 (13, 117) facing north to Route23 (13, 113) facing north", "19.03 walk north 4 -> Route23 (13, 113) [surfing] [wild]", "19.03 marsh_guard_approach from Route23 (13, 112) facing north to Route23 (9, 95) facing north", - "19.03 walk north 8 -> Route23 (13, 104) [surfing] [wild]", - "19.03 walk north 1 -> Route23 (13, 103) [surfing] [wild]", - "19.03 walk north 4 (to wall) -> Route23 (13, 99)", + "19.03 walk north 13 (to wall) (lands after 9) -> Route23 (13, 99) [surfing] [wild]", "19.03 walk west 4 -> Route23 (9, 99)", "19.03 surf north -> Route23 (9, 98) [wild]", "19.03 walk north 3 -> Route23 (9, 95) [surfing] [wild]", "19.03 land_below_volcano from Route23 (9, 94) facing north to Route23 (17, 77) facing north", - "19.03 walk north 15 -> Route23 (9, 79) [surfing] [wild]", - "19.03 walk north 1 -> Route23 (9, 78) [surfing] [wild]", - "19.03 walk north 1 (to wall) -> Route23 (9, 77)", + "19.03 walk north 17 (to wall) (lands after 16) -> Route23 (9, 77) [surfing] [wild]", "19.03 walk east 8 -> Route23 (17, 77)", "19.03 face north -> Route23 (17, 77)", "19.03 volcano_guard_approach from Route23 (17, 77) facing north to Route23 (12, 62) facing north", diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngReturns.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngReturns.cpp new file mode 100644 index 0000000000..b46572f2b9 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngReturns.cpp @@ -0,0 +1,50 @@ +/* AutoStory RNG Returns + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "CommonFramework/VideoPipeline/VideoFeed.h" +#include "NintendoSwitch/Inference/NintendoSwitch_HomeMenuDetector.h" +#include "NintendoSwitch/Programs/NintendoSwitch_GameEntry.h" +#include "PokemonFRLG_AutoStoryPresses.h" +#include "PokemonFRLG_AutoStoryRngReturns.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using namespace std::chrono_literals; + + +namespace{ + +// After the RNG program `program`: from HOME, if it went there, back to +// the game, then B until the overworld. +void back_to_overworld_after(ConsoleHandle& console, ProControllerContext& context, const std::string& program){ + context.wait_for_all_requests(); + VideoSnapshot screen = console.video().snapshot(); + if (screen && HomeMenuDetector(console, COLOR_RED).detect(screen)){ + console.log("After " + program + ": on HOME, back to the game."); + resume_game_from_home(console, context); + } + mash_until_overworld(console, context, 20000ms); +} + +} // namespace + +void close_trainer_card(ConsoleHandle& console, ProControllerContext& context){ + // B closes the card, then the START menu. + back_to_overworld_after(console, context, "SID Helper"); +} + +void leave_gift_summary(ConsoleHandle& console, ProControllerContext& context){ + // B closes the summary, the party, then the START menu. + back_to_overworld_after(console, context, "Gift RNG"); +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySave.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySave.h index cf5af79645..c3c8c6cc3b 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySave.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySave.h @@ -11,6 +11,7 @@ #define PokemonAutomation_PokemonFRLG_AutoStorySave_H #include +#include #include #include #include @@ -71,15 +72,24 @@ class SaveDialog{ size_t m_nothing_seen = 0; }; +// Why the save card's place (read_save_card_location(), empty if it can't +// be read) shows the game mustn't be saved there, or empty if it may be. +using SavePlaceCheck = std::function; + // From the overworld: SAVE, YES, and YES again to overwrite an older save // (see SaveDialog). The menu's cursor is read, since the game remembers it -// and the menu grows. Returns to the overworld; throws -// OperationFailedException if the save wasn't made. +// and the menu grows. Before the first YES the card's place is read in +// `language` and given to `check`: an error answers NO, goes back to the +// overworld and throws OperationFailedException, unsaved. Returns to the +// overworld; throws OperationFailedException if the save wasn't made. // Overlaps save_game_to_overworld() in PokemonFRLG_Navigation.h. // Ours reads the cursor for any START menu length (the menu grows during the // story) and checks the overworld is back; upstream assumes the full menu. // Could be unified with upstream if needed. -void save_game(ConsoleHandle& console, ProControllerContext& context, StartMenu menu); +void save_game( + ConsoleHandle& console, ProControllerContext& context, StartMenu menu, + Language language, const SavePlaceCheck& check +); // From the overworld: the place the START menu's SAVE card names (see // read_save_card_location()), then NO, back to the overworld. Empty if it diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp new file mode 100644 index 0000000000..c28c9ce9ba --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp @@ -0,0 +1,40 @@ +/* AutoStory Save Checks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "PokemonFRLG_AutoStorySaveChecks.h" +#include "PokemonFRLG_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +std::string checkpoint_end_map(const std::string& id){ + const std::vector& table = story_checkpoints(); + for (size_t c = 0; c + 1 < table.size(); c++){ + if (table[c].id == id && table[c + 1].start){ + return Pokemon::grid_pose_of(*table[c + 1].start).map; + } + } + return ""; +} + +std::string save_end_error(const std::string& end_map, const std::string& place){ + const std::string expected = save_card_place(end_map); + if (expected.empty() || place == expected){ + return ""; + } + if (place.empty()){ + return "The save card's place can't be read, so the end in " + expected + " (" + end_map + ") isn't confirmed."; + } + return "The save card names " + place + ", but the checkpoint ends in " + expected + " (" + end_map + ")."; +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h index 218cebb6c5..9dcc1c87d2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h @@ -44,6 +44,19 @@ std::string save_card_place(const std::string& map); // be read (empty `place`), or the map has no known place. std::string save_place_error(const std::string& start_map, const std::string& place); +// The map checkpoint `id` ends on: where the table's next row starts. +// Empty for the last row, or one whose next row has no start. +std::string checkpoint_end_map(const std::string& id); + +// Why the save card, naming `place`, shows the player isn't where a +// checkpoint that ends on `end_map` ends, or empty if it is: the places +// match, or the map has no known place. A card that can't be read (empty +// `place`) doesn't show the end, so it is refused too: the story has one +// save slot, and a save made in the wrong place loses the one before it +// (12.03 saved on Route 21, short of Cinnabar's Center: console B, +// 2026-11-13). +std::string save_end_error(const std::string& end_map, const std::string& place); + // Why a save standing at `stands` can't start a checkpoint that starts at // `start`, or empty if it can: the same map and tile, and the same facing // when both are known. diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.cpp new file mode 100644 index 0000000000..18dcf4f45b --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.cpp @@ -0,0 +1,266 @@ +/* AutoStory Surf Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Row 12.03's water (StoryData/frlg-12.03.plan.json) on the overworld sim: + * each leg on Route 21's water lands on a sand bar, an island or the shore + * and ends at its bank, or at a wall, never on open water, where a stop + * part-way can't be placed (console B, 2026-11-13 04:15, 06:55 and 07:02). + * The faults seen there are injected at every step on the water. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryEvents.h" +#include "PokemonFRLG_AutoStoryPacedRoute.h" +#include "PokemonFRLG_AutoStoryPoses.h" +#include "PokemonFRLG_AutoStoryRooms.h" +#include "PokemonFRLG_AutoStoryRowSim.h" +#include "PokemonFRLG_AutoStorySurf_Tests.h" +#include "PokemonFRLG_AutoStoryTable.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +namespace{ + +const MapPosition BELOW_CINNABAR_CENTER{"CinnabarIsland", {14, 12}}; + +const StoryCheckpoint& row_12_03(){ + const std::vector& rows = story_checkpoints(); + const auto row = std::find_if(rows.begin(), rows.end(), [](const StoryCheckpoint& data){ return data.id == "12.03"; }); + if (row == rows.end()){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "No checkpoint 12.03."); + } + return *row; +} + +// A table pose as a StoryPose; FRLG's table has one-tile poses only. +StoryPose one_tile(const Pokemon::GridPose& grid_pose){ + const std::optional ret = story_pose(Pokemon::pose_of(grid_pose)); + if (!ret){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Not a one-tile pose."); + } + return *ret; +} + +// A leg on the water that ends where the player can be found again: at a +// wall (the bank of a sand bar it lands on, or the map's edge) or a warp. +bool ends_found(const RouteStep& step){ + return step.action != RouteAction::WALK || !step.surfing || step.to_wall || step.warps; +} + +// What a 12.03 run on the sim did: its failure (empty if none), where it +// ended, the steps it surfed and the legs it planned on the water that +// end in the open. +struct SurfRun{ + std::string failure; + MapPosition end; + int surf_steps = 0; + std::vector surf_holds; + std::vector open_water_legs; + std::vector trainers; +}; + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += "\n " + line; + } + return text; +} + +// 12.03 on a sim with `script`, from Fuchsia's nurse up to the Center's +// door on Cinnabar, as test_fuchsia_to_cinnabar() runs it. Each ROUTE +// step is also planned from where the sim stands, for its water legs. +SurfRun run_12_03(SimScript script = {}){ + const StoryCheckpoint& row = row_12_03(); + StoryPose at = *story_pose(Pokemon::pose_of(*row.start)); + OverworldSim sim(story_maps(), MapPosition{at.map, at.tile}, *at.facing, std::move(script)); + SurfRun run; + for (const StoryStep& step : row.steps){ + if (step.action == CENTER_ENTER_VARIANT){ + break; + } + if (step.kind == StoryStepKind::ROUTE){ + const StoryPose target = one_tile(Pokemon::story_arg(step.args, "target")); + const std::optional> route = plan_overworld_route( + story_maps(), sim.state().position, RouteGoal{{target.map, target.tile}, target.facing}, + story_route_costs(), sim.state().facing + ); + for (const RouteStep& planned : route.value_or(std::vector())){ + if (!ends_found(planned)){ + run.open_water_legs.push_back(step.name + ": " + to_string(planned)); + } + } + } + run.failure = row_step_on_sim(sim, at, step, row.id); + if (!run.failure.empty()){ + break; + } + } + run.end = sim.state().position; + run.surf_steps = sim.state().surf_steps; + run.surf_holds = sim.state().surf_holds; + if (run.failure.empty() && !sim.state().violations.empty()){ + run.failure = "a violation: " + sim.state().violations.back(); + } + run.trainers = sim.state().trainers_beaten; + return run; +} + +// No leg on Route 21's water ends in the open: each lands and is held to +// the bank, or ends at a wall. Before, "south 15 (open)" from (7, 0) was +// stopped 8 tiles in by a message, and the player wasn't found again +// (console B, 2026-11-13 06:55 and 07:02). +UnitTestResult test_no_open_water_legs(){ + const SurfRun run = run_12_03(); + if (!run.failure.empty()){ + return "12.03 failed on the sim: " + run.failure; + } + if (run.surf_steps == 0){ + return "12.03 surfed no step on the sim."; + } + if (!run.open_water_legs.empty()){ + return "12.03 plans legs on the water that end in the open:" + joined(run.open_water_legs); + } + // Swimmer Roland wanders Route21_South's (8..12, 27..31) and sees 4 + // tiles each way: every way from sand bar to sand bar south of + // (5..11, 21..25) crosses his sight. He is the one trainer fought. + if (run.trainers.size() > 1){ + return "12.03 meets more than Swimmer Roland:" + joined(run.trainers); + } + return true; +} + +// A message stops the hold after every step on the water in turn, as +// "a message" did 8 tiles into "south 15 (open)" (console B, 2026-11-13 +// 06:55 and 07:02): each run still ends below Cinnabar's Center door, the +// leg held on to its bank after the message. +UnitTestResult test_message_at_every_surf_step(){ + const SurfRun clean = run_12_03(); + if (!clean.failure.empty()){ + return "12.03 failed on the sim: " + clean.failure; + } + std::vector failed; + for (int step = 1; step <= clean.surf_steps; step++){ + SimScript script; + script.messages_at_surf_steps = {step}; + const SurfRun run = run_12_03(std::move(script)); + if (!run.failure.empty() || !(run.end == BELOW_CINNABAR_CENTER)){ + failed.push_back(std::format( + "message after surfed step {}: ended on {}{}", step, to_string(run.end), + run.failure.empty() ? "" : "; " + run.failure + )); + } + } + if (!failed.empty()){ + return std::format("{} of {} runs with a message on the water fail:{}", failed.size(), clean.surf_steps, joined(failed)); + } + return true; +} + +// A hold on the water that stops short with nothing seen, as "south 86 +// (open)" did (console B, 2026-11-13 04:15), at each in turn: the run +// ends below Cinnabar's Center door, or fails (a leg that lands checks the +// player is on foot); it never goes on from where it wrongly takes the +// player to be and ends elsewhere. +UnitTestResult test_short_hold_never_goes_on_from_a_guess(){ + const SurfRun clean = run_12_03(); + if (!clean.failure.empty()){ + return "12.03 failed on the sim: " + clean.failure; + } + std::vector wrong; + if (clean.surf_holds.empty()){ + return "12.03 held no leg on the water on the sim."; + } + for (const int hold : clean.surf_holds){ + SimScript script; + script.short_holds = {{hold, 3}}; + const SurfRun run = run_12_03(std::move(script)); + if (run.failure.empty() && !(run.end == BELOW_CINNABAR_CENTER)){ + wrong.push_back(std::format("hold {} 3 tiles short: passed, ended on {}", hold, to_string(run.end))); + } + if (run.failure.find("trainer") != std::string::npos || run.failure.find("violation") != std::string::npos){ + wrong.push_back(std::format("hold {} 3 tiles short: {}", hold, run.failure)); + } + } + if (!wrong.empty()){ + return "12.03 went on from a guess after a short hold:" + joined(wrong); + } + return true; +} + +struct SurfInput{ + std::string_view name; + UnitTestResult (*body)(); +}; + +struct SurfExpectation{ + std::string_view description; +}; + +struct SurfCase{ + SurfInput input; + SurfExpectation expectation; +}; + +class Test_AutoStorySurf : public UnitTest{ +public: + explicit Test_AutoStorySurf(SurfCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStory - Surf: {}", test_case.input.name)) + , m_case(test_case) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return m_case.input.body(); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + } + } + +private: + SurfCase m_case; +}; + +const SurfCase SURF_CASES[] = { + { + .input = {.name = "12.03's legs on the water land or end at a wall", .body = test_no_open_water_legs}, + .expectation = {.description = "no leg on Route 21's water ends in the open"}, + }, + { + .input = {.name = "12.03 with a message after each step on the water", .body = test_message_at_every_surf_step}, + .expectation = {.description = "every run ends below Cinnabar's Center door"}, + }, + { + .input = {.name = "12.03 with a hold stopped short, unseen", .body = test_short_hold_never_goes_on_from_a_guess}, + .expectation = {.description = "each run ends below the Center door or fails, never elsewhere"}, + }, +}; + +} // namespace + + +void add_tests_AutoStorySurf(UnitTestDatabase& database){ + for (const SurfCase& item : SURF_CASES){ + database.add(item); + } +} + + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.h new file mode 100644 index 0000000000..7ba5aed19d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.h @@ -0,0 +1,24 @@ +/* AutoStory Surf Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStorySurf_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStorySurf_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// 12.03's legs on Route 21's water, with the faults seen there. +void add_tests_AutoStorySurf(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index b503efea43..ed99db0918 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -700,7 +700,13 @@ UnitTestResult check_lines(const StoryCheckpoint& checkpoint, std::set 0){ + // 12.03's leg down Route21_South's column 5 to the beach: Swimmer + // Roland wanders (8..12, 27..31) and sees 4 tiles each way, across + // every way from sand bar to sand bar. The leg lands on the beach + // and is held to Cinnabar Island's wall, so wherever he stops the + // player, the leg is held on to its end after the battle. + const bool roland = checkpoint.id == "12.03" && step.name == "land_beach"; + if (!lewis && !roland && std::count(stops.begin(), stops.end(), std::nullopt) > 0){ return where + "crosses a wandering trainer's sight where the walk can't tell where they'd stop the player."; } if (const std::string problem = walks ? held_past_lookers(pose, step, beaten) : ""; !problem.empty()){ @@ -1113,6 +1119,84 @@ UnitTestResult test_save_place_error(){ return true; } +// A checkpoint saves only where it ends. 12.03 ends in Cinnabar's Center, +// where 13.01 starts; on console B (2026-11-13 04:16) it stopped surfing +// on Route 21 and saved there, its card reading "ROUTE 21": that save is +// refused, and so is one whose card can't be read. +// 12.03's legs on Route 21's water (console B, 2026-11-13 04:15: "south 86 +// (open)" stopped short at a sand bar unseen, and the route went on from a +// guess): an open leg of more than 20 tiles on the water is followed by a +// leg to a wall or a warp, or lands and then walks to one, which finds the +// player again. +UnitTestResult test_route21_water_legs(){ + std::vector plan; + UnitTestResult result = chain_story_routes(plan); + if (result.result != UnitTestResult::PASSED){ + return result; + } + const auto finds_player = [](std::string_view line){ + return line.find("(to wall)") != std::string_view::npos || line.find("(warp)") != std::string_view::npos; + }; + size_t water_legs = 0; + for (size_t c = 0; c < plan.size(); c++){ + const std::string& line = plan[c]; + if (!line.starts_with("12.03 walk ") || line.find("[surfing]") == std::string::npos){ + continue; + } + water_legs++; + // As the walker checks them: legs of up to 20 tiles go unchecked + // (OPEN_WATER_CHECKED_TILES, PokemonFRLG_OverworldRecovery.cpp). + int tiles = 0; + const size_t number = line.find_first_of("0123456789", std::string_view("12.03 walk ").size()); + if (number != std::string::npos){ + tiles = std::stoi(line.substr(number)); + } + const std::string_view next = c + 1 < plan.size() ? std::string_view(plan[c + 1]) : std::string_view(); + const std::string_view after = c + 2 < plan.size() ? std::string_view(plan[c + 2]) : std::string_view(); + // A step onto a sand bar or the shore, then a leg to a wall on it. + const bool lands_then_wall = next.find(" 1 -> ") != std::string_view::npos && finds_player(after); + if (tiles > 20 && !finds_player(line) && !finds_player(next) && !lands_then_wall){ + return std::format("12.03: \"{}\" is open, and \"{}\" after it doesn't find the player again.", line, next); + } + } + if (water_legs == 0){ + return "12.03 plans no leg on the water."; + } + return true; +} + +UnitTestResult test_save_end_error(){ + const std::string end = checkpoint_end_map("12.03"); + if (end != "CinnabarIsland_PokemonCenter_1F"){ + return "12.03 ends on \"" + end + "\", not CinnabarIsland_PokemonCenter_1F."; + } + if (!checkpoint_end_map("no-such-row").empty()){ + return "A row that isn't in the table has an end map."; + } + struct Case{ + std::string end_map; + std::string place; + bool error; + }; + const Case CASES[] = { + {.end_map = end, .place = "route-21", .error = true}, + {.end_map = end, .place = "cinnabar-island", .error = false}, + {.end_map = end, .place = "", .error = true}, + {.end_map = "Route21_North", .place = "route-21", .error = false}, + // A map with no known place isn't checked. + {.end_map = "Route9", .place = "pewter-city", .error = false}, + {.end_map = "", .place = "route-21", .error = false}, + }; + for (const Case& c : CASES){ + const std::string error = save_end_error(c.end_map, c.place); + if (error.empty() == c.error){ + return "A save card naming \"" + c.place + "\" for an end on " + c.end_map + ": expected " + + (c.error ? "an error" : "no error") + ", got \"" + error + "\"."; + } + } + return true; +} + std::string_view save_action_name(SaveAction action){ switch (action){ case SaveAction::WAIT: @@ -1357,6 +1441,14 @@ const AutoStoryTestCase AUTO_STORY_CASES[] = { .input = {.name = "a reload's save is checked against the start", .body = test_save_place_error}, .expectation = {.description = "detects mismatch between checkpoint initial state and save point"}, }, + { + .input = {.name = "12.03's water legs end where the player is found again", .body = test_route21_water_legs}, + .expectation = {.description = "no open leg on Route 21's water is followed by another open leg"}, + }, + { + .input = {.name = "a checkpoint saves only where it ends", .body = test_save_end_error}, + .expectation = {.description = "refuses a save whose card names another place than the checkpoint's end"}, + }, { .input = {.name = "save dialogs: every YES/NO is YES, a cancel fails", .body = test_save_dialog}, .expectation = {.description = "verifies save confirmation prompt interactions"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-12.03.plan.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-12.03.plan.json index 28838d8592..4dfd2cc058 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-12.03.plan.json +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData/frlg-12.03.plan.json @@ -50,12 +50,49 @@ "declared_after": {"map": "PalletTown", "tiles": [[8, 17]], "facing": "south"}, "why": "the lead's Surf (taught in 12.01): A at the water, YES, onto (8, 17)" }, + { + "id": "land_north_sand_bar", + "kind": "route", + "action": "run", + "args": {"target": {"map": "Route21_North", "tiles": [[5, 31]], "facing": "south"}}, + "why": "south down column 8 until the player lands on Route21_North's sand bar (4..10, 27..32), then over it to its south bank on column 5 (5, 31), where the water stops a player on foot: the leg on the water lands, and the walk on the sand bar ends at walls, so each finds the player again (user's design 2026-11-13: surf until landed, walk to the bank, Surf again; console B 06:55 and 07:02, an open leg on the water stopped by a message couldn't be placed)" + }, + { + "id": "surf_off_north_sand_bar", + "kind": "game_action", + "action": "surf", + "args": {"direction": "south"}, + "declared_after": {"map": "Route21_North", "tiles": [[5, 32]], "facing": "south"}, + "why": "Surf from the bank onto (5, 32)" + }, + { + "id": "land_middle_sand_bar", + "kind": "route", + "action": "run", + "args": {"target": {"map": "Route21_South", "tiles": [[5, 23]], "facing": "south"}}, + "why": "south down column 5 until the player lands on Route21_South's sand bar (5..11, 21..25) at (5, 21), and on to its bank (5, 23): column 5 keeps off the swimmers' lines ((12, 44), (14, 35) and (15, 35) see a tile; (10, 29) sees 4 tiles each way; (15, 24) looks along row 24, which the bank is above)" + }, + { + "id": "surf_off_middle_sand_bar", + "kind": "game_action", + "action": "surf", + "args": {"direction": "south"}, + "declared_after": {"map": "Route21_South", "tiles": [[5, 24]], "facing": "south"}, + "why": "Surf from the bank onto (5, 24)" + }, + { + "id": "land_beach", + "kind": "route", + "action": "run", + "args": {"target": {"map": "Route21_South", "tiles": [[5, 49]], "facing": "east"}}, + "why": "south down column 5 until the player lands on the beach (5..13, 48..49) at (5, 48), and on to (5, 49), where Cinnabar Island's wall below stops the player" + }, { "id": "surf_route_21", "kind": "route", "action": "run", "args": {"target": {"map": "CinnabarIsland", "tiles": [[14, 12]], "facing": "north"}}, - "why": "south over Route 21's water (Pallet Town, Route21_North, Route21_South) to Cinnabar's north shore, off the water onto land and down to below the Center's door (14, 11); round the swimmers' and tuber's lines (pokeguy avoids them, 1:19:00 to 1:19:23)" + "why": "east along the beach to its east end (13, 49), where the water stops the player, then south onto the island and over to below the Center's door (14, 11)" }, { "id": "cinnabar_nurse", diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6287e0686a..e7fd122a51 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1999,6 +1999,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementMechanics.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementStepPresses.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_MovementTurnPresses.h + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBike.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBikeTaps.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldBikeTaps.h @@ -2060,6 +2061,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldWildTiles.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PlayerFacingReader.h + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.cpp + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterFloorReader.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_PokemonCenterRoom.h Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.cpp Source/VideoGames/PokemonFRLG/PokemonFRLG_Navigation.h @@ -2097,6 +2100,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCapture_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladon_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCeladonGym_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySurf_Tests.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCenterHeal.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaffron_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryCinnabarGym_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySilph_Tests.cpp @@ -2220,6 +2226,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryUnauthoredStarts_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSimStubs.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngReturns.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngReturns.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRunPlan_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySimulation_Tests.cpp @@ -2227,6 +2234,7 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPoses.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryPresses.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySave.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStorySaveChecks.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStartFrameCompare.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryStartFrameStore.h