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 cc6c0f6d35..6680dae72f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -12,13 +12,14 @@ #define PokemonAutomation_Pokemon_AutoStory_Tests_H #include "Common/Cpp/TestRunners/UnitTestDatabase.h" -#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" -#include "Pokemon_AutoStorySelect_Tests.h" #include "Data/Pokemon_AuthoredCheckpointJson_Tests.h" #include "Data/Pokemon_StoryStepJson_Tests.h" +#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" +#include "Pokemon_AutoStorySelect_Tests.h" #include "Pokemon_AutoStoryStats_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryLog_Tests.h" +#include "Pokemon_StorySavePolicy_Tests.h" #include "Pokemon_StorySegments_Tests.h" #include "Pokemon_StoryStepExecutor_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" @@ -39,6 +40,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..644c138f22 --- /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(const GridPose& end, const ObservedState& observed, std::vector& out){ + 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(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector& out){ + 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(const GridPose& end, const ObservedState& observed, bool map_failed, std::vector& out){ + 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(const StoryCheckpoint& checkpoint, const ObservedState& observed, std::vector& out){ + const std::optional end = checkpoint_end_pose(checkpoint); + if (!end){ + return; + } + check_map(*end, observed, out); + const bool map_failed = out.back().verdict == PostconditionVerdict::FAILED; + check_tile(*end, observed, map_failed, out); + check_facing(*end, observed, map_failed, out); +} + +void check_items(const StoryPostconditions& expected, const ObservedState& observed, std::vector& out){ + for (const StoryItem& item : expected.items){ + 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)); + 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(const StoryPostconditions& expected, const ObservedState& observed, std::vector& out){ + for (const std::string& flag : expected.flags){ + 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)); + 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(checkpoint, observed, report.results); + check_items(checkpoint.postconditions, observed, report.results); + check_flags(checkpoint.postconditions, observed, report.results); + 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..2e1ecac7f9 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySavePolicy_Tests.cpp @@ -0,0 +1,501 @@ +/* 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 +#include "Common/Cpp/Json/JsonValue.h" +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Data/Pokemon_StoryStepJson.h" +#include "Pokemon_StoryPostconditionCheck.h" +#include "Pokemon_StorySavePolicy_Tests.h" +#include "Pokemon_StorySegments.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 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; +} + + +// 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; +} + +// 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(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; +} + + +// 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 std::format("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 std::format("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 += std::format("\n{}", line); + } + return std::format("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 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){ + 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(std::format("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_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 c6cd802e00..c85971adf9 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(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(std::format("[Checkpoint] {}: postconditions failed, not saving", id), 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(std::format("[Checkpoint] {}: not saving (save mode {})", id, 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, 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 SegmentPolicy& policy, CheckpointSpec& spec, + const Segment& segment, const Row& row, const SegmentPolicy& policy, SegmentHooks& hooks, SegmentReport& report, CheckpointRun& run, RunState& state ){ + 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(row, policy, hooks, checkpoint); EventLogger log = [&hooks](const std::string& line){ say(line, hooks); }; CheckpointReport result = run_checkpoint(state, checkpoint, log); if (result.completed){ run.passed = true; - say(checkpoint_pass_line(spec.id, attempt), hooks); + say(checkpoint_pass_line(row.spec.id, attempt), hooks); return result; } if (result.failure.failure == GAME_NOT_RUNNING){ - say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, spec.id), hooks); + say(std::format("[Segment] {}: {} failed; the game is not running, so no reload", segment.id, row.spec.id), hooks); return result; } - if (attempt >= policy.attempts_per_checkpoint){ + if (attempt >= attempts){ + if (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 result; } - say(segment_reload_line(segment.id, spec.id, attempt + 1, policy.attempts_per_checkpoint), hooks); + say(segment_reload_line(segment.id, row.spec.id, attempt + 1, policy.attempts_per_checkpoint), hooks); report.reloads++; Outcome reloaded = reload(state, hooks); if (!reloaded.succeeded()){ @@ -167,11 +239,15 @@ SegmentReport run_segment( 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}); - CheckpointReport result = run_row(segment, policy, spec, hooks, report, report.runs.back(), state); + 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; @@ -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( - size_t run, const VerifyPolicy& policy, CheckpointSpec& spec, SegmentHooks& hooks, RunState& state + size_t run, const VerifyPolicy& policy, bool save_at_end, + CheckpointSpec& spec, SegmentHooks& hooks, RunState& state ){ 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(line, hooks); }; 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(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: {}", @@ -252,7 +347,10 @@ VerifyReport verify_checkpoint( } } report.runs++; - report.passes += verify_run(run, policy, spec, hooks, state) ? 1 : 0; + 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); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StorySegments.h index 5601ddeac9..ddb4325878 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 e262415cf2..f6113a6466 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_AutoStoryStats.h" #include "Pokemon/AutoStory/Pokemon_StoryEvents.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 d7d77747ac..5834a024a4 100644 --- a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp @@ -23,6 +23,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 : std::format("{} {}", checkpoint.id, checkpoint.title); } @@ -86,11 +96,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{ @@ -101,12 +127,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 e05d6bace4..4b2e5bd9df 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1523,6 +1523,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryLog_Tests.h 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_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 @@ -1543,6 +1547,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