diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp index 7c5b6685e1..f2376d2048 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp @@ -22,6 +22,8 @@ #include "Inference/PokemonLZA_PartyBarDetector.h" #include "Inference/PokemonLZA_SelectionArrowDetector.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" +#include "Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h" #include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h" #include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h" @@ -67,6 +69,8 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStoryObjectiveMarkerReader(database); add_tests_AutoStoryZoneGateReader(database); add_tests_AutoStoryGateWalk(database); + add_tests_AutoStoryFailures(database); + add_tests_AutoStoryFaultSweep(database); add_tests_AutoStoryCatchPlan(database); add_tests_AutoStoryCatchStop(database); add_tests_AutoStoryDialogs(database); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp index 63d0a1129f..059ccc0285 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp @@ -10,6 +10,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h" #include "PokemonLZA_AutoStoryGateWalk.h" #include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -121,12 +122,12 @@ Pokemon::Outcome walk_to_zone_gate_entrance( angle = gate_steer_degrees(read->x / 1000.0); } if (!last && walked >= GATE_FIRST_SEEN_WITHIN){ - return Pokemon::outcome_failed("gate_not_seen", std::format( + return plza_failed("gate_not_seen", std::format( "walk_to_zone_gate_entrance: {} in {} ms of walking", gate_readings(seen, looks, last), walked.count() )); } if (walked >= walk.timeout){ - return Pokemon::outcome_failed("no_prompt", std::format( + return plza_failed("no_prompt", std::format( "walk_to_zone_gate_entrance: no A prompt in {} ms of walking, {}", walk.timeout.count(), gate_readings(seen, looks, last) )); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp index 4416369828..8566242235 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include "Common/Cpp/Color.h" #include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h" #include "CommonTools/Async/InferenceRoutines.h" @@ -15,6 +16,7 @@ #include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h" #include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h" #include "PokemonLZA_AutoStoryMoves.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -44,6 +46,32 @@ void pause_before_leg(PlzaBackend& backend, Pokemon::StoryPausePoints* pause, co backend.scope().throw_if_cancelled(); } +// Whether the screen on a fresh look has not changed since the look +// before (SCREEN_STILL): the input in between did nothing, a dropped turn +// or a dropped hold. A walk into a wall that already stops the player +// (a walk into the nurse's counter) reads still too, and plays its leg +// twice. +bool screen_still(PlzaBackend& backend){ + return backend.observe().events.has(PokemonAutomation::ScreenEvent::SCREEN_STILL); +} + +// A recorded turn: story_turn(), then L to recenter the camera, since the +// walk goes where it looks. +void turn_and_recenter(PlzaBackend& backend, double degrees){ + story_turn(backend, degrees); + backend.press(BUTTON_L, 100ms, 150ms); +} + +// `input`, played again once when the screen stays still after it. +template +void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){ + input(); + if (screen_still(backend)){ + backend.log(std::format("[PLZA] walk: the screen stayed still after {}; once more", what)); + input(); + } +} + } // namespace @@ -54,10 +82,10 @@ Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, do now = read_heading(backend, reader); } if (!now){ - return Pokemon::outcome_failed("heading_unread", "no look read the minimap's heading"); + return plza_failed("heading_unread", "no look read the minimap's heading"); } if (get_angle_between_facing_directions(*now, heading) > tolerance){ - return Pokemon::outcome_failed( + return plza_failed( "heading_missed", std::format("facing {} deg, wanted {:.0f} +/- {:.0f}", *now, heading, tolerance) ); @@ -102,16 +130,20 @@ Pokemon::Outcome run_story_move( } return Pokemon::outcome_ok(); } + // The look the first input is checked against. + backend.observe(); for (size_t index = 0; index < move.legs.size(); index++){ const StoryWalkLeg& leg = move.legs[index]; pause_before_leg(backend, pause, move, index); if (leg.turn != 0){ - // The walk goes where the camera looks: recenter it after the turn. - story_turn(backend, leg.turn); - backend.press(BUTTON_L, 100ms, 150ms); + play_checked(backend, std::format("the turn of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{ + turn_and_recenter(backend, leg.turn); + }); } if (leg.walk > Milliseconds(0)){ - backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); + play_checked(backend, std::format("the walk of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{ + backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK); + }); } } return Pokemon::outcome_ok(); diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h index c959d8f275..3a32419727 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.h @@ -71,9 +71,11 @@ Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, do // From the overworld, walks as `move` says: faces its heading first, if it // has one, then each leg's turn (and L to recenter the camera, since the -// walk goes where it looks) and walk. Nothing is read on the way: a leg -// that is cut short or blocked goes unnoticed, and a step that needs a -// prompt after the walk fails there instead. `heading` reads the compass +// walk goes where it looks) and walk. Not held: a look after each turn +// and each walk; one that shows the screen still (the game dropped the +// input) plays that turn or walk once more, then goes on. A leg cut short +// or blocked part way goes unnoticed, and a step that needs a prompt after +// the walk fails there instead. `heading` reads the compass // (face_heading()). `pause`, if given, is the run's pause points: each leg // is a breakpoint (StoryPausePoints::before_leg(), story_walk_leg_label()). Pokemon::Outcome run_story_move( diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp index 422a479ac9..396a7e2f0d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryScenes.cpp @@ -10,6 +10,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h" #include "PokemonLZA_AutoStoryDialogs.h" #include "PokemonLZA_AutoStoryScenes.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -89,7 +90,7 @@ Pokemon::Outcome mash_scene( log_event_change(backend, look.events, last_events); const bool never_started = started->scene.never_started(look.events, look.when); if (start_window && never_started){ - return Pokemon::outcome_failed("scene_never_started", std::format( + return plza_failed("scene_never_started", std::format( "mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.", start_window->count() )); @@ -100,7 +101,7 @@ Pokemon::Outcome mash_scene( } press_for(backend, action, text_button); } - return Pokemon::outcome_failed("scene_timeout", std::format("mash_scene(): the goal did not show within {} ms.", timeout.count())); + return plza_failed("scene_timeout", std::format("mash_scene(): the goal did not show within {} ms.", timeout.count())); } Pokemon::Outcome mash_story_scene(PlzaBackend& backend, Button text_button, SceneOpening opening){ diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp index a9caba61d8..a1210bd9f2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryWalkScreenChecks.cpp @@ -8,6 +8,7 @@ #include #include #include "PokemonLZA_AutoStoryWalkScreenChecks.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -66,7 +67,7 @@ Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){ } backend.wait(FREE_CONTROL_LOOK); } - return Pokemon::outcome_failed( + return plza_failed( "walk_not_free", std::format("{}: no free control within {} ms, last on {}", walk, FREE_CONTROL_WAIT.count(), busy) ); @@ -109,7 +110,7 @@ Pokemon::Outcome walk_until( return Pokemon::outcome_ok(); } } - return Pokemon::outcome_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count())); + return plza_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count())); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp index 793d667ea8..1392832590 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.cpp @@ -21,6 +21,7 @@ #include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h" #include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h" #include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -170,7 +171,7 @@ Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){ }catch (Pokemon::StoryActionNotImplemented&){ throw; }catch (OperationFailedException& e){ - return Pokemon::outcome_failed("battle_failed", e.message()); + return plza_failed("battle_failed", e.message()); } return Pokemon::outcome_ok(); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp index fcbb43b3e7..e8cb5b42d2 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp @@ -180,10 +180,10 @@ UnitTestResult test_walk_needs_free_control(CancellableScope& scope){ // platform_to_plaza: the first leg, the turn (L, the stick tapped at 90 // degrees) and L again, then the last leg held in slices until the white -// fade. +// fade. Looks: 2 of free control, 3 around the legs' inputs, 1 per slice. UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){ FakeBackend backend = fake_plza({ - look_at(0ms), look_at(150ms), + look_at(0ms), look_at(150ms), look_at(250ms), look_at(4750ms), look_at(5000ms), look_at(12000ms), look_at(12250ms), look_at(12500ms, {EngineEvent::FADE_WHITE}), }, scope); const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza")); @@ -891,6 +891,87 @@ UnitTestResult test_walk_pauses_before_each_leg(CancellableScope& scope){ } +// The inputs of `backend` that start with `kind` ("hold", "press A"). +size_t count_inputs(const FakeBackend& backend, std::string_view kind){ + return (size_t)std::ranges::count_if(backend.inputs(), [kind](const std::string& input){ return input.starts_with(kind); }); +} + +// A walk's leg is played once more when the screen stays still after it +// (the game dropped the hold), and once when it moves. +UnitTestResult test_walk_plays_still_leg_again(CancellableScope& scope){ + StoryCheckpoint row; + row.id = "03.06"; + row.steps.push_back(StoryStep{.kind = Pokemon::StoryStepKind::GAME, .name = "center_to_zone_1", .why = "test", .action = "walk"}); + row.steps.back().args["legs"] = std::vector{{.turn = 0, .ms = 2000}}; + { + FakeBackend backend = fake_plza({look_at(0ms)}, scope); + if (run_plza(backend, row).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "hold") != 1){ + return std::format("Moving: expected one hold, got [{}].", joined(backend.inputs())); + } + } + FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::SCREEN_STILL})}, scope); + if (run_plza(backend, row).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "hold") != 2){ + return std::format("Still: expected two holds, got [{}].", joined(backend.inputs())); + } + return true; +} + +// A gate's "A Enter" alone in a checkpoint (mash_to_move_hints with +// press_a_first). +StoryCheckpoint enter_gate_row(){ + StoryCheckpoint row; + row.id = "03.06"; + row.steps.push_back(StoryStep{ + .kind = Pokemon::StoryStepKind::GAME, .name = "enter_zone_1", .why = "test", .action = "mash_to_move_hints", + .args = {{"press_a_first", true}}, + }); + return row; +} + +// Looks every 250 ms over each span: [from, to), with its events. +struct LookSpan{ + Milliseconds from; + Milliseconds to; + std::initializer_list events; +}; +std::vector> looks_over(std::initializer_list spans){ + std::vector> looks; + for (const LookSpan& span : spans){ + for (Milliseconds at = span.from; at < span.to; at += 250ms){ + looks.push_back(look_at(at, span.events)); + } + } + return looks; +} + +// press_a_first: the party bar under a gone prompt is not control before +// the gate's late fade (2 s after the A): the step waits the fade out. +UnitTestResult test_prompt_waits_for_late_fade(CancellableScope& scope){ + FakeBackend backend = fake_plza(looks_over({ + {0ms, 2000ms, {EngineEvent::PLAYER_CAN_MOVE}}, {2000ms, 3000ms, {EngineEvent::FADE_BLACK}}, + {3000ms, 6000ms, {EngineEvent::PLAYER_CAN_MOVE}}, + }), scope); + if (run_plza(backend, enter_gate_row()).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "press A") != 1){ + return std::format("Expected a pass on one A, got [{}]: {}", joined(backend.inputs()), joined(backend.lines())); + } + if (line_index(backend.lines(), "mash_to_move_hints] fade_black") == backend.lines().size()){ + return std::format("Expected the step to wait the fade out: {}", joined(backend.lines())); + } + return true; +} + +// press_a_first: A again when the prompt stays with no fade (a dropped A). +UnitTestResult test_prompt_pressed_again_when_it_stays(CancellableScope& scope){ + FakeBackend backend = fake_plza(looks_over({ + {0ms, 3000ms, {EngineEvent::PLAYER_CAN_MOVE, EngineEvent::INTERACT_PROMPT}}, + {3000ms, 4000ms, {EngineEvent::FADE_BLACK}}, {4000ms, 7000ms, {EngineEvent::PLAYER_CAN_MOVE}}, + }), scope); + if (run_plza(backend, enter_gate_row()).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "press A") != 2){ + return std::format("Expected a pass on two A, got [{}]: {}", joined(backend.inputs()), joined(backend.lines())); + } + return true; +} + struct EngineTestCase{ std::string name; struct Input{ @@ -951,6 +1032,9 @@ const EngineTestCase ENGINE_TEST_CASES[] = { {.name = "reload_when_chained: \"always\" reloads straight through too", .input = {.body = test_straight_through_reloads_always}, .expectation = {}}, {.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {.body = test_walk_to_prompt}, .expectation = {}}, {.name = "breakpoints: a walk pauses before each leg", .input = {.body = test_walk_pauses_before_each_leg}, .expectation = {}}, + {.name = "walk: a leg the screen stays still after is played once more", .input = {.body = test_walk_plays_still_leg_again}, .expectation = {}}, + {.name = "press_a_first: waits out a late fade, not done on the party bar", .input = {.body = test_prompt_waits_for_late_fade}, .expectation = {}}, + {.name = "press_a_first: A again while the prompt stays with no fade", .input = {.body = test_prompt_pressed_again_when_it_stays}, .expectation = {}}, {.name = "03.10 to_lab_elevator: one look once the legs are in", .input = {.body = test_walk_looks_after_its_legs}, .expectation = {}}, {.name = "03.11 to_mable: the last leg held until Mable's dialog box", .input = {.body = test_walk_ends_in_mables_talk}, .expectation = {}}, {.name = "03.11 to_mable: a black fade in the last leg ends the walk", .input = {.body = test_walk_ends_on_black_fade}, .expectation = {}}, @@ -1019,7 +1103,6 @@ void add_tests_AutoStoryEngine(UnitTestDatabase& database){ } - } // namespace PokemonLZA } // namespace NintendoSwitch } // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp new file mode 100644 index 0000000000..1776dbd850 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp @@ -0,0 +1,88 @@ +/* PokemonLZA AutoStory - Failures + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using Pokemon::StoryFailureClass; + + +const std::vector& plza_failures(){ + using C = StoryFailureClass; + static const std::vector failures{ + // The checkpoint's start and the reload. + {"not_at_start", C::WRONG_START}, + {"older_save", C::WRONG_START}, + {"no_reload", C::RELOAD_FAILED}, + // Walks. A walk that ends where the gate is out of view, or facing + // away from the heading it turned to, is a pose not as planned. + {"gate_not_seen", C::POSE_LOST}, + {"heading_missed", C::POSE_LOST}, + // walk_back_to_zone_road's stops: a catch spot on Wild Zone 1's + // road the objective's distance never reached (03.08). + {"zone_road_missed", C::WALK_SHORT}, + // check_out_of_wild_zone's end check: the minimap's arrow still in + // the zone's green after the gate (console B 2026-11-14). + {"zone_exit_missed", C::WALK_SHORT}, + // A walk held until a fade that never showed. + {"walk_no_end", C::LATE_FADE}, + // A walk that never got free control: a banner, text or a fade + // that stayed. + {"walk_not_free", C::INTERRUPT_FAILED}, + // A screen the step waits for, not seen: the A prompt a walk ends + // on (a missed gate, console B 2026-11-14 13:35), the compass, the + // platform, a scene's end. + {"no_prompt", C::SCREEN_UNREAD}, + {"heading_unread", C::SCREEN_UNREAD}, + {"platform_not_seen", C::SCREEN_UNREAD}, + {"scene_quiet", C::SCREEN_UNREAD}, + {"scene_timeout", C::SCREEN_UNREAD}, + {"scene_stuck", C::SCREEN_UNREAD}, + {"scene_unknown", C::SCREEN_UNREAD}, + {"scene_never_started", C::SCREEN_UNREAD}, + // Battles. + {"battle_failed", C::BATTLE_LOST}, + }; + return failures; +} + +std::optional plza_failure_class(std::string_view name){ + const std::vector& failures = plza_failures(); + const auto found = std::find_if(failures.begin(), failures.end(), [name](const PlzaFailure& failure){ + return failure.name == name; + }); + if (found == failures.end()){ + return std::nullopt; + } + return found->failure_class; +} + +Pokemon::Outcome plza_failed(std::string name, std::string detail){ + const std::optional failure_class = plza_failure_class(name); + return Pokemon::outcome_failed(std::move(name), std::move(detail), failure_class); +} + +std::string_view console_step_failure(std::string_view action){ + if (action == "walk_back_to_zone_road"){ + return "zone_road_missed"; + } + if (action == "check_out_of_wild_zone"){ + return "zone_exit_missed"; + } + return "operation_failed"; +} + + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h new file mode 100644 index 0000000000..453f3c5971 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h @@ -0,0 +1,56 @@ +/* PokemonLZA AutoStory - Failures + * + * From: https://github.com/PokemonAutomation/ + * + * PLZA's own step failure names, each with its class in the shared list + * (Pokemon_StoryFailureClasses.h), so the run log tags a PLZA failure + * "(class=)" as it does the engine's. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryFailures_H +#define PokemonAutomation_PokemonLZA_AutoStoryFailures_H + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailureClasses.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// One of PLZA's failure names and its class. +struct PlzaFailure{ + std::string_view name; + Pokemon::StoryFailureClass failure_class; +}; + +// Every failure name PLZA's steps report that the shared list has no class +// for, with the class it takes. +const std::vector& plza_failures(); + +// The class of PLZA's failure `name`; nothing for a name not in +// plza_failures() (the shared list's names, "operation_failed", keep theirs). +std::optional plza_failure_class(std::string_view name); + +// Pokemon::outcome_failed() with plza_failure_class()'s class for `name`. +Pokemon::Outcome plza_failed(std::string name, std::string detail); + +// A console step's failure name when it throws: its own where a console +// run showed what its failures mean, else "operation_failed". +std::string_view console_step_failure(std::string_view action); + +// Registers the tests of the names' classes. +void add_tests_AutoStoryFailures(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp new file mode 100644 index 0000000000..334960880d --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp @@ -0,0 +1,184 @@ +/* PokemonLZA AutoStory - Failures Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" +#include "PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +namespace{ + +struct FailureClassCase{ + std::string name; + struct Input{ + std::string failure; + }; + Input input; + struct Expectation{ + // The class the log tags; none: no tag. + std::optional tag; + }; + Expectation expectation; +}; + +const FailureClassCase FAILURE_CLASS_CASES[] = { + {.name = "a missed gate is a pose lost", .input = {.failure = "gate_not_seen"}, .expectation = {.tag = "class=pose_lost"}}, + {.name = "no A prompt is a screen unread", .input = {.failure = "no_prompt"}, .expectation = {.tag = "class=screen_unread"}}, + {.name = "a road stop missed is a walk short", .input = {.failure = "zone_road_missed"}, .expectation = {.tag = "class=walk_short"}}, + {.name = "a zone exit missed is a walk short", .input = {.failure = "zone_exit_missed"}, .expectation = {.tag = "class=walk_short"}}, + {.name = "not at the start is a wrong start", .input = {.failure = "not_at_start"}, .expectation = {.tag = "class=wrong_start"}}, + {.name = "a shared name keeps the shared class", .input = {.failure = "operation_failed"}, .expectation = {.tag = "class=operation_failed"}}, + {.name = "an unknown name has no class", .input = {.failure = "no_such_failure"}, .expectation = {.tag = std::nullopt}}, +}; + +UnitTestResult check_failure_class(const FailureClassCase& test){ + const std::string line = Pokemon::to_string_with_class(plza_failed(test.input.failure, "detail")); + const size_t tag = line.find("class="); + if (!test.expectation.tag){ + if (tag != std::string::npos){ + return std::format("Expected no class, got \"{}\".", line); + } + return true; + } + if (line.find(*test.expectation.tag) == std::string::npos){ + return std::format("Expected \"{}\" in \"{}\".", *test.expectation.tag, line); + } + return true; +} + +// A table of PLZA's names, none of which the shared list classes: the +// game's class would hide the list's. +struct NameTableCase{ + std::string name; + struct Input{ + const std::vector& (*table)(); + }; + Input input; + struct Expectation{ + // The names the shared list already has. + std::vector shared; + }; + Expectation expectation; +}; + +const NameTableCase NAME_TABLE_CASES[] = { + {.name = "no PLZA name is a shared one", .input = {.table = plza_failures}, .expectation = {.shared = {}}}, +}; + +UnitTestResult check_name_table(const NameTableCase& test){ + std::vector shared; + for (const PlzaFailure& failure : test.input.table()){ + if (Pokemon::story_failure_class_of_outcome(failure.name)){ + shared.push_back(failure.name); + } + } + if (shared != test.expectation.shared){ + std::string names; + for (std::string_view name : shared){ + names += std::format(" {}", name); + } + return std::format("In the shared list already:{}", names); + } + return true; +} + +// The failure name of a console step that throws. +struct ConsoleStepCase{ + std::string name; + struct Input{ + std::string action; + }; + Input input; + struct Expectation{ + std::string failure; + }; + Expectation expectation; +}; + +const ConsoleStepCase CONSOLE_STEP_CASES[] = { + {.name = "03.08's walk back to the road", .input = {.action = "walk_back_to_zone_road"}, .expectation = {.failure = "zone_road_missed"}}, + {.name = "the check out of the wild zone", .input = {.action = "check_out_of_wild_zone"}, .expectation = {.failure = "zone_exit_missed"}}, + {.name = "03.08's catches keep the shared name", .input = {.action = "catch_in_zone"}, .expectation = {.failure = "operation_failed"}}, +}; + +UnitTestResult check_console_step(const ConsoleStepCase& test){ + const std::string_view failure = console_step_failure(test.input.action); + if (failure != test.expectation.failure){ + return std::format("Expected {}, got {}.", test.expectation.failure, failure); + } + return true; +} + +class Test_FailureClass : public UnitTest{ +public: + explicit Test_FailureClass(const FailureClassCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_failure_class(m_test); + } + +private: + const FailureClassCase& m_test; +}; + +class Test_NameTable : public UnitTest{ +public: + explicit Test_NameTable(const NameTableCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Failure class: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_name_table(m_test); + } + +private: + const NameTableCase& m_test; +}; + +class Test_ConsoleStep : public UnitTest{ +public: + explicit Test_ConsoleStep(const ConsoleStepCase& test) + : UnitTest(std::format("PokemonLZA::AutoStory - Console step failure: {}", test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return check_console_step(m_test); + } + +private: + const ConsoleStepCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryFailures(UnitTestDatabase& database){ + for (const FailureClassCase& test : FAILURE_CLASS_CASES){ + database.add(test); + } + for (const NameTableCase& test : NAME_TABLE_CASES){ + database.add(test); + } + for (const ConsoleStepCase& test : CONSOLE_STEP_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.cpp new file mode 100644 index 0000000000..c89e09b782 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.cpp @@ -0,0 +1,737 @@ +/* PokemonLZA AutoStory - Fault Sweep Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The shared fault sweep (Pokemon_StoryFaultSweep.h) over PLZA's rows, on + * a row sim (RowSim below) of the steps that need no console: the timed + * walks ("walk"), the walk to a wild zone's gate + * ("walk_to_zone_gate_entrance") and A through a gate or door's prompt + * ("mash_to_move_hints" with press_a_first). A step the sim does not play + * (a console step, a scene) puts the player at its end, if it started at + * its start with no fade under way; else the player is off the story. A + * look with no input taken since the look before shows the screen still + * (SCREEN_STILL), as the console's frame diff does. + * + * The sim faults it injects, each a class the console showed or could: + * + * - LOST_TAP: each input the sim acts on (a stick, a hold, an A), one at + * a time, dropped by the game. + * - LATE_FADE: every prompt's fade starts LATE_FADE later. + * - WARP_NO_OP: each prompt's fade, one at a time, leaves the player + * where it was, prompt and all. + * - SLOW_FADE_IN: after every fade the game takes no input for + * SLOW_FADE_IN. + * - SCENE_END_POSE: the player facing SCENE_END_TURN degrees off, either + * way, at the row's start (a load) and after each step the game ends + * (a prompt's scene). + * + * Not swept: UNSEEN_STEP (PLZA's walks read no steps), MOUNT_LOST (no + * mount yet), DARK_CAVE, SCENE_EARLY and BATTLE_OPENING (none on these + * rows), WILD_BATTLES (real-time wild Pokemon, which the sim does not + * model). The PLZA failures seen on console B that no sim fault injects + * are classed by their failure names instead (PokemonLZA_AutoStoryFailures.h): + * a missed gate (no_prompt, screen_unread), stuck on the title + * (launch_failed), 03.08's walk back to the road short of a catch spot + * (zone_road_missed, walk_short). 03.08's catches (catch_in_zone) throw + * for many reasons and stay operation_failed. + * + * The sim's world is small: the player is at the end of a row's step (a + * spot), facing a compass heading. A timed walk reaches its end only on the + * inputs and facings of the row's clean run; a walk to a gate gets there when it walks the gate's way: its heading, + * or with none (03.08's, from the road at 60) the way the clean run faced + * at the walk's start. Each row runs clean first; a variant fails when the + * row then fails or ends elsewhere. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/CancellableScope.h" +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon/AutoStory/Pokemon_StoryFaultSweep.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryFakeBackend.h" +#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.h" +#include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryStoryFiles.h" +#include "PokemonLZA_AutoStoryFaultSweep_Tests.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using Pokemon::FaultClass; +using Pokemon::FaultSweepResult; +using Pokemon::Observation; +using Pokemon::StoryCheckpoint; +using Pokemon::StoryStep; +using Pokemon::ZonePose; + + +namespace{ + +const std::string TEST_PREFIX = "PokemonLZA::AutoStory"; + +// The rows swept. +const std::vector SWEPT_ROWS{"03.08"}; + +// The sweeps the program fails today, by test name, with the likely fix. +const std::map KNOWN_FAILING{ + {"PokemonLZA::AutoStory - Fault sweep: 03.08, warp_no_op", + "leave_zone_1 is done with the player still at the far gate's A Leave: after the fade the prompt shows again. A again on any prompt after a fade is not safe: the street outside the gate may show the gate's A Enter where the warp lands, and A there would undo it. " + "Likely fix: after the fade, read the minimap's zone green (MinimapZoneDetector); still inside it with the prompt up, press A again; and an end check that the arrow is out of the green, so 03.09 never starts in the zone"}, +}; + +// How much later than the clean run's a late fade starts. +constexpr Milliseconds LATE_FADE = 1500ms; +// After a fade, how long the game takes no input in SLOW_FADE_IN. +constexpr Milliseconds SLOW_FADE_IN = 1000ms; +// How far off a scene's end leaves the player's facing. +constexpr double SCENE_END_TURN = 30; + +// A prompt's fade: it starts this long after the A, and lasts this long. +constexpr Milliseconds FADE_START = 300ms; +constexpr Milliseconds FADE_LENGTH = 1500ms; +// The walk to a gate: how far it starts from the gate's prompt, at what +// pace (~5 units/s, 03.06's walks, console B 2026-11-08), how far off the +// gate's bearing the screen shows it and a slice walks toward it. +constexpr double GATE_DISTANCE = 15; +constexpr double UNITS_PER_MS = 0.005; +constexpr double GATE_IN_VIEW = 30; +constexpr double GATE_WALK_CONE = 20; +constexpr double VIEW_DEGREES = 60; +// A turn: story_turn()'s stick is this short and released. +constexpr Milliseconds TURN_STICK = 150ms; + +// The degrees from `from` to `to`, -180 to 180. +double turn_between(double from, double to){ + double turn = std::fmod(to - from, 360.0); + if (turn > 180){ + turn -= 360; + }else if (turn <= -180){ + turn += 360; + } + return turn; +} + +// Which steps the sim plays. +enum class SimStep{ + WALK, // "walk": timed legs + GATE, // "walk_to_zone_gate_entrance" + PROMPT, // "mash_to_move_hints" with press_a_first + NOT_PLAYED, // a console step or a scene +}; + +SimStep sim_step(const StoryStep& step){ + if (step.action == "walk"){ + return SimStep::WALK; + } + if (step.action == "walk_to_zone_gate_entrance"){ + return SimStep::GATE; + } + if (step.action == "mash_to_move_hints" && Pokemon::story_arg(step.args, "press_a_first")){ + return SimStep::PROMPT; + } + return SimStep::NOT_PLAYED; +} + +// The faults one variant plays. +struct SimFaults{ + // LOST_TAP: the input the game drops, from 0 over those the sim acts on. + std::optional dropped_input = std::nullopt; + // LATE_FADE: how much later every prompt's fade starts. + Milliseconds late_fade = 0ms; + // WARP_NO_OP: the prompt's fade, from 0, that leaves the player there. + std::optional no_op_fade = std::nullopt; + // SLOW_FADE_IN: no input this long after each fade. + Milliseconds fade_in = 0ms; + // SCENE_END_POSE: the facing off by `turn` after the step named + // `after` ("" for the row's start). + struct PoseOff{ + std::string after; + double turn = 0; + }; + std::optional pose_off = std::nullopt; +}; + +// What the row's clean run did, which a faulted run is held to. +struct CleanRun{ + // Per timed walk: its inputs and the facing at each. + struct Walk{ + std::vector inputs; + std::vector facings; + }; + std::map> walks; + // Per step: the facing it ends on. + std::map> end_facing; + size_t inputs = 0; + size_t fades = 0; + // Inputs that came within SLOW_FADE_IN of a fade's end. + size_t inputs_after_fades = 0; +}; + +// A prompt's fade under way. +struct Fade{ + WallClock start; + WallClock end; + bool moves; + std::string to; +}; + +// One row's game, played as the inputs come. +// code-health: allow public_methods -- the backend calls it plays, plus the row driver's step bounds and the clean run's record +class RowSim : public Pokemon::StoryBackend{ +public: + RowSim(const StoryCheckpoint& row, SimFaults faults, const CleanRun* clean, CancellableScope& scope) + : m_row(row), m_faults(std::move(faults)), m_clean(clean), m_scope(scope), m_start(current_time()) + { + for (size_t i = 0; i + 1 < row.steps.size(); i++){ + if (sim_step(row.steps[i + 1]) == SimStep::PROMPT){ + m_prompt_spots.insert(row.steps[i].name); + } + } + turn_if_posed(""); + } + + const CleanRun& record() const{ + return m_record; + } + // Where the row ended, when not at the end of its step `last` with + // the game settled: "at 03.08's zone_1_to_far_gate", "off the story: ...". + std::optional off_end(std::string_view last) const{ + if (m_spot == last && !m_fade){ + return std::nullopt; + } + std::string where = m_spot.starts_with("off") ? m_spot : std::format("at {}", spot_name(m_spot)); + return m_fade ? std::format("{}, in a fade", where) : where; + } + + // Step `index` of the row starts. + void begin_step(size_t index){ + m_step = index; + m_walk = CleanRun::Walk{}; + m_gate.reset(); + if (sim_step(step()) == SimStep::GATE && m_spot == start_of(index)){ + m_gate = GATE_DISTANCE; + m_gate_bearing = gate_bearing(index); + } + } + // The step started by begin_step() is over. + void end_step(){ + const StoryStep& now = step(); + switch (sim_step(now)){ + case SimStep::WALK: + if (walk_reached()){ + arrive(now.name); + }else if (m_spot == start_of(m_step)){ + m_spot = std::format("off the story after {}", now.name); + } + break; + case SimStep::NOT_PLAYED: + if (m_spot == start_of(m_step) && !m_fade){ + arrive(now.name); + }else if (!m_spot.starts_with("off")){ + m_spot = std::format("off the story: {} started {}", now.name, m_fade ? "in a fade" : std::format("at {}", spot_name(m_spot))); + } + break; + default: + break; + } + m_record.walks[now.name] = m_walk; + m_record.end_facing[now.name] = m_facing; + turn_if_posed(now.name); + } + + virtual Pokemon::StoryBackendKind kind() const override{ + return Pokemon::StoryBackendKind::SCRIPT; + } + virtual void press(Button button, Milliseconds hold, Milliseconds release) override{ + if (button == BUTTON_A && take_input("press A") && sim_step(step()) == SimStep::PROMPT && prompt_shows()){ + start_fade(); + } + m_clock += hold + release; + } + virtual void tap(Pokemon::Direction) override{ + m_clock += 100ms; + } + virtual void hold(Pokemon::Direction, Milliseconds duration, Pokemon::StoryPace) override{ + take_input(std::format("hold {}ms", duration.count())); + m_clock += duration; + } + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, Pokemon::StoryPace) override{ + const bool turn = duration <= TURN_STICK && release > 0ms; + if (take_input(std::format("{} {:g}deg {}ms", turn ? "turn" : "stick", degrees, duration.count()))){ + if (turn){ + m_facing = std::fmod(m_facing + degrees + 360, 360); + }else{ + walk_toward_gate(degrees, duration); + } + } + m_clock += duration + release; + } + virtual void wait(Milliseconds duration) override{ + m_clock += duration; + } + virtual Observation observe() override; + virtual Pokemon::Outcome battle(const Pokemon::StoryActionArgs&) override{ + return Pokemon::outcome_failed("no_battle", "the row sim plays no battles"); + } + virtual void log(std::string_view line) override{ + m_lines.emplace_back(line); + } + virtual CancellableScope& scope() override{ + return m_scope; + } + +private: + const StoryStep& step() const{ + return m_row.steps[m_step]; + } + // Where step `index` starts: the end of the one before, "" for the first. + std::string start_of(size_t index) const{ + return index == 0 ? "" : m_row.steps[index - 1].name; + } + static std::string spot_name(const std::string& spot){ + return spot.empty() ? "the row's start" : spot; + } + WallClock now() const{ + return m_start + m_clock; + } + // The compass bearing of the gate from step `index`'s start: its + // heading arg, else the facing the clean run had there. 03.08's walk + // has no heading: walk_back_to_zone_road faces the objective, past the + // gate to its left, and the walk steers by the gate's pillars. + double gate_bearing(size_t index) const{ + const auto heading = step().args.find("heading"); + if (heading != step().args.end()){ + return (double)std::get(heading->second); + } + if (m_clean == nullptr || index == 0){ + return m_facing; + } + return m_clean->end_facing.at(start_of(index)); + } + bool prompt_shows() const{ + return m_prompt_spots.contains(m_spot) && !m_fade; + } + + void arrive(const std::string& name){ + m_spot = name; + if (m_clean != nullptr){ + m_facing = m_clean->end_facing.at(name); + } + } + void turn_if_posed(std::string_view after){ + if (m_faults.pose_off && m_faults.pose_off->after == after){ + m_facing = std::fmod(m_facing + m_faults.pose_off->turn + 360, 360); + } + } + + // Counts `input` and says whether the game takes it: not dropped, not + // in a fade nor in the fade-in after one. + bool take_input(std::string input){ + settle_fade(); + const size_t index = m_record.inputs++; + if (m_last_fade_end && now() < *m_last_fade_end + SLOW_FADE_IN){ + m_record.inputs_after_fades++; + } + const bool fading = m_fade && now() >= m_fade->start; + const bool locked = m_last_fade_end && now() < *m_last_fade_end + m_faults.fade_in; + if (index == m_faults.dropped_input || fading || locked){ + return false; + } + m_moved = true; + m_walk.facings.push_back(m_facing); + m_walk.inputs.push_back(std::move(input)); + return true; + } + + void start_fade(){ + const size_t index = m_record.fades++; + const WallClock start = now() + FADE_START + m_faults.late_fade; + m_fade = Fade{.start = start, .end = start + FADE_LENGTH, .moves = index != m_faults.no_op_fade, .to = step().name}; + } + // The fade, once over, moves the player. + void settle_fade(){ + if (!m_fade || now() < m_fade->end){ + return; + } + if (m_fade->moves){ + arrive(m_fade->to); + } + m_last_fade_end = m_fade->end; + m_fade.reset(); + m_moved = true; + } + + // A slice of the walk to the gate. + void walk_toward_gate(double degrees, Milliseconds duration){ + if (!m_gate || sim_step(step()) != SimStep::GATE){ + return; + } + if (std::abs(turn_between(m_facing + degrees, m_gate_bearing)) > GATE_WALK_CONE){ + return; + } + *m_gate -= UNITS_PER_MS * (double)duration.count(); + if (*m_gate <= 0){ + m_gate.reset(); + arrive(step().name); + } + } + + // A timed walk ends where the clean run's did on its inputs, at its + // facings. + bool walk_reached() const{ + if (m_spot != start_of(m_step)){ + return false; + } + if (m_clean == nullptr){ + return true; + } + const CleanRun::Walk& clean = m_clean->walks.at(step().name); + if (clean.inputs != m_walk.inputs){ + return false; + } + for (size_t i = 0; i < clean.facings.size(); i++){ + if (std::abs(turn_between(clean.facings[i], m_walk.facings[i])) > 1){ + return false; + } + } + return true; + } + + const StoryCheckpoint& m_row; + const SimFaults m_faults; + const CleanRun* m_clean; + CancellableScope& m_scope; + const WallClock m_start; + Milliseconds m_clock = 0ms; + std::set> m_prompt_spots; + size_t m_step = 0; + std::string m_spot; + double m_facing = 0; + CleanRun::Walk m_walk; + // How far the walk to the gate has left; none when no gate is ahead. + std::optional m_gate; + // The gate's compass bearing (gate_bearing()) while m_gate is set. + double m_gate_bearing = 0; + std::optional m_fade; + std::optional m_last_fade_end; + // An input was taken or a fade ended since the last look: the screen + // changed. + bool m_moved = false; + CleanRun m_record; + std::vector m_lines; +}; + +Observation RowSim::observe(){ + settle_fade(); + Observation look; + look.when = now(); + if (m_fade && now() >= m_fade->start){ + look.events.set(ScreenEvent::FADE_BLACK, true); + return look; + } + look.events.set(ScreenEvent::PLAYER_CAN_MOVE, true); + look.events.set(ScreenEvent::SCREEN_STILL, !m_moved); + m_moved = false; + look.readings[std::string(Pokemon::STORY_HEADING)] = (int)std::lround(m_facing) % 360; + if (prompt_shows()){ + look.events.set(ScreenEvent::INTERACT_PROMPT, true); + } + if (m_gate && sim_step(step()) == SimStep::GATE){ + const double off = turn_between(m_facing, m_gate_bearing); + if (std::abs(off) <= GATE_IN_VIEW){ + look.readings[std::string(ZONE_GATE_X_READING)] = (int)std::lround(500 + off * 1000 / VIEW_DEGREES); + look.readings[std::string(ZONE_GATE_WIDTH_READING)] = 150; + } + } + return look; +} + + +// The row `id`. +const StoryCheckpoint& row_of(std::string_view id){ + for (const StoryCheckpoint& row : story_checkpoints()){ + if (row.id == id){ + return row; + } + } + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("No row {}.", id)); +} + +// How a row's run ended. +struct RowRun{ + std::optional failure; + CleanRun record; +}; + +// `step` alone through PLZA's engine on `sim`: its failure, if any. +std::optional run_played(const StoryCheckpoint& row, const StoryStep& step, RowSim& sim){ + const Pokemon::StoryGame game = make_plza_story_game(StarterChoice::CHIKORITA); + StoryCheckpoint one{.id = row.id, .title = row.title}; + one.steps.push_back(step); + Pokemon::StoryEngine engine(game, sim); + Pokemon::RunState state; + const Pokemon::StoryEngineReport report = engine.run_checkpoint(one, Pokemon::SavePoint::RUN_END, state); + if (report.verdict == Pokemon::CheckpointVerdict::PASSED){ + return std::nullopt; + } + return report.run.failure; +} + +// The row's steps on `sim`: the ones it plays through PLZA's engine, each +// alone, the others by the sim. Fails on a step that fails or on an end +// elsewhere. +RowRun run_row(const StoryCheckpoint& row, const std::string& variant, RowSim& sim){ + for (size_t i = 0; i < row.steps.size(); i++){ + const StoryStep& step = row.steps[i]; + sim.begin_step(i); + const std::optional failed = sim_step(step) == SimStep::NOT_PLAYED ? std::nullopt : run_played(row, step, sim); + if (failed){ + return RowRun{.failure = FaultSweepResult::Failure{ + .step = step.name, .variant = variant, .stopped = Pokemon::to_string_with_class(*failed), + }, .record = sim.record()}; + } + sim.end_step(); + } + const std::string& last = row.steps.back().name; + const std::optional off = sim.off_end(last); + if (off){ + return RowRun{.failure = FaultSweepResult::Failure{.step = last, .variant = variant, .stopped = std::format("ended {}", *off)}, .record = sim.record()}; + } + return RowRun{.failure = std::nullopt, .record = sim.record()}; +} + +// The row's clean run; `not_swept` says why when it fails clean. +struct CleanResult{ + CleanRun run; + std::optional not_swept; +}; +CleanResult clean_run(const StoryCheckpoint& row, CancellableScope& scope){ + RowSim sim(row, SimFaults{}, nullptr, scope); + const RowRun run = run_row(row, "clean", sim); + if (run.failure){ + return CleanResult{.run = run.record, .not_swept = std::format("fails clean at {}: {}", run.failure->step, run.failure->stopped)}; + } + return CleanResult{.run = run.record, .not_swept = std::nullopt}; +} + +// Each variant over `row_id`, after its clean run; `variants_of` lists +// them from the clean run, with a reason when the fault has no chance. +using Variant = std::pair; +FaultSweepResult sweep_row( + const std::string& row_id, + const std::function(const StoryCheckpoint&, const CleanRun&, std::optional&)>& variants_of +){ + CancellableHolder scope; + const StoryCheckpoint& row = row_of(row_id); + const CleanResult clean = clean_run(row, scope); + FaultSweepResult result; + if (clean.not_swept){ + result.not_swept = clean.not_swept; + return result; + } + std::optional no_chance; + const std::vector variants = variants_of(row, clean.run, no_chance); + if (no_chance){ + result.not_swept = no_chance; + return result; + } + for (const Variant& variant : variants){ + RowSim sim(row, variant.second, &clean.run, scope); + const RowRun run = run_row(row, variant.first, sim); + result.variants++; + if (run.failure){ + result.failures.push_back(*run.failure); + } + } + return result; +} + +std::vector lost_tap_variants(const StoryCheckpoint&, const CleanRun& clean, std::optional&){ + std::vector variants; + for (size_t i = 0; i < clean.inputs; i++){ + variants.emplace_back(std::format("input {}", i + 1), SimFaults{.dropped_input = i}); + } + return variants; +} + +std::vector late_fade_variants(const StoryCheckpoint&, const CleanRun& clean, std::optional& no_chance){ + if (clean.fades == 0){ + no_chance = "no fade the sim plays"; + return {}; + } + return {{"every fade late", SimFaults{.late_fade = LATE_FADE}}}; +} + +std::vector warp_no_op_variants(const StoryCheckpoint&, const CleanRun& clean, std::optional& no_chance){ + if (clean.fades == 0){ + no_chance = "no fade the sim plays"; + return {}; + } + std::vector variants; + for (size_t i = 0; i < clean.fades; i++){ + variants.emplace_back(std::format("fade {}", i + 1), SimFaults{.no_op_fade = i}); + } + return variants; +} + +std::vector slow_fade_in_variants(const StoryCheckpoint&, const CleanRun& clean, std::optional& no_chance){ + if (clean.inputs_after_fades == 0){ + no_chance = std::format("no input the sim plays comes within {} ms of a fade's end", SLOW_FADE_IN.count()); + return {}; + } + return {{"every fade-in slow", SimFaults{.fade_in = SLOW_FADE_IN}}}; +} + +std::vector scene_end_pose_variants(const StoryCheckpoint& row, const CleanRun&, std::optional&){ + std::vector ends{""}; + for (const StoryStep& step : row.steps){ + if (sim_step(step) == SimStep::PROMPT){ + ends.push_back(step.name); + } + } + std::vector variants; + for (const std::string& end : ends){ + for (const double turn : {SCENE_END_TURN, -SCENE_END_TURN}){ + variants.emplace_back( + std::format("{:+g} deg after {}", turn, end.empty() ? "the load" : end), + SimFaults{.pose_off = SimFaults::PoseOff{.after = end, .turn = turn}} + ); + } + } + return variants; +} + +// The injector of one sim fault, from its variants. +Pokemon::StoryFaultSweep::Injector injector( + std::vector (*variants_of)(const StoryCheckpoint&, const CleanRun&, std::optional&) +){ + return [variants_of](Logger&, const std::string& row){ + return sweep_row(row, variants_of); + }; +} + +std::shared_ptr plza_fault_sweep(){ + auto sweep = std::make_shared(); + sweep->test_prefix = TEST_PREFIX; + sweep->rows = SWEPT_ROWS; + sweep->injectors = { + {FaultClass::LOST_TAP, injector(lost_tap_variants)}, + {FaultClass::LATE_FADE, injector(late_fade_variants)}, + {FaultClass::WARP_NO_OP, injector(warp_no_op_variants)}, + {FaultClass::SLOW_FADE_IN, injector(slow_fade_in_variants)}, + {FaultClass::SCENE_END_POSE, injector(scene_end_pose_variants)}, + }; + sweep->known_failing = KNOWN_FAILING; + return sweep; +} + + +// A row's walk to a zone's gate through PLZA's engine, on looks facing +// the step's heading (if it has one) with no gate on screen: the failure +// line carries the class of PLZA's own failure name. +struct GateClassCase{ + std::string name; + struct Input{ + std::string row; + }; + Input input; + struct Expectation{ + // In a failure line of the run's log. + std::string failure; + std::string tag; + }; + Expectation expectation; +}; + +const GateClassCase GATE_CLASS_CASES[] = { + { + .name = "03.06's missed gate is logged as class=pose_lost", + .input = {.row = "03.06"}, + .expectation = {.failure = "gate_not_seen", .tag = "(class=pose_lost)"}, + }, + { + .name = "03.08's missed far gate is logged as class=pose_lost", + .input = {.row = "03.08"}, + .expectation = {.failure = "gate_not_seen", .tag = "(class=pose_lost)"}, + }, +}; + +UnitTestResult check_gate_class(const GateClassCase& test, CancellableScope& scope){ + const StoryCheckpoint& row = row_of(test.input.row); + StoryCheckpoint one{.id = row.id, .title = row.title}; + for (const StoryStep& step : row.steps){ + if (sim_step(step) == SimStep::GATE){ + one.steps.push_back(step); + } + } + if (one.steps.empty()){ + return std::format("{} has no walk to a gate.", test.input.row); + } + Observation look; + look.when = current_time(); + look.events.set(ScreenEvent::PLAYER_CAN_MOVE, true); + const auto heading = one.steps[0].args.find("heading"); + if (heading != one.steps[0].args.end()){ + look.readings[std::string(Pokemon::STORY_HEADING)] = (int)std::get(heading->second); + } + Pokemon::FakeStoryBackend backend(Pokemon::FakeStoryScript{ + .looks = {look}, .battle = Pokemon::outcome_ok(), .kind = Pokemon::StoryBackendKind::SCRIPT, + }, scope); + const Pokemon::StoryGame game = make_plza_story_game(StarterChoice::CHIKORITA); + Pokemon::StoryEngine engine(game, backend); + Pokemon::RunState state; + engine.run_checkpoint(one, Pokemon::SavePoint::RUN_END, state); + for (const std::string& line : backend.lines()){ + if (line.find(test.expectation.failure) != std::string::npos && line.find(test.expectation.tag) != std::string::npos){ + return true; + } + } + return std::format("No log line with {} {}.", test.expectation.failure, test.expectation.tag); +} + +class Test_GateClass : public UnitTest{ +public: + explicit Test_GateClass(const GateClassCase& test) + : UnitTest(std::format("{} - Failure class: {}", TEST_PREFIX, test.name)) + , m_test(test) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_gate_class(m_test, scope); + } + +private: + const GateClassCase& m_test; +}; + +} // namespace + + +void add_tests_AutoStoryFaultSweep(UnitTestDatabase& database){ + Pokemon::add_fault_sweep_tests(database, plza_fault_sweep()); + for (const GateClassCase& test : GATE_CLASS_CASES){ + database.add(test); + } +} + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h new file mode 100644 index 0000000000..0580512cec --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h @@ -0,0 +1,27 @@ +/* PokemonLZA AutoStory - Fault Sweep Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStoryFaultSweep_Tests_H +#define PokemonAutomation_PokemonLZA_AutoStoryFaultSweep_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// The shared fault sweep (Pokemon_StoryFaultSweep.h) over PLZA's rows, on +// a row sim of the steps that need no console, and that a PLZA step's +// failure line carries its class. +void add_tests_AutoStoryFaultSweep(UnitTestDatabase& database); + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp index f2d054a66a..9757b610ee 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp @@ -36,6 +36,7 @@ #include "PokemonLZA_AutoStorySceneMachine.h" #include "PokemonLZA_AutoStoryStepValidator.h" #include "PokemonLZA_AutoStoryGame.h" +#include "PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -232,6 +233,49 @@ Outcome gate_walk_step(Pokemon::StoryBackend& backend, const StoryActi return walk_to_zone_gate_entrance(backend, heading, zone_gate_reader(), walk); } +// After A at a gate's or door's prompt: how long a fade or text may take +// to show (the sweep's late fade starts 1.8 s after the A), how often it +// is looked for, and how many times A is pressed while the prompt stays +// with neither (a dropped A). +constexpr Milliseconds PROMPT_TAKEN_WAIT(3000); +constexpr Milliseconds PROMPT_POLL(250); +constexpr size_t PROMPT_PRESSES = 3; + +// Whether `look` shows that A at a prompt was taken: a fade, or text. +bool prompt_taken(const Pokemon::Observation& look){ + using PokemonAutomation::ScreenEvent; + return look.events.has(ScreenEvent::FADE_BLACK) || look.events.has(ScreenEvent::FADE_WHITE) || + look.events.has(ScreenEvent::DIALOG_BOX) || look.events.has(ScreenEvent::CHOICE_BOX); +} + +// A at the A prompt the player stands at, then looks for up to +// PROMPT_TAKEN_WAIT until a fade or text shows; while the prompt stays +// with neither, A again, PROMPT_PRESSES times at most. The party bar +// shows under the prompt (console B, 2026-11-10), so the scene machine +// that follows would take it for control before a late fade. A prompt +// gone with no fade or text is left to the scene machine, as is one +// still up after the last A. +void press_through_prompt(Pokemon::StoryBackend& backend){ + for (size_t press = 1; press <= PROMPT_PRESSES; press++){ + backend.press(BUTTON_A, 160ms, 400ms); + bool prompt = true; + for (Milliseconds waited(0); waited < PROMPT_TAKEN_WAIT; waited += PROMPT_POLL){ + backend.scope().throw_if_cancelled(); + const Pokemon::Observation look = backend.observe(); + if (prompt_taken(look)){ + return; + } + prompt = look.events.has(PokemonAutomation::ScreenEvent::INTERACT_PROMPT); + backend.wait(PROMPT_POLL); + } + if (!prompt){ + backend.log(std::format("[PLZA] mash_to_move_hints: the prompt went with no fade in {} ms", PROMPT_TAKEN_WAIT.count())); + return; + } + backend.log(std::format("[PLZA] mash_to_move_hints: the prompt is still up {} ms after A {}", PROMPT_TAKEN_WAIT.count(), press)); + } +} + // Builds the handlers. The console actions hold the console; without one // (a test, a simulation) their specs are console_only and the engine // refuses them before their handler. @@ -265,7 +309,7 @@ class PlzaHandlers{ throw; }catch (OperationFailedException& e){ backend.log(std::format("[PLZA] {} failed: {}", name, e.message())); - return Pokemon::outcome_failed("operation_failed", e.message()); + return plza_failed(std::string(console_step_failure(name)), e.message()); } return Pokemon::outcome_ok(); }; @@ -328,13 +372,14 @@ class PlzaHandlers{ // of control, A every 2 s, for text the detectors miss (01.01's ad). // A banner that stays past the 10 s gets A up to 3 times (01.03's // "MAIN MISSION 02"), so the step ends with control. "press_a_first" - // presses A once at a prompt (02.03's Hotel Z door, "A Enter"), which - // would otherwise end the step at once as control. "quiet_is_control" + // presses A at a prompt (02.03's Hotel Z door, "A Enter"), which + // would otherwise end the step at once as control, and waits for its + // fade or text (press_through_prompt()). "quiet_is_control" // ends it on the quiet time instead of failing, inside Hotel Z. static Handler move_hints(){ return [](Pokemon::StoryBackend& backend, const StoryActionCall& call, Pokemon::RunState&){ if (Pokemon::story_arg(call.args, "press_a_first")){ - backend.press(BUTTON_A, 160ms, 400ms); + press_through_prompt(backend); } SceneMachineConfig config; config.quiet = Milliseconds(Pokemon::story_arg(call.args, "quiet_ms")); @@ -441,7 +486,7 @@ Outcome wait_for_platform(Pokemon::StoryBackend& backend, Milliseconds progress.text_waiting = text_now; backend.wait(PLATFORM_POLL); } - return Pokemon::outcome_failed("platform_not_seen", std::format( + return plza_failed("platform_not_seen", std::format( "No platform on {} looks in a row within {} ms (autosave {}).", PLATFORM_LOOKS, timeout.count(), progress.autosave_seen ? "seen" : "not seen" )); @@ -462,7 +507,7 @@ Outcome walk_to_prompt(Pokemon::StoryBackend& backend, Milliseconds ti return Pokemon::outcome_ok(); } if (walked >= timeout){ - return Pokemon::outcome_failed("no_prompt", std::format("walk_to_prompt: no A prompt in {} ms of walking", timeout.count())); + return plza_failed("no_prompt", std::format("walk_to_prompt: no A prompt in {} ms of walking", timeout.count())); } backend.hold(Pokemon::Direction::NORTH, PROMPT_WALK_SLICE, Pokemon::StoryPace::WALK); walked += PROMPT_WALK_SLICE; @@ -837,7 +882,7 @@ Outcome load_checkpoint_start(StoryEnv& env, Pokemon::StoryBackend& ba if (action == StartLoadAction::STOP){ const std::string_view tried = start_tried(from); backend.log(std::format("[Checkpoint] {}: not at its start after {}, stopping", row, tried)); - return Pokemon::outcome_failed("not_at_start", std::format("not at {}'s start after {}", row, tried)); + return plza_failed("not_at_start", std::format("not at {}'s start after {}", row, tried)); } const StartSave save = start_load_save(action); backend.log(std::format("[Save] {}: {} {}", row, save == StartSave::RUNNING ? "keeping" : "loading", start_save_name(save))); @@ -864,7 +909,7 @@ constexpr size_t BOUNDARY_LOOKS = 4; // from, which a reload_when_chained row comes after, but an older one. Outcome reload_at_boundary(const Pokemon::StoryGame& game, Pokemon::StoryBackend& backend, std::string_view row){ if (!game.reload){ - return Pokemon::outcome_failed("no_reload", "this game has no reload"); + return plza_failed("no_reload", "this game has no reload"); } const Outcome reloaded = game.reload(backend); if (!reloaded.succeeded()){ @@ -875,7 +920,7 @@ Outcome reload_at_boundary(const Pokemon::StoryGame& game, Pokemon::St backend.scope().throw_if_cancelled(); streak = backend.observe().readings.contains(PLATFORM_READING) ? streak + 1 : 0; if (streak >= PLATFORM_LOOKS){ - return Pokemon::outcome_failed("older_save", std::format( + return plza_failed("older_save", std::format( "the reload did not land at {}'s start: the station platform shows, so it loaded an older save", row )); } diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp index ddddd66e83..28d8ee750d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -13,6 +13,7 @@ #include #include "CommonTools/ScreenMachine/ScreenMachine.h" #include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryFailures.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -453,7 +454,7 @@ Pokemon::Outcome scene_failure(std::string_view label, const std::string& failur }else if (failure.starts_with("The screen matches none")){ kind = "scene_unknown"; } - return Pokemon::outcome_failed(std::string(kind), std::format("{}: {}", label, failure)); + return plza_failed(std::string(kind), std::format("{}: {}", label, failure)); } // Whether a new look shows the mission banner. @@ -505,7 +506,7 @@ Pokemon::Outcome scene_outcome( if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ // Nothing busy for the quiet time, and no party bar or controls // hint: a cutscene with no text, or control the detectors miss. - return Pokemon::outcome_failed("scene_quiet", std::format( + return plza_failed("scene_quiet", std::format( "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 )); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index e48a2003c7..c79ff51e66 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2562,6 +2562,11 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryGame.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryLaunch.cpp