From 3d68225a5ce8783261d23b1060ed5c43e0bdf34a Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:14:53 -0800 Subject: [PATCH 1/3] AutoStory: checkpoint attempts and story segments --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_CheckpointAttempts.cpp | 36 ++ .../AutoStory/Pokemon_CheckpointAttempts.h | 74 +++ .../AutoStory/Pokemon_StorySegments.cpp | 375 +++++++++++++ .../Pokemon/AutoStory/Pokemon_StorySegments.h | 133 +++++ .../AutoStory/Pokemon_StorySegments_Tests.cpp | 493 ++++++++++++++++++ .../AutoStory/Pokemon_StorySegments_Tests.h | 23 + .../Pokemon_StoryStepExecutor_Tests.cpp | 296 +++++++++++ .../Types/Pokemon_StorySegmentTypes.cpp | 38 ++ .../Types/Pokemon_StorySegmentTypes.h | 91 ++++ SerialPrograms/cmake/SourceFiles.cmake | 8 + 11 files changed, 1569 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index f647539ef1..ddba8e49b1 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -16,6 +16,7 @@ #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" @@ -31,6 +32,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryEvents(database); add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); + add_tests_StorySegments(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp new file mode 100644 index 0000000000..39d63731ce --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp @@ -0,0 +1,36 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryLog.h" +#include "Pokemon_CheckpointAttempts.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts){ + CheckpointAttemptsResult result; + for (size_t attempt = 1;; attempt++){ + result.attempts = attempt; + const AttemptResult outcome = attempts.run(attempt); + if (outcome == AttemptResult::PASSED){ + result.passed = true; + attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.add_checkpoint_prefix)); + return result; + } + if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){ + return result; + } + if (!attempts.reset(attempt + 1)){ + return result; + } + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h new file mode 100644 index 0000000000..3834e234f5 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -0,0 +1,74 @@ +/* Checkpoint Attempts + * + * From: https://github.com/PokemonAutomation/ + * + * The retry loop every AutoStory runs a checkpoint in: run an attempt, and + * when it fails go back to the checkpoint's start save and try again, + * until an attempt passes, fails in a way another attempt would repeat, + * the last attempt fails, or a reset fails. SV's loop + * (Pokemon_CheckpointRetry.h), the story segments and the story engine all + * run their checkpoints through it, so each writes the same pass line + * (Pokemon_StoryLog.h) and counts attempts the same way. The game supplies + * the attempt and the reset. + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointAttempts_H +#define PokemonAutomation_Pokemon_CheckpointAttempts_H + +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How one attempt at a checkpoint ended. +enum class AttemptResult{ + PASSED, + // Failed; another attempt from the start save may pass. + FAILED, + // Failed in a way another attempt would repeat (the game is not + // running): no more attempts. + FINAL, +}; + +// Writes one log line. +using CheckpointLogger = std::function; + +// What the game does at each step of the retry loop. +struct CheckpointAttempts{ + // Attempts before the loop gives up. + size_t max_attempts; + // Runs attempt `attempt`, counting from 1. + std::function run; + // Goes back to the checkpoint's start save before attempt + // `next_attempt`, writing its own log line. Returns false when it could + // not, which ends the loop. + std::function reset; + CheckpointLogger log; + // False when `checkpoint` is the game's own text that already says it + // is a checkpoint, so the pass line leaves out "[Checkpoint] " + // (checkpoint_start_line()). + bool add_checkpoint_prefix = true; +}; + +// What the loop came to. +struct CheckpointAttemptsResult{ + bool passed = false; + // Attempts run, counting the last one. + size_t attempts = 0; +}; + +// Runs `checkpoint` until an attempt passes, an attempt is FINAL, +// `max_attempts` attempts failed, or a reset failed. Writes the pass line. +// Exceptions from `run` and `reset` pass through. +CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, const CheckpointAttempts& attempts); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp new file mode 100644 index 0000000000..2ff6960ed0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -0,0 +1,375 @@ +/* Story Segments + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_CheckpointAttempts.h" +#include "Pokemon_StoryLog.h" +#include "Pokemon_StoryOutcomeClass.h" +#include "Pokemon_StorySegments.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// A line for the log, and without its tag for the status sink, if set. +void say(const std::string& line, SegmentHooks& hooks){ + if (hooks.log){ + hooks.log(line); + } + if (hooks.status){ + hooks.status(story_short_line(line)); + } +} + +std::optional index_of(const Segment& segment, const std::string& id){ + for (size_t c = 0; c < segment.checkpoints.size(); c++){ + if (segment.checkpoints[c].id == id){ + return c; + } + } + return std::nullopt; +} + +// The rows `range` names, both inclusive, or the failure that stops the +// segment before it starts. +struct Selected{ + size_t first = 0; + size_t last = 0; + Outcome failure; +}; + +Selected select(const Segment& segment, const SegmentRange& range){ + Selected selected; + if (segment.checkpoints.empty()){ + selected.failure = outcome_failed("empty_segment"); + return selected; + } + selected.last = segment.checkpoints.size() - 1; + if (range.from){ + const std::optional index = index_of(segment, *range.from); + if (!index){ + selected.failure = outcome_failed("unknown_checkpoint", *range.from); + return selected; + } + selected.first = *index; + } + if (range.to){ + const std::optional index = index_of(segment, *range.to); + if (!index){ + selected.failure = outcome_failed("unknown_checkpoint", *range.to); + return selected; + } + selected.last = *index; + } + if (selected.first > selected.last){ + selected.failure = outcome_failed("bad_range", std::format("{} is after {}", *range.from, *range.to)); + } + return selected; +} + +CheckpointReport failed_report(Outcome failure){ + CheckpointReport report; + report.failure = std::move(failure); + return report; +} + +// Empties the run state and reloads the last save; the reload says where +// the player stands. +Outcome reload(RunState& state, SegmentHooks& hooks){ + state = RunState{}; + if (!hooks.reload){ + return outcome_failed("reload_failed", "no reload hook"); + } + Outcome reloaded = hooks.reload(state); + if (!reloaded.succeeded()){ + return outcome_failed("reload_failed", to_string(reloaded)); + } + if (reloaded.pose){ + state.pose = *reloaded.pose; + } + return outcome_ok(); +} + +// The segment's save hook as a checkpoint's save. +Act hook_save(SegmentHooks& hooks){ + return [&hooks](RunState& state){ + return hooks.save ? hooks.save(state) : outcome_failed("no_save_hook"); + }; +} + +// `save`, after the game's postcondition check of checkpoint `id`: one +// that failed stops the save. Without a check, logs that nothing was +// checked, never that it passed. +Act checked_save(Act save, std::string id, SegmentHooks& hooks){ + return [save = std::move(save), id = std::move(id), &hooks](RunState& state) mutable{ + if (!hooks.check_postconditions){ + say(std::format("[Postcondition] {}: not checked: the game has no postcondition check", id), hooks); + return save(state); + } + const Outcome checked = hooks.check_postconditions(id, state); + if (!checked.succeeded()){ + say(checkpoint_log_line(id, "postconditions failed, not saving"), hooks); + return outcome_failed("postconditions_failed", to_string(checked)); + } + return save(state); + }; +} + +// A row to run, and where it stands in the run. +struct Row{ + CheckpointSpec& spec; + // The run's first: its save is the one the run started from. + bool first = false; + // The run's last: it ends at the run's end state. + bool last = false; +}; + +// The checkpoint's save as `policy.save_mode` allows: one the mode skips +// logs that it did not save; the run's last in AFTER_VERIFICATION saves +// (with the segment's hook if it has none) after the postcondition check. +void set_save(const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, Checkpoint& checkpoint){ + const StorySaveMode mode = policy.save_mode; + const bool saves_at_end = mode == StorySaveMode::AFTER_VERIFICATION && row.last; + if ((row.spec.saves || saves_at_end) && !checkpoint.save){ + checkpoint.save = hook_save(hooks); + } + if (!checkpoint.save){ + return; + } + const SavePoint point = row.last ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; + if (!story_save_allowed(mode, CheckpointVerdict::PASSED, point)){ + checkpoint.save = [&hooks, id = row.spec.id, mode](RunState&){ + say(checkpoint_log_line(id, std::format("not saving (save mode {})", story_save_mode_name(mode))), hooks); + return outcome_ok(); + }; + return; + } + if (saves_at_end){ + checkpoint.save = checked_save(std::move(checkpoint.save), row.spec.id, hooks); + } +} + +// Attempts the row gets: a row after the first has a save at its start +// only when every checkpoint saves, so otherwise a reload would go back +// to the run's start, and it gets one. +size_t row_attempts(const Row& row, const SegmentPolicy& policy){ + if (row.first || policy.save_mode == StorySaveMode::PER_CHECKPOINT){ + return policy.attempts_per_checkpoint; + } + return 1; +} + +// One row to success through run_checkpoint_attempts(), reloading and +// rebuilding after each failed attempt. Returns the last attempt's report, +// or the reload's failure; `run` says how many attempts it took. +CheckpointReport run_row( + const Segment& segment, const Row& row, const SegmentPolicy& policy, + SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run, RunState& state +){ + const size_t max_attempts = row_attempts(row, policy); + CheckpointReport result; + const CheckpointAttempts attempts{ + .max_attempts = max_attempts, + .run = [&](size_t attempt){ + run.attempts = attempt; + Checkpoint checkpoint = row.spec.build(); + set_save(row, policy, hooks, checkpoint); + EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; + result = run_checkpoint(state, checkpoint, log); + if (result.completed){ + return AttemptResult::PASSED; + } + if (result.failure.failure == GAME_NOT_RUNNING){ + say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id), hooks); + return AttemptResult::FINAL; + } + if (attempt >= max_attempts && max_attempts < policy.attempts_per_checkpoint){ + say(std::format( + "[Segment] {}: {} failed; save mode {} left no save at its start, so no reload", + segment.id, row.spec.id, story_save_mode_name(policy.save_mode) + ), hooks); + } + return AttemptResult::FAILED; + }, + .reset = [&](size_t next_attempt){ + say(checkpoint_reset_line(row.spec.id, next_attempt, policy.attempts_per_checkpoint), hooks); + report.reloads++; + Outcome reloaded = reload(state, hooks); + if (!reloaded.succeeded()){ + result = failed_report(std::move(reloaded)); + return false; + } + return true; + }, + .log = [&hooks](const std::string& line){ say(line, hooks); }, + }; + run.passed = run_checkpoint_attempts(row.spec.id, attempts).passed; + return result; +} + +SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& hooks, SegmentReport& report){ + std::string where; + if (!report.failed_checkpoint.empty()){ + where = std::format(" at {}", report.failed_checkpoint); + if (!report.failed_event.empty()){ + where += std::format(" {}", report.failed_event); + } + } + say(std::format("[Segment] {}: failed{}: {}", id, where, to_string_with_class(failure)), hooks); + report.failure = std::move(failure); + return report; +} + +} // namespace + + +SegmentReport run_segment( + const SegmentRange& range, const SegmentPolicy& policy, + Segment& segment, SegmentHooks& hooks, RunState& state +){ + SegmentReport report; + const Selected selected = select(segment, range); + if (!selected.failure.succeeded()){ + return stop_segment(segment.id, selected.failure, hooks, report); + } + say(std::format("[Segment] {}: start, {} checkpoints", segment.id, selected.last - selected.first + 1), hooks); + if (policy.save_mode != StorySaveMode::PER_CHECKPOINT){ + say(std::format("[Segment] {}: save mode {}", segment.id, story_save_mode_name(policy.save_mode)), hooks); + } + for (size_t c = selected.first; c <= selected.last; c++){ + CheckpointSpec& spec = segment.checkpoints[c]; + report.checkpoints_run++; + report.runs.push_back(CheckpointRun{.id = spec.id}); + const Row row{.spec = spec, .first = c == selected.first, .last = c == selected.last}; + CheckpointReport result = run_row(segment, row, policy, hooks, report, report.runs.back(), state); + if (!result.completed){ + report.failed_checkpoint = spec.id; + report.failed_event = result.failed_name; + return stop_segment(segment.id, std::move(result.failure), hooks, report); + } + } + report.completed = true; + say(std::format("[Segment] {}: {} of {} checkpoints one-shot", segment.id, report.one_shot_passes(), report.checkpoints_run), hooks); + say(std::format("[Segment] {}: done, {} checkpoints", segment.id, report.checkpoints_run), hooks); + return report; +} + + +namespace{ + +// One verify run: the checkpoint built fresh. Its end save only if +// `save_at_end` (AFTER_VERIFICATION's last run, every run before it +// passed), after the postcondition check. Returns true if it passed. +bool verify_run( + size_t run, const VerifyPolicy& policy, bool save_at_end, + CheckpointSpec& spec, SegmentHooks& hooks, RunState& state +){ + Checkpoint checkpoint = spec.build(); + if (save_at_end){ + Act save = checkpoint.save ? std::move(checkpoint.save) : hook_save(hooks); + checkpoint.save = checked_save(std::move(save), spec.id, hooks); + }else{ + checkpoint.save = nullptr; + } + EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; + const CheckpointReport result = run_checkpoint(state, checkpoint, log); + if (result.completed){ + say(std::format("[Verify] {}: run {}/{} passed", spec.id, run, policy.runs), hooks); + return true; + } + say(std::format( + "[Verify] {}: run {}/{} failed{}: {}", + spec.id, run, policy.runs, + result.failed_name.empty() ? "" : std::format(" at {}", result.failed_name), + to_string_with_class(result.failure) + ), hooks); + return false; +} + +// AFTER_VERIFICATION saves at the end of the last run when every run +// before it passed. +bool verify_saves_after(const VerifyReport& report, const VerifyPolicy& policy, size_t run){ + return policy.save_mode == StorySaveMode::AFTER_VERIFICATION && run == policy.runs && report.passes + 1 == run; +} + +VerifyReport verify_summary(const VerifyPolicy& policy, SegmentHooks& hooks, VerifyReport& report){ + if (policy.save_mode == StorySaveMode::AFTER_VERIFICATION){ + say(std::format( + "[Verify] {}: {}", report.checkpoint, + report.saved ? "every run passed: saved at the end state" : "not every run passed: not saving" + ), hooks); + } + report.verified = report.failure.succeeded() && report.passes >= policy.passes_needed; + say(std::format( + "[Verify] {}: {}/{} passed, {} needed: {}", + report.checkpoint, report.passes, policy.runs, policy.passes_needed, + report.verified ? "verified" : "not verified" + ), hooks); + return report; +} + +} // namespace + + +VerifyReport verify_checkpoint( + const std::string& id, const VerifyPolicy& policy, + Segment& segment, SegmentHooks& hooks, RunState& state +){ + VerifyReport report; + report.checkpoint = id; + const std::optional index = index_of(segment, id); + if (!index){ + report.failure = outcome_failed("unknown_checkpoint", id); + }else if (policy.runs == 0 || policy.passes_needed > policy.runs){ + report.failure = outcome_failed("bad_verify_policy", std::format("{} of {} runs", policy.passes_needed, policy.runs)); + } + if (!report.failure.succeeded()){ + say(std::format("[Verify] {}: {}", id, to_string(report.failure)), hooks); + return report; + } + CheckpointSpec& spec = segment.checkpoints[*index]; + say(std::format( + "[Verify] {}: start, {} runs from one save, {} must pass", id, policy.runs, policy.passes_needed + ), hooks); + for (size_t run = 1; run <= policy.runs; run++){ + if (run > 1){ + Outcome reloaded = reload(state, hooks); + if (!reloaded.succeeded()){ + say(std::format("[Verify] {}: no reload before run {}: {}", id, run, to_string(reloaded)), hooks); + report.failure = std::move(reloaded); + break; + } + } + report.runs++; + const bool save_at_end = verify_saves_after(report, policy, run); + const bool passed = verify_run(run, policy, save_at_end, spec, hooks, state); + report.passes += passed ? 1 : 0; + report.saved = save_at_end && passed; + } + return verify_summary(policy, hooks, report); +} + + +std::string describe(Segment& segment){ + std::string text = std::format("segment {}: {} checkpoints\n", segment.id, segment.checkpoints.size()); + for (const CheckpointSpec& spec : segment.checkpoints){ + text += std::format(" {} \"{}\"{}\n", spec.id, spec.title, spec.saves ? ", saves" : ""); + } + for (CheckpointSpec& spec : segment.checkpoints){ + if (spec.build){ + text += describe(spec.build()); + } + } + return text; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h new file mode 100644 index 0000000000..280504487f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h @@ -0,0 +1,133 @@ +/* Story Segments + * + * From: https://github.com/PokemonAutomation/ + * + * A story segment as a table of checkpoints. Each row names a checkpoint + * and holds a factory that builds it, so every attempt runs a checkpoint + * built fresh, with nothing the last attempt used up. The runner takes the + * checkpoints in order from a save; a checkpoint that fails is answered + * the one way the event model has no recovery for: the game is reloaded + * to its last save, the checkpoint is rebuilt and run again, up to the + * policy's attempts. Then the segment stops and the report names the + * checkpoint, the event and the reason. + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySegments_H +#define PokemonAutomation_Pokemon_StorySegments_H + +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryEvents.h" +#include "Pokemon_StoryLog.h" +#include "Types/Pokemon_StorySegmentTypes.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// One row of a segment. `build` makes the checkpoint anew for each attempt. +// `saves` has the segment save the game (SegmentHooks::save) after the +// checkpoint's end check, when the checkpoint has no save of its own. +struct CheckpointSpec{ + std::string id; + std::string title; + bool saves = false; + MoveOnlyFunction build; +}; + +// Collection of ordered checkpoints that make up a story segment. +struct Segment{ + std::string id; + std::vector checkpoints; +}; + + +// What the game lends the runner: how to save, how to reload the last save +// (the run state is emptied first; the reload's Outcome says where the +// player stands), and where to log. +struct SegmentHooks{ + Act save; + Act reload; + EventLogger log; + // Optional: each [Segment], [Checkpoint] and [Event] line again, + // without its tag ("00.03/2 below_oaks_trigger: start"), for the video + // overlay, so whoever watches the stream sees the step under way; and, + // once run records land (#132), for the run's events. + EventLogger status; + // Optional: the game's postcondition check of a checkpoint, by id + // (check_postconditions(), Pokemon_StoryPostconditionCheck.h), run + // before a save in AFTER_VERIFICATION mode: one that failed stops the + // save and fails the run. The game logs its lines. + MoveOnlyFunction check_postconditions; +}; + +// Runs the checkpoints in `range`, in order, saving as `policy.save_mode` +// allows (story_save_allowed()). Logs through `hooks.log`, +// around the checkpoint lines: +// [Segment] 00: start, 3 checkpoints +// [Checkpoint] 00.02: failed, resetting for attempt 2/3 +// [Checkpoint] 00.02: passed on attempt 2 (1 reset) +// [Checkpoint] 00.03: passed on attempt 1 (one-shot) +// [Segment] 00: 2 of 3 checkpoints one-shot +// [Segment] 00: done, 3 checkpoints +// [Segment] 00: failed at 00.02 talk_mom: expect_failed: dialogue opened +SegmentReport run_segment( + const SegmentRange& range, const SegmentPolicy& policy, + Segment& segment, SegmentHooks& hooks, RunState& state +); + + +// Verify mode, for a checkpoint that must be shown to hold up (a battle +// lost twice or more gets its own checkpoint, saved right before it, and is +// verified this way): the checkpoint run `runs` times, each from the same +// save. The checkpoint never saves at its end, so each run after the first +// reloads the save the first started from. +struct VerifyPolicy{ + size_t runs = 3; + // Passing runs the checkpoint needs to be verified. + size_t passes_needed = 2; + // AFTER_VERIFICATION saves once, at the end of the last run, when + // every run passed; any other mode never saves. + StorySaveMode save_mode = StorySaveMode::NONE; +}; + +// Verification results for multiple runs of a checkpoint. +struct VerifyReport{ + std::string checkpoint; + size_t runs = 0; + size_t passes = 0; + // At least VerifyPolicy::passes_needed runs passed. + bool verified = false; + // AFTER_VERIFICATION: every run passed and the last saved at its end. + bool saved = false; + // What stopped the runs early: an unknown checkpoint, a bad policy or + // a failed reload. Runs that fail are counted, not stopped at. + Outcome failure; +}; + +// Runs checkpoint `id` of `segment` as `policy` says, from the save the +// game is on. No retries, no in-place recoveries and no save at its end, +// but for `policy.save_mode` AFTER_VERIFICATION's one save at the end of +// the last run, when every run passed (after the postcondition check). +// Logs through `hooks.log`, after each run's checkpoint lines: +// [Verify] 00.15b: start, 3 runs from one save, 2 must pass +// [Verify] 00.15b: run 1/3 passed +// [Verify] 00.15b: run 2/3 failed at rival_battle: battle_lost +// [Verify] 00.15b: 2/3 passed, 2 needed: verified +VerifyReport verify_checkpoint( + const std::string& id, const VerifyPolicy& policy, + Segment& segment, SegmentHooks& hooks, RunState& state +); + +// A readable dump for review: the rows, then each checkpoint built once +// and described as describe(const Checkpoint&) does. Nothing runs. +std::string describe(Segment& segment); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp new file mode 100644 index 0000000000..721007b6c6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp @@ -0,0 +1,493 @@ +/* Story Segments Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The segment runner against a scripted world with a save slot: a few + * fields the checkpoints' events change, saved and reloaded by the hooks. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryEvents.h" +#include "Pokemon_StorySegments.h" +#include "Pokemon_StorySegments_Tests.h" +#include "Pokemon_TilePose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +// Twinleaf Town to Sandgem Town, shortened. +struct World{ + std::string map = "Twinleaf Town"; + bool parcel = false; + // Walks south that fail before one gets through. + int walk_failures = 0; + std::vector trace; +}; + +Pose at_home_door(){ + Pose pose; + pose.map = "Twinleaf Town"; + pose.certainty = PoseCertainty::TILE; + pose.where = std::make_shared(std::vector{TilePos{5, 9}}, Direction::SOUTH); + return pose; +} + +// The world, its save slot, and the log. +struct Lab{ + World world; + World saved; + std::vector lines; + + SegmentHooks hooks(){ + SegmentHooks hooks; + hooks.save = [this](RunState&){ + world.trace.push_back("save"); + saved = world; + saved.trace.clear(); + return outcome_ok(); + }; + hooks.reload = [this](RunState&){ + // Whatever blocked a walk has moved on by the reload. + const int failures = world.walk_failures; + std::vector trace = std::move(world.trace); + world = saved; + world.walk_failures = failures; + world.trace = std::move(trace); + world.trace.push_back("reload"); + return outcome_ok(at_home_door()); + }; + hooks.log = [this](const std::string& line){ lines.push_back(line); }; + return hooks; + } + bool has(const std::string& line) const{ + return std::find(lines.begin(), lines.end(), line) != lines.end(); + } + std::string all() const{ + std::string text; + for (const std::string& line : lines){ + text += std::format("\n{}", line); + } + return text; + } +}; + +Event make_event(std::string name, Act act){ + Event event; + event.name = std::move(name); + event.act = std::move(act); + return event; +} + +Condition on_map(std::string map, World& world){ + return condition(std::format("on {}", map), [&world, map](RunState&){ return world.map == map; }); +} + +CheckpointSpec leave_home(World& world){ + CheckpointSpec spec; + spec.id = "00.01"; + spec.title = "Leave the house"; + spec.saves = true; + spec.build = [&world]{ + Checkpoint checkpoint; + checkpoint.id = "00.01"; + Event walk = make_event("walk_out", [&world](RunState&){ + world.trace.push_back("walk_out"); + world.map = "Route 201"; + return outcome_ok(); + }); + walk.why = "the story starts outside"; + walk.skip_if.push_back(on_map("Route 201", world)); + checkpoint.events.push_back(std::move(walk)); + checkpoint.end.push_back(on_map("Route 201", world)); + return checkpoint; + }; + return spec; +} + +CheckpointSpec walk_to_sandgem(World& world){ + CheckpointSpec spec; + spec.id = "00.02"; + spec.title = "Walk to Sandgem Town"; + spec.build = [&world]{ + Checkpoint checkpoint; + checkpoint.id = "00.02"; + Event walk = make_event("walk_south", [&world](RunState&){ + world.trace.push_back("walk_south"); + if (world.walk_failures > 0){ + world.walk_failures--; + return outcome_failed("blocked", "a wanderer on the path"); + } + world.map = "Sandgem Town"; + return outcome_ok(); + }); + walk.require.push_back(on_map("Route 201", world)); + walk.expect.push_back(on_map("Sandgem Town", world)); + Recovery stop; + stop.kind = RecoveryKind::STOP; + stop.why = "the path is one tile wide"; + walk.recoveries.push_back(std::move(stop)); + checkpoint.events.push_back(std::move(walk)); + return checkpoint; + }; + return spec; +} + +CheckpointSpec get_parcel(World& world){ + CheckpointSpec spec; + spec.id = "00.03"; + spec.title = "Get the Parcel"; + spec.build = [&world]{ + Checkpoint checkpoint; + checkpoint.id = "00.03"; + Event talk = make_event("talk_clerk", [&world](RunState&){ + world.trace.push_back("talk_clerk"); + world.parcel = true; + return outcome_ok(); + }); + talk.expect.push_back(condition("parcel in the Bag", [&world](RunState&){ return world.parcel; })); + talk.pose_learned = at_home_door(); + checkpoint.events.push_back(std::move(talk)); + return checkpoint; + }; + return spec; +} + +Segment twinleaf_to_sandgem(World& world){ + Segment segment; + segment.id = "00"; + segment.checkpoints.push_back(leave_home(world)); + segment.checkpoints.push_back(walk_to_sandgem(world)); + segment.checkpoints.push_back(get_parcel(world)); + return segment; +} + + +UnitTestResult test_runs_checkpoints_in_order(){ + Lab lab; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); + const std::vector expected = {"walk_out", "save", "walk_south", "talk_clerk"}; + if (!report.completed || report.checkpoints_run != 3 || lab.world.trace != expected){ + return std::format("Expected three checkpoints, saved after the first:{}", lab.all()); + } + if (!lab.has("[Segment] 00: start, 3 checkpoints") || lab.lines.back() != "[Segment] 00: done, 3 checkpoints"){ + return std::format("Unexpected log:{}", lab.all()); + } + if (lab.saved.map != "Route 201"){ + return "The save should hold the world after the first checkpoint."; + } + return true; +} + +UnitTestResult test_reloads_a_failed_checkpoint(){ + Lab lab; + lab.world.walk_failures = 1; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); + const std::vector expected = {"walk_out", "save", "walk_south", "reload", "walk_south", "talk_clerk"}; + if (!report.completed || report.reloads != 1 || lab.world.trace != expected){ + return std::format("Expected a reload, then the walk again:{}", lab.all()); + } + if (!lab.has("[Checkpoint] 00.02: failed, resetting for attempt 2/3")){ + return std::format("Expected the reload logged:{}", lab.all()); + } + return true; +} + +UnitTestResult test_gives_up_and_names_the_failure(){ + Lab lab; + lab.world.walk_failures = 10; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{.attempts_per_checkpoint = 2}, segment, hooks, state); + if (report.completed || report.failed_checkpoint != "00.02" || report.failed_event != "walk_south"){ + return std::format("Expected the failed checkpoint and event named:{}", lab.all()); + } + if (report.failure.failure != "blocked" || report.reloads != 1 || report.checkpoints_run != 2){ + return std::format("Expected two attempts with one reload between them:{}", lab.all()); + } + if (lab.world.parcel || lab.lines.back() != "[Segment] 00: failed at 00.02 walk_south: blocked: a wanderer on the path"){ + return std::format("Nothing after the failed checkpoint should run:{}", lab.all()); + } + if (state.pose.map != "Twinleaf Town" || state.pose.certainty != PoseCertainty::TILE){ + return std::format("The reload should have set the pose: {}", to_string(state.pose)); + } + return true; +} + +UnitTestResult test_range(){ + Lab lab; + lab.world.map = "Route 201"; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + SegmentReport report = run_segment(SegmentRange{.from = "00.02", .to = "00.02"}, SegmentPolicy{}, segment, hooks, state); + if (!report.completed || lab.world.trace != std::vector{"walk_south"}){ + return std::format("Expected only the second checkpoint:{}", lab.all()); + } + report = run_segment(SegmentRange{.from = "00.09", .to = std::nullopt}, SegmentPolicy{}, segment, hooks, state); + if (report.completed || report.failure.failure != "unknown_checkpoint" || report.failure.detail != "00.09"){ + return std::format("Expected the unknown id reported:{}", lab.all()); + } + report = run_segment(SegmentRange{.from = "00.03", .to = "00.01"}, SegmentPolicy{}, segment, hooks, state); + if (report.completed || report.failure.failure != "bad_range"){ + return std::format("Expected the reversed range reported:{}", lab.all()); + } + return true; +} + +// The status sink gets every log line again, without its tag, for the +// video overlay. +UnitTestResult test_status_lines(){ + Lab lab; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + std::vector status; + hooks.status = [&status](const std::string& line){ status.push_back(line); }; + RunState state; + run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); + if (status.size() != lab.lines.size() || status.empty()){ + return std::format("Expected a status line per log line:{}", lab.all()); + } + for (size_t c = 0; c < status.size(); c++){ + const std::string& line = lab.lines[c]; + if (line.size() <= status[c].size() || line.compare(line.size() - status[c].size(), status[c].size(), status[c]) != 0){ + return std::format("Status \"{}\" isn't log line \"{}\" without its tag.", status[c], line); + } + } + if (std::find(status.begin(), status.end(), "00.01/1 walk_out: start") == status.end()){ + return "Expected \"00.01/1 walk_out: start\" on the overlay."; + } + return true; +} + +// An event that throws (a console step failing deep down) is logged as +// failed, with its checkpoint, and the exception goes on to the game. +UnitTestResult test_throwing_event_is_logged(){ + Lab lab; + Segment segment; + segment.id = "00"; + CheckpointSpec spec; + spec.id = "00.09"; + spec.build = []{ + Checkpoint checkpoint; + checkpoint.id = "00.09"; + checkpoint.events.push_back(make_event("brock", [](RunState&) -> Outcome{ + throw std::runtime_error("the console vanished"); + })); + return checkpoint; + }; + segment.checkpoints.push_back(std::move(spec)); + SegmentHooks hooks = lab.hooks(); + RunState state; + try{ + run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); + }catch (std::runtime_error&){ + if (!lab.has("[Event] 00.09/1 brock: failed: the console vanished") + || !lab.has("[Checkpoint] 00.09: failed at 1 brock: the console vanished") + ){ + return std::format("Expected the event and checkpoint logged as failed:{}", lab.all()); + } + return true; + } + return "Expected the exception to go on to the game."; +} + +// Verify mode: every run starts from the save the first started from, a +// failed run is counted, not retried, and 2 of 3 passes verify it. +UnitTestResult test_verify_runs_from_one_save(){ + Lab lab; + lab.world.map = "Route 201"; + lab.saved = lab.world; + lab.world.walk_failures = 1; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const VerifyReport report = verify_checkpoint("00.02", VerifyPolicy{}, segment, hooks, state); + const std::vector expected = {"walk_south", "reload", "walk_south", "reload", "walk_south"}; + if (!report.verified || report.runs != 3 || report.passes != 2 || lab.world.trace != expected){ + return std::format("Expected three runs, reloading between them, two passing:{}", lab.all()); + } + if (!lab.has("[Verify] 00.02: start, 3 runs from one save, 2 must pass") + || !lab.has("[Verify] 00.02: run 1/3 failed at walk_south: blocked: a wanderer on the path") + || !lab.has("[Verify] 00.02: run 2/3 passed") + || lab.lines.back() != "[Verify] 00.02: 2/3 passed, 2 needed: verified" + ){ + return std::format("Expected a line per run, then the summary:{}", lab.all()); + } + return true; +} + +// A checkpoint that saves at its end doesn't in verify mode: the next run +// would start after it. +UnitTestResult test_verify_skips_the_end_save(){ + Lab lab; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const VerifyReport report = verify_checkpoint("00.01", VerifyPolicy{.runs = 2, .passes_needed = 2}, segment, hooks, state); + const std::vector expected = {"walk_out", "reload", "walk_out"}; + if (!report.verified || lab.world.trace != expected || lab.saved.map != "Twinleaf Town"){ + return std::format("Expected two runs from Twinleaf Town and no save:{}", lab.all()); + } + return true; +} + +UnitTestResult test_verify_too_few_passes(){ + Lab lab; + lab.world.map = "Route 201"; + lab.saved = lab.world; + lab.world.walk_failures = 2; + Segment segment = twinleaf_to_sandgem(lab.world); + SegmentHooks hooks = lab.hooks(); + RunState state; + const VerifyReport report = verify_checkpoint("00.02", VerifyPolicy{}, segment, hooks, state); + if (report.verified || report.runs != 3 || report.passes != 1 + || lab.lines.back() != "[Verify] 00.02: 1/3 passed, 2 needed: not verified" + ){ + return std::format("Expected 1 of 3, not verified:{}", lab.all()); + } + VerifyReport bad = verify_checkpoint("00.02", VerifyPolicy{.runs = 2, .passes_needed = 3}, segment, hooks, state); + if (bad.verified || bad.runs != 0 || bad.failure.failure != "bad_verify_policy"){ + return std::format("Expected more passes than runs refused:{}", lab.all()); + } + bad = verify_checkpoint("00.09", VerifyPolicy{}, segment, hooks, state); + if (bad.verified || bad.runs != 0 || bad.failure.failure != "unknown_checkpoint"){ + return std::format("Expected an unknown checkpoint refused:{}", lab.all()); + } + return true; +} + +UnitTestResult test_describe(){ + Lab lab; + Segment segment = twinleaf_to_sandgem(lab.world); + const std::string expected = + "segment 00: 3 checkpoints\n" + " 00.01 \"Leave the house\", saves\n" + " 00.02 \"Walk to Sandgem Town\"\n" + " 00.03 \"Get the Parcel\"\n" + "checkpoint 00.01: 1 events\n" + " event 1 walk_out: the story starts outside\n" + " skip_if: on Route 201\n" + " end: on Route 201\n" + "checkpoint 00.02: 1 events\n" + " event 1 walk_south\n" + " require: on Route 201\n" + " expect: on Sandgem Town\n" + " recovery: stop on any failure: the path is one tile wide\n" + "checkpoint 00.03: 1 events\n" + " event 1 talk_clerk\n" + " expect: parcel in the Bag\n" + " pose_learned: Twinleaf Town (5, 9) facing south (tile)\n"; + const std::string text = describe(segment); + if (text != expected){ + return std::format("Unexpected description:\n{}", text); + } + if (!lab.world.trace.empty()){ + return "Describing a segment should run nothing."; + } + return true; +} + + +struct StorySegmentTestCase{ + std::string name; + struct Input{ + UnitTestResult (*test)(); + }; + + struct Expectation{ + std::string_view description; + }; + + Input input; + Expectation expectation; +}; + +class Test_StorySegments : public UnitTest{ +public: + explicit Test_StorySegments(StorySegmentTestCase scenario) + : UnitTest(std::format("Pokemon::StorySegments - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_scenario.input.test(); + } + +private: + StorySegmentTestCase m_scenario; +}; + +const StorySegmentTestCase STORY_SEGMENT_CASES[] = { + { + .name = "checkpoints run in order", .input = {.test = test_runs_checkpoints_in_order}, + .expectation = {.description = "executes sequential checkpoints and saves state"}, + }, + { + .name = "a failed checkpoint is reloaded", .input = {.test = test_reloads_a_failed_checkpoint}, + .expectation = {.description = "reloads prior saved state upon checkpoint failure"}, + }, + { + .name = "gives up and names the failure", .input = {.test = test_gives_up_and_names_the_failure}, + .expectation = {.description = "reports terminal checkpoint failure after exhausting retries"}, + }, + { + .name = "a range of checkpoints", .input = {.test = test_range}, + .expectation = {.description = "runs only the specified slice of checkpoints"}, + }, + { + .name = "describe", .input = {.test = test_describe}, + .expectation = {.description = "formats human-readable segment summary without executing"}, + }, + { + .name = "status lines for the overlay", .input = {.test = test_status_lines}, + .expectation = {.description = "generates UI overlay status updates during execution"}, + }, + { + .name = "a throwing event is logged, then goes on", .input = {.test = test_throwing_event_is_logged}, + .expectation = {.description = "handles and logs exception without terminating segment"}, + }, + { + .name = "verify: runs from one save", .input = {.test = test_verify_runs_from_one_save}, + .expectation = {.description = "runs verification passes against the initial save state"}, + }, + { + .name = "verify: no save at the end", .input = {.test = test_verify_skips_the_end_save}, + .expectation = {.description = "skips checkpoint state save during verification mode"}, + }, + { + .name = "verify: too few passes", .input = {.test = test_verify_too_few_passes}, + .expectation = {.description = "fails verification when required pass count is not met"}, + }, +}; + +} // namespace + + +void add_tests_StorySegments(UnitTestDatabase& database){ + for (const auto& scenario : STORY_SEGMENT_CASES){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h new file mode 100644 index 0000000000..92f97b90c9 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h @@ -0,0 +1,23 @@ +/* Story Segments Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySegments_Tests_H +#define PokemonAutomation_Pokemon_StorySegments_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for story segment running, verification, and branching. +void add_tests_StorySegments(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp index ade175345d..8249895110 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -20,8 +20,10 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_AutoStoryStats.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_BuiltinStoryActions.h" +#include "Pokemon_StorySegments.h" #include "Pokemon_StoryStepExecutor.h" #include "Pokemon_TilePose.h" #include "Pokemon_StoryStepExecutor_Tests.h" @@ -325,6 +327,259 @@ UnitTestResult test_unsupported_and_lost_battles(){ return true; } +// Runs `tables` as one segment whose saves and reloads go into the +// executor's trace and whose log lines go into `lines`. Whatever blocked a +// step has gone by the reload. +SegmentReport run_tables(std::span tables, FakeExecutor& executor, std::vector& lines){ + Segment segment; + segment.id = "00"; + for (const StoryCheckpoint& data : tables){ + segment.checkpoints.push_back(CheckpointSpec{ + .id = data.id, .title = data.title, .saves = true, + .build = [&data, &executor]{ return build_story_checkpoint(data, executor); }, + }); + } + SegmentHooks hooks; + hooks.save = [&executor](RunState&){ + executor.trace.push_back("save"); + return outcome_ok(); + }; + hooks.reload = [&executor](RunState&){ + executor.trace.push_back("reload"); + executor.failing.clear(); + return outcome_ok(); + }; + hooks.log = [&lines](const std::string& line){ lines.push_back(line); }; + RunState state; + return run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); +} + +UnitTestResult test_segment_of_tables(){ + // Two rows built into a segment, run with saves and a reload from the + // game's hooks: the second row's first attempt fails, its retry starts + // over from its start pose. + const std::vector tables{ + lab_checkpoint(), + StoryCheckpoint{ + .id = "00.04", + .title = "the parcel", + .start = declared("Lab", {6, 4}, Direction::NORTH), + .steps = {make(StoryStepKind::TALK, "give_parcel")}, + }, + }; + FakeExecutor executor; + executor.failing = {"give_parcel"}; + std::vector lines; + const SegmentReport report = run_tables(tables, executor, lines); + const std::vector tail{"save", "TALK give_parcel", "reload", "TALK give_parcel", "save"}; + const bool tail_matches = executor.trace.size() >= tail.size() + && std::equal(tail.begin(), tail.end(), executor.trace.end() - (std::ptrdiff_t)tail.size()); + if (!report.completed || report.reloads != 1 || !tail_matches){ + return std::format("Expected 00.04 reloaded once and passed:{}{}", joined(executor.trace), joined(lines)); + } + // Each attempt is begun anew: 00.03 once, 00.04 twice. + if (executor.attempts != std::vector{"00.03", "00.04", "00.04"}){ + return std::format("Expected the executor told of each attempt:{}", joined(executor.attempts)); + } + // The report and the log tell a one-shot pass from a retried one. + const bool runs_ok = report.runs.size() == 2 + && report.runs[0].one_shot() && report.runs[0].attempts == 1 + && !report.runs[1].one_shot() && report.runs[1].attempts == 2 && report.runs[1].passed + && report.one_shot_passes() == 1; + const auto has = [&lines](std::string_view line){ + return std::find(lines.begin(), lines.end(), line) != lines.end(); + }; + if (!runs_ok + || !has("[Checkpoint] 00.03: passed on attempt 1 (one-shot)") + || !has("[Checkpoint] 00.04: passed on attempt 2 (1 reset)") + || !has("[Segment] 00: 1 of 2 checkpoints one-shot") + ){ + return std::format("Expected 00.03 one-shot and 00.04 passed on attempt 2:{}", joined(lines)); + } + return true; +} + + +// The two tables of test_segment_of_tables() as a segment run by `executor`, +// with hooks that write saves and reloads into its trace. +struct PolicyLab{ + std::vector tables{ + lab_checkpoint(), + StoryCheckpoint{ + .id = "00.04", + .title = "the parcel", + .start = declared("Lab", {6, 4}, Direction::NORTH), + .steps = {make(StoryStepKind::TALK, "give_parcel")}, + }, + }; + FakeExecutor executor; + std::vector lines; + + SegmentReport run(const SegmentPolicy& policy){ + Segment segment; + segment.id = "00"; + for (const StoryCheckpoint& data : tables){ + segment.checkpoints.push_back(CheckpointSpec{ + .id = data.id, .title = data.title, .saves = true, + .build = [&data, this]{ return build_story_checkpoint(data, executor); }, + }); + } + SegmentHooks hooks; + hooks.save = [this](RunState&){ + executor.trace.push_back("save"); + return outcome_ok(); + }; + hooks.reload = [this](RunState&){ + executor.trace.push_back("reload"); + return outcome_ok(); + }; + hooks.log = [this](const std::string& line){ lines.push_back(line); }; + RunState state; + return run_segment(SegmentRange{}, policy, segment, hooks, state); + } + bool has(std::string_view line) const{ + return std::find(lines.begin(), lines.end(), line) != lines.end(); + } + size_t count(std::string_view item) const{ + return (size_t)std::count(executor.trace.begin(), executor.trace.end(), item); + } +}; + +UnitTestResult test_policy_attempts_then_fail(){ + // Three attempts by default, then the segment stops on the last one's failure. + PolicyLab lab; + lab.executor.failing = {"give_parcel"}; + const SegmentReport report = lab.run(SegmentPolicy{}); + if (report.completed || report.reloads != 2 || lab.count("TALK give_parcel") != 3 + || report.runs.size() != 2 || report.runs[1].passed || report.runs[1].attempts != 3 + || report.failed_checkpoint != "00.04" || report.failure.failure != "blocked" + ){ + return std::format("Expected 00.04 tried 3 times, then failed:{}{}", joined(lab.executor.trace), joined(lab.lines)); + } + return true; +} + +UnitTestResult test_policy_stop_at_first_failure(){ + PolicyLab lab; + lab.executor.failing = {"give_parcel"}; + const SegmentReport report = lab.run(SegmentPolicy{.attempts_per_checkpoint = 1}); + if (report.completed || report.reloads != 0 || lab.count("reload") != 0 + || lab.count("TALK give_parcel") != 1 + ){ + return std::format("Expected a stop at the first failure, with no reload:{}{}", joined(lab.executor.trace), joined(lab.lines)); + } + return true; +} + +// A boss's checkpoint: a TEACH of a move the lead knows (changes +// nothing), a TEACH that teaches, both once per save, then the battle, +// lost on the first attempt. The segment's saves empty `prepared`. +struct BossLab{ + StoryCheckpoint table{ + .id = "00.15b", + .title = "the rival", + .start = declared("Cerulean", {23, 7}, Direction::NORTH), + .steps = {once("teach_known"), once("teach_new"), make(StoryStepKind::BATTLE, "rival")}, + }; + FakeExecutor executor; + PreparedSteps prepared; + std::vector lines; + + static StoryStep once(std::string name){ + StoryStep step = make(StoryStepKind::GAME, name); + step.action = name; + step.once_per_save = true; + return step; + } + BossLab(){ + executor.unchanged = {"teach_known"}; + } + Segment segment(){ + Segment segment; + segment.id = "00"; + segment.checkpoints.push_back(CheckpointSpec{ + .id = table.id, .title = table.title, .saves = true, + .build = [this]{ return build_story_checkpoint(table, executor, &prepared); }, + }); + return segment; + } + SegmentHooks hooks(){ + SegmentHooks hooks; + hooks.save = [this](RunState&){ + executor.trace.push_back("save"); + prepared.clear(); + return outcome_ok(); + }; + hooks.reload = [this](RunState&){ + executor.trace.push_back("reload"); + return outcome_ok(); + }; + hooks.log = [this](const std::string& line){ lines.push_back(line); }; + return hooks; + } + size_t count(std::string_view item) const{ + return (size_t)std::count(executor.trace.begin(), executor.trace.end(), item); + } +}; + +// A retry after a reload of the same save skips the preparation that +// changed nothing and goes to the battle; the TEACH that taught runs +// again, as the reload undid it. The save at the end empties the memory. +UnitTestResult test_once_per_save_retry(){ + BossLab lab; + StoryStep battle = make(StoryStepKind::BATTLE, "rival"); + battle.args = {{"may_lose", true}}; + lab.table.steps.back() = battle; + lab.executor.failures_left = {{"rival", 1}}; + Segment segment = lab.segment(); + SegmentHooks hooks = lab.hooks(); + RunState state; + const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); + if (!report.completed || report.reloads != 1 || lab.count("GAME teach_known") != 1 + || lab.count("GAME teach_new") != 2 || lab.count("BATTLE rival") != 2 + ){ + return std::format("Expected the known TEACH once, the new one twice, the battle twice:{}{}", joined(lab.executor.trace), joined(lab.lines)); + } + if (!lab.prepared.empty()){ + return "Expected the save to empty the prepared steps."; + } + return true; +} + +// Verify mode: three runs from one save; the preparation that changed +// nothing runs in the first only. +UnitTestResult test_once_per_save_verify(){ + BossLab lab; + StoryStep battle = make(StoryStepKind::BATTLE, "rival"); + battle.args = {{"may_lose", true}}; + lab.table.steps.back() = battle; + Segment segment = lab.segment(); + SegmentHooks hooks = lab.hooks(); + RunState state; + const VerifyReport report = verify_checkpoint("00.15b", VerifyPolicy{}, segment, hooks, state); + if (!report.verified || report.runs != 3 || lab.count("GAME teach_known") != 1 + || lab.count("GAME teach_new") != 3 || lab.count("BATTLE rival") != 3 + ){ + return std::format("Expected the known TEACH once in 3 runs, the new one each run:{}{}", joined(lab.executor.trace), joined(lab.lines)); + } + // Without the memory, every run prepares. + FakeExecutor plain; + plain.unchanged = {"teach_known"}; + Segment without; + without.id = "00"; + without.checkpoints.push_back(CheckpointSpec{ + .id = lab.table.id, .title = "", .saves = true, + .build = [&]{ return build_story_checkpoint(lab.table, plain); }, + }); + RunState again; + verify_checkpoint("00.15b", VerifyPolicy{}, without, hooks, again); + if (std::count(plain.trace.begin(), plain.trace.end(), "GAME teach_known") != 3){ + return std::format("Expected every run to prepare without PreparedSteps:{}", joined(plain.trace)); + } + return true; +} + + // An executor with pause points pauses before each of its checkpoint's steps, // labelled with the checkpoint and the step's place; a skipped step does not // pause, as it does not run. @@ -351,6 +606,23 @@ UnitTestResult test_pause_points(){ } +// A segment's report in the shared AutoStory stats. +UnitTestResult test_story_stats(){ + SegmentReport report; + report.reloads = 2; + report.runs = { + CheckpointRun{.id = "00.03", .passed = true, .attempts = 1}, + CheckpointRun{.id = "00.04", .passed = true, .attempts = 3}, + CheckpointRun{.id = "00.05", .passed = false, .attempts = 1}, + }; + AutoStoryStats stats; + record_story_counts(report.story_counts(), stats); + if (stats.m_segments_started != 1 || stats.m_checkpoints_started != 3 || stats.m_resets != 2){ + return "Expected 1 segment, 3 checkpoints started and 2 resets."; + } + return true; +} + UnitTestResult test_lint(){ StoryStep no_target = make(StoryStepKind::ROUTE, "walk"); StoryStep again = make(StoryStepKind::TALK, "walk"); @@ -426,6 +698,10 @@ const StoryStepTestCase STORY_STEP_CASES[] = { .name = "unsupported kinds and lost battles", .input = {.test = test_unsupported_and_lost_battles}, .expectation = {.description = "flags unsupported step types and handles battle defeat"}, }, + { + .name = "tables as a segment, with a reload", .input = {.test = test_segment_of_tables}, + .expectation = {.description = "converts step tables into segment with checkpoint reload support"}, + }, { .name = "pause points pause before each step", .input = {.test = test_pause_points}, .expectation = {.description = "calls the game's pause points before each step that runs"}, @@ -434,6 +710,26 @@ const StoryStepTestCase STORY_STEP_CASES[] = { .name = "lint", .input = {.test = test_lint}, .expectation = {.description = "verifies lint validation rules on story checkpoints"}, }, + { + .name = "a segment's run in the AutoStory stats", .input = {.test = test_story_stats}, + .expectation = {.description = "updates story run statistics following segment completion"}, + }, + { + .name = "policy: attempts per checkpoint, then fail", .input = {.test = test_policy_attempts_then_fail}, + .expectation = {.description = "enforces max checkpoint attempt policy before terminal failure"}, + }, + { + .name = "policy: one attempt stops at the first failure", .input = {.test = test_policy_stop_at_first_failure}, + .expectation = {.description = "halts without a reload when a checkpoint has one attempt"}, + }, + { + .name = "once per save: a retry skips what changed nothing", .input = {.test = test_once_per_save_retry}, + .expectation = {.description = "skips steps that changed nothing during once per save retry"}, + }, + { + .name = "once per save: verify runs prepare once", .input = {.test = test_once_per_save_verify}, + .expectation = {.description = "executes once per save prepare step only once during verification"}, + }, }; } // namespace diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp new file mode 100644 index 0000000000..cd935cb96e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp @@ -0,0 +1,38 @@ +/* Story Segment Types + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StorySegmentTypes.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats){ + stats.m_segments_started += counts.segments; + stats.m_checkpoints_started += counts.checkpoints; + stats.m_resets += counts.resets; +} + +bool CheckpointRun::one_shot() const{ + return passed && attempts == 1; +} + +size_t SegmentReport::one_shot_passes() const{ + size_t count = 0; + for (const CheckpointRun& run : runs){ + count += run.one_shot() ? 1 : 0; + } + return count; +} + +StoryRunCounts SegmentReport::story_counts() const{ + return StoryRunCounts{.segments = 1, .checkpoints = runs.size(), .resets = reloads}; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h new file mode 100644 index 0000000000..898690f9eb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h @@ -0,0 +1,91 @@ +/* Story Segment Types + * + * From: https://github.com/PokemonAutomation/ + * + * The plain data of a segment run (Pokemon_StorySegments.h): its policy, + * the range of checkpoints to run and the report of what happened. + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySegmentTypes_H +#define PokemonAutomation_Pokemon_StorySegmentTypes_H + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" +#include "Pokemon/AutoStory/Pokemon_StoryEvents.h" +#include "Pokemon_StorySaveMode.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Retry and timeout configuration for story segment execution. +struct SegmentPolicy{ + // Runs of one checkpoint, reloading its save between them, before the + // segment stops. 1 stops at the first failure without reloading, leaving + // the game where it failed for a look. + size_t attempts_per_checkpoint = 3; + // When the run saves. In the other modes no checkpoint after the first + // has a save at its start, so only the first is retried with a reload. + // The options' default is NONE; the engine's stays the playthrough's. + StorySaveMode save_mode = StorySaveMode::PER_CHECKPOINT; +}; + +// The checkpoints to run, by id, both inclusive. An unset end is the +// segment's first or last checkpoint. +struct SegmentRange{ + std::optional from; + std::optional to; +}; + + +// What a story run adds to the program's stats. +struct StoryRunCounts{ + size_t segments = 0; + // Checkpoints started, passed or not. + size_t checkpoints = 0; + // Game resets or reloads after failed attempts. + size_t resets = 0; +}; + +// Adds `counts` to the shared AutoStory stats. +void record_story_counts(const StoryRunCounts& counts, AutoStoryStats& stats); + + +// Summary of checkpoint attempts and pass status. +struct CheckpointRun{ + std::string id; + bool passed = false; + size_t attempts = 0; + + bool one_shot() const; +}; + +// Execution results and failure diagnostics for a story segment run. +struct SegmentReport{ + bool completed = false; + // Both empty when nothing failed; the event is empty when a start + // check, an end check, a save or a reload failed. + std::string failed_checkpoint; + std::string failed_event; + Outcome failure; + size_t checkpoints_run = 0; + // Reloads after failed attempts. + size_t reloads = 0; + // Each checkpoint run, in order, the failed one last. + std::vector runs; + + size_t one_shot_passes() const; + // What the run adds to the program's stats (record_story_counts()): + // the segment, each checkpoint it started, each reload. + StoryRunCounts story_counts() const; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8635a95e80..55e02c6bbb 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1459,6 +1459,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h + Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h Source/Pokemon/AutoStory/Pokemon_MapUtil.h Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h Source/Pokemon/AutoStory/Pokemon_StoryEvent.h @@ -1478,6 +1480,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.cpp Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h Source/Pokemon/AutoStory/Pokemon_StoryPose.h + Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp + Source/Pokemon/AutoStory/Pokemon_StorySegments.h + Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.cpp Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -1502,6 +1508,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h + Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h From df9f4a9cae4ce94d20c400d5efdc14cb847c5a83 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:56:09 -0800 Subject: [PATCH 2/3] AutoStory: the retry loop and the segment saves get their tests here run_checkpoint_attempts() gets a case table (pass, reset, max_attempts, FINAL, a failed reset, max_attempts 0). The segment and verify save-mode tests move here from the save policy suite, beside the runner they test. The segment tests are plain named tests, max_attempts has a default, the prefix flag is the CheckpointPrefix enum, and comments describe the code rather than our records or the SV loop. --- .../AutoStory/Pokemon_AutoStory_Tests.h | 2 + .../AutoStory/Pokemon_CheckpointAttempts.cpp | 2 +- .../AutoStory/Pokemon_CheckpointAttempts.h | 21 +- .../Pokemon_StorySegmentSaves_Tests.cpp | 395 ++++++++++++++++++ .../Pokemon_StorySegmentSaves_Tests.h | 24 ++ .../Pokemon/AutoStory/Pokemon_StorySegments.h | 10 +- .../AutoStory/Pokemon_StorySegments_Tests.cpp | 64 +-- SerialPrograms/cmake/SourceFiles.cmake | 2 + 8 files changed, 452 insertions(+), 68 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 997ce71119..f1a5c079cb 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -16,6 +16,7 @@ #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StorySegmentSaves_Tests.h" #include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" @@ -34,6 +35,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); add_tests_StorySegments(database); + add_tests_StorySegmentSaves(database); add_tests_StoryRunTypes(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp index 39d63731ce..44edd9ff0c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.cpp @@ -18,7 +18,7 @@ CheckpointAttemptsResult run_checkpoint_attempts(std::string_view checkpoint, co const AttemptResult outcome = attempts.run(attempt); if (outcome == AttemptResult::PASSED){ result.passed = true; - attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.add_checkpoint_prefix)); + attempts.log(checkpoint_pass_line(checkpoint, attempt, attempts.prefix_kind)); return result; } if (outcome == AttemptResult::FINAL || attempt >= attempts.max_attempts){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h index 3834e234f5..88121f2a25 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointAttempts.h @@ -5,11 +5,10 @@ * The retry loop every AutoStory runs a checkpoint in: run an attempt, and * when it fails go back to the checkpoint's start save and try again, * until an attempt passes, fails in a way another attempt would repeat, - * the last attempt fails, or a reset fails. SV's loop - * (Pokemon_CheckpointRetry.h), the story segments and the story engine all - * run their checkpoints through it, so each writes the same pass line - * (Pokemon_StoryLog.h) and counts attempts the same way. The game supplies - * the attempt and the reset. + * the last attempt fails, or a reset fails. The story segments and the + * story engine run their checkpoints through it, so each writes the same + * pass line (Pokemon_StoryLog.h) and counts attempts the same way. The + * game supplies the attempt and the reset. * */ @@ -20,6 +19,7 @@ #include #include #include +#include "Pokemon_StoryLog.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -40,8 +40,9 @@ using CheckpointLogger = std::function; // What the game does at each step of the retry loop. struct CheckpointAttempts{ - // Attempts before the loop gives up. - size_t max_attempts; + // Attempts before the loop gives up. The first attempt always runs, + // so 0 runs one, as 1 does. + size_t max_attempts = 1; // Runs attempt `attempt`, counting from 1. std::function run; // Goes back to the checkpoint's start save before attempt @@ -49,10 +50,10 @@ struct CheckpointAttempts{ // not, which ends the loop. std::function reset; CheckpointLogger log; - // False when `checkpoint` is the game's own text that already says it - // is a checkpoint, so the pass line leaves out "[Checkpoint] " + // AS_GIVEN when `checkpoint` is the game's own text that already says + // it is a checkpoint, so the pass line leaves out "[Checkpoint] " // (checkpoint_start_line()). - bool add_checkpoint_prefix = true; + CheckpointPrefix prefix_kind = CheckpointPrefix::ADD; }; // What the loop came to. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp new file mode 100644 index 0000000000..bbd5d0ef23 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp @@ -0,0 +1,395 @@ +/* Story Segment Saves Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The checkpoint retry loop (Pokemon_CheckpointAttempts.h) on scripted + * attempts, then when the segment runner and verify mode save in each save + * mode (Types/Pokemon_StorySaveMode.h), against a world that only records + * what happened. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_CheckpointAttempts.h" +#include "Pokemon_StorySegmentSaves_Tests.h" +#include "Pokemon_StorySegments.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// The retry loop given each attempt's result in turn. +struct AttemptsCase{ + std::string_view name; + struct Input{ + size_t max_attempts; + std::vector results; + // The reset before this attempt fails; 0 for none. + size_t failed_reset = 0; + }; + + struct Expectation{ + bool passed; + size_t attempts; + // The attempts and resets, in order: "run 1", "reset 2", ... + std::vector trace; + }; + + Input input; + Expectation expectation; +}; + +using A = AttemptResult; + +const AttemptsCase ATTEMPTS_CASES[] = { + { + .name = "passes first time", + .input = {.max_attempts = 3, .results = {A::PASSED}}, + .expectation = {.passed = true, .attempts = 1, .trace = {"run 1", "[Checkpoint] 00.04: passed on attempt 1 (one-shot)"}}, + }, + { + .name = "passes after a reset", + .input = {.max_attempts = 3, .results = {A::FAILED, A::PASSED}}, + .expectation = {.passed = true, .attempts = 2, .trace = {"run 1", "reset 2", "run 2", "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}}, + }, + { + .name = "stops at max_attempts", + .input = {.max_attempts = 2, .results = {A::FAILED, A::FAILED, A::PASSED}}, + .expectation = {.passed = false, .attempts = 2, .trace = {"run 1", "reset 2", "run 2"}}, + }, + { + .name = "FINAL stops without a reset", + .input = {.max_attempts = 3, .results = {A::FINAL, A::PASSED}}, + .expectation = {.passed = false, .attempts = 1, .trace = {"run 1"}}, + }, + { + .name = "a failed reset stops", + .input = {.max_attempts = 3, .results = {A::FAILED, A::PASSED}, .failed_reset = 2}, + .expectation = {.passed = false, .attempts = 1, .trace = {"run 1", "reset 2"}}, + }, + { + .name = "max_attempts 0 still runs once", + .input = {.max_attempts = 0, .results = {A::FAILED, A::PASSED}}, + .expectation = {.passed = false, .attempts = 1, .trace = {"run 1"}}, + }, +}; + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += std::format("\n{}", line); + } + return text; +} + +class Test_CheckpointAttempts : public UnitTest{ +public: + explicit Test_CheckpointAttempts(AttemptsCase test_case) + : UnitTest(std::format("Pokemon::StorySegmentSaves - attempts: {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector trace; + const AttemptsCase::Input& in = m_case.input; + const CheckpointAttempts attempts{ + .max_attempts = in.max_attempts, + .run = [&](size_t attempt){ + trace.push_back(std::format("run {}", attempt)); + return in.results.at(attempt - 1); + }, + .reset = [&](size_t next_attempt){ + trace.push_back(std::format("reset {}", next_attempt)); + return next_attempt != in.failed_reset; + }, + .log = [&](const std::string& line){ + trace.push_back(line); + }, + }; + const CheckpointAttemptsResult result = run_checkpoint_attempts("00.04", attempts); + const AttemptsCase::Expectation& expected = m_case.expectation; + if (result.passed != expected.passed || result.attempts != expected.attempts || trace != expected.trace){ + return std::format( + "expected {} after {} attempts, got {} after {}:{}", + expected.passed ? "a pass" : "no pass", expected.attempts, + result.passed ? "a pass" : "no pass", result.attempts, joined(trace) + ); + } + return true; + } + +private: + const AttemptsCase m_case; +}; + + +// A world of three checkpoints that only records what happened: each +// checkpoint's walk, each save and each reload. +struct Lab{ + std::vector trace; + std::vector lines; + // Checkpoints whose walk fails this many more times. + std::map failures; + // The game's postcondition check: none when unset. + std::optional postconditions; + + SegmentHooks hooks(){ + SegmentHooks hooks; + hooks.save = [this](RunState&){ + trace.push_back("save"); + return outcome_ok(); + }; + hooks.reload = [this](RunState&){ + trace.push_back("reload"); + return outcome_ok(); + }; + hooks.log = [this](const std::string& line){ lines.push_back(line); }; + if (postconditions){ + hooks.check_postconditions = [this](const std::string& id, RunState&){ + trace.push_back(std::format("check {}", id)); + return *postconditions; + }; + } + return hooks; + } + bool has(const std::string& line) const{ + return std::find(lines.begin(), lines.end(), line) != lines.end(); + } + std::string all() const{ + std::string text; + for (const std::string& line : lines){ + text += std::format("\n{}", line); + } + return text; + } +}; + +CheckpointSpec walk_row(std::string id, Lab& lab){ + CheckpointSpec spec; + spec.id = id; + spec.saves = true; + spec.build = [&lab, id]{ + Checkpoint checkpoint; + checkpoint.id = id; + Event walk; + walk.name = "walk"; + walk.act = [&lab, id](RunState&){ + lab.trace.push_back(id); + int& left = lab.failures[id]; + if (left > 0){ + left--; + return outcome_failed("blocked", "a wanderer on the path"); + } + return outcome_ok(); + }; + Recovery stop; + stop.kind = RecoveryKind::STOP; + walk.recoveries.push_back(std::move(stop)); + checkpoint.events.push_back(std::move(walk)); + return checkpoint; + }; + return spec; +} + +Segment three_rows(Lab& lab){ + Segment segment; + segment.id = "00"; + for (std::string_view id : {"00.01", "00.02", "00.03"}){ + segment.checkpoints.push_back(walk_row(std::string(id), lab)); + } + return segment; +} + +SegmentReport run_in(StorySaveMode mode, Lab& lab){ + Segment segment = three_rows(lab); + SegmentHooks hooks = lab.hooks(); + RunState state; + return run_segment(SegmentRange{}, SegmentPolicy{.save_mode = mode}, segment, hooks, state); +} + +UnitTestResult test_none_never_saves(){ + Lab lab; + const SegmentReport report = run_in(StorySaveMode::NONE, lab); + const std::vector expected = {"00.01", "00.02", "00.03"}; + if (!report.completed || lab.trace != expected){ + return std::format("Expected three checkpoints and no save:{}", lab.all()); + } + if (!lab.has("[Segment] 00: save mode none") || !lab.has("[Checkpoint] 00.02: not saving (save mode none)")){ + return std::format("Expected the skipped saves logged:{}", lab.all()); + } + return true; +} + +UnitTestResult test_per_checkpoint_saves_each(){ + Lab lab; + const SegmentReport report = run_in(StorySaveMode::PER_CHECKPOINT, lab); + const std::vector expected = {"00.01", "save", "00.02", "save", "00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return std::format("Expected a save after each checkpoint:{}", lab.all()); + } + return true; +} + +UnitTestResult test_after_verification_saves_at_the_end(){ + Lab lab; + const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); + const std::vector expected = {"00.01", "00.02", "00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return std::format("Expected one save, at the end:{}", lab.all()); + } + if (!lab.has("[Postcondition] 00.03: not checked: the game has no postcondition check")){ + return std::format("Expected the missing check logged as not checked:{}", lab.all()); + } + return true; +} + +UnitTestResult test_after_verification_checks_before_saving(){ + Lab lab; + lab.postconditions = outcome_ok(); + const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); + const std::vector expected = {"00.01", "00.02", "00.03", "check 00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return std::format("Expected the check, then the save:{}", lab.all()); + } + return true; +} + +// A run whose game reports it on the wrong floor at the end must not save. +UnitTestResult test_failed_postconditions_never_save(){ + Lab lab; + lab.postconditions = outcome_failed("postconditions", "map: expected RocketHideout_B3F, read RocketHideout_B4F"); + const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); + if (report.completed || report.failed_checkpoint != "00.03"){ + return std::format("Expected the run failed at its last checkpoint:{}", lab.all()); + } + if (std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ + return std::format("A run whose postconditions failed must not save:{}", lab.all()); + } + if (!lab.has("[Checkpoint] 00.03: postconditions failed, not saving")){ + return std::format("Expected the refused save logged:{}", lab.all()); + } + return true; +} + +// Without per-checkpoint saves, a reload would go back to the run's start. +UnitTestResult test_no_reload_after_the_first(){ + Lab lab; + lab.failures["00.02"] = 1; + const SegmentReport report = run_in(StorySaveMode::NONE, lab); + const std::vector expected = {"00.01", "00.02"}; + if (report.completed || report.reloads != 0 || lab.trace != expected){ + return std::format("Expected 00.02 to fail without a reload:{}", lab.all()); + } + if (!lab.has("[Segment] 00: 00.02 failed; save mode none left no save at its start, so no reload")){ + return std::format("Expected why it didn't reload:{}", lab.all()); + } + return true; +} + +UnitTestResult test_first_row_still_reloads(){ + Lab lab; + lab.failures["00.01"] = 1; + const SegmentReport report = run_in(StorySaveMode::NONE, lab); + const std::vector expected = {"00.01", "reload", "00.01", "00.02", "00.03"}; + if (!report.completed || report.reloads != 1 || lab.trace != expected){ + return std::format("Expected the first checkpoint retried from the start save:{}", lab.all()); + } + return true; +} + +VerifyReport verify_in(StorySaveMode mode, Lab& lab){ + Segment segment = three_rows(lab); + SegmentHooks hooks = lab.hooks(); + RunState state; + const VerifyPolicy policy{.runs = 3, .passes_needed = 2, .save_mode = mode}; + return verify_checkpoint("00.02", policy, segment, hooks, state); +} + +UnitTestResult test_verify_saves_once_every_run_passed(){ + Lab lab; + const VerifyReport report = verify_in(StorySaveMode::AFTER_VERIFICATION, lab); + const std::vector expected = {"00.02", "reload", "00.02", "reload", "00.02", "save"}; + if (!report.verified || !report.saved || lab.trace != expected){ + return std::format("Expected one save, after the last run:{}", lab.all()); + } + if (!lab.has("[Verify] 00.02: every run passed: saved at the end state")){ + return std::format("Expected the save logged:{}", lab.all()); + } + return true; +} + +UnitTestResult test_verify_no_save_after_a_failed_run(){ + Lab lab; + lab.failures["00.02"] = 1; + const VerifyReport report = verify_in(StorySaveMode::AFTER_VERIFICATION, lab); + if (!report.verified || report.saved || std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ + return std::format("Verified 2/3, but a failed run means no save:{}", lab.all()); + } + if (!lab.has("[Verify] 00.02: not every run passed: not saving")){ + return std::format("Expected why it didn't save:{}", lab.all()); + } + return true; +} + + +// A named test; it returns its own failure. +struct NamedTest{ + std::string_view name; + UnitTestResult (*test)(); +}; + +class Test_SegmentSaves : public UnitTest{ +public: + explicit Test_SegmentSaves(NamedTest test) + : UnitTest(std::format("Pokemon::StorySegmentSaves - {}", test.name)) + , m_test(test) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_test.test(); + } + +private: + const NamedTest m_test; +}; + +const NamedTest SEGMENT_SAVE_TESTS[] = { + {.name = "none never saves", .test = test_none_never_saves}, + {.name = "per checkpoint saves each", .test = test_per_checkpoint_saves_each}, + {.name = "after verification saves at the end", .test = test_after_verification_saves_at_the_end}, + {.name = "after verification checks before saving", .test = test_after_verification_checks_before_saving}, + {.name = "failed postconditions never save", .test = test_failed_postconditions_never_save}, + {.name = "no reload after the first checkpoint", .test = test_no_reload_after_the_first}, + {.name = "the first checkpoint still reloads", .test = test_first_row_still_reloads}, + {.name = "verify saves once every run passed", .test = test_verify_saves_once_every_run_passed}, + {.name = "verify: no save after a failed run", .test = test_verify_no_save_after_a_failed_run}, +}; + + +} // namespace + + +void add_tests_StorySegmentSaves(UnitTestDatabase& database){ + for (const AttemptsCase& test_case : ATTEMPTS_CASES){ + database.add(test_case); + } + for (const NamedTest& test : SEGMENT_SAVE_TESTS){ + database.add(test); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h new file mode 100644 index 0000000000..7fd48b5ebc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h @@ -0,0 +1,24 @@ +/* Story Segment Saves Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySegmentSaves_Tests_H +#define PokemonAutomation_Pokemon_StorySegmentSaves_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests of the checkpoint retry loop and of when the segment +// runner and verify mode save. +void add_tests_StorySegmentSaves(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h index 280504487f..dbef8c63d7 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h @@ -54,8 +54,7 @@ struct SegmentHooks{ EventLogger log; // Optional: each [Segment], [Checkpoint] and [Event] line again, // without its tag ("00.03/2 below_oaks_trigger: start"), for the video - // overlay, so whoever watches the stream sees the step under way; and, - // once run records land (#132), for the run's events. + // overlay, so whoever watches the stream sees the step under way. EventLogger status; // Optional: the game's postcondition check of a checkpoint, by id // (check_postconditions(), Pokemon_StoryPostconditionCheck.h), run @@ -81,10 +80,9 @@ SegmentReport run_segment( // Verify mode, for a checkpoint that must be shown to hold up (a battle -// lost twice or more gets its own checkpoint, saved right before it, and is -// verified this way): the checkpoint run `runs` times, each from the same -// save. The checkpoint never saves at its end, so each run after the first -// reloads the save the first started from. +// that does not always go the same way): the checkpoint run `runs` times, +// each from the same save. The checkpoint never saves at its end, so each +// run after the first reloads the save the first started from. struct VerifyPolicy{ size_t runs = 3; // Passing runs the checkpoint needs to be verified. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp index 721007b6c6..7b2a93bac3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp @@ -407,18 +407,10 @@ UnitTestResult test_describe(){ } +// A named test; it returns its own failure. struct StorySegmentTestCase{ std::string name; - struct Input{ - UnitTestResult (*test)(); - }; - - struct Expectation{ - std::string_view description; - }; - - Input input; - Expectation expectation; + UnitTestResult (*test)(); }; class Test_StorySegments : public UnitTest{ @@ -428,7 +420,7 @@ class Test_StorySegments : public UnitTest{ , m_scenario(std::move(scenario)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_scenario.input.test(); + return m_scenario.test(); } private: @@ -436,46 +428,16 @@ class Test_StorySegments : public UnitTest{ }; const StorySegmentTestCase STORY_SEGMENT_CASES[] = { - { - .name = "checkpoints run in order", .input = {.test = test_runs_checkpoints_in_order}, - .expectation = {.description = "executes sequential checkpoints and saves state"}, - }, - { - .name = "a failed checkpoint is reloaded", .input = {.test = test_reloads_a_failed_checkpoint}, - .expectation = {.description = "reloads prior saved state upon checkpoint failure"}, - }, - { - .name = "gives up and names the failure", .input = {.test = test_gives_up_and_names_the_failure}, - .expectation = {.description = "reports terminal checkpoint failure after exhausting retries"}, - }, - { - .name = "a range of checkpoints", .input = {.test = test_range}, - .expectation = {.description = "runs only the specified slice of checkpoints"}, - }, - { - .name = "describe", .input = {.test = test_describe}, - .expectation = {.description = "formats human-readable segment summary without executing"}, - }, - { - .name = "status lines for the overlay", .input = {.test = test_status_lines}, - .expectation = {.description = "generates UI overlay status updates during execution"}, - }, - { - .name = "a throwing event is logged, then goes on", .input = {.test = test_throwing_event_is_logged}, - .expectation = {.description = "handles and logs exception without terminating segment"}, - }, - { - .name = "verify: runs from one save", .input = {.test = test_verify_runs_from_one_save}, - .expectation = {.description = "runs verification passes against the initial save state"}, - }, - { - .name = "verify: no save at the end", .input = {.test = test_verify_skips_the_end_save}, - .expectation = {.description = "skips checkpoint state save during verification mode"}, - }, - { - .name = "verify: too few passes", .input = {.test = test_verify_too_few_passes}, - .expectation = {.description = "fails verification when required pass count is not met"}, - }, + {.name = "checkpoints run in order", .test = test_runs_checkpoints_in_order}, + {.name = "a failed checkpoint is reloaded", .test = test_reloads_a_failed_checkpoint}, + {.name = "gives up and names the failure", .test = test_gives_up_and_names_the_failure}, + {.name = "a range of checkpoints", .test = test_range}, + {.name = "describe", .test = test_describe}, + {.name = "status lines for the overlay", .test = test_status_lines}, + {.name = "a throwing event is logged, then goes on", .test = test_throwing_event_is_logged}, + {.name = "verify: runs from one save", .test = test_verify_runs_from_one_save}, + {.name = "verify: no save at the end", .test = test_verify_skips_the_end_save}, + {.name = "verify: too few passes", .test = test_verify_too_few_passes}, }; } // namespace diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index ad14b67529..fdd600063b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1484,6 +1484,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp Source/Pokemon/AutoStory/Pokemon_StoryPose.h + Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp Source/Pokemon/AutoStory/Pokemon_StorySegments.h Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp From bc2205f8e4b79d1a1f3386e91ab69fdcdcbe1faa Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:26:27 -0800 Subject: [PATCH 3/3] AutoStory: drop the segment runner, StorySaveMode and SegmentPolicy The story engine is the one runner; a checkpoint that passes saves as its row's execution says. Their tests go with them. --- .../AutoStory/Pokemon_AutoStory_Tests.h | 4 - .../Pokemon_StorySegmentSaves_Tests.cpp | 395 --------------- .../Pokemon_StorySegmentSaves_Tests.h | 24 - .../AutoStory/Pokemon_StorySegments.cpp | 375 --------------- .../Pokemon/AutoStory/Pokemon_StorySegments.h | 131 ----- .../AutoStory/Pokemon_StorySegments_Tests.cpp | 455 ------------------ .../AutoStory/Pokemon_StorySegments_Tests.h | 23 - .../Pokemon_StoryStepExecutor_Tests.cpp | 260 +--------- .../Types/Pokemon_StoryRunTypes_Tests.cpp | 76 +-- .../AutoStory/Types/Pokemon_StorySaveMode.cpp | 43 -- .../AutoStory/Types/Pokemon_StorySaveMode.h | 59 --- .../Types/Pokemon_StorySegmentTypes.h | 25 +- SerialPrograms/cmake/SourceFiles.cmake | 8 - 13 files changed, 7 insertions(+), 1871 deletions(-) delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index f1a5c079cb..ba2eb11a71 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -16,8 +16,6 @@ #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" -#include "Pokemon_StorySegmentSaves_Tests.h" -#include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" #include "Types/Pokemon_StoryRunTypes_Tests.h" @@ -34,8 +32,6 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryEvents(database); add_tests_StoryStepExecutor(database); add_tests_StoryStepJson(database); - add_tests_StorySegments(database); - add_tests_StorySegmentSaves(database); add_tests_StoryRunTypes(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp deleted file mode 100644 index bbd5d0ef23..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp +++ /dev/null @@ -1,395 +0,0 @@ -/* Story Segment Saves Tests - * - * From: https://github.com/PokemonAutomation/ - * - * The checkpoint retry loop (Pokemon_CheckpointAttempts.h) on scripted - * attempts, then when the segment runner and verify mode save in each save - * mode (Types/Pokemon_StorySaveMode.h), against a world that only records - * what happened. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon_CheckpointAttempts.h" -#include "Pokemon_StorySegmentSaves_Tests.h" -#include "Pokemon_StorySegments.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -namespace{ - - -// The retry loop given each attempt's result in turn. -struct AttemptsCase{ - std::string_view name; - struct Input{ - size_t max_attempts; - std::vector results; - // The reset before this attempt fails; 0 for none. - size_t failed_reset = 0; - }; - - struct Expectation{ - bool passed; - size_t attempts; - // The attempts and resets, in order: "run 1", "reset 2", ... - std::vector trace; - }; - - Input input; - Expectation expectation; -}; - -using A = AttemptResult; - -const AttemptsCase ATTEMPTS_CASES[] = { - { - .name = "passes first time", - .input = {.max_attempts = 3, .results = {A::PASSED}}, - .expectation = {.passed = true, .attempts = 1, .trace = {"run 1", "[Checkpoint] 00.04: passed on attempt 1 (one-shot)"}}, - }, - { - .name = "passes after a reset", - .input = {.max_attempts = 3, .results = {A::FAILED, A::PASSED}}, - .expectation = {.passed = true, .attempts = 2, .trace = {"run 1", "reset 2", "run 2", "[Checkpoint] 00.04: passed on attempt 2 (1 reset)"}}, - }, - { - .name = "stops at max_attempts", - .input = {.max_attempts = 2, .results = {A::FAILED, A::FAILED, A::PASSED}}, - .expectation = {.passed = false, .attempts = 2, .trace = {"run 1", "reset 2", "run 2"}}, - }, - { - .name = "FINAL stops without a reset", - .input = {.max_attempts = 3, .results = {A::FINAL, A::PASSED}}, - .expectation = {.passed = false, .attempts = 1, .trace = {"run 1"}}, - }, - { - .name = "a failed reset stops", - .input = {.max_attempts = 3, .results = {A::FAILED, A::PASSED}, .failed_reset = 2}, - .expectation = {.passed = false, .attempts = 1, .trace = {"run 1", "reset 2"}}, - }, - { - .name = "max_attempts 0 still runs once", - .input = {.max_attempts = 0, .results = {A::FAILED, A::PASSED}}, - .expectation = {.passed = false, .attempts = 1, .trace = {"run 1"}}, - }, -}; - -std::string joined(const std::vector& lines){ - std::string text; - for (const std::string& line : lines){ - text += std::format("\n{}", line); - } - return text; -} - -class Test_CheckpointAttempts : public UnitTest{ -public: - explicit Test_CheckpointAttempts(AttemptsCase test_case) - : UnitTest(std::format("Pokemon::StorySegmentSaves - attempts: {}", test_case.name)) - , m_case(std::move(test_case)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::vector trace; - const AttemptsCase::Input& in = m_case.input; - const CheckpointAttempts attempts{ - .max_attempts = in.max_attempts, - .run = [&](size_t attempt){ - trace.push_back(std::format("run {}", attempt)); - return in.results.at(attempt - 1); - }, - .reset = [&](size_t next_attempt){ - trace.push_back(std::format("reset {}", next_attempt)); - return next_attempt != in.failed_reset; - }, - .log = [&](const std::string& line){ - trace.push_back(line); - }, - }; - const CheckpointAttemptsResult result = run_checkpoint_attempts("00.04", attempts); - const AttemptsCase::Expectation& expected = m_case.expectation; - if (result.passed != expected.passed || result.attempts != expected.attempts || trace != expected.trace){ - return std::format( - "expected {} after {} attempts, got {} after {}:{}", - expected.passed ? "a pass" : "no pass", expected.attempts, - result.passed ? "a pass" : "no pass", result.attempts, joined(trace) - ); - } - return true; - } - -private: - const AttemptsCase m_case; -}; - - -// A world of three checkpoints that only records what happened: each -// checkpoint's walk, each save and each reload. -struct Lab{ - std::vector trace; - std::vector lines; - // Checkpoints whose walk fails this many more times. - std::map failures; - // The game's postcondition check: none when unset. - std::optional postconditions; - - SegmentHooks hooks(){ - SegmentHooks hooks; - hooks.save = [this](RunState&){ - trace.push_back("save"); - return outcome_ok(); - }; - hooks.reload = [this](RunState&){ - trace.push_back("reload"); - return outcome_ok(); - }; - hooks.log = [this](const std::string& line){ lines.push_back(line); }; - if (postconditions){ - hooks.check_postconditions = [this](const std::string& id, RunState&){ - trace.push_back(std::format("check {}", id)); - return *postconditions; - }; - } - return hooks; - } - bool has(const std::string& line) const{ - return std::find(lines.begin(), lines.end(), line) != lines.end(); - } - std::string all() const{ - std::string text; - for (const std::string& line : lines){ - text += std::format("\n{}", line); - } - return text; - } -}; - -CheckpointSpec walk_row(std::string id, Lab& lab){ - CheckpointSpec spec; - spec.id = id; - spec.saves = true; - spec.build = [&lab, id]{ - Checkpoint checkpoint; - checkpoint.id = id; - Event walk; - walk.name = "walk"; - walk.act = [&lab, id](RunState&){ - lab.trace.push_back(id); - int& left = lab.failures[id]; - if (left > 0){ - left--; - return outcome_failed("blocked", "a wanderer on the path"); - } - return outcome_ok(); - }; - Recovery stop; - stop.kind = RecoveryKind::STOP; - walk.recoveries.push_back(std::move(stop)); - checkpoint.events.push_back(std::move(walk)); - return checkpoint; - }; - return spec; -} - -Segment three_rows(Lab& lab){ - Segment segment; - segment.id = "00"; - for (std::string_view id : {"00.01", "00.02", "00.03"}){ - segment.checkpoints.push_back(walk_row(std::string(id), lab)); - } - return segment; -} - -SegmentReport run_in(StorySaveMode mode, Lab& lab){ - Segment segment = three_rows(lab); - SegmentHooks hooks = lab.hooks(); - RunState state; - return run_segment(SegmentRange{}, SegmentPolicy{.save_mode = mode}, segment, hooks, state); -} - -UnitTestResult test_none_never_saves(){ - Lab lab; - const SegmentReport report = run_in(StorySaveMode::NONE, lab); - const std::vector expected = {"00.01", "00.02", "00.03"}; - if (!report.completed || lab.trace != expected){ - return std::format("Expected three checkpoints and no save:{}", lab.all()); - } - if (!lab.has("[Segment] 00: save mode none") || !lab.has("[Checkpoint] 00.02: not saving (save mode none)")){ - return std::format("Expected the skipped saves logged:{}", lab.all()); - } - return true; -} - -UnitTestResult test_per_checkpoint_saves_each(){ - Lab lab; - const SegmentReport report = run_in(StorySaveMode::PER_CHECKPOINT, lab); - const std::vector expected = {"00.01", "save", "00.02", "save", "00.03", "save"}; - if (!report.completed || lab.trace != expected){ - return std::format("Expected a save after each checkpoint:{}", lab.all()); - } - return true; -} - -UnitTestResult test_after_verification_saves_at_the_end(){ - Lab lab; - const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); - const std::vector expected = {"00.01", "00.02", "00.03", "save"}; - if (!report.completed || lab.trace != expected){ - return std::format("Expected one save, at the end:{}", lab.all()); - } - if (!lab.has("[Postcondition] 00.03: not checked: the game has no postcondition check")){ - return std::format("Expected the missing check logged as not checked:{}", lab.all()); - } - return true; -} - -UnitTestResult test_after_verification_checks_before_saving(){ - Lab lab; - lab.postconditions = outcome_ok(); - const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); - const std::vector expected = {"00.01", "00.02", "00.03", "check 00.03", "save"}; - if (!report.completed || lab.trace != expected){ - return std::format("Expected the check, then the save:{}", lab.all()); - } - return true; -} - -// A run whose game reports it on the wrong floor at the end must not save. -UnitTestResult test_failed_postconditions_never_save(){ - Lab lab; - lab.postconditions = outcome_failed("postconditions", "map: expected RocketHideout_B3F, read RocketHideout_B4F"); - const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); - if (report.completed || report.failed_checkpoint != "00.03"){ - return std::format("Expected the run failed at its last checkpoint:{}", lab.all()); - } - if (std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ - return std::format("A run whose postconditions failed must not save:{}", lab.all()); - } - if (!lab.has("[Checkpoint] 00.03: postconditions failed, not saving")){ - return std::format("Expected the refused save logged:{}", lab.all()); - } - return true; -} - -// Without per-checkpoint saves, a reload would go back to the run's start. -UnitTestResult test_no_reload_after_the_first(){ - Lab lab; - lab.failures["00.02"] = 1; - const SegmentReport report = run_in(StorySaveMode::NONE, lab); - const std::vector expected = {"00.01", "00.02"}; - if (report.completed || report.reloads != 0 || lab.trace != expected){ - return std::format("Expected 00.02 to fail without a reload:{}", lab.all()); - } - if (!lab.has("[Segment] 00: 00.02 failed; save mode none left no save at its start, so no reload")){ - return std::format("Expected why it didn't reload:{}", lab.all()); - } - return true; -} - -UnitTestResult test_first_row_still_reloads(){ - Lab lab; - lab.failures["00.01"] = 1; - const SegmentReport report = run_in(StorySaveMode::NONE, lab); - const std::vector expected = {"00.01", "reload", "00.01", "00.02", "00.03"}; - if (!report.completed || report.reloads != 1 || lab.trace != expected){ - return std::format("Expected the first checkpoint retried from the start save:{}", lab.all()); - } - return true; -} - -VerifyReport verify_in(StorySaveMode mode, Lab& lab){ - Segment segment = three_rows(lab); - SegmentHooks hooks = lab.hooks(); - RunState state; - const VerifyPolicy policy{.runs = 3, .passes_needed = 2, .save_mode = mode}; - return verify_checkpoint("00.02", policy, segment, hooks, state); -} - -UnitTestResult test_verify_saves_once_every_run_passed(){ - Lab lab; - const VerifyReport report = verify_in(StorySaveMode::AFTER_VERIFICATION, lab); - const std::vector expected = {"00.02", "reload", "00.02", "reload", "00.02", "save"}; - if (!report.verified || !report.saved || lab.trace != expected){ - return std::format("Expected one save, after the last run:{}", lab.all()); - } - if (!lab.has("[Verify] 00.02: every run passed: saved at the end state")){ - return std::format("Expected the save logged:{}", lab.all()); - } - return true; -} - -UnitTestResult test_verify_no_save_after_a_failed_run(){ - Lab lab; - lab.failures["00.02"] = 1; - const VerifyReport report = verify_in(StorySaveMode::AFTER_VERIFICATION, lab); - if (!report.verified || report.saved || std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ - return std::format("Verified 2/3, but a failed run means no save:{}", lab.all()); - } - if (!lab.has("[Verify] 00.02: not every run passed: not saving")){ - return std::format("Expected why it didn't save:{}", lab.all()); - } - return true; -} - - -// A named test; it returns its own failure. -struct NamedTest{ - std::string_view name; - UnitTestResult (*test)(); -}; - -class Test_SegmentSaves : public UnitTest{ -public: - explicit Test_SegmentSaves(NamedTest test) - : UnitTest(std::format("Pokemon::StorySegmentSaves - {}", test.name)) - , m_test(test) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_test.test(); - } - -private: - const NamedTest m_test; -}; - -const NamedTest SEGMENT_SAVE_TESTS[] = { - {.name = "none never saves", .test = test_none_never_saves}, - {.name = "per checkpoint saves each", .test = test_per_checkpoint_saves_each}, - {.name = "after verification saves at the end", .test = test_after_verification_saves_at_the_end}, - {.name = "after verification checks before saving", .test = test_after_verification_checks_before_saving}, - {.name = "failed postconditions never save", .test = test_failed_postconditions_never_save}, - {.name = "no reload after the first checkpoint", .test = test_no_reload_after_the_first}, - {.name = "the first checkpoint still reloads", .test = test_first_row_still_reloads}, - {.name = "verify saves once every run passed", .test = test_verify_saves_once_every_run_passed}, - {.name = "verify: no save after a failed run", .test = test_verify_no_save_after_a_failed_run}, -}; - - -} // namespace - - -void add_tests_StorySegmentSaves(UnitTestDatabase& database){ - for (const AttemptsCase& test_case : ATTEMPTS_CASES){ - database.add(test_case); - } - for (const NamedTest& test : SEGMENT_SAVE_TESTS){ - database.add(test); - } -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h deleted file mode 100644 index 7fd48b5ebc..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h +++ /dev/null @@ -1,24 +0,0 @@ -/* Story Segment Saves Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_StorySegmentSaves_Tests_H -#define PokemonAutomation_Pokemon_StorySegmentSaves_Tests_H - -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Registers the tests of the checkpoint retry loop and of when the segment -// runner and verify mode save. -void add_tests_StorySegmentSaves(UnitTestDatabase& database); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp deleted file mode 100644 index 2ff6960ed0..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ /dev/null @@ -1,375 +0,0 @@ -/* Story Segments - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include "Pokemon_CheckpointAttempts.h" -#include "Pokemon_StoryLog.h" -#include "Pokemon_StoryOutcomeClass.h" -#include "Pokemon_StorySegments.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -namespace{ - -// A line for the log, and without its tag for the status sink, if set. -void say(const std::string& line, SegmentHooks& hooks){ - if (hooks.log){ - hooks.log(line); - } - if (hooks.status){ - hooks.status(story_short_line(line)); - } -} - -std::optional index_of(const Segment& segment, const std::string& id){ - for (size_t c = 0; c < segment.checkpoints.size(); c++){ - if (segment.checkpoints[c].id == id){ - return c; - } - } - return std::nullopt; -} - -// The rows `range` names, both inclusive, or the failure that stops the -// segment before it starts. -struct Selected{ - size_t first = 0; - size_t last = 0; - Outcome failure; -}; - -Selected select(const Segment& segment, const SegmentRange& range){ - Selected selected; - if (segment.checkpoints.empty()){ - selected.failure = outcome_failed("empty_segment"); - return selected; - } - selected.last = segment.checkpoints.size() - 1; - if (range.from){ - const std::optional index = index_of(segment, *range.from); - if (!index){ - selected.failure = outcome_failed("unknown_checkpoint", *range.from); - return selected; - } - selected.first = *index; - } - if (range.to){ - const std::optional index = index_of(segment, *range.to); - if (!index){ - selected.failure = outcome_failed("unknown_checkpoint", *range.to); - return selected; - } - selected.last = *index; - } - if (selected.first > selected.last){ - selected.failure = outcome_failed("bad_range", std::format("{} is after {}", *range.from, *range.to)); - } - return selected; -} - -CheckpointReport failed_report(Outcome failure){ - CheckpointReport report; - report.failure = std::move(failure); - return report; -} - -// Empties the run state and reloads the last save; the reload says where -// the player stands. -Outcome reload(RunState& state, SegmentHooks& hooks){ - state = RunState{}; - if (!hooks.reload){ - return outcome_failed("reload_failed", "no reload hook"); - } - Outcome reloaded = hooks.reload(state); - if (!reloaded.succeeded()){ - return outcome_failed("reload_failed", to_string(reloaded)); - } - if (reloaded.pose){ - state.pose = *reloaded.pose; - } - return outcome_ok(); -} - -// The segment's save hook as a checkpoint's save. -Act hook_save(SegmentHooks& hooks){ - return [&hooks](RunState& state){ - return hooks.save ? hooks.save(state) : outcome_failed("no_save_hook"); - }; -} - -// `save`, after the game's postcondition check of checkpoint `id`: one -// that failed stops the save. Without a check, logs that nothing was -// checked, never that it passed. -Act checked_save(Act save, std::string id, SegmentHooks& hooks){ - return [save = std::move(save), id = std::move(id), &hooks](RunState& state) mutable{ - if (!hooks.check_postconditions){ - say(std::format("[Postcondition] {}: not checked: the game has no postcondition check", id), hooks); - return save(state); - } - const Outcome checked = hooks.check_postconditions(id, state); - if (!checked.succeeded()){ - say(checkpoint_log_line(id, "postconditions failed, not saving"), hooks); - return outcome_failed("postconditions_failed", to_string(checked)); - } - return save(state); - }; -} - -// A row to run, and where it stands in the run. -struct Row{ - CheckpointSpec& spec; - // The run's first: its save is the one the run started from. - bool first = false; - // The run's last: it ends at the run's end state. - bool last = false; -}; - -// The checkpoint's save as `policy.save_mode` allows: one the mode skips -// logs that it did not save; the run's last in AFTER_VERIFICATION saves -// (with the segment's hook if it has none) after the postcondition check. -void set_save(const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, Checkpoint& checkpoint){ - const StorySaveMode mode = policy.save_mode; - const bool saves_at_end = mode == StorySaveMode::AFTER_VERIFICATION && row.last; - if ((row.spec.saves || saves_at_end) && !checkpoint.save){ - checkpoint.save = hook_save(hooks); - } - if (!checkpoint.save){ - return; - } - const SavePoint point = row.last ? SavePoint::RUN_END : SavePoint::CHECKPOINT_END; - if (!story_save_allowed(mode, CheckpointVerdict::PASSED, point)){ - checkpoint.save = [&hooks, id = row.spec.id, mode](RunState&){ - say(checkpoint_log_line(id, std::format("not saving (save mode {})", story_save_mode_name(mode))), hooks); - return outcome_ok(); - }; - return; - } - if (saves_at_end){ - checkpoint.save = checked_save(std::move(checkpoint.save), row.spec.id, hooks); - } -} - -// Attempts the row gets: a row after the first has a save at its start -// only when every checkpoint saves, so otherwise a reload would go back -// to the run's start, and it gets one. -size_t row_attempts(const Row& row, const SegmentPolicy& policy){ - if (row.first || policy.save_mode == StorySaveMode::PER_CHECKPOINT){ - return policy.attempts_per_checkpoint; - } - return 1; -} - -// One row to success through run_checkpoint_attempts(), reloading and -// rebuilding after each failed attempt. Returns the last attempt's report, -// or the reload's failure; `run` says how many attempts it took. -CheckpointReport run_row( - const Segment& segment, const Row& row, const SegmentPolicy& policy, - SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run, RunState& state -){ - const size_t max_attempts = row_attempts(row, policy); - CheckpointReport result; - const CheckpointAttempts attempts{ - .max_attempts = max_attempts, - .run = [&](size_t attempt){ - run.attempts = attempt; - Checkpoint checkpoint = row.spec.build(); - set_save(row, policy, hooks, checkpoint); - EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; - result = run_checkpoint(state, checkpoint, log); - if (result.completed){ - return AttemptResult::PASSED; - } - if (result.failure.failure == GAME_NOT_RUNNING){ - say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id), hooks); - return AttemptResult::FINAL; - } - if (attempt >= max_attempts && max_attempts < policy.attempts_per_checkpoint){ - say(std::format( - "[Segment] {}: {} failed; save mode {} left no save at its start, so no reload", - segment.id, row.spec.id, story_save_mode_name(policy.save_mode) - ), hooks); - } - return AttemptResult::FAILED; - }, - .reset = [&](size_t next_attempt){ - say(checkpoint_reset_line(row.spec.id, next_attempt, policy.attempts_per_checkpoint), hooks); - report.reloads++; - Outcome reloaded = reload(state, hooks); - if (!reloaded.succeeded()){ - result = failed_report(std::move(reloaded)); - return false; - } - return true; - }, - .log = [&hooks](const std::string& line){ say(line, hooks); }, - }; - run.passed = run_checkpoint_attempts(row.spec.id, attempts).passed; - return result; -} - -SegmentReport stop_segment(const std::string& id, Outcome failure, SegmentHooks& hooks, SegmentReport& report){ - std::string where; - if (!report.failed_checkpoint.empty()){ - where = std::format(" at {}", report.failed_checkpoint); - if (!report.failed_event.empty()){ - where += std::format(" {}", report.failed_event); - } - } - say(std::format("[Segment] {}: failed{}: {}", id, where, to_string_with_class(failure)), hooks); - report.failure = std::move(failure); - return report; -} - -} // namespace - - -SegmentReport run_segment( - const SegmentRange& range, const SegmentPolicy& policy, - Segment& segment, SegmentHooks& hooks, RunState& state -){ - SegmentReport report; - const Selected selected = select(segment, range); - if (!selected.failure.succeeded()){ - return stop_segment(segment.id, selected.failure, hooks, report); - } - say(std::format("[Segment] {}: start, {} checkpoints", segment.id, selected.last - selected.first + 1), hooks); - if (policy.save_mode != StorySaveMode::PER_CHECKPOINT){ - say(std::format("[Segment] {}: save mode {}", segment.id, story_save_mode_name(policy.save_mode)), hooks); - } - for (size_t c = selected.first; c <= selected.last; c++){ - CheckpointSpec& spec = segment.checkpoints[c]; - report.checkpoints_run++; - report.runs.push_back(CheckpointRun{.id = spec.id}); - const Row row{.spec = spec, .first = c == selected.first, .last = c == selected.last}; - CheckpointReport result = run_row(segment, row, policy, hooks, report, report.runs.back(), state); - if (!result.completed){ - report.failed_checkpoint = spec.id; - report.failed_event = result.failed_name; - return stop_segment(segment.id, std::move(result.failure), hooks, report); - } - } - report.completed = true; - say(std::format("[Segment] {}: {} of {} checkpoints one-shot", segment.id, report.one_shot_passes(), report.checkpoints_run), hooks); - say(std::format("[Segment] {}: done, {} checkpoints", segment.id, report.checkpoints_run), hooks); - return report; -} - - -namespace{ - -// One verify run: the checkpoint built fresh. Its end save only if -// `save_at_end` (AFTER_VERIFICATION's last run, every run before it -// passed), after the postcondition check. Returns true if it passed. -bool verify_run( - size_t run, const VerifyPolicy& policy, bool save_at_end, - CheckpointSpec& spec, SegmentHooks& hooks, RunState& state -){ - Checkpoint checkpoint = spec.build(); - if (save_at_end){ - Act save = checkpoint.save ? std::move(checkpoint.save) : hook_save(hooks); - checkpoint.save = checked_save(std::move(save), spec.id, hooks); - }else{ - checkpoint.save = nullptr; - } - EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; - const CheckpointReport result = run_checkpoint(state, checkpoint, log); - if (result.completed){ - say(std::format("[Verify] {}: run {}/{} passed", spec.id, run, policy.runs), hooks); - return true; - } - say(std::format( - "[Verify] {}: run {}/{} failed{}: {}", - spec.id, run, policy.runs, - result.failed_name.empty() ? "" : std::format(" at {}", result.failed_name), - to_string_with_class(result.failure) - ), hooks); - return false; -} - -// AFTER_VERIFICATION saves at the end of the last run when every run -// before it passed. -bool verify_saves_after(const VerifyReport& report, const VerifyPolicy& policy, size_t run){ - return policy.save_mode == StorySaveMode::AFTER_VERIFICATION && run == policy.runs && report.passes + 1 == run; -} - -VerifyReport verify_summary(const VerifyPolicy& policy, SegmentHooks& hooks, VerifyReport& report){ - if (policy.save_mode == StorySaveMode::AFTER_VERIFICATION){ - say(std::format( - "[Verify] {}: {}", report.checkpoint, - report.saved ? "every run passed: saved at the end state" : "not every run passed: not saving" - ), hooks); - } - report.verified = report.failure.succeeded() && report.passes >= policy.passes_needed; - say(std::format( - "[Verify] {}: {}/{} passed, {} needed: {}", - report.checkpoint, report.passes, policy.runs, policy.passes_needed, - report.verified ? "verified" : "not verified" - ), hooks); - return report; -} - -} // namespace - - -VerifyReport verify_checkpoint( - const std::string& id, const VerifyPolicy& policy, - Segment& segment, SegmentHooks& hooks, RunState& state -){ - VerifyReport report; - report.checkpoint = id; - const std::optional index = index_of(segment, id); - if (!index){ - report.failure = outcome_failed("unknown_checkpoint", id); - }else if (policy.runs == 0 || policy.passes_needed > policy.runs){ - report.failure = outcome_failed("bad_verify_policy", std::format("{} of {} runs", policy.passes_needed, policy.runs)); - } - if (!report.failure.succeeded()){ - say(std::format("[Verify] {}: {}", id, to_string(report.failure)), hooks); - return report; - } - CheckpointSpec& spec = segment.checkpoints[*index]; - say(std::format( - "[Verify] {}: start, {} runs from one save, {} must pass", id, policy.runs, policy.passes_needed - ), hooks); - for (size_t run = 1; run <= policy.runs; run++){ - if (run > 1){ - Outcome reloaded = reload(state, hooks); - if (!reloaded.succeeded()){ - say(std::format("[Verify] {}: no reload before run {}: {}", id, run, to_string(reloaded)), hooks); - report.failure = std::move(reloaded); - break; - } - } - report.runs++; - const bool save_at_end = verify_saves_after(report, policy, run); - const bool passed = verify_run(run, policy, save_at_end, spec, hooks, state); - report.passes += passed ? 1 : 0; - report.saved = save_at_end && passed; - } - return verify_summary(policy, hooks, report); -} - - -std::string describe(Segment& segment){ - std::string text = std::format("segment {}: {} checkpoints\n", segment.id, segment.checkpoints.size()); - for (const CheckpointSpec& spec : segment.checkpoints){ - text += std::format(" {} \"{}\"{}\n", spec.id, spec.title, spec.saves ? ", saves" : ""); - } - for (CheckpointSpec& spec : segment.checkpoints){ - if (spec.build){ - text += describe(spec.build()); - } - } - return text; -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h deleted file mode 100644 index dbef8c63d7..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h +++ /dev/null @@ -1,131 +0,0 @@ -/* Story Segments - * - * From: https://github.com/PokemonAutomation/ - * - * A story segment as a table of checkpoints. Each row names a checkpoint - * and holds a factory that builds it, so every attempt runs a checkpoint - * built fresh, with nothing the last attempt used up. The runner takes the - * checkpoints in order from a save; a checkpoint that fails is answered - * the one way the event model has no recovery for: the game is reloaded - * to its last save, the checkpoint is rebuilt and run again, up to the - * policy's attempts. Then the segment stops and the report names the - * checkpoint, the event and the reason. - * - */ - -#ifndef PokemonAutomation_Pokemon_StorySegments_H -#define PokemonAutomation_Pokemon_StorySegments_H - -#include -#include -#include -#include "Common/Cpp/MoveOnlyFunction.h" -#include "Pokemon_StoryEvents.h" -#include "Pokemon_StoryLog.h" -#include "Types/Pokemon_StorySegmentTypes.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// One row of a segment. `build` makes the checkpoint anew for each attempt. -// `saves` has the segment save the game (SegmentHooks::save) after the -// checkpoint's end check, when the checkpoint has no save of its own. -struct CheckpointSpec{ - std::string id; - std::string title; - bool saves = false; - MoveOnlyFunction build; -}; - -// Collection of ordered checkpoints that make up a story segment. -struct Segment{ - std::string id; - std::vector checkpoints; -}; - - -// What the game lends the runner: how to save, how to reload the last save -// (the run state is emptied first; the reload's Outcome says where the -// player stands), and where to log. -struct SegmentHooks{ - Act save; - Act reload; - EventLogger log; - // Optional: each [Segment], [Checkpoint] and [Event] line again, - // without its tag ("00.03/2 below_oaks_trigger: start"), for the video - // overlay, so whoever watches the stream sees the step under way. - EventLogger status; - // Optional: the game's postcondition check of a checkpoint, by id - // (check_postconditions(), Pokemon_StoryPostconditionCheck.h), run - // before a save in AFTER_VERIFICATION mode: one that failed stops the - // save and fails the run. The game logs its lines. - MoveOnlyFunction check_postconditions; -}; - -// Runs the checkpoints in `range`, in order, saving as `policy.save_mode` -// allows (story_save_allowed()). Logs through `hooks.log`, -// around the checkpoint lines: -// [Segment] 00: start, 3 checkpoints -// [Checkpoint] 00.02: failed, resetting for attempt 2/3 -// [Checkpoint] 00.02: passed on attempt 2 (1 reset) -// [Checkpoint] 00.03: passed on attempt 1 (one-shot) -// [Segment] 00: 2 of 3 checkpoints one-shot -// [Segment] 00: done, 3 checkpoints -// [Segment] 00: failed at 00.02 talk_mom: expect_failed: dialogue opened -SegmentReport run_segment( - const SegmentRange& range, const SegmentPolicy& policy, - Segment& segment, SegmentHooks& hooks, RunState& state -); - - -// Verify mode, for a checkpoint that must be shown to hold up (a battle -// that does not always go the same way): the checkpoint run `runs` times, -// each from the same save. The checkpoint never saves at its end, so each -// run after the first reloads the save the first started from. -struct VerifyPolicy{ - size_t runs = 3; - // Passing runs the checkpoint needs to be verified. - size_t passes_needed = 2; - // AFTER_VERIFICATION saves once, at the end of the last run, when - // every run passed; any other mode never saves. - StorySaveMode save_mode = StorySaveMode::NONE; -}; - -// Verification results for multiple runs of a checkpoint. -struct VerifyReport{ - std::string checkpoint; - size_t runs = 0; - size_t passes = 0; - // At least VerifyPolicy::passes_needed runs passed. - bool verified = false; - // AFTER_VERIFICATION: every run passed and the last saved at its end. - bool saved = false; - // What stopped the runs early: an unknown checkpoint, a bad policy or - // a failed reload. Runs that fail are counted, not stopped at. - Outcome failure; -}; - -// Runs checkpoint `id` of `segment` as `policy` says, from the save the -// game is on. No retries, no in-place recoveries and no save at its end, -// but for `policy.save_mode` AFTER_VERIFICATION's one save at the end of -// the last run, when every run passed (after the postcondition check). -// Logs through `hooks.log`, after each run's checkpoint lines: -// [Verify] 00.15b: start, 3 runs from one save, 2 must pass -// [Verify] 00.15b: run 1/3 passed -// [Verify] 00.15b: run 2/3 failed at rival_battle: battle_lost -// [Verify] 00.15b: 2/3 passed, 2 needed: verified -VerifyReport verify_checkpoint( - const std::string& id, const VerifyPolicy& policy, - Segment& segment, SegmentHooks& hooks, RunState& state -); - -// A readable dump for review: the rows, then each checkpoint built once -// and described as describe(const Checkpoint&) does. Nothing runs. -std::string describe(Segment& segment); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp deleted file mode 100644 index 7b2a93bac3..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp +++ /dev/null @@ -1,455 +0,0 @@ -/* Story Segments Tests - * - * From: https://github.com/PokemonAutomation/ - * - * The segment runner against a scripted world with a save slot: a few - * fields the checkpoints' events change, saved and reloaded by the hooks. - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include "Common/Cpp/TestRunners/UnitTest.h" -#include "Pokemon_StoryEvents.h" -#include "Pokemon_StorySegments.h" -#include "Pokemon_StorySegments_Tests.h" -#include "Pokemon_TilePose.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -namespace{ - -// Twinleaf Town to Sandgem Town, shortened. -struct World{ - std::string map = "Twinleaf Town"; - bool parcel = false; - // Walks south that fail before one gets through. - int walk_failures = 0; - std::vector trace; -}; - -Pose at_home_door(){ - Pose pose; - pose.map = "Twinleaf Town"; - pose.certainty = PoseCertainty::TILE; - pose.where = std::make_shared(std::vector{TilePos{5, 9}}, Direction::SOUTH); - return pose; -} - -// The world, its save slot, and the log. -struct Lab{ - World world; - World saved; - std::vector lines; - - SegmentHooks hooks(){ - SegmentHooks hooks; - hooks.save = [this](RunState&){ - world.trace.push_back("save"); - saved = world; - saved.trace.clear(); - return outcome_ok(); - }; - hooks.reload = [this](RunState&){ - // Whatever blocked a walk has moved on by the reload. - const int failures = world.walk_failures; - std::vector trace = std::move(world.trace); - world = saved; - world.walk_failures = failures; - world.trace = std::move(trace); - world.trace.push_back("reload"); - return outcome_ok(at_home_door()); - }; - hooks.log = [this](const std::string& line){ lines.push_back(line); }; - return hooks; - } - bool has(const std::string& line) const{ - return std::find(lines.begin(), lines.end(), line) != lines.end(); - } - std::string all() const{ - std::string text; - for (const std::string& line : lines){ - text += std::format("\n{}", line); - } - return text; - } -}; - -Event make_event(std::string name, Act act){ - Event event; - event.name = std::move(name); - event.act = std::move(act); - return event; -} - -Condition on_map(std::string map, World& world){ - return condition(std::format("on {}", map), [&world, map](RunState&){ return world.map == map; }); -} - -CheckpointSpec leave_home(World& world){ - CheckpointSpec spec; - spec.id = "00.01"; - spec.title = "Leave the house"; - spec.saves = true; - spec.build = [&world]{ - Checkpoint checkpoint; - checkpoint.id = "00.01"; - Event walk = make_event("walk_out", [&world](RunState&){ - world.trace.push_back("walk_out"); - world.map = "Route 201"; - return outcome_ok(); - }); - walk.why = "the story starts outside"; - walk.skip_if.push_back(on_map("Route 201", world)); - checkpoint.events.push_back(std::move(walk)); - checkpoint.end.push_back(on_map("Route 201", world)); - return checkpoint; - }; - return spec; -} - -CheckpointSpec walk_to_sandgem(World& world){ - CheckpointSpec spec; - spec.id = "00.02"; - spec.title = "Walk to Sandgem Town"; - spec.build = [&world]{ - Checkpoint checkpoint; - checkpoint.id = "00.02"; - Event walk = make_event("walk_south", [&world](RunState&){ - world.trace.push_back("walk_south"); - if (world.walk_failures > 0){ - world.walk_failures--; - return outcome_failed("blocked", "a wanderer on the path"); - } - world.map = "Sandgem Town"; - return outcome_ok(); - }); - walk.require.push_back(on_map("Route 201", world)); - walk.expect.push_back(on_map("Sandgem Town", world)); - Recovery stop; - stop.kind = RecoveryKind::STOP; - stop.why = "the path is one tile wide"; - walk.recoveries.push_back(std::move(stop)); - checkpoint.events.push_back(std::move(walk)); - return checkpoint; - }; - return spec; -} - -CheckpointSpec get_parcel(World& world){ - CheckpointSpec spec; - spec.id = "00.03"; - spec.title = "Get the Parcel"; - spec.build = [&world]{ - Checkpoint checkpoint; - checkpoint.id = "00.03"; - Event talk = make_event("talk_clerk", [&world](RunState&){ - world.trace.push_back("talk_clerk"); - world.parcel = true; - return outcome_ok(); - }); - talk.expect.push_back(condition("parcel in the Bag", [&world](RunState&){ return world.parcel; })); - talk.pose_learned = at_home_door(); - checkpoint.events.push_back(std::move(talk)); - return checkpoint; - }; - return spec; -} - -Segment twinleaf_to_sandgem(World& world){ - Segment segment; - segment.id = "00"; - segment.checkpoints.push_back(leave_home(world)); - segment.checkpoints.push_back(walk_to_sandgem(world)); - segment.checkpoints.push_back(get_parcel(world)); - return segment; -} - - -UnitTestResult test_runs_checkpoints_in_order(){ - Lab lab; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); - const std::vector expected = {"walk_out", "save", "walk_south", "talk_clerk"}; - if (!report.completed || report.checkpoints_run != 3 || lab.world.trace != expected){ - return std::format("Expected three checkpoints, saved after the first:{}", lab.all()); - } - if (!lab.has("[Segment] 00: start, 3 checkpoints") || lab.lines.back() != "[Segment] 00: done, 3 checkpoints"){ - return std::format("Unexpected log:{}", lab.all()); - } - if (lab.saved.map != "Route 201"){ - return "The save should hold the world after the first checkpoint."; - } - return true; -} - -UnitTestResult test_reloads_a_failed_checkpoint(){ - Lab lab; - lab.world.walk_failures = 1; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); - const std::vector expected = {"walk_out", "save", "walk_south", "reload", "walk_south", "talk_clerk"}; - if (!report.completed || report.reloads != 1 || lab.world.trace != expected){ - return std::format("Expected a reload, then the walk again:{}", lab.all()); - } - if (!lab.has("[Checkpoint] 00.02: failed, resetting for attempt 2/3")){ - return std::format("Expected the reload logged:{}", lab.all()); - } - return true; -} - -UnitTestResult test_gives_up_and_names_the_failure(){ - Lab lab; - lab.world.walk_failures = 10; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{.attempts_per_checkpoint = 2}, segment, hooks, state); - if (report.completed || report.failed_checkpoint != "00.02" || report.failed_event != "walk_south"){ - return std::format("Expected the failed checkpoint and event named:{}", lab.all()); - } - if (report.failure.failure != "blocked" || report.reloads != 1 || report.checkpoints_run != 2){ - return std::format("Expected two attempts with one reload between them:{}", lab.all()); - } - if (lab.world.parcel || lab.lines.back() != "[Segment] 00: failed at 00.02 walk_south: blocked: a wanderer on the path"){ - return std::format("Nothing after the failed checkpoint should run:{}", lab.all()); - } - if (state.pose.map != "Twinleaf Town" || state.pose.certainty != PoseCertainty::TILE){ - return std::format("The reload should have set the pose: {}", to_string(state.pose)); - } - return true; -} - -UnitTestResult test_range(){ - Lab lab; - lab.world.map = "Route 201"; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - SegmentReport report = run_segment(SegmentRange{.from = "00.02", .to = "00.02"}, SegmentPolicy{}, segment, hooks, state); - if (!report.completed || lab.world.trace != std::vector{"walk_south"}){ - return std::format("Expected only the second checkpoint:{}", lab.all()); - } - report = run_segment(SegmentRange{.from = "00.09", .to = std::nullopt}, SegmentPolicy{}, segment, hooks, state); - if (report.completed || report.failure.failure != "unknown_checkpoint" || report.failure.detail != "00.09"){ - return std::format("Expected the unknown id reported:{}", lab.all()); - } - report = run_segment(SegmentRange{.from = "00.03", .to = "00.01"}, SegmentPolicy{}, segment, hooks, state); - if (report.completed || report.failure.failure != "bad_range"){ - return std::format("Expected the reversed range reported:{}", lab.all()); - } - return true; -} - -// The status sink gets every log line again, without its tag, for the -// video overlay. -UnitTestResult test_status_lines(){ - Lab lab; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - std::vector status; - hooks.status = [&status](const std::string& line){ status.push_back(line); }; - RunState state; - run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); - if (status.size() != lab.lines.size() || status.empty()){ - return std::format("Expected a status line per log line:{}", lab.all()); - } - for (size_t c = 0; c < status.size(); c++){ - const std::string& line = lab.lines[c]; - if (line.size() <= status[c].size() || line.compare(line.size() - status[c].size(), status[c].size(), status[c]) != 0){ - return std::format("Status \"{}\" isn't log line \"{}\" without its tag.", status[c], line); - } - } - if (std::find(status.begin(), status.end(), "00.01/1 walk_out: start") == status.end()){ - return "Expected \"00.01/1 walk_out: start\" on the overlay."; - } - return true; -} - -// An event that throws (a console step failing deep down) is logged as -// failed, with its checkpoint, and the exception goes on to the game. -UnitTestResult test_throwing_event_is_logged(){ - Lab lab; - Segment segment; - segment.id = "00"; - CheckpointSpec spec; - spec.id = "00.09"; - spec.build = []{ - Checkpoint checkpoint; - checkpoint.id = "00.09"; - checkpoint.events.push_back(make_event("brock", [](RunState&) -> Outcome{ - throw std::runtime_error("the console vanished"); - })); - return checkpoint; - }; - segment.checkpoints.push_back(std::move(spec)); - SegmentHooks hooks = lab.hooks(); - RunState state; - try{ - run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); - }catch (std::runtime_error&){ - if (!lab.has("[Event] 00.09/1 brock: failed: the console vanished") - || !lab.has("[Checkpoint] 00.09: failed at 1 brock: the console vanished") - ){ - return std::format("Expected the event and checkpoint logged as failed:{}", lab.all()); - } - return true; - } - return "Expected the exception to go on to the game."; -} - -// Verify mode: every run starts from the save the first started from, a -// failed run is counted, not retried, and 2 of 3 passes verify it. -UnitTestResult test_verify_runs_from_one_save(){ - Lab lab; - lab.world.map = "Route 201"; - lab.saved = lab.world; - lab.world.walk_failures = 1; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const VerifyReport report = verify_checkpoint("00.02", VerifyPolicy{}, segment, hooks, state); - const std::vector expected = {"walk_south", "reload", "walk_south", "reload", "walk_south"}; - if (!report.verified || report.runs != 3 || report.passes != 2 || lab.world.trace != expected){ - return std::format("Expected three runs, reloading between them, two passing:{}", lab.all()); - } - if (!lab.has("[Verify] 00.02: start, 3 runs from one save, 2 must pass") - || !lab.has("[Verify] 00.02: run 1/3 failed at walk_south: blocked: a wanderer on the path") - || !lab.has("[Verify] 00.02: run 2/3 passed") - || lab.lines.back() != "[Verify] 00.02: 2/3 passed, 2 needed: verified" - ){ - return std::format("Expected a line per run, then the summary:{}", lab.all()); - } - return true; -} - -// A checkpoint that saves at its end doesn't in verify mode: the next run -// would start after it. -UnitTestResult test_verify_skips_the_end_save(){ - Lab lab; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const VerifyReport report = verify_checkpoint("00.01", VerifyPolicy{.runs = 2, .passes_needed = 2}, segment, hooks, state); - const std::vector expected = {"walk_out", "reload", "walk_out"}; - if (!report.verified || lab.world.trace != expected || lab.saved.map != "Twinleaf Town"){ - return std::format("Expected two runs from Twinleaf Town and no save:{}", lab.all()); - } - return true; -} - -UnitTestResult test_verify_too_few_passes(){ - Lab lab; - lab.world.map = "Route 201"; - lab.saved = lab.world; - lab.world.walk_failures = 2; - Segment segment = twinleaf_to_sandgem(lab.world); - SegmentHooks hooks = lab.hooks(); - RunState state; - const VerifyReport report = verify_checkpoint("00.02", VerifyPolicy{}, segment, hooks, state); - if (report.verified || report.runs != 3 || report.passes != 1 - || lab.lines.back() != "[Verify] 00.02: 1/3 passed, 2 needed: not verified" - ){ - return std::format("Expected 1 of 3, not verified:{}", lab.all()); - } - VerifyReport bad = verify_checkpoint("00.02", VerifyPolicy{.runs = 2, .passes_needed = 3}, segment, hooks, state); - if (bad.verified || bad.runs != 0 || bad.failure.failure != "bad_verify_policy"){ - return std::format("Expected more passes than runs refused:{}", lab.all()); - } - bad = verify_checkpoint("00.09", VerifyPolicy{}, segment, hooks, state); - if (bad.verified || bad.runs != 0 || bad.failure.failure != "unknown_checkpoint"){ - return std::format("Expected an unknown checkpoint refused:{}", lab.all()); - } - return true; -} - -UnitTestResult test_describe(){ - Lab lab; - Segment segment = twinleaf_to_sandgem(lab.world); - const std::string expected = - "segment 00: 3 checkpoints\n" - " 00.01 \"Leave the house\", saves\n" - " 00.02 \"Walk to Sandgem Town\"\n" - " 00.03 \"Get the Parcel\"\n" - "checkpoint 00.01: 1 events\n" - " event 1 walk_out: the story starts outside\n" - " skip_if: on Route 201\n" - " end: on Route 201\n" - "checkpoint 00.02: 1 events\n" - " event 1 walk_south\n" - " require: on Route 201\n" - " expect: on Sandgem Town\n" - " recovery: stop on any failure: the path is one tile wide\n" - "checkpoint 00.03: 1 events\n" - " event 1 talk_clerk\n" - " expect: parcel in the Bag\n" - " pose_learned: Twinleaf Town (5, 9) facing south (tile)\n"; - const std::string text = describe(segment); - if (text != expected){ - return std::format("Unexpected description:\n{}", text); - } - if (!lab.world.trace.empty()){ - return "Describing a segment should run nothing."; - } - return true; -} - - -// A named test; it returns its own failure. -struct StorySegmentTestCase{ - std::string name; - UnitTestResult (*test)(); -}; - -class Test_StorySegments : public UnitTest{ -public: - explicit Test_StorySegments(StorySegmentTestCase scenario) - : UnitTest(std::format("Pokemon::StorySegments - {}", scenario.name)) - , m_scenario(std::move(scenario)) - {} - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - return m_scenario.test(); - } - -private: - StorySegmentTestCase m_scenario; -}; - -const StorySegmentTestCase STORY_SEGMENT_CASES[] = { - {.name = "checkpoints run in order", .test = test_runs_checkpoints_in_order}, - {.name = "a failed checkpoint is reloaded", .test = test_reloads_a_failed_checkpoint}, - {.name = "gives up and names the failure", .test = test_gives_up_and_names_the_failure}, - {.name = "a range of checkpoints", .test = test_range}, - {.name = "describe", .test = test_describe}, - {.name = "status lines for the overlay", .test = test_status_lines}, - {.name = "a throwing event is logged, then goes on", .test = test_throwing_event_is_logged}, - {.name = "verify: runs from one save", .test = test_verify_runs_from_one_save}, - {.name = "verify: no save at the end", .test = test_verify_skips_the_end_save}, - {.name = "verify: too few passes", .test = test_verify_too_few_passes}, -}; - -} // namespace - - -void add_tests_StorySegments(UnitTestDatabase& database){ - for (const auto& scenario : STORY_SEGMENT_CASES){ - database.add(scenario); - } -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h deleted file mode 100644 index 92f97b90c9..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h +++ /dev/null @@ -1,23 +0,0 @@ -/* Story Segments Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_StorySegments_Tests_H -#define PokemonAutomation_Pokemon_StorySegments_Tests_H - -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Registers unit tests for story segment running, verification, and branching. -void add_tests_StorySegments(UnitTestDatabase& database); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp index 757d88eaa0..2b380aac8a 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -23,7 +23,7 @@ #include "Pokemon_AutoStoryStats.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_BuiltinStoryActions.h" -#include "Pokemon_StorySegments.h" +#include "Types/Pokemon_StorySegmentTypes.h" #include "Pokemon_StoryStepExecutor.h" #include "Pokemon_TilePose.h" #include "Pokemon_StoryStepExecutor_Tests.h" @@ -327,259 +327,6 @@ UnitTestResult test_unsupported_and_lost_battles(){ return true; } -// Runs `tables` as one segment whose saves and reloads go into the -// executor's trace and whose log lines go into `lines`. Whatever blocked a -// step has gone by the reload. -SegmentReport run_tables(std::span tables, FakeExecutor& executor, std::vector& lines){ - Segment segment; - segment.id = "00"; - for (const StoryCheckpoint& data : tables){ - segment.checkpoints.push_back(CheckpointSpec{ - .id = data.id, .title = data.title, .saves = true, - .build = [&data, &executor]{ return build_story_checkpoint(data, executor); }, - }); - } - SegmentHooks hooks; - hooks.save = [&executor](RunState&){ - executor.trace.push_back("save"); - return outcome_ok(); - }; - hooks.reload = [&executor](RunState&){ - executor.trace.push_back("reload"); - executor.failing.clear(); - return outcome_ok(); - }; - hooks.log = [&lines](const std::string& line){ lines.push_back(line); }; - RunState state; - return run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); -} - -UnitTestResult test_segment_of_tables(){ - // Two rows built into a segment, run with saves and a reload from the - // game's hooks: the second row's first attempt fails, its retry starts - // over from its start pose. - const std::vector tables{ - lab_checkpoint(), - StoryCheckpoint{ - .id = "00.04", - .title = "the parcel", - .start = declared("Lab", {6, 4}, Direction::NORTH), - .steps = {make(StoryStepKind::TALK, "give_parcel")}, - }, - }; - FakeExecutor executor; - executor.failing = {"give_parcel"}; - std::vector lines; - const SegmentReport report = run_tables(tables, executor, lines); - const std::vector tail{"save", "TALK give_parcel", "reload", "TALK give_parcel", "save"}; - const bool tail_matches = executor.trace.size() >= tail.size() - && std::equal(tail.begin(), tail.end(), executor.trace.end() - (std::ptrdiff_t)tail.size()); - if (!report.completed || report.reloads != 1 || !tail_matches){ - return std::format("Expected 00.04 reloaded once and passed:{}{}", joined(executor.trace), joined(lines)); - } - // Each attempt is begun anew: 00.03 once, 00.04 twice. - if (executor.attempts != std::vector{"00.03", "00.04", "00.04"}){ - return std::format("Expected the executor told of each attempt:{}", joined(executor.attempts)); - } - // The report and the log tell a one-shot pass from a retried one. - const bool runs_ok = report.runs.size() == 2 - && report.runs[0].one_shot() && report.runs[0].attempts == 1 - && !report.runs[1].one_shot() && report.runs[1].attempts == 2 && report.runs[1].passed - && report.one_shot_passes() == 1; - const auto has = [&lines](std::string_view line){ - return std::find(lines.begin(), lines.end(), line) != lines.end(); - }; - if (!runs_ok - || !has("[Checkpoint] 00.03: passed on attempt 1 (one-shot)") - || !has("[Checkpoint] 00.04: passed on attempt 2 (1 reset)") - || !has("[Segment] 00: 1 of 2 checkpoints one-shot") - ){ - return std::format("Expected 00.03 one-shot and 00.04 passed on attempt 2:{}", joined(lines)); - } - return true; -} - - -// The two tables of test_segment_of_tables() as a segment run by `executor`, -// with hooks that write saves and reloads into its trace. -struct PolicyLab{ - std::vector tables{ - lab_checkpoint(), - StoryCheckpoint{ - .id = "00.04", - .title = "the parcel", - .start = declared("Lab", {6, 4}, Direction::NORTH), - .steps = {make(StoryStepKind::TALK, "give_parcel")}, - }, - }; - FakeExecutor executor; - std::vector lines; - - SegmentReport run(const SegmentPolicy& policy){ - Segment segment; - segment.id = "00"; - for (const StoryCheckpoint& data : tables){ - segment.checkpoints.push_back(CheckpointSpec{ - .id = data.id, .title = data.title, .saves = true, - .build = [&data, this]{ return build_story_checkpoint(data, executor); }, - }); - } - SegmentHooks hooks; - hooks.save = [this](RunState&){ - executor.trace.push_back("save"); - return outcome_ok(); - }; - hooks.reload = [this](RunState&){ - executor.trace.push_back("reload"); - return outcome_ok(); - }; - hooks.log = [this](const std::string& line){ lines.push_back(line); }; - RunState state; - return run_segment(SegmentRange{}, policy, segment, hooks, state); - } - bool has(std::string_view line) const{ - return std::find(lines.begin(), lines.end(), line) != lines.end(); - } - size_t count(std::string_view item) const{ - return (size_t)std::count(executor.trace.begin(), executor.trace.end(), item); - } -}; - -UnitTestResult test_policy_attempts_then_fail(){ - // Three attempts by default, then the segment stops on the last one's failure. - PolicyLab lab; - lab.executor.failing = {"give_parcel"}; - const SegmentReport report = lab.run(SegmentPolicy{}); - if (report.completed || report.reloads != 2 || lab.count("TALK give_parcel") != 3 - || report.runs.size() != 2 || report.runs[1].passed || report.runs[1].attempts != 3 - || report.failed_checkpoint != "00.04" || report.failure.failure != "blocked" - ){ - return std::format("Expected 00.04 tried 3 times, then failed:{}{}", joined(lab.executor.trace), joined(lab.lines)); - } - return true; -} - -UnitTestResult test_policy_stop_at_first_failure(){ - PolicyLab lab; - lab.executor.failing = {"give_parcel"}; - const SegmentReport report = lab.run(SegmentPolicy{.attempts_per_checkpoint = 1}); - if (report.completed || report.reloads != 0 || lab.count("reload") != 0 - || lab.count("TALK give_parcel") != 1 - ){ - return std::format("Expected a stop at the first failure, with no reload:{}{}", joined(lab.executor.trace), joined(lab.lines)); - } - return true; -} - -// A boss's checkpoint: a TEACH of a move the lead knows (changes -// nothing), a TEACH that teaches, both once per save, then the battle, -// lost on the first attempt. The segment's saves empty `prepared`. -struct BossLab{ - StoryCheckpoint table{ - .id = "00.15b", - .title = "the rival", - .start = declared("Cerulean", {23, 7}, Direction::NORTH), - .steps = {once("teach_known"), once("teach_new"), make(StoryStepKind::BATTLE, "rival")}, - }; - FakeExecutor executor; - PreparedSteps prepared; - std::vector lines; - - static StoryStep once(std::string name){ - StoryStep step = make(StoryStepKind::GAME, name); - step.action = name; - step.once_per_save = true; - return step; - } - BossLab(){ - executor.unchanged = {"teach_known"}; - } - Segment segment(){ - Segment segment; - segment.id = "00"; - segment.checkpoints.push_back(CheckpointSpec{ - .id = table.id, .title = table.title, .saves = true, - .build = [this]{ return build_story_checkpoint(table, executor, &prepared); }, - }); - return segment; - } - SegmentHooks hooks(){ - SegmentHooks hooks; - hooks.save = [this](RunState&){ - executor.trace.push_back("save"); - prepared.clear(); - return outcome_ok(); - }; - hooks.reload = [this](RunState&){ - executor.trace.push_back("reload"); - return outcome_ok(); - }; - hooks.log = [this](const std::string& line){ lines.push_back(line); }; - return hooks; - } - size_t count(std::string_view item) const{ - return (size_t)std::count(executor.trace.begin(), executor.trace.end(), item); - } -}; - -// A retry after a reload of the same save skips the preparation that -// changed nothing and goes to the battle; the TEACH that taught runs -// again, as the reload undid it. The save at the end empties the memory. -UnitTestResult test_once_per_save_retry(){ - BossLab lab; - StoryStep battle = make(StoryStepKind::BATTLE, "rival"); - battle.args = {{"may_lose", true}}; - lab.table.steps.back() = battle; - lab.executor.failures_left = {{"rival", 1}}; - Segment segment = lab.segment(); - SegmentHooks hooks = lab.hooks(); - RunState state; - const SegmentReport report = run_segment(SegmentRange{}, SegmentPolicy{}, segment, hooks, state); - if (!report.completed || report.reloads != 1 || lab.count("GAME teach_known") != 1 - || lab.count("GAME teach_new") != 2 || lab.count("BATTLE rival") != 2 - ){ - return std::format("Expected the known TEACH once, the new one twice, the battle twice:{}{}", joined(lab.executor.trace), joined(lab.lines)); - } - if (!lab.prepared.empty()){ - return "Expected the save to empty the prepared steps."; - } - return true; -} - -// Verify mode: three runs from one save; the preparation that changed -// nothing runs in the first only. -UnitTestResult test_once_per_save_verify(){ - BossLab lab; - StoryStep battle = make(StoryStepKind::BATTLE, "rival"); - battle.args = {{"may_lose", true}}; - lab.table.steps.back() = battle; - Segment segment = lab.segment(); - SegmentHooks hooks = lab.hooks(); - RunState state; - const VerifyReport report = verify_checkpoint("00.15b", VerifyPolicy{}, segment, hooks, state); - if (!report.verified || report.runs != 3 || lab.count("GAME teach_known") != 1 - || lab.count("GAME teach_new") != 3 || lab.count("BATTLE rival") != 3 - ){ - return std::format("Expected the known TEACH once in 3 runs, the new one each run:{}{}", joined(lab.executor.trace), joined(lab.lines)); - } - // Without the memory, every run prepares. - FakeExecutor plain; - plain.unchanged = {"teach_known"}; - Segment without; - without.id = "00"; - without.checkpoints.push_back(CheckpointSpec{ - .id = lab.table.id, .title = "", .saves = true, - .build = [&]{ return build_story_checkpoint(lab.table, plain); }, - }); - RunState again; - verify_checkpoint("00.15b", VerifyPolicy{}, without, hooks, again); - if (std::count(plain.trace.begin(), plain.trace.end(), "GAME teach_known") != 3){ - return std::format("Expected every run to prepare without PreparedSteps:{}", joined(plain.trace)); - } - return true; -} - - // An executor with pause points pauses before each of its checkpoint's steps, // labelled with the checkpoint and the step's place; a skipped step does not // pause, as it does not run. @@ -675,14 +422,9 @@ const StoryStepTestCase STORY_STEP_CASES[] = { {.name = "each step starts where the last left the player", .test = test_pose_propagation}, {.name = "a failure names its step", .test = test_failure_names_the_step}, {.name = "unsupported kinds and lost battles", .test = test_unsupported_and_lost_battles}, - {.name = "tables as a segment, with a reload", .test = test_segment_of_tables}, {.name = "pause points pause before each step", .test = test_pause_points}, {.name = "lint", .test = test_lint}, {.name = "a segment's run in the AutoStory stats", .test = test_story_stats}, - {.name = "policy: attempts per checkpoint, then fail", .test = test_policy_attempts_then_fail}, - {.name = "policy: one attempt stops at the first failure", .test = test_policy_stop_at_first_failure}, - {.name = "once per save: a retry skips what changed nothing", .test = test_once_per_save_retry}, - {.name = "once per save: verify runs prepare once", .test = test_once_per_save_verify}, }; } // namespace diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp index 8473ed991d..1e3a662720 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp @@ -2,9 +2,9 @@ * * From: https://github.com/PokemonAutomation/ * - * When a run may save (Pokemon_StorySaveMode.h), the phase names a - * checkpoint's metadata uses (Pokemon_StoryCheckpointMetadata.h), and - * where a run pauses (Pokemon_StoryBreakpoints.h). + * The phase names a checkpoint's metadata uses + * (Pokemon_StoryCheckpointMetadata.h) and where a run pauses + * (Pokemon_StoryBreakpoints.h). * */ @@ -16,7 +16,6 @@ #include "Pokemon_StoryBreakpoints.h" #include "Pokemon_StoryCheckpointMetadata.h" #include "Pokemon_StoryRunTypes_Tests.h" -#include "Pokemon_StorySaveMode.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -25,72 +24,6 @@ namespace Pokemon{ namespace{ -// One row of the save decision: mode x verdict x save point. -struct SaveDecisionCase{ - struct Input{ - StorySaveMode mode; - CheckpointVerdict verdict; - SavePoint point; - }; - - struct Expectation{ - bool saves; - }; - - Input input; - Expectation expectation; -}; - -std::string decision_name(const SaveDecisionCase::Input& input){ - return std::format( - "{}, {}, {}", story_save_mode_name(input.mode), - input.verdict == CheckpointVerdict::PASSED ? "passed" : "failed", - input.point == SavePoint::RUN_END ? "run end" : "checkpoint end" - ); -} - -class Test_SaveDecision : public UnitTest{ -public: - explicit Test_SaveDecision(SaveDecisionCase test_case) - : UnitTest(std::format("Pokemon::StoryRunTypes - save decision: {}", decision_name(test_case.input))) - , m_case(test_case) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const SaveDecisionCase::Input& in = m_case.input; - const bool saves = story_save_allowed(in.mode, in.verdict, in.point); - if (saves != m_case.expectation.saves){ - return std::format("expected {}, got {}", m_case.expectation.saves ? "a save" : "no save", saves ? "a save" : "no save"); - } - return true; - } - -private: - SaveDecisionCase m_case; -}; - -using M = StorySaveMode; -using V = CheckpointVerdict; -using S = SavePoint; - -// A failed checkpoint never saves; NONE never saves; AFTER_VERIFICATION -// saves only at the run's end; PER_CHECKPOINT at every checkpoint's end. -const SaveDecisionCase SAVE_DECISIONS[] = { - {.input = {M::NONE, V::FAILED, S::CHECKPOINT_END}, .expectation = {.saves = false}}, - {.input = {M::NONE, V::FAILED, S::RUN_END}, .expectation = {.saves = false}}, - {.input = {M::NONE, V::PASSED, S::CHECKPOINT_END}, .expectation = {.saves = false}}, - {.input = {M::NONE, V::PASSED, S::RUN_END}, .expectation = {.saves = false}}, - {.input = {M::AFTER_VERIFICATION, V::FAILED, S::CHECKPOINT_END}, .expectation = {.saves = false}}, - {.input = {M::AFTER_VERIFICATION, V::FAILED, S::RUN_END}, .expectation = {.saves = false}}, - {.input = {M::AFTER_VERIFICATION, V::PASSED, S::CHECKPOINT_END}, .expectation = {.saves = false}}, - {.input = {M::AFTER_VERIFICATION, V::PASSED, S::RUN_END}, .expectation = {.saves = true}}, - {.input = {M::PER_CHECKPOINT, V::FAILED, S::CHECKPOINT_END}, .expectation = {.saves = false}}, - {.input = {M::PER_CHECKPOINT, V::FAILED, S::RUN_END}, .expectation = {.saves = false}}, - {.input = {M::PER_CHECKPOINT, V::PASSED, S::CHECKPOINT_END}, .expectation = {.saves = true}}, - {.input = {M::PER_CHECKPOINT, V::PASSED, S::RUN_END}, .expectation = {.saves = true}}, -}; - - // A named check that returns its own failure. struct NamedCheck{ std::string_view name; @@ -234,9 +167,6 @@ const NamedCheck CHECKS[] = { void add_tests_StoryRunTypes(UnitTestDatabase& database){ - for (const SaveDecisionCase& test_case : SAVE_DECISIONS){ - database.add(test_case); - } for (const NamedCheck& check : CHECKS){ database.add(check); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp deleted file mode 100644 index 7fbeebf500..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* Story Save Mode - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include "Pokemon_StorySaveMode.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -std::string story_save_mode_name(StorySaveMode mode){ - switch (mode){ - case StorySaveMode::NONE: - return "none"; - case StorySaveMode::AFTER_VERIFICATION: - return "after_verification"; - case StorySaveMode::PER_CHECKPOINT: - return "per_checkpoint"; - } - return "?"; -} - -bool story_save_allowed(StorySaveMode mode, CheckpointVerdict verdict, SavePoint point){ - if (verdict != CheckpointVerdict::PASSED){ - return false; - } - switch (mode){ - case StorySaveMode::NONE: - return false; - case StorySaveMode::AFTER_VERIFICATION: - return point == SavePoint::RUN_END; - case StorySaveMode::PER_CHECKPOINT: - return true; - } - return false; -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h deleted file mode 100644 index c06b0f52c5..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h +++ /dev/null @@ -1,59 +0,0 @@ -/* Story Save Mode - * - * From: https://github.com/PokemonAutomation/ - * - * When an AutoStory run may save the game. A run's own verdict can be - * wrong (a warp confirmed on the next floor while the player is still on - * the one before), and a save made then overwrites the only good one. So a segment - * under test never saves, a verified one saves once at its end, and only - * a playthrough of verified segments saves at each checkpoint. - * - */ - -#ifndef PokemonAutomation_Pokemon_StorySaveMode_H -#define PokemonAutomation_Pokemon_StorySaveMode_H - -#include - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// When a run saves. A failed run never saves, in any mode. -enum class StorySaveMode{ - // Testing: never save; the save the run started from stays. - NONE, - // Verification: save once, at the end of a run that passed. - AFTER_VERIFICATION, - // Reproduction (a playthrough of verified segments): save at the end - // of each checkpoint that passed. - PER_CHECKPOINT, -}; - -// Returns the wire and log name of a save mode: "none", "after_verification", "per_checkpoint". -std::string story_save_mode_name(StorySaveMode mode); - -// How a checkpoint ended: passed (and its postconditions, where checked, -// did not fail) or not. -enum class CheckpointVerdict{ - PASSED, - FAILED, -}; - -// Where a save would be made. -enum class SavePoint{ - // A checkpoint's end, with more checkpoints of the run after it. - CHECKPOINT_END, - // The last checkpoint's end: the run's end state. - RUN_END, -}; - -// Whether a run in `mode` saves at `point` after a checkpoint that ended -// as `verdict`. -bool story_save_allowed(StorySaveMode mode, CheckpointVerdict verdict, SavePoint point); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h index 898690f9eb..2ab25347c8 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h @@ -2,8 +2,8 @@ * * From: https://github.com/PokemonAutomation/ * - * The plain data of a segment run (Pokemon_StorySegments.h): its policy, - * the range of checkpoints to run and the report of what happened. + * The plain data of a segment run (Engine/Pokemon_StoryEngine.h): the + * report of what happened. * */ @@ -11,35 +11,16 @@ #define PokemonAutomation_Pokemon_StorySegmentTypes_H #include -#include #include #include #include "Pokemon/AutoStory/Pokemon_AutoStoryStats.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" -#include "Pokemon_StorySaveMode.h" + namespace PokemonAutomation{ namespace Pokemon{ -// Retry and timeout configuration for story segment execution. -struct SegmentPolicy{ - // Runs of one checkpoint, reloading its save between them, before the - // segment stops. 1 stops at the first failure without reloading, leaving - // the game where it failed for a look. - size_t attempts_per_checkpoint = 3; - // When the run saves. In the other modes no checkpoint after the first - // has a save at its start, so only the first is retried with a reload. - // The options' default is NONE; the engine's stays the playthrough's. - StorySaveMode save_mode = StorySaveMode::PER_CHECKPOINT; -}; - -// The checkpoints to run, by id, both inclusive. An unset end is the -// segment's first or last checkpoint. -struct SegmentRange{ - std::optional from; - std::optional to; -}; // What a story run adds to the program's stats. diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 8b7ec6d4e5..821f17d301 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1484,12 +1484,6 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryOutcomeClass.h Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp Source/Pokemon/AutoStory/Pokemon_StoryPose.h - Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.cpp - Source/Pokemon/AutoStory/Pokemon_StorySegmentSaves_Tests.h - Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp - Source/Pokemon/AutoStory/Pokemon_StorySegments.h - Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp - Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.cpp Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor.h Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -1512,8 +1506,6 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h - Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp - Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.cpp Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h