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..657a3b5857 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -0,0 +1,361 @@ +/* Story Engine + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_CheckpointAttempts.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h" +#include "Pokemon_StoryEngine.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// The StoryStepExecutor interface over the step runner, so the engine +// builds its events with build_story_checkpoint() (from_save, +// once_per_save), as the segment runner does. +template +class RunnerExecutor : public StoryStepExecutor{ +public: + RunnerExecutor(StoryStepRunner& runner, const StoryGame& game, StoryPausePoints& pause) + : m_runner(runner) + , m_game(game) + , m_pause(pause) + {} + + virtual void begin_attempt(const StoryCheckpoint& data) override{ + m_checkpoint = &data; + if (m_game.begin_attempt){ + m_game.begin_attempt(data); + } + } + virtual Outcome run(const StoryStep& step, RunState& state) override{ + m_pause.before_step(pause_label(step)); + return m_runner.act(step, state); + } + virtual bool holds(std::string_view condition, RunState& state) override{ + return m_runner.holds(condition, state); + } + +private: + // The step's place in its checkpoint (step names are unique, which the + // lint checks). + std::string pause_label(const StoryStep& step) const{ + size_t index = 0; + if (m_checkpoint != nullptr){ + while (index < m_checkpoint->steps.size() && m_checkpoint->steps[index].name != step.name){ + index++; + } + } + return story_step_pause_label(m_checkpoint == nullptr ? "" : m_checkpoint->id, index, step.name); + } + + StoryStepRunner& m_runner; + const StoryGame& m_game; + StoryPausePoints& m_pause; + const StoryCheckpoint* m_checkpoint = nullptr; +}; + +// `game` with its SAVE step "save" saving `current`. Sets `saved` when it +// saves. +template +StoryGame with_save_step( + StoryGame game, const StoryCheckpoint* const& current, bool& saved +){ + StoryActionHandler step = [¤t, &saved, save = game.save]( + StoryBackend& backend, const StoryActionCall&, RunState& + ){ + if (!save || current == nullptr){ + return outcome_failed("unsupported_step", "save needs StoryGame::save"); + } + Outcome outcome = save(backend, *current); + saved = saved || outcome.succeeded(); + return outcome; + }; + game.actions.push_back({.spec = {.kind = StoryStepKind::SAVE, .name = "save", .args = {}}, .handler = std::move(step)}); + return game; +} + +// A failure every attempt would repeat: no reload for it. +bool repeats_on_reload(const Outcome& failure){ + return failure.failure == GAME_NOT_RUNNING || failure.failure == CONSOLE_ONLY || failure.failure == "lint_failed"; +} + +// What stopped `report`'s run: its steps' failure, or its postconditions. +Outcome run_failure(const StoryEngineReport& report){ + if (!report.run.completed){ + return report.run.failure; + } + return outcome_failed("postconditions_failed"); +} + + +// The items ("bag.") and flags ("flag.", non-zero is set) of +// `observation`'s readings. +ObservedState observed_readings(const ScreenObservation& observation){ + constexpr std::string_view BAG = "bag."; + constexpr std::string_view FLAG = "flag."; + ObservedState observed; + for (const auto& [name, value] : observation.readings){ + if (name.starts_with(BAG)){ + observed.items[name.substr(BAG.size())] = value < 0 ? 0 : (size_t)value; + }else if (name.starts_with(FLAG)){ + observed.flags[name.substr(FLAG.size())] = value != 0; + } + } + return observed; +} + + +// What checking `checkpoint`'s postconditions needs read: "pose" for an +// end position, "bag." per item, "flag." per flag. +std::vector postcondition_needs(const StoryCheckpoint& checkpoint){ + std::vector needs; + if (checkpoint_end_pose(checkpoint)){ + needs.emplace_back("pose"); + } + for (const StoryItem& item : checkpoint.postconditions.items){ + needs.push_back(std::format("bag.{}", item.item)); + } + for (const std::string& flag : checkpoint.postconditions.flags){ + needs.push_back(std::format("flag.{}", flag)); + } + return needs; +} + +// Whether the postcondition check of a checkpoint that `needs` these runs +// `reader` on a backend of `kind`. +template +bool runs_after_checkpoint(const StoryGameReader& reader, const std::vector& needs, StoryBackendKind kind){ + if (!reader.read || reader.use != StoryReaderUse::AFTER_CHECKPOINT){ + return false; + } + if (reader.console_only && kind != StoryBackendKind::CONSOLE){ + return false; + } + for (const std::string& need : needs){ + if (story_reader_supplies(reader.supplies, need)){ + return true; + } + } + return false; +} + + +} // namespace + + +ObservedState observed_state(const Observation& observation){ + ObservedState observed = observed_readings(observation); + if (observation.pose){ + observed.map = observation.pose->map; + if (observation.pose->tiles.size() == 1){ + observed.tile = observation.pose->tiles[0]; + } + observed.facing = observation.pose->facing; + } + return observed; +} + +ObservedState observed_state(const Observation& observation){ + ObservedState observed = observed_readings(observation); + if (observation.pose){ + observed.map = observation.pose->zone; + } + return observed; +} + + +template +StoryEngine::StoryEngine(const StoryGame& game, StoryBackend& backend) + : m_game(with_save_step(game, m_checkpoint, m_saved_part_way)) + , m_backend(backend) + , m_registry(m_game) + , m_runner(m_registry, backend) +{ + m_registry.pause_points().set_log([&backend](std::string_view line){ + backend.log(line); + }); +} + +template +StoryEngineReport StoryEngine::run_checkpoint(const StoryCheckpoint& checkpoint, RunState& state){ + StoryEngineReport report; + const std::vector problems = m_registry.lint(checkpoint); + if (!problems.empty()){ + for (const std::string& problem : problems){ + m_backend.log(std::format("[Lint] {}", problem)); + } + report.run.failure = outcome_failed("lint_failed", problems.front()); + return report; + } + m_checkpoint = &checkpoint; + m_saved_part_way = false; + report.run = run_steps(checkpoint, state); + report.saved_part_way = m_saved_part_way; + m_checkpoint = nullptr; + if (!report.run.completed){ + m_backend.log(std::format("[Save] {}: run failed: not saved", checkpoint.id)); + return report; + } + report.postconditions = check_postconditions(checkpoint); + report.verdict = report.postconditions->count(PostconditionVerdict::FAILED) == 0 + ? CheckpointVerdict::PASSED + : CheckpointVerdict::FAILED; + save_if_passed(checkpoint, report); + return report; +} + +template +Outcome StoryEngine::reload(RunState& state){ + state = RunState{}; + if (!m_game.reload){ + return outcome_failed("reload_failed", "the game has no reload"); + } + const Outcome reloaded = m_game.reload(m_backend); + if (!reloaded.succeeded()){ + return outcome_failed("reload_failed", to_string(reloaded)); + } + if (reloaded.pose){ + state.pose = *reloaded.pose; + } + return outcome_ok(); +} + +template +StoryEngineReport StoryEngine::run_with_retries( + const StoryCheckpoint& checkpoint, size_t attempts, RunState& state, CheckpointRun& run +){ + StoryEngineReport report; + const CheckpointAttempts loop{ + .max_attempts = attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + report = run_checkpoint(checkpoint, state); + if (report.verdict == CheckpointVerdict::PASSED){ + return AttemptResult::PASSED; + } + if (report.saved_part_way){ + m_backend.log(std::format( + "[Checkpoint] {}: a save step saved part-way, so a reload would not return to its start: no retry", + checkpoint.id + )); + return AttemptResult::FINAL; + } + return repeats_on_reload(report.run.failure) ? AttemptResult::FINAL : AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + m_backend.log(checkpoint_reset_line(checkpoint.id, next_attempt, attempts)); + const Outcome reloaded = reload(state); + if (!reloaded.succeeded()){ + report.run.completed = false; + report.run.failed_name.clear(); + report.run.failure = reloaded; + return false; + } + return true; + }, + .log = [this](const std::string& line){ + m_backend.log(line); + }, + }; + run.passed = run_checkpoint_attempts(checkpoint.id, loop).passed; + return report; +} + +template +SegmentReport StoryEngine::run_segment( + std::string_view id, std::span checkpoints, size_t attempts_per_checkpoint, + RunState& state +){ + SegmentReport report; + m_backend.log(std::format("[Segment] {}: start, {} checkpoints", id, checkpoints.size())); + // Whether a reload returns to the save the next checkpoint starts from. + bool start_saved = true; + for (size_t c = 0; c < checkpoints.size(); c++){ + const StoryCheckpoint& checkpoint = checkpoints[c]; + const size_t attempts = start_saved ? attempts_per_checkpoint : 1; + if (!start_saved && attempts_per_checkpoint > 1){ + m_backend.log(std::format("[Segment] {}: {}: no save at its start, so no reload if it fails", id, checkpoint.id)); + } + report.checkpoints_run++; + report.runs.push_back(CheckpointRun{.id = checkpoint.id}); + const StoryEngineReport result = run_with_retries(checkpoint, attempts, state, report.runs.back()); + report.reloads += report.runs.back().attempts - 1; + if (result.verdict != CheckpointVerdict::PASSED){ + report.failed_checkpoint = checkpoint.id; + report.failed_event = result.run.failed_name; + report.failure = run_failure(result); + m_backend.log(std::format("[Segment] {}: failed at {}: {}", id, checkpoint.id, to_string_with_class(report.failure))); + return report; + } + start_saved = result.saved; + } + report.completed = true; + m_backend.log(std::format("[Segment] {}: done, {} checkpoints", id, report.checkpoints_run)); + return report; +} + +template +CheckpointReport StoryEngine::run_steps(const StoryCheckpoint& checkpoint, RunState& state){ + RunnerExecutor executor(m_runner, m_game, m_registry.pause_points()); + Checkpoint events = build_story_checkpoint(checkpoint, executor, &m_prepared); + if (m_game.interrupts){ + events.interrupts = m_game.interrupts(m_backend); + } + EventLogger log = [this](const std::string& line){ + m_backend.log(line); + }; + return Pokemon::run_checkpoint(state, events, log); +} + +template +void StoryEngine::save_if_passed(const StoryCheckpoint& checkpoint, StoryEngineReport& report){ + if (report.verdict != CheckpointVerdict::PASSED){ + m_backend.log(std::format("[Save] {}: not saved: postconditions failed", checkpoint.id)); + return; + } + if (!m_game.save){ + m_backend.log(std::format("[Save] {}: not saved: the game has no save", checkpoint.id)); + return; + } + const Outcome saved = m_game.save(m_backend, checkpoint); + report.saved = saved.succeeded(); + m_backend.log(std::format("[Save] {}: {}", checkpoint.id, report.saved ? "saved" : std::format("save failed: {}", to_string(saved)))); +} + +template +PostconditionReport StoryEngine::check_postconditions(const StoryCheckpoint& checkpoint){ + const std::vector needs = postcondition_needs(checkpoint); + Observation observation = m_backend.observe(); + for (const StoryGameReader& reader : m_game.readers){ + if (runs_after_checkpoint(reader, needs, m_backend.kind())){ + reader.read(m_backend, observation); + } + } + PostconditionReport report = Pokemon::check_postconditions(checkpoint, observed_state(observation)); + for (const std::string& line : postcondition_log_lines(report)){ + m_backend.log(line); + } + return report; +} + + +template class StoryEngine; +template class StoryEngine; + + + + +} // 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..2e154a3c4a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.h @@ -0,0 +1,112 @@ +/* Story Engine + * + * From: https://github.com/PokemonAutomation/ + * + * The one story executor, for every game and both backends. A run of a + * checkpoint lints its steps against the game's actions (a problem fails + * the run before any input), runs them through the event layer + * (Pokemon_StoryEvents.h: skip_if, retries, recoveries, interrupts) with + * the step runner, checks the postconditions with the game's readers, and + * saves at the end of each checkpoint that passed (StoryGame::save: the + * game knows whether the row autosaved or needs its menu's save). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryEngine_H +#define PokemonAutomation_Pokemon_StoryEngine_H + +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h" +#include "Pokemon/AutoStory/Pokemon_StoryStepExecutor.h" +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h" +#include "Pokemon_StoryActionRegistry.h" +#include "Pokemon_StoryEnginePolicy.h" +#include "Pokemon_StoryStepRunner.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What a look at a tile game shows of a checkpoint's postconditions: the +// map, the tile (when the pose is one tile) and the facing from the pose; +// items from "bag." readings; flags from "flag." readings +// (non-zero is set). +ObservedState observed_state(const Observation& observation); + +// The same for a zone game: the map is the zone; no tile or facing. +ObservedState observed_state(const Observation& observation); + +// Runs `game`'s checkpoints on `backend`. Both must outlive it. +template +class StoryEngine{ +public: + StoryEngine(const StoryGame& game, StoryBackend& backend); + StoryEngine(const StoryEngine&) = delete; + StoryEngine& operator=(const StoryEngine&) = delete; + + const StoryActionRegistry& registry() const{ + return m_registry; + } + + // Runs `checkpoint` from `state`, then checks its postconditions and, + // when it passed, saves (StoryGame::save). A SAVE step among its steps + // saves too. + StoryEngineReport run_checkpoint(const StoryCheckpoint& checkpoint, RunState& state); + + // `checkpoints` in order from the save the game is on, each run with + // run_checkpoint(), until one does not pass. + // One that fails is run again after a reload (StoryGame::reload), up to + // `attempts_per_checkpoint` runs, only when a reload returns to the + // save it started from: the first checkpoint's, or one the checkpoint + // before it saved at its end. No reload once a SAVE step has saved + // part-way through the attempt (the reload would land there), nor for + // a failure every attempt would repeat: a lint, a console_only action, + // the game not running. Logs "[Segment] : ..." lines. + SegmentReport run_segment( + std::string_view id, std::span checkpoints, size_t attempts_per_checkpoint, + RunState& state + ); + + // `checkpoint`'s postconditions against a fresh observation, read by + // the game's readers that opted in (StoryReaderUse::AFTER_CHECKPOINT) + // and supply what they need, but no console_only one off a console; + // logged as "[Postcondition]" lines. + PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint); + +private: + CheckpointReport run_steps(const StoryCheckpoint& checkpoint, RunState& state); + void save_if_passed(const StoryCheckpoint& checkpoint, StoryEngineReport& report); + // Empties `state` and reloads the last save; the reload's pose, if it + // says one, goes into `state`. + Outcome reload(RunState& state); + // One checkpoint of a segment until it passes or runs out of attempts. + StoryEngineReport run_with_retries( + const StoryCheckpoint& checkpoint, size_t attempts, RunState& state, CheckpointRun& run + ); + + // Declared first: m_game's save step reads them, and sets + // m_saved_part_way when it saves. + const StoryCheckpoint* m_checkpoint = nullptr; + bool m_saved_part_way = false; + StoryGame m_game; + StoryBackend& m_backend; + StoryActionRegistry m_registry; + StoryStepRunner m_runner; + PreparedSteps m_prepared; +}; + +// Instantiated once, in Pokemon_StoryEngine.cpp. +extern template class StoryEngine; +// And for ZonePose (Pokemon_ZonePose.h). +extern template class StoryEngine; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp new file mode 100644 index 0000000000..44851811da --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.cpp @@ -0,0 +1,180 @@ +/* Story Engine Policy Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The engine's whole run of a checkpoint on a scripted backend: the + * postconditions (passed, failed, not checked) and whether it saved: at + * the end of a checkpoint that passed, and at each SAVE step. + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" +#include "Pokemon_StoryEngine.h" +#include "Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryEnginePolicy_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// A checkpoint's end in the shared format, its steps given: beside B3F's +// Rare Candy, facing north, with two Rare Candies. +std::string checkpoint_0031(std::string_view steps){ + return std::format(R"({{"id": "00.31", "title": "Rocket Hideout", "events": {}, + "metadata": {{"source": "test", "postconditions": {{ + "position": {{"map": "RocketHideout_B3F", "tiles": [[12, 13]], "facing": "north"}}, + "items": [{{"item": "RARE_CANDY", "quantity": 2}}] + }}}}}})", steps); +} + +inline constexpr std::string_view TALK_STEPS = R"([{"id": "b3f_rare_candy", "kind": "talk", "why": "the second candy"}])"; +inline constexpr std::string_view SAVE_STEPS = R"([{"id": "b3f_rare_candy", "kind": "talk"}, {"id": "save_here", "kind": "save"}])"; + +// A look at B3F (12, 13) or wherever `map` is. +Observation end_look(std::string map){ + Observation observation; + observation.events = ScreenEvents{ScreenEvent::PLAYER_CAN_MOVE}; + observation.pose = GridPose{.map = std::move(map), .tiles = {{12, 13}}, .facing = Direction::NORTH}; + return observation; +} + +// A game whose save is recorded as an input, and whose "bag" reader +// reads `candies` Rare Candies (no reader when unset). +StoryGame engine_game(std::optional candies){ + StoryGame game; + game.id = "test"; + game.save = [](StoryBackend& backend, const StoryCheckpoint&){ + backend.wait(Milliseconds(1)); + backend.log("[Test] save"); + return outcome_ok(); + }; + if (candies){ + game.readers.push_back({ + .name = "bag", .supplies = {"bag.*"}, .use = StoryReaderUse::AFTER_CHECKPOINT, .console_only = false, + .read = [count = *candies](StoryBackend&, Observation& observation){ + observation.readings["bag.RARE_CANDY"] = count; + }, + }); + } + return game; +} + + + +struct EngineCase{ + // One engine run of 00.31, the player ending on `end_map`. + struct Input{ + std::string_view steps = TALK_STEPS; + // Where the talk leaves the player; empty: the talk opens no text. + std::string_view end_map = "RocketHideout_B3F"; + std::optional candies = 2; + }; + + // The verdict, how many times the game saved (each save logs + // "[Test] save") and a line the run logged. + struct Expectation{ + CheckpointVerdict verdict = CheckpointVerdict::PASSED; + size_t saves = 0; + std::string_view line = {}; + }; + + std::string_view name; + Input input; + Expectation expectation; +}; + +const EngineCase ENGINE_CASES[] = { + {.name = "a pass saves at the end", .input = {}, + .expectation = {.saves = 1, .line = "[Save] 00.31: saved"}}, + {.name = "B4F false pass fails postconditions", .input = {.end_map = "RocketHideout_B4F"}, + .expectation = {.verdict = CheckpointVerdict::FAILED, .saves = 0, + .line = "[Postcondition] 00.31 map: failed: expected RocketHideout_B3F, read RocketHideout_B4F"}}, + {.name = "unread bag is not checked", .input = {.candies = std::nullopt}, + .expectation = {.saves = 1, .line = "[Postcondition] 00.31: 3 passed, 0 failed, 1 not checked: incomplete"}}, + {.name = "wrong candy count fails", .input = {.candies = 1}, + .expectation = {.verdict = CheckpointVerdict::FAILED, .saves = 0, + .line = "[Postcondition] 00.31 item RARE_CANDY: failed: expected 2, read 1"}}, + {.name = "failed run never saves", .input = {.end_map = ""}, + .expectation = {.verdict = CheckpointVerdict::FAILED, .saves = 0, .line = "[Save] 00.31: run failed: not saved"}}, + {.name = "save step saves", .input = {.steps = SAVE_STEPS}, + .expectation = {.saves = 2, .line = "[Step] save_here SAVE/default: ok"}}, +}; + +// A text box, then the overworld on `end_map`; only the overworld when +// `end_map` is empty. +std::vector> engine_looks(std::string_view end_map){ + if (end_map.empty()){ + return {end_look("RocketHideout_B3F")}; + } + Observation text; + text.events = ScreenEvents{ScreenEvent::DIALOG_BOX}; + return {std::move(text), end_look(std::string(end_map))}; +} + +std::string check_engine_lines(const EngineCase& test, const FakeStoryBackend& backend){ + size_t saves = 0; + bool found = false; + for (const std::string& line : backend.lines()){ + saves += line == "[Test] save" ? 1 : 0; + found |= line.starts_with(test.expectation.line); + } + if (saves != test.expectation.saves){ + return std::format("expected {} saves, got {}", test.expectation.saves, saves); + } + return found ? "" : std::format("expected a line starting \"{}\"", test.expectation.line); +} + +std::string check_engine(const EngineCase& test, CancellableScope& scope){ + const StoryGame game = engine_game(test.input.candies); + FakeStoryBackend backend({.looks = engine_looks(test.input.end_map)}, scope); + StoryEngine engine(game, backend); + const StoryCheckpoint checkpoint = read_story_checkpoint( + parse_json(checkpoint_0031(test.input.steps)), test.name, engine.registry().specs() + ); + RunState state; + const StoryEngineReport report = engine.run_checkpoint(checkpoint, state); + if (report.verdict != test.expectation.verdict){ + return std::format("expected the checkpoint {}", test.expectation.verdict == CheckpointVerdict::PASSED ? "passed" : "failed"); + } + return check_engine_lines(test, backend); +} + +class StoryEngineSaveTest : public UnitTest{ +public: + explicit StoryEngineSaveTest(EngineCase test) + : UnitTest(std::format("Pokemon::StoryEngine save - {}", test.name)) + , m_case(std::move(test)) + {} + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + const std::string error = check_engine(m_case, scope); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } +private: + EngineCase m_case; +}; + + +} // namespace + + +void add_tests_StoryEnginePolicy(UnitTestDatabase& database){ + for (const EngineCase& test : ENGINE_CASES){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.h new file mode 100644 index 0000000000..267cbae30b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEnginePolicy_Tests.h @@ -0,0 +1,23 @@ +/* Story Engine Policy Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryEnginePolicy_Tests_H +#define PokemonAutomation_Pokemon_StoryEnginePolicy_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the engine's save decision, postconditions, steps hash, metadata and observation screens. +void add_tests_StoryEnginePolicy(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp new file mode 100644 index 0000000000..658bd99ffc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -0,0 +1,446 @@ +/* Story Engine Runs Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The engine on games of either pose type: a scripted backend for any + * pose type, declared poses of the game's kind only, and tile actions + * only for tile games. Then readers that opt in to the postcondition + * check, and the engine's breakpoints. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Data/Pokemon_StoryStepJson.h" +#include "Pokemon_StoryEngine.h" +#include "Pokemon_StoryFakeBackend.h" +#include "Pokemon_StoryEngineRuns_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +std::string joined(const std::vector& items){ + std::string text; + for (const std::string& item : items){ + text += (text.empty() ? "" : ", ") + item; + } + return text; +} + +template +bool has_line(const FakeStoryBackend& backend, std::string_view line){ + for (const std::string& each : backend.lines()){ + if (each == line){ + return true; + } + } + return false; +} + +template +std::string expect_inputs(const FakeStoryBackend& backend, std::string_view expected){ + const std::string got = joined(backend.inputs()); + return got == expected ? "" : std::format("expected inputs \"{}\", got \"{}\"", expected, got); +} + +template +Observation look(std::initializer_list events){ + Observation observation; + observation.events = ScreenEvents(events); + return observation; +} + +StoryStep game_step(std::string name, std::string action, std::string why = ""){ + StoryStep step; + step.kind = StoryStepKind::GAME; + step.name = std::move(name); + step.why = std::move(why); + step.action = std::move(action); + return step; +} + +StoryCheckpoint checkpoint_of(std::string id, std::vector steps){ + StoryCheckpoint checkpoint; + checkpoint.id = std::move(id); + checkpoint.steps = std::move(steps); + return checkpoint; +} + +// A GAME action of no args. +template +StoryAction game_action(std::string name, StoryActionHandler handler){ + return {.spec = {.kind = StoryStepKind::GAME, .name = std::move(name), .args = {}}, .handler = std::move(handler)}; +} + +// A game whose "flaky" action fails its first `failures` calls, whose +// save logs "[Test] save " and whose reload logs "[Test] reload". +template +StoryGame flaky_game(size_t failures){ + StoryGame game; + game.id = "test"; + std::shared_ptr calls = std::make_shared(0); + game.actions.push_back(game_action("flaky", [calls, failures](StoryBackend&, const StoryActionCall&, RunState&){ + return (*calls)++ < failures ? outcome_failed("flaky_failed") : outcome_ok(); + })); + game.actions.push_back(game_action("fine", [](StoryBackend&, const StoryActionCall&, RunState&){ + return outcome_ok(); + })); + game.save = [](StoryBackend& backend, const StoryCheckpoint& checkpoint){ + backend.log(std::format("[Test] save {}", checkpoint.id)); + return outcome_ok(); + }; + game.reload = [](StoryBackend& backend){ + backend.log("[Test] reload"); + return outcome_ok(); + }; + return game; +} + + +// A zone game's checkpoint read from JSON whose start names +// only its zone, run on the scripted backend of its pose type. +std::string zone_checkpoint_runs(CancellableScope& scope){ + StoryGame game = flaky_game(0); + FakeStoryBackend backend({.looks = {look({ScreenEvent::PLAYER_CAN_MOVE})}}, scope); + StoryEngine engine(game, backend); + const StoryCheckpoint checkpoint = read_story_checkpoint(parse_json(R"({ + "id": "03.01", "title": "Down to the lobby", "start": {"place": "HotelZ", "spot": "Room", "heading": 180}, + "events": [{"id": "walk", "kind": "game_action", "action": "fine", "declared_after": {"place": "HotelZLobby"}}] + })"), "zone", engine.registry().specs()); + RunState state; + const StoryEngineReport report = engine.run_checkpoint(checkpoint, state); + if (report.verdict != CheckpointVerdict::PASSED){ + return std::format("expected a pass, got {}", to_string(report.run.failure)); + } + return state.pose.map == "HotelZLobby" ? "" : std::format("expected the run's pose in HotelZLobby, got \"{}\"", state.pose.map); +} + +// A declared tile pose is no zone. +std::string zone_rejects_tiles(CancellableScope& scope){ + StoryGame game = flaky_game(0); + FakeStoryBackend backend({}, scope); + StoryEngine engine(game, backend); + StoryCheckpoint checkpoint = checkpoint_of("03.01", {game_step("walk", "fine")}); + checkpoint.start = TileDeclared{.map = "HotelZ", .tiles = {{1, 2}}, .facing = std::nullopt}; + const std::vector lint = engine.registry().lint(checkpoint); + if (lint.size() != 1 || !lint[0].starts_with("03.01 start: \"HotelZ\" is a map")){ + return std::format("expected one start problem, got \"{}\"", joined(lint)); + } + RunState state; + const StoryEngineReport report = engine.run_checkpoint(checkpoint, state); + if (report.run.failure.failure != "lint_failed"){ + return std::format("expected lint_failed, got {}", to_string(report.run.failure)); + } + return expect_inputs(backend, ""); +} + +// A tile game's checkpoint that declares a place, at its start or after +// a step, fails the lint. +std::string grid_rejects_places(CancellableScope&){ + const StoryGame game = flaky_game(0); + const StoryActionRegistry registry(game); + StoryCheckpoint checkpoint = checkpoint_of("00.01", {game_step("walk", "fine")}); + checkpoint.start = ZoneDeclared{.place = "PalletTown", .spot = std::nullopt, .heading = std::nullopt}; + checkpoint.steps[0].pose_after = ZoneDeclared{.place = "Route1", .spot = "Gate", .heading = 90}; + const std::string lint = joined(registry.lint(checkpoint)); + const std::string expected = + "00.01 start: \"PalletTown\" is a place, but this game's poses are tiles: declare a map, tiles and a facing, " + "00.01 step 1 walk: declared_after: \"Route1\" is a place, but this game's poses are tiles: declare a map, tiles and a facing"; + return lint == expected ? "" : std::format("expected \"{}\", got \"{}\"", expected, lint); +} + +// A zone game may not use tile args: not in a step (a route's GRID_POSE +// target), not in its own actions (a LEGS arg). Timed legs are fine. +std::string zone_rejects_tile_args(CancellableScope&){ + StoryGame game; + game.actions.push_back({ + .spec = {.kind = StoryStepKind::GAME, .name = "walk", .args = { + {.key = "legs", .type = StoryArgType::LEGS, .required = true, .fallback = std::nullopt, .why = ""}, + }}, + .handler = nullptr, + }); + game.actions.push_back({ + .spec = {.kind = StoryStepKind::GAME, .name = "timed_walk", .args = { + {.key = "legs", .type = StoryArgType::TIMED_LEGS, .required = true, .fallback = std::nullopt, .why = ""}, + }}, + .handler = nullptr, + }); + const StoryActionRegistry registry(game); + StoryStep route; + route.kind = StoryStepKind::ROUTE; + route.name = "route"; + route.args = {{"target", GridPose{.map = "HotelZ", .tiles = {}, .facing = std::nullopt}}}; + StoryStep timed; + timed.kind = StoryStepKind::GAME; + timed.name = "timed"; + timed.action = "timed_walk"; + timed.args = {{"legs", std::vector{{.turn = 90, .ms = 1200}}}}; + const std::string lint = joined(registry.lint(checkpoint_of("03.01", {route, timed}))); + const std::string expected = + "action walk takes a tile argument \"legs\", but this game has no tiles, " + "03.01 step 1 route: argument \"target\" is a tile one, but this game has no tiles"; + return lint == expected ? "" : std::format("expected \"{}\", got \"{}\"", expected, lint); +} + +// No tile actions in a zone game, so a legs step fails the lint; +// a tile game keeps them. +std::string tile_actions_only_with_tiles(CancellableScope&){ + StoryStep legs; + legs.kind = StoryStepKind::LEGS; + legs.name = "legs"; + legs.args = {{"legs", std::vector{{Direction::NORTH, 1}}}}; + StoryStep face; + face.kind = StoryStepKind::STEP; + face.name = "face"; + face.action = "face"; + face.args = {{"direction", Direction::EAST}}; + const StoryGame zone_game; + const StoryActionRegistry zone(zone_game); + if (joined(zone.check(legs)) != "unknown action \"\" for LEGS" || joined(zone.check(face)) != "unknown action \"face\" for STEP"){ + return std::format("expected the zone game to have no legs or face, got \"{}\", \"{}\"", joined(zone.check(legs)), joined(zone.check(face))); + } + const StoryGame grid_game; + const StoryActionRegistry grid(grid_game); + if (!grid.check(legs).empty() || !grid.check(face).empty()){ + return "expected the tile game to keep legs and face"; + } + return ""; +} + +// A reader that logs "[Test] read " when it runs and fills nothing. +StoryGameReader logging_reader(std::string name, std::vector supplies, StoryReaderUse use){ + StoryGameReader reader; + reader.name = name; + reader.supplies = std::move(supplies); + reader.use = use; + reader.read = [name](StoryBackend& backend, Observation&){ + backend.log(std::format("[Test] read {}", name)); + }; + return reader; +} + +// The readers `checkpoint`'s postcondition check runs on a backend of +// `kind`, as "pose, bag". +std::string readers_run(const StoryCheckpoint& checkpoint, StoryBackendKind kind, CancellableScope& scope){ + StoryGame game; + game.readers.push_back(logging_reader("pose", {"pose"}, StoryReaderUse::AFTER_CHECKPOINT)); + game.readers.push_back(logging_reader("bag", {"bag.*"}, StoryReaderUse::AFTER_CHECKPOINT)); + game.readers.push_back(logging_reader("heading", {"heading"}, StoryReaderUse::ON_REQUEST)); + StoryGameReader flags = logging_reader("flags", {"flag.*"}, StoryReaderUse::AFTER_CHECKPOINT); + flags.console_only = true; + game.readers.push_back(std::move(flags)); + FakeStoryBackend backend({.kind = kind}, scope); + StoryEngine engine(game, backend); + engine.check_postconditions(checkpoint); + std::vector read; + for (const std::string& line : backend.lines()){ + if (line.starts_with("[Test] read ")){ + read.push_back(line.substr(12)); + } + } + return joined(read); +} + +// The postcondition check runs only the readers that opted in and supply +// what it needs, and no console_only one off a console; never the +// heading, which opted out. +std::string readers_opt_in(CancellableScope& scope){ + StoryCheckpoint items = checkpoint_of("01", {}); + items.postconditions.items = {{.item = "POTION", .quantity = 1}}; + std::string read = readers_run(items, StoryBackendKind::SCRIPT, scope); + if (read != "bag"){ + return std::format("expected only the bag read for an item, got \"{}\"", read); + } + StoryCheckpoint place = checkpoint_of("02", {}); + place.postconditions.position = GridPose{.map = "Town", .tiles = {{1, 2}}, .facing = std::nullopt}; + place.postconditions.flags = {"DOOR"}; + read = readers_run(place, StoryBackendKind::SCRIPT, scope); + if (read != "pose"){ + return std::format("expected only the pose read off a console, got \"{}\"", read); + } + read = readers_run(place, StoryBackendKind::CONSOLE, scope); + if (read != "pose, flags"){ + return std::format("expected the pose and the console's flags read, got \"{}\"", read); + } + read = readers_run(checkpoint_of("03", {}), StoryBackendKind::CONSOLE, scope); + return read.empty() ? "" : std::format("expected no reader without postconditions, got \"{}\"", read); +} + +// A game whose "mark" action records "run " and whose pause hook +// records "pause