From 15d9c5afe9663b038f38073955c8b5917a6e06cf Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:14:29 -0800 Subject: [PATCH 1/2] AutoStory: story log, failure classes and the step outcome --- .../AutoStory/Pokemon_AutoStory_Tests.h | 4 + .../AutoStory/Pokemon_StoryFailureClasses.cpp | 294 ++++++++++++++++++ .../AutoStory/Pokemon_StoryFailureClasses.h | 107 +++++++ .../Pokemon_StoryFailureClasses_Tests.cpp | 260 ++++++++++++++++ .../Pokemon_StoryFailureClasses_Tests.h | 23 ++ .../Pokemon/AutoStory/Pokemon_StoryLog.cpp | 85 +++++ .../Pokemon/AutoStory/Pokemon_StoryLog.h | 85 +++++ .../AutoStory/Pokemon_StoryLog_Tests.cpp | 167 ++++++++++ .../AutoStory/Pokemon_StoryLog_Tests.h | 23 ++ .../Pokemon/AutoStory/Pokemon_StoryOutcome.h | 76 +++++ .../Pokemon/AutoStory/Pokemon_StoryPose.h | 79 +++++ SerialPrograms/cmake/SourceFiles.cmake | 10 + 12 files changed, 1213 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 4767126bfb..3b2328fa87 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -12,6 +12,8 @@ #define PokemonAutomation_Pokemon_AutoStory_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_StoryFailureClasses_Tests.h" +#include "Pokemon_StoryLog_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" namespace PokemonAutomation{ @@ -21,6 +23,8 @@ namespace Pokemon{ // Registers every shared AutoStory suite, for the unit test runner. inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryActionArg(database); + add_tests_StoryLog(database); + add_tests_StoryFailureClasses(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp new file mode 100644 index 0000000000..11fc343fe9 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -0,0 +1,294 @@ +/* Story Failure Classes + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_StoryFailureClasses.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +constexpr std::string_view TAG_PREFIX = "class="; + +using C = StoryFailureClass; +using R = StoryFailureClassInfo::Retry; + +// The run and the console. +std::vector run_rows(){ + return { + { + C::GAME_NOT_RUNNING, "game_not_running", + "the Switch HOME menu shows instead of the game", + "the HOME menu read at a step's start or after a failure", + {"game_not_running"}, R::STOP, + }, + { + C::LAUNCH_FAILED, "launch_failed", + "getting from HOME to the game's overworld failed", + "the launch driver: no game tile, a screen that ignores its press, no overworld in time", + {"launch_failed"}, R::STOP, + }, + { + C::WRONG_START, "wrong_start", + "the game is not at the checkpoint's start: another save, an older one, another place", + "the checkpoint's start checks and the preflight", + {"start_failed", "wrong_save", "precondition_failed"}, R::STOP, + }, + { + C::RELOAD_FAILED, "reload_failed", + "reloading the checkpoint's save failed", + "the game's reload hook", + {"reload_failed"}, R::STOP, + }, + { + C::SAVE_FAILED, "save_failed", + "saving at the checkpoint's end failed", + "the game's save hook", + {"save_failed"}, R::RELOAD, + }, + { + C::STORY_ERROR, "story_error", + "the story's data or code is wrong: a step, range or action the program cannot run", + "the engine's lint and lookups, before or as a step starts", + { + "unsupported_step", "unknown_checkpoint", "bad_range", "empty_segment", + "bad_verify_policy", "lint_failed", "no_save_hook", "console_only", + }, + R::STOP, + }, + }; +} + +// Walking. +std::vector walk_rows(){ + return { + { + C::LOST_TAP, "lost_tap", + "a tap the game drops", + "the walker reads the player where the tap started", + {}, R::RETRY_STEP, + }, + { + C::UNSEEN_STEP, "unseen_step", + "a step tap that moved the player, read as not moved", + "the walker finds the player a tile past where it read", + {}, R::RETRY_STEP, + }, + { + C::WALK_SHORT, "walk_short", + "a walk or hold ended short of its tile", + "the walker's position check at a wall or landmark", + {}, R::RETRY_STEP, + }, + { + C::BLOCKED, "blocked", + "something on the way stops the walk: a person, a wall the map does not have", + "the walker's progress check: no tile gained", + {}, R::RETRY_STEP, + }, + { + C::MOUNT_LOST, "mount_lost", + "off the Bicycle (or another mount) at a leg that rides", + "the walker's tiles per tap", + {}, R::RETRY_STEP, + }, + { + C::POSE_LOST, "pose_lost", + "where the player is or faces is unknown or not what the map says", + "the localizer: no pose, another map, another facing", + {}, R::RELOAD, + }, + }; +} + +// Warps and fades. +std::vector warp_rows(){ + return { + { + C::WARP_NO_OP, "warp_no_op", + "a step onto a warp shows the fade's black but does not warp", + "the map or tile after the fade is the one before it", + {}, R::RETRY_STEP, + }, + { + C::LATE_FADE, "late_fade", + "a warp's fade shows late or is missed", + "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", + {}, R::RETRY_STEP, + }, + { + C::SLOW_FADE_IN, "slow_fade_in", + "a warp's fade-in runs long: a move right after it goes nowhere", + "the first move after a fade does not move the player", + {}, R::RETRY_STEP, + }, + { + C::DARK_MAP, "dark_map", + "a dark map (a cave without Flash) the readers cannot see", + "the floor reads dim; the walker's and the scene's reads fail there", + {}, R::STOP, + }, + }; +} + +// Scenes, talks and battles. +std::vector scene_rows(){ + return { + { + C::SCENE_END_POSE, "scene_end_pose", + "a step whose end the game sets leaves the player on a tile next to the expected one", + "the next walk starts or ends a tile off", + {}, R::RETRY_STEP, + }, + { + C::SCENE_EARLY, "scene_early", + "a scene starts on the last tiles of the walk before it", + "the walk is interrupted by the scene's dialogue", + {}, R::RETRY_STEP, + }, + { + C::BATTLE_OPENING, "battle_opening", + "a battle's opening is still playing when a step starts", + "the step's first read is not the overworld or battle menu it expects", + {}, R::RETRY_STEP, + }, + { + C::WILD_BATTLES, "wild_battles", + "more wild battles than a clean run meets", + "the interrupt handler clears each; too many in a step fail it", + {"too_many_interrupts"}, R::RETRY_STEP, + }, + }; +} + +// Interrupts, reads, menus and checks. +std::vector check_rows(){ + return { + { + C::INTERRUPT_FAILED, "interrupt_failed", + "something interrupted a step and was not cleared", + "the interrupt handler: no handler, a failed clear, the screen not settled", + {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, R::RELOAD, + }, + { + C::SCREEN_UNREAD, "screen_unread", + "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", + "the step's detector times out", + {"talk_no_text", "mash_no_overworld"}, R::RELOAD, + }, + { + C::MENU_FAILED, "menu_failed", + "a menu action (mart, PC, item, TM, Fly) did not do what it was to", + "the menu's readers: the item, count or choice not as planned", + {}, R::RELOAD, + }, + { + C::BATTLE_LOST, "battle_lost", + "a battle lost or not won: a blackout", + "the battle's end screen or the blackout's heal spot", + {}, R::RELOAD, + }, + { + C::STATE_CHECK_FAILED, "state_check_failed", + "a check of the game's state failed: an event's expects, the checkpoint's end", + "the event's expects and requires, the checkpoint's end and postconditions", + {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, R::RELOAD, + }, + { + C::OPERATION_FAILED, "operation_failed", + "a game's action threw, with no class of its own", + "an OperationFailedException out of a step", + {"operation_failed"}, R::RELOAD, + }, + { + C::SIM_MISMATCH, "sim_mismatch", + "a simulated run left the story: the sim and the table disagree", + "the story sim's end checks (sim runs only)", + {}, R::STOP, + }, + }; +} + +// Every class, in the enum's order. Function-local, so a static +// initializer in another file may read it. +const std::vector& class_rows(){ + static const std::vector CLASSES = []{ + std::vector rows; + const auto append = [&rows](std::vector group){ + rows.insert(rows.end(), std::make_move_iterator(group.begin()), std::make_move_iterator(group.end())); + }; + append(run_rows()); + append(walk_rows()); + append(warp_rows()); + append(scene_rows()); + append(check_rows()); + return rows; + }(); + return CLASSES; +} + +// The class whose id is `id`; nothing for an unknown id. +std::optional find_story_failure_class(std::string_view id){ + for (const StoryFailureClassInfo& info : class_rows()){ + if (info.id == id){ + return info.failure_class; + } + } + return std::nullopt; +} + +} // namespace + + +const std::vector& story_failure_classes(){ + return class_rows(); +} + +const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class){ + const std::vector& classes = class_rows(); + const size_t index = static_cast(failure_class); + if (index >= classes.size() || classes[index].failure_class != failure_class){ + throw InternalProgramError( + nullptr, PA_CURRENT_FUNCTION, std::format("Story failure class {} has no row in its place.", index) + ); + } + return classes[index]; +} + +std::optional story_failure_class_of_outcome(std::string_view failure){ + for (const StoryFailureClassInfo& info : class_rows()){ + if (std::ranges::find(info.outcomes, failure) != info.outcomes.end()){ + return info.failure_class; + } + } + return std::nullopt; +} + + +std::string story_failure_class_tag(StoryFailureClass failure_class){ + return std::format("{}{}", TAG_PREFIX, story_failure_class_info(failure_class).id); +} + +std::optional read_story_failure_class_tag(std::string_view text){ + const size_t start = text.find(TAG_PREFIX); + if (start == std::string_view::npos){ + return std::nullopt; + } + const std::string_view rest = text.substr(start + TAG_PREFIX.size()); + const size_t end = rest.find_first_not_of("abcdefghijklmnopqrstuvwxyz_"); + return find_story_failure_class(rest.substr(0, end)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h new file mode 100644 index 0000000000..6ed2e678b4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h @@ -0,0 +1,107 @@ +/* Story Failure Classes + * + * From: https://github.com/PokemonAutomation/ + * + * One list of the ways an AutoStory step fails, for every game: what the + * run log and run records name a failure by. Each class is a row of data: + * its id, what it means, how a run sees it, and what a retry should do. + * + * An Outcome's failure name ("expect_failed") is finer than a class: a + * class covers the names that mean the same to a retry. The engine's names + * are mapped here; a game's own names are mapped when it adopts the list. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFailureClasses_H +#define PokemonAutomation_Pokemon_StoryFailureClasses_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A class of story failure, in story_failure_classes()'s order. +enum class StoryFailureClass{ + GAME_NOT_RUNNING, + LAUNCH_FAILED, + WRONG_START, + RELOAD_FAILED, + SAVE_FAILED, + STORY_ERROR, + LOST_TAP, + UNSEEN_STEP, + WALK_SHORT, + BLOCKED, + MOUNT_LOST, + POSE_LOST, + WARP_NO_OP, + LATE_FADE, + SLOW_FADE_IN, + DARK_MAP, + SCENE_END_POSE, + SCENE_EARLY, + BATTLE_OPENING, + WILD_BATTLES, + INTERRUPT_FAILED, + SCREEN_UNREAD, + MENU_FAILED, + BATTLE_LOST, + STATE_CHECK_FAILED, + OPERATION_FAILED, + // The last: the tests check that every class up to it has a row. + SIM_MISMATCH, +}; + +// One class of failure. +struct StoryFailureClassInfo{ + // What a retry should do after a failure of the class: a hint, the + // program decides. + enum class Retry{ + // Find where the player is and run the step again. + RETRY_STEP, + // Reload the checkpoint's save and run it again. + RELOAD, + // Stop the run: no retry fixes it, a person or a code change must. + STOP, + }; + + StoryFailureClass failure_class; + // The class in logs and run records: "warp_no_op". + std::string_view id; + // What went wrong, in one line. + std::string_view meaning; + // How a run sees it. + std::string_view detected_by; + // The engine's Outcome failure names of this class. + std::vector outcomes; + Retry retry; +}; + +// Every class, in the enum's order. +const std::vector& story_failure_classes(); + +// The row of `failure_class`. Throws InternalProgramError when the list +// has no row for it in the enum's place. +const StoryFailureClassInfo& story_failure_class_info(StoryFailureClass failure_class); + +// The class of an engine Outcome failure name ("expect_failed"); nothing +// for a name no class lists yet, like a game's own. +std::optional story_failure_class_of_outcome(std::string_view failure); + + +// The class as a log line or run record writes it: "class=warp_no_op". +std::string story_failure_class_tag(StoryFailureClass failure_class); + +// The class tagged in `text`, a log line or run record field that holds a +// story_failure_class_tag(); nothing when it holds none or an unknown id. +std::optional read_story_failure_class_tag(std::string_view text); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp new file mode 100644 index 0000000000..674c2b97cb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -0,0 +1,260 @@ +/* Story Failure Classes Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The list's own checks: every sim fault and every engine failure name + * maps to one class, and every class id reads back from the text a log + * line or run record writes. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Pokemon_StoryFailureClasses.h" +#include "Pokemon_StoryFailureClasses_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// The class id of `failure_class`, or "none" for nothing: for messages. +std::string_view id_or_none(std::optional failure_class){ + return failure_class ? story_failure_class_info(*failure_class).id : "none"; +} + + +// An engine Outcome failure name and the class it maps to. +struct OutcomeClassTestCase{ + struct Input{ + std::string_view failure; + }; + + struct Expectation{ + std::optional failure_class; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_OutcomeClass : public UnitTest{ +public: + explicit Test_OutcomeClass(OutcomeClassTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFailureClasses - outcome: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional got = story_failure_class_of_outcome(m_case.input.failure); + if (got != m_case.expectation.failure_class){ + return std::format( + "\"{}\": expected {}, got {}", + m_case.input.failure, id_or_none(m_case.expectation.failure_class), id_or_none(got) + ); + } + return true; + } + +private: + const OutcomeClassTestCase m_case; +}; + +using C = StoryFailureClass; + +// Every failure name the engine's code returns (autostory/engine), and +// #477's launch_failed. +const OutcomeClassTestCase OUTCOME_CASES[] = { + {.name = "HOME menu", .input = {"game_not_running"}, .expectation = {C::GAME_NOT_RUNNING}}, + {.name = "launch", .input = {"launch_failed"}, .expectation = {C::LAUNCH_FAILED}}, + {.name = "start checks", .input = {"start_failed"}, .expectation = {C::WRONG_START}}, + {.name = "another save", .input = {"wrong_save"}, .expectation = {C::WRONG_START}}, + {.name = "preflight", .input = {"precondition_failed"}, .expectation = {C::WRONG_START}}, + {.name = "reload", .input = {"reload_failed"}, .expectation = {C::RELOAD_FAILED}}, + {.name = "save", .input = {"save_failed"}, .expectation = {C::SAVE_FAILED}}, + {.name = "unsupported step", .input = {"unsupported_step"}, .expectation = {C::STORY_ERROR}}, + {.name = "unknown checkpoint", .input = {"unknown_checkpoint"}, .expectation = {C::STORY_ERROR}}, + {.name = "bad range", .input = {"bad_range"}, .expectation = {C::STORY_ERROR}}, + {.name = "empty segment", .input = {"empty_segment"}, .expectation = {C::STORY_ERROR}}, + {.name = "bad verify policy", .input = {"bad_verify_policy"}, .expectation = {C::STORY_ERROR}}, + {.name = "lint", .input = {"lint_failed"}, .expectation = {C::STORY_ERROR}}, + {.name = "no save hook", .input = {"no_save_hook"}, .expectation = {C::STORY_ERROR}}, + {.name = "console only", .input = {"console_only"}, .expectation = {C::STORY_ERROR}}, + {.name = "too many interrupts", .input = {"too_many_interrupts"}, .expectation = {C::WILD_BATTLES}}, + {.name = "unhandled interrupt", .input = {"unhandled_interrupt"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupt not cleared", .input = {"interrupt_failed"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupt unsettled", .input = {"interrupt_unsettled"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "interrupted", .input = {"interrupted"}, .expectation = {C::INTERRUPT_FAILED}}, + {.name = "talk without text", .input = {"talk_no_text"}, .expectation = {C::SCREEN_UNREAD}}, + {.name = "mash without overworld", .input = {"mash_no_overworld"}, .expectation = {C::SCREEN_UNREAD}}, + {.name = "expect", .input = {"expect_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "require", .input = {"require_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "checkpoint end", .input = {"end_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "postconditions", .input = {"postconditions_failed"}, .expectation = {C::STATE_CHECK_FAILED}}, + {.name = "operation", .input = {"operation_failed"}, .expectation = {C::OPERATION_FAILED}}, + {.name = "a game's own name", .input = {"no_banner"}, .expectation = {std::nullopt}}, + {.name = "empty", .input = {""}, .expectation = {std::nullopt}}, +}; + + +// A check over the whole list. +struct ListCheckTestCase{ + // The check, run over story_failure_classes(). + struct Input{ + UnitTestResult (*check)(); + }; + + // What holds when the check passes. + struct Expectation{ + std::string_view holds; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_ListCheck : public UnitTest{ +public: + explicit Test_ListCheck(ListCheckTestCase test_case) + : UnitTest(std::format("Pokemon::StoryFailureClasses - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.check(); + } + +private: + const ListCheckTestCase m_case; +}; + +UnitTestResult rows_in_enum_order(){ + const std::vector& classes = story_failure_classes(); + for (size_t c = 0; c < classes.size(); c++){ + if (static_cast(classes[c].failure_class) != c){ + return std::format("row {} ({}) is out of the enum's order", c, classes[c].id); + } + } + return true; +} + +UnitTestResult every_class_has_a_row(){ + const size_t count = static_cast(StoryFailureClass::SIM_MISMATCH) + 1; + if (story_failure_classes().size() != count){ + return std::format("{} rows for {} classes", story_failure_classes().size(), count); + } + try{ + story_failure_class_info(static_cast(count)); + }catch (const InternalProgramError&){ + return true; + } + return std::string("a class past the last row did not throw"); +} + +UnitTestResult no_outcome_in_two_classes(){ + std::map seen; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + for (std::string_view failure : info.outcomes){ + const auto [iter, added] = seen.emplace(failure, info.id); + if (!added){ + return std::format("\"{}\" is in {} and {}", failure, iter->second, info.id); + } + } + } + return true; +} + +UnitTestResult ids_round_trip(){ + std::set ids; + for (const StoryFailureClassInfo& info : story_failure_classes()){ + if (!ids.insert(info.id).second){ + return std::format("id \"{}\" is in two rows", info.id); + } + const std::optional got = read_story_failure_class_tag(story_failure_class_tag(info.failure_class)); + if (got != info.failure_class){ + return std::format("id \"{}\" reads back as {}", info.id, id_or_none(got)); + } + } + return true; +} + +UnitTestResult tags_round_trip_through_a_log_line(){ + for (const StoryFailureClassInfo& info : story_failure_classes()){ + const std::string line = std::format( + "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: has_parcel ({})", + story_failure_class_tag(info.failure_class) + ); + const std::optional got = read_story_failure_class_tag(line); + if (got != info.failure_class){ + return std::format("\"{}\" reads back as {}", line, id_or_none(got)); + } + } + return true; +} + +UnitTestResult tags_round_trip_through_a_run_record(){ + for (const StoryFailureClassInfo& info : story_failure_classes()){ + const std::string field = std::format("\"failure\": \"{}\"", story_failure_class_tag(info.failure_class)); + const std::optional got = read_story_failure_class_tag(field); + if (got != info.failure_class){ + return std::format("{} reads back as {}", field, id_or_none(got)); + } + } + return true; +} + +UnitTestResult no_class_without_a_tag(){ + const std::string_view lines[] = { + "[Checkpoint] 00.04: failed at 2 talk_mom: expect_failed: has_parcel", + "[Checkpoint] 00.04: failed (class=no_such_class)", + "class=", + }; + for (std::string_view line : lines){ + const std::optional got = read_story_failure_class_tag(line); + if (got){ + return std::format("\"{}\" reads as {}", line, id_or_none(got)); + } + } + return true; +} + +const ListCheckTestCase LIST_CHECKS[] = { + {.name = "rows in the enum's order", .input = {rows_in_enum_order}, .expectation = {"story_failure_class_info() indexes by the enum"}}, + {.name = "every class has a row", .input = {every_class_has_a_row}, .expectation = {"no class reads past the table"}}, + {.name = "no outcome name in two classes", .input = {no_outcome_in_two_classes}, .expectation = {"a failure name has one class"}}, + {.name = "ids round-trip", .input = {ids_round_trip}, .expectation = {"each id is unique and its tag alone reads back"}}, + {.name = "tags round-trip through a log line", .input = {tags_round_trip_through_a_log_line}, .expectation = {"the run log names the class"}}, + {.name = "tags round-trip through a run record", .input = {tags_round_trip_through_a_run_record}, .expectation = {"a run record names the class"}}, + {.name = "no class without a tag", .input = {no_class_without_a_tag}, .expectation = {"untagged or unknown text has no class"}}, +}; + +} // namespace + + +void add_tests_StoryFailureClasses(UnitTestDatabase& database){ + for (const OutcomeClassTestCase& test_case : OUTCOME_CASES){ + database.add(test_case); + } + for (const ListCheckTestCase& test_case : LIST_CHECKS){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h new file mode 100644 index 0000000000..c14ff5bca4 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h @@ -0,0 +1,23 @@ +/* Story Failure Classes Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFailureClasses_Tests_H +#define PokemonAutomation_Pokemon_StoryFailureClasses_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared story failure classes. +void add_tests_StoryFailureClasses(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp new file mode 100644 index 0000000000..5fed3725bf --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -0,0 +1,85 @@ +/* Story Log + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryLog.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +std::string resets_text(size_t attempt){ + if (attempt <= 1){ + return "one-shot"; + } + return std::format("{} reset{}", attempt - 1, attempt == 2 ? "" : "s"); +} + +// "[Checkpoint] 00.04", or the game's own checkpoint text unchanged. +std::string checkpoint_prefix(std::string_view checkpoint, bool add_checkpoint_prefix){ + if (!add_checkpoint_prefix){ + return std::string(checkpoint); + } + return std::format("[Checkpoint] {}", checkpoint); +} + +} // namespace + + +std::string checkpoint_start_line( + std::string_view checkpoint, std::optional events, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + if (!events){ + return std::format("{}: start", prefix); + } + return std::format("{}: start, {} events", prefix, *events); +} + +std::string checkpoint_attempt_line( + std::string_view checkpoint, size_t attempt, std::optional attempts, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + if (!attempts){ + return std::format("{}: attempt {}", prefix, attempt); + } + return std::format("{}: attempt {}/{}", prefix, attempt, *attempts); +} + +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, bool add_checkpoint_prefix){ + return std::format( + "{}: passed on attempt {} ({})", + checkpoint_prefix(checkpoint, add_checkpoint_prefix), attempt, resets_text(attempt) + ); +} + +std::string checkpoint_reset_line( + std::string_view checkpoint, size_t next_attempt, std::optional attempts, bool add_checkpoint_prefix +){ + const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + if (!attempts){ + return std::format("{}: failed, resetting for attempt {}", prefix, next_attempt); + } + return std::format("{}: failed, resetting for attempt {}/{}", prefix, next_attempt, *attempts); +} + +std::string story_short_line(std::string_view line){ + if (!line.starts_with('[')){ + return std::string(line); + } + const size_t end = line.find("] "); + if (end == std::string_view::npos){ + return std::string(line); + } + return std::string(line.substr(end + 2)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h new file mode 100644 index 0000000000..11bfb009d2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -0,0 +1,85 @@ +/* Story Log + * + * From: https://github.com/PokemonAutomation/ + * + * A story is split at three levels: a segment is a chapter of + * checkpoints, a checkpoint is the stretch between two saves and the unit + * a run retries, and an event is one step inside a checkpoint. This file + * writes the checkpoint lines: start, attempt, reset and pass, in one + * format for every game, so the program developer and user find each + * checkpoint's attempts the same way in any game's log. Nothing here runs + * a story. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryLog_H +#define PokemonAutomation_Pokemon_StoryLog_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The lines below start with "[Checkpoint] " and then `checkpoint`. A game +// whose checkpoint text already says it is a checkpoint (SV's +// "Checkpoint:: 004 - 02 ...") passes that text as `checkpoint` and +// `add_checkpoint_prefix` false, and the line starts with the text alone: +// Checkpoint:: 004 - 02 ...: start + +// A checkpoint starts; the story engine also says it has `events` events: +// [Checkpoint] 00.04: start +// [Checkpoint] 00.04: start, 3 events +std::string checkpoint_start_line( + std::string_view checkpoint, + std::optional events = std::nullopt, + bool add_checkpoint_prefix = true +); + +// An attempt at a checkpoint starts, counting from 1, out of the +// program's limit of `attempts` if it has one: +// [Checkpoint] 00.04: attempt 2 +// [Checkpoint] 00.04: attempt 2/3 +std::string checkpoint_attempt_line( + std::string_view checkpoint, + size_t attempt, + std::optional attempts = std::nullopt, + bool add_checkpoint_prefix = true +); + +// A checkpoint passed on attempt `attempt`, counting from 1; every attempt +// before it ended in a reset: +// [Checkpoint] 00.03: passed on attempt 1 (one-shot) +// [Checkpoint] 00.04: passed on attempt 3 (2 resets) +std::string checkpoint_pass_line( + std::string_view checkpoint, + size_t attempt, + bool add_checkpoint_prefix = true +); + +// An attempt failed and the game resets to the checkpoint's save for +// attempt `next_attempt`, out of the limit of `attempts` if it has one: +// [Checkpoint] 00.04: failed, resetting for attempt 2 +// [Checkpoint] 00.04: failed, resetting for attempt 2/3 +std::string checkpoint_reset_line( + std::string_view checkpoint, + size_t next_attempt, + std::optional attempts = std::nullopt, + bool add_checkpoint_prefix = true +); + +// The full lines above go to the log. The video overlay, which wraps its +// lines, shows each one short, without its leading "[Tag] ": +// [Checkpoint] 12.03: start, 18 events -> 12.03: start, 18 events +// Any line with a leading "[Tag] " is shortened the same way. A line +// without a tag, like SV's checkpoint text, is returned unchanged. +std::string story_short_line(std::string_view line); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp new file mode 100644 index 0000000000..759ed9b3d7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -0,0 +1,167 @@ +/* Story Log Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryLog.h" +#include "Pokemon_StoryLog_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// One formatter call and the line it must make. +struct StoryLogTestCase{ + // A formatter's line, made when the table is built. + struct Input{ + std::string line; + }; + + // The exact line: the lab watcher and the engine tests read these. + struct Expectation{ + std::string line; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_StoryLog : public UnitTest{ +public: + Test_StoryLog(StoryLogTestCase test_case) + : UnitTest(std::format("Pokemon::StoryLog - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + if (m_case.input.line != m_case.expectation.line){ + return std::format("Expected \"{}\", got \"{}\"", m_case.expectation.line, m_case.input.line); + } + return true; + } + +private: + const StoryLogTestCase m_case; +}; + +// SV's text for a checkpoint, which already says "Checkpoint::". +constexpr std::string_view SV_TEXT = "Checkpoint:: 004 - 02. Start point: X"; + +const StoryLogTestCase STORY_LOG_CASES[] = { + { + .name = "checkpoint start", + .input = {.line = checkpoint_start_line("00.04")}, + .expectation = {.line = "[Checkpoint] 00.04: start"}, + }, + { + .name = "checkpoint start with its events", + .input = {.line = checkpoint_start_line("00.04", /*events=*/3)}, + .expectation = {.line = "[Checkpoint] 00.04: start, 3 events"}, + }, + { + .name = "attempt", + .input = {.line = checkpoint_attempt_line("00.04", 2)}, + .expectation = {.line = "[Checkpoint] 00.04: attempt 2"}, + }, + { + .name = "attempt out of a limit", + .input = {.line = checkpoint_attempt_line("00.04", 2, /*attempts=*/3)}, + .expectation = {.line = "[Checkpoint] 00.04: attempt 2/3"}, + }, + { + .name = "one-shot pass", + .input = {.line = checkpoint_pass_line("00.03", 1)}, + .expectation = {.line = "[Checkpoint] 00.03: passed on attempt 1 (one-shot)"}, + }, + { + .name = "pass after one reset", + .input = {.line = checkpoint_pass_line("00.04", 2)}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}, + }, + { + .name = "pass after resets", + .input = {.line = checkpoint_pass_line("00.04", 3)}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets)"}, + }, + { + .name = "reset", + .input = {.line = checkpoint_reset_line("00.04", 2)}, + .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2"}, + }, + { + .name = "reset out of a limit", + .input = {.line = checkpoint_reset_line("00.04", 2, /*attempts=*/3)}, + .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2/3"}, + }, + { + .name = "start without the checkpoint prefix", + .input = {.line = checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .name = "attempt without the checkpoint prefix", + .input = {.line = checkpoint_attempt_line(SV_TEXT, 2, /*attempts=*/100, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, + }, + { + .name = "pass without the checkpoint prefix", + .input = {.line = checkpoint_pass_line(SV_TEXT, 2, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: passed on attempt 2 (1 reset)"}, + }, + { + .name = "reset without the checkpoint prefix", + .input = {.line = checkpoint_reset_line(SV_TEXT, 2, /*attempts=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: failed, resetting for attempt 2"}, + }, + { + .name = "short checkpoint start", + .input = {.line = story_short_line("[Checkpoint] 12.03: start, 18 events")}, + .expectation = {.line = "12.03: start, 18 events"}, + }, + { + .name = "short line with another tag", + .input = {.line = story_short_line("[Tag] 12.03: some text")}, + .expectation = {.line = "12.03: some text"}, + }, + { + .name = "short line without the checkpoint prefix", + .input = {.line = story_short_line( + checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false) + )}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, + }, + { + .name = "short line with an unclosed tag", + .input = {.line = story_short_line("[Tag 12.03")}, + .expectation = {.line = "[Tag 12.03"}, + }, + { + .name = "short empty line", + .input = {.line = story_short_line("")}, + .expectation = {.line = ""}, + }, +}; + + +} // namespace + + +void add_tests_StoryLog(UnitTestDatabase& database){ + for (const StoryLogTestCase& test_case : STORY_LOG_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h new file mode 100644 index 0000000000..822a222b31 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h @@ -0,0 +1,23 @@ +/* Story Log Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryLog_Tests_H +#define PokemonAutomation_Pokemon_StoryLog_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared checkpoint log lines. +void add_tests_StoryLog(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h new file mode 100644 index 0000000000..66a1e1a083 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h @@ -0,0 +1,76 @@ +/* Story Outcome + * + * From: https://github.com/PokemonAutomation/ + * + * Split out of Pokemon_StoryEvents.h, unchanged; it includes this header. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryOutcome_H +#define PokemonAutomation_Pokemon_StoryOutcome_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryFailureClasses.h" +#include "Pokemon_StoryLog.h" +#include "Pokemon_StoryPose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How an action or a check ended: empty `failure` is success; otherwise a +// name that recoveries match (e.g. "probe_exhausted"). A succeeded action +// that knows where it left the player sets `pose`; the runner writes it +// into the run before the event's expects are checked. +struct Outcome{ + std::string failure; + std::string detail; + std::optional pose; + // A succeeded action that left the game as it found it: a read, a + // check, a TM the lead already knows. A reload of the same save undoes + // nothing it did. + bool changed_nothing = false; + // The failure's class, when the action knows it: a game's own failure + // name ("probe_exhausted") has none in the shared list until the game + // gives it one here. Nothing: the class the list gives `failure`. + std::optional failure_class = std::nullopt; + + bool succeeded() const; +}; +// Constructs a successful event outcome, updating the run pose when given one. +Outcome outcome_ok(std::optional pose = std::nullopt); +// outcome_ok() with `changed_nothing`. +Outcome outcome_unchanged(); +// Constructs a failed event outcome with reason and optional diagnostic detail. +// `failure_class` is for a name the shared list does not class (a game's +// own); without it the list's class for `failure` holds. +Outcome outcome_failed( + std::string failure, std::string detail = "", + std::optional failure_class = std::nullopt +); +// "probe_exhausted: 4 rows": the failure and its detail, if any. +std::string to_string(const Outcome& outcome); + +// The failure of a step that found the Switch's HOME menu instead of the +// game. Nothing in the story can answer it, as every press goes to the +// console's menus: no recovery runs and no reload, the run stops. +inline constexpr std::string_view GAME_NOT_RUNNING = "game_not_running"; +// GAME_NOT_RUNNING, saying so for the run log. +Outcome outcome_game_not_running(); + + +// Callable action executed for a story event. +using Act = MoveOnlyFunction; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h new file mode 100644 index 0000000000..b9c0eb53db --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h @@ -0,0 +1,79 @@ +/* Story Pose + * + * From: https://github.com/PokemonAutomation/ + * + * Split out of Pokemon_StoryEvents.h, unchanged; it includes this header. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryPose_H +#define PokemonAutomation_Pokemon_StoryPose_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryLog.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How much of the pose is known. COLUMN and ROW pin one axis (a wall, a +// door); SET means one of the positions the pose holds. +enum class PoseCertainty{ + TILE, + COLUMN, + ROW, + SET, + UNKNOWN, +}; +// Returns the string label for a pose certainty level. +std::string certainty_name(PoseCertainty certainty); + +// Whether a pose known to `have` is good enough for an event that needs `need`. +bool certainty_covers(PoseCertainty have, PoseCertainty need); + +// Where on a map the player is, in the game's own terms: tiles and a +// facing in a 2D game (Pokemon_TilePose.h), a map point or a fly point in +// an open-world one. The engine only carries and prints it; the game's +// actions read it back. +struct Position{ + virtual ~Position() = default; + // The text after the map in a log line, e.g. "(10, 20) facing north"; + // none for a game that has nothing to say beyond the map. + virtual std::string describe() const{ + return ""; + } +}; + +// Where the player is believed to be. The screen is read; the pose is +// tracked, and only an anchor (a wall, a door, an NPC who answered) makes +// it certain. `where` is null until the game learns a position and after +// the pose is lost; it is shared because poses are copied (an event's +// learned pose into the run) and never changed in place. +struct Pose{ + std::string map; + PoseCertainty certainty = PoseCertainty::UNKNOWN; + std::shared_ptr where; +}; +// Formats a Pose as a human-readable string. +std::string to_string(const Pose& pose); + + +// What a run knows. Flags are set only by confirmed effects and are lost +// on a reload. +struct RunState{ + Pose pose; + std::set flags; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6e5342bdc1..9fe0bbaa32 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1449,6 +1449,16 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h Source/Pokemon/AutoStory/Pokemon_MapUtil.h + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp + Source/Pokemon/AutoStory/Pokemon_StoryLog.h + Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h + Source/Pokemon/AutoStory/Pokemon_StoryPose.h Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp From cce8838ea6badb051cef50269429364620ddd36c Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:47:26 -0800 Subject: [PATCH 2/2] AutoStory: Outcome and Pose define their own functions; tests check what they claim Outcome's and Pose's functions were declared here but defined only with the event runner; they get their own .cpp files. The checkpoint prefix flag is an enum instead of a defaulted bool. The log tests call the formatter when they run instead of during static initialization, the list checks are plain named tests, and the failure class rows use designated initializers. --- .../AutoStory/Pokemon_StoryFailureClasses.cpp | 218 +++++++++--------- .../Pokemon_StoryFailureClasses_Tests.cpp | 58 ++--- .../Pokemon/AutoStory/Pokemon_StoryLog.cpp | 20 +- .../Pokemon/AutoStory/Pokemon_StoryLog.h | 16 +- .../AutoStory/Pokemon_StoryLog_Tests.cpp | 51 ++-- .../AutoStory/Pokemon_StoryOutcome.cpp | 50 ++++ .../Pokemon/AutoStory/Pokemon_StoryOutcome.h | 14 +- .../Pokemon/AutoStory/Pokemon_StoryPose.cpp | 59 +++++ .../Pokemon/AutoStory/Pokemon_StoryPose.h | 14 +- SerialPrograms/cmake/SourceFiles.cmake | 2 + 10 files changed, 298 insertions(+), 204 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp index 11fc343fe9..726ea4cdea 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp @@ -26,44 +26,44 @@ using R = StoryFailureClassInfo::Retry; std::vector run_rows(){ return { { - C::GAME_NOT_RUNNING, "game_not_running", - "the Switch HOME menu shows instead of the game", - "the HOME menu read at a step's start or after a failure", - {"game_not_running"}, R::STOP, + .failure_class = C::GAME_NOT_RUNNING, .id = "game_not_running", + .meaning = "the Switch HOME menu shows instead of the game", + .detected_by = "the HOME menu read at a step's start or after a failure", + .outcomes = {"game_not_running"}, .retry = R::STOP, }, { - C::LAUNCH_FAILED, "launch_failed", - "getting from HOME to the game's overworld failed", - "the launch driver: no game tile, a screen that ignores its press, no overworld in time", - {"launch_failed"}, R::STOP, + .failure_class = C::LAUNCH_FAILED, .id = "launch_failed", + .meaning = "getting from HOME to the game's overworld failed", + .detected_by = "the launch driver: no game tile, a screen that ignores its press, no overworld in time", + .outcomes = {"launch_failed"}, .retry = R::STOP, }, { - C::WRONG_START, "wrong_start", - "the game is not at the checkpoint's start: another save, an older one, another place", - "the checkpoint's start checks and the preflight", - {"start_failed", "wrong_save", "precondition_failed"}, R::STOP, + .failure_class = C::WRONG_START, .id = "wrong_start", + .meaning = "the game is not at the checkpoint's start: another save, an older one, another place", + .detected_by = "the checkpoint's start checks and the preflight", + .outcomes = {"start_failed", "wrong_save", "precondition_failed"}, .retry = R::STOP, }, { - C::RELOAD_FAILED, "reload_failed", - "reloading the checkpoint's save failed", - "the game's reload hook", - {"reload_failed"}, R::STOP, + .failure_class = C::RELOAD_FAILED, .id = "reload_failed", + .meaning = "reloading the checkpoint's save failed", + .detected_by = "the game's reload hook", + .outcomes = {"reload_failed"}, .retry = R::STOP, }, { - C::SAVE_FAILED, "save_failed", - "saving at the checkpoint's end failed", - "the game's save hook", - {"save_failed"}, R::RELOAD, + .failure_class = C::SAVE_FAILED, .id = "save_failed", + .meaning = "saving at the checkpoint's end failed", + .detected_by = "the game's save hook", + .outcomes = {"save_failed"}, .retry = R::RELOAD, }, { - C::STORY_ERROR, "story_error", - "the story's data or code is wrong: a step, range or action the program cannot run", - "the engine's lint and lookups, before or as a step starts", - { + .failure_class = C::STORY_ERROR, .id = "story_error", + .meaning = "the story's data or code is wrong: a step, range or action the program cannot run", + .detected_by = "the engine's lint and lookups, before or as a step starts", + .outcomes = { "unsupported_step", "unknown_checkpoint", "bad_range", "empty_segment", "bad_verify_policy", "lint_failed", "no_save_hook", "console_only", }, - R::STOP, + .retry = R::STOP, }, }; } @@ -72,40 +72,40 @@ std::vector run_rows(){ std::vector walk_rows(){ return { { - C::LOST_TAP, "lost_tap", - "a tap the game drops", - "the walker reads the player where the tap started", - {}, R::RETRY_STEP, + .failure_class = C::LOST_TAP, .id = "lost_tap", + .meaning = "a tap the game drops", + .detected_by = "the walker reads the player where the tap started", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::UNSEEN_STEP, "unseen_step", - "a step tap that moved the player, read as not moved", - "the walker finds the player a tile past where it read", - {}, R::RETRY_STEP, + .failure_class = C::UNSEEN_STEP, .id = "unseen_step", + .meaning = "a step tap that moved the player, read as not moved", + .detected_by = "the walker finds the player a tile past where it read", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::WALK_SHORT, "walk_short", - "a walk or hold ended short of its tile", - "the walker's position check at a wall or landmark", - {}, R::RETRY_STEP, + .failure_class = C::WALK_SHORT, .id = "walk_short", + .meaning = "a walk or hold ended short of its tile", + .detected_by = "the walker's position check at a wall or landmark", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::BLOCKED, "blocked", - "something on the way stops the walk: a person, a wall the map does not have", - "the walker's progress check: no tile gained", - {}, R::RETRY_STEP, + .failure_class = C::BLOCKED, .id = "blocked", + .meaning = "something on the way stops the walk: a person, a wall the map does not have", + .detected_by = "the walker's progress check: no tile gained", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::MOUNT_LOST, "mount_lost", - "off the Bicycle (or another mount) at a leg that rides", - "the walker's tiles per tap", - {}, R::RETRY_STEP, + .failure_class = C::MOUNT_LOST, .id = "mount_lost", + .meaning = "off the Bicycle (or another mount) at a leg that rides", + .detected_by = "the walker's tiles per tap", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::POSE_LOST, "pose_lost", - "where the player is or faces is unknown or not what the map says", - "the localizer: no pose, another map, another facing", - {}, R::RELOAD, + .failure_class = C::POSE_LOST, .id = "pose_lost", + .meaning = "where the player is or faces is unknown or not what the map says", + .detected_by = "the localizer: no pose, another map, another facing", + .outcomes = {}, .retry = R::RELOAD, }, }; } @@ -114,28 +114,28 @@ std::vector walk_rows(){ std::vector warp_rows(){ return { { - C::WARP_NO_OP, "warp_no_op", - "a step onto a warp shows the fade's black but does not warp", - "the map or tile after the fade is the one before it", - {}, R::RETRY_STEP, + .failure_class = C::WARP_NO_OP, .id = "warp_no_op", + .meaning = "a step onto a warp shows the fade's black but does not warp", + .detected_by = "the map or tile after the fade is the one before it", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::LATE_FADE, "late_fade", - "a warp's fade shows late or is missed", - "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", - {}, R::RETRY_STEP, + .failure_class = C::LATE_FADE, .id = "late_fade", + .meaning = "a warp's fade shows late or is missed", + .detected_by = "the fade watcher: the warp unconfirmed, the player on a map the walk did not plan", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::SLOW_FADE_IN, "slow_fade_in", - "a warp's fade-in runs long: a move right after it goes nowhere", - "the first move after a fade does not move the player", - {}, R::RETRY_STEP, + .failure_class = C::SLOW_FADE_IN, .id = "slow_fade_in", + .meaning = "a warp's fade-in runs long: a move right after it goes nowhere", + .detected_by = "the first move after a fade does not move the player", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::DARK_MAP, "dark_map", - "a dark map (a cave without Flash) the readers cannot see", - "the floor reads dim; the walker's and the scene's reads fail there", - {}, R::STOP, + .failure_class = C::DARK_MAP, .id = "dark_map", + .meaning = "a dark map (a cave without Flash) the readers cannot see", + .detected_by = "the floor reads dim; the walker's and the scene's reads fail there", + .outcomes = {}, .retry = R::STOP, }, }; } @@ -144,28 +144,28 @@ std::vector warp_rows(){ std::vector scene_rows(){ return { { - C::SCENE_END_POSE, "scene_end_pose", - "a step whose end the game sets leaves the player on a tile next to the expected one", - "the next walk starts or ends a tile off", - {}, R::RETRY_STEP, + .failure_class = C::SCENE_END_POSE, .id = "scene_end_pose", + .meaning = "a step whose end the game sets leaves the player on a tile next to the expected one", + .detected_by = "the next walk starts or ends a tile off", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::SCENE_EARLY, "scene_early", - "a scene starts on the last tiles of the walk before it", - "the walk is interrupted by the scene's dialogue", - {}, R::RETRY_STEP, + .failure_class = C::SCENE_EARLY, .id = "scene_early", + .meaning = "a scene starts on the last tiles of the walk before it", + .detected_by = "the walk is interrupted by the scene's dialogue", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::BATTLE_OPENING, "battle_opening", - "a battle's opening is still playing when a step starts", - "the step's first read is not the overworld or battle menu it expects", - {}, R::RETRY_STEP, + .failure_class = C::BATTLE_OPENING, .id = "battle_opening", + .meaning = "a battle's opening is still playing when a step starts", + .detected_by = "the step's first read is not the overworld or battle menu it expects", + .outcomes = {}, .retry = R::RETRY_STEP, }, { - C::WILD_BATTLES, "wild_battles", - "more wild battles than a clean run meets", - "the interrupt handler clears each; too many in a step fail it", - {"too_many_interrupts"}, R::RETRY_STEP, + .failure_class = C::WILD_BATTLES, .id = "wild_battles", + .meaning = "more wild battles than a clean run meets", + .detected_by = "the interrupt handler clears each; too many in a step fail it", + .outcomes = {"too_many_interrupts"}, .retry = R::RETRY_STEP, }, }; } @@ -174,46 +174,46 @@ std::vector scene_rows(){ std::vector check_rows(){ return { { - C::INTERRUPT_FAILED, "interrupt_failed", - "something interrupted a step and was not cleared", - "the interrupt handler: no handler, a failed clear, the screen not settled", - {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, R::RELOAD, + .failure_class = C::INTERRUPT_FAILED, .id = "interrupt_failed", + .meaning = "something interrupted a step and was not cleared", + .detected_by = "the interrupt handler: no handler, a failed clear, the screen not settled", + .outcomes = {"unhandled_interrupt", "interrupt_failed", "interrupt_unsettled", "interrupted"}, .retry = R::RELOAD, }, { - C::SCREEN_UNREAD, "screen_unread", - "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", - "the step's detector times out", - {"talk_no_text", "mash_no_overworld"}, R::RELOAD, + .failure_class = C::SCREEN_UNREAD, .id = "screen_unread", + .meaning = "a screen the step waits for (dialogue, banner, overworld, prompt) is not seen", + .detected_by = "the step's detector times out", + .outcomes = {"talk_no_text", "mash_no_overworld"}, .retry = R::RELOAD, }, { - C::MENU_FAILED, "menu_failed", - "a menu action (mart, PC, item, TM, Fly) did not do what it was to", - "the menu's readers: the item, count or choice not as planned", - {}, R::RELOAD, + .failure_class = C::MENU_FAILED, .id = "menu_failed", + .meaning = "a menu action (mart, PC, item, TM, Fly) did not do what it was to", + .detected_by = "the menu's readers: the item, count or choice not as planned", + .outcomes = {}, .retry = R::RELOAD, }, { - C::BATTLE_LOST, "battle_lost", - "a battle lost or not won: a blackout", - "the battle's end screen or the blackout's heal spot", - {}, R::RELOAD, + .failure_class = C::BATTLE_LOST, .id = "battle_lost", + .meaning = "a battle lost or not won: a blackout", + .detected_by = "the battle's end screen or the blackout's heal spot", + .outcomes = {}, .retry = R::RELOAD, }, { - C::STATE_CHECK_FAILED, "state_check_failed", - "a check of the game's state failed: an event's expects, the checkpoint's end", - "the event's expects and requires, the checkpoint's end and postconditions", - {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, R::RELOAD, + .failure_class = C::STATE_CHECK_FAILED, .id = "state_check_failed", + .meaning = "a check of the game's state failed: an event's expects, the checkpoint's end", + .detected_by = "the event's expects and requires, the checkpoint's end and postconditions", + .outcomes = {"expect_failed", "require_failed", "end_failed", "postconditions_failed"}, .retry = R::RELOAD, }, { - C::OPERATION_FAILED, "operation_failed", - "a game's action threw, with no class of its own", - "an OperationFailedException out of a step", - {"operation_failed"}, R::RELOAD, + .failure_class = C::OPERATION_FAILED, .id = "operation_failed", + .meaning = "a game's action threw, with no class of its own", + .detected_by = "an OperationFailedException out of a step", + .outcomes = {"operation_failed"}, .retry = R::RELOAD, }, { - C::SIM_MISMATCH, "sim_mismatch", - "a simulated run left the story: the sim and the table disagree", - "the story sim's end checks (sim runs only)", - {}, R::STOP, + .failure_class = C::SIM_MISMATCH, .id = "sim_mismatch", + .meaning = "a simulated run left the story: the sim and the table disagree", + .detected_by = "the story sim's end checks (sim runs only)", + .outcomes = {}, .retry = R::STOP, }, }; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp index 674c2b97cb..d8af0a9936 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -2,9 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * The list's own checks: every sim fault and every engine failure name - * maps to one class, and every class id reads back from the text a log - * line or run record writes. + * The list's own checks: every engine failure name maps to one class, and + * every class id reads back from the text a log line or run record writes. * */ @@ -75,8 +74,7 @@ class Test_OutcomeClass : public UnitTest{ using C = StoryFailureClass; -// Every failure name the engine's code returns (autostory/engine), and -// #477's launch_failed. +// Every failure name the story engine returns, and the game launch's. const OutcomeClassTestCase OUTCOME_CASES[] = { {.name = "HOME menu", .input = {"game_not_running"}, .expectation = {C::GAME_NOT_RUNNING}}, {.name = "launch", .input = {"launch_failed"}, .expectation = {C::LAUNCH_FAILED}}, @@ -110,36 +108,25 @@ const OutcomeClassTestCase OUTCOME_CASES[] = { }; -// A check over the whole list. -struct ListCheckTestCase{ - // The check, run over story_failure_classes(). - struct Input{ - UnitTestResult (*check)(); - }; - - // What holds when the check passes. - struct Expectation{ - std::string_view holds; - }; - - std::string name; - Input input; - Expectation expectation; +// A named check over the whole list. +struct ListCheck{ + std::string_view name; + UnitTestResult (*check)(); }; class Test_ListCheck : public UnitTest{ public: - explicit Test_ListCheck(ListCheckTestCase test_case) - : UnitTest(std::format("Pokemon::StoryFailureClasses - {}", test_case.name)) - , m_case(std::move(test_case)) + explicit Test_ListCheck(ListCheck check) + : UnitTest(std::format("Pokemon::StoryFailureClasses - {}", check.name)) + , m_check(check) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_case.input.check(); + return m_check.check(); } private: - const ListCheckTestCase m_case; + const ListCheck m_check; }; UnitTestResult rows_in_enum_order(){ @@ -232,14 +219,15 @@ UnitTestResult no_class_without_a_tag(){ return true; } -const ListCheckTestCase LIST_CHECKS[] = { - {.name = "rows in the enum's order", .input = {rows_in_enum_order}, .expectation = {"story_failure_class_info() indexes by the enum"}}, - {.name = "every class has a row", .input = {every_class_has_a_row}, .expectation = {"no class reads past the table"}}, - {.name = "no outcome name in two classes", .input = {no_outcome_in_two_classes}, .expectation = {"a failure name has one class"}}, - {.name = "ids round-trip", .input = {ids_round_trip}, .expectation = {"each id is unique and its tag alone reads back"}}, - {.name = "tags round-trip through a log line", .input = {tags_round_trip_through_a_log_line}, .expectation = {"the run log names the class"}}, - {.name = "tags round-trip through a run record", .input = {tags_round_trip_through_a_run_record}, .expectation = {"a run record names the class"}}, - {.name = "no class without a tag", .input = {no_class_without_a_tag}, .expectation = {"untagged or unknown text has no class"}}, +const ListCheck LIST_CHECKS[] = { + // story_failure_class_info() indexes by the enum. + {.name = "rows in the enum's order", .check = rows_in_enum_order}, + {.name = "every class has a row", .check = every_class_has_a_row}, + {.name = "no outcome name in two classes", .check = no_outcome_in_two_classes}, + {.name = "ids round-trip", .check = ids_round_trip}, + {.name = "tags round-trip through a log line", .check = tags_round_trip_through_a_log_line}, + {.name = "tags round-trip through a run record", .check = tags_round_trip_through_a_run_record}, + {.name = "no class without a tag", .check = no_class_without_a_tag}, }; } // namespace @@ -249,8 +237,8 @@ void add_tests_StoryFailureClasses(UnitTestDatabase& database){ for (const OutcomeClassTestCase& test_case : OUTCOME_CASES){ database.add(test_case); } - for (const ListCheckTestCase& test_case : LIST_CHECKS){ - database.add(test_case); + for (const ListCheck& check : LIST_CHECKS){ + database.add(check); } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp index 5fed3725bf..91b35e5257 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -21,8 +21,8 @@ std::string resets_text(size_t attempt){ } // "[Checkpoint] 00.04", or the game's own checkpoint text unchanged. -std::string checkpoint_prefix(std::string_view checkpoint, bool add_checkpoint_prefix){ - if (!add_checkpoint_prefix){ +std::string checkpoint_prefix(std::string_view checkpoint, CheckpointPrefix prefix_kind){ + if (prefix_kind == CheckpointPrefix::AS_GIVEN){ return std::string(checkpoint); } return std::format("[Checkpoint] {}", checkpoint); @@ -32,9 +32,9 @@ std::string checkpoint_prefix(std::string_view checkpoint, bool add_checkpoint_p std::string checkpoint_start_line( - std::string_view checkpoint, std::optional events, bool add_checkpoint_prefix + std::string_view checkpoint, std::optional events, CheckpointPrefix prefix_kind ){ - const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + const std::string prefix = checkpoint_prefix(checkpoint, prefix_kind); if (!events){ return std::format("{}: start", prefix); } @@ -42,26 +42,26 @@ std::string checkpoint_start_line( } std::string checkpoint_attempt_line( - std::string_view checkpoint, size_t attempt, std::optional attempts, bool add_checkpoint_prefix + std::string_view checkpoint, size_t attempt, std::optional attempts, CheckpointPrefix prefix_kind ){ - const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + const std::string prefix = checkpoint_prefix(checkpoint, prefix_kind); if (!attempts){ return std::format("{}: attempt {}", prefix, attempt); } return std::format("{}: attempt {}/{}", prefix, attempt, *attempts); } -std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, bool add_checkpoint_prefix){ +std::string checkpoint_pass_line(std::string_view checkpoint, size_t attempt, CheckpointPrefix prefix_kind){ return std::format( "{}: passed on attempt {} ({})", - checkpoint_prefix(checkpoint, add_checkpoint_prefix), attempt, resets_text(attempt) + checkpoint_prefix(checkpoint, prefix_kind), attempt, resets_text(attempt) ); } std::string checkpoint_reset_line( - std::string_view checkpoint, size_t next_attempt, std::optional attempts, bool add_checkpoint_prefix + std::string_view checkpoint, size_t next_attempt, std::optional attempts, CheckpointPrefix prefix_kind ){ - const std::string prefix = checkpoint_prefix(checkpoint, add_checkpoint_prefix); + const std::string prefix = checkpoint_prefix(checkpoint, prefix_kind); if (!attempts){ return std::format("{}: failed, resetting for attempt {}", prefix, next_attempt); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h index 11bfb009d2..b19a638cc0 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -26,9 +26,13 @@ namespace Pokemon{ // The lines below start with "[Checkpoint] " and then `checkpoint`. A game // whose checkpoint text already says it is a checkpoint (SV's -// "Checkpoint:: 004 - 02 ...") passes that text as `checkpoint` and -// `add_checkpoint_prefix` false, and the line starts with the text alone: +// "Checkpoint:: 004 - 02 ...") passes that text as `checkpoint` with +// AS_GIVEN, and the line starts with the text alone: // Checkpoint:: 004 - 02 ...: start +enum class CheckpointPrefix{ + ADD, + AS_GIVEN, +}; // A checkpoint starts; the story engine also says it has `events` events: // [Checkpoint] 00.04: start @@ -36,7 +40,7 @@ namespace Pokemon{ std::string checkpoint_start_line( std::string_view checkpoint, std::optional events = std::nullopt, - bool add_checkpoint_prefix = true + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD ); // An attempt at a checkpoint starts, counting from 1, out of the @@ -47,7 +51,7 @@ std::string checkpoint_attempt_line( std::string_view checkpoint, size_t attempt, std::optional attempts = std::nullopt, - bool add_checkpoint_prefix = true + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD ); // A checkpoint passed on attempt `attempt`, counting from 1; every attempt @@ -57,7 +61,7 @@ std::string checkpoint_attempt_line( std::string checkpoint_pass_line( std::string_view checkpoint, size_t attempt, - bool add_checkpoint_prefix = true + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD ); // An attempt failed and the game resets to the checkpoint's save for @@ -68,7 +72,7 @@ std::string checkpoint_reset_line( std::string_view checkpoint, size_t next_attempt, std::optional attempts = std::nullopt, - bool add_checkpoint_prefix = true + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD ); // The full lines above go to the log. The video overlay, which wraps its diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp index 759ed9b3d7..e30ad0dc62 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -20,12 +20,12 @@ namespace{ // One formatter call and the line it must make. struct StoryLogTestCase{ - // A formatter's line, made when the table is built. + // The formatter call, made when the test runs. struct Input{ - std::string line; + std::string (*format)(); }; - // The exact line: the lab watcher and the engine tests read these. + // The exact line: log readers and the engine tests match these. struct Expectation{ std::string line; }; @@ -43,8 +43,9 @@ class Test_StoryLog : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - if (m_case.input.line != m_case.expectation.line){ - return std::format("Expected \"{}\", got \"{}\"", m_case.expectation.line, m_case.input.line); + const std::string line = m_case.input.format(); + if (line != m_case.expectation.line){ + return std::format("Expected \"{}\", got \"{}\"", m_case.expectation.line, line); } return true; } @@ -59,94 +60,94 @@ constexpr std::string_view SV_TEXT = "Checkpoint:: 004 - 02. Start point: X"; const StoryLogTestCase STORY_LOG_CASES[] = { { .name = "checkpoint start", - .input = {.line = checkpoint_start_line("00.04")}, + .input = {.format = []{ return checkpoint_start_line("00.04"); }}, .expectation = {.line = "[Checkpoint] 00.04: start"}, }, { .name = "checkpoint start with its events", - .input = {.line = checkpoint_start_line("00.04", /*events=*/3)}, + .input = {.format = []{ return checkpoint_start_line("00.04", /*events=*/3); }}, .expectation = {.line = "[Checkpoint] 00.04: start, 3 events"}, }, { .name = "attempt", - .input = {.line = checkpoint_attempt_line("00.04", 2)}, + .input = {.format = []{ return checkpoint_attempt_line("00.04", 2); }}, .expectation = {.line = "[Checkpoint] 00.04: attempt 2"}, }, { .name = "attempt out of a limit", - .input = {.line = checkpoint_attempt_line("00.04", 2, /*attempts=*/3)}, + .input = {.format = []{ return checkpoint_attempt_line("00.04", 2, /*attempts=*/3); }}, .expectation = {.line = "[Checkpoint] 00.04: attempt 2/3"}, }, { .name = "one-shot pass", - .input = {.line = checkpoint_pass_line("00.03", 1)}, + .input = {.format = []{ return checkpoint_pass_line("00.03", 1); }}, .expectation = {.line = "[Checkpoint] 00.03: passed on attempt 1 (one-shot)"}, }, { .name = "pass after one reset", - .input = {.line = checkpoint_pass_line("00.04", 2)}, + .input = {.format = []{ return checkpoint_pass_line("00.04", 2); }}, .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}, }, { .name = "pass after resets", - .input = {.line = checkpoint_pass_line("00.04", 3)}, + .input = {.format = []{ return checkpoint_pass_line("00.04", 3); }}, .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets)"}, }, { .name = "reset", - .input = {.line = checkpoint_reset_line("00.04", 2)}, + .input = {.format = []{ return checkpoint_reset_line("00.04", 2); }}, .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2"}, }, { .name = "reset out of a limit", - .input = {.line = checkpoint_reset_line("00.04", 2, /*attempts=*/3)}, + .input = {.format = []{ return checkpoint_reset_line("00.04", 2, /*attempts=*/3); }}, .expectation = {.line = "[Checkpoint] 00.04: failed, resetting for attempt 2/3"}, }, { .name = "start without the checkpoint prefix", - .input = {.line = checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .input = {.format = []{ return checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, CheckpointPrefix::AS_GIVEN); }}, .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, }, { .name = "attempt without the checkpoint prefix", - .input = {.line = checkpoint_attempt_line(SV_TEXT, 2, /*attempts=*/100, /*add_checkpoint_prefix=*/false)}, + .input = {.format = []{ return checkpoint_attempt_line(SV_TEXT, 2, /*attempts=*/100, CheckpointPrefix::AS_GIVEN); }}, .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, }, { .name = "pass without the checkpoint prefix", - .input = {.line = checkpoint_pass_line(SV_TEXT, 2, /*add_checkpoint_prefix=*/false)}, + .input = {.format = []{ return checkpoint_pass_line(SV_TEXT, 2, CheckpointPrefix::AS_GIVEN); }}, .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: passed on attempt 2 (1 reset)"}, }, { .name = "reset without the checkpoint prefix", - .input = {.line = checkpoint_reset_line(SV_TEXT, 2, /*attempts=*/std::nullopt, /*add_checkpoint_prefix=*/false)}, + .input = {.format = []{ return checkpoint_reset_line(SV_TEXT, 2, /*attempts=*/std::nullopt, CheckpointPrefix::AS_GIVEN); }}, .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: failed, resetting for attempt 2"}, }, { .name = "short checkpoint start", - .input = {.line = story_short_line("[Checkpoint] 12.03: start, 18 events")}, + .input = {.format = []{ return story_short_line("[Checkpoint] 12.03: start, 18 events"); }}, .expectation = {.line = "12.03: start, 18 events"}, }, { .name = "short line with another tag", - .input = {.line = story_short_line("[Tag] 12.03: some text")}, + .input = {.format = []{ return story_short_line("[Tag] 12.03: some text"); }}, .expectation = {.line = "12.03: some text"}, }, { .name = "short line without the checkpoint prefix", - .input = {.line = story_short_line( - checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, /*add_checkpoint_prefix=*/false) - )}, + .input = {.format = []{ return story_short_line( + checkpoint_start_line(SV_TEXT, /*events=*/std::nullopt, CheckpointPrefix::AS_GIVEN) + ); }}, .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: start"}, }, { .name = "short line with an unclosed tag", - .input = {.line = story_short_line("[Tag 12.03")}, + .input = {.format = []{ return story_short_line("[Tag 12.03"); }}, .expectation = {.line = "[Tag 12.03"}, }, { .name = "short empty line", - .input = {.line = story_short_line("")}, + .input = {.format = []{ return story_short_line(""); }}, .expectation = {.line = ""}, }, }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.cpp new file mode 100644 index 0000000000..4442a361f9 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.cpp @@ -0,0 +1,50 @@ +/* Story Outcome + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +bool Outcome::succeeded() const{ + return failure.empty(); +} + +Outcome outcome_ok(std::optional pose){ + Outcome outcome; + outcome.pose = std::move(pose); + return outcome; +} + +Outcome outcome_unchanged(){ + Outcome outcome; + outcome.changed_nothing = true; + return outcome; +} + +Outcome outcome_failed(std::string failure, std::string detail, std::optional failure_class){ + Outcome outcome; + outcome.failure = std::move(failure); + outcome.detail = std::move(detail); + outcome.failure_class = failure_class; + return outcome; +} + +std::string to_string(const Outcome& outcome){ + return outcome.detail.empty() ? outcome.failure : std::format("{}: {}", outcome.failure, outcome.detail); +} + +Outcome outcome_game_not_running(){ + return outcome_failed(std::string(GAME_NOT_RUNNING), "the game is not running: the Switch HOME menu is showing"); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h index 66a1e1a083..316fe4276b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h @@ -2,23 +2,19 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_StoryEvents.h, unchanged; it includes this header. + * How a story action or check ended: success, or a named failure with its + * class, and the pose the action left the player in when it knows it. * */ #ifndef PokemonAutomation_Pokemon_StoryOutcome_H #define PokemonAutomation_Pokemon_StoryOutcome_H -#include -#include #include -#include #include #include -#include #include "Common/Cpp/MoveOnlyFunction.h" #include "Pokemon_StoryFailureClasses.h" -#include "Pokemon_StoryLog.h" #include "Pokemon_StoryPose.h" namespace PokemonAutomation{ @@ -44,11 +40,11 @@ struct Outcome{ bool succeeded() const; }; -// Constructs a successful event outcome, updating the run pose when given one. +// Success; with a pose, the runner moves the run's pose to it. Outcome outcome_ok(std::optional pose = std::nullopt); // outcome_ok() with `changed_nothing`. Outcome outcome_unchanged(); -// Constructs a failed event outcome with reason and optional diagnostic detail. +// A failure named `failure`, with what was seen in `detail`. // `failure_class` is for a name the shared list does not class (a game's // own); without it the list's class for `failure` holds. Outcome outcome_failed( @@ -66,7 +62,7 @@ inline constexpr std::string_view GAME_NOT_RUNNING = "game_not_running"; Outcome outcome_game_not_running(); -// Callable action executed for a story event. +// What an event does; it may move the run's pose. using Act = MoveOnlyFunction; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp new file mode 100644 index 0000000000..d075cf4e40 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp @@ -0,0 +1,59 @@ +/* Story Pose + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryPose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string certainty_name(PoseCertainty certainty){ + switch (certainty){ + case PoseCertainty::TILE: + return "tile"; + case PoseCertainty::COLUMN: + return "column"; + case PoseCertainty::ROW: + return "row"; + case PoseCertainty::SET: + return "set"; + case PoseCertainty::UNKNOWN: + return "unknown"; + } + return "?"; +} + +bool certainty_covers(PoseCertainty have, PoseCertainty need){ + switch (need){ + case PoseCertainty::UNKNOWN: + return true; + case PoseCertainty::SET: + return have != PoseCertainty::UNKNOWN; + case PoseCertainty::COLUMN: + case PoseCertainty::ROW: + return have == need || have == PoseCertainty::TILE; + case PoseCertainty::TILE: + return have == PoseCertainty::TILE; + } + return false; +} + +std::string to_string(const Pose& pose){ + std::string text = pose.map.empty() ? "?" : pose.map; + if (pose.where){ + const std::string where = pose.where->describe(); + if (!where.empty()){ + text += std::format(" {}", where); + } + } + return std::format("{} ({})", text, certainty_name(pose.certainty)); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h index b9c0eb53db..7ea985629b 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPose.h @@ -2,22 +2,16 @@ * * From: https://github.com/PokemonAutomation/ * - * Split out of Pokemon_StoryEvents.h, unchanged; it includes this header. + * Where the player is believed to be, and how sure the run is of it. * */ #ifndef PokemonAutomation_Pokemon_StoryPose_H #define PokemonAutomation_Pokemon_StoryPose_H -#include #include -#include #include #include -#include -#include -#include "Common/Cpp/MoveOnlyFunction.h" -#include "Pokemon_StoryLog.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -32,14 +26,14 @@ enum class PoseCertainty{ SET, UNKNOWN, }; -// Returns the string label for a pose certainty level. +// "tile", "column", ...: for log lines. std::string certainty_name(PoseCertainty certainty); // Whether a pose known to `have` is good enough for an event that needs `need`. bool certainty_covers(PoseCertainty have, PoseCertainty need); // Where on a map the player is, in the game's own terms: tiles and a -// facing in a 2D game (Pokemon_TilePose.h), a map point or a fly point in +// facing in a 2D game, a map point or a fly point in // an open-world one. The engine only carries and prints it; the game's // actions read it back. struct Position{ @@ -61,7 +55,7 @@ struct Pose{ PoseCertainty certainty = PoseCertainty::UNKNOWN; std::shared_ptr where; }; -// Formats a Pose as a human-readable string. +// "Route 201 (10, 20) facing north (tile)": for log lines. std::string to_string(const Pose& pose); diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f6154ee425..894ef2df9e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1457,7 +1457,9 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryLog.h Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h + Source/Pokemon/AutoStory/Pokemon_StoryOutcome.cpp Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h + Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp Source/Pokemon/AutoStory/Pokemon_StoryPose.h Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp