diff --git a/SerialPrograms/CMakeLists.txt b/SerialPrograms/CMakeLists.txt index b03c3bb1c5..95ce076b24 100644 --- a/SerialPrograms/CMakeLists.txt +++ b/SerialPrograms/CMakeLists.txt @@ -238,6 +238,8 @@ pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/Aut VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryBattleData.h PokemonAutomation::NintendoSwitch::PokemonFRLG autostory_battle_json) pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/StoryData VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryTable.h PokemonAutomation::NintendoSwitch::PokemonFRLG autostory_checkpoint_json) +pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/ConsoleFailures + VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h PokemonAutomation::NintendoSwitch::PokemonFRLG console_failure_json) pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonFRLG/Programs/AutoStory/ReplayData VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_StoryReplay.h PokemonAutomation::NintendoSwitch::PokemonFRLG story_replay_fixture_json) 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/Pokemon/Navigation/Pokemon_LegTrace.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace.cpp new file mode 100644 index 0000000000..8043c6c8f4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace.cpp @@ -0,0 +1,267 @@ +/* Leg Trace + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon_LegTrace.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string_view leg_end_kind_name(LegEndKind kind){ + switch (kind){ + case LegEndKind::OPEN: + return "open"; + case LegEndKind::WALL: + return "wall"; + case LegEndKind::TILE: + return "tile"; + } + return "?"; +} + +std::string_view leg_end_known_name(LegEndKnown known){ + switch (known){ + case LegEndKnown::WALL: + return "wall"; + case LegEndKnown::FADE: + return "fade"; + case LegEndKnown::DIALOGUE: + return "dialogue"; + case LegEndKnown::COUNTED: + return "counted"; + case LegEndKnown::DEAD_RECKONED: + return "dead_reckoned"; + case LegEndKnown::SEEN: + return "seen"; + case LegEndKnown::UNKNOWN: + return "unknown"; + } + return "?"; +} + +std::string_view leg_interrupt_name(LegInterrupt interrupt){ + switch (interrupt){ + case LegInterrupt::WILD_BATTLE: + return "wild_battle"; + case LegInterrupt::TRAINER: + return "trainer"; + case LegInterrupt::SCENE: + return "scene"; + case LegInterrupt::DIALOGUE: + return "dialogue"; + case LegInterrupt::BLOCKED: + return "blocked"; + } + return "?"; +} + + +namespace{ + +// [["Route 202", 23, 25], ...] +JsonArray tiles_json(const std::vector& tiles){ + JsonArray out; + for (const RouteTile& tile : tiles){ + JsonArray entry; + entry.push_back(tile.map); + entry.push_back((int64_t)tile.pos.x); + entry.push_back((int64_t)tile.pos.y); + out.push_back(std::move(entry)); + } + return out; +} + +} // namespace + +std::string leg_trace_json(const LegRecord& record){ + JsonObject out; + out["checkpoint"] = record.checkpoint; + out["event"] = record.event; + out["leg"] = (int64_t)record.leg; + out["dir"] = direction_name(record.plan.direction); + out["tiles"] = (int64_t)record.plan.tiles; + out["end_kind"] = std::string(leg_end_kind_name(record.plan.end_kind)); + out["planned_end"] = tiles_json(record.plan.ends); + if (record.moved){ + out["moved"] = (int64_t)*record.moved; + } + out["chunks"] = (int64_t)record.chunks; + out["ms"] = record.walked_ms; + out["end"] = tiles_json(record.end); + out["known"] = std::string(leg_end_known_name(record.known)); + JsonArray interrupts; + for (LegInterrupt interrupt : record.interrupts){ + interrupts.push_back(std::string(leg_interrupt_name(interrupt))); + } + out["interrupts"] = std::move(interrupts); + if (!record.note.empty()){ + out["note"] = record.note; + } + return out.dump(-1); +} + +std::string leg_trace_line(const LegRecord& record){ + return std::string(LEG_TRACE_TAG) + leg_trace_json(record); +} + + +namespace{ + +std::optional parse_int(std::string_view text){ + int value = 0; + const auto result = std::from_chars(text.data(), text.data() + text.size(), value); + if (result.ec != std::errc() || result.ptr != text.data() + text.size()){ + return std::nullopt; + } + return value; +} + +// "Route 202 (23, 25)". +std::optional parse_route_tile(std::string_view text){ + const size_t open = text.rfind(" ("); + const size_t comma = text.rfind(", "); + if (open == std::string_view::npos || comma == std::string_view::npos || comma < open || !text.ends_with(")")){ + return std::nullopt; + } + const std::optional x = parse_int(text.substr(open + 2, comma - open - 2)); + const std::optional y = parse_int(text.substr(comma + 2, text.size() - comma - 3)); + if (open == 0 || !x || !y){ + return std::nullopt; + } + return RouteTile{std::string(text.substr(0, open)), TilePos{*x, *y}}; +} + +std::optional parse_direction(std::string_view text){ + for (Direction direction : ALL_DIRECTIONS){ + if (text == direction_name(direction)){ + return direction; + } + } + return std::nullopt; +} + +} // namespace + +std::vector parse_route_tiles(std::string_view text){ + std::vector out; + while (!text.empty()){ + // Each tile ends at its ")"; ", " separates them. + const size_t close = text.find(')'); + if (close == std::string_view::npos){ + return {}; + } + const std::optional tile = parse_route_tile(text.substr(0, close + 1)); + if (!tile){ + return {}; + } + out.push_back(*tile); + text.remove_prefix(close + 1); + if (text.starts_with(", ")){ + text.remove_prefix(2); + }else if (!text.empty()){ + return {}; + } + } + return out; +} + +std::optional parse_route_leg_line(std::string_view text){ + // "Leg /: () to " + if (!text.starts_with("Leg ")){ + return std::nullopt; + } + const size_t colon = text.find(": "); + if (colon == std::string_view::npos){ + return std::nullopt; + } + text.remove_prefix(colon + 2); + const size_t space = text.find(' '); + const size_t open = text.find(" ("); + const size_t close = text.find(") to "); + if (space == std::string_view::npos || open == std::string_view::npos || close == std::string_view::npos + || !(space <= open && open < close)){ + return std::nullopt; + } + const std::optional direction = parse_direction(text.substr(0, space)); + const std::optional tiles = parse_int(text.substr(space + 1, open - space - 1)); + const std::string_view end = text.substr(open + 2, close - open - 2); + if (!direction || !tiles || *tiles < 0){ + return std::nullopt; + } + LegPlan plan{.direction = *direction, .tiles = (size_t)*tiles}; + if (end == "to wall"){ + plan.end_kind = LegEndKind::WALL; + }else if (end == "warp"){ + plan.end_kind = LegEndKind::TILE; + }else if (end == "open"){ + plan.end_kind = LegEndKind::OPEN; + }else{ + return std::nullopt; + } + plan.ends = parse_route_tiles(text.substr(close + 5)); + return plan; +} + + +void LegTracer::begin_event(const std::string& checkpoint, const std::string& event){ + end_event(); + m_checkpoint = checkpoint; + m_event = event; + m_legs = 0; +} + +void LegTracer::end_event(){ + if (m_leg){ + end_leg({}, LegEndKnown::UNKNOWN); + } +} + +void LegTracer::begin_leg(LegPlan plan){ + end_event(); + m_leg = LegRecord{.checkpoint = m_checkpoint, .event = m_event, .leg = ++m_legs, .plan = std::move(plan)}; +} + +void LegTracer::walked(const LegWalk& walk){ + if (!m_leg){ + return; + } + m_leg->chunks += walk.chunks; + m_leg->walked_ms += walk.walked_ms; + for (size_t i = 0; i < walk.wild_battles; i++){ + m_leg->interrupts.push_back(LegInterrupt::WILD_BATTLE); + } + if (walk.tapped){ + m_leg->moved = m_leg->moved.value_or(0) + *walk.tapped; + } +} + +void LegTracer::interrupted(LegInterrupt interrupt){ + if (m_leg){ + m_leg->interrupts.push_back(interrupt); + } +} + +void LegTracer::end_leg(std::vector end, LegEndKnown known, std::string note){ + if (!m_leg){ + return; + } + LegRecord record = std::move(*m_leg); + m_leg.reset(); + record.end = std::move(end); + record.known = known; + record.note = std::move(note); + m_emit(leg_trace_line(record)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace.h new file mode 100644 index 0000000000..d177c6e09b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace.h @@ -0,0 +1,167 @@ +/* Leg Trace + * + * From: https://github.com/PokemonAutomation/ + * + * One structured record per walked leg, shared by the games' walkers and + * their simulations, so a console run's route can be compared leg by leg + * with the simulation's (devtools/maps/leg_trace.py). Each leg is one log + * line beside the free-text route lines: + * + * [Leg] {"checkpoint":"00.04","event":"to_the_signs_row","leg":1, + * "dir":"north","tiles":1,"end_kind":"tile","moved":1,...} + * + * A record holds what was planned (direction, tiles, how the leg ends and + * the tiles it may end on), what was measured (taps that moved, the + * walk's moved chunks and held time), where the leg ended and how that is + * known, and what interrupted it. + * +// code-health: allow header_symbols -- leg trace diagnostic models and route execution logging types + */ + +#ifndef PokemonAutomation_Pokemon_LegTrace_H +#define PokemonAutomation_Pokemon_LegTrace_H + +#include +#include +#include +#include +#include +#include +#include +#include "Pokemon_Grid.h" +#include "Pokemon_RouteLog.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Prefix tag of a leg record's log line. +inline constexpr std::string_view LEG_TRACE_TAG = "[Leg] "; + +// How a leg is planned to end. +enum class LegEndKind{ + OPEN, // timed or tapped into the open + WALL, // held until a wall stops it + TILE, // counted taps onto an exact tile +}; +// "open", "wall", "tile". +std::string_view leg_end_kind_name(LegEndKind kind); + +// How the end of a leg is known. +enum class LegEndKnown{ + WALL, // a wall stopped the walk + FADE, // a warp's fade or banner ended it + DIALOGUE, // a dialogue stopped it + COUNTED, // taps that moved, counted against the map + DEAD_RECKONED, // taken to have ended as planned + SEEN, // stopped short, where the walker found itself + UNKNOWN, // not known: the leg was cut off +}; +// "wall", "fade", "dialogue", "counted", "dead_reckoned", "seen", "unknown". +std::string_view leg_end_known_name(LegEndKnown known); + +// What stopped or delayed a leg on the way. +enum class LegInterrupt{ + WILD_BATTLE, + TRAINER, + SCENE, + DIALOGUE, + BLOCKED, // someone the map lacks stood in the way +}; +// "wild_battle", "trainer", "scene", "dialogue", "blocked". +std::string_view leg_interrupt_name(LegInterrupt interrupt); + +// What a leg is planned to do. +struct LegPlan{ + Direction direction = Direction::NORTH; + size_t tiles = 0; + LegEndKind end_kind = LegEndKind::OPEN; + // Every tile the leg may end on; empty where the plan does not say. + std::vector ends; +}; + +// One walked leg. +struct LegRecord{ + std::string checkpoint; + std::string event; + // 1-based, counted through the event, replans included. + size_t leg = 0; + LegPlan plan; + // The taps that moved, where the leg counts them. + std::optional moved; + // What the walks of the leg report: chunks of screen movement, and the + // time the direction was held. + size_t chunks = 0; + int64_t walked_ms = 0; + // The tiles the leg ended on, as the walker knows them. + std::vector end; + LegEndKnown known = LegEndKnown::UNKNOWN; + std::vector interrupts; + // Why the leg ended other than as planned ("replan", "warp"), or "". + std::string note; +}; + +// The record as one line of compact JSON, keys sorted. +std::string leg_trace_json(const LegRecord& record); +// LEG_TRACE_TAG and the record's JSON. +std::string leg_trace_line(const LegRecord& record); + + +// The route log's text read back: "Route 202 (23, 25), Route 202 (30, 26)". +// Nothing for text that is not a list of tiles. +std::vector parse_route_tiles(std::string_view text); +// A route_leg_line() read back: "Leg 1/2: east 15 (to wall) to ". +// A warp leg ends at its tiles, as a tile leg. +std::optional parse_route_leg_line(std::string_view text); + + +// What one walk of a leg reports. +struct LegWalk{ + // Chunks of screen movement, and the time the direction was held. + size_t chunks = 0; + int64_t walked_ms = 0; + size_t wild_battles = 0; + // The taps that moved, for a walk of taps. + std::optional tapped; +}; + +// Builds a walker's leg records from what it does, and hands each, as its +// log line, to `emit` once the leg ends. A leg still open when the next +// starts, or when its event ends, is emitted with its end unknown. +class LegTracer{ +public: + explicit LegTracer(std::function emit) + : m_emit(std::move(emit)) + {} + + // The legs from here on are `event`'s of `checkpoint`. + void begin_event(const std::string& checkpoint, const std::string& event); + // Emits the leg still open, if one is. + void end_event(); + + // A leg starts. + void begin_leg(LegPlan plan); + // The open leg's plan; null if no leg is open. + const LegPlan* open_leg() const{ + return m_leg ? &m_leg->plan : nullptr; + } + // A walk of the open leg ended. A leg can take several (a walk on to + // the wall after a battle); they add up. + void walked(const LegWalk& walk); + void interrupted(LegInterrupt interrupt); + // The open leg ended on `end`, known by `known`; emits it. + void end_leg(std::vector end, LegEndKnown known, std::string note = ""); + +private: + std::function m_emit; + std::string m_checkpoint; + std::string m_event; + size_t m_legs = 0; + std::optional m_leg; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.cpp new file mode 100644 index 0000000000..dc2c889309 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.cpp @@ -0,0 +1,167 @@ +/* Leg Trace Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The leg record's line word for word, the route log's leg lines read + * back, and the tracer's records for a leg that ends, one cut off by the + * next, and one with a wild battle on the way. + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_LegTrace.h" +#include "Pokemon_LegTrace_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +struct LegTraceInput{ + std::string name; + std::function actual; +}; +struct LegTraceExpectation{ + std::string expected; +}; +struct LegTraceTestCase{ + LegTraceInput input; + LegTraceExpectation expectation; +}; + +class Test_LegTrace : public UnitTest{ +public: + explicit Test_LegTrace(LegTraceTestCase scenario) + : UnitTest("Pokemon::LegTrace - " + scenario.input.name) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string actual = m_scenario.input.actual(); + if (actual == m_scenario.expectation.expected){ + return true; + } + return "expected:\n" + m_scenario.expectation.expected + "\ngot:\n" + actual; + } + +private: + LegTraceTestCase m_scenario; +}; + +std::string tiles_text(const std::vector& tiles){ + return tiles.empty() ? "none" : describe(tiles); +} + +std::string plan_text(const std::optional& plan){ + if (!plan){ + return "none"; + } + return std::string(direction_name(plan->direction)) + " " + std::to_string(plan->tiles) + " " + + std::string(leg_end_kind_name(plan->end_kind)) + " -> " + tiles_text(plan->ends); +} + +// The lines a tracer emits for `script`, one a line. +std::string traced(const std::function& script){ + std::string out; + LegTracer tracer([&](const std::string& line){ out += line + "\n"; }); + script(tracer); + return out; +} + +const LegTraceTestCase CASES[] = { + { + .input = {.name = "a tap leg's line", .actual = []{ + LegRecord record{ + .checkpoint = "00.04", .event = "to_the_signs_row", .leg = 1, + .plan = {.direction = Direction::NORTH, .tiles = 1, .end_kind = LegEndKind::TILE, + .ends = {{"Route 201", {57, 4}}}}, + .moved = 1, .chunks = 1, .walked_ms = 800, + .end = {{"Route 201", {57, 4}}}, .known = LegEndKnown::COUNTED, + }; + return leg_trace_line(record); + }}, + .expectation = {.expected = + "[Leg] {\"checkpoint\":\"00.04\",\"chunks\":1,\"dir\":\"north\",\"end\":[[\"Route 201\",57,4]]," + "\"end_kind\":\"tile\",\"event\":\"to_the_signs_row\",\"interrupts\":[],\"known\":\"counted\"," + "\"leg\":1,\"moved\":1,\"ms\":800,\"planned_end\":[[\"Route 201\",57,4]],\"tiles\":1}"}, + }, + { + .input = {.name = "tiles read back", .actual = []{ + return tiles_text(parse_route_tiles("Route 202 (23, 25), Mom's House (30, -1)")); + }}, + .expectation = {.expected = "Route 202 (23, 25), Mom's House (30, -1)"}, + }, + { + .input = {.name = "no tiles in other text", .actual = []{ + return tiles_text(parse_route_tiles("an unknown tile")); + }}, + .expectation = {.expected = "none"}, + }, + { + .input = {.name = "a wall leg line read back", .actual = []{ + return plan_text(parse_route_leg_line("Leg 1/2: east 15 (to wall) to Route 202 (23, 25), Route 202 (30, 26)")); + }}, + .expectation = {.expected = "east 15 wall -> Route 202 (23, 25), Route 202 (30, 26)"}, + }, + { + .input = {.name = "an open leg line read back", .actual = []{ + return plan_text(parse_route_leg_line("Leg 3/3: west 1 (open) to Twinleaf Town (7, 9)")); + }}, + .expectation = {.expected = "west 1 open -> Twinleaf Town (7, 9)"}, + }, + { + .input = {.name = "not a leg line", .actual = []{ + return plan_text(parse_route_leg_line("[Route] Turning west.")); + }}, + .expectation = {.expected = "none"}, + }, + { + .input = {.name = "a leg cut off by the next, and by its event's end", .actual = []{ + return traced([](LegTracer& tracer){ + tracer.begin_event("00.04", "beside_mom"); + tracer.begin_leg({.direction = Direction::WEST, .tiles = 2, .end_kind = LegEndKind::OPEN}); + tracer.begin_leg({.direction = Direction::NORTH, .tiles = 1, .end_kind = LegEndKind::WALL}); + tracer.walked({.chunks = 3, .walked_ms = 640}); + tracer.begin_event("00.04", "mom"); + }); + }}, + .expectation = {.expected = + "[Leg] {\"checkpoint\":\"00.04\",\"chunks\":0,\"dir\":\"west\",\"end\":[],\"end_kind\":\"open\"," + "\"event\":\"beside_mom\",\"interrupts\":[],\"known\":\"unknown\",\"leg\":1,\"ms\":0,\"planned_end\":[],\"tiles\":2}\n" + "[Leg] {\"checkpoint\":\"00.04\",\"chunks\":3,\"dir\":\"north\",\"end\":[],\"end_kind\":\"wall\"," + "\"event\":\"beside_mom\",\"interrupts\":[],\"known\":\"unknown\",\"leg\":2,\"ms\":640,\"planned_end\":[],\"tiles\":1}\n"}, + }, + { + .input = {.name = "a wild battle on the way, two walks to the wall", .actual = []{ + return traced([](LegTracer& tracer){ + tracer.begin_event("00.04b", "to_catch_grass"); + tracer.begin_leg({.direction = Direction::SOUTH, .tiles = 4, .end_kind = LegEndKind::WALL}); + tracer.walked({.chunks = 2, .walked_ms = 500, .wild_battles = 1}); + tracer.walked({.chunks = 2, .walked_ms = 400}); + tracer.end_leg({{"Route 202", {20, 27}}}, LegEndKnown::WALL); + tracer.end_event(); + }); + }}, + .expectation = {.expected = + "[Leg] {\"checkpoint\":\"00.04b\",\"chunks\":4,\"dir\":\"south\",\"end\":[[\"Route 202\",20,27]],\"end_kind\":\"wall\"," + "\"event\":\"to_catch_grass\",\"interrupts\":[\"wild_battle\"],\"known\":\"wall\",\"leg\":1,\"ms\":900,\"planned_end\":[],\"tiles\":4}\n"}, + }, +}; + +} // namespace + + +void add_tests_LegTrace(UnitTestDatabase& database){ + for (const auto& scenario : CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.h new file mode 100644 index 0000000000..a81561fa7f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.h @@ -0,0 +1,23 @@ +/* Leg Trace Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_LegTrace_Tests_H +#define PokemonAutomation_Pokemon_LegTrace_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers leg trace record and tracer unit tests. +void add_tests_LegTrace(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp index 27594906ca..51025bc98e 100644 --- a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_RouteLog_Tests.cpp @@ -13,6 +13,7 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_LegTrace_Tests.h" #include "Pokemon_RouteLog.h" #include "Pokemon_RouteLog_Tests.h" @@ -205,6 +206,8 @@ void add_tests_RouteLog(UnitTestDatabase& database){ for (const auto& scenario : CASES){ database.add(scenario); } + // The leg records read the route lines back. + add_tests_LegTrace(database); } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.cpp new file mode 100644 index 0000000000..701010a036 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.cpp @@ -0,0 +1,80 @@ +/* Overworld Leg Trace + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "PokemonFRLG_OverworldLegTrace.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +using Pokemon::LegEndKind; +using Pokemon::LegEndKnown; +using Pokemon::LegInterrupt; + + +namespace{ + +Pokemon::RouteTile route_tile(const MapPosition& position){ + return {position.map, position.point}; +} + +Pokemon::LegPlan plan_of(const RouteStep& step){ + Pokemon::LegPlan plan{.direction = step.direction, .tiles = (size_t)step.tiles, .end_kind = LegEndKind::OPEN, .ends = {route_tile(step.end)}}; + if (step.action == RouteAction::WALK && step.warps){ + plan.end_kind = LegEndKind::TILE; + }else if (step.action == RouteAction::WALK && step.to_wall){ + plan.end_kind = LegEndKind::WALL; + } + return plan; +} + +} // namespace + + +OverworldLegTrace::OverworldLegTrace( + const std::string& checkpoint, const std::string& event, + std::function log +) + : m_tracer(std::move(log)) +{ + m_tracer.begin_event(checkpoint, event); +} + +OverworldLegTrace::~OverworldLegTrace(){ + m_tracer.end_event(); +} + +void OverworldLegTrace::begin(const RouteStep& step){ + m_tracer.begin_leg(plan_of(step)); +} + +void OverworldLegTrace::done(const RouteStep& step){ + const bool warped = step.action == RouteAction::WALK && step.warps; + m_tracer.end_leg({route_tile(step.end)}, warped ? LegEndKnown::FADE : LegEndKnown::DEAD_RECKONED); +} + +void OverworldLegTrace::trainer_stopped(const MapPosition& at){ + m_tracer.interrupted(LegInterrupt::TRAINER); + m_tracer.end_leg({route_tile(at)}, LegEndKnown::SEEN, "replan"); +} + +void OverworldLegTrace::unknown(const std::string& note){ + m_tracer.end_leg({}, LegEndKnown::UNKNOWN, note); +} + +void OverworldLegTrace::held_to_warp(const RouteStep& step){ + m_tracer.end_leg({route_tile(step.end)}, LegEndKnown::FADE, "warp"); +} + +void OverworldLegTrace::battle_before(){ + m_tracer.interrupted(LegInterrupt::WILD_BATTLE); +} + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.h new file mode 100644 index 0000000000..872995bbdc --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.h @@ -0,0 +1,78 @@ +/* Overworld Leg Trace + * + * From: https://github.com/PokemonAutomation/ + * + * One [Leg] record (Pokemon_LegTrace.h) per RouteStep that run_route() + * walks, for devtools/maps/leg_trace.py and the replay of console failures. + * A leg starts where the route log says "Leg n/m" and ends where it says + * how it ended; the record says how that end is known: + * + * FADE a warp's fade was seen + * DEAD_RECKONED taken to have ended as planned. FRLG's walker does not + * measure whether the screen moved: a hold to a wall + * that never moved the player ends at its planned tile + * just the same (console B, 2026-11-12, 12.03 + * pallet_shore: two holds east from (6, 10) moved 0 + * tiles, and were logged "ended at (10, 10)" and "found + * again at (12, 10)") + * SEEN where the map puts a trainer who stopped the player + * UNKNOWN the end is not known (stopped part-way, a hold from + * an unknown tile) + * + * The taps that moved (LegRecord::moved) are left out: the walker holds + * legs and counts nothing. + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_OverworldLegTrace_H +#define PokemonAutomation_PokemonFRLG_OverworldLegTrace_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_LegTrace.h" +#include "PokemonFRLG_OverworldRouteSteps.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// The leg records of one run_route() (one event of one checkpoint), handed +// to `log` as lines. A leg still open when the trace is destroyed is +// written with its end unknown. +class OverworldLegTrace{ +public: + OverworldLegTrace( + const std::string& checkpoint, const std::string& event, + std::function log + ); + ~OverworldLegTrace(); + OverworldLegTrace(const OverworldLegTrace&) = delete; + void operator=(const OverworldLegTrace&) = delete; + + // `step` starts; the leg before it, if still open, ends unknown. + void begin(const RouteStep& step); + // `step` ended as planned: on its warp's fade if it warps, else dead + // reckoned. + void done(const RouteStep& step); + // The open leg ended on `at` as the map places a trainer who stopped + // the player there. + void trainer_stopped(const MapPosition& at); + // The open leg's end is not known; `note` says why ("replan": the + // route is planned again from where the player may be). + void unknown(const std::string& note); + // The open leg's hold went on to its warp, whose fade was seen. + void held_to_warp(const RouteStep& step); + // A wild battle set off before the open leg started. + void battle_before(); + +private: + Pokemon::LegTracer m_tracer; +}; + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp index 2c892018fe..f4d8ca241d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp @@ -7,6 +7,7 @@ #include #include #include "Pokemon/Navigation/Pokemon_RouteLog.h" +#include "PokemonFRLG_OverworldLegTrace.h" #include "PokemonFRLG_OverworldReanchor.h" #include "PokemonFRLG_OverworldRecovery.h" @@ -99,12 +100,17 @@ int anchor_tiles(const Reanchor& anchor){ } // The route's lines in the route log the games share, as BDSP's walker -// writes them, under the caller's label. +// writes them, under the caller's label; and one [Leg] record per leg +// (PokemonFRLG_OverworldLegTrace.h), written to the same log. class RouteLogLines{ public: RouteLogLines(const RouteCalls& calls, const RoutePlanning* planning) : m_calls(calls) , m_label(planning != nullptr ? planning->label : "Route") + , m_trace( + planning != nullptr ? planning->checkpoint : "", m_label, + [&calls](const std::string& line){ calls.log(line); } + ) {} void plan(const std::optional& from, const std::vector& steps) const{ std::vector legs; @@ -113,36 +119,56 @@ class RouteLogLines{ } m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_plan_line(m_label, route_tiles(from), legs)); } - void leg(size_t index, const std::vector& steps) const{ + void leg(size_t index, const std::vector& steps){ const RouteStep& step = steps[index]; m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_leg_line(index + 1, steps.size(), {.leg = route_log_leg(step), .after = route_tiles(step.end)})); + m_trace.begin(step); } - void end(const RouteStep& step, const std::optional& at, EndBasis basis, const std::string& why) const{ - m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_leg_end_line(m_label, route_log_leg(step), route_tiles(at), basis, why)); + // The leg ended other than as planned and walked: its hold went on to + // the warp (ASSUMED), or its end is not known (UNKNOWN). + void end(const RouteStep& step, const std::optional& at, EndBasis basis, const std::string& why){ + log_end(step, at, basis, why); + if (basis == EndBasis::ASSUMED){ + m_trace.held_to_warp(step); + }else{ + m_trace.unknown(why == "stopped part-way" ? "replan" : "again"); + } } - void trainer_stopped(const RouteStep& step, const MapPosition& at) const{ + void trainer_stopped(const RouteStep& step, const MapPosition& at){ m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_replan_line(m_label, route_log_leg(step), "stopped by a trainer who saw the player", route_tiles(at))); + m_trace.trainer_stopped(at); + } + // A wild battle set off before the leg that just started. + void battle_before(){ + m_trace.battle_before(); } void found(const MapPosition& at, size_t holds) const{ m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_found_line(m_label, route_tiles(at), holds)); } // Step `number` (from 1) of a plan whose first `holds` steps are holds // to walls, walked to its end. - void done(const RouteStep& step, size_t number, size_t holds) const{ + void done(const RouteStep& step, size_t number, size_t holds){ if (number < holds){ // Where a hold but the last ends depends on where it started. - end(step, std::nullopt, EndBasis::UNKNOWN, "a hold from an unknown tile"); + log_end(step, std::nullopt, EndBasis::UNKNOWN, "a hold from an unknown tile"); + m_trace.unknown("hold"); return; } - end(step, step.end, EndBasis::WALKED, ""); + log_end(step, step.end, EndBasis::WALKED, ""); + m_trace.done(step); if (number == holds){ found(step.end, holds); } } private: + void log_end(const RouteStep& step, const std::optional& at, EndBasis basis, const std::string& why) const{ + m_calls.log(std::string(ROUTE_LOG_TAG) + Pokemon::route_leg_end_line(m_label, route_log_leg(step), route_tiles(at), basis, why)); + } + const RouteCalls& m_calls; std::string m_label; + OverworldLegTrace m_trace; }; // Walks each step through `calls`; watches a leg that crosses a trainer's @@ -214,6 +240,7 @@ class RouteRun{ , m_plan(with_warp_slack(route)) , m_at(planning != nullptr ? planning->start : std::nullopt) , m_facing(facing) + , m_known(m_at.has_value()) {} void run(){ @@ -223,10 +250,11 @@ class RouteRun{ m_lines.leg(m_next, m_plan); // Where a hold that finds the player starts isn't known. const std::optional crossing = m_next >= m_holds ? crossing_here(step) : std::nullopt; - const StepOutcome outcome = m_steps.walk(step, m_facing, crossing); + const StepOutcome outcome = judge_movement(step, m_steps.walk(step, m_facing, crossing)); if (outcome.end == StepEnd::DONE){ const std::optional start = m_next >= m_unchecked ? m_at : std::nullopt; remember_walked(step); + m_known = m_known || step.to_wall || step.warps; m_at = step.end; m_facing = step.facing_after; m_lines.done(step, ++m_next, m_holds); @@ -290,6 +318,7 @@ class RouteRun{ // With nothing walked since the plan was made, the player is where // `step` starts. False once the route has failed. bool after_battle_before(const RouteStep& step){ + m_lines.battle_before(); if (!m_walked){ m_calls.log("Route: a wild battle before " + to_string(step) + ", with nothing walked since the plan: walking it from its start."); return true; @@ -302,12 +331,81 @@ class RouteRun{ return after_stop(walked.step, StepOutcome{.end = StepEnd::LATE, .walked = std::nullopt}, walked.start, std::nullopt); } + // How many tiles the plan has `step`, a hold or an open leg, walk, at + // the least: its tiles from a tile the player is known to be on, and + // for the first hold that finds the player, the fewest from any tile + // they may be on. 0 where that isn't known (a route the caller gave + // with no start, whose first holds are from tiles it doesn't name), or + // if one of those tiles stands at the wall already (a bump right away + // moves nothing). + int expected_tiles(const RouteStep& step) const{ + if (m_holds > 0 ? m_next >= m_holds : m_known){ + return step.tiles; + } + if (m_next != 0 || m_hold_from.empty() || maps() == nullptr){ + return 0; + } + int least = step.tiles; + for (const MapPosition& from : m_hold_from){ + const std::optional hold = hold_to_wall(*maps(), from, false, step.direction); + least = std::min(least, hold ? hold->tiles : 0); + } + return least; + } + + // A walk that ended DONE with the screen never having moved did not + // walk the tiles the plan expected: it is STOPPED at its start, never + // taken as walked or as at its wall (console B, 2026-11-12, 12.03 + // pallet_shore: two holds east moved 0 tiles and were logged as + // ended). A hold that only bumped a wall it stood at moves nothing + // too, which expected_tiles() tells apart. + StepOutcome judge_movement(const RouteStep& step, StepOutcome outcome){ + if (outcome.end != StepEnd::DONE || step.action != RouteAction::WALK || step.warps){ + return outcome; + } + if (outcome.movement != HoldMovement::NONE){ + m_unmoved.clear(); + return outcome; + } + const int expected = expected_tiles(step); + if (expected < 1){ + m_calls.log("Route: " + to_string(step) + " moved the screen not at all, as a bump into the wall it may stand at already; taken as walked."); + return outcome; + } + m_calls.log(std::format( + "Route: {} moved the screen not at all, though the plan has it walk at least {} tiles; the player never moved.", + to_string(step), expected + )); + return StepOutcome{.end = StepEnd::STOPPED, .walked = WalkedTiles{.min = 0, .max = 0}, .movement = HoldMovement::NONE}; + } + + // After `step` moved nothing, from `start` if known: walked again + // from where it started, once. False once the route has failed. + bool after_unmoved(const RouteStep& step, const std::optional& start){ + const std::string leg = to_string(step); + const bool again = m_unmoved == leg; + m_unmoved = leg; + m_lines.end(step, std::nullopt, EndBasis::UNKNOWN, "stopped part-way"); + if (again){ + m_calls.fail("walk_overworld_route(): " + leg + " moved nothing twice in a row: the player is not walking."); + return false; + } + if (!start || maps() == nullptr){ + m_calls.fail("walk_overworld_route(): " + leg + " moved nothing, and the tile it was held from isn't known to walk it again from."); + return false; + } + return rejoin(step, {StopCandidate{*start, std::nullopt}}, WalkedTiles{.min = 0, .max = 0}); + } + // After a stop on `step`, from `start` if known. False once the route // has failed. bool after_stop( const RouteStep& step, const StepOutcome& outcome, const std::optional& start, const std::optional& crossing ){ + if (outcome.end == StepEnd::STOPPED && outcome.movement == HoldMovement::NONE){ + return after_unmoved(step, start); + } if (step.to_wall){ return after_wall_stop(step, outcome, start, crossing); } @@ -384,15 +482,19 @@ class RouteRun{ "Route: position check: {} may have ended up to {} tiles short with nothing seen; finding the player at walls.", to_string(step), OPEN_LEG_SHORTFALL )); - replan(*anchor, m_next); + replan(*anchor, m_next, candidates); m_at = std::nullopt; m_lines.plan(std::nullopt, m_plan); } // The plan from here: `anchor`'s holds and way back, then the old // plan's steps from `rest` on. Neither the holds nor the way back are - // checked as open legs again. - void replan(const Reanchor& anchor, size_t rest){ + // checked as open legs again. The first hold starts from a tile of `from`. + void replan(const Reanchor& anchor, size_t rest, const std::vector& from){ + m_hold_from.clear(); + for (const StopCandidate& candidate : from){ + m_hold_from.push_back(candidate.player); + } std::vector plan = anchor.holds; plan.insert(plan.end(), anchor.back.begin(), anchor.back.end()); plan.insert(plan.end(), m_plan.begin() + rest, m_plan.end()); @@ -424,7 +526,7 @@ class RouteRun{ ); return false; } - replan(*anchor, m_next + 1); + replan(*anchor, m_next + 1, candidates); if (m_holds > 0){ m_at = std::nullopt; m_lines.plan(std::nullopt, m_plan); @@ -439,6 +541,7 @@ class RouteRun{ m_lines.found(anchor->found, 0); } m_at = anchor->found; + m_known = true; m_facing = step.direction; m_lines.plan(m_at, m_plan); return true; @@ -446,16 +549,21 @@ class RouteRun{ const RoutePlanning* m_planning; const RouteCalls& m_calls; - const RouteLogLines m_lines; + RouteLogLines m_lines; CheckedSteps m_steps; std::vector m_plan; std::optional m_at; std::optional m_facing; + // Whether the player's tile is known when a plan without holds starts: + // from the caller's start, or since a wall or a warp. + bool m_known = false; size_t m_next = 0; size_t m_stops = 0; size_t m_holds = 0; // of the plan, holds to walls that find the player size_t m_unchecked = 0; // of the plan, the holds and the way back after them std::optional m_walked; // the step walked last, if it may have stopped part-way + std::vector m_hold_from; // the tiles the first hold of the plan may start from + std::string m_unmoved; // the leg that moved nothing last, until another walks }; } // namespace diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h index ab3ed66e24..1a7f5d16cd 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h @@ -60,6 +60,8 @@ struct RoutePlanning{ RouteCosts costs = default_route_costs(); std::optional start; std::string label = "Route"; + // The checkpoint the route belongs to, for its [Leg] records. + std::string checkpoint; WallCheck wall_check = WallCheck::TAP; OpenLegCheck open_leg_check = OpenLegCheck::REANCHOR; }; diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp index f3e58ab5f6..244d04290e 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -50,47 +51,80 @@ Pokemon::LegSpec leg_spec(const RouteStep& step, MoveSpeed speed){ struct StepWalk{ std::optional stopped; WallClock hold_start; + HoldMovement movement = HoldMovement::UNMEASURED; }; +// Whether the map scrolled between the shot before a hold and now, once +// the hold's first tile has had its time (`since_start`: how long ago the +// hold started; a one-tile open leg is let go half-way through its step). +HoldMovement measure_movement( + ConsoleHandle& console, ProControllerContext& context, + const VideoSnapshot& before, Milliseconds first_tile, WallClock hold_start +){ + const Milliseconds since_start = std::chrono::duration_cast(current_time() - hold_start); + if (since_start < first_tile + TURN_SETTLE){ + context.wait_for(first_tile + TURN_SETTLE - since_start); + } + context.wait_for_all_requests(); + const VideoSnapshot after = console.video().snapshot(); + if (!before || !after){ + return HoldMovement::UNMEASURED; + } + const double change = hold_scroll_change(*before.frame, *after.frame, GameSettings::instance().GAME_BOX); + console.log(std::format("Position check: the screen changed by {:.1f} over the hold.", change)); + return change > HOLD_UNMOVED_RMSD ? HoldMovement::MOVED : HoldMovement::NONE; +} + +// A WALK step, for a player facing `facing`: its hold, and for a hold not +// onto a warp whether the screen moved during it. +StepWalk walk_hold( + ConsoleHandle& console, ProControllerContext& context, + const RouteStep& step, std::optional facing, + const OverworldInterruptSet& interrupts, MoveSpeed speed +){ + StepWalk out{std::nullopt, current_time()}; + // Turning first, with a tap, makes the hold start from a standstill + // facing the right way: holding through a turn lost a tile on the + // console, while holds from a standstill were exact over 15 tiles. + if (facing != step.direction){ + pbf_press_dpad(context, dpad(step.direction), TURN_TAP, TURN_SETTLE); + } + if (const std::string note = speed_note(step, speed); !note.empty()){ + console.log(note); + } + context.wait_for_all_requests(); + const VideoSnapshot before = step.warps ? VideoSnapshot() : console.video().snapshot(); + out.hold_start = current_time(); + const Pokemon::LegResult result = Pokemon::walk_leg( + console, context, leg_mover(step, speed), step.wild ? interrupts.wild : interrupts.other, + leg_spec(step, speed), MAX_INTERRUPTIONS_PER_LEG + ); + if (result.stopped){ + out.stopped = walked_near(step, result.tiles, tile_time(step, speed)); + return out; + } + if (!step.warps){ + out.movement = measure_movement(console, context, before, tile_time(step, speed), out.hold_start); + return out; + } + wait_out_warp(console, context, step); + static_assert(AFTER_WARP_BATTLE_WATCH >= AFTER_WARP); + if (run_from_battle_after_warp(console, context)){ + console.log("Route: a wild battle, not the warp to " + step.end.map + "; the player is on " + to_string(step) + " before the warp.", COLOR_ORANGE); + out.stopped = walked_before_warp(step); + } + return out; +} + // One step of a route, for a player facing `facing`. StepWalk walk_step( ConsoleHandle& console, ProControllerContext& context, const RouteStep& step, std::optional facing, const OverworldInterruptSet& interrupts, MoveSpeed speed ){ - StepWalk out{std::nullopt, current_time()}; switch (step.action){ - case RouteAction::WALK:{ - // Turning first, with a tap, makes the hold start from a standstill - // facing the right way: holding through a turn lost a tile on the - // console, while holds from a standstill were exact over 15 tiles. - if (facing != step.direction){ - pbf_press_dpad(context, dpad(step.direction), TURN_TAP, TURN_SETTLE); - } - if (const std::string note = speed_note(step, speed); !note.empty()){ - console.log(note); - } - context.wait_for_all_requests(); - out.hold_start = current_time(); - const Pokemon::LegResult result = Pokemon::walk_leg( - console, context, leg_mover(step, speed), step.wild ? interrupts.wild : interrupts.other, - leg_spec(step, speed), MAX_INTERRUPTIONS_PER_LEG - ); - if (result.stopped){ - out.stopped = walked_near(step, result.tiles, tile_time(step, speed)); - return out; - } - if (step.warps){ - wait_out_warp(console, context, step); - static_assert(AFTER_WARP_BATTLE_WATCH >= AFTER_WARP); - if (run_from_battle_after_warp(console, context)){ - console.log("Route: a wild battle, not the warp to " + step.end.map + "; the player is on " + to_string(step) + " before the warp.", COLOR_ORANGE); - out.stopped = walked_before_warp(step); - return out; - } - } - break; - } + case RouteAction::WALK: + return walk_hold(console, context, step, facing, interrupts, speed); case RouteAction::SURF: start_surf(console, context, step.direction, facing == step.direction); break; @@ -98,7 +132,7 @@ StepWalk walk_step( pbf_press_dpad(context, dpad(step.direction), FACE_HOLD, STEP_RELEASE); break; } - return out; + return StepWalk{std::nullopt, current_time()}; } // An open leg's screens, recorded with the interrupts' observer from @@ -172,6 +206,7 @@ StepOutcome walk_and_watch( if (step.action == RouteAction::FACE){ return StepOutcome{}; } + const HoldMovement movement = walked.movement; const Milliseconds watch = step.wild ? AFTER_WILD_LEG_WATCH : AFTER_LEG_WATCH; const OverworldInterrupts& watched = step.wild ? interrupts.wild : interrupts.other; size_t late = Pokemon::clear_late_interrupts(console, context, watched, watch, MAX_INTERRUPTIONS_PER_LEG); @@ -181,7 +216,7 @@ StepOutcome walk_and_watch( // After a warp it was checked for already. const bool done = late == 0 || step.warps || step.action != RouteAction::WALK; if (done){ - return StepOutcome{}; + return StepOutcome{.end = StepEnd::DONE, .walked = std::nullopt, .movement = movement}; } return StepOutcome{.end = StepEnd::LATE, .walked = recorder.place(console, step, tile_time(step, speed), walked.hold_start)}; } @@ -284,6 +319,18 @@ bool moved_past_wall(ConsoleHandle& console, ProControllerContext& context, cons } // namespace +double hold_scroll_change(const ImageViewRGB32& before, const ImageViewRGB32& after, const ImageFloatBox& game){ + // The top and bottom quarters, clear of the player in the middle. A + // scroll moves both; a town's banner (top) or water (either) only one. + const double h = game.height * 0.25; + const ImageFloatBox top(game.x, game.y, game.width, h); + const ImageFloatBox bottom(game.x, game.y + game.height - h, game.width, h); + return std::min( + ImageMatch::pixel_RMSD(extract_box_reference(before, top), extract_box_reference(after, top)), + ImageMatch::pixel_RMSD(extract_box_reference(before, bottom), extract_box_reference(after, bottom)) + ); +} + void walk_overworld_route_with_recovery( ConsoleHandle& console, ProControllerContext& context, const std::vector& route, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h index 51cd54ac1a..ee78bf2e19 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.h @@ -19,6 +19,7 @@ #include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h" #include "PokemonFRLG_OverworldRecovery.h" #include "PokemonFRLG_OverworldRoute.h" +#include "CommonFramework/ImageTools/ImageBoxes.h" #include "PokemonFRLG_OverworldWalk.h" namespace PokemonAutomation{ @@ -26,6 +27,19 @@ namespace NintendoSwitch{ namespace PokemonFRLG{ +// How far the map in the game screen (`game`) moved between the shots +// before and after a hold, as an RMSD: a hold that walked even one tile +// scrolls the whole map by it, while a town's name banner sliding in over +// the top of the screen (Pallet Town after a Fly, console B, 2026-11-13 +// 02:46) is not a move. +double hold_scroll_change(const ImageViewRGB32& before, const ImageViewRGB32& after, const ImageFloatBox& game); + +// How much hold_scroll_change() may read for a player who stood still (an +// animated tile, water). Not yet measured on the console: a guess, set low +// so that only a screen that did not move at all reads as not moved. +constexpr double HOLD_UNMOVED_RMSD = 6.0; + + // Walks `route`, planned with `planning`, at `speed`, as // walk_overworld_route() does, from the same start: standing still on the // overworld, not on the bike, facing `facing` (read from the screen if not diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp index 3b9ba87f71..cbb9c48891 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery_Tests.cpp @@ -17,9 +17,11 @@ #include #include "Common/Cpp/Exceptions.h" #include "Common/Cpp/TestRunners/UnitTest.h" +#include "CommonFramework/ImageTypes/ImageRGB32.h" #include "PokemonFRLG_OverworldMaps.h" #include "PokemonFRLG_OverworldReanchor.h" #include "PokemonFRLG_OverworldRecovery.h" +#include "PokemonFRLG_OverworldRecoveryWalk.h" namespace PokemonAutomation{ namespace NintendoSwitch{ @@ -308,6 +310,133 @@ UnitTestResult test_wall_check(){ return true; } +// A hall to walk: floor from x=1 to x=7, nothing in it. +const std::string TEST_HALL_JSON = R"({ + "name": "Hall", "source": {"decomp_file": "", "revision": "test", "date": "2026-11-12"}, + "tiles": ["#########", "#.......#", "#########"], + "elevation": ["333333333", "333333333", "333333333"], + "objects": [] +})"; + +StepOutcome no_movement(){ + return StepOutcome{.end = StepEnd::DONE, .walked = std::nullopt, .movement = HoldMovement::NONE}; +} + +// A leg held from a known tile that moved the screen not at all did not +// walk (console B, 2026-11-12, 12.03 pallet_shore): it is not taken as +// walked, whether open or to a wall, but walked again from where it +// started; once more with nothing moved, the route fails. A leg that +// moved is walked as before. +// A map of 64-pixel tiles, each its own color, seen with the camera +// `scroll` pixels down; `banner` paints a town's name banner over the top +// left, as the game shows on entering a town. +ImageRGB32 tile_map(size_t scroll, bool banner){ + ImageRGB32 image(960, 640); + for (size_t y = 0; y < image.height(); y++){ + for (size_t x = 0; x < image.width(); x++){ + const uint32_t tile = (uint32_t)(x / 64 * 7 + (y + scroll) / 64 * 13); + const bool in_banner = banner && x < 400 && y < 80; + image.pixel(x, y) = in_banner ? 0xfff8f8f8 : 0xff000000 | (tile * 0x2f1b3d & 0x00ffffff); + } + } + return image; +} + +// Pallet Town after a Fly (console B, 2026-11-13 02:42 and 02:46, 12.03): +// the first hold walked nothing while the town's banner slid in over the +// top of the screen, and was taken as walked ("changed by 223.0"); every +// later tile was then 2 rows off. A banner is not a scroll; a step is. +UnitTestResult test_banner_is_no_scroll(){ + const ImageFloatBox game(0, 0, 1, 1); + const double banner = hold_scroll_change(tile_map(0, false), tile_map(0, true), game); + const double step = hold_scroll_change(tile_map(0, false), tile_map(64, false), game); + if (banner > HOLD_UNMOVED_RMSD){ + return std::format("a banner over a still map changed it by {:.1f}, over {:.1f}", banner, HOLD_UNMOVED_RMSD); + } + if (step <= HOLD_UNMOVED_RMSD){ + return std::format("a one-tile step changed the map by {:.1f}, not over {:.1f}", step, HOLD_UNMOVED_RMSD); + } + return true; +} + +UnitTestResult test_unmoved_leg(){ + const std::vector maps{parse_map(TEST_HALL_JSON, "Hall")}; + RouteStep wall = walk(Direction::EAST, 5, {"Hall", {7, 1}}); + wall.to_wall = true; + const std::vector legs[] = { + {walk(Direction::EAST, 3, {"Hall", {5, 1}})}, + {wall}, + }; + const RoutePlanning planning{.maps = &maps, .costs = default_route_costs(), .start = MapPosition{"Hall", {2, 1}}, .label = "Route", .wall_check = WallCheck::SKIP}; + for (const std::vector& route : legs){ + const std::string name = to_string(route[0]); + ScriptedRoute once; + once.ends[0] = no_movement(); + if (once.run(route, &planning) || once.walked.size() != 2){ + return name + ": expected the leg walked a second time after it moved nothing: " + once.failure; + } + const UnitTestResult logged = expect_logged(once.logs, {"the player never moved", "stopped 0 to 0 tiles in"}); + if (logged.result != UnitTestResult::PASSED){ + return name + ": " + logged.message; + } + ScriptedRoute twice; + twice.ends[0] = no_movement(); + twice.ends[1] = no_movement(); + if (!twice.run(route, &planning) || twice.failure.find("moved nothing twice in a row") == std::string::npos){ + return name + ": expected a failure after two holds that moved nothing, got: " + twice.failure; + } + ScriptedRoute moved; + moved.ends[0] = StepOutcome{.end = StepEnd::DONE, .walked = std::nullopt, .movement = HoldMovement::MOVED}; + if (moved.run(route, &planning) || moved.walked.size() != 1){ + return name + ": a leg that moved is walked once: " + moved.failure; + } + ScriptedRoute unmeasured; + if (unmeasured.run(route, &planning) || unmeasured.walked.size() != 1){ + return name + ": a leg not measured is walked once: " + unmeasured.failure; + } + } + return true; +} + +// A hold that finds the player starts from one of a few tiles. If one of +// them stands at its wall, a hold that moved nothing may be a bump right +// away and is taken as walked; if none does, it never moved, and as the +// tile it was held from isn't known, the route fails. +UnitTestResult test_unmoved_hold_from_unknown_tile(){ + const std::vector maps{parse_map(TEST_HALL_JSON, "Hall")}; + const RouteStep open = walk(Direction::EAST, 5, {"Hall", {7, 1}}); + const RoutePlanning planning{.maps = &maps, .costs = default_route_costs(), .start = MapPosition{"Hall", {2, 1}}, .label = "Route", .wall_check = WallCheck::SKIP}; + struct Case{ + std::string_view name; + WalkedTiles stopped; + bool bump; + }; + // Walked 4 to 5 tiles stops on (6, 1) or (7, 1), the wall's tile; 2 to + // 3 on (4, 1) or (5, 1). + const Case cases[] = { + {.name = "one tile at the wall", .stopped = WalkedTiles{.min = 4, .max = 5}, .bump = true}, + {.name = "none at the wall", .stopped = WalkedTiles{.min = 2, .max = 3}, .bump = false}, + }; + for (const Case& c : cases){ + ScriptedRoute script; + script.ends[0] = stop_part_way(c.stopped); + script.ends[1] = no_movement(); + const bool failed = script.run({open}, &planning); + if (script.walked.size() < 2 || !script.walked[1].to_wall){ + return std::format("{}: expected a hold to a wall after the stop:{}", c.name, describe(script.walked)); + } + if (c.bump){ + const UnitTestResult logged = expect_logged(script.logs, {"as a bump into the wall it may stand at already"}); + if (failed || logged.result != UnitTestResult::PASSED){ + return std::format("{}: expected the hold taken as a bump: {}", c.name, script.failure); + } + }else if (!failed || script.failure.find("moved nothing") == std::string::npos){ + return std::format("{}: expected a failure, got: {}", c.name, script.failure); + } + } + return true; +} + // The route log lines the games share (Pokemon_RouteLog.h): the plan from // the caller's start under its label, then each leg and how it ended. UnitTestResult test_route_logged(){ @@ -327,9 +456,22 @@ UnitTestResult test_route_logged(){ "[Route] Leg 2/2: west 1 (open) to Corridor (3, 1)", "[Route] test walk: west 1 (open): ended at Corridor (3, 1) (walked).", }; + // The [Leg] records are the replay's; they are checked apart. + std::vector logs; + size_t leg_records = 0; + for (const std::string& line : script.logs){ + if (line.starts_with("[Leg] ")){ + leg_records++; + }else{ + logs.push_back(line); + } + } + if (leg_records != route.size()){ + return "Expected one [Leg] record per leg, got " + std::to_string(leg_records) + "."; + } for (size_t c = 0; c < expected.size(); c++){ - const std::string got = c < script.logs.size() ? "\"" + script.logs[c] + "\"" : std::string("nothing"); - if (c >= script.logs.size() || script.logs[c] != expected[c]){ + const std::string got = c < logs.size() ? "\"" + logs[c] + "\"" : std::string("nothing"); + if (c >= logs.size() || logs[c] != expected[c]){ return "Log line " + std::to_string(c) + ": expected \"" + expected[c] + "\", got " + got + "."; } } @@ -518,6 +660,18 @@ const OverworldRecoveryTestCase OVERWORLD_RECOVERY_CASES[] = { .input = {.name = "a leg to a wall ends at it", .body = test_wall_check}, .expectation = {.description = "validates arrival position at wall terminator"}, }, + { + .input = {.name = "a hold that moved nothing", .body = test_unmoved_leg}, + .expectation = {.description = "a leg whose screen never moved is walked again, then fails; not taken as walked"}, + }, + { + .input = {.name = "a hold that moved nothing from an unknown tile", .body = test_unmoved_hold_from_unknown_tile}, + .expectation = {.description = "a bump at the wall it may stand at is walked; no movement from short of it fails"}, + }, + { + .input = {.name = "a town banner over a hold that moved nothing", .body = test_banner_is_no_scroll}, + .expectation = {.description = "only a scroll of the whole map reads as moved"}, + }, { .input = {.name = "a leg crosses a trainer's sight", .body = test_trainer_crossing}, .expectation = {.description = "recovers when traversing through trainer vision cones"}, diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp index d18e2f71e0..bf88891e8d 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.cpp @@ -735,19 +735,25 @@ StepOutcome OverworldSimWalker::walk(const RouteStep& step, std::optional walked = walk_step(step, facing)){ return StepOutcome{.end = StepEnd::STOPPED, .walked = walked}; } if (step.action == RouteAction::FACE){ return StepOutcome{}; } + // What the console reads off the screen: whether the map scrolled. + HoldMovement movement = HoldMovement::UNMEASURED; + if (step.action == RouteAction::WALK && !step.warps){ + movement = m_sim.state().position == before ? HoldMovement::NONE : HoldMovement::MOVED; + } size_t late = clear_late(interrupts(step), step.wild ? m_rules.after_wild_leg_watch : AFTER_LEG_WATCH, MAX_INTERRUPTIONS_PER_LEG); if (late == 0){ late = clear_unseen_battle(step); } const bool done = late == 0 || step.warps || step.action != RouteAction::WALK; if (done){ - return StepOutcome{}; + return StepOutcome{.end = StepEnd::DONE, .walked = std::nullopt, .movement = movement}; } return StepOutcome{.end = StepEnd::LATE, .walked = late_stop(step)}; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp index 917031fc11..388c42d5e1 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim_Tests.cpp @@ -328,7 +328,8 @@ UnitTestResult test_battle_on_warp_leg(){ // cp0007-zy (console B, 2026-11-01): with the START menu still open, three // legs to walls in Viridian Forest "walked" without the player moving. -// The walker now stops before the first. +// The walker now stops before the first; with that check off, it measures +// that the screen never moved and fails on the first leg. UnitTestResult test_start_menu_open(){ SimCase c{.start = at("ViridianForest", 42, 44), .facing = Direction::NORTH, .goal = at("ViridianForest", 39, 25)}; c.script.start_menu_open = true; @@ -339,8 +340,8 @@ UnitTestResult test_start_menu_open(){ SimCase unchecked = c; unchecked.rules.check_overworld = false; const SimRun old = run_case(unchecked); - if (!old.failure.empty() || !(old.state.position == c.start)){ - return "Expected the walk to claim the route done without moving, with the check off:" + describe(old); + if (old.failure.find("moved nothing") == std::string::npos || !(old.state.position == c.start)){ + return "Expected a failure for a leg that moved nothing, with the check off:" + describe(old); } return true; } diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldStepChecks.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldStepChecks.h index 906a3a6682..203f4abca7 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldStepChecks.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldStepChecks.h @@ -33,12 +33,22 @@ enum class StepEnd{ BATTLE_BEFORE, }; +// Whether the screen moved while a step's hold ran, as the walker saw it. +enum class HoldMovement{ + UNMEASURED, // not measured: not a plain walk, or no screen to compare + MOVED, // the map scrolled: at least one tile was walked + NONE, // the map did not move at all +}; + // How a step ended, and for an open leg STOPPED part-way, how many of its // tiles the timing of the stop says the player walked. Not set: anywhere -// on the leg. +// on the leg. `movement` is for a DONE walk: a hold that moved nothing is +// not trusted to have reached its end (run_route() judges it against the +// tiles the plan expected). struct StepOutcome{ StepEnd end = StepEnd::DONE; std::optional walked; + HoldMovement movement = HoldMovement::UNMEASURED; }; // Whether, after each leg to a wall, the walker taps once more toward the diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/ConsoleFailures/12.03-22-22-pallet-shore-east-unmoved.json b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/ConsoleFailures/12.03-22-22-pallet-shore-east-unmoved.json new file mode 100644 index 0000000000..0b974338ae --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/ConsoleFailures/12.03-22-22-pallet-shore-east-unmoved.json @@ -0,0 +1,21 @@ +{ + "about": "lab-B 2026-11-12 22:22, FRLG 12.03 pallet_shore (lab-B log lines 317846-317974): from (6, 10) both holds east, the open leg east 4 (held 465 ms) and the reanchoring hold east 4 to the wall (held 798 ms), moved 0 tiles; the user watched and the player never went east. The walker logged \"ended at (10, 10) (walked)\", then \"ended at (12, 10) (walked)\" and \"found again at (12, 10)\", and walked on as if it stood there. Nothing on screen told it otherwise: the walker does not measure whether the screen moved.", + "checkpoint": "12.03", + "start": {"map": "PalletTown", "tile": [6, 8], "facing": "south"}, + "walk": { + "event": "pallet_shore", + "legs": [["south", 2], ["east", 4], ["south", 4], ["west", 2]] + }, + "faults": [ + {"kind": "moved_less", "step": "pallet_shore", "index": 2, "tiles": 4}, + {"kind": "moved_less", "step": "pallet_shore", "index": 3, "tiles": 6} + ], + "failed": {"step": "pallet_shore", "error": "none: the walk ended 'arrived at PalletTown (8, 14)' with the player still on (6, 10)"}, + "frame": "20261112-222235-pallet-shore-east-unmoved.png", + "evidence": [ + "[Route] pallet_shore: east 4 (open): ended at PalletTown (10, 10) (walked).", + "Route: position check: walk east 4 -> PalletTown (10, 10) may have ended up to 2 tiles short with nothing seen; finding the player at walls.", + "[Route] pallet_shore: east 4 (to wall): ended at PalletTown (12, 10) (walked).", + "[Route] pallet_shore: found again at PalletTown (12, 10), after 1 hold(s)." + ] +} diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h new file mode 100644 index 0000000000..4d49ae08af --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h @@ -0,0 +1,29 @@ +/* FRLG 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_PokemonFRLG_AutoStory_ConsoleFailureData_H +#define PokemonAutomation_PokemonFRLG_AutoStory_ConsoleFailureData_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// (file name without ".json", text) of each fault file. +const std::vector>& console_failure_json(); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.cpp new file mode 100644 index 0000000000..3bcf7510da --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.cpp @@ -0,0 +1,187 @@ +/* FRLG AutoStory - Console failure replay tests + * + * From: https://github.com/PokemonAutomation/ + * + * Every fault file (Pokemon_LegReplay.h) of a console failure runs here on + * the overworld simulation: the maps give the walls, trainers and signs, + * only the file's faults are injected, and the walk must end where its + * route says. No case has code of its own: a new console failure is a new + * file in ConsoleFailures/. + * + * The walk is the file's "walk": the event's name and its legs, as the + * story's "legs" arguments give them, from the file's "start". + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/Navigation/Pokemon_LegReplay.h" +#include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldSim.h" +#include "PokemonFRLG_AutoStoryCheckpoints.h" +#include "PokemonFRLG_AutoStoryConsoleFailureData.h" +#include "PokemonFRLG_AutoStoryConsoleFailure_Tests.h" +#include "PokemonFRLG_AutoStoryPacedRoute.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + +namespace{ + +using Pokemon::ConsoleFailure; +using Pokemon::ReplayFault; +using Pokemon::ReplayFaultKind; + +Direction direction_of(const std::string& text){ + for (const Direction direction : Pokemon::ALL_DIRECTIONS){ + if (direction_name(direction) == text){ + return direction; + } + } + throw ParseException("console failure: unknown direction \"" + text + "\""); +} + +// The "walk" of a fault file: its event, and its legs from the file's start. +struct FaultWalk{ + std::string event; + std::vector legs; +}; + +FaultWalk walk_of(const JsonObject& json){ + const JsonObject& walk = json.get_object_throw("walk"); + FaultWalk out{.event = walk.get_string_throw("event"), .legs = {}}; + for (const JsonValue& leg : walk.get_array_throw("legs")){ + const JsonArray& pair = leg.to_array_throw(); + out.legs.push_back({direction_of(pair[0].to_string_throw()), (int)pair[1].to_integer_throw()}); + } + return out; +} + +// The faults as the sim's script: the holds that moved less. +SimScript script_of(const ConsoleFailure& failure){ + SimScript script; + for (const ReplayFault& fault : failure.faults){ + if (fault.kind != ReplayFaultKind::MOVED_LESS){ + throw ParseException(std::format( + "{}: the fault \"{}\" is not one the overworld simulation injects", + failure.name, Pokemon::replay_fault_kind_name(fault.kind) + )); + } + script.short_holds[(int)fault.index] = (int)fault.tiles; + } + return script; +} + +struct ReplayInput{ + std::string name; +}; +struct ReplayExpectation{ + std::string description; +}; +const std::string& fault_text(const std::string& name){ + for (const auto& [file, text] : console_failure_json()){ + if (file == name){ + return text; + } + } + throw ParseException(name + ": no such file in ConsoleFailures/"); +} + +struct ReplayCase{ + ReplayInput input; + ReplayExpectation expectation; +}; + +class Test_ConsoleFailureReplay : public UnitTest{ +public: + explicit Test_ConsoleFailureReplay(ReplayCase test_case) + : UnitTest(std::format("PokemonFRLG::AutoStory - Console failure replay: {}", test_case.input.name)) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + try{ + return replay(); + }catch (Exception& e){ + return std::format("{}: {}", e.name(), e.message()); + }catch (std::exception& e){ + return std::string(e.what()); + } + } + +private: + UnitTestResult replay() const{ + const std::string& name = m_case.input.name; + const std::string text = fault_text(name); + const ConsoleFailure failure = Pokemon::parse_console_failure(name, text); + const JsonValue json = parse_json(text); + const FaultWalk walk = walk_of(json.to_object_throw()); + if (!failure.start || !failure.start_facing){ + return name + ": a fault file for the overworld walk names its start"; + } + const MapPosition start{failure.start->map, failure.start->tile}; + const std::optional> route = fixed_route(story_maps(), start, walk.legs); + if (!route || route->empty()){ + for (size_t n = 1; n <= walk.legs.size(); n++){ + const std::vector prefix(walk.legs.begin(), walk.legs.begin() + n); + if (!fixed_route(story_maps(), start, prefix)){ + return name + ": leg " + std::to_string(n) + " is blocked"; + } + } + return name + ": the walk's legs are not a route from " + to_string(start); + } + OverworldSim sim(story_maps(), start, *failure.start_facing, script_of(failure)); + OverworldSimWalker walker(sim); + const RoutePlanning planning{.maps = &story_maps(), .start = start, .label = walk.event, .checkpoint = failure.checkpoint}; + const std::string failed = walker.run(*route, planning); + const bool right = failed.empty() && sim.state().position == route->back().end; + // The walker may stop itself instead: it did not take the move as seen. + const bool noticed = !failed.empty(); + const std::string fix = json.to_object_throw().get_string_default("known_failing", ""); + if (fix.empty()){ + return right || noticed ? UnitTestResult(true) : UnitTestResult(wrong_end(name, route->back().end, sim, walker)); + } + if (right || noticed){ + return std::format("{} now passes: take its \"known_failing\" off the file", name); + } + return true; + } + + static std::string wrong_end(const std::string& name, const MapPosition& planned, const OverworldSim& sim, const OverworldSimWalker& walker){ + std::string text = std::format( + "{}: the walk ended as done on {}, the player stands on {}", + name, to_string(planned), to_string(sim.state().position) + ); + for (const std::string& line : walker.log()){ + text += "\n " + line; + } + return text; + } + + ReplayCase m_case; +}; + +// One row per file in ConsoleFailures/, named as the file without ".json". +const ReplayCase CONSOLE_FAILURE_CASES[] = { + { + .input = {.name = "12.03-22-22-pallet-shore-east-unmoved"}, + .expectation = {.description = "a hold that moved nothing must not be walked as if it had"}, + }, +}; + +} // namespace + +void add_tests_AutoStoryConsoleFailure(UnitTestDatabase& database){ + for (const auto& scenario : CONSOLE_FAILURE_CASES){ + database.add(scenario); + } +} + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.h new file mode 100644 index 0000000000..e4d31188c1 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.h @@ -0,0 +1,29 @@ +/* FRLG AutoStory - Console failure replay tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_PokemonFRLG_AutoStory_ConsoleFailure_Tests_H +#define PokemonAutomation_PokemonFRLG_AutoStory_ConsoleFailure_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonFRLG{ + + +// One test per fault file in ConsoleFailures/ (Pokemon_LegReplay.h): the +// walk it names runs on the overworld simulation with only the file's +// faults injected, and must end where the route says. A file with a +// "known_failing" fix is red on the walker that failed on the console: its +// test passes while the walk is wrong and fails once it is right, so the +// file's entry is then taken off. +void add_tests_AutoStoryConsoleFailure(UnitTestDatabase& database); + + +} // namespace PokemonFRLG +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp index 87d3fee06d..b4acf5485c 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.cpp @@ -161,7 +161,7 @@ Outcome walk_route(StoryEnv& env, RunState& state, const StoryStep& step, const // Planning lets the walker recover a stopped open leg: it plans back to // the leg's end, then walks the rest as given. From a set of tiles // the start isn't known: the walker reads the facing. - const RoutePlanning planning{.maps = &maps, .costs = story_route_costs(), .start = start, .label = step.name, .wall_check = wall_check(env)}; + const RoutePlanning planning{.maps = &maps, .costs = story_route_costs(), .start = start, .label = step.name, .checkpoint = env.checkpoint, .wall_check = wall_check(env)}; walk_overworld_route_with_recovery(env.console, env.context, *route, planning, facing, interrupts, move_speed(step)); log_arrival(env, step.name, target->map, target->tile); const std::optional end_facing = route->empty() ? facing : std::optional(route->back().facing_after); @@ -185,7 +185,7 @@ Outcome walk_legs(StoryEnv& env, RunState& state, const StoryStep& step){ const OverworldInterruptSet interrupts = walk_interrupts(env, step); // Planning lets the walker recover a stopped open leg: it plans back to // the leg's end, then walks the rest as given. - const RoutePlanning planning{.maps = &story_maps(), .costs = story_route_costs(), .start = start, .label = step.name, .wall_check = wall_check(env)}; + const RoutePlanning planning{.maps = &story_maps(), .costs = story_route_costs(), .start = start, .label = step.name, .checkpoint = env.checkpoint, .wall_check = wall_check(env)}; walk_overworld_route_with_recovery(env.console, env.context, *route, planning, facing, interrupts, move_speed(step)); log_arrival(env, step.name, route->back().end.map, route->back().end.point); return Pokemon::outcome_ok(to_pose(StoryPose{route->back().end.map, route->back().end.point, route->back().facing_after})); @@ -888,6 +888,7 @@ Pokemon::Checkpoint build_checkpoint(const StoryCheckpoint& data, StoryExecutor& [&env](const std::string& line){ env.console.log(line, COLOR_ORANGE); } ); env.start_map = data.start ? Pokemon::grid_pose_of(*data.start).map : ""; + env.checkpoint = data.id; env.start_menu = data.menu; const StartMenu menu = data.menu; checkpoint.save = [&env, menu, id = data.id](RunState&){ diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h index aa9f8d2966..b0d53c9fd0 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryEvents.h @@ -66,6 +66,8 @@ struct StoryEnv{ // The map the checkpoint under way starts on, where its save is: a // reload checks the save card against it. std::string start_map = {}; + // That checkpoint's id, for the walker's [Leg] records. + std::string checkpoint = {}; // That checkpoint's START menu: a reload reads the bag again with it. StartMenu start_menu = StartMenu::BEFORE_POKEMON; // The once_per_save steps done from the last save that changed diff --git a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp index c0dd7c79fa..7347eec5ae 100644 --- a/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp +++ b/SerialPrograms/Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStory_Tests.cpp @@ -27,6 +27,7 @@ #include "VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRoute.h" #include "PokemonFRLG_AutoStoryActions.h" #include "PokemonFRLG_AutoStoryCapture.h" +#include "PokemonFRLG_AutoStoryConsoleFailure_Tests.h" #include "PokemonFRLG_AutoStoryGrind.h" #include "PokemonFRLG_AutoStoryPcStorage.h" #include "PokemonFRLG_AutoStoryMoveData.h" @@ -1074,6 +1075,7 @@ const AutoStoryTestCase AUTO_STORY_CASES[] = { void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStoryRunPlan(database); + add_tests_AutoStoryConsoleFailure(database); for (const auto& scenario : AUTO_STORY_CASES){ database.add(scenario); } diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 79bf7704ab..ab07727075 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1508,6 +1508,12 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Navigation/Pokemon_LegWalker.h Source/Pokemon/Navigation/Pokemon_LegWalker_Tests.cpp Source/Pokemon/Navigation/Pokemon_LegWalker_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 + Source/Pokemon/Navigation/Pokemon_LegTrace_Tests.h Source/Pokemon/Navigation/Pokemon_MapData.cpp Source/Pokemon/Navigation/Pokemon_MapData.h Source/Pokemon/Navigation/Pokemon_MapData_Tests.cpp @@ -2011,6 +2017,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldReanchor_Tests.cpp + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.cpp + Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldLegTrace.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.cpp Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecovery.h Source/VideoGames/PokemonFRLG/Overworld/PokemonFRLG_OverworldRecoveryWalk.cpp @@ -2120,6 +2128,9 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngLoop_Tests.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngOptions.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRngOptions.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailureData.h + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.cpp + Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryConsoleFailure_Tests.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.cpp Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRowSim.h Source/VideoGames/PokemonFRLG/Programs/AutoStory/PokemonFRLG_AutoStoryRows_Tests.h 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()) diff --git a/devtools/maps/leg_trace.py b/devtools/maps/leg_trace.py new file mode 100644 index 0000000000..2f09325e75 --- /dev/null +++ b/devtools/maps/leg_trace.py @@ -0,0 +1,463 @@ +"""Leg traces: a console run's route read back from its log, and graded +against the simulation's route for the same checkpoints. + +A walker writes one record per leg (SerialPrograms Pokemon_LegTrace.h): + + [Leg] {"checkpoint":"00.04","event":"to_the_signs_row","leg":1,"dir":"north", + "tiles":1,"end_kind":"tile","planned_end":[...],"moved":1,"chunks":1, + "ms":800,"end":[["Route 201",57,4]],"known":"counted","interrupts":[]} + +A trace is {"legs": [record, ...]}; the simulation's are checked in as +golden files (Simulation/leg_trace_.golden.json). Logs written before +the records existed are read from their route lines instead (replay_text()): +plan legs, their "ended at" lines, the story's tap and dialogue runs. + +The diff aligns the two traces by checkpoint, event and leg, grades each +leg, and fails only on LARGE: + + OK the same plan, and the end the same, or for an open leg within + OPEN_BAND_TILES of the simulation's + drift outside that band, but a later leg of the event that re-localizes + (a wall, a count, a fade) ends where the simulation's does; or + a leg of a replan after a leg stopped short, until the route + re-syncs + LARGE outside the band and never re-synced, a different map, legs or + events one side lacks, a recovery loop, or a checkpoint whose + walking time is over TIME_RATIO_LARGE times the simulation's + masked a leg with an interruption (wild battle, trainer, scene, + someone in the way), and + the console's legs after it until one ends where a simulation + leg does + + python3 -m devtools.maps.leg_trace replay run.log -o console.json + python3 -m devtools.maps.leg_trace diff run.log Simulation/leg_trace_00.04.golden.json +""" +import argparse +import json +import re +import sys +from dataclasses import dataclass, field + +# Thresholds, in one place. +# An open leg (timed or tapped into the open) ending this many tiles or +# fewer from the simulation's end is OK: a later wall clip absorbs it. +OPEN_BAND_TILES = 2 +# A checkpoint's walking time over this times the simulation's is LARGE... +TIME_RATIO_LARGE = 1.5 +# ...when it is also at least this much longer (short checkpoints jitter). +TIME_MIN_EXTRA_MS = 3000 +# An event with this many more legs than the simulation's is a recovery loop. +LOOP_EXTRA_LEGS = 6 +# How a leg's end is known when it re-localizes the walker. +RELOCALIZING = {'wall', 'counted', 'fade', 'seen', 'dialogue'} + +OK = 'OK' +DRIFT = 'drift' +LARGE = 'LARGE' +MASKED = 'masked' + +LEG_TAG = '[Leg] ' +DIRECTION = r'(north|south|east|west)' +TILE = re.compile(r'([^,()]+?) \((-?\d+), (-?\d+)\)') +CHECKPOINT = re.compile(r'\[Checkpoint\] (\S+): start') +EVENT = re.compile(r'\[Event\] (\S+)/\d+ (\S+): start') +PLAN_LEG = re.compile(r'(?:\[Route\] )?Leg \d+/\d+: ' + DIRECTION + r' (\d+) \((to wall|open|warp)\) to (.*?)\s*$') +LEG_END = re.compile(r'\[Route\] .*: ended at (.*) \((walked|assumed)[^)]*\)\.\s*$') +REPLAN = re.compile(r'\[Route\] .*, at (.*); planning again\.\s*$') +TAPS = re.compile(r'\[(\S+)\] (\S+): taps ' + DIRECTION + r' moved (\d+) of (\d+)(?: tiles\.|: )') +TO_DIALOGUE = re.compile(r'\[(\S+)\] (\S+): ran ' + DIRECTION + + r' to a dialogue: (\d+) moved chunks, (\d+) tiles at most, ([^,]+), (\d+) wild battle') +WILD = re.compile(r'\[Walk\] .*wild battle[^;]*; running\.') +TRAINER = re.compile(r'\[Route\] .* stopped us about ') +BLOCKED = re.compile(r'; waiting for the way to clear\.|: someone is in the way; talking to them\.') +WALKED = re.compile(r'\[Walk\] .*: \w+ after (\d+)ms, (\d+) moved chunks, (\d+) wild battle') + + +def tiles_of(text): + return [[m.group(1).strip(), int(m.group(2)), int(m.group(3))] for m in TILE.finditer(text)] + + +def replay(lines, attempt=None): + """The trace of a log: its [Leg] records, or if it has none, what its + route lines say (replay_text()). A log can hold several runs of a + checkpoint: each record gets its run's "attempt" (1, 2, ... a + checkpoint), and only `attempt` is kept (-1: each checkpoint's last; + None: all).""" + legs = [] + attempts = {} + for line in lines: + m = CHECKPOINT.search(line) + if m: + attempts[m.group(1)] = attempts.get(m.group(1), 0) + 1 + at = line.find(LEG_TAG) + if at >= 0: + record = json.loads(line[at + len(LEG_TAG):]) + record.setdefault('attempt', attempts.get(record['checkpoint'], 1)) + legs.append(record) + trace = {'legs': legs} if legs else replay_text(lines) + return pick_attempt(trace, attempt) + + +def pick_attempt(trace, attempt): + """`trace` with only run `attempt` of each checkpoint (-1: the last).""" + if attempt is None: + return trace + last = {} + for leg in trace['legs']: + last[leg['checkpoint']] = max(last.get(leg['checkpoint'], 0), leg.get('attempt', 1)) + def wanted(checkpoint, run): + return run == (last.get(checkpoint, 0) if attempt == -1 else attempt) + out = {'legs': [leg for leg in trace['legs'] if wanted(leg['checkpoint'], leg.get('attempt', 1))]} + if 'events' in trace: + out['events'] = [e for e in trace['events'] if wanted(e[0], e[2])] + return out + + +def replay_text(lines): + """A trace from a log's free-text route lines: no held time, and tap + legs without their end tiles (the log does not name them).""" + legs = [] + state = {'checkpoint': '', 'event': '', 'count': 0, 'open': None} + attempts = {} + events = [] + + def start(record): + close(None, 'unknown') + state['count'] += 1 + record.update({'checkpoint': state['checkpoint'], 'event': state['event'], 'leg': state['count'], + 'attempt': attempts.get(state['checkpoint'], 1)}) + record.setdefault('chunks', 0) + record.setdefault('ms', 0) + record.setdefault('interrupts', []) + record.setdefault('planned_end', []) + state['open'] = record + + def close(end, known, note=''): + record = state['open'] + if record is None: + return + record['end'] = end or [] + record['known'] = known + if note: + record['note'] = note + legs.append(record) + state['open'] = None + + for line in lines: + m = CHECKPOINT.search(line) + if m: + close(None, 'unknown') + attempts[m.group(1)] = attempts.get(m.group(1), 0) + 1 + continue + m = EVENT.search(line) + if m: + close(None, 'unknown') + state.update({'checkpoint': m.group(1), 'event': m.group(2), 'count': 0}) + events.append([m.group(1), m.group(2), attempts.get(m.group(1), 1)]) + continue + m = PLAN_LEG.search(line) + if m: + kind = {'to wall': 'wall', 'open': 'open', 'warp': 'tile'}[m.group(3)] + start({'dir': m.group(1), 'tiles': int(m.group(2)), 'end_kind': kind, 'planned_end': tiles_of(m.group(4))}) + continue + m = LEG_END.search(line) + if m and state['open'] is not None: + assumed = m.group(2) == 'assumed' + if assumed and 'wild_battle' not in state['open']['interrupts']: + state['open']['interrupts'].append('wild_battle') + wall = state['open']['end_kind'] == 'wall' and not assumed + close(tiles_of(m.group(1)), 'wall' if wall else 'dead_reckoned') + continue + m = REPLAN.search(line) + if m and state['open'] is not None: + close(tiles_of(m.group(1)), 'seen', 'replan') + continue + m = TAPS.search(line) + if m: + start({'dir': m.group(3), 'tiles': int(m.group(5)), 'end_kind': 'tile', 'moved': int(m.group(4)), + 'chunks': int(m.group(4))}) + close(None, 'counted') + continue + m = TO_DIALOGUE.search(line) + if m: + start({'dir': m.group(3), 'tiles': int(m.group(5)), 'end_kind': 'open', 'chunks': int(m.group(4))}) + if int(m.group(7)) > 0: + state['open']['interrupts'].append('wild_battle') + close(None, 'dialogue' if 'dialog' in m.group(6) else 'unknown') + continue + if state['open'] is not None and WILD.search(line): + state['open']['interrupts'].append('wild_battle') + elif state['open'] is not None and TRAINER.search(line): + state['open']['interrupts'].append('trainer') + elif state['open'] is not None and BLOCKED.search(line): + state['open']['interrupts'].append('blocked') + elif state['open'] is not None and WALKED.search(line): + m = WALKED.search(line) + record = state['open'] + record['ms'] += int(m.group(1)) + record['chunks'] += int(m.group(2)) + if record['end_kind'] == 'open': + record['moved'] = record.get('moved', 0) + int(m.group(2)) + if int(m.group(3)) > 0 and 'wild_battle' not in record['interrupts']: + record['interrupts'].append('wild_battle') + close(None, 'unknown') + # The events the log ran: one with no leg lines walked legs the text + # does not describe (a LEGS step to the walls). + return {'legs': legs, 'events': events} + + +@dataclass +class LegGrade: + checkpoint: str + event: str + console: int # the console's leg number, 0 for none + sim: int # the simulation's leg number, 0 for none + grade: str + why: str = '' + + +@dataclass +class Report: + grades: list = field(default_factory=list) + # Checkpoint-level findings: (checkpoint, grade, why). + findings: list = field(default_factory=list) + + def large(self): + return any(g.grade == LARGE for g in self.grades) or any(f[1] == LARGE for f in self.findings) + + def first_divergence(self): + for g in self.grades: + if g.grade in (DRIFT, LARGE): + return g + return None + + def summary(self): + out = [] + checkpoints = [] + for g in self.grades: + if g.checkpoint not in checkpoints: + checkpoints.append(g.checkpoint) + for checkpoint in checkpoints: + mine = [g for g in self.grades if g.checkpoint == checkpoint] + counts = {k: sum(1 for g in mine if g.grade == k) for k in (OK, DRIFT, LARGE, MASKED)} + extra = [f for f in self.findings if f[0] == checkpoint] + large = counts[LARGE] + sum(1 for f in extra if f[1] == LARGE) + line = '{}: {} legs: {} OK, {} drift, {} LARGE, {} masked'.format( + checkpoint, len(mine), counts[OK], counts[DRIFT], large, counts[MASKED]) + first = next((g for g in mine if g.grade in (DRIFT, LARGE)), None) + if first: + line += '; first divergence: {} leg {} ({}): {}'.format(first.event, first.console or '-', first.grade, first.why) + for f in extra: + line += '; {}: {}'.format(f[1], f[2]) + out.append(line) + return out + + +def _key(tile): + return (tile[0], int(tile[1]), int(tile[2])) + + +def _maps(leg): + return {t[0] for t in leg.get('end', [])} + + +def _distance(a, b): + """The fewest tiles between a tile of `a` and one of `b` on the same map; + None if they share no map.""" + best = None + for x in a: + for y in b: + if x[0] == y[0]: + d = abs(int(x[1]) - int(y[1])) + abs(int(x[2]) - int(y[2])) + best = d if best is None else min(best, d) + return best + + +def _same_plan(c, s): + return c['dir'] == s['dir'] and c['tiles'] == s['tiles'] and c['end_kind'] == s['end_kind'] + + +def _same_end(c, s): + return {_key(t) for t in c.get('end', [])} == {_key(t) for t in s.get('end', [])} + + +def _relocalizes(c, s): + """Whether console leg `c` ends, re-localized, where sim leg `s` does.""" + return c.get('known') in RELOCALIZING and c.get('end') and _same_end(c, s) + + +def grade_leg(c, s): + """(grade, why) of console leg `c` against sim leg `s`, of the same plan; + DRIFT here means "outside the band": the caller decides whether a later + leg re-syncs it.""" + if c.get('end') and s.get('end') and _maps(c) != _maps(s): + return LARGE, 'ended on {} where the simulation ended on {}'.format(sorted(_maps(c)), sorted(_maps(s))) + if c['end_kind'] == 'tile' and 'moved' in c and 'moved' in s and c['moved'] != s['moved']: + return DRIFT, 'moved {} of {} tiles, the simulation {}'.format(c['moved'], c['tiles'], s['moved']) + # An open leg's end is dead-reckoned from its plan: the taps that moved + # are what it measured. + if c['end_kind'] == 'open' and 'moved' in c and 'moved' in s and abs(c['moved'] - s['moved']) > OPEN_BAND_TILES: + return DRIFT, 'open leg moved {} of {} tiles, the simulation {}'.format(c['moved'], c['tiles'], s['moved']) + if not c.get('end') or not s.get('end') or _same_end(c, s): + return OK, '' + distance = _distance(c['end'], s['end']) + if c['end_kind'] == 'open' and distance is not None and distance <= OPEN_BAND_TILES: + return OK, 'open leg {} tile(s) off'.format(distance) + return DRIFT, 'ended {} tile(s) from the simulation: {} vs {}'.format(distance, c['end'], s['end']) + + +def _resync(console, i, sim, j): + """After an interruption: the first console leg from `i` that ends where + a sim leg from `j` does, as (console index, sim index), or None. The + legs go on from the ones after both.""" + for k in range(i, len(console)): + for m in range(j, len(sim)): + if console[k].get('end') and _same_end(console[k], sim[m]) and not console[k].get('interrupts'): + return k, m + return None + + +def _grade_event(checkpoint, event, console, sim, grades): + pending = [] # grades outside the band, waiting for a re-sync + i = j = 0 + + def add(c, s, grade, why=''): + g = LegGrade(checkpoint, event, c['leg'] if c else 0, s['leg'] if s else 0, grade, why) + grades.append(g) + return g + + while i < len(console) and j < len(sim): + c, s = console[i], sim[j] + if c.get('interrupts') or s.get('interrupts'): + add(c, s, MASKED, 'interrupted: ' + ', '.join(c.get('interrupts', []) + s.get('interrupts', []))) + found = _resync(console, i + 1, sim, j) + if found is None: + for rest in console[i + 1:]: + add(rest, None, MASKED, 'after an interruption, never re-synced') + return + k, m = found + for rest in console[i + 1:k]: + add(rest, None, MASKED, 'after an interruption') + add(console[k], sim[m], MASKED, 're-synced: ends where the simulation leg {} does'.format(sim[m]['leg'])) + i, j = k + 1, m + 1 + continue + if not _same_plan(c, s): + add(c, s, LARGE, 'planned {} {} ({}), the simulation {} {} ({})'.format( + c['dir'], c['tiles'], c['end_kind'], s['dir'], s['tiles'], s['end_kind'])) + i += 1 + j += 1 + continue + if c.get('note') == 'replan': + # Stopped short and planned again: the legs until the route + # re-syncs are drift, or LARGE if it never does. + grade, why = grade_leg(c, s) + add(c, s, grade if grade != DRIFT else LARGE, ('stopped short and planned again; ' + why).rstrip('; ')) + found = _resync(console, i + 1, sim, j + 1) + if found is None: + for rest in console[i + 1:]: + add(rest, None, LARGE, 'after a replan, never re-synced') + return + k, m = found + for rest in console[i + 1:k]: + add(rest, None, DRIFT, 'a replanned leg; re-synced at leg {}'.format(console[k]['leg'])) + add(console[k], sim[m], OK, 're-synced: ends where the simulation leg {} does'.format(sim[m]['leg'])) + i, j = k + 1, m + 1 + continue + grade, why = grade_leg(c, s) + if grade == OK and _relocalizes(c, s): + for g in pending: + g.grade, g.why = DRIFT, g.why + '; re-synced at leg {}'.format(c['leg']) + pending = [] + g = add(c, s, grade, why) + if grade == DRIFT: + pending.append(g) + i += 1 + j += 1 + for g in pending: + g.grade, g.why = LARGE, g.why + '; never re-synced' + for c in console[i:]: + add(c, None, LARGE, 'a leg the simulation does not walk') + for s in sim[j:]: + add(None, s, LARGE, 'a simulation leg the console did not walk') + + +def _by_event(legs): + out = {} + for leg in legs: + out.setdefault((leg['checkpoint'], leg['event']), []).append(leg) + return out + + +def diff(console, sim, checkpoints=None): + """Grades `console`'s legs (a trace) against `sim`'s, for `checkpoints` + (default: those the simulation trace has).""" + report = Report() + sim_legs = [leg for leg in sim['legs'] if checkpoints is None or leg['checkpoint'] in checkpoints] + wanted = {leg['checkpoint'] for leg in sim_legs} + console_legs = [leg for leg in console['legs'] if leg['checkpoint'] in wanted] + c_events = _by_event(console_legs) + s_events = _by_event(sim_legs) + unrecorded = {(e[0], e[1]) for e in console.get('events', [])} - set(c_events) + for key, s in s_events.items(): + c = c_events.get(key, []) + if key in unrecorded: + for leg in s: + report.grades.append(LegGrade(key[0], key[1], 0, leg['leg'], MASKED, 'the log ran the event but has no lines for its legs')) + continue + if len(c) > len(s) + LOOP_EXTRA_LEGS: + report.findings.append((key[0], LARGE, '{} walked {} legs, the simulation {}: a recovery loop'.format( + key[1], len(c), len(s)))) + _grade_event(key[0], key[1], c, s, report.grades) + for key, c in c_events.items(): + if key not in s_events: + report.findings.append((key[0], LARGE, 'event {} walks {} legs the simulation does not'.format(key[1], len(c)))) + for checkpoint in sorted(wanted): + c_ms = sum(leg.get('ms', 0) for leg in console_legs if leg['checkpoint'] == checkpoint and not leg.get('interrupts')) + s_ms = sum(leg.get('ms', 0) for leg in sim_legs if leg['checkpoint'] == checkpoint) + if s_ms > 0 and c_ms > TIME_RATIO_LARGE * s_ms and c_ms - s_ms >= TIME_MIN_EXTRA_MS: + report.findings.append((checkpoint, LARGE, 'walked {} ms, the simulation {} ms'.format(c_ms, s_ms))) + return report + + +def _load(path, attempt=None): + with open(path, encoding='utf-8', errors='replace') as f: + text = f.read() + if text.lstrip().startswith('{'): + return pick_attempt(json.loads(text), attempt) + return replay(text.splitlines(), attempt) + + +def main(argv=None): + parser = argparse.ArgumentParser(description=__doc__.split('\n')[0]) + sub = parser.add_subparsers(dest='command', required=True) + r = sub.add_parser('replay', help='a log as a trace') + r.add_argument('log') + r.add_argument('-o', '--out') + r.add_argument('--attempt', type=int, help='only this run of each checkpoint (-1: the last)') + d = sub.add_parser('diff', help='grade a console log or trace against a simulation trace') + d.add_argument('console') + d.add_argument('sim') + d.add_argument('--checkpoint', action='append') + d.add_argument('--attempt', type=int, default=-1, help='the console run of each checkpoint to grade (default -1: the last)') + d.add_argument('-v', '--verbose', action='store_true', help='every leg, not only those off') + args = parser.parse_args(argv) + if args.command == 'replay': + text = json.dumps(_load(args.log, args.attempt), indent=1) + if args.out: + with open(args.out, 'w', encoding='utf-8') as f: + f.write(text + '\n') + else: + print(text) + return 0 + report = diff(_load(args.console, args.attempt), _load(args.sim), args.checkpoint) + for g in report.grades: + if args.verbose or g.grade != OK: + print('{} {} console leg {} / sim leg {}: {} {}'.format( + g.checkpoint, g.event, g.console or '-', g.sim or '-', g.grade, g.why).rstrip()) + for line in report.summary(): + print(line) + return 1 if report.large() else 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/devtools/maps/tests/test_leg_trace.py b/devtools/maps/tests/test_leg_trace.py new file mode 100644 index 0000000000..5f4af7f304 --- /dev/null +++ b/devtools/maps/tests/test_leg_trace.py @@ -0,0 +1,228 @@ +import contextlib +import io +import json +import os +import tempfile +import unittest + +from devtools.maps import leg_trace +from devtools.maps.leg_trace import DRIFT, LARGE, MASKED, OK + +GOLDEN = os.path.join(os.path.dirname(__file__), '..', '..', '..', 'SerialPrograms', 'Source', 'PokemonBDSP', + 'Programs', 'AutoStory', 'Simulation', 'leg_trace_00.04.golden.json') + + +def leg(event, number, direction, tiles, kind, end, known=None, ms=1000, interrupts=(), checkpoint='00.04', **extra): + record = {'checkpoint': checkpoint, 'event': event, 'leg': number, 'dir': direction, 'tiles': tiles, + 'end_kind': kind, 'planned_end': end, 'chunks': tiles, 'ms': ms, 'end': end, + 'known': known or ('wall' if kind == 'wall' else 'dead_reckoned'), 'interrupts': list(interrupts)} + record.update(extra) + return record + + +def at(x, y, map_name='Route 201'): + return [[map_name, x, y]] + + +# The simulation's walk of one event: west to a wall, a tile south, west in +# the open, north to a wall. +SIM = {'legs': [ + leg('to_ledge_column', 1, 'west', 7, 'wall', at(56, 4)), + leg('to_ledge_column', 2, 'south', 1, 'open', at(56, 5)), + leg('to_ledge_column', 3, 'west', 5, 'open', at(51, 5)), + leg('to_ledge_column', 4, 'north', 3, 'wall', at(51, 2)), +]} + + +def console(*changes): + """SIM's legs with `changes` (index, {field: value}) applied.""" + legs = json.loads(json.dumps(SIM['legs'])) + for index, change in changes: + legs[index].update(change) + return {'legs': legs} + + +def grades(report): + return [(g.console, g.sim, g.grade) for g in report.grades] + + +class ReplayTest(unittest.TestCase): + def test_records_are_read_back(self): + lines = [ + '2026-11-09 18:41:00.1 - [Console 0]: [Route] Turning north.', + '2026-11-09 18:41:00.2 - [Console 0]: ' + leg_trace.LEG_TAG + json.dumps(SIM['legs'][0]), + '2026-11-09 18:41:00.3 - [Console 0]: ' + leg_trace.LEG_TAG + json.dumps(SIM['legs'][1]), + ] + self.assertEqual(leg_trace.replay(lines), {'legs': [dict(x, attempt=1) for x in SIM['legs'][:2]]}) + + def test_route_lines_without_records(self): + lines = ''' +[Program]: [Event] 00.04/4 to_the_signs_row: start +[Console 0]: [Route] Turning north. +[Console 0]: [00.04] to_the_signs_row: taps north moved 1 of 1 tiles. +[Program]: [Event] 00.04/5 to_ledge_column: start +[Console 0]: [Route] to ledge column: plan from Route 201 (57, 4), Route 201 (58, 4): west 7 (to wall) to Route 201 (56, 4); south 1 (open) to Route 201 (56, 5); west 2 (to wall) to Route 201 (54, 5) +[Console 0]: [Route] Leg 1/3: west 7 (to wall) to Route 201 (56, 4) +[Console 0]: [Walk] to ledge column: west 7 (to wall): wild battle; running. +[Console 0]: [Route] to ledge column: west 7 (to wall): ended at Route 201 (56, 4) (walked). +[Console 0]: [Route] Leg 2/3: south 1 (open) to Route 201 (56, 5) +[Console 0]: [Walk] to ledge column: south 1 (open): blocked after 800ms, 0 moved chunks, 0 wild battle(s). +[Console 0]: [Route] to ledge column: south 1 (open): blocked after 0 of 1 tiles; waiting for the way to clear. +[Console 0]: [Route] to ledge column: south 1 (open): blocked after 0 of 1, at Route 201 (56, 4); planning again. +[Program]: [Event] 00.04/18 into_moms_call: start +[Console 0]: [00.04] into_moms_call: ran west to a dialogue: 2 moved chunks, 1 tiles at most, dialog, 0 wild battle(s). +'''.splitlines() + legs = leg_trace.replay(lines)['legs'] + self.assertEqual([(x['event'], x['leg'], x['dir'], x['tiles'], x['end_kind'], x['known']) for x in legs], [ + ('to_the_signs_row', 1, 'north', 1, 'tile', 'counted'), + ('to_ledge_column', 1, 'west', 7, 'wall', 'wall'), + ('to_ledge_column', 2, 'south', 1, 'open', 'seen'), + ('into_moms_call', 1, 'west', 1, 'open', 'dialogue'), + ]) + self.assertEqual(legs[0]['moved'], 1) + self.assertEqual(legs[1]['interrupts'], ['wild_battle']) + self.assertEqual(legs[1]['end'], at(56, 4)) + self.assertEqual(legs[2]['note'], 'replan') + self.assertEqual((legs[2]['interrupts'], legs[2]['ms'], legs[2]['moved']), (['blocked'], 800, 0)) + self.assertEqual(legs[2]['checkpoint'], '00.04') + + + def test_runs_of_a_checkpoint_are_told_apart(self): + record = json.dumps(SIM['legs'][0]) + lines = ['[Checkpoint] 00.04: start, 23 events', leg_trace.LEG_TAG + record, + '[Checkpoint] 00.04: start, 23 events', leg_trace.LEG_TAG + record, leg_trace.LEG_TAG + record] + self.assertEqual([x['attempt'] for x in leg_trace.replay(lines)['legs']], [1, 2, 2]) + self.assertEqual(len(leg_trace.replay(lines, attempt=1)['legs']), 1) + self.assertEqual(len(leg_trace.replay(lines, attempt=-1)['legs']), 2) + + +class DiffTest(unittest.TestCase): + def test_same_route_is_ok(self): + report = leg_trace.diff(console(), SIM) + self.assertEqual([g.grade for g in report.grades], [OK] * 4) + self.assertFalse(report.large()) + self.assertEqual(report.summary(), ['00.04: 4 legs: 4 OK, 0 drift, 0 LARGE, 0 masked']) + + def test_open_leg_inside_its_band_is_ok(self): + report = leg_trace.diff(console((2, {'end': at(49, 5)})), SIM) + self.assertEqual([g.grade for g in report.grades], [OK] * 4) + + def test_open_leg_off_then_a_wall_resyncs_is_drift(self): + report = leg_trace.diff(console((2, {'end': at(46, 5)})), SIM) + self.assertEqual([g.grade for g in report.grades], [OK, OK, DRIFT, OK]) + self.assertFalse(report.large()) + self.assertIn('re-synced at leg 4', report.grades[2].why) + + def test_open_leg_taps_short_then_a_wall_resyncs_is_drift(self): + report = leg_trace.diff(console((2, {'moved': 1}), (2, {'moved': 5})), {'legs': console((2, {'moved': 5}))['legs']}) + self.assertEqual([g.grade for g in report.grades], [OK] * 4) + short = console((2, {'moved': 1})) + sim = console((2, {'moved': 5})) + report = leg_trace.diff(short, sim) + self.assertEqual([g.grade for g in report.grades], [OK, OK, DRIFT, OK]) + self.assertIn('moved 1 of 5 tiles, the simulation 5', report.grades[2].why) + + def test_wall_leg_off_never_resynced_is_large(self): + report = leg_trace.diff(console((3, {'end': at(51, 3)})), SIM) + self.assertEqual([g.grade for g in report.grades], [OK, OK, OK, LARGE]) + self.assertTrue(report.large()) + self.assertEqual(report.first_divergence().console, 4) + + def test_different_map_is_large(self): + report = leg_trace.diff(console((0, {'end': at(56, 4, 'Twinleaf Town')})), SIM) + self.assertEqual(report.grades[0].grade, LARGE) + + def test_interruption_mid_leg_is_masked_and_resyncs(self): + # A wild battle on the open leg west: the console replans from where + # it is (two legs) and joins the simulation's route at the wall. + legs = json.loads(json.dumps(SIM['legs'][:2])) + legs += [ + leg('to_ledge_column', 3, 'west', 5, 'open', at(53, 5), interrupts=['wild_battle'], known='seen', note='replan'), + leg('to_ledge_column', 4, 'west', 2, 'open', at(51, 5)), + leg('to_ledge_column', 5, 'north', 3, 'wall', at(51, 2)), + ] + report = leg_trace.diff({'legs': legs}, SIM) + self.assertEqual(grades(report), [(1, 1, OK), (2, 2, OK), (3, 3, MASKED), (4, 3, MASKED), (5, 4, OK)]) + self.assertFalse(report.large()) + + def test_interruption_never_resynced_masks_the_rest(self): + legs = json.loads(json.dumps(SIM['legs'][:2])) + legs += [leg('to_ledge_column', 3, 'west', 5, 'open', at(53, 5), interrupts=['trainer'])] + legs += [leg('to_ledge_column', 4, 'north', 9, 'wall', at(53, 0))] + report = leg_trace.diff({'legs': legs}, SIM) + self.assertEqual([g.grade for g in report.grades], [OK, OK, MASKED, MASKED]) + self.assertFalse(report.large()) + + def test_replan_that_resyncs_is_drift(self): + legs = json.loads(json.dumps(SIM['legs'][:2])) + legs += [ + leg('to_ledge_column', 3, 'west', 5, 'open', at(51, 5), known='seen', note='replan'), + leg('to_ledge_column', 4, 'north', 1, 'wall', at(51, 4)), + leg('to_ledge_column', 5, 'north', 2, 'wall', at(51, 2)), + ] + report = leg_trace.diff({'legs': legs}, SIM) + self.assertEqual(grades(report), [(1, 1, OK), (2, 2, OK), (3, 3, OK), (4, 0, DRIFT), (5, 4, OK)]) + self.assertFalse(report.large()) + + def test_event_without_leg_lines_in_an_old_log_is_masked(self): + old = {'legs': SIM['legs'][:1], 'events': [['00.04', 'to_ledge_column', 1], ['00.04', 'into_the_east_corner', 1]]} + sim = {'legs': SIM['legs'][:1] + [leg('into_the_east_corner', 1, 'north', 4, 'wall', [])]} + report = leg_trace.diff(old, sim) + self.assertEqual([g.grade for g in report.grades], [OK, MASKED]) + + def test_missing_and_extra_legs_are_large(self): + report = leg_trace.diff({'legs': SIM['legs'][:3]}, SIM) + self.assertEqual(grades(report)[-1], (0, 4, LARGE)) + extra = {'legs': SIM['legs'] + [leg('to_ledge_column', 5, 'east', 1, 'open', at(52, 2))]} + self.assertEqual(grades(leg_trace.diff(extra, SIM))[-1], (5, 0, LARGE)) + + def test_different_plan_is_large(self): + report = leg_trace.diff(console((1, {'tiles': 2})), SIM) + self.assertEqual(report.grades[1].grade, LARGE) + + def test_event_the_simulation_lacks_is_large(self): + extra = {'legs': SIM['legs'] + [leg('to_ledge_column_recovery', 1, 'east', 1, 'open', at(52, 2))]} + report = leg_trace.diff(extra, SIM) + self.assertTrue(report.large()) + self.assertIn('to_ledge_column_recovery', report.summary()[0]) + + def test_recovery_loop_is_large(self): + loop = [leg('to_ledge_column', 5 + i, 'east' if i % 2 else 'west', 1, 'open', at(51, 5), interrupts=['wild_battle']) + for i in range(leg_trace.LOOP_EXTRA_LEGS + 1)] + report = leg_trace.diff({'legs': SIM['legs'] + loop}, SIM) + self.assertTrue(any('recovery loop' in f[2] for f in report.findings)) + + def test_slow_checkpoint_is_large(self): + slow = console(*[(i, {'ms': 3000}) for i in range(4)]) + report = leg_trace.diff(slow, SIM) + self.assertTrue(report.large()) + self.assertIn('walked 12000 ms, the simulation 4000 ms', report.summary()[0]) + + def test_slow_checkpoint_within_the_ratio_is_ok(self): + report = leg_trace.diff(console(*[(i, {'ms': 1400}) for i in range(4)]), SIM) + self.assertFalse(report.large()) + + +class GoldenTest(unittest.TestCase): + @unittest.skipUnless(os.path.exists(GOLDEN), 'the BDSP golden trace is not on this branch') + def test_golden_trace_against_itself(self): + with open(GOLDEN, encoding='utf-8') as f: + sim = json.load(f) + self.assertTrue(sim['legs']) + self.assertFalse(leg_trace.diff(sim, sim).large()) + + def test_cli_exit_code_is_large(self): + with tempfile.TemporaryDirectory() as folder: + off = os.path.join(folder, 'console.json') + same = os.path.join(folder, 'same.json') + sim = os.path.join(folder, 'sim.json') + for path, trace in ((off, console((3, {'end': at(51, 3)}))), (same, console()), (sim, SIM)): + with open(path, 'w', encoding='utf-8') as f: + json.dump(trace, f) + with contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(leg_trace.main(['diff', off, sim]), 1) + self.assertEqual(leg_trace.main(['diff', same, sim]), 0) + + +if __name__ == '__main__': + unittest.main()