diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h index 294dd5d21d..e711f8b1ab 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h @@ -8,6 +8,7 @@ #define PokemonAutomation_Pokemon_AutoStorySelect_H #include +#include #include #include #include "CommonTools/Options/StringSelectOption.h" @@ -40,6 +41,22 @@ StringSelectDatabase make_story_select_database( } +// The start/end dropdown database of a story's checkpoints, given by their +// labels in order ("00.09 Pewter Gym: Camper Liam, then Brock"), slugged +// by their index, as make_story_select_database() slugs SV's. A game whose +// story is in sections (BDSP's segments 00 and 01) passes them joined: a +// section's entry i is then story_index(true, i, first of the section). +// The program keeps it in a function-local static: the options hold a +// reference. +inline StringSelectDatabase story_checkpoint_database(std::span labels){ + StringSelectDatabase database; + for (size_t c = 0; c < labels.size(); c++){ + database.add_entry(StringSelectEntry(std::to_string(c), labels[c])); + } + return database; +} + + // The index in the whole story list of entry `index_in_section` of the // tutorial or the main story, whose first entry is `first_main_story_index`. // NOTE: a section ends where the user must step in (e.g. level up the diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp index efdd49bd8f..c9eae2f1ae 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp @@ -4,6 +4,7 @@ * */ +#include #include #include #include @@ -103,6 +104,68 @@ class Test_AutoStorySelectDatabase : public UnitTest{ const AutoStorySelectScenario m_scenario; }; +// (slug, label) entries as [0 "label", ...]. +std::string describe_entries(const std::vector>& entries){ + std::string text; + for (const auto& [slug, label] : entries){ + text += std::format("{}{} \"{}\"", text.empty() ? "" : ", ", slug, label); + } + return std::format("[{}]", text); +} + +// A game's checkpoint labels, in story order. +struct StoryCheckpointDatabaseInput{ + std::vector labels; +}; + +// The (slug, label) entries: one per label, in order, slugged by its index. +struct StoryCheckpointDatabaseExpectation{ + std::vector> entries; +}; + +// story_checkpoint_database() over a game's labels and the entries it must make. +struct StoryCheckpointDatabaseCase{ + std::string name; + StoryCheckpointDatabaseInput input; + StoryCheckpointDatabaseExpectation expectation; +}; + +class Test_StoryCheckpointDatabase : public UnitTest{ +public: + Test_StoryCheckpointDatabase(StoryCheckpointDatabaseCase test_case) + : UnitTest(std::format("Pokemon::AutoStorySelectDatabase - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const StringSelectDatabase database = story_checkpoint_database(m_case.input.labels); + std::vector> got; + for (const StringSelectEntry& entry : database.case_list()){ + got.emplace_back(entry.slug, entry.display_name); + } + if (got != m_case.expectation.entries){ + return std::format("Expected {}, got {}", describe_entries(m_case.expectation.entries), describe_entries(got)); + } + return true; + } + +private: + const StoryCheckpointDatabaseCase m_case; +}; + +const StoryCheckpointDatabaseCase STORY_CHECKPOINT_DATABASE_CASES[] = { + { + .name = "checkpoint labels slugged by index", + .input = {.labels = {"00.01 Pallet Town", "00.02 Oak's lab"}}, + .expectation = {.entries = {{"0", "00.01 Pallet Town"}, {"1", "00.02 Oak's lab"}}}, + }, + { + .name = "no checkpoints, no entries", + .input = {.labels = {}}, + .expectation = {.entries = {}}, + }, +}; + void add_index_tests(UnitTestDatabase& database){ const AutoStoryIndexScenario scenarios[] = { { @@ -158,6 +221,9 @@ void add_select_database_tests(UnitTestDatabase& database){ for (const AutoStorySelectScenario& scenario : select_scenarios){ database.add(scenario); } + for (const StoryCheckpointDatabaseCase& test_case : STORY_CHECKPOINT_DATABASE_CASES){ + database.add(test_case); + } } } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 6fcaf02f34..c12e91bc93 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -19,6 +19,8 @@ #include "Engine/Pokemon_StoryEnginePolicy_Tests.h" #include "Engine/Pokemon_StoryEngineRuns_Tests.h" #include "Engine/Pokemon_StoryEngineSaveRetry_Tests.h" +#include "Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions_Tests.h" #include "Pokemon_SceneJudges_Tests.h" #include "Pokemon_StoryEvents_Tests.h" #include "Pokemon_StoryFailureClasses_Tests.h" @@ -48,6 +50,8 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StorySavePolicy(database); add_tests_SceneJudges(database); add_tests_StoryFiles(database); + add_tests_StoryRangeOptions(database); + add_tests_StoryRunOptions(database); add_tests_StoryRunTypes(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp index 2b380aac8a..99ce9251d3 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryStepExecutor_Tests.cpp @@ -20,6 +20,8 @@ #include #include #include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/Options/Pokemon_StoryRunOptions.h" +#include "Pokemon_AutoStorySelect.h" #include "Pokemon_AutoStoryStats.h" #include "Pokemon_StoryEvents.h" #include "Pokemon_BuiltinStoryActions.h" @@ -353,6 +355,60 @@ UnitTestResult test_pause_points(){ } +// Two sections of a story, as BDSP has segments 00 and 01. +const std::vector& two_sections(){ + static const std::vector rows{ + StoryCheckpoint{.id = "00.01", .title = "Leave the house", .start = std::nullopt, .steps = {}}, + StoryCheckpoint{.id = "00.02", .title = "Walk to Sandgem Town", .start = std::nullopt, .steps = {}}, + StoryCheckpoint{.id = "01.01", .title = "Jubilife to Barry", .start = std::nullopt, .steps = {}}, + }; + return rows; +} + +const StringSelectDatabase& two_sections_database(){ + static const StringSelectDatabase database = []{ + std::vector labels; + for (const StoryCheckpoint& checkpoint : two_sections()){ + labels.push_back(story_checkpoint_label(checkpoint)); + } + return story_checkpoint_database(labels); + }(); + return database; +} + +// The dropdowns list the checkpoints as "id title", slugged by their index +// in the whole story, which is story_index() of a section's own index. +UnitTestResult test_checkpoint_dropdowns(){ + const StringSelectDatabase& database = two_sections_database(); + const std::vector expected{"00.01 Leave the house", "00.02 Walk to Sandgem Town", "01.01 Jubilife to Barry"}; + if (database.size() != expected.size()){ + return std::format("Expected 3 entries, got {}", database.size()); + } + for (size_t c = 0; c < expected.size(); c++){ + if (database[c].display_name != expected[c] || database[c].slug != std::to_string(c)){ + return std::format("Unexpected entry {}: {} {}", c, database[c].slug, database[c].display_name); + } + } + if (database[story_index(true, 0, 2)].display_name != "01.01 Jubilife to Barry"){ + return "Expected the second section's first entry at story_index(true, 0, 2)."; + } + if (story_checkpoint_label(StoryCheckpoint{.id = "00.09", .title = "Pewter Gym: Camper Liam, then Brock", .start = std::nullopt, .steps = {}}) + != "00.09 Pewter Gym: Camper Liam, then Brock" + ){ + return "Unexpected label."; + } + StoryRunOptions options(database); + if (options.start_index() != 0 || options.end_index() != 2 || !options.check_validity().empty()){ + return "Expected the whole story by default."; + } + options.START_CHECKPOINT.set_by_index(2); + options.END_CHECKPOINT.set_by_index(1); + if (options.check_validity().empty()){ + return "Expected a start after the end to be refused."; + } + return true; +} + // A segment's report in the shared AutoStory stats. UnitTestResult test_story_stats(){ SegmentReport report; @@ -370,6 +426,20 @@ UnitTestResult test_story_stats(){ return true; } +// The program's option group: 3 attempts, no stop, Thorough, by default; +// Fast when chosen. +UnitTestResult test_run_options(){ + StoryRunOptions options(two_sections_database()); + if (options.ATTEMPTS_PER_CHECKPOINT != 3 || options.check_level() != StoryCheckLevel::DEFAULT){ + return "Expected 3 attempts, no stop at the first failure, and Thorough checks by default."; + } + options.CHECKS.set(StoryCheckLevel::FAST); + if (options.check_level() != StoryCheckLevel::FAST){ + return "Expected Fast checks once chosen."; + } + return true; +} + UnitTestResult test_lint(){ StoryStep no_target = make(StoryStepKind::ROUTE, "walk"); StoryStep again = make(StoryStepKind::TALK, "walk"); @@ -424,6 +494,8 @@ const StoryStepTestCase STORY_STEP_CASES[] = { {.name = "unsupported kinds and lost battles", .test = test_unsupported_and_lost_battles}, {.name = "pause points pause before each step", .test = test_pause_points}, {.name = "lint", .test = test_lint}, + {.name = "the run options' defaults", .test = test_run_options}, + {.name = "start and end checkpoint dropdowns", .test = test_checkpoint_dropdowns}, {.name = "a segment's run in the AutoStory stats", .test = test_story_stats}, }; diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp new file mode 100644 index 0000000000..aade2b3809 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.cpp @@ -0,0 +1,32 @@ +/* Story Checks + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryCheckLevel.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_check_level_name(StoryCheckLevel level){ + switch (level){ + case StoryCheckLevel::TESTING: + return "testing"; + case StoryCheckLevel::THOROUGH: + return "thorough"; + case StoryCheckLevel::DEFAULT: + return "default"; + case StoryCheckLevel::FAST: + return "fast"; + case StoryCheckLevel::SPEEDRUN: + return "speedrun"; + } + return "?"; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h new file mode 100644 index 0000000000..86de9f8c76 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h @@ -0,0 +1,43 @@ +/* Story Check Level + * + * From: https://github.com/PokemonAutomation/ + * + * How much a story step reads the screen to confirm what it did. The + * program's options choose it (StoryRunOptions); each game decides what + * its steps check at each level. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckLevel_H +#define PokemonAutomation_Pokemon_StoryCheckLevel_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// How much a step reads the screen to confirm what it did. +enum class StoryCheckLevel{ + // Everything THOROUGH checks, each checkpoint run 3 times by default to + // make sure it really passes. + TESTING, + // Check everything for the user: Bag items, Pokemon stats, + // preconditions. + THOROUGH, + // Assume most things are fine; check the most important ones. + DEFAULT, + // Minimal checks. + FAST, + // Almost no checks, to be as fast as possible. + SPEEDRUN, +}; + +// Returns the readable string representation of a story check level. +std::string story_check_level_name(StoryCheckLevel level); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.cpp new file mode 100644 index 0000000000..d294e5e386 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.cpp @@ -0,0 +1,51 @@ +/* Story Range Options + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_StoryRangeOptions.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +StoryRangeOptions::StoryRangeOptions(std::string label, const StringSelectDatabase& checkpoints, LockMode end_lock) + : GroupOption(std::move(label), LockMode::LOCK_WHILE_RUNNING) + , START_CHECKPOINT( + "Start checkpoint:
The program starts from this checkpoint's save.", + checkpoints, + LockMode::LOCK_WHILE_RUNNING, + 0 + ) + , END_CHECKPOINT( + "End checkpoint:
The program stops after this checkpoint.", + checkpoints, + end_lock, + checkpoints.size() == 0 ? 0 : checkpoints.size() - 1 + ) +{ + PA_ADD_OPTION(START_CHECKPOINT); + PA_ADD_OPTION(END_CHECKPOINT); +} + +size_t StoryRangeOptions::start_index() const{ + return START_CHECKPOINT.index(); +} + +size_t StoryRangeOptions::end_index() const{ + return END_CHECKPOINT.index(); +} + +std::string StoryRangeOptions::check_validity() const{ + if (start_index() > end_index()){ + return "The start checkpoint comes after the end checkpoint."; + } + return GroupOption::check_validity(); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.h new file mode 100644 index 0000000000..809f3324d2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions.h @@ -0,0 +1,53 @@ +/* Story Range Options + * + * From: https://github.com/PokemonAutomation/ + * + * The start and end checkpoint dropdowns of an AutoStory program, over one + * list of the story's checkpoints, and the check that the start does not + * come after the end. StoryRunOptions (Pokemon_StoryRunOptions.h) is this + * group plus the run policy. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRangeOptions_H +#define PokemonAutomation_Pokemon_StoryRangeOptions_H + +#include +#include "Common/Cpp/Options/GroupOption.h" +#include "CommonTools/Options/StringSelectOption.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A program shows the group, or adds START_CHECKPOINT and END_CHECKPOINT +// to its own options under its own keys (keeping the keys its saved +// settings already use) and calls check_validity() from its own. +class StoryRangeOptions : public GroupOption{ +public: + // `checkpoints`: the dropdown database (make_story_select_database() + // or story_checkpoint_database()), kept alive by the caller. The + // dropdowns start on its first and last entries. `end_lock`: + // UNLOCK_WHILE_RUNNING for a program that reads the end after each + // checkpoint, so the end can be moved during a run. + StoryRangeOptions( + std::string label, const StringSelectDatabase& checkpoints, + LockMode end_lock = LockMode::LOCK_WHILE_RUNNING + ); + + // The checkpoints chosen, by index in the database, both inclusive. + size_t start_index() const; + size_t end_index() const; + // "The start checkpoint comes after the end checkpoint." if it does. + virtual std::string check_validity() const override; + +public: + StringSelectOption START_CHECKPOINT; + StringSelectOption END_CHECKPOINT; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp new file mode 100644 index 0000000000..fce811e4c6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp @@ -0,0 +1,142 @@ +/* Story Range Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon_StoryRangeOptions.h" +#include "Pokemon_StoryRangeOptions_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// A stand-in for a story checkpoint: only its name is read. +struct NamedCheckpoint{ + std::string label; + std::string name() const{ + return label; + } +}; + +// Two tutorial checkpoints, then three of the main story, as SV's list is +// laid out. +const std::vector>& story_list(){ + static const std::vector> list = []{ + std::vector> ret; + for (std::string_view label : {"tutorial 0", "tutorial 1", "main 0", "main 1", "main 2"}){ + ret.emplace_back(std::make_unique(NamedCheckpoint{.label = std::string(label)})); + } + return ret; + }(); + return list; +} + +// The main story's dropdowns: entries 2..4 of the list, slugged 2..4. +const StringSelectDatabase& main_story_database(){ + static const StringSelectDatabase database = make_story_select_database(story_list(), 2, story_list().size()); + return database; +} + +// A range chosen in the main story's dropdowns and what the group reads from it. +struct StoryRangeCase{ + // The start and end set by slug, as saved settings load them; empty + // leaves the default. + struct Input{ + std::string start_slug; + std::string end_slug; + }; + + // The indexes in the dropdown, and whether the range is valid. + struct Expectation{ + size_t start; + size_t end; + bool valid; + }; + + std::string name; + Input input; + Expectation expectation; +}; + +class Test_StoryRange : public UnitTest{ +public: + Test_StoryRange(StoryRangeCase test_case) + : UnitTest(std::format("Pokemon::StoryRangeOptions - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + StoryRangeOptions range("Checkpoints", main_story_database()); + if (!m_case.input.start_slug.empty()){ + range.START_CHECKPOINT.set_by_slug(m_case.input.start_slug); + } + if (!m_case.input.end_slug.empty()){ + range.END_CHECKPOINT.set_by_slug(m_case.input.end_slug); + } + const StoryRangeCase::Expectation& expected = m_case.expectation; + if (range.start_index() != expected.start || range.end_index() != expected.end){ + return std::format( + "Expected indexes {} to {}, got {} to {}", + expected.start, expected.end, range.start_index(), range.end_index() + ); + } + const std::string error = range.check_validity(); + if (error.empty() != expected.valid){ + return expected.valid ? std::format("Expected a valid range, got: {}", error) : "Expected the range refused."; + } + if (!expected.valid && error != "The start checkpoint comes after the end checkpoint."){ + return std::format("Unexpected message: {}", error); + } + return true; + } + +private: + const StoryRangeCase m_case; +}; + +const StoryRangeCase STORY_RANGE_CASES[] = { + { + .name = "the whole database by default", + .input = {.start_slug = "", .end_slug = ""}, + .expectation = {.start = 0, .end = 2, .valid = true}, + }, + { + .name = "slugs are whole-list indexes, indexes are the dropdown's", + .input = {.start_slug = "3", .end_slug = "4"}, + .expectation = {.start = 1, .end = 2, .valid = true}, + }, + { + .name = "one checkpoint", + .input = {.start_slug = "3", .end_slug = "3"}, + .expectation = {.start = 1, .end = 1, .valid = true}, + }, + { + .name = "a start after the end is refused", + .input = {.start_slug = "4", .end_slug = "2"}, + .expectation = {.start = 2, .end = 0, .valid = false}, + }, +}; + +} // namespace + + +void add_tests_StoryRangeOptions(UnitTestDatabase& database){ + for (const StoryRangeCase& test_case : STORY_RANGE_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h new file mode 100644 index 0000000000..1d3e48e19d --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h @@ -0,0 +1,23 @@ +/* Story Range Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRangeOptions_Tests_H +#define PokemonAutomation_Pokemon_StoryRangeOptions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared start/end checkpoint range options. +void add_tests_StoryRangeOptions(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp new file mode 100644 index 0000000000..41f69690ce --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp @@ -0,0 +1,74 @@ +/* Story Run Options + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_StoryRunOptions.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + +const EnumDropdownDatabase& StoryCheckLevel_Database(){ + static const EnumDropdownDatabase database({ + {StoryCheckLevel::TESTING, "testing", "Testing: check everything, as Thorough does, while a checkpoint is new"}, + {StoryCheckLevel::THOROUGH, "thorough", "Thorough: check everything for the user (Bag items, Pok\u00e9mon stats, preconditions)"}, + {StoryCheckLevel::DEFAULT, "default", "Default: check the most important things, assume the rest is fine"}, + {StoryCheckLevel::FAST, "fast", "Fast: minimal checks"}, + {StoryCheckLevel::SPEEDRUN, "speedrun", "Speedrun: almost no checks, as fast as possible"}, + }); + return database; +} + + +constexpr std::string_view RELOAD_AT_START_LABEL = + "Reload the Save at the Start:
Close the game and load the save before the first checkpoint. " + "Off: start from wherever the game is (resumed from HOME if need be), for a game already at the start checkpoint. " + "Later checkpoints reload as usual."; + +} // namespace + + +std::string story_checkpoint_label(const StoryCheckpoint& checkpoint){ + return checkpoint.title.empty() ? checkpoint.id : std::format("{} {}", checkpoint.id, checkpoint.title); +} + + + +StoryRunOptions::StoryRunOptions(const StringSelectDatabase& checkpoints) + : StoryRangeOptions("Story Run", checkpoints) + , ATTEMPTS_PER_CHECKPOINT( + "Attempts per checkpoint:
A failed checkpoint reloads its save and runs again, up to this many times in all. " + "1 stops at the first failure, leaving the game where it failed.", + LockMode::LOCK_WHILE_RUNNING, + 3, 1, 20 + ) + , CHECKS( + "Checks:
How much each step reads the screen to confirm what it did, from Testing (the most) to Speedrun (almost none).", + StoryCheckLevel_Database(), + LockMode::LOCK_WHILE_RUNNING, + StoryCheckLevel::DEFAULT + ) + + , RELOAD_AT_START(std::string(RELOAD_AT_START_LABEL), LockMode::LOCK_WHILE_RUNNING, true) +{ + PA_ADD_OPTION(ATTEMPTS_PER_CHECKPOINT); + PA_ADD_OPTION(CHECKS); + + PA_ADD_OPTION(RELOAD_AT_START); +} + +StoryCheckLevel StoryRunOptions::check_level() const{ + return CHECKS; +} + + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h new file mode 100644 index 0000000000..61800fa8a6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions.h @@ -0,0 +1,64 @@ +/* Story Run Options + * + * From: https://github.com/PokemonAutomation/ + * + * The options every AutoStory program shows for where to start and end, how + * hard to try and how much to check: the shared range group + * (Pokemon_StoryRangeOptions.h) plus the attempts per checkpoint, the + * checks level (Types/Pokemon_StoryCheckLevel.h) and whether it reloads + * the save before its first checkpoint. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRunOptions_H +#define PokemonAutomation_Pokemon_StoryRunOptions_H + +#include +#include +#include "Common/Cpp/Options/BooleanCheckBoxOption.h" +#include "Common/Cpp/Options/EnumDropdownOption.h" +#include "Common/Cpp/Options/SimpleIntegerOption.h" + +#include "CommonTools/Options/StringSelectOption.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckLevel.h" +#include "Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h" +#include "Pokemon_StoryRangeOptions.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A checkpoint's label in the start and end dropdowns, as SV labels its +// segments: "00.09 Pewter Gym: Camper Liam, then Brock". The labels of a +// story's checkpoints, in order, make its dropdowns +// (story_checkpoint_database()). +std::string story_checkpoint_label(const StoryCheckpoint& checkpoint); + + +// The range group (START_CHECKPOINT, END_CHECKPOINT, start_index(), +// end_index()) with the run's options added to the same group, so the +// saved settings keep one flat object. Each checkpoint that passes saves +// as its row's execution says (the game's own autosave, or the menu). +class StoryRunOptions : public StoryRangeOptions{ +public: + // `checkpoints`: story_checkpoint_database() of the game's story, kept + // alive by the caller; the dropdowns start on its first and last. + explicit StoryRunOptions(const StringSelectDatabase& checkpoints); + + // The checks level, for the game's steps to read. + StoryCheckLevel check_level() const; + +public: + SimpleIntegerOption ATTEMPTS_PER_CHECKPOINT; + EnumDropdownOption CHECKS; + // On (the default), the run loads the save before its first + // checkpoint. Off, it starts from where the game is; later checkpoints + // reload as before. + BooleanCheckBoxOption RELOAD_AT_START; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp new file mode 100644 index 0000000000..353e83a8ac --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp @@ -0,0 +1,84 @@ +/* Story Run Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon/AutoStory/Pokemon_AutoStorySelect.h" +#include "Pokemon_StoryRunOptions.h" +#include "Pokemon_StoryRunOptions_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// Two checkpoints' dropdowns. +const StringSelectDatabase& two_checkpoints(){ + static const StringSelectDatabase database = story_checkpoint_database(std::vector{"01.01", "01.02"}); + return database; +} + +// The reload at the start a user set, and whether the options let the run +// start. +struct ReloadAtStartCase{ + struct Input{ + bool reload_at_start; + }; + struct Expectation{ + // Empty: the options are valid; else the refusal's message. + std::string_view error; + }; + std::string_view name; + Input input; + Expectation expectation; +}; + +const ReloadAtStartCase RELOAD_AT_START_CASES[] = { + {.name = "reload at the start", .input = {.reload_at_start = true}, .expectation = {.error = ""}}, + {.name = "no reload at the start", .input = {.reload_at_start = false}, .expectation = {.error = ""}}, +}; + +class Test_ReloadAtStart : public UnitTest{ +public: + explicit Test_ReloadAtStart(ReloadAtStartCase test_case) + : UnitTest(std::format("Pokemon::StoryRunOptions - {}", test_case.name)) + , m_case(std::move(test_case)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + StoryRunOptions run(two_checkpoints()); + if (!run.RELOAD_AT_START){ + return "Expected the reload at the start on by default."; + } + run.RELOAD_AT_START = m_case.input.reload_at_start; + const std::string error = run.check_validity(); + if (error != m_case.expectation.error){ + return std::format("Expected \"{}\", got \"{}\".", m_case.expectation.error, error); + } + return true; + } + +private: + const ReloadAtStartCase m_case; +}; + +} // namespace + + +void add_tests_StoryRunOptions(UnitTestDatabase& database){ + for (const ReloadAtStartCase& test_case : RELOAD_AT_START_CASES){ + database.add(test_case); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h new file mode 100644 index 0000000000..1ebc703cd0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h @@ -0,0 +1,23 @@ +/* Story Run Options Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRunOptions_Tests_H +#define PokemonAutomation_Pokemon_StoryRunOptions_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for the shared run options' checks. +void add_tests_StoryRunOptions(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index a06267b228..2b11392f48 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1556,6 +1556,8 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h + 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_StoryCheckpointMetadata.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h @@ -1612,6 +1614,14 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h + Source/Pokemon/Options/Pokemon_StoryRangeOptions.cpp + Source/Pokemon/Options/Pokemon_StoryRangeOptions.h + Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp + Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.h + Source/Pokemon/Options/Pokemon_StoryRunOptions.cpp + Source/Pokemon/Options/Pokemon_StoryRunOptions.h + Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.cpp + Source/Pokemon/Options/Pokemon_StoryRunOptions_Tests.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp