From 6a1e81abb701a36eeea6eddb3ed2ad50a7d00f87 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 09:43:29 -0800 Subject: [PATCH 1/2] AutoStory: save modes and a postcondition check A run's own verdict can be wrong (FRLG 00.31, 2026-11-10: a warp leg confirmed B3F while still on B4F) and its save overwrote the only save. - StorySaveMode none / after_verification / per_checkpoint, and story_save_allowed() as the decision table. A failed checkpoint never saves. SegmentPolicy and VerifyPolicy carry the mode; StoryRunOptions shows it (default none) and a "check postconditions only" option. - run_segment: none skips every save (logged), after_verification saves once at the run's end after the game's postcondition check (a failed check fails the run, no save), per_checkpoint is today's behaviour. Without per-checkpoint saves only the first checkpoint reloads. - verify_checkpoint: after_verification saves at the end of the last run when every run passed. - StoryPostconditions on the checkpoint (position, items, flags) in the checkpoint JSON, and check_postconditions(): what the game can't read is "not checked", never passed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Data/Pokemon_StoryStepJson.cpp | 38 +- .../AutoStory/Pokemon_AutoStory_Tests.h | 3 + .../Pokemon_StoryPostconditionCheck.cpp | 225 ++++++++ .../Pokemon_StoryPostconditionCheck.h | 89 ++++ .../Pokemon_StorySavePolicy_Tests.cpp | 498 ++++++++++++++++++ .../AutoStory/Pokemon_StorySegments.cpp | 132 ++++- .../Pokemon/AutoStory/Pokemon_StorySegments.h | 17 +- .../AutoStory/Types/Pokemon_StoryCheckpoint.h | 3 + .../Types/Pokemon_StoryPostconditions.h | 43 ++ .../AutoStory/Types/Pokemon_StorySaveMode.cpp | 43 ++ .../AutoStory/Types/Pokemon_StorySaveMode.h | 59 +++ .../Types/Pokemon_StorySegmentTypes.h | 5 + .../Options/Pokemon_StoryRunOptions.cpp | 38 ++ .../Pokemon/Options/Pokemon_StoryRunOptions.h | 16 +- SerialPrograms/cmake/SourceFiles.cmake | 6 + 15 files changed, 1192 insertions(+), 23 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp index f81ecc7ef2..98f434c568 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Data/Pokemon_StoryStepJson.cpp @@ -280,6 +280,36 @@ StoryActionArgs read_args( } +JsonValue postconditions_json(const StoryPostconditions& postconditions){ + JsonObject result; + if (postconditions.position){ + result["position"] = grid_pose_json(*postconditions.position); + } + if (!postconditions.items.empty()){ + result["items"] = items_json(postconditions.items); + } + if (!postconditions.flags.empty()){ + result["flags"] = strings_json(postconditions.flags); + } + return result; +} + +StoryPostconditions read_postconditions(const JsonValue& value, std::string_view source){ + const JsonObject& root = fields(value, {"position", "items", "flags"}, source); + StoryPostconditions result; + if (const JsonValue* field = root.get_value("position")){ + result.position = read_grid_pose(*field, source); + } + if (const JsonValue* field = root.get_value("items")){ + result.items = read_items(*field, source); + } + if (const JsonValue* field = root.get_value("flags")){ + result.flags = read_strings(*field, source); + } + return result; +} + + } // namespace @@ -366,11 +396,14 @@ JsonValue story_checkpoint_json(const StoryCheckpoint& checkpoint){ events.push_back(story_step_json(step)); } result["events"] = std::move(events); + if (checkpoint.postconditions != StoryPostconditions{}){ + result["postconditions"] = postconditions_json(checkpoint.postconditions); + } return result; } StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view source, std::span actions){ - const JsonObject& root = fields(value, {"id", "title", "start", "events"}, source); + const JsonObject& root = fields(value, {"id", "title", "start", "events", "postconditions"}, source); StoryCheckpoint result; result.id = root.get_string_throw("id"); if (result.id.empty()){ @@ -388,6 +421,9 @@ StoryCheckpoint read_story_checkpoint(const JsonValue& value, std::string_view s } result.steps.push_back(std::move(step)); } + if (const JsonValue* postconditions = root.get_value("postconditions")){ + result.postconditions = read_postconditions(*postconditions, source); + } return result; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index b9dfbcc80f..5fa2aad20e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -27,6 +27,8 @@ void add_tests_StoryStepJson(UnitTestDatabase& database); void add_tests_StoryActionArg(UnitTestDatabase& database); // Registers unit tests for the shared authored envelope, game identity and embedded-resource loading. void add_tests_AuthoredCheckpointJson(UnitTestDatabase& database); +// Registers unit tests for when a run saves (save modes) and the postcondition check. +void add_tests_StorySavePolicy(UnitTestDatabase& database); // Registers the story engine's suites above (StoryEvents and StorySegments are // registered on their own), for the unit test runner. @@ -35,6 +37,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryStepJson(database); add_tests_StoryActionArg(database); add_tests_AuthoredCheckpointJson(database); + add_tests_StorySavePolicy(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp new file mode 100644 index 0000000000..af4b83a44b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp @@ -0,0 +1,225 @@ +/* Story Postcondition Check + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include "Pokemon_StoryPostconditionCheck.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +std::string verdict_name(PostconditionVerdict verdict){ + switch (verdict){ + case PostconditionVerdict::PASSED: + return "passed"; + case PostconditionVerdict::FAILED: + return "failed"; + case PostconditionVerdict::NOT_CHECKED: + return "not checked"; + } + return "?"; +} + +PostconditionResult result(std::string what, PostconditionVerdict verdict, std::string detail){ + return PostconditionResult{.what = std::move(what), .verdict = verdict, .detail = std::move(detail)}; +} + +PostconditionResult not_read(std::string what){ + return result(std::move(what), PostconditionVerdict::NOT_CHECKED, "the game can't read it"); +} + +// The first pose among a step's args (a route's target). +std::optional pose_arg(const StoryStep& step){ + for (const auto& [unused, value] : step.args){ + if (const GridPose* pose = std::get_if(&value)){ + return *pose; + } + } + return std::nullopt; +} + +std::string tiles_text(const std::vector& tiles){ + std::string text; + for (const TilePos& tile : tiles){ + text += (text.empty() ? "" : " or ") + to_string(tile); + } + return text; +} + +void check_map(std::vector& out, const GridPose& end, const ObservedState& observed){ + if (!observed.map){ + out.push_back(not_read("map")); + return; + } + const bool same = *observed.map == end.map; + out.push_back(result( + "map", same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED, + same ? end.map : std::format("expected {}, read {}", end.map, *observed.map) + )); +} + +// The tile and facing mean nothing on another map: not checked then. +void check_tile(std::vector& out, const GridPose& end, const ObservedState& observed, bool map_failed){ + if (end.tiles.empty()){ + return; + } + if (map_failed){ + out.push_back(result("tile", PostconditionVerdict::NOT_CHECKED, "on another map")); + return; + } + if (!observed.tile){ + out.push_back(not_read("tile")); + return; + } + const bool on = std::find(end.tiles.begin(), end.tiles.end(), *observed.tile) != end.tiles.end(); + out.push_back(result( + "tile", on ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED, + on ? to_string(*observed.tile) : std::format("expected {}, read {}", tiles_text(end.tiles), to_string(*observed.tile)) + )); +} + +void check_facing(std::vector& out, const GridPose& end, const ObservedState& observed, bool map_failed){ + if (!end.facing){ + return; + } + if (map_failed){ + out.push_back(result("facing", PostconditionVerdict::NOT_CHECKED, "on another map")); + return; + } + if (!observed.facing){ + out.push_back(not_read("facing")); + return; + } + const bool same = *observed.facing == *end.facing; + out.push_back(result( + "facing", same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED, + same ? direction_name(*end.facing) + : std::format("expected {}, read {}", direction_name(*end.facing), direction_name(*observed.facing)) + )); +} + +void check_position(std::vector& out, const StoryCheckpoint& checkpoint, const ObservedState& observed){ + const std::optional end = checkpoint_end_pose(checkpoint); + if (!end){ + return; + } + check_map(out, *end, observed); + const bool map_failed = out.back().verdict == PostconditionVerdict::FAILED; + check_tile(out, *end, observed, map_failed); + check_facing(out, *end, observed, map_failed); +} + +void check_items(std::vector& out, const StoryPostconditions& expected, const ObservedState& observed){ + for (const StoryItem& item : expected.items){ + const std::string what = "item " + item.item; + const auto read = observed.items.find(item.item); + if (read == observed.items.end()){ + out.push_back(not_read(what)); + continue; + } + const bool same = read->second == item.quantity; + out.push_back(result( + what, same ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED, + same ? std::format("{}", item.quantity) : std::format("expected {}, read {}", item.quantity, read->second) + )); + } +} + +void check_flags(std::vector& out, const StoryPostconditions& expected, const ObservedState& observed){ + for (const std::string& flag : expected.flags){ + const std::string what = "flag " + flag; + const auto read = observed.flags.find(flag); + if (read == observed.flags.end()){ + out.push_back(not_read(what)); + continue; + } + out.push_back(result( + what, read->second ? PostconditionVerdict::PASSED : PostconditionVerdict::FAILED, + read->second ? "set" : "expected set, read clear" + )); + } +} + +std::string summary_verdict(const PostconditionReport& report){ + if (report.results.empty()){ + return "nothing to check"; + } + if (report.count(PostconditionVerdict::FAILED) > 0){ + return "failed"; + } + return report.all_passed() ? "passed" : "incomplete"; +} + +} // namespace + + +size_t PostconditionReport::count(PostconditionVerdict verdict) const{ + size_t total = 0; + for (const PostconditionResult& item : results){ + total += item.verdict == verdict ? 1 : 0; + } + return total; +} + +bool PostconditionReport::all_passed() const{ + return !results.empty() && count(PostconditionVerdict::PASSED) == results.size(); +} + + +std::optional checkpoint_end_pose(const StoryCheckpoint& checkpoint){ + if (checkpoint.postconditions.position){ + return checkpoint.postconditions.position; + } + for (auto step = checkpoint.steps.rbegin(); step != checkpoint.steps.rend(); ++step){ + if (step->pose_after){ + return step->pose_after; + } + if (std::optional target = pose_arg(*step)){ + return target; + } + if (step->kind == StoryStepKind::LEGS){ + return std::nullopt; + } + } + return std::nullopt; +} + +PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint, const ObservedState& observed){ + PostconditionReport report; + report.checkpoint = checkpoint.id; + check_position(report.results, checkpoint, observed); + check_items(report.results, checkpoint.postconditions, observed); + check_flags(report.results, checkpoint.postconditions, observed); + return report; +} + +std::vector postcondition_log_lines(const PostconditionReport& report){ + std::vector lines; + for (const PostconditionResult& item : report.results){ + lines.push_back(std::format( + "[Postcondition] {} {}: {}: {}", report.checkpoint, item.what, verdict_name(item.verdict), item.detail + )); + } + lines.push_back(std::format( + "[Postcondition] {}: {} passed, {} failed, {} not checked: {}", + report.checkpoint, + report.count(PostconditionVerdict::PASSED), + report.count(PostconditionVerdict::FAILED), + report.count(PostconditionVerdict::NOT_CHECKED), + summary_verdict(report) + )); + return lines; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h new file mode 100644 index 0000000000..8e1b0c496a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h @@ -0,0 +1,89 @@ +/* Story Postcondition Check + * + * From: https://github.com/PokemonAutomation/ + * + * A checkpoint's postconditions (Types/Pokemon_StoryPostconditions.h) + * checked against what the game read once the run was over: a step of its + * own, not the run's verdict, which can be wrong. A postcondition the game + * can't read yet is reported as not checked, never as passed. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryPostconditionCheck_H +#define PokemonAutomation_Pokemon_StoryPostconditionCheck_H + +#include +#include +#include +#include +#include +#include +#include "Pokemon/AutoStory/Types/Pokemon_GridPose.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// What the game read after the run. Each part is unset (or, for items and +// flags, absent from the map) when the game can't read it: the matching +// postconditions are then not checked. +struct ObservedState{ + std::optional map; + std::optional tile; + std::optional facing; + // The Bag's count of each item read, 0 for one read and not there. + std::map> items; + // Each story flag read, and whether it is set. + std::map> flags; +}; + +// The result of one postcondition. +enum class PostconditionVerdict{ + PASSED, + FAILED, + NOT_CHECKED, +}; + +// One postcondition checked: "map", "tile", "facing", "item LIFT_KEY", +// "flag ...", its verdict and what was expected and read. +struct PostconditionResult{ + std::string what; + PostconditionVerdict verdict = PostconditionVerdict::NOT_CHECKED; + std::string detail; +}; + +// Every postcondition of one checkpoint, checked. +struct PostconditionReport{ + std::string checkpoint; + std::vector results; + + size_t count(PostconditionVerdict verdict) const; + // Every postcondition was read and held: none failed, none unchecked, + // and there was at least one. + bool all_passed() const; +}; + +// Where `checkpoint` ends: its postconditions' position, else the last +// step's declared pose or target. Unset when a step after that moves the +// player in a way the plan does not place (taps of legs). +std::optional checkpoint_end_pose(const StoryCheckpoint& checkpoint); + +// Checks the end position (map, tile among the pose's tiles, facing), +// then each item count and flag, against `observed`. +PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint, const ObservedState& observed); + +// The report as log lines, one per postcondition and a summary: +// [Postcondition] 00.31 map: passed: RocketHideout_B3F +// [Postcondition] 00.31 item RARE_CANDY: failed: expected 2, read 1 +// [Postcondition] 00.31 flag hideout_lift_key_grunt: not checked: the game can't read it +// [Postcondition] 00.31: 3 passed, 1 failed, 1 not checked: failed +// The summary is "passed" only when every postcondition passed; +// "incomplete" when none failed and some were not checked. +std::vector postcondition_log_lines(const PostconditionReport& report); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp new file mode 100644 index 0000000000..b587854125 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp @@ -0,0 +1,498 @@ +/* Story Save Policy Tests + * + * From: https://github.com/PokemonAutomation/ + * + * When a run saves (Types/Pokemon_StorySaveMode.h): the decision table, + * then the segment runner and verify mode in each mode against a world + * that only records what happened. Then the postcondition check + * (Pokemon_StoryPostconditionCheck.h) on FRLG 00.31's end state. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Data/Pokemon_StoryStepJson.h" +#include "Pokemon_StoryPostconditionCheck.h" +#include "Pokemon_StorySegments.h" +#include "Pokemon_AutoStory_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// One row of the decision table. +struct SaveDecision{ + StorySaveMode mode; + CheckpointVerdict verdict; + SavePoint point; + bool saves; +}; + +constexpr SaveDecision SAVE_DECISIONS[] = { + {StorySaveMode::NONE, CheckpointVerdict::FAILED, SavePoint::CHECKPOINT_END, false}, + {StorySaveMode::NONE, CheckpointVerdict::FAILED, SavePoint::RUN_END, false}, + {StorySaveMode::NONE, CheckpointVerdict::PASSED, SavePoint::CHECKPOINT_END, false}, + {StorySaveMode::NONE, CheckpointVerdict::PASSED, SavePoint::RUN_END, false}, + {StorySaveMode::AFTER_VERIFICATION, CheckpointVerdict::FAILED, SavePoint::CHECKPOINT_END, false}, + {StorySaveMode::AFTER_VERIFICATION, CheckpointVerdict::FAILED, SavePoint::RUN_END, false}, + {StorySaveMode::AFTER_VERIFICATION, CheckpointVerdict::PASSED, SavePoint::CHECKPOINT_END, false}, + {StorySaveMode::AFTER_VERIFICATION, CheckpointVerdict::PASSED, SavePoint::RUN_END, true}, + {StorySaveMode::PER_CHECKPOINT, CheckpointVerdict::FAILED, SavePoint::CHECKPOINT_END, false}, + {StorySaveMode::PER_CHECKPOINT, CheckpointVerdict::FAILED, SavePoint::RUN_END, false}, + {StorySaveMode::PER_CHECKPOINT, CheckpointVerdict::PASSED, SavePoint::CHECKPOINT_END, true}, + {StorySaveMode::PER_CHECKPOINT, CheckpointVerdict::PASSED, SavePoint::RUN_END, true}, +}; + +UnitTestResult test_decision_table(){ + for (const SaveDecision& row : SAVE_DECISIONS){ + if (story_save_allowed(row.mode, row.verdict, row.point) != row.saves){ + return "Wrong decision for " + story_save_mode_name(row.mode) + + (row.verdict == CheckpointVerdict::PASSED ? ", passed" : ", failed") + + (row.point == SavePoint::RUN_END ? ", run end" : ", mid-run"); + } + } + return true; +} + + +// 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](RunState&, const std::string& id){ + trace.push_back("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 += "\n" + line; + } + return text; + } +}; + +CheckpointSpec walk_row(Lab& lab, std::string id){ + 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(lab, std::string(id))); + } + return segment; +} + +SegmentReport run_in(Lab& lab, StorySaveMode mode){ + Segment segment = three_rows(lab); + SegmentHooks hooks = lab.hooks(); + RunState state; + return run_segment(state, segment, SegmentRange{}, SegmentPolicy{.save_mode = mode}, hooks); +} + +UnitTestResult test_none_never_saves(){ + Lab lab; + const SegmentReport report = run_in(lab, StorySaveMode::NONE); + const std::vector expected = {"00.01", "00.02", "00.03"}; + if (!report.completed || lab.trace != expected){ + return "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 "Expected the skipped saves logged:" + lab.all(); + } + return true; +} + +UnitTestResult test_per_checkpoint_saves_each(){ + Lab lab; + const SegmentReport report = run_in(lab, StorySaveMode::PER_CHECKPOINT); + const std::vector expected = {"00.01", "save", "00.02", "save", "00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return "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(lab, StorySaveMode::AFTER_VERIFICATION); + const std::vector expected = {"00.01", "00.02", "00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return "Expected one save, at the end:" + lab.all(); + } + if (!lab.has("[Postcondition] 00.03: not checked: the game has no postcondition check")){ + return "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(lab, StorySaveMode::AFTER_VERIFICATION); + const std::vector expected = {"00.01", "00.02", "00.03", "check 00.03", "save"}; + if (!report.completed || lab.trace != expected){ + return "Expected the check, then the save:" + lab.all(); + } + return true; +} + +// FRLG 00.31, 2026-11-10: the run said it passed on the wrong floor. +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(lab, StorySaveMode::AFTER_VERIFICATION); + if (report.completed || report.failed_checkpoint != "00.03"){ + return "Expected the run failed at its last checkpoint:" + lab.all(); + } + if (std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ + return "A run whose postconditions failed must not save:" + lab.all(); + } + if (!lab.has("[Checkpoint] 00.03: postconditions failed, not saving")){ + return "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(lab, StorySaveMode::NONE); + const std::vector expected = {"00.01", "00.02"}; + if (report.completed || report.reloads != 0 || lab.trace != expected){ + return "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 "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(lab, StorySaveMode::NONE); + const std::vector expected = {"00.01", "reload", "00.01", "00.02", "00.03"}; + if (!report.completed || report.reloads != 1 || lab.trace != expected){ + return "Expected the first checkpoint retried from the start save:" + lab.all(); + } + return true; +} + +VerifyReport verify_in(Lab& lab, StorySaveMode mode){ + 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(state, segment, "00.02", policy, hooks); +} + +UnitTestResult test_verify_saves_once_every_run_passed(){ + Lab lab; + const VerifyReport report = verify_in(lab, StorySaveMode::AFTER_VERIFICATION); + const std::vector expected = {"00.02", "reload", "00.02", "reload", "00.02", "save"}; + if (!report.verified || !report.saved || lab.trace != expected){ + return "Expected one save, after the last run:" + lab.all(); + } + if (!lab.has("[Verify] 00.02: every run passed: saved at the end state")){ + return "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(lab, StorySaveMode::AFTER_VERIFICATION); + if (!report.verified || report.saved || std::find(lab.trace.begin(), lab.trace.end(), "save") != lab.trace.end()){ + return "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 "Expected why it didn't save:" + lab.all(); + } + return true; +} + + +// FRLG 00.31's end, in the shared format: beside B3F's Rare Candy, with +// the LIFT KEY and both Rare Candies. +inline constexpr std::string_view CHECKPOINT_0031 = R"({ + "id": "00.31", + "title": "Rocket Hideout to the LIFT KEY", + "start": {"map": "CeladonCity_PokemonCenter_1F", "tiles": [[7, 4]], "facing": "north"}, + "events": [ + {"id": "beside_b3f_rare_candy", "kind": "route", "args": {"target": {"map": "RocketHideout_B3F", "tiles": [[12, 13]], "facing": "north"}}}, + {"id": "b3f_rare_candy", "kind": "talk"} + ], + "postconditions": { + "position": {"map": "RocketHideout_B3F", "tiles": [[12, 13]], "facing": "north"}, + "items": [{"item": "LIFT_KEY", "quantity": 1}, {"item": "RARE_CANDY", "quantity": 2}] + } +})"; + +StoryCheckpoint checkpoint_0031(){ + return read_story_checkpoint(parse_json(std::string(CHECKPOINT_0031)), "00.31"); +} + +ObservedState read_0031(std::string map, int candies){ + ObservedState observed; + observed.map = std::move(map); + observed.tile = TilePos{12, 13}; + observed.facing = Direction::NORTH; + observed.items = {{"LIFT_KEY", 1}, {"RARE_CANDY", (size_t)candies}}; + return observed; +} + +UnitTestResult test_postconditions_json(){ + const StoryCheckpoint checkpoint = checkpoint_0031(); + const StoryPostconditions& post = checkpoint.postconditions; + if (!post.position || post.position->map != "RocketHideout_B3F" || post.items.size() != 2 + || post.items[1] != StoryItem{.item = "RARE_CANDY", .quantity = 2} + ){ + return "Unexpected postconditions read."; + } + const StoryCheckpoint again = read_story_checkpoint(story_checkpoint_json(checkpoint), "again"); + if (again.postconditions != post){ + return "The postconditions should survive a write and read: " + story_checkpoint_json(checkpoint).dump(); + } + return true; +} + +UnitTestResult test_postconditions_pass(){ + const PostconditionReport report = check_postconditions(checkpoint_0031(), read_0031("RocketHideout_B3F", 2)); + const std::vector lines = postcondition_log_lines(report); + if (!report.all_passed() || lines.back() != "[Postcondition] 00.31: 5 passed, 0 failed, 0 not checked: passed"){ + return "Expected every postcondition passed: " + lines.back(); + } + return true; +} + +// The 2026-11-10 false pass: still on B4F, the Rare Candy not picked up. +UnitTestResult test_postconditions_fail(){ + const PostconditionReport report = check_postconditions(checkpoint_0031(), read_0031("RocketHideout_B4F", 1)); + const std::vector lines = postcondition_log_lines(report); + const std::vector expected = { + "[Postcondition] 00.31 map: failed: expected RocketHideout_B3F, read RocketHideout_B4F", + "[Postcondition] 00.31 tile: not checked: on another map", + "[Postcondition] 00.31 facing: not checked: on another map", + "[Postcondition] 00.31 item LIFT_KEY: passed: 1", + "[Postcondition] 00.31 item RARE_CANDY: failed: expected 2, read 1", + "[Postcondition] 00.31: 1 passed, 2 failed, 2 not checked: failed", + }; + if (lines != expected){ + std::string text; + for (const std::string& line : lines){ + text += "\n" + line; + } + return "Unexpected lines:" + text; + } + return true; +} + +UnitTestResult test_postconditions_not_checked(){ + StoryCheckpoint checkpoint = checkpoint_0031(); + checkpoint.postconditions.flags = {"hideout_lift_key_grunt"}; + ObservedState observed; + observed.map = "RocketHideout_B3F"; + const PostconditionReport report = check_postconditions(checkpoint, observed); + const std::vector lines = postcondition_log_lines(report); + if (report.all_passed() || report.count(PostconditionVerdict::NOT_CHECKED) != 5 + || lines.back() != "[Postcondition] 00.31: 1 passed, 0 failed, 5 not checked: incomplete" + ){ + return "What isn't read is not checked, never passed: " + lines.back(); + } + observed.flags["hideout_lift_key_grunt"] = false; + if (check_postconditions(checkpoint, observed).count(PostconditionVerdict::FAILED) != 1){ + return "A flag read clear should fail."; + } + return true; +} + +UnitTestResult test_end_pose_from_the_plan(){ + StoryCheckpoint checkpoint = checkpoint_0031(); + checkpoint.postconditions.position.reset(); + const std::optional end = checkpoint_end_pose(checkpoint); + if (!end || end->map != "RocketHideout_B3F" || end->tiles != std::vector{TilePos{12, 13}}){ + return "Expected the last route's target as the end."; + } + StoryStep legs; + legs.kind = StoryStepKind::LEGS; + legs.name = "taps"; + checkpoint.steps.push_back(std::move(legs)); + if (checkpoint_end_pose(checkpoint)){ + return "Legs after the last target leave the end unknown."; + } + if (!check_postconditions(checkpoint, read_0031("RocketHideout_B3F", 2)).all_passed()){ + return "Without an end pose, the items alone should be checked."; + } + return true; +} + + +// The scenario to run. +struct SavePolicyInput{ + std::string name; + UnitTestResult (*test)() = nullptr; +}; + +// What the scenario checks; it returns its own failure. +struct SavePolicyExpectation{ + std::string_view description; +}; + +// A save-policy scenario and its expected result. +struct SavePolicyCase{ + SavePolicyInput input; + SavePolicyExpectation expectation; +}; + +class Test_StorySavePolicy : public UnitTest{ +public: + explicit Test_StorySavePolicy(SavePolicyCase test_case) + : UnitTest("Pokemon::StorySavePolicy - " + test_case.input.name) + , m_case(std::move(test_case)) + {} + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + return m_case.input.test(); + } + +private: + SavePolicyCase m_case; +}; + +const SavePolicyCase SAVE_POLICY_CASES[] = { + { + .input = {.name = "decision table", .test = test_decision_table}, + .expectation = {.description = "mode x verdict x save point, all twelve rows"}, + }, + { + .input = {.name = "none never saves", .test = test_none_never_saves}, + .expectation = {.description = "three passing checkpoints, no save, each skip logged"}, + }, + { + .input = {.name = "per checkpoint saves each", .test = test_per_checkpoint_saves_each}, + .expectation = {.description = "a save after every passing checkpoint"}, + }, + { + .input = {.name = "after verification saves at the end", .test = test_after_verification_saves_at_the_end}, + .expectation = {.description = "one save after the last checkpoint; no check logged as not checked"}, + }, + { + .input = {.name = "after verification checks before saving", .test = test_after_verification_checks_before_saving}, + .expectation = {.description = "the game's postcondition check runs before the save"}, + }, + { + .input = {.name = "failed postconditions never save", .test = test_failed_postconditions_never_save}, + .expectation = {.description = "a failed check fails the run and refuses the save"}, + }, + { + .input = {.name = "no reload after the first checkpoint", .test = test_no_reload_after_the_first}, + .expectation = {.description = "a later checkpoint has no save to reload, so it fails at once"}, + }, + { + .input = {.name = "the first checkpoint still reloads", .test = test_first_row_still_reloads}, + .expectation = {.description = "the first checkpoint reloads the run's start save"}, + }, + { + .input = {.name = "verify saves once every run passed", .test = test_verify_saves_once_every_run_passed}, + .expectation = {.description = "3/3 passed: one save at the end of run 3"}, + }, + { + .input = {.name = "verify: no save after a failed run", .test = test_verify_no_save_after_a_failed_run}, + .expectation = {.description = "2/3 verified, but no save"}, + }, + { + .input = {.name = "postconditions: json", .test = test_postconditions_json}, + .expectation = {.description = "00.31's postconditions read and written back"}, + }, + { + .input = {.name = "postconditions: pass", .test = test_postconditions_pass}, + .expectation = {.description = "B3F (12, 13) north, LIFT_KEY 1, RARE_CANDY 2: passed"}, + }, + { + .input = {.name = "postconditions: fail", .test = test_postconditions_fail}, + .expectation = {.description = "still on B4F with 1 Rare Candy: failed"}, + }, + { + .input = {.name = "postconditions: not checked", .test = test_postconditions_not_checked}, + .expectation = {.description = "unread parts are not checked, never passed"}, + }, + { + .input = {.name = "postconditions: end pose from the plan", .test = test_end_pose_from_the_plan}, + .expectation = {.description = "the last route's target, or unknown after legs"}, + }, +}; + +} // namespace + + +void add_tests_StorySavePolicy(UnitTestDatabase& database){ + for (const SavePolicyCase& test_case : SAVE_POLICY_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp index 64becf2b68..ccaf449d6c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -103,36 +103,108 @@ Outcome reload(RunState& state, SegmentHooks& hooks){ 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(hooks, std::format("[Postcondition] {}: not checked: the game has no postcondition check", id)); + return save(state); + } + const Outcome checked = hooks.check_postconditions(state, id); + if (!checked.succeeded()){ + say(hooks, std::format("[Checkpoint] {}: postconditions failed, not saving", id)); + 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(Checkpoint& checkpoint, const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks){ + 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(hooks, std::format("[Checkpoint] {}: not saving (save mode {})", id, story_save_mode_name(mode))); + 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, 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( - RunState& state, const Segment& segment, CheckpointSpec& spec, + RunState& state, const Segment& segment, const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run ){ + const size_t attempts = row_attempts(row, policy); for (size_t attempt = 1; ; attempt++){ run.attempts = attempt; - Checkpoint checkpoint = spec.build(); - if (spec.saves && !checkpoint.save){ - checkpoint.save = [&hooks](RunState& state){ - return hooks.save ? hooks.save(state) : outcome_failed("no_save_hook"); - }; - } + Checkpoint checkpoint = row.spec.build(); + set_save(checkpoint, row, policy, hooks); EventLogger log = [&hooks](const std::string& line){ say(hooks, line); }; CheckpointReport result = run_checkpoint(state, checkpoint, log); if (result.completed){ run.passed = true; - say(hooks, checkpoint_pass_line(spec.id, attempt)); + say(hooks, checkpoint_pass_line(row.spec.id, attempt)); return result; } if (result.failure.failure == GAME_NOT_RUNNING){ - say(hooks, std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, spec.id)); + say(hooks, std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id)); return result; } - if (attempt >= policy.attempts_per_checkpoint){ + if (attempt >= attempts){ + if (attempts < policy.attempts_per_checkpoint){ + say(hooks, 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) + )); + } return result; } - say(hooks, segment_reload_line(segment.id, spec.id, attempt + 1, policy.attempts_per_checkpoint)); + say(hooks, segment_reload_line(segment.id, row.spec.id, attempt + 1, policy.attempts_per_checkpoint)); report.reloads++; Outcome reloaded = reload(state, hooks); if (!reloaded.succeeded()){ @@ -167,11 +239,15 @@ SegmentReport run_segment( return stop_segment(report, hooks, segment.id, selected.failure); } say(hooks, std::format("[Segment] {}: start, {} checkpoints", segment.id, selected.last - selected.first + 1)); + if (policy.save_mode != StorySaveMode::PER_CHECKPOINT){ + say(hooks, std::format("[Segment] {}: save mode {}", segment.id, story_save_mode_name(policy.save_mode))); + } 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}); - CheckpointReport result = run_row(state, segment, spec, policy, hooks, report, report.runs.back()); + const Row row{.spec = spec, .first = c == selected.first, .last = c == selected.last}; + CheckpointReport result = run_row(state, segment, row, policy, hooks, report, report.runs.back()); if (!result.completed){ report.failed_checkpoint = spec.id; report.failed_event = result.failed_name; @@ -187,13 +263,20 @@ SegmentReport run_segment( namespace{ -// One verify run: the checkpoint built fresh, without its end save. -// Returns true if it passed. +// 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( - RunState& state, CheckpointSpec& spec, size_t run, const VerifyPolicy& policy, SegmentHooks& hooks + RunState& state, CheckpointSpec& spec, size_t run, const VerifyPolicy& policy, SegmentHooks& hooks, + bool save_at_end ){ Checkpoint checkpoint = spec.build(); - checkpoint.save = nullptr; + 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(hooks, line); }; const CheckpointReport result = run_checkpoint(state, checkpoint, log); if (result.completed){ @@ -209,7 +292,19 @@ bool verify_run( 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(VerifyReport& report, const VerifyPolicy& policy, SegmentHooks& hooks){ + if (policy.save_mode == StorySaveMode::AFTER_VERIFICATION){ + say(hooks, std::format( + "[Verify] {}: {}", report.checkpoint, + report.saved ? "every run passed: saved at the end state" : "not every run passed: not saving" + )); + } report.verified = report.failure.succeeded() && report.passes >= policy.passes_needed; say(hooks, std::format( "[Verify] {}: {}/{} passed, {} needed: {}", @@ -252,7 +347,10 @@ VerifyReport verify_checkpoint( } } report.runs++; - report.passes += verify_run(state, spec, run, policy, hooks) ? 1 : 0; + const bool save_at_end = verify_saves_after(report, policy, run); + const bool passed = verify_run(state, spec, run, policy, hooks, save_at_end); + report.passes += passed ? 1 : 0; + report.saved = save_at_end && passed; } return verify_summary(report, policy, hooks); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h index da96488d14..b7b43c633c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h @@ -57,9 +57,15 @@ struct SegmentHooks{ // 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. Logs through `hooks.log`, +// 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 // [Segment] 00: 00.02 failed, reloading for attempt 2/3 @@ -83,6 +89,9 @@ 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. @@ -92,13 +101,17 @@ struct VerifyReport{ 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. +// 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 diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h index 9374862ecb..1a4719017c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -15,6 +15,7 @@ #include #include #include "Pokemon_GridPose.h" +#include "Pokemon_StoryPostconditions.h" #include "Pokemon_StoryStep.h" namespace PokemonAutomation{ @@ -28,6 +29,8 @@ struct StoryCheckpoint{ // Where its starting save leaves the player; none for a new game. std::optional start; std::vector steps; + // What must hold when it is done; checked on its own after a run. + StoryPostconditions postconditions; }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h new file mode 100644 index 0000000000..5b3cd7df37 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h @@ -0,0 +1,43 @@ +/* Story Postconditions + * + * From: https://github.com/PokemonAutomation/ + * + * What must hold once a checkpoint is done, as plain data from its plan: + * where the player stands, what the Bag holds, which story flags moved. + * check_postconditions() (Pokemon_StoryPostconditionCheck.h) checks them + * against what the game read, as a step of its own after the run. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryPostconditions_H +#define PokemonAutomation_Pokemon_StoryPostconditions_H + +#include +#include +#include +#include "Pokemon_GridPose.h" +#include "Pokemon_StoryActionArg.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The end state a checkpoint's plan promises: position, Bag items and flags. +struct StoryPostconditions{ + // Where the checkpoint ends; unset, it is the last step's target + // (checkpoint_end_pose()). + std::optional position; + // Each item's count in the Bag, exactly, by the game's item id + // ("LIFT_KEY", "RARE_CANDY"). + std::vector items; + // Story flags that must be set: a trainer beaten, a door open. + std::vector flags; + + bool operator==(const StoryPostconditions& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp new file mode 100644 index 0000000000..7fbeebf500 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.cpp @@ -0,0 +1,43 @@ +/* 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 new file mode 100644 index 0000000000..04ad331908 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h @@ -0,0 +1,59 @@ +/* Story Save Mode + * + * From: https://github.com/PokemonAutomation/ + * + * When an AutoStory run may save the game. A run's own verdict can be + * wrong (FRLG 00.31, 2026-11-10: a warp leg confirmed B3F while still on + * B4F), 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 d245deacf1..8098f5082c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StorySegmentTypes.h @@ -16,6 +16,7 @@ #include #include "Pokemon/AutoStory/Pokemon_StoryEvents.h" #include "Pokemon/AutoStory/Pokemon_StoryLog.h" +#include "Pokemon_StorySaveMode.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -27,6 +28,10 @@ struct SegmentPolicy{ // 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 diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp index 478a3dc9c2..099747ca8a 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp @@ -22,6 +22,16 @@ const EnumDropdownDatabase& StoryCheckLevel_Database(){ } +const EnumDropdownDatabase& StorySaveMode_Database(){ + static const EnumDropdownDatabase database({ + {StorySaveMode::NONE, "none", "None: never save (testing a checkpoint; the start save stays)"}, + {StorySaveMode::AFTER_VERIFICATION, "after_verification", "After verification: save once, at the end of a run that passed"}, + {StorySaveMode::PER_CHECKPOINT, "per_checkpoint", "Each checkpoint: save after each checkpoint that passed (a playthrough)"}, + }); + return database; +} + + std::string story_checkpoint_label(const StoryCheckpoint& checkpoint){ return checkpoint.title.empty() ? checkpoint.id : checkpoint.id + " " + checkpoint.title; } @@ -85,11 +95,27 @@ StoryRunOptions::StoryRunOptions(const StringSelectDatabase& checkpoints, const LockMode::LOCK_WHILE_RUNNING, Language::English ) + , SAVE_MODE( + "Save:
When the run saves the game. A failed run never saves. " + "None keeps the save the run started from; After verification saves once at the end, after the checkpoint " + "passed 2-3 times; Each checkpoint is for a playthrough of verified checkpoints.", + StorySaveMode_Database(), + LockMode::LOCK_WHILE_RUNNING, + StorySaveMode::NONE + ) + , POSTCONDITIONS_ONLY( + "Check postconditions only:
Run nothing: read where the game stands and check the end checkpoint's " + "position, items and flags. What the game can't read yet is reported as not checked.", + LockMode::LOCK_WHILE_RUNNING, + false + ) { PA_ADD_OPTION(ATTEMPTS_PER_CHECKPOINT); PA_ADD_OPTION(CHECKS); PA_ADD_OPTION(LANGUAGE); PA_ADD_OPTION(VERIFY); + PA_ADD_OPTION(SAVE_MODE); + PA_ADD_OPTION(POSTCONDITIONS_ONLY); } std::string StoryRunOptions::check_validity() const{ @@ -100,12 +126,24 @@ std::string StoryRunOptions::check_validity() const{ if (VERIFY.enabled() && start_index() != end_index()){ return "Verify mode runs one checkpoint: set the end checkpoint to the start checkpoint."; } + if (VERIFY.enabled() && POSTCONDITIONS_ONLY){ + return "Check postconditions only runs nothing: turn off Verify Mode."; + } return error; } SegmentPolicy StoryRunOptions::policy() const{ SegmentPolicy policy; policy.attempts_per_checkpoint = ATTEMPTS_PER_CHECKPOINT; + policy.save_mode = SAVE_MODE; + return policy; +} + +std::optional StoryRunOptions::verify_policy() const{ + std::optional policy = VERIFY.policy(); + if (policy){ + policy->save_mode = SAVE_MODE; + } return policy; } diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h index a7ebea5640..43e979cbf8 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h @@ -6,8 +6,9 @@ * hard to try and how much to check: the shared range group * (Pokemon_StoryRangeOptions.h) plus the run policy, read into the shared * SegmentPolicy (Pokemon/AutoStory/Pokemon_StorySegments.h), the checks - * level (Types/Pokemon_StoryCheckLevel.h) and the language the game's steps - * read text in. + * level (Types/Pokemon_StoryCheckLevel.h), the language the game's steps + * read text in, when the run saves (Types/Pokemon_StorySaveMode.h) and + * whether it only checks postconditions. * */ @@ -18,6 +19,7 @@ #include #include #include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" #include "Common/Cpp/Options/EnumDropdownOption.h" #include "Common/Cpp/Options/GroupOption.h" #include "Common/Cpp/Options/SimpleIntegerOption.h" @@ -26,6 +28,7 @@ #include "Pokemon/AutoStory/Pokemon_StorySegments.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" #include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon/AutoStory/Types/Pokemon_StorySaveMode.h" #include "Pokemon_StoryRangeOptions.h" namespace PokemonAutomation{ @@ -70,8 +73,10 @@ class StoryRunOptions : public StoryRangeOptions{ // The range's check, then verify mode's: one checkpoint only. virtual std::string check_validity() const override; - // The policy these options set. + // The policy these options set, with the save mode. SegmentPolicy policy() const; + // Verify mode's runs and passes with the save mode; unset when off. + std::optional verify_policy() const; // The checks level, for the game's steps to read. StoryCheckLevel check_level() const; // The game's language, for the steps that read text. @@ -85,6 +90,11 @@ class StoryRunOptions : public StoryRangeOptions{ EnumDropdownOption LANGUAGE; // On, it runs one checkpoint: the start, which must also be the end. StoryVerifyOptions VERIFY; + // When the run saves; NONE (testing) by default. + EnumDropdownOption SAVE_MODE; + // On, nothing runs: the game reads where it stands and checks the end + // checkpoint's postconditions (Pokemon_StoryPostconditionCheck.h). + BooleanCheckBoxOption POSTCONDITIONS_ONLY; }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index bd52dc5ec3..85b75fea58 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1527,7 +1527,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h Source/Pokemon/AutoStory/Pokemon_StoryPose.h + Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp + Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h Source/Pokemon/AutoStory/Pokemon_StoryRun_Tests.h + Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp Source/Pokemon/AutoStory/Pokemon_StorySegments.h Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp @@ -1545,6 +1548,9 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h + 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 From e1f068b2297acc87924a4f7c54eeb9cd1d54e013 Mon Sep 17 00:00:00 2001 From: dayfine Date: Tue, 10 Nov 2026 14:32:23 -0800 Subject: [PATCH 2/2] AutoStory save policy: output parameters last, std::format The postcondition checks take their result vector last, set_save takes the checkpoint last, and SegmentHooks::check_postconditions now takes (checkpoint id, RunState&). Messages are built with std::format. Pokemon_StorySavePolicy_Tests.h declares the suite's add_tests function; the test file includes only that header. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013VUubhHeYSMGSSDXZkKZss --- .../AutoStory/Pokemon_AutoStory_Tests.h | 3 +- .../Pokemon_StoryPostconditionCheck.cpp | 28 +++--- .../Pokemon_StorySavePolicy_Tests.cpp | 87 ++++++++++--------- .../AutoStory/Pokemon_StorySavePolicy_Tests.h | 23 +++++ .../AutoStory/Pokemon_StorySegments.cpp | 6 +- .../Pokemon/AutoStory/Pokemon_StorySegments.h | 2 +- SerialPrograms/cmake/SourceFiles.cmake | 1 + 7 files changed, 88 insertions(+), 62 deletions(-) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index bb6adb7ba7..d76d3600bf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -14,6 +14,7 @@ #include "Common/Cpp/TestRunners/UnitTestDatabase.h" #include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" #include "Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StoryStep_Tests.h" namespace PokemonAutomation{ @@ -28,8 +29,6 @@ void add_tests_AutoStoryStats(UnitTestDatabase& database); void add_tests_StoryEvents(UnitTestDatabase& database); // Registers unit tests for story segment running, verification, and branching. void add_tests_StorySegments(UnitTestDatabase& database); -// Registers unit tests for when a run saves (save modes) and the postcondition check. -void add_tests_StorySavePolicy(UnitTestDatabase& database); // Registers every shared AutoStory suite, for the unit test runner. inline void add_tests_AutoStory(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp index af4b83a44b..644c138f22 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp @@ -54,7 +54,7 @@ std::string tiles_text(const std::vector& tiles){ return text; } -void check_map(std::vector& out, const GridPose& end, const ObservedState& observed){ +void check_map(const GridPose& end, const ObservedState& observed, std::vector& out){ if (!observed.map){ out.push_back(not_read("map")); return; @@ -67,7 +67,7 @@ void check_map(std::vector& out, const GridPose& end, const } // The tile and facing mean nothing on another map: not checked then. -void check_tile(std::vector& out, const GridPose& end, const ObservedState& observed, bool map_failed){ +void check_tile(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector& out){ if (end.tiles.empty()){ return; } @@ -86,7 +86,7 @@ void check_tile(std::vector& out, const GridPose& end, cons )); } -void check_facing(std::vector& out, const GridPose& end, const ObservedState& observed, bool map_failed){ +void check_facing(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector& out){ if (!end.facing){ return; } @@ -106,20 +106,20 @@ void check_facing(std::vector& out, const GridPose& end, co )); } -void check_position(std::vector& out, const StoryCheckpoint& checkpoint, const ObservedState& observed){ +void check_position(const StoryCheckpoint& checkpoint, const ObservedState& observed, std::vector& out){ const std::optional end = checkpoint_end_pose(checkpoint); if (!end){ return; } - check_map(out, *end, observed); + check_map(*end, observed, out); const bool map_failed = out.back().verdict == PostconditionVerdict::FAILED; - check_tile(out, *end, observed, map_failed); - check_facing(out, *end, observed, map_failed); + check_tile(*end, observed, map_failed, out); + check_facing(*end, observed, map_failed, out); } -void check_items(std::vector& out, const StoryPostconditions& expected, const ObservedState& observed){ +void check_items(const StoryPostconditions& expected, const ObservedState& observed, std::vector& out){ for (const StoryItem& item : expected.items){ - const std::string what = "item " + item.item; + const std::string what = std::format("item {}", item.item); const auto read = observed.items.find(item.item); if (read == observed.items.end()){ out.push_back(not_read(what)); @@ -133,9 +133,9 @@ void check_items(std::vector& out, const StoryPostcondition } } -void check_flags(std::vector& out, const StoryPostconditions& expected, const ObservedState& observed){ +void check_flags(const StoryPostconditions& expected, const ObservedState& observed, std::vector& out){ for (const std::string& flag : expected.flags){ - const std::string what = "flag " + flag; + const std::string what = std::format("flag {}", flag); const auto read = observed.flags.find(flag); if (read == observed.flags.end()){ out.push_back(not_read(what)); @@ -195,9 +195,9 @@ std::optional checkpoint_end_pose(const StoryCheckpoint& checkpoint){ PostconditionReport check_postconditions(const StoryCheckpoint& checkpoint, const ObservedState& observed){ PostconditionReport report; report.checkpoint = checkpoint.id; - check_position(report.results, checkpoint, observed); - check_items(report.results, checkpoint.postconditions, observed); - check_flags(report.results, checkpoint.postconditions, observed); + check_position(checkpoint, observed, report.results); + check_items(checkpoint.postconditions, observed, report.results); + check_flags(checkpoint.postconditions, observed, report.results); return report; } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp index b587854125..093a1ef6e3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -20,8 +21,8 @@ #include "Common/Cpp/TestRunners/UnitTest.h" #include "Data/Pokemon_StoryStepJson.h" #include "Pokemon_StoryPostconditionCheck.h" +#include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StorySegments.h" -#include "Pokemon_AutoStory_Tests.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -56,9 +57,11 @@ constexpr SaveDecision SAVE_DECISIONS[] = { UnitTestResult test_decision_table(){ for (const SaveDecision& row : SAVE_DECISIONS){ if (story_save_allowed(row.mode, row.verdict, row.point) != row.saves){ - return "Wrong decision for " + story_save_mode_name(row.mode) - + (row.verdict == CheckpointVerdict::PASSED ? ", passed" : ", failed") - + (row.point == SavePoint::RUN_END ? ", run end" : ", mid-run"); + return std::format( + "Wrong decision for {}{}{}", story_save_mode_name(row.mode), + row.verdict == CheckpointVerdict::PASSED ? ", passed" : ", failed", + row.point == SavePoint::RUN_END ? ", run end" : ", mid-run" + ); } } return true; @@ -87,8 +90,8 @@ struct Lab{ }; hooks.log = [this](const std::string& line){ lines.push_back(line); }; if (postconditions){ - hooks.check_postconditions = [this](RunState&, const std::string& id){ - trace.push_back("check " + id); + hooks.check_postconditions = [this](const std::string& id, RunState&){ + trace.push_back(std::format("check {}", id)); return *postconditions; }; } @@ -100,13 +103,13 @@ struct Lab{ std::string all() const{ std::string text; for (const std::string& line : lines){ - text += "\n" + line; + text += std::format("\n{}", line); } return text; } }; -CheckpointSpec walk_row(Lab& lab, std::string id){ +CheckpointSpec walk_row(std::string id, Lab& lab){ CheckpointSpec spec; spec.id = id; spec.saves = true; @@ -137,12 +140,12 @@ 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(lab, std::string(id))); + segment.checkpoints.push_back(walk_row(std::string(id), lab)); } return segment; } -SegmentReport run_in(Lab& lab, StorySaveMode mode){ +SegmentReport run_in(StorySaveMode mode, Lab& lab){ Segment segment = three_rows(lab); SegmentHooks hooks = lab.hooks(); RunState state; @@ -151,36 +154,36 @@ SegmentReport run_in(Lab& lab, StorySaveMode mode){ UnitTestResult test_none_never_saves(){ Lab lab; - const SegmentReport report = run_in(lab, StorySaveMode::NONE); + 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 "Expected three checkpoints and no save:" + lab.all(); + 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 "Expected the skipped saves logged:" + lab.all(); + 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(lab, StorySaveMode::PER_CHECKPOINT); + 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 "Expected a save after each checkpoint:" + lab.all(); + 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(lab, StorySaveMode::AFTER_VERIFICATION); + 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 "Expected one save, at the end:" + lab.all(); + 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 "Expected the missing check logged as not checked:" + lab.all(); + return std::format("Expected the missing check logged as not checked:{}", lab.all()); } return true; } @@ -188,10 +191,10 @@ UnitTestResult test_after_verification_saves_at_the_end(){ UnitTestResult test_after_verification_checks_before_saving(){ Lab lab; lab.postconditions = outcome_ok(); - const SegmentReport report = run_in(lab, StorySaveMode::AFTER_VERIFICATION); + 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 "Expected the check, then the save:" + lab.all(); + return std::format("Expected the check, then the save:{}", lab.all()); } return true; } @@ -200,15 +203,15 @@ UnitTestResult test_after_verification_checks_before_saving(){ 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(lab, StorySaveMode::AFTER_VERIFICATION); + const SegmentReport report = run_in(StorySaveMode::AFTER_VERIFICATION, lab); if (report.completed || report.failed_checkpoint != "00.03"){ - return "Expected the run failed at its last checkpoint:" + lab.all(); + 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 "A run whose postconditions failed must not save:" + lab.all(); + return std::format("A run whose postconditions failed must not save:{}", lab.all()); } if (!lab.has("[Checkpoint] 00.03: postconditions failed, not saving")){ - return "Expected the refused save logged:" + lab.all(); + return std::format("Expected the refused save logged:{}", lab.all()); } return true; } @@ -217,13 +220,13 @@ UnitTestResult test_failed_postconditions_never_save(){ UnitTestResult test_no_reload_after_the_first(){ Lab lab; lab.failures["00.02"] = 1; - const SegmentReport report = run_in(lab, StorySaveMode::NONE); + 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 "Expected 00.02 to fail without a reload:" + lab.all(); + 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 "Expected why it didn't reload:" + lab.all(); + return std::format("Expected why it didn't reload:{}", lab.all()); } return true; } @@ -231,15 +234,15 @@ UnitTestResult test_no_reload_after_the_first(){ UnitTestResult test_first_row_still_reloads(){ Lab lab; lab.failures["00.01"] = 1; - const SegmentReport report = run_in(lab, StorySaveMode::NONE); + 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 "Expected the first checkpoint retried from the start save:" + lab.all(); + return std::format("Expected the first checkpoint retried from the start save:{}", lab.all()); } return true; } -VerifyReport verify_in(Lab& lab, StorySaveMode mode){ +VerifyReport verify_in(StorySaveMode mode, Lab& lab){ Segment segment = three_rows(lab); SegmentHooks hooks = lab.hooks(); RunState state; @@ -249,13 +252,13 @@ VerifyReport verify_in(Lab& lab, StorySaveMode mode){ UnitTestResult test_verify_saves_once_every_run_passed(){ Lab lab; - const VerifyReport report = verify_in(lab, StorySaveMode::AFTER_VERIFICATION); + 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 "Expected one save, after the last run:" + lab.all(); + 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 "Expected the save logged:" + lab.all(); + return std::format("Expected the save logged:{}", lab.all()); } return true; } @@ -263,12 +266,12 @@ UnitTestResult test_verify_saves_once_every_run_passed(){ UnitTestResult test_verify_no_save_after_a_failed_run(){ Lab lab; lab.failures["00.02"] = 1; - const VerifyReport report = verify_in(lab, StorySaveMode::AFTER_VERIFICATION); + 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 "Verified 2/3, but a failed run means no save:" + lab.all(); + 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 "Expected why it didn't save:" + lab.all(); + return std::format("Expected why it didn't save:{}", lab.all()); } return true; } @@ -313,7 +316,7 @@ UnitTestResult test_postconditions_json(){ } const StoryCheckpoint again = read_story_checkpoint(story_checkpoint_json(checkpoint), "again"); if (again.postconditions != post){ - return "The postconditions should survive a write and read: " + story_checkpoint_json(checkpoint).dump(); + return std::format("The postconditions should survive a write and read: {}", story_checkpoint_json(checkpoint).dump()); } return true; } @@ -322,7 +325,7 @@ UnitTestResult test_postconditions_pass(){ const PostconditionReport report = check_postconditions(checkpoint_0031(), read_0031("RocketHideout_B3F", 2)); const std::vector lines = postcondition_log_lines(report); if (!report.all_passed() || lines.back() != "[Postcondition] 00.31: 5 passed, 0 failed, 0 not checked: passed"){ - return "Expected every postcondition passed: " + lines.back(); + return std::format("Expected every postcondition passed: {}", lines.back()); } return true; } @@ -342,9 +345,9 @@ UnitTestResult test_postconditions_fail(){ if (lines != expected){ std::string text; for (const std::string& line : lines){ - text += "\n" + line; + text += std::format("\n{}", line); } - return "Unexpected lines:" + text; + return std::format("Unexpected lines:{}", text); } return true; } @@ -359,7 +362,7 @@ UnitTestResult test_postconditions_not_checked(){ if (report.all_passed() || report.count(PostconditionVerdict::NOT_CHECKED) != 5 || lines.back() != "[Postcondition] 00.31: 1 passed, 0 failed, 5 not checked: incomplete" ){ - return "What isn't read is not checked, never passed: " + lines.back(); + return std::format("What isn't read is not checked, never passed: {}", lines.back()); } observed.flags["hideout_lift_key_grunt"] = false; if (check_postconditions(checkpoint, observed).count(PostconditionVerdict::FAILED) != 1){ @@ -409,7 +412,7 @@ struct SavePolicyCase{ class Test_StorySavePolicy : public UnitTest{ public: explicit Test_StorySavePolicy(SavePolicyCase test_case) - : UnitTest("Pokemon::StorySavePolicy - " + test_case.input.name) + : UnitTest(std::format("Pokemon::StorySavePolicy - {}", test_case.input.name)) , m_case(std::move(test_case)) {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.h new file mode 100644 index 0000000000..4e95d5e81a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.h @@ -0,0 +1,23 @@ +/* Story Save Policy Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StorySavePolicy_Tests_H +#define PokemonAutomation_Pokemon_StorySavePolicy_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for when a run saves (save modes) and the postcondition check. +void add_tests_StorySavePolicy(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 index ccaf449d6c..47fb9df2a6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp @@ -119,7 +119,7 @@ Act checked_save(Act save, std::string id, SegmentHooks& hooks){ say(hooks, std::format("[Postcondition] {}: not checked: the game has no postcondition check", id)); return save(state); } - const Outcome checked = hooks.check_postconditions(state, id); + const Outcome checked = hooks.check_postconditions(id, state); if (!checked.succeeded()){ say(hooks, std::format("[Checkpoint] {}: postconditions failed, not saving", id)); return outcome_failed("postconditions_failed", to_string(checked)); @@ -140,7 +140,7 @@ struct Row{ // 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(Checkpoint& checkpoint, const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks){ +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){ @@ -183,7 +183,7 @@ CheckpointReport run_row( for (size_t attempt = 1; ; attempt++){ run.attempts = attempt; Checkpoint checkpoint = row.spec.build(); - set_save(checkpoint, row, policy, hooks); + set_save(row, policy, hooks, checkpoint); EventLogger log = [&hooks](const std::string& line){ say(hooks, line); }; CheckpointReport result = run_checkpoint(state, checkpoint, log); if (result.completed){ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h index b7b43c633c..2f146fdd52 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h @@ -61,7 +61,7 @@ struct SegmentHooks{ // (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; + MoveOnlyFunction check_postconditions; }; // Runs the checkpoints in `range`, in order, saving as `policy.save_mode` diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 4b67d3a69f..9cbc7edb2c 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1533,6 +1533,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.cpp Source/Pokemon/AutoStory/Pokemon_StoryPostconditionCheck.h Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.h Source/Pokemon/AutoStory/Pokemon_StorySegments.cpp Source/Pokemon/AutoStory/Pokemon_StorySegments.h Source/Pokemon/AutoStory/Pokemon_StorySegments_Tests.cpp