diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.cpp new file mode 100644 index 0000000000..46a7aae831 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.cpp @@ -0,0 +1,116 @@ +/* Story Checkpoint JSON + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonArray.h" +#include "Pokemon_StoryCheckpointJson.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// Throws ParseException naming the first of `keys` missing from `object`. +void require_keys(const JsonObject& object, std::initializer_list keys){ + for (std::string_view key : keys){ + object.get_value_throw(std::string(key)); + } +} + +// A step, parsed by its action among `actions`. +StoryStep read_story_step(const JsonValue& value, StoryActions& actions){ + const JsonObject& root = value.to_object_throw(); + reject_unknown_keys(root, {"id", "why", "action", "args"}); + StoryStep result; + result.name = root.get_string_throw("id"); + if (result.name.empty()){ + throw ParseException("empty step id"); + } + if (const JsonValue* field = root.get_value("why")){ + result.why = field->to_string_throw(); + } + const std::string& action = root.get_string_throw("action"); + result.action = action; + const auto parser = actions.find(action); + if (parser == actions.end()){ + throw ParseException(std::format("step {}: unknown action \"{}\"", result.name, action)); + } + const JsonObject no_args; + const JsonValue* args = root.get_value("args"); + try{ + result.run = parser->second(args != nullptr ? args->to_object_throw() : no_args); + }catch (const ParseException& error){ + throw ParseException(std::format("step {}: {}", result.name, error.message())); + } + return result; +} + +// The checkpoint object of a file, each step parsed by its action. +StoryCheckpoint read_checkpoint_object(const JsonValue& value, StoryActions& actions){ + const JsonObject& root = value.to_object_throw(); + reject_unknown_keys(root, {"id", "title", "start", "events", "metadata"}); + StoryCheckpoint result; + result.id = root.get_string_throw("id"); + if (result.id.empty()){ + throw ParseException("empty checkpoint id"); + } + result.title = root.get_string_throw("title"); + std::set names; + for (const JsonValue& event : root.get_array_throw("events")){ + StoryStep step = read_story_step(event, actions); + if (!names.insert(step.name).second){ + throw ParseException(std::format("duplicate step id {}", step.name)); + } + result.steps.push_back(std::move(step)); + } + return result; +} + + +} // namespace + + +void reject_unknown_keys(const JsonObject& object, std::initializer_list keys){ + for (const auto& [key, unused] : object){ + bool known = false; + for (std::string_view allowed : keys){ + known |= key == allowed; + } + if (!known){ + throw ParseException(std::format("unknown field \"{}\"", key)); + } + } +} + +StoryCheckpoint read_story_checkpoint( + const JsonValue& file, std::string_view game, std::string_view source, StoryActions& actions +){ + try{ + const JsonObject& root = file.to_object_throw(); + reject_unknown_keys(root, {"format", "game", "checkpoint", "execution"}); + require_keys(root, {"format", "game", "checkpoint", "execution"}); + if (root.get_string_throw("format") != "pokesim-authored-checkpoint/1"){ + throw ParseException("unsupported format"); + } + if (game.empty() || root.get_string_throw("game") != game){ + throw ParseException(std::format("not a {} checkpoint", game)); + } + root.get_object_throw("execution"); + return read_checkpoint_object(root.get_value_throw("checkpoint"), actions); + }catch (const ParseException& error){ + throw ParseException(std::format("{}: {}", source, error.message())); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h new file mode 100644 index 0000000000..0275378110 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h @@ -0,0 +1,52 @@ +/* Story Checkpoint JSON + * + * From: https://github.com/PokemonAutomation/ + * + * Reads a story checkpoint file into steps ready to run. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpointJson_H +#define PokemonAutomation_Pokemon_StoryCheckpointJson_H + +#include +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Common/Cpp/Json/JsonObject.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Parses a step's "args" into its typed args and returns the step's run +// with them bound; throws ParseException on a bad arg. +using StoryActionParser = MoveOnlyFunction; + +// A game's actions by the name a step's "action" gives. +using StoryActions = std::map>; + +// Throws ParseException naming the first key of `object` not in `keys`. +// Only an action knows its args, so each parser checks its own with this. +void reject_unknown_keys(const JsonObject& object, std::initializer_list keys); + +// The checkpoint of a checkpoint file of `game`: +// {"format": "pokesim-authored-checkpoint/1", "game": ..., +// "checkpoint": {"id", "title", "events": [steps]}, "execution": {...}} +// Each step is parsed by its action among `actions`. Throws ParseException +// naming `source` on anything unknown, mistyped or missing. "execution" is +// the game's to read from `file`; the checkpoint's "start" and "metadata" +// are notes for the plan's readers and are not read. +StoryCheckpoint read_story_checkpoint( + const JsonValue& file, std::string_view game, std::string_view source, StoryActions& actions +); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.cpp new file mode 100644 index 0000000000..f9d837833c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.cpp @@ -0,0 +1,276 @@ +/* Story Checkpoint JSON Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Each case reads a checkpoint file through read_story_checkpoint() with a + * test game's actions and runs its steps on a FakeStoryBackend, or expects + * the read to fail. + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryCheckpointJson.h" +#include "Pokemon_StoryCheckpointJson_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// A "test" game's file around the checkpoint `checkpoint`. +std::string test_file(std::string_view checkpoint){ + return std::format( + R"({{"format": "pokesim-authored-checkpoint/1", "game": "test", "checkpoint": {}, "execution": {{}}}})", + checkpoint + ); +} + +struct ReadCase{ + std::string name; + struct Input{ + // A checkpoint file's JSON, of the "test" game. + std::string json; + }; + struct Expectation{ + // The lines running every step logs, joined by " | "; none: the + // read fails. + std::optional log = std::nullopt; + }; + Input input; + Expectation expectation; +}; + +const ReadCase CASES[] = { + { + .name = "a parsed step runs its handler with typed args", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "buy", "action": "mart", "args": {"map": "M", "budget": 3000}}] + })")}, + .expectation = {.log = "mart M 3000"}, + }, + { + .name = "steps run in plan order", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [ + {"id": "a", "action": "talk"}, + {"id": "b", "action": "mart", "args": {"map": "M", "budget": 1}} + ] + })")}, + .expectation = {.log = "talk | mart M 1"}, + }, + { + .name = "start and metadata are notes, not read", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "start": {"tiles": [[1, 2]]}, + "metadata": {"phase": "tested"}, + "events": [{"id": "a", "action": "talk"}] + })")}, + .expectation = {.log = "talk"}, + }, + { + .name = "the execution object is left to the game", + .input = {.json = R"({ + "format": "pokesim-authored-checkpoint/1", + "game": "test", + "checkpoint": {"id": "x", "title": "X", "events": [{"id": "a", "action": "talk"}]}, + "execution": {"zero": 0, "false": false, "null": null} + })"}, + .expectation = {.log = "talk"}, + }, + { + .name = "an unknown arg key is rejected", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "a", "action": "mart", "args": {"map": "M", "budget": 1, "budgte": 1}}] + })")}, + .expectation = {}, + }, + { + .name = "a missing required arg is rejected", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "a", "action": "mart", "args": {"map": "M"}}] + })")}, + .expectation = {}, + }, + { + .name = "an unknown action is rejected", + .input = {.json = test_file(R"({"id": "x", "title": "X", "events": [{"id": "a", "action": "dance"}]})")}, + .expectation = {}, + }, + { + .name = "a step without an action is rejected", + .input = {.json = test_file(R"({"id": "x", "title": "X", "events": [{"id": "a"}]})")}, + .expectation = {}, + }, + { + .name = "the old kind field is rejected", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "a", "kind": "talk", "action": "talk"}] + })")}, + .expectation = {}, + }, + { + .name = "args that are not an object are rejected", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "a", "action": "mart", "args": ["map"]}] + })")}, + .expectation = {}, + }, + { + .name = "duplicate step ids are rejected", + .input = {.json = test_file(R"({ + "id": "x", "title": "X", + "events": [{"id": "a", "action": "talk"}, {"id": "a", "action": "talk"}] + })")}, + .expectation = {}, + }, + { + .name = "another game's file is rejected", + .input = {.json = R"({ + "format": "pokesim-authored-checkpoint/1", + "game": "frlg", + "checkpoint": {"id": "x", "title": "X", "events": []}, + "execution": {} + })"}, + .expectation = {}, + }, + { + .name = "an unknown format is rejected", + .input = {.json = R"({ + "format": "future", + "game": "test", + "checkpoint": {"id": "x", "title": "X", "events": []}, + "execution": {} + })"}, + .expectation = {}, + }, + { + .name = "a file without execution is rejected", + .input = {.json = R"({ + "format": "pokesim-authored-checkpoint/1", + "game": "test", + "checkpoint": {"id": "x", "title": "X", "events": []} + })"}, + .expectation = {}, + }, + { + .name = "an extra file field is rejected", + .input = {.json = R"({ + "format": "pokesim-authored-checkpoint/1", + "game": "test", + "checkpoint": {"id": "x", "title": "X", "events": []}, + "execution": {}, + "extra": 0 + })"}, + .expectation = {}, + }, + { + .name = "an execution that is not an object is rejected", + .input = {.json = R"({ + "format": "pokesim-authored-checkpoint/1", + "game": "test", + "checkpoint": {"id": "x", "title": "X", "events": []}, + "execution": false + })"}, + .expectation = {}, + }, +}; + + +// A test game's actions: "talk" takes no args; "mart" needs a map and a +// budget. Each logs its name and args. +StoryActions test_actions(){ + StoryActions actions; + actions.emplace("talk", [](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, {}); + return [](StoryBackend& backend, RunState&){ + backend.log("talk"); + return std::optional(); + }; + }); + actions.emplace("mart", [](const JsonObject& args) -> StoryStepRun{ + reject_unknown_keys(args, {"map", "budget"}); + return [map = args.get_string_throw("map"), budget = args.get_integer_throw("budget")](StoryBackend& backend, RunState&){ + backend.log(std::format("mart {} {}", map, budget)); + return std::optional(); + }; + }); + return actions; +} + +// The lines running every step of the file `json` logs, joined by " | "; +// none when the read fails. +std::optional run_log(std::string_view json, CancellableScope& scope){ + StoryActions actions = test_actions(); + StoryCheckpoint checkpoint; + try{ + checkpoint = read_story_checkpoint(parse_json(std::string(json)), "test", "test.json", actions); + }catch (const ParseException&){ + return std::nullopt; + } + FakeStoryBackend backend({}, scope); + RunState state; + for (StoryStep& step : checkpoint.steps){ + step.run(backend, state); + } + std::string log; + for (const std::string& line : backend.lines()){ + log += (log.empty() ? "" : " | ") + line; + } + return log; +} + + +class StoryCheckpointJsonTest : public UnitTest{ +public: + explicit StoryCheckpointJsonTest(ReadCase test) + : UnitTest(std::format("Pokemon::StoryCheckpointJson - {}", test.name)) + , m_case(std::move(test)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::optional log = run_log(m_case.input.json, scope); + if (log == m_case.expectation.log){ + return true; + } + return std::format( + "expected {}, got {}", + m_case.expectation.log ? std::format("\"{}\"", *m_case.expectation.log) : "a rejected read", + log ? std::format("\"{}\"", *log) : "a rejected read" + ); + } + +private: + ReadCase m_case; +}; + + +} // namespace + + +void add_tests_StoryCheckpointJson(UnitTestDatabase& database){ + for (const ReadCase& test : CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.h new file mode 100644 index 0000000000..4df38452f2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.h @@ -0,0 +1,23 @@ +/* Story Checkpoint JSON Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpointJson_Tests_H +#define PokemonAutomation_Pokemon_StoryCheckpointJson_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the checkpoint file reader's tests. +void add_tests_StoryCheckpointJson(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp new file mode 100644 index 0000000000..212d51a1b8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp @@ -0,0 +1,40 @@ +/* Screen Observation + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_ScreenObservation.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +ScreenObservation screen_observation(const ScreenEvents& events){ + // The events the story engine reads; the rest are left out. + constexpr ScreenEvent ENGINE_EVENTS[] = { + ScreenEvent::FADE_BLACK, + ScreenEvent::FADE_WHITE, + ScreenEvent::DIALOG_BOX, + ScreenEvent::BATTLE_STARTED, + ScreenEvent::PLAYER_CAN_MOVE, + ScreenEvent::SCREEN_STILL, + ScreenEvent::CHOICE_BOX, + ScreenEvent::INTERACT_PROMPT, + }; + ScreenObservation look; + for (ScreenEvent event : ENGINE_EVENTS){ + if (events.has(event)){ + look.events.set(event, true); + } + } + if (events.has(ScreenEvent::CHOICE_BOX)){ + look.events.set(ScreenEvent::DIALOG_BOX, true); + } + return look; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h new file mode 100644 index 0000000000..3d2bb56e6f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h @@ -0,0 +1,29 @@ +/* Screen Observation + * + * From: https://github.com/PokemonAutomation/ + * + * Turns what a console's screen detectors saw in one frame (ScreenEvents) + * into the look a story backend returns (ScreenObservation). + * + */ + +#ifndef PokemonAutomation_Pokemon_ScreenObservation_H +#define PokemonAutomation_Pokemon_ScreenObservation_H + +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "CommonTools/ScreenEvents/ScreenEventTypes.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The look for one frame's detected `events`. It keeps only the events +// story steps read; LOCATION_BANNER, BATTLE_ENDED and SWITCH_HOME_MENU are +// dropped. A choice box sits inside a dialog box, so CHOICE_BOX also sets +// DIALOG_BOX. The caller fills in the time and the frame. +ScreenObservation screen_observation(const ScreenEvents& events); + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h new file mode 100644 index 0000000000..45b0646b30 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h @@ -0,0 +1,53 @@ +/* Story Backend + * + * From: https://github.com/PokemonAutomation/ + * + * The story engine's input and observation: a console, a simulation or a + * scripted fake. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryBackend_H +#define PokemonAutomation_Pokemon_StoryBackend_H + +#include +#include "Common/Cpp/CancellableScope.h" +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How fast a held direction moves the player. +enum class StoryPace{ + WALK, + RUN, +}; + +class StoryBackend{ +public: + virtual ~StoryBackend() = default; + + virtual void press(NintendoSwitch::Button button, Milliseconds hold, Milliseconds release) = 0; + // The move stick held at `degrees` clockwise from north (straight on, + // for a camera-relative game) for `duration`, then let go for `release` + // (a turn's time to finish) like press(): the games' walk and turn. + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace) = 0; + virtual void wait(Milliseconds duration) = 0; + // The game now, once every input sent so far has been done. + virtual ScreenObservation observe() = 0; + virtual void log(std::string_view line) = 0; + // A line for the file log only, not the stream's overlay. + virtual void log_detail(std::string_view line){ + log(line); + } + virtual CancellableScope& scope() = 0; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp new file mode 100644 index 0000000000..dfb1f9835a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp @@ -0,0 +1,116 @@ +/* Story Backend Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_ScreenObservation.h" +#include "Pokemon_StoryBackend.h" +#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryBackend_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +using namespace std::chrono_literals; + + +// A look at one piece: what it gives as text. +struct BackendCase{ + std::string name = {}; + struct Input{ + std::function read; + }; + Input input = {}; + struct Expectation{ + std::string text; + }; + Expectation expectation = {}; +}; + +std::string observation_text(ScreenEvents events){ + return std::format("{}", screen_observation(events).events); +} + +// The scripted backend's inputs and looks. +std::string fake_text(CancellableScope& scope){ + ScreenObservation fade; + fade.events = {ScreenEvent::FADE_WHITE}; + FakeStoryBackend backend({.looks = {ScreenObservation{}, fade}}, scope); + backend.press(NintendoSwitch::BUTTON_A, /*hold=*/80ms, /*release=*/80ms); + backend.stick(/*degrees=*/0, /*duration=*/400ms, /*release=*/0ms, StoryPace::RUN); + backend.stick(/*degrees=*/90, /*duration=*/150ms, /*release=*/300ms, StoryPace::WALK); + backend.wait(100ms); + std::string looks; + for (size_t c = 0; c < 3; c++){ + looks += std::format("{}{}", looks.empty() ? "" : " / ", backend.observe().events); + } + std::string text; + for (const std::string& input : backend.inputs()){ + text += std::format("{}, ", input); + } + return text + looks; +} + +const BackendCase CASES[] = { + { + .name = "a look keeps the engine's events", + .input = {.read = [](CancellableScope&){ + return observation_text({ScreenEvent::FADE_WHITE, ScreenEvent::LOCATION_BANNER, ScreenEvent::SWITCH_HOME_MENU}); + }}, + .expectation = {.text = "white fade"}, + }, + { + .name = "a choice box in a look is also a dialog box", + .input = {.read = [](CancellableScope&){ return observation_text({ScreenEvent::CHOICE_BOX}); }}, + .expectation = {.text = "dialog box, choice box"}, + }, + { + .name = "the scripted backend records inputs and repeats its last look", + .input = {.read = fake_text}, + .expectation = {.text = "press A, stick 0deg 400ms run, stick 90deg 150ms walk, release 300ms, wait 100ms, " + "nothing / white fade / white fade"}, + }, +}; + + +class StoryBackendTest : public UnitTest{ +public: + explicit StoryBackendTest(BackendCase test_case) + : UnitTest(std::format("Pokemon::StoryBackend - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::string got = m_case.input.read(scope); + if (got != m_case.expectation.text){ + return std::format("expected \"{}\", got \"{}\"", m_case.expectation.text, got); + } + return true; + } + +private: + BackendCase m_case; +}; + +} // namespace + + +void add_tests_StoryBackend(UnitTestDatabase& database){ + for (const BackendCase& test : CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.h new file mode 100644 index 0000000000..82a63dbbbc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.h @@ -0,0 +1,23 @@ +/* Story Backend Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryBackend_Tests_H +#define PokemonAutomation_Pokemon_StoryBackend_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the backend's pieces: a look from screen events, zone pose matching and the scripted backend. +void add_tests_StoryBackend(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp new file mode 100644 index 0000000000..8a278a9496 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -0,0 +1,138 @@ +/* Story Engine + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon/AutoStory/Pokemon_StoryLog.h" +#include "Pokemon_StoryEngine.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// The text of the exception being handled. +std::string current_exception_text(){ + try{ + throw; + }catch (const Exception& e){ + return e.message(); + }catch (std::exception& e){ + return e.what(); + }catch (...){ + return "unknown exception"; + } +} + +// Runs `step`, step `number` (from 1) of checkpoint `checkpoint_id`; a +// throw is logged and passes through. +std::optional run_step( + std::string_view checkpoint_id, size_t number, StoryBackend& backend, StoryStep& step, RunState& state +){ + backend.log(event_line(checkpoint_id, number, step.name, "start")); + state.step = std::format("{}/{}", checkpoint_id, step.name); + std::optional failure; + try{ + backend.scope().throw_if_cancelled(); + failure = step.run(backend, state); + }catch (...){ + const std::string what = current_exception_text(); + backend.log(event_line(checkpoint_id, number, step.name, std::format("failed: {}", what))); + backend.log(checkpoint_failed_line(checkpoint_id, number, step.name, what)); + throw; + } + backend.log(event_line( + checkpoint_id, number, step.name, failure ? std::format("failed: {}", *failure) : "done" + )); + return failure; +} + +// One attempt: the steps in order until one fails, then the save when +// they passed. +std::optional run_once( + StoryBackend& backend, StoryCheckpoint& checkpoint, StoryGame& game, RunState& state +){ + const std::string& id = checkpoint.id; + backend.log(checkpoint_start_line(id, checkpoint.steps.size())); + for (size_t c = 0; c < checkpoint.steps.size(); c++){ + std::optional failure = run_step(id, c + 1, backend, checkpoint.steps[c], state); + if (failure){ + backend.log(checkpoint_failed_line(id, c + 1, checkpoint.steps[c].name, std::format("{}", *failure))); + return CheckpointFailure{.checkpoint_id = id, .event_index = c, .failure = std::move(*failure)}; + } + } + backend.log(checkpoint_done_line(id, checkpoint.steps.size())); + if (game.save){ + if (const std::optional failure = game.save(backend, checkpoint)){ + backend.log(save_failed_line(id, std::format("{}", *failure))); + } + } + return std::nullopt; +} + +// Empties `state` and reloads after `checkpoints[failed]` failed; returns +// the index of the checkpoint up to `failed` whose start the game is at, +// none when the game is at no such start. +std::optional reload( + StoryBackend& backend, std::span checkpoints, size_t failed, StoryGame& game, RunState& state +){ + state = RunState{}; + const std::optional start = game.reload(backend, checkpoints[failed]); + for (size_t c = 0; c <= failed; c++){ + if (start == checkpoints[c].id){ + return c; + } + } + return std::nullopt; +} + + +} // namespace + + +std::optional run_checkpoints( + StoryBackend& backend, std::span checkpoints, + StoryGame& game, RunState& state +){ + // Failed runs of each checkpoint so far. + std::vector failures(checkpoints.size(), 0); + size_t index = 0; + while (index < checkpoints.size()){ + StoryCheckpoint& checkpoint = checkpoints[index]; + std::optional failure = run_once(backend, checkpoint, game, state); + if (!failure){ + backend.log(checkpoint_pass_line(checkpoint.id, failures[index] + 1)); + index++; + continue; + } + const size_t failed = ++failures[index]; + if (failure->failure.repeats || !game.reload || failed >= game.attempts_per_checkpoint){ + return failure; + } + backend.log(checkpoint_reset_line(checkpoint.id, failed + 1, game.attempts_per_checkpoint)); + const std::optional start = reload(backend, checkpoints, index, game, state); + if (!start){ + backend.log(checkpoint_no_start_line(checkpoint.id)); + return CheckpointFailure{ + .checkpoint_id = checkpoint.id, + .failure = {.code = "start_unknown", .detail = "the reload is at no start of this run"}, + }; + } + index = *start; + } + return std::nullopt; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h new file mode 100644 index 0000000000..9fcbccf512 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -0,0 +1,48 @@ +/* Story Engine + * + * From: https://github.com/PokemonAutomation/ + * + * Runs a range of story checkpoints on a game's backend. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryEngine_H +#define PokemonAutomation_Pokemon_StoryEngine_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon_StoryBackend.h" +#include "Pokemon_StoryGame.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +struct CheckpointFailure{ + std::string checkpoint_id = {}; + // 0-based; none when the checkpoint failed outside its steps (a + // reload). + std::optional event_index = std::nullopt; + StoryFailure failure = {}; +}; + +// Runs `checkpoints` in order from `state`. Returns std::nullopt when every +// checkpoint passed, else the failure that ended the run. +// After a failure that does not repeat, game.reload says which checkpoint's +// start the game is at and the run goes on from there, until a checkpoint +// fails game.attempts_per_checkpoint times. A step that throws is logged and +// the exception passes through. +std::optional run_checkpoints( + StoryBackend& backend, std::span checkpoints, + StoryGame& game, RunState& state +); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp new file mode 100644 index 0000000000..a7a29377ad --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp @@ -0,0 +1,291 @@ +/* Story Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The engine on a FakeStoryBackend: a test game whose actions answer as + * scripted (pass, fail, fail once, press, wait for a white fade in the + * scripted looks), so a run's report, inputs and retries are checked with + * no console. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h" +#include "Pokemon_StoryEngine.h" +#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryEngine_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +using namespace std::chrono_literals; + + +// What the test game's actions share across a run. +struct TestWorld{ + // How many times "fail_once" ran. + size_t fail_once_calls = 0; +}; + +// The test game's actions; none takes args. +StoryActions test_actions(TestWorld& world){ + StoryActions actions; + actions.emplace("ok", [](const JsonObject&) -> StoryStepRun{ + return [](StoryBackend&, RunState&){ + return std::optional(); + }; + }); + actions.emplace("fail", [](const JsonObject&) -> StoryStepRun{ + return [](StoryBackend&, RunState&){ + return std::optional(StoryFailure{.code = "scripted_failure"}); + }; + }); + actions.emplace("fail_once", [&world](const JsonObject&) -> StoryStepRun{ + return [&world](StoryBackend&, RunState&){ + return world.fail_once_calls++ == 0 ? std::optional(StoryFailure{.code = "scripted_failure"}) : std::nullopt; + }; + }); + actions.emplace("not_running", [](const JsonObject&) -> StoryStepRun{ + return [](StoryBackend&, RunState&){ + return std::optional(game_not_running_failure()); + }; + }); + actions.emplace("press_a", [](const JsonObject&) -> StoryStepRun{ + return [](StoryBackend& backend, RunState&){ + backend.press(NintendoSwitch::BUTTON_A, /*hold=*/80ms, /*release=*/80ms); + return std::optional(); + }; + }); + // Walks north until a look shows a white fade, at most 3 slices. + actions.emplace("walk_to_fade", [](const JsonObject&) -> StoryStepRun{ + return [](StoryBackend& backend, RunState&){ + for (size_t slice = 0; slice < 3; slice++){ + backend.stick(/*degrees=*/0, /*duration=*/500ms, /*release=*/0ms, StoryPace::WALK); + if (backend.observe().events.has(ScreenEvent::FADE_WHITE)){ + return std::optional(); + } + } + return std::optional(StoryFailure{.code = "no_fade", .detail = "no white fade in 3 slices"}); + }; + }); + return actions; +} + +// The test game. Its save fails at the checkpoints of `save_failures`; its +// reload presses HOME and answers `reload_start`. +StoryGame test_game(std::set save_failures, std::optional reload_start){ + StoryGame game; + game.id = "test"; + game.save = [save_failures = std::move(save_failures)](StoryBackend&, const StoryCheckpoint& checkpoint){ + if (save_failures.contains(checkpoint.id)){ + return std::optional(StoryFailure{.code = "save_error", .detail = "the disk is full"}); + } + return std::optional(); + }; + game.reload = [reload_start = std::move(reload_start)](StoryBackend& backend, const StoryCheckpoint&){ + backend.press(NintendoSwitch::BUTTON_HOME, /*hold=*/80ms, /*release=*/80ms); + return reload_start; + }; + return game; +} + +// A checkpoint `id` whose steps are named for their actions. +struct TestCheckpoint{ + std::string id = {}; + std::vector actions = {}; +}; + +std::vector checkpoints_of(const std::vector& tests, TestWorld& world){ + StoryActions actions = test_actions(world); + std::vector checkpoints; + for (const TestCheckpoint& test : tests){ + StoryCheckpoint checkpoint{.id = test.id, .title = "Test", .steps = {}}; + for (const std::string& action : test.actions){ + checkpoint.steps.push_back(StoryStep{.name = action, .why = "", .run = actions.find(action)->second(JsonObject())}); + } + checkpoints.push_back(std::move(checkpoint)); + } + return checkpoints; +} + +// A look that shows `events`. +ScreenObservation look(ScreenEvents events){ + ScreenObservation observation; + observation.events = events; + return observation; +} + +// "a b": the strings, space-separated. +std::string joined(const std::vector& texts, std::string_view separator = " "){ + std::string text; + for (const std::string& part : texts){ + text += (text.empty() ? "" : std::string(separator)) + part; + } + return text; +} + +// "done" or "failed at 01.01 fail: scripted_failure" (no step name when the +// checkpoint failed outside its steps). +std::string verdict(const std::optional& failure, const std::vector& checkpoints){ + if (!failure){ + return "done"; + } + std::string event; + for (const StoryCheckpoint& checkpoint : checkpoints){ + if (checkpoint.id == failure->checkpoint_id && failure->event_index){ + event = checkpoint.steps[*failure->event_index].name; + } + } + return std::format("failed at {} {}: {}", failure->checkpoint_id, event, failure->failure.code); +} + + +struct RangeCase{ + std::string name; + struct Input{ + std::vector checkpoints = {}; + size_t attempts = 1; + std::set save_failures = {}; + // What the reload answers. + std::optional reload_start = std::nullopt; + std::vector looks = {}; + }; + Input input; + struct Expectation{ + // verdict() of the run. + std::string verdict = {}; + // The backend's inputs, joined by ", ". + std::string inputs = {}; + // A log line the run wrote, if any is checked. + std::string line = {}; + }; + Expectation expectation; +}; + +const RangeCase RANGE_CASES[] = { + { + .name = "two checkpoints pass", + .input = {.checkpoints = {{"01.00", {"ok", "press_a"}}, {"01.01", {"ok"}}}}, + .expectation = {.verdict = "done", .inputs = "press A", .line = "[Checkpoint] 01.01: passed on attempt 1 (one-shot)"}, + }, + { + .name = "a save that fails is logged and the run goes on", + .input = {.checkpoints = {{"01.00", {"ok"}}, {"01.01", {"press_a"}}}, .save_failures = {"01.00"}}, + .expectation = {.verdict = "done", .inputs = "press A", .line = "[Save] 01.00: save failed: save_error: the disk is full"}, + }, + { + .name = "a failed step stops the run at its event", + .input = {.checkpoints = {{"01.00", {"ok", "fail", "press_a"}}}}, + .expectation = {.verdict = "failed at 01.00 fail: scripted_failure", .inputs = "", .line = "[Checkpoint] 01.00: failed at 2 fail: scripted_failure"}, + }, + { + .name = "a failed checkpoint passes after a reload to its start", + .input = {.checkpoints = {{"01.01", {"fail_once"}}}, .attempts = 3, .reload_start = "01.01"}, + .expectation = {.verdict = "done", .inputs = "press HOME", .line = "[Checkpoint] 01.01: passed on attempt 2 (1 reset)"}, + }, + { + .name = "a reload to an earlier checkpoint's start replays it", + .input = { + .checkpoints = {{"01.00", {"press_a"}}, {"01.01", {"fail_once"}}}, + .attempts = 2, + .reload_start = "01.00", + }, + .expectation = {.verdict = "done", .inputs = "press A, press HOME, press A", .line = "[Checkpoint] 01.01: passed on attempt 2 (1 reset)"}, + }, + { + .name = "a checkpoint stops the run after its last attempt fails", + .input = {.checkpoints = {{"01.01", {"fail"}}}, .attempts = 2, .reload_start = "01.01"}, + .expectation = {.verdict = "failed at 01.01 fail: scripted_failure", .inputs = "press HOME"}, + }, + { + .name = "a reload to an unknown start stops the run", + .input = {.checkpoints = {{"01.01", {"fail"}}}, .attempts = 3}, + .expectation = {.verdict = "failed at 01.01 : start_unknown", .inputs = "press HOME", .line = "[Checkpoint] 01.01: reloaded to no start of this run"}, + }, + { + .name = "a failure that repeats stops the run with no reload", + .input = {.checkpoints = {{"01.01", {"not_running"}}}, .attempts = 3, .reload_start = "01.01"}, + .expectation = {.verdict = "failed at 01.01 not_running: game_not_running", .inputs = ""}, + }, + { + .name = "a walk ends on the scripted white fade", + .input = {.checkpoints = {{"01.01", {"walk_to_fade"}}}, .looks = {look({}), look({ScreenEvent::FADE_WHITE})}}, + .expectation = {.verdict = "done", .inputs = "stick 0deg 500ms walk, stick 0deg 500ms walk"}, + }, + { + .name = "a walk whose fade never shows fails", + .input = {.checkpoints = {{"01.01", {"walk_to_fade"}}}, .looks = {look({ScreenEvent::DIALOG_BOX})}}, + .expectation = { + .verdict = "failed at 01.01 walk_to_fade: no_fade", + .inputs = "stick 0deg 500ms walk, stick 0deg 500ms walk, stick 0deg 500ms walk", + .line = "[Event] 01.01/1 walk_to_fade: failed: no_fade: no white fade in 3 slices", + }, + }, +}; + +UnitTestResult check_range(const RangeCase& test, CancellableScope& scope){ + TestWorld world; + StoryGame game = test_game(test.input.save_failures, test.input.reload_start); + game.attempts_per_checkpoint = test.input.attempts; + std::vector checkpoints = checkpoints_of(test.input.checkpoints, world); + FakeStoryBackend backend({.looks = test.input.looks}, scope); + RunState state; + const std::optional failure = run_checkpoints(backend, checkpoints, game, state); + const std::string got = verdict(failure, checkpoints); + if (got != test.expectation.verdict){ + return std::format("expected \"{}\", got \"{}\"", test.expectation.verdict, got); + } + const std::string inputs = joined(backend.inputs(), ", "); + if (inputs != test.expectation.inputs){ + return std::format("expected inputs \"{}\", got \"{}\"", test.expectation.inputs, inputs); + } + if (test.expectation.line.empty()){ + return true; + } + for (const std::string& line : backend.lines()){ + if (line == test.expectation.line){ + return true; + } + } + return std::format("expected the line \"{}\" in:\n{}", test.expectation.line, joined(backend.lines(), "\n")); +} + + +class StoryEngineTest : public UnitTest{ +public: + explicit StoryEngineTest(RangeCase test_case) + : UnitTest(std::format("Pokemon::StoryEngine - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return check_range(m_case, scope); + } + +private: + RangeCase m_case; +}; + +} // namespace + + +void add_tests_StoryEngine(UnitTestDatabase& database){ + for (const RangeCase& test : RANGE_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.h new file mode 100644 index 0000000000..0f5713f2df --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.h @@ -0,0 +1,23 @@ +/* Story Engine Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryEngine_Tests_H +#define PokemonAutomation_Pokemon_StoryEngine_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the story engine on a scripted backend: runs, lint, retries and saves. +void add_tests_StoryEngine(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h new file mode 100644 index 0000000000..123b55cf1d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h @@ -0,0 +1,41 @@ +/* Story Game + * + * From: https://github.com/PokemonAutomation/ + * + * What a game hands the story engine: its save, reload and retries. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryGame_H +#define PokemonAutomation_Pokemon_StoryGame_H + +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon_StoryBackend.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +struct StoryGame{ + // "frlg", "bdsp", "plza". + std::string id = {}; + // Called after each passed checkpoint; a failure is logged. None: nothing saves. + MoveOnlyFunction(StoryBackend& backend, const StoryCheckpoint& passed)> save = {}; + // Reloads after `failed` failed; returns the id of the checkpoint whose + // start the game is now at, none when it could not reload or cannot tell. + // None: a failed checkpoint is not retried. + MoveOnlyFunction(StoryBackend& backend, const StoryCheckpoint& failed)> reload = {}; + // Runs of one checkpoint, counting the first. + size_t attempts_per_checkpoint = 1; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index c10d032f14..06807dceb7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -12,8 +12,12 @@ #define PokemonAutomation_Pokemon_AutoStory_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Data/Pokemon_StoryCheckpointJson_Tests.h" +#include "Engine/Pokemon_StoryBackend_Tests.h" +#include "Engine/Pokemon_StoryEngine_Tests.h" #include "Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon_StoryLog_Tests.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -23,6 +27,10 @@ namespace Pokemon{ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_AutoStorySelect(database); add_tests_StoryRangeOptions(database); + add_tests_StoryLog(database); + add_tests_StoryCheckpointJson(database); + add_tests_StoryBackend(database); + add_tests_StoryEngine(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailure.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailure.h new file mode 100644 index 0000000000..238992250e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryFailure.h @@ -0,0 +1,57 @@ +/* Story Failure + * + * From: https://github.com/PokemonAutomation/ + * + * Why a story action, check or checkpoint failed. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFailure_H +#define PokemonAutomation_Pokemon_StoryFailure_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A failed action, check or checkpoint; std::nullopt in its place is success. +struct StoryFailure{ + // A name the game owns, which recoveries match. + std::string code = {}; + std::string detail = {}; + // Another attempt would fail the same way, so the engine does not reload. + bool repeats = false; +}; + +// The code of a step that found the Switch's HOME menu instead of the game. +inline constexpr std::string_view GAME_NOT_RUNNING = "game_not_running"; +// GAME_NOT_RUNNING: the run stops, with no reload. +inline StoryFailure game_not_running_failure(){ + return StoryFailure{ + .code = std::string(GAME_NOT_RUNNING), + .detail = "the game is not running: the Switch HOME menu is showing", + .repeats = true, + }; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +// "probe_exhausted: 4 rows", or the code alone when there is no detail. +template <> +struct std::formatter : std::formatter{ + auto format(const PokemonAutomation::Pokemon::StoryFailure& failure, std::format_context& context) const{ + if (failure.detail.empty()){ + return std::format_to(context.out(), "{}", failure.code); + } + return std::format_to(context.out(), "{}: {}", failure.code, failure.detail); + } +}; + +#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..acf521ac13 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.cpp @@ -0,0 +1,105 @@ +/* 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, CheckpointPrefix prefix_kind){ + if (prefix_kind == CheckpointPrefix::AS_GIVEN){ + return std::string(checkpoint); + } + return std::format("[Checkpoint] {}", checkpoint); +} + +} // namespace + + +std::string checkpoint_start_line( + std::string_view checkpoint, std::optional events, CheckpointPrefix prefix_kind +){ + const std::string prefix = checkpoint_prefix(checkpoint, prefix_kind); + 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, CheckpointPrefix prefix_kind +){ + 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, CheckpointPrefix prefix_kind){ + return std::format( + "{}: passed on 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, CheckpointPrefix prefix_kind +){ + const std::string prefix = checkpoint_prefix(checkpoint, prefix_kind); + 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 event_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status){ + return std::format("[Event] {}/{} {}: {}", checkpoint, number, event, status); +} + +std::string checkpoint_failed_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view reason){ + return std::format("[Checkpoint] {}: failed at {} {}: {}", checkpoint, number, event, reason); +} + +std::string checkpoint_done_line(std::string_view checkpoint, size_t events){ + return std::format("[Checkpoint] {}: done, {} run", checkpoint, events); +} + +std::string checkpoint_no_start_line(std::string_view checkpoint){ + return std::format("[Checkpoint] {}: reloaded to no start of this run", checkpoint); +} + +std::string save_failed_line(std::string_view checkpoint, std::string_view reason){ + return std::format("[Save] {}: save failed: {}", checkpoint, reason); +} + +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..7f2e948595 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog.h @@ -0,0 +1,101 @@ +/* Story Log + * + * From: https://github.com/PokemonAutomation/ + * + * The log lines every game's AutoStory and the story engine write. + * + */ + +// code-health: allow header_symbols -- every story log line format in one place, as review asked; each symbol is one line format + +#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` 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 +// [Checkpoint] 00.04: start, 3 events +std::string checkpoint_start_line( + std::string_view checkpoint, + std::optional events = std::nullopt, + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD +); + +// 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, + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD +); + +// 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, + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD +); + +// 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, + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD +); + +// The story engine's lines; `number` counts a checkpoint's steps from 1. +// A step starts or ends, with its status: +// [Event] 00.04/2 to_lab: start +// [Event] 00.04/2 to_lab: failed: no_fade: no white fade in 3 slices +std::string event_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view status); +// A checkpoint's step `number`, named `event`, failed for `reason`: +// [Checkpoint] 00.04: failed at 2 to_lab: no_fade: no white fade in 3 slices +std::string checkpoint_failed_line(std::string_view checkpoint, size_t number, std::string_view event, std::string_view reason); +// Every one of a checkpoint's `events` steps passed: +// [Checkpoint] 00.04: done, 3 run +std::string checkpoint_done_line(std::string_view checkpoint, size_t events); +// A reload after the checkpoint failed came back at no checkpoint start of +// the run: +// [Checkpoint] 00.04: reloaded to no start of this run +std::string checkpoint_no_start_line(std::string_view checkpoint); +// The save after a passed checkpoint failed for `reason`: +// [Save] 00.04: save failed: save_error: the disk is full +std::string save_failed_line(std::string_view checkpoint, std::string_view reason); + +// `line` without its leading "[Tag] ", for the video overlay; a line with +// no tag 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..1766e621f7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp @@ -0,0 +1,168 @@ +/* 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{ + // The formatter call, made when the test runs. + struct Input{ + std::string (*format)(); + }; + + // The exact line: log readers and the engine tests match 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{ + 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; + } + +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 = {.format = []{ return checkpoint_start_line("00.04"); }}, + .expectation = {.line = "[Checkpoint] 00.04: start"}, + }, + { + .name = "checkpoint start with its events", + .input = {.format = []{ return checkpoint_start_line("00.04", /*events=*/3); }}, + .expectation = {.line = "[Checkpoint] 00.04: start, 3 events"}, + }, + { + .name = "attempt", + .input = {.format = []{ return checkpoint_attempt_line("00.04", 2); }}, + .expectation = {.line = "[Checkpoint] 00.04: attempt 2"}, + }, + { + .name = "attempt out of a limit", + .input = {.format = []{ return checkpoint_attempt_line("00.04", 2, /*attempts=*/3); }}, + .expectation = {.line = "[Checkpoint] 00.04: attempt 2/3"}, + }, + { + .name = "one-shot pass", + .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 = {.format = []{ return checkpoint_pass_line("00.04", 2); }}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}, + }, + { + .name = "pass after resets", + .input = {.format = []{ return checkpoint_pass_line("00.04", 3); }}, + .expectation = {.line = "[Checkpoint] 00.04: passed on attempt 3 (2 resets)"}, + }, + { + .name = "reset", + .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 = {.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 = {.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 = {.format = []{ return checkpoint_attempt_line(SV_TEXT, /*attempt=*/2, /*attempts=*/100, CheckpointPrefix::AS_GIVEN); }}, + .expectation = {.line = "Checkpoint:: 004 - 02. Start point: X: attempt 2/100"}, + }, + { + .name = "pass without the checkpoint prefix", + .input = {.format = []{ return checkpoint_pass_line(SV_TEXT, /*attempt=*/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 = {.format = []{ return checkpoint_reset_line(SV_TEXT, /*next_attempt=*/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 = {.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 = {.format = []{ return story_short_line("[Tag] 12.03: some text"); }}, + .expectation = {.line = "12.03: some text"}, + }, + { + .name = "short line without the checkpoint prefix", + .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 = {.format = []{ return story_short_line("[Tag 12.03"); }}, + .expectation = {.line = "[Tag 12.03"}, + }, + { + .name = "short empty line", + .input = {.format = []{ return 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/Testing/Pokemon_StoryFakeBackend.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.cpp new file mode 100644 index 0000000000..852fa6ac29 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.cpp @@ -0,0 +1,54 @@ +/* Story Fake Backend + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_StoryFakeBackend.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +FakeStoryBackend::FakeStoryBackend(FakeStoryScript script, CancellableScope& scope) + : m_script(std::move(script)) + , m_scope(scope) +{} + +void FakeStoryBackend::press(NintendoSwitch::Button button, Milliseconds, Milliseconds){ + m_inputs.push_back(std::format("press {}", NintendoSwitch::button_to_string(button))); +} +void FakeStoryBackend::stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace){ + std::string input = std::format("stick {}deg {}ms {}", degrees, duration.count(), pace == StoryPace::RUN ? "run" : "walk"); + if (release > Milliseconds::zero()){ + input += std::format(", release {}ms", release.count()); + } + m_inputs.push_back(std::move(input)); +} +void FakeStoryBackend::wait(Milliseconds duration){ + m_inputs.push_back(std::format("wait {}ms", duration.count())); +} +ScreenObservation FakeStoryBackend::observe(){ + if (m_script.looks.empty()){ + return {}; + } + const size_t look = m_next_look < m_script.looks.size() ? m_next_look++ : m_script.looks.size() - 1; + return m_script.looks[look]; +} +void FakeStoryBackend::log(std::string_view line){ + m_lines.emplace_back(line); + m_overlay_lines.emplace_back(line); +} +void FakeStoryBackend::log_detail(std::string_view line){ + m_lines.emplace_back(line); +} +CancellableScope& FakeStoryBackend::scope(){ + return m_scope; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h b/SerialPrograms/Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h new file mode 100644 index 0000000000..a84a5ff574 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h @@ -0,0 +1,72 @@ +/* Story Fake Backend + * + * From: https://github.com/PokemonAutomation/ + * + * A scripted backend for tests: it records every input and log line. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryFakeBackend_H +#define PokemonAutomation_Pokemon_StoryFakeBackend_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a FakeStoryBackend shows. +struct FakeStoryScript{ + // observe()'s answers in order; the last repeats. None: an empty look. + std::vector looks = {}; +}; + +// Records every input and log line; observe() plays the script. +// code-health: allow public_methods -- the backend calls it fakes plus the three recordings tests read; nothing to split +// code-health: allow test_only_api -- a test double: the scripted backend the engine and game tests run on +class FakeStoryBackend : public StoryBackend{ +public: + FakeStoryBackend(FakeStoryScript script, CancellableScope& scope); + + // Each input, in order: "press A", "press +" (by button_to_string()), + // "stick 90deg 150ms walk", "stick 90deg 150ms walk, release 300ms" + // (a release only when not zero), "wait 100ms". + const std::vector& inputs() const{ + return m_inputs; + } + // Each log line, in order, log_detail()'s too: the file log. + const std::vector& lines() const{ + return m_lines; + } + // The lines log() wrote, in order: the stream's overlay. + const std::vector& overlay_lines() const{ + return m_overlay_lines; + } + + virtual void press(NintendoSwitch::Button button, Milliseconds hold, Milliseconds release) override; + virtual void stick(double degrees, Milliseconds duration, Milliseconds release, StoryPace pace) override; + virtual void wait(Milliseconds duration) override; + virtual ScreenObservation observe() override; + virtual void log(std::string_view line) override; + virtual void log_detail(std::string_view line) override; + virtual CancellableScope& scope() override; + +private: + FakeStoryScript m_script; + CancellableScope& m_scope; + size_t m_next_look = 0; + std::vector m_inputs; + std::vector m_lines; + std::vector m_overlay_lines; +}; + + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h new file mode 100644 index 0000000000..e1cf5084c7 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -0,0 +1,31 @@ +/* Story Checkpoint + * + * From: https://github.com/PokemonAutomation/ + * + * A checkpoint of a game's story. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpoint_H +#define PokemonAutomation_Pokemon_StoryCheckpoint_H + +#include +#include +#include "Pokemon_StoryStep.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The stretch of story between two saves: its steps. +struct StoryCheckpoint{ + std::string id = {}; + std::string title = {}; + std::vector steps = {}; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h new file mode 100644 index 0000000000..c5b68afad8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h @@ -0,0 +1,48 @@ +/* Story Step + * + * From: https://github.com/PokemonAutomation/ + * + * One step of a checkpoint: an action of the game's, named by the plan, + * with its args bound, ready to run. A checkpoint (StoryCheckpoint) is the + * steps between two saves; the engine runs them in order, and the first + * step that fails fails the checkpoint. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryStep_H +#define PokemonAutomation_Pokemon_StoryStep_H + +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h" +#include "Pokemon/AutoStory/Pokemon_StoryFailure.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a run knows; a reload empties it. +struct RunState{ + std::set flags = {}; + // The step under way, "/", for its log lines. + std::string step = {}; +}; + +// A step's action with its args bound. +using StoryStepRun = MoveOnlyFunction(StoryBackend& backend, RunState& state)>; + +struct StoryStep{ + std::string name = {}; + std::string why = {}; + // The plan's "action": what kind of step it is. + std::string action = {}; + StoryStepRun run = {}; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 5eefe64cc4..8b544dc359 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1512,6 +1512,29 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions.h Source/Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions_Tests.cpp Source/Pokemon/AutoStory/Options/Pokemon_StoryRangeOptions_Tests.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson.h + Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.cpp + Source/Pokemon/AutoStory/Data/Pokemon_StoryCheckpointJson_Tests.h + Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_ScreenObservation.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_StoryBackend_Tests.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.cpp + Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine_Tests.h + Source/Pokemon/AutoStory/Engine/Pokemon_StoryGame.h + Source/Pokemon/AutoStory/Pokemon_StoryFailure.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/Testing/Pokemon_StoryFakeBackend.cpp + Source/Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp