Repository navigation
AutoStory: shared checkpoint range options #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: autostory/r3-shared-stats
Are you sure you want to change the base?
Changes from all commits
2871e3e
1ddd9aa
25331e3
824117c
348ed17
495c7c1
8e82c9f
c7eae8a
9ea62b9
2b966b3
a59977a
f49d02b
ae48485
3429101
2fc9ada
ec64003
036d1d3
d24b86d
35242c8
be8c044
62ec604
7ff24df
eacc561
8c2155f
002a6eb
274bbda
cc6ed14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
| * | ||
| */ | ||
|
|
||
| #include <format> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <utility> | ||
|
|
@@ -103,6 +104,66 @@ | |
| const AutoStorySelectScenario m_scenario; | ||
| }; | ||
|
|
||
| // (slug, label) entries as [0 "label", ...]. | ||
| std::string describe_entries(const std::vector<std::pair<std::string, std::string>>& entries){ | ||
| std::string text; | ||
| for (const auto& [slug, label] : entries){ | ||
| text += std::format("{}{} \"{}\"", text.empty() ? "" : ", ", slug, label); | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there a StrJoin function that avoid repeated append?
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, Common/Cpp/Strings (StringTools, Unicode) has no join helper, and nothing else in the repo has one. C++23 |
||
| } | ||
| return std::format("[{}]", text); | ||
| } | ||
|
|
||
| // A game's checkpoint labels, in story order. | ||
| struct StoryCheckpointDatabaseInput{ | ||
| std::string name; | ||
| std::vector<std::string> labels; | ||
| }; | ||
|
|
||
| // The (slug, label) entries: one per label, in order, slugged by its index. | ||
| struct StoryCheckpointDatabaseExpectation{ | ||
| std::vector<std::pair<std::string, std::string>> entries; | ||
| }; | ||
|
|
||
| // story_checkpoint_database() over a game's labels and the entries it must make. | ||
| struct StoryCheckpointDatabaseCase{ | ||
| StoryCheckpointDatabaseInput input; | ||
| StoryCheckpointDatabaseExpectation expectation; | ||
| }; | ||
|
|
||
| class Test_StoryCheckpointDatabase : public UnitTest{ | ||
|
Check failure on line 133 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp
|
||
| public: | ||
| Test_StoryCheckpointDatabase(StoryCheckpointDatabaseCase test_case) | ||
| : UnitTest(std::format("Pokemon::AutoStorySelectDatabase - {}", test_case.input.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<std::pair<std::string, std::string>> 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[] = { | ||
| { | ||
| .input = {.name = "checkpoint labels slugged by index", .labels = {"00.01 Pallet Town", "00.02 Oak's lab"}}, | ||
| .expectation = {.entries = {{"0", "00.01 Pallet Town"}, {"1", "00.02 Oak's lab"}}}, | ||
| }, | ||
| { | ||
| .input = {.name = "no checkpoints, no entries", .labels = {}}, | ||
| .expectation = {.entries = {}}, | ||
| }, | ||
| }; | ||
|
|
||
| void add_index_tests(UnitTestDatabase& database){ | ||
| const AutoStoryIndexScenario scenarios[] = { | ||
| { | ||
|
|
@@ -134,7 +195,7 @@ | |
| } | ||
| } | ||
|
|
||
| void add_select_database_tests(UnitTestDatabase& database){ | ||
|
Check warning on line 198 in SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp
|
||
| const AutoStorySelectScenario select_scenarios[] = { | ||
| { | ||
| .name = "the tutorial dropdown", | ||
|
|
@@ -158,6 +219,9 @@ | |
| for (const AutoStorySelectScenario& scenario : select_scenarios){ | ||
| database.add<Test_AutoStorySelectDatabase>(scenario); | ||
| } | ||
| for (const StoryCheckpointDatabaseCase& test_case : STORY_CHECKPOINT_DATABASE_CASES){ | ||
| database.add<Test_StoryCheckpointDatabase>(test_case); | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* Story Range Options | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include <utility> | ||
| #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( | ||
| "<b>Start checkpoint:</b><br>The program starts from this checkpoint's save.", | ||
| checkpoints, | ||
| LockMode::LOCK_WHILE_RUNNING, | ||
| 0 | ||
| ) | ||
| , END_CHECKPOINT( | ||
| "<b>End checkpoint:</b><br>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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <string> | ||
| #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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* Story Range Options Tests | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| */ | ||
|
|
||
| #include <format> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <utility> | ||
| #include <vector> | ||
| #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<std::unique_ptr<NamedCheckpoint>>& story_list(){ | ||
| static const std::vector<std::unique_ptr<NamedCheckpoint>> list = []{ | ||
| std::vector<std::unique_ptr<NamedCheckpoint>> ret; | ||
| for (std::string_view label : {"tutorial 0", "tutorial 1", "main 0", "main 1", "main 2"}){ | ||
| ret.emplace_back(std::make_unique<NamedCheckpoint>(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; | ||
| } | ||
|
|
||
| // The start and end set by slug, as saved settings load them; empty | ||
| // leaves the default. | ||
| struct StoryRangeInput{ | ||
| std::string name; | ||
| std::string start_slug; | ||
| std::string end_slug; | ||
| }; | ||
|
|
||
| // The indexes in the dropdown, and whether the range is valid. | ||
| struct StoryRangeExpectation{ | ||
| size_t start; | ||
| size_t end; | ||
| bool valid; | ||
| }; | ||
|
|
||
| // A range chosen in the main story's dropdowns and what the group reads from it. | ||
| struct StoryRangeCase{ | ||
| StoryRangeInput input; | ||
| StoryRangeExpectation expectation; | ||
| }; | ||
|
|
||
| class Test_StoryRange : public UnitTest{ | ||
|
Check failure on line 71 in SerialPrograms/Source/Pokemon/Options/Pokemon_StoryRangeOptions_Tests.cpp
|
||
| public: | ||
| Test_StoryRange(StoryRangeCase test_case) | ||
| : UnitTest(std::format("Pokemon::StoryRangeOptions - {}", test_case.input.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 StoryRangeExpectation& 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[] = { | ||
| { | ||
| .input = {.name = "the whole database by default", .start_slug = "", .end_slug = ""}, | ||
| .expectation = {.start = 0, .end = 2, .valid = true}, | ||
| }, | ||
| { | ||
| .input = {.name = "slugs are whole-list indexes, indexes are the dropdown's", .start_slug = "3", .end_slug = "4"}, | ||
| .expectation = {.start = 1, .end = 2, .valid = true}, | ||
| }, | ||
| { | ||
| .input = {.name = "one checkpoint", .start_slug = "3", .end_slug = "3"}, | ||
| .expectation = {.start = 1, .end = 1, .valid = true}, | ||
| }, | ||
| { | ||
| .input = {.name = "a start after the end is refused", .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_StoryRange>(test_case); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } // namespace Pokemon | ||
| } // namespace PokemonAutomation | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not directly related here: but what if there are multiple orders, e.g. in FRLG for different starter choice? would that work with here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes for FRLG's starter choice. The starter doesn't change the order of checkpoints: every starter goes through the same story beats. Only what happens inside some of them changes, such as which ball to walk to and the rival's team. Today FRLG has one table (
story_checkpoints()) and assumes Squirtle (TODO atPokemonFRLG_AutoStory.cpp:457). A starter option would feed into those events and battle data, as SV'sSTARTERCHOICEdoes, and this dropdown stays as it is.It would break for a game whose order really branches, with different checkpoints per choice. The slugs are list indices, so a saved "12" would point at a different checkpoint in another order. The database is also a function-local static, so it can't follow an option change. The smallest fix is to keep one list with every branch's checkpoints and skip the ones that don't apply at run time, the way FRLG already joins or skips handoff rows. If we ever need several real lists, slug by checkpoint id ("01.05") instead of index, so saved settings stay valid. That isn't a one-liner, so I haven't done it here.