diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index 58a39e28bc..3759999f9f 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -230,6 +230,9 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES}) include(cmake/EmbedTextFiles.cmake) pa_embed_text_files(SerialProgramsLib Source/Pokemon/Navigation/TestMaps Pokemon/Navigation/Pokemon_NavigationTestMaps.h PokemonAutomation::Pokemon navigation_test_map_json) +pa_embed_text_files(SerialProgramsLib Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures + PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureData.h + PokemonAutomation::NintendoSwitch::PokemonBDSP console_failure_json) # Prepared data is validated by the shared publisher tooling. No source-map # fallback or network download is performed during configuration. set(POKESIM_TOOLS_SOURCE "$ENV{POKESIM_TOOLS_SOURCE}" CACHE PATH "Pinned PokemonSimulation tools checkout") diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp new file mode 100644 index 0000000000..d593f1949c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.cpp @@ -0,0 +1,133 @@ +/* Leg Replay + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon_LegReplay.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +[[noreturn]] void bad(const std::string& name, const std::string& what){ + throw std::runtime_error("console failure " + name + ": " + what); +} + +Direction direction_of(const std::string& name, const std::string& text){ + for (const Direction direction : ALL_DIRECTIONS){ + if (direction_name(direction) == text){ + return direction; + } + } + bad(name, "unknown direction \"" + text + "\""); +} + +TilePos tile_of(const std::string& name, const JsonArray& array){ + if (array.size() != 2){ + bad(name, "a tile is [x, y]"); + } + return {(int)array[0].to_integer_throw(), (int)array[1].to_integer_throw()}; +} + +ReplayFade fade_of(const std::string& name, const std::string& text){ + if (text == "unseen"){ + return ReplayFade::UNSEEN; + } + if (text == "black"){ + return ReplayFade::BLACK; + } + if (text == "dim"){ + return ReplayFade::DIM; + } + if (text == "white"){ + return ReplayFade::WHITE; + } + bad(name, "unknown fade \"" + text + "\""); +} + +ReplayFault fault_of(const std::string& name, const JsonObject& json){ + const std::string kind = json.get_string_throw("kind"); + ReplayFault fault; + if (kind == "fade_missed" || kind == "banner_missed"){ + fault.kind = kind == "fade_missed" ? ReplayFaultKind::FADE_MISSED : ReplayFaultKind::BANNER_MISSED; + fault.index = (size_t)json.get_integer_throw("index"); + }else if (kind == "moved_further" || kind == "hold_through_warp" || kind == "scene_ends_further"){ + fault.kind = kind == "moved_further" ? ReplayFaultKind::MOVED_FURTHER + : kind == "hold_through_warp" ? ReplayFaultKind::HOLD_THROUGH_WARP : ReplayFaultKind::SCENE_ENDS_FURTHER; + fault.step = json.get_string_throw("step"); + fault.direction = direction_of(name, json.get_string_throw("direction")); + fault.tiles = (size_t)json.get_integer_default("tiles", 1); + fault.fade = fade_of(name, json.get_string_default("fade", "unseen")); + fault.far_pose_of = json.get_string_default("far_pose_of", ""); + }else if (kind == "moved_less"){ + fault.kind = ReplayFaultKind::MOVED_LESS; + fault.step = json.get_string_throw("step"); + fault.index = (size_t)json.get_integer_throw("index"); + fault.tiles = (size_t)json.get_integer_throw("tiles"); + }else if (kind == "person_stands"){ + fault.kind = ReplayFaultKind::PERSON_STANDS; + fault.name = json.get_string_throw("name"); + fault.tile = tile_of(name, json.get_array_throw("tile")); + }else{ + bad(name, "unknown fault kind \"" + kind + "\""); + } + return fault; +} + +} // namespace + + +std::string replay_fault_kind_name(ReplayFaultKind kind){ + switch (kind){ + case ReplayFaultKind::FADE_MISSED: + return "fade_missed"; + case ReplayFaultKind::BANNER_MISSED: + return "banner_missed"; + case ReplayFaultKind::MOVED_FURTHER: + return "moved_further"; + case ReplayFaultKind::HOLD_THROUGH_WARP: + return "hold_through_warp"; + case ReplayFaultKind::SCENE_ENDS_FURTHER: + return "scene_ends_further"; + case ReplayFaultKind::PERSON_STANDS: + return "person_stands"; + case ReplayFaultKind::MOVED_LESS: + return "moved_less"; + } + return ""; +} + +ConsoleFailure parse_console_failure(const std::string& name, const std::string& text){ + const JsonValue value = parse_json(text); + const JsonObject& json = value.to_object_throw(); + ConsoleFailure out; + out.name = name; + out.checkpoint = json.get_string_throw("checkpoint"); + if (const JsonObject* start = json.get_object("start")){ + out.start = ReplayTile{start->get_string_throw("map"), tile_of(name, start->get_array_throw("tile"))}; + out.start_facing = direction_of(name, start->get_string_throw("facing")); + } + for (const auto& item : json.get_array_throw("faults")){ + out.faults.push_back(fault_of(name, item.to_object_throw())); + } + const JsonObject& failed = json.get_object_throw("failed"); + out.failed_step = failed.get_string_throw("step"); + out.failed_error = failed.get_string_throw("error"); + out.frame = json.get_string_throw("frame"); + if (const JsonArray* evidence = json.get_array("evidence")){ + for (const auto& line : *evidence){ + out.evidence.push_back(line.to_string_throw()); + } + } + return out; +} + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.h new file mode 100644 index 0000000000..4f699912c1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegReplay.h @@ -0,0 +1,107 @@ +/* Leg Replay + * + * From: https://github.com/PokemonAutomation/ + * + * A console failure as a small fault file (the files of a game's ConsoleFailures folder beside + * a game's simulation tests), made from a console log's [Leg] records and + * raw walk facts by devtools/maps/leg_replay.py. The file keeps only what + * the simulation cannot work out from its map: where the run started if + * not on its checkpoint's start, the detections it missed, the walks that + * went further than counted, and the people the map lacks. Trainers, NPCs + * and walls come from the map. Where the failure happened and the frame it + * was checked against ride along for the reader. + * + * {"checkpoint": "01.02", + * "faults": [{"kind": "hold_through_warp", "step": "past_curtis", + * "direction": "east", "fade": "unseen", "far_pose_of": "into_oreburgh"}], + * "failed": {"step": "into_oreburgh", "error": "no_banner"}, +// code-health: allow header_symbols -- the fault file model and its reader are one unit, a split would scatter it + */ + +#ifndef PokemonAutomation_Pokemon_LegReplay_H +#define PokemonAutomation_Pokemon_LegReplay_H + +#include +#include +#include +#include +#include "Pokemon_Direction.h" +#include "Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A tile of a named map. +struct ReplayTile{ + std::string map; + TilePos tile; +}; + +// What went differently on the console than the program counted on. +enum class ReplayFaultKind{ + FADE_MISSED, // the `index`th fade of the run goes unseen + BANNER_MISSED, // the `index`th map banner of the run goes unseen + MOVED_FURTHER, // the step's counted taps went `tiles` further (onto a warp, `far_pose_of`) + HOLD_THROUGH_WARP, // the step's hold toward `direction` ran on through a warp (`far_pose_of`) + SCENE_ENDS_FURTHER, // the player stood `tiles` toward `direction` past where the scene step leaves them + PERSON_STANDS, // `name`, a person of the map (who wanders or stays put), stood on `tile` + MOVED_LESS, // the `index`th hold of `step`'s walk (from 1) moved `tiles` fewer than it was held for, with + // nothing on screen: the walker took the move as seen +}; +// "fade_missed", "banner_missed", "moved_further", "hold_through_warp", "scene_ends_further", "person_stands", +// "moved_less". +std::string replay_fault_kind_name(ReplayFaultKind kind); + +// How the walk saw the fade of a warp it went through. +enum class ReplayFade{ + UNSEEN, + BLACK, + DIM, + WHITE, +}; + +// One fault; the fields of its kind (above) are the ones that count. +struct ReplayFault{ + ReplayFaultKind kind = ReplayFaultKind::FADE_MISSED; + // FADE_MISSED, BANNER_MISSED: counted within the checkpoint, from 0. + // MOVED_LESS: counted within the step's walk, from 1. + size_t index = 0; + // MOVED_FURTHER, HOLD_THROUGH_WARP, SCENE_ENDS_FURTHER, MOVED_LESS. + std::string step; + Direction direction = Direction::EAST; + size_t tiles = 1; + ReplayFade fade = ReplayFade::UNSEEN; + bool banner_up = false; + // The step of the checkpoint whose pose after it is where the warp lands. + std::string far_pose_of; + // PERSON_STANDS. + std::string name; + TilePos tile; +}; + +// A console failure: the checkpoint it ran, its faults and what it failed at. +struct ConsoleFailure{ + std::string name; + std::string checkpoint; + // Where the run starts if not on the checkpoint's start. + std::optional start; + std::optional start_facing; + std::vector faults; + // What the console did: the step and the start of its error. + std::string failed_step; + std::string failed_error; + // The frame the failure was checked against, a file name in the lab's + // failure screenshots. + std::string frame; + // The log lines the faults were worked out from. + std::vector evidence; +}; + +// Reads a fault file; throws on a missing or unknown field. +ConsoleFailure parse_console_failure(const std::string& name, const std::string& text); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp index 2b5857e0ed..87bc3165e6 100644 --- a/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/PokemonBDSP_Tests.cpp @@ -29,6 +29,7 @@ #include "Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h" #include "Programs/AutoStory/Utils/PokemonBDSP_AutoStory_WildBattlePolicy_Tests.h" #include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.h" +#include "Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h" #include "PokemonBDSP_Tests.h" namespace PokemonAutomation{ @@ -60,6 +61,7 @@ void add_tests(UnitTestDatabase& database){ add_tests_AutoStory_RecoveryWalk(database); add_tests_AutoStory_StorySim(database); add_tests_AutoStory_SimFailureCases(database); + add_tests_AutoStory_ConsoleFailureReplay(database); add_tests_AutoStory_FaultSweep(database); add_tests_AutoStory_StoryActions(database); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-18-36-east-mouth-slid.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-18-36-east-mouth-slid.json new file mode 100644 index 0000000000..ae8a2a4a05 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-18-36-east-mouth-slid.json @@ -0,0 +1,13 @@ +{ + "about": "lab-B 2026-11-12 18:27-18:42, 01.02: by_the_east_mouth's counted taps went two tiles past the leg's end onto the Gate's east warp, its fade unseen; into_oreburgh then saw no fade and no OreburghCity banner.", + "checkpoint": "01.02", + "faults": [ + {"kind": "moved_further", "step": "by_the_east_mouth", "direction": "east", "tiles": 2, "fade": "unseen", "far_pose_of": "into_oreburgh"} + ], + "failed": {"step": "into_oreburgh", "error": "no_banner"}, + "frame": "", + "evidence": [ + "[Leg] by_the_east_mouth leg 7: east 3 planned, moved 2, dead_reckoned at (26, 22)", + "into_oreburgh: no fade and no OreburghCity banner in 4 taps east" + ] +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-20-23-hold-out-the-east-door.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-20-23-hold-out-the-east-door.json new file mode 100644 index 0000000000..d327f785ff --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-20-23-hold-out-the-east-door.json @@ -0,0 +1,15 @@ +{ + "about": "lab-B 2026-11-12 20:20:50, 01.02 past_curtis, after the same one tile of drift as 21:02 (Curtis stopped the east walk at 9 tiles, a fight left tiles 13-16 possible): the hold east to find us again ran along row 22 and out the Gate's east door, its fade unseen; the program took it as ending at (16, 22) and walked on in Oreburgh City, which it took for the Gate, until into_oreburgh saw no banner at 20:23:09.", + "checkpoint": "01.02", + "faults": [ + {"kind": "person_stands", "name": "camper_1", "tile": [17, 22]}, + {"kind": "scene_ends_further", "step": "the_hiker", "direction": "east", "tiles": 1}, + {"kind": "hold_through_warp", "step": "past_curtis", "direction": "east", "fade": "unseen", "far_pose_of": "into_oreburgh"} + ], + "failed": {"step": "into_oreburgh", "error": "no_banner"}, + "frame": "", + "evidence": [ + "[Walk] past curtis: east 3 (to wall) to find us again: screen change 266.96 at 1200 ms (the fade), then limit after 1850 ms", + "[Route] past curtis: found again at OreburghGate (16, 22), after 1 hold(s)." + ] +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-21-02-camper-drift.json b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-21-02-camper-drift.json new file mode 100644 index 0000000000..92a87cca1a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/ConsoleFailures/01.02-21-02-camper-drift.json @@ -0,0 +1,16 @@ +{ + "about": "lab-B 2026-11-12 21:02:53, 01.02 past_curtis: the player stood one tile east of the table's (7, 22) after the Hiker's scene (hikers_trigger's step east, the scene after it; the same drift as 20:15), so the east walk from the believed (7, 22) met Camper Curtis's sight line on column 17 after 9 tiles, not 10, and he stopped it at (17, 22) while the program believed (15-16, 22). Its recovery hold east then ran 12 tiles, out of the Gate.", + "checkpoint": "01.02", + "faults": [ + {"kind": "person_stands", "name": "camper_1", "tile": [17, 22]}, + {"kind": "scene_ends_further", "step": "the_hiker", "direction": "east", "tiles": 1} + ], + "failed": {"step": "past_curtis", "error": "operation_failed: past curtis: east 1 (to wall) to find us again: still moving after 12 tiles, past the wall 1 tiles away at most: position unknown."}, + "frame": "20261112-210253120715-attempt0.png", + "evidence": [ + "[Event] 01.02/7 hikers_trigger: a step east, limit.", + "[Walk] past curtis: east 13 (open): blocked after 8800 ms, 9 moved chunks", + "[Route] someone the map lacks is at OreburghGate (17, 22)", + "[Leg] past_curtis leg 1: east, moved 9, seen at (15, 22),(16, 22)" + ] +} diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureData.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureData.h new file mode 100644 index 0000000000..9c7891b5cb --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureData.h @@ -0,0 +1,29 @@ +/* BDSP AutoStory - Console failure data + * + * From: https://github.com/PokemonAutomation/ + * + * The fault files in ConsoleFailures/, built into the program + * (cmake/EmbedTextFiles.cmake): (file name, text), sorted by name. + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_ConsoleFailureData_H +#define PokemonAutomation_PokemonBDSP_AutoStory_ConsoleFailureData_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// (file name without ".json", text) of each fault file. +const std::vector>& console_failure_json(); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.cpp new file mode 100644 index 0000000000..0fe3a4f321 --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.cpp @@ -0,0 +1,203 @@ +/* BDSP AutoStory - Console failure replay tests + * + * From: https://github.com/PokemonAutomation/ + * + * Every fault file (Pokemon_LegReplay.h) of a console failure runs here on + * the story simulation: trainers, NPCs and walls come from the map, only + * the file's faults are injected, and the checkpoint it names must pass. + * A file is red on the program that failed on the console and green on the + * program that fixed it. No case has code of its own. + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/Navigation/Pokemon_LegReplay.h" +#include "PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryTable.h" +#include "PokemonBDSP_AutoStory_ConsoleFailureData.h" +#include "PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h" +#include "PokemonBDSP_AutoStory_StorySim.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + +namespace{ + +using Pokemon::ConsoleFailure; +using Pokemon::ReplayFault; +using Pokemon::ReplayFaultKind; +using Pokemon::StoryStep; + +const StoryStep* find_step(const std::vector& rows, const std::string& row, const std::string& name){ + for (const StoryCheckpoint& checkpoint : rows){ + if (checkpoint.id != row){ + continue; + } + for (const StoryStep& step : checkpoint.steps){ + if (step.name == name){ + return &step; + } + } + } + return nullptr; +} + +SimFade sim_fade_of(Pokemon::ReplayFade fade){ + switch (fade){ + case Pokemon::ReplayFade::UNSEEN: + return SimFade::UNSEEN; + case Pokemon::ReplayFade::BLACK: + return SimFade::BLACK; + case Pokemon::ReplayFade::DIM: + return SimFade::DIM; + case Pokemon::ReplayFade::WHITE: + return SimFade::WHITE; + } + return SimFade::UNSEEN; +} + +// The walk that goes on past its end, as the fault says. +SimSlide slide_of(const ConsoleFailure& failure, const std::vector& rows, const ReplayFault& fault){ + SimSlide slide{ + .step = fault.step, .direction = fault.direction, .tiles = fault.tiles, + .fade = sim_fade_of(fault.fade), .banner_up = fault.banner_up, + }; + if (fault.far_pose_of.empty()){ + return slide; + } + const StoryStep* landing = find_step(rows, failure.checkpoint, fault.far_pose_of); + if (landing == nullptr || !landing->pose_after){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, + std::format("{}: step {} has no pose after it", failure.name, fault.far_pose_of)); + } + slide.far = Pokemon::pose_of(*landing->pose_after); + return slide; +} + +// The faults as the simulation's knobs. +void inject(const ConsoleFailure& failure, const std::vector& rows, SimMisses& misses, SimWorldKnobs& knobs){ + if (failure.start){ + knobs.start_at = Pokemon::pose_of(Pokemon::GridPose{ + failure.start->map, {failure.start->tile}, *failure.start_facing + }); + } + for (const ReplayFault& fault : failure.faults){ + switch (fault.kind){ + case ReplayFaultKind::FADE_MISSED: + misses.fade = fault.index; + break; + case ReplayFaultKind::BANNER_MISSED: + misses.banner = fault.index; + break; + case ReplayFaultKind::MOVED_FURTHER: + knobs.slides.push_back(slide_of(failure, rows, fault)); + break; + case ReplayFaultKind::HOLD_THROUGH_WARP: + knobs.hold_slides.push_back(slide_of(failure, rows, fault)); + break; + case ReplayFaultKind::SCENE_ENDS_FURTHER: + knobs.scene_slides.push_back(slide_of(failure, rows, fault)); + break; + case ReplayFaultKind::PERSON_STANDS: + knobs.standing[fault.name] = fault.tile; + break; + case ReplayFaultKind::MOVED_LESS: + // The BDSP story sim takes each hold as walked; only the FRLG overworld sim shortens holds. + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format( + "{}: the fault \"{}\" is not one the BDSP story simulation injects", + failure.name, Pokemon::replay_fault_kind_name(fault.kind) + )); + } + } +} + +struct ReplayInput{ + std::string name; + std::string text; +}; +struct ReplayExpectation{ +}; +struct ReplayCase{ + ReplayInput input; + ReplayExpectation expectation; +}; + +// The note the sim leaves when a fault really happened; empty for kinds with no note. +std::string fault_marker(ReplayFaultKind kind){ + switch (kind){ + case ReplayFaultKind::MOVED_FURTHER: return "[Sim] slid"; + case ReplayFaultKind::HOLD_THROUGH_WARP: return "[Sim] held through a warp"; + case ReplayFaultKind::SCENE_ENDS_FURTHER: return "[Sim] scene ended"; + default: return ""; + } +} + +// The cases are discovered from the embedded fault JSONs, so a new console failure needs no +// C++ edit; the code-health test-case table rule does not fit a data-driven test. +class Test_ConsoleFailureReplay : public UnitTest{ +public: + explicit Test_ConsoleFailureReplay(ReplayCase test_case) + : UnitTest(std::format("PokemonBDSP::AutoStory - Console failure replay: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger& logger, CancellableScope&) const override{ + try{ + const ConsoleFailure failure = Pokemon::parse_console_failure(m_case.input.name, m_case.input.text); + const std::vector rows = story_checkpoints(); + if (find_step(rows, failure.checkpoint, failure.failed_step) == nullptr){ + return std::format("{}: the step {} it failed at is not in {}", m_case.input.name, failure.failed_step, failure.checkpoint); + } + SimMisses misses{.fade = {}, .banner = {}}; + SimWorldKnobs knobs; + inject(failure, rows, misses, knobs); + SimExecutor executor(logger, std::move(misses), std::move(knobs)); + const Pokemon::SegmentReport report = run_rows_on(executor, logger, {.from = failure.checkpoint, .to = failure.checkpoint}, rows); + // A fault that never happened on the sim replays nothing. + for (const ReplayFault& fault : failure.faults){ + const std::string marker = fault_marker(fault.kind); + if (marker.empty()){ + continue; + } + const auto& notes = executor.notes(); + if (std::none_of(notes.begin(), notes.end(), [&](const std::string& note){ return note.find(marker) != std::string::npos; })){ + return std::format("{}: the {} fault at {} never happened on the sim", m_case.input.name, + Pokemon::replay_fault_kind_name(fault.kind), fault.step); + } + } + if (report.completed){ + return true; + } + return std::format("stopped in {}: {} (the console failed at {}: {})", executor.result().failed_step, + executor.result().error, failure.failed_step, failure.failed_error); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + }catch (std::exception& e){ + return std::string(e.what()); + } + } + +private: + ReplayCase m_case; +}; + +} // namespace + +void add_tests_AutoStory_ConsoleFailureReplay(UnitTestDatabase& database){ + std::vector cases; + for (const auto& [name, text] : console_failure_json()){ + cases.push_back({.input = {name, text}, .expectation = {}}); + } + for (const ReplayCase& item : cases){ + database.add(item); + } +} + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h new file mode 100644 index 0000000000..2f3fbb122a --- /dev/null +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h @@ -0,0 +1,25 @@ +/* BDSP AutoStory - Console failure replay tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests_H +#define PokemonAutomation_PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonBDSP{ + + +// One test per fault file in ConsoleFailures/: the checkpoint it names runs +// on the story simulation with only the file's faults injected, and must pass. +void add_tests_AutoStory_ConsoleFailureReplay(UnitTestDatabase& database); + + +} // namespace PokemonBDSP +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp index b989fa548b..fd3b837f4a 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp @@ -377,9 +377,12 @@ bool GridSim::press_a(){ m_player.trainer = trainer.name; } } - for (const SimWanderer& wanderer : m_wanderers){ + for (SimWanderer& wanderer : m_wanderers){ if (wanderer.pos == faced){ m_player.dialog = true; + if (wanderer.goes_home){ + wanderer.pos = *wanderer.goes_home; + } } } return m_player.dialog; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h index 8e366beb9e..2d39e28128 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.h @@ -104,6 +104,7 @@ struct SimWanderer{ Milliseconds step_interval = Milliseconds(2000); // how often the NPC attempts a move Milliseconds elapsed = Milliseconds(0); // time accumulated towards next step Milliseconds time_offset = Milliseconds(0); // initial start time offset + std::optional goes_home = std::nullopt; // where they walk back to once talked to }; // Simulated grid environment. diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp index 763918ce41..93288d5dd2 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp @@ -2,6 +2,7 @@ * * From: https://github.com/PokemonAutomation/ * +// code-health: allow file_lines -- the sim executor, its knobs and the faults the console failures need share one translation unit */ #include @@ -292,6 +293,23 @@ WalkResult SimKnobRouteIO::walk_to_wall(const RouteWallWalk& walk){ if (result.stop == WalkStop::BLOCKED && pushing){ result.walked += m_knobs.pushing_someone; } + if (result.warp.fade != WarpFade::NONE){ + for (const SimSlide& item : m_knobs.hold_slides){ + if (item.step == m_step && item.direction == walk.direction){ + m_misses.slid = true; + note("[Sim] held through a warp to " + Pokemon::to_string(item.far) + ".", RouteNote::LOG); + result = through_warp(item, result); + // A hold's walk stops on a change of the whole screen that walking cannot + // make when no battle follows (a warp whose fade fell between two frames, + // "its fade unseen", 8a570a642; the console's 21:54 hold), where the + // taps of a slide read the time out. + if (item.fade == SimFade::UNSEEN){ + result.stop = WalkStop::TRANSITION; + } + return result; + } + } + } return result; } @@ -324,19 +342,30 @@ WalkResult SimKnobRouteIO::slide_on(const SimSlide& slide, WalkResult result){ continue; } note("[Sim] slid onto a warp to " + Pokemon::to_string(slide.far) + ".", RouteNote::LOG); - m_cross(slide.far); - m_misses.banner_up = slide.banner_up ? slide.far.map : ""; - if (slide.fade == SimFade::UNSEEN){ - m_misses.banner_soon = slide.far.map; - return result; - } - const double brightness = slide.fade == SimFade::BLACK ? SIM_DOOR_FADE : slide.fade == SimFade::DIM ? SIM_DIM_FADE : SIM_WHITE_FADE; - result.warp.fade = fade_of(sim_screen(brightness, SIM_FADE_STDDEV)); - if (result.warp.fade != WarpFade::NONE){ - result.stop = WalkStop::TRANSITION; + return through_warp(slide, result); + } + return result; +} + +// The player went through a warp to `slide.far` on a walk: the fade as the +// slide says, the walk's own result otherwise. +WalkResult SimKnobRouteIO::through_warp(const SimSlide& slide, WalkResult result){ + m_cross(slide.far); + m_misses.banner_up = slide.banner_up ? slide.far.map : ""; + if (slide.fade == SimFade::UNSEEN){ + m_misses.banner_soon = slide.far.map; + // The console's walk read the time out, not a fade. + result.warp.fade = WarpFade::NONE; + if (result.stop == WalkStop::TRANSITION){ + result.stop = WalkStop::LIMIT; } return result; } + const double brightness = slide.fade == SimFade::BLACK ? SIM_DOOR_FADE : slide.fade == SimFade::DIM ? SIM_DIM_FADE : SIM_WHITE_FADE; + result.warp.fade = fade_of(sim_screen(brightness, SIM_FADE_STDDEV)); + if (result.warp.fade != WarpFade::NONE){ + result.stop = WalkStop::TRANSITION; + } return result; } @@ -467,13 +496,14 @@ std::vector SimExecutor::wanderers(const StoryWorld& world) const{ } for (const auto& object : story_map(map).objects){ const auto stands = m_knobs.standing.find(object.name); - if (!object.wander || (!m_knobs.wander_seed && stands == m_knobs.standing.end())){ + const bool still = stands != m_knobs.standing.end(); + // A person who stays put on the map (a trainer) and stood elsewhere on the console. + if ((!object.wander && !still) || (!m_knobs.wander_seed && !still)){ continue; } const uint64_t seed = m_knobs.wander_seed.value_or(0) * 1000003 + out.size() * 7919 + (uint64_t)(object.x * 31 + object.y); - const TilePos low = story_to_world(world, map, {object.wander->x0, object.wander->y0}); - const TilePos high = story_to_world(world, map, {object.wander->x1, object.wander->y1}); - const bool still = stands != m_knobs.standing.end(); + const TilePos low = story_to_world(world, map, object.wander ? TilePos{object.wander->x0, object.wander->y0} : stands->second); + const TilePos high = story_to_world(world, map, object.wander ? TilePos{object.wander->x1, object.wander->y1} : stands->second); out.push_back({ .name = object.name, .box = {.x0 = low.x, .y0 = low.y, .x1 = high.x, .y1 = high.y}, @@ -482,6 +512,7 @@ std::vector SimExecutor::wanderers(const StoryWorld& world) const{ .step_interval = still ? Milliseconds(0) : Milliseconds(2000), .elapsed = Milliseconds(0), .time_offset = still ? Milliseconds(0) : Milliseconds((int64_t)(seed % 2000)), + .goes_home = object.wander ? std::nullopt : std::optional(story_to_world(world, map, {object.x, object.y})), }); } } @@ -578,6 +609,7 @@ void SimExecutor::start_scene_at_end(const StoryStep& step){ void SimExecutor::land(const std::string& step){ count_coupons(step); + slide_after_scene(step); const auto lands = m_knobs.lands.find(step); if (lands == m_knobs.lands.end()){ return; @@ -591,6 +623,22 @@ void SimExecutor::land(const std::string& step){ } } +// The step's scene ran on: the player stands further along than the table's +// pose says (SimWorldKnobs::scene_slides), and stays there like a slide on (the next walks are not checked against the table). +void SimExecutor::slide_after_scene(const std::string& step){ + for (const SimSlide& item : m_knobs.scene_slides){ + if (item.step != step || !m_placed){ + continue; + } + for (size_t tile = 0; tile < item.tiles; tile++){ + m_sim.tap(item.direction, ROUTE_MS_PER_TILE_ACROSS, false); + } + m_sim.end_dialogue(); + m_astray = true; + m_notes.push_back(std::format("[Sim] scene ended {} tile(s) further, at {}.", item.tiles, to_string(m_sim.player().pos))); + } +} + // Through a door or a cave mouth onto the step's far side, the player's // facing kept: a hold goes on from there. bool SimExecutor::cross(){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h index b0c4ae623b..93e18a048e 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h @@ -153,6 +153,17 @@ std::string play_scene(const SimScene& scene, const TalkOverRule& rule); // unless set. All of it is deterministic: wanderers walk from `wander_seed`. struct SimWorldKnobs{ std::vector slides = {}; + // A hold of the step `step` toward `direction` (a recovery's hold to a + // wall, a walk to a wall) that runs on through a warp its watchers + // miss: the player lands on `far` (lab-B 2026-11-12 20:20:50, 01.02's + // past_curtis, a hold east along row 22 that went out the Gate's east + // door). `tiles` is not used. + std::vector hold_slides = {}; + // A scene or trigger step `step` that leaves the player `tiles` toward + // `direction` past the pose the table gives it (lab-B 2026-11-12 21:02, + // 01.02's the_hiker: the walk east after it met Curtis a tile sooner than + // counted). The program goes on believing the table's pose. `far` is not used. + std::vector scene_slides = {}; // The mean brightness (0 to 255) of a map's floor, by JSON map name, and // of the fade into a map; a tap step judges its fade from these with the // program's fade_of(), a floor textured, a fade uniform (SIM_FLOOR_STDDEV, @@ -247,6 +258,7 @@ class SimKnobRouteIO : public GridSimRouteIO{ private: WalkResult slide_on(const SimSlide& slide, WalkResult result); + WalkResult through_warp(const SimSlide& slide, WalkResult result); GridSim& m_grid; SimMisses& m_misses; @@ -548,6 +560,7 @@ class SimExecutor : public Pokemon::TableExecutor{ // Puts the player where SimWorldKnobs::lands says `step` leaves them, if // it says, and counts its coupon or Poketch (count_coupons()). void land(const std::string& step); + void slide_after_scene(const std::string& step); bool cross(); bool cross_to(const Pokemon::Pose& far); std::vector wanderers(const StoryWorld& world) const; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 25156664aa..655b6e0bff 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1484,6 +1484,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_LegPlanner.h Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.cpp Source/Pokemon/Navigation/Pokemon_LegPlanner_Tests.h + Source/Pokemon/Navigation/Pokemon_LegReplay.cpp + Source/Pokemon/Navigation/Pokemon_LegReplay.h Source/Pokemon/Navigation/Pokemon_LegTrace.cpp Source/Pokemon/Navigation/Pokemon_LegTrace.h Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.cpp @@ -1838,6 +1840,9 @@ file(GLOB LIBRARY_SOURCES Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walk_Tests.h Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.cpp Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_Walks.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureData.h + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.cpp + Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_ConsoleFailureReplay_Tests.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FaultSweep_Tests.cpp Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_FaultSweep_Tests.h Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim.cpp diff --git a/devtools/maps/leg_replay.py b/devtools/maps/leg_replay.py new file mode 100644 index 0000000000..61e25fa238 --- /dev/null +++ b/devtools/maps/leg_replay.py @@ -0,0 +1,111 @@ +"""A console failure as a fault file for the simulation (Pokemon_LegReplay.h). + + python3 -m devtools.maps.leg_replay LOG --failed-at 'HH:MM:SS' [--date YYYY-MM-DD] + [--fault JSON]... [--pin 'step: Map x,y'] [--frames DIR] [--build SHA] -o FILE + +Reads the failure the program logged at that time ("[Event] 01.02/11 +into_oreburgh: failed: ..."), the [Leg] records and route lines of the run up +to it, and the failure frame nearest in time, and writes the fault file: the +checkpoint, where it failed, the frame, and the log lines the faults were +worked out from. The faults are the ones given with --fault (the file's own +JSON, see Pokemon_LegReplay.h); the tool derives none the log cannot show by +itself, so it stops (exit 2) and prints the facts it found when none are +given, asking for one pinned position (--pin) taken by hand from the frame. +""" + +import argparse +import glob +import json +import os +import re +import sys +from datetime import datetime + +STAMP = re.compile(r'^(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d)\.\d+ - ') +FAILED = re.compile(r'\[Event\] (\d\d\.\d\d[a-z]?)/(\d+) (\w+): failed: (.*)$') +FACTS = ('[Leg] ', 'someone the map lacks', 'a trainer the map lacks', 'find us again', + 'no fade and no', 'blocked after', 'talked to us') + + +def failure_at(lines, stamp): + """(index, checkpoint, step, error) of the failure logged at `stamp`.""" + for i, line in enumerate(lines): + m = FAILED.search(line) + if m and line.startswith(stamp): + return i, m.group(1), m.group(3), m.group(4).strip() + return None + + +def run_facts(lines, end, checkpoint): + """The facts of the checkpoint's run that ends at line `end`.""" + start = end + while start > 0 and f'[Checkpoint] {checkpoint}: start' not in lines[start]: + start -= 1 + out = [] + for line in lines[start:end + 1]: + if any(tag in line for tag in FACTS) and 'Watcher' not in line: + out.append(STAMP.sub('', line).replace('[Console 0]: ', '').strip()) + return out + + +def nearest_frame(frames, stamp): + """The failure frame closest in time to `stamp` (file names: YYYYMMDD-HHMMSS...).""" + want = datetime.strptime(stamp, '%Y-%m-%d %H:%M:%S') + best = None + for path in glob.glob(os.path.join(frames, '*.*')): + m = re.match(r'(\d{8}-\d{6})', os.path.basename(path)) + if not m: + continue + gap = abs((datetime.strptime(m.group(1), '%Y%m%d-%H%M%S') - want).total_seconds()) + if best is None or gap < best[0]: + best = (gap, os.path.basename(path)) + return best[1] if best and best[0] <= 120 else '' + + +def main(argv=None): + p = argparse.ArgumentParser(description=__doc__.split('\n')[0]) + p.add_argument('log') + p.add_argument('--failed-at', required=True) + p.add_argument('--date', default=datetime.now().strftime('%Y-%m-%d')) + p.add_argument('--fault', action='append', default=[]) + p.add_argument('--pin', help="'step: Map x,y' read from the frame") + p.add_argument('--frames') + p.add_argument('--build', default='') + p.add_argument('-o', '--out') + a = p.parse_args(argv) + stamp = f'{a.date} {a.failed_at}' + with open(a.log, errors='replace') as f: + lines = [l.rstrip('\n') for l in f] + found = failure_at(lines, stamp) + if not found: + print(f'no failure logged at {stamp}', file=sys.stderr) + return 1 + index, checkpoint, step, error = found + facts = run_facts(lines, index, checkpoint) + if not a.fault and not a.pin: + print(f'{checkpoint} failed at {step}: {error}\nthe log shows, not enough to place the player:', file=sys.stderr) + for fact in facts[-30:]: + print(' ' + fact[:200], file=sys.stderr) + print("pin one position from the frame (--pin 'step: Map x,y') or give the faults (--fault).", file=sys.stderr) + return 2 + out = { + 'about': f'lab-B {stamp}, {checkpoint} {step}' + (f', build {a.build}' if a.build else ''), + 'checkpoint': checkpoint, + 'faults': [json.loads(x) for x in a.fault], + 'failed': {'step': step, 'error': error}, + 'frame': nearest_frame(a.frames, stamp) if a.frames else '', + 'evidence': facts[-12:], + } + if a.pin: + out['pin'] = a.pin + text = json.dumps(out, indent=2) + '\n' + if a.out: + with open(a.out, 'w') as f: + f.write(text) + else: + sys.stdout.write(text) + return 0 + + +if __name__ == '__main__': + sys.exit(main())