From 99f60e08549e0ce7ed9a1c8353fa90864ffa94f7 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:18:33 -0800 Subject: [PATCH 1/8] AutoStory: CheckpointId value type and segment-start check CheckpointId parses "SS.CC" ids like "03.08" (exactly two digits, a dot, two digits), prints them back, orders them, and starts_segment() tells a caller when a run enters a new segment so it can count Segments Started. --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../AutoStory/Pokemon_CheckpointId.cpp | 62 +++++ .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 48 ++++ .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 260 ++++++++++++++++++ .../AutoStory/Pokemon_CheckpointId_Tests.h | 23 ++ SerialPrograms/cmake/SourceFiles.cmake | 4 + 6 files changed, 399 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index c6c3e6b052..9ead73d4c0 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -16,6 +16,7 @@ #include "Common/Qt/QtThreadPool_Tests.h" #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" #include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h" @@ -78,6 +79,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Kernels::add_tests(ret); Pokemon::add_tests_AutoStorySelect(ret); Pokemon::add_tests_AutoStoryStats(ret); + Pokemon::add_tests_CheckpointId(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp new file mode 100644 index 0000000000..6bb35dedae --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -0,0 +1,62 @@ +/* AutoStory Checkpoint Id + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_CheckpointId.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + +// The value of two decimal digits, or nullopt if either is not a digit. +std::optional parse_two_digits(std::string_view text){ + if (text.size() != 2){ + return std::nullopt; + } + uint8_t value = 0; + for (char c : text){ + if (c < '0' || c > '9'){ + return std::nullopt; + } + value = (uint8_t)(value * 10 + (c - '0')); + } + return value; +} + +} // namespace + + + +std::optional CheckpointId::parse(std::string_view text){ + if (text.size() != 5 || text[2] != '.'){ + return std::nullopt; + } + std::optional segment = parse_two_digits(text.substr(0, 2)); + std::optional checkpoint = parse_two_digits(text.substr(3, 2)); + if (!segment || !checkpoint){ + return std::nullopt; + } + return CheckpointId{*segment, *checkpoint}; +} + +std::string CheckpointId::to_string() const{ + return std::format("{:02}.{:02}", segment, checkpoint); +} + +std::string CheckpointId::segment_string() const{ + return std::format("{:02}", segment); +} + + +bool starts_segment(std::optional previous, CheckpointId next){ + return !previous || previous->segment != next.segment; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h new file mode 100644 index 0000000000..67ffb07c12 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -0,0 +1,48 @@ +/* AutoStory Checkpoint Id + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointId_H +#define PokemonAutomation_Pokemon_CheckpointId_H + +#include +#include +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// An AutoStory checkpoint, written "SS.CC": segment 03, checkpoint 08 is +// "03.08". Ids order by segment, then by checkpoint. +struct CheckpointId{ + uint8_t segment; + uint8_t checkpoint; + + // Reads exactly two digits, a dot and two digits. Anything else + // ("3.08", "03.08b", "03-08", "03.08.01") returns nullopt. + static std::optional parse(std::string_view text); + + // "03.08". + std::string to_string() const; + // "03". + std::string segment_string() const; + + auto operator<=>(const CheckpointId&) const = default; +}; + + +// Whether reaching `next` starts a segment: true for the first checkpoint of +// a run (`previous` is nullopt) and when the segment differs from the last +// checkpoint's. A caller adds one to AutoStoryStats::m_segments_started then. +bool starts_segment(std::optional previous, CheckpointId next); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp new file mode 100644 index 0000000000..dd2a3fe601 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -0,0 +1,260 @@ +/* AutoStory Checkpoint Id Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include "Pokemon_CheckpointId.h" +#include "Pokemon_CheckpointId_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + +namespace{ + + +// Parse: text in, the id printed back out (nullopt when parse rejects it). + +struct ParseInput{ + std::string text; +}; + +struct ParseExpectation{ + std::optional id; + std::optional segment; +}; + +struct ParseScenario{ + std::string name; + ParseInput input; + ParseExpectation expectation; +}; + +class Test_CheckpointIdParse : public UnitTest{ +public: + Test_CheckpointIdParse(ParseScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId::parse - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::optional got = CheckpointId::parse(m_scenario.input.text); + const ParseExpectation& expected = m_scenario.expectation; + if (!expected.id){ + if (got){ + return std::format("Expected \"{}\" to be rejected, got {}", m_scenario.input.text, got->to_string()); + } + return true; + } + if (!got){ + return std::format("Expected \"{}\" to parse, it was rejected", m_scenario.input.text); + } + if (got->to_string() != *expected.id){ + return std::format("Expected id {}, got {}", *expected.id, got->to_string()); + } + if (got->segment_string() != *expected.segment){ + return std::format("Expected segment {}, got {}", *expected.segment, got->segment_string()); + } + return true; + } + +private: + const ParseScenario m_scenario; +}; + + +// Ordering: two ids, how the first compares to the second. + +struct OrderInput{ + CheckpointId left; + CheckpointId right; +}; + +struct OrderExpectation{ + std::strong_ordering order; +}; + +struct OrderScenario{ + std::string name; + OrderInput input; + OrderExpectation expectation; +}; + +const char* order_name(std::strong_ordering order){ + if (order < 0){ + return "less"; + } + if (order > 0){ + return "greater"; + } + return "equal"; +} + +class Test_CheckpointIdOrder : public UnitTest{ +public: + Test_CheckpointIdOrder(OrderScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId order - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::strong_ordering got = m_scenario.input.left <=> m_scenario.input.right; + if (got != m_scenario.expectation.order){ + return std::format( + "Expected {} to be {} than {}, got {}", + m_scenario.input.left.to_string(), + order_name(m_scenario.expectation.order), + m_scenario.input.right.to_string(), + order_name(got) + ); + } + return true; + } + +private: + const OrderScenario m_scenario; +}; + + +// Segment starts: the checkpoints a run passes, in order, and the ones that +// start a segment. + +struct SegmentStartInput{ + std::vector passed; +}; + +struct SegmentStartExpectation{ + std::vector starts; +}; + +struct SegmentStartScenario{ + std::string name; + SegmentStartInput input; + SegmentStartExpectation expectation; +}; + +std::string join(const std::vector& ids){ + std::string ret; + for (const std::string& id : ids){ + if (!ret.empty()){ + ret += ", "; + } + ret += id; + } + return ret; +} + +class Test_CheckpointIdSegmentStart : public UnitTest{ +public: + Test_CheckpointIdSegmentStart(SegmentStartScenario scenario) + : UnitTest(std::format("Pokemon::CheckpointId starts_segment - {}", scenario.name)) + , m_scenario(std::move(scenario)) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + std::vector got; + std::optional previous; + for (CheckpointId next : m_scenario.input.passed){ + if (starts_segment(previous, next)){ + got.emplace_back(next.to_string()); + } + previous = next; + } + if (got != m_scenario.expectation.starts){ + return std::format( + "Expected segment starts [{}], got [{}]", + join(m_scenario.expectation.starts), join(got) + ); + } + return true; + } + +private: + const SegmentStartScenario m_scenario; +}; + + +} // namespace + + + +void add_tests_CheckpointId(UnitTestDatabase& database){ + const std::vector parse_cases{ + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, + {.name = "letter suffix", .input = {.text = "03.08b"}}, + {.name = "one-digit segment", .input = {.text = "3.08"}}, + {.name = "one-digit checkpoint", .input = {.text = "03.8"}}, + {.name = "three-digit segment", .input = {.text = "100.08"}}, + {.name = "three-digit checkpoint", .input = {.text = "03.100"}}, + {.name = "no dot", .input = {.text = "0308"}}, + {.name = "other separator", .input = {.text = "03-08"}}, + {.name = "extra part", .input = {.text = "03.08.01"}}, + {.name = "segment only", .input = {.text = "03"}}, + {.name = "sign", .input = {.text = "+3.08"}}, + {.name = "spaces", .input = {.text = " 3.08"}}, + {.name = "empty", .input = {.text = ""}}, + }; + for (const ParseScenario& scenario : parse_cases){ + database.add(scenario); + } + + const std::vector order_cases{ + { + .name = "same id", + .input = {.left = {3, 8}, .right = {3, 8}}, + .expectation = {.order = std::strong_ordering::equal}, + }, + { + .name = "later checkpoint in the same segment", + .input = {.left = {3, 9}, .right = {3, 8}}, + .expectation = {.order = std::strong_ordering::greater}, + }, + { + .name = "segment wins over checkpoint", + .input = {.left = {2, 99}, .right = {3, 0}}, + .expectation = {.order = std::strong_ordering::less}, + }, + }; + for (const OrderScenario& scenario : order_cases){ + database.add(scenario); + } + + const std::vector segment_cases{ + { + .name = "first checkpoint of a run starts its segment", + .input = {.passed = {{3, 8}}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "checkpoints in one segment start it once", + .input = {.passed = {{3, 8}, {3, 9}, {3, 10}}}, + .expectation = {.starts = {"03.08"}}, + }, + { + .name = "each new segment starts once", + .input = {.passed = {{2, 4}, {2, 5}, {3, 0}, {3, 1}, {4, 0}}}, + .expectation = {.starts = {"02.04", "03.00", "04.00"}}, + }, + { + .name = "empty run starts nothing", + .input = {.passed = {}}, + .expectation = {.starts = {}}, + }, + }; + for (const SegmentStartScenario& scenario : segment_cases){ + database.add(scenario); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h new file mode 100644 index 0000000000..37212aa763 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h @@ -0,0 +1,23 @@ +/* AutoStory Checkpoint Id Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_CheckpointId_Tests_H +#define PokemonAutomation_Pokemon_CheckpointId_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests for parsing, printing and ordering checkpoint ids. +void add_tests_CheckpointId(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index b2df0b557f..3c546cb9f6 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1483,6 +1483,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h + Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointId.h + Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h From 99cdd6d0bfc4d9cb04a50527d680a1b0c9e195bf Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:18:54 -0800 Subject: [PATCH 2/8] CheckpointId tests: split registration, full case rows --- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 54 +++++++++++-------- 1 file changed, 33 insertions(+), 21 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index dd2a3fe601..a9a100f3cc 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Pokemon_CheckpointId.h" @@ -86,7 +87,7 @@ struct OrderScenario{ OrderExpectation expectation; }; -const char* order_name(std::strong_ordering order){ +std::string_view order_name(std::strong_ordering order){ if (order < 0){ return "less"; } @@ -180,32 +181,30 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ }; -} // namespace - - - -void add_tests_CheckpointId(UnitTestDatabase& database){ +void add_parse_tests(UnitTestDatabase& database){ const std::vector parse_cases{ - {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, - {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, - {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "03.08b"}}, - {.name = "one-digit segment", .input = {.text = "3.08"}}, - {.name = "one-digit checkpoint", .input = {.text = "03.8"}}, - {.name = "three-digit segment", .input = {.text = "100.08"}}, - {.name = "three-digit checkpoint", .input = {.text = "03.100"}}, - {.name = "no dot", .input = {.text = "0308"}}, - {.name = "other separator", .input = {.text = "03-08"}}, - {.name = "extra part", .input = {.text = "03.08.01"}}, - {.name = "segment only", .input = {.text = "03"}}, - {.name = "sign", .input = {.text = "+3.08"}}, - {.name = "spaces", .input = {.text = " 3.08"}}, - {.name = "empty", .input = {.text = ""}}, + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, + {.name = "letter suffix", .input = {.text = "03.08b"}, .expectation = {}}, + {.name = "one-digit segment", .input = {.text = "3.08"}, .expectation = {}}, + {.name = "one-digit checkpoint", .input = {.text = "03.8"}, .expectation = {}}, + {.name = "three-digit segment", .input = {.text = "100.08"}, .expectation = {}}, + {.name = "three-digit checkpoint", .input = {.text = "03.100"}, .expectation = {}}, + {.name = "no dot", .input = {.text = "0308"}, .expectation = {}}, + {.name = "other separator", .input = {.text = "03-08"}, .expectation = {}}, + {.name = "extra part", .input = {.text = "03.08.01"}, .expectation = {}}, + {.name = "segment only", .input = {.text = "03"}, .expectation = {}}, + {.name = "sign", .input = {.text = "+3.08"}, .expectation = {}}, + {.name = "spaces", .input = {.text = " 3.08"}, .expectation = {}}, + {.name = "empty", .input = {.text = ""}, .expectation = {}}, }; for (const ParseScenario& scenario : parse_cases){ database.add(scenario); } +} +void add_order_tests(UnitTestDatabase& database){ const std::vector order_cases{ { .name = "same id", @@ -226,7 +225,9 @@ void add_tests_CheckpointId(UnitTestDatabase& database){ for (const OrderScenario& scenario : order_cases){ database.add(scenario); } +} +void add_segment_start_tests(UnitTestDatabase& database){ const std::vector segment_cases{ { .name = "first checkpoint of a run starts its segment", @@ -255,6 +256,17 @@ void add_tests_CheckpointId(UnitTestDatabase& database){ } +} // namespace + + + +void add_tests_CheckpointId(UnitTestDatabase& database){ + add_parse_tests(database); + add_order_tests(database); + add_segment_start_tests(database); +} + + } // namespace Pokemon } // namespace PokemonAutomation From 51b07e89fd1d8563a39a4e29b77c0b2128cdac00 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:19:11 -0800 Subject: [PATCH 3/8] CheckpointId tests: segment runs as id strings --- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index a9a100f3cc..b1eade625f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -127,7 +127,7 @@ class Test_CheckpointIdOrder : public UnitTest{ // start a segment. struct SegmentStartInput{ - std::vector passed; + std::vector passed; }; struct SegmentStartExpectation{ @@ -161,9 +161,13 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ std::vector got; std::optional previous; - for (CheckpointId next : m_scenario.input.passed){ - if (starts_segment(previous, next)){ - got.emplace_back(next.to_string()); + for (const std::string& text : m_scenario.input.passed){ + const std::optional next = CheckpointId::parse(text); + if (!next){ + return std::format("Bad checkpoint id \"{}\" in the test input", text); + } + if (starts_segment(previous, *next)){ + got.emplace_back(next->to_string()); } previous = next; } @@ -231,17 +235,17 @@ void add_segment_start_tests(UnitTestDatabase& database){ const std::vector segment_cases{ { .name = "first checkpoint of a run starts its segment", - .input = {.passed = {{3, 8}}}, + .input = {.passed = {"03.08"}}, .expectation = {.starts = {"03.08"}}, }, { .name = "checkpoints in one segment start it once", - .input = {.passed = {{3, 8}, {3, 9}, {3, 10}}}, + .input = {.passed = {"03.08", "03.09", "03.10"}}, .expectation = {.starts = {"03.08"}}, }, { .name = "each new segment starts once", - .input = {.passed = {{2, 4}, {2, 5}, {3, 0}, {3, 1}, {4, 0}}}, + .input = {.passed = {"02.04", "02.05", "03.00", "03.01", "04.00"}}, .expectation = {.starts = {"02.04", "03.00", "04.00"}}, }, { From 6d5cfa409aaf652d0db857deb9183b3a53c9de39 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:22:07 -0800 Subject: [PATCH 4/8] CheckpointId: allow one optional lowercase letter suffix FRLG splits boss battles out of a checkpoint without renumbering the ones after it (00.12b, 12.02b). The suffix sorts after the bare id and before the next checkpoint; starts_segment ignores it. --- .../AutoStory/Pokemon_CheckpointId.cpp | 17 +++++-- .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 18 +++++--- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 46 +++++++++++++++++-- 3 files changed, 68 insertions(+), 13 deletions(-) diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp index 6bb35dedae..38a7f2491f 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -32,7 +32,7 @@ std::optional parse_two_digits(std::string_view text){ std::optional CheckpointId::parse(std::string_view text){ - if (text.size() != 5 || text[2] != '.'){ + if ((text.size() != 5 && text.size() != 6) || text[2] != '.'){ return std::nullopt; } std::optional segment = parse_two_digits(text.substr(0, 2)); @@ -40,11 +40,22 @@ std::optional CheckpointId::parse(std::string_view text){ if (!segment || !checkpoint){ return std::nullopt; } - return CheckpointId{*segment, *checkpoint}; + char suffix = '\0'; + if (text.size() == 6){ + suffix = text[5]; + if (suffix < 'a' || suffix > 'z'){ + return std::nullopt; + } + } + return CheckpointId{*segment, *checkpoint, suffix}; } std::string CheckpointId::to_string() const{ - return std::format("{:02}.{:02}", segment, checkpoint); + std::string ret = std::format("{:02}.{:02}", segment, checkpoint); + if (suffix != '\0'){ + ret += suffix; + } + return ret; } std::string CheckpointId::segment_string() const{ diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h index 67ffb07c12..16fca4c994 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -17,17 +17,23 @@ namespace PokemonAutomation{ namespace Pokemon{ -// An AutoStory checkpoint, written "SS.CC": segment 03, checkpoint 08 is -// "03.08". Ids order by segment, then by checkpoint. +// An AutoStory checkpoint, written "SS.CC" with an optional lowercase +// letter: segment 03, checkpoint 08 is "03.08". A letter splits a checkpoint +// without renumbering the ones after it, e.g. FRLG's "12.02b" for a boss +// battle cut out of 12.02. Ids order by segment, then checkpoint, then +// letter: 12.02 < 12.02b < 12.02c < 12.03. struct CheckpointId{ uint8_t segment; uint8_t checkpoint; + // 'a' to 'z', or '\0' for none. + char suffix = '\0'; - // Reads exactly two digits, a dot and two digits. Anything else - // ("3.08", "03.08b", "03-08", "03.08.01") returns nullopt. + // Reads two digits, a dot, two digits and at most one lowercase letter. + // Anything else ("3.08", "03.08B", "03.08bc", "03.08b1", "03-08", + // "03.08.01") returns nullopt. static std::optional parse(std::string_view text); - // "03.08". + // "03.08", or "03.08b" with a suffix. std::string to_string() const; // "03". std::string segment_string() const; @@ -38,7 +44,7 @@ struct CheckpointId{ // Whether reaching `next` starts a segment: true for the first checkpoint of // a run (`previous` is nullopt) and when the segment differs from the last -// checkpoint's. A caller adds one to AutoStoryStats::m_segments_started then. +// checkpoint's. The suffix does not matter. A caller adds one to AutoStoryStats::m_segments_started then. bool starts_segment(std::optional previous, CheckpointId next); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index b1eade625f..ec2f9ed0b6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -190,7 +190,15 @@ void add_parse_tests(UnitTestDatabase& database){ {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "03.08b"}, .expectation = {}}, + {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.id = "12.02b", .segment = "12"}}, + {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.id = "00.12a", .segment = "00"}}, + {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.id = "03.08z", .segment = "03"}}, + {.name = "uppercase letter", .input = {.text = "03.08B"}, .expectation = {}}, + {.name = "two letters", .input = {.text = "03.08bc"}, .expectation = {}}, + {.name = "digit after the letter", .input = {.text = "03.08b1"}, .expectation = {}}, + {.name = "third checkpoint digit", .input = {.text = "03.081"}, .expectation = {}}, + {.name = "punctuation suffix", .input = {.text = "03.08!"}, .expectation = {}}, + {.name = "letter in the segment", .input = {.text = "0b.08"}, .expectation = {}}, {.name = "one-digit segment", .input = {.text = "3.08"}, .expectation = {}}, {.name = "one-digit checkpoint", .input = {.text = "03.8"}, .expectation = {}}, {.name = "three-digit segment", .input = {.text = "100.08"}, .expectation = {}}, @@ -212,19 +220,39 @@ void add_order_tests(UnitTestDatabase& database){ const std::vector order_cases{ { .name = "same id", - .input = {.left = {3, 8}, .right = {3, 8}}, + .input = {.left = {3, 8, '\0'}, .right = {3, 8, '\0'}}, .expectation = {.order = std::strong_ordering::equal}, }, { .name = "later checkpoint in the same segment", - .input = {.left = {3, 9}, .right = {3, 8}}, + .input = {.left = {3, 9, '\0'}, .right = {3, 8, '\0'}}, .expectation = {.order = std::strong_ordering::greater}, }, { .name = "segment wins over checkpoint", - .input = {.left = {2, 99}, .right = {3, 0}}, + .input = {.left = {2, 99, '\0'}, .right = {3, 0, '\0'}}, .expectation = {.order = std::strong_ordering::less}, }, + { + .name = "no suffix before a suffix", + .input = {.left = {12, 2, '\0'}, .right = {12, 2, 'b'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffixes in letter order", + .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'c'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "suffix before the next checkpoint", + .input = {.left = {12, 2, 'c'}, .right = {12, 3, '\0'}}, + .expectation = {.order = std::strong_ordering::less}, + }, + { + .name = "same suffix", + .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'b'}}, + .expectation = {.order = std::strong_ordering::equal}, + }, }; for (const OrderScenario& scenario : order_cases){ database.add(scenario); @@ -248,6 +276,16 @@ void add_segment_start_tests(UnitTestDatabase& database){ .input = {.passed = {"02.04", "02.05", "03.00", "03.01", "04.00"}}, .expectation = {.starts = {"02.04", "03.00", "04.00"}}, }, + { + .name = "suffix does not start a segment", + .input = {.passed = {"12.01", "12.02", "12.02b", "12.03"}}, + .expectation = {.starts = {"12.01"}}, + }, + { + .name = "suffixed checkpoint can start a segment", + .input = {.passed = {"00.12", "00.12b", "01.00"}}, + .expectation = {.starts = {"00.12", "01.00"}}, + }, { .name = "empty run starts nothing", .input = {.passed = {}}, From 6a78529a22f35bb001da27b434db4129c049c849 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sat, 14 Nov 2026 23:28:25 -0800 Subject: [PATCH 5/8] CheckpointId: optional suffix, private fields built only by parse() suffix is std::optional instead of a '\0' sentinel, the fields have defaults and are private behind accessors, so parse() is the only way to build an id. Order tests take ids as text. The parse test expects one optional. Sorted the CMake list and the test-runner include; shortened the starts_segment comment. --- .../ComputerPrograms_UnitTestRunner.cpp | 2 +- .../AutoStory/Pokemon_CheckpointId.cpp | 17 ++--- .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 29 ++++++--- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 62 +++++++++++-------- SerialPrograms/cmake/SourceFiles.cmake | 6 +- 5 files changed, 71 insertions(+), 45 deletions(-) diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 9ead73d4c0..cfc9664f24 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -16,8 +16,8 @@ #include "Common/Qt/QtThreadPool_Tests.h" #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" -#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" +#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" #include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h" #include "CommonFramework/AudioPipeline/SinkPipeline_Tests.h" diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp index 38a7f2491f..432f32333c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp @@ -40,31 +40,32 @@ std::optional CheckpointId::parse(std::string_view text){ if (!segment || !checkpoint){ return std::nullopt; } - char suffix = '\0'; + std::optional suffix; if (text.size() == 6){ - suffix = text[5]; - if (suffix < 'a' || suffix > 'z'){ + const char letter = text[5]; + if (letter < 'a' || letter > 'z'){ return std::nullopt; } + suffix = letter; } return CheckpointId{*segment, *checkpoint, suffix}; } std::string CheckpointId::to_string() const{ - std::string ret = std::format("{:02}.{:02}", segment, checkpoint); - if (suffix != '\0'){ - ret += suffix; + std::string ret = std::format("{:02}.{:02}", m_segment, m_checkpoint); + if (m_suffix){ + ret += *m_suffix; } return ret; } std::string CheckpointId::segment_string() const{ - return std::format("{:02}", segment); + return std::format("{:02}", m_segment); } bool starts_segment(std::optional previous, CheckpointId next){ - return !previous || previous->segment != next.segment; + return !previous || previous->segment() != next.segment(); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h index 16fca4c994..7e5001266e 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h @@ -21,13 +21,10 @@ namespace Pokemon{ // letter: segment 03, checkpoint 08 is "03.08". A letter splits a checkpoint // without renumbering the ones after it, e.g. FRLG's "12.02b" for a boss // battle cut out of 12.02. Ids order by segment, then checkpoint, then -// letter: 12.02 < 12.02b < 12.02c < 12.03. -struct CheckpointId{ - uint8_t segment; - uint8_t checkpoint; - // 'a' to 'z', or '\0' for none. - char suffix = '\0'; - +// letter: 12.02 < 12.02b < 12.02c < 12.03. parse() is the only way to build +// one, so every id is valid. +class CheckpointId{ +public: // Reads two digits, a dot, two digits and at most one lowercase letter. // Anything else ("3.08", "03.08B", "03.08bc", "03.08b1", "03-08", // "03.08.01") returns nullopt. @@ -38,13 +35,29 @@ struct CheckpointId{ // "03". std::string segment_string() const; + uint8_t segment() const{ return m_segment; } + uint8_t checkpoint() const{ return m_checkpoint; } + // 'a' to 'z'. + std::optional suffix() const{ return m_suffix; } + auto operator<=>(const CheckpointId&) const = default; + +private: + CheckpointId(uint8_t segment, uint8_t checkpoint, std::optional suffix) + : m_segment(segment) + , m_checkpoint(checkpoint) + , m_suffix(suffix) + {} + + uint8_t m_segment = 0; + uint8_t m_checkpoint = 0; + std::optional m_suffix; }; // Whether reaching `next` starts a segment: true for the first checkpoint of // a run (`previous` is nullopt) and when the segment differs from the last -// checkpoint's. The suffix does not matter. A caller adds one to AutoStoryStats::m_segments_started then. +// checkpoint's. The suffix does not matter. bool starts_segment(std::optional previous, CheckpointId next); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp index ec2f9ed0b6..9a507d7234 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp @@ -26,9 +26,13 @@ struct ParseInput{ std::string text; }; +struct ParsedId{ + std::string id; + std::string segment; +}; + struct ParseExpectation{ - std::optional id; - std::optional segment; + std::optional parsed; }; struct ParseScenario{ @@ -47,7 +51,7 @@ class Test_CheckpointIdParse : public UnitTest{ virtual UnitTestResult run(Logger&, CancellableScope&) const override{ const std::optional got = CheckpointId::parse(m_scenario.input.text); const ParseExpectation& expected = m_scenario.expectation; - if (!expected.id){ + if (!expected.parsed){ if (got){ return std::format("Expected \"{}\" to be rejected, got {}", m_scenario.input.text, got->to_string()); } @@ -56,11 +60,11 @@ class Test_CheckpointIdParse : public UnitTest{ if (!got){ return std::format("Expected \"{}\" to parse, it was rejected", m_scenario.input.text); } - if (got->to_string() != *expected.id){ - return std::format("Expected id {}, got {}", *expected.id, got->to_string()); + if (got->to_string() != expected.parsed->id){ + return std::format("Expected id {}, got {}", expected.parsed->id, got->to_string()); } - if (got->segment_string() != *expected.segment){ - return std::format("Expected segment {}, got {}", *expected.segment, got->segment_string()); + if (got->segment_string() != expected.parsed->segment){ + return std::format("Expected segment {}, got {}", expected.parsed->segment, got->segment_string()); } return true; } @@ -73,8 +77,8 @@ class Test_CheckpointIdParse : public UnitTest{ // Ordering: two ids, how the first compares to the second. struct OrderInput{ - CheckpointId left; - CheckpointId right; + std::string left; + std::string right; }; struct OrderExpectation{ @@ -105,13 +109,21 @@ class Test_CheckpointIdOrder : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::strong_ordering got = m_scenario.input.left <=> m_scenario.input.right; + const std::optional left = CheckpointId::parse(m_scenario.input.left); + const std::optional right = CheckpointId::parse(m_scenario.input.right); + if (!left || !right){ + return std::format( + "Bad checkpoint id \"{}\" or \"{}\" in the test input", + m_scenario.input.left, m_scenario.input.right + ); + } + const std::strong_ordering got = *left <=> *right; if (got != m_scenario.expectation.order){ return std::format( "Expected {} to be {} than {}, got {}", - m_scenario.input.left.to_string(), + m_scenario.input.left, order_name(m_scenario.expectation.order), - m_scenario.input.right.to_string(), + m_scenario.input.right, order_name(got) ); } @@ -187,12 +199,12 @@ class Test_CheckpointIdSegmentStart : public UnitTest{ void add_parse_tests(UnitTestDatabase& database){ const std::vector parse_cases{ - {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.id = "03.08", .segment = "03"}}, - {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.id = "00.00", .segment = "00"}}, - {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.id = "99.99", .segment = "99"}}, - {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.id = "12.02b", .segment = "12"}}, - {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.id = "00.12a", .segment = "00"}}, - {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.id = "03.08z", .segment = "03"}}, + {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.parsed = ParsedId{.id = "03.08", .segment = "03"}}}, + {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.parsed = ParsedId{.id = "00.00", .segment = "00"}}}, + {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.parsed = ParsedId{.id = "99.99", .segment = "99"}}}, + {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.parsed = ParsedId{.id = "12.02b", .segment = "12"}}}, + {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.parsed = ParsedId{.id = "00.12a", .segment = "00"}}}, + {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.parsed = ParsedId{.id = "03.08z", .segment = "03"}}}, {.name = "uppercase letter", .input = {.text = "03.08B"}, .expectation = {}}, {.name = "two letters", .input = {.text = "03.08bc"}, .expectation = {}}, {.name = "digit after the letter", .input = {.text = "03.08b1"}, .expectation = {}}, @@ -220,37 +232,37 @@ void add_order_tests(UnitTestDatabase& database){ const std::vector order_cases{ { .name = "same id", - .input = {.left = {3, 8, '\0'}, .right = {3, 8, '\0'}}, + .input = {.left = "03.08", .right = "03.08"}, .expectation = {.order = std::strong_ordering::equal}, }, { .name = "later checkpoint in the same segment", - .input = {.left = {3, 9, '\0'}, .right = {3, 8, '\0'}}, + .input = {.left = "03.09", .right = "03.08"}, .expectation = {.order = std::strong_ordering::greater}, }, { .name = "segment wins over checkpoint", - .input = {.left = {2, 99, '\0'}, .right = {3, 0, '\0'}}, + .input = {.left = "02.99", .right = "03.00"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "no suffix before a suffix", - .input = {.left = {12, 2, '\0'}, .right = {12, 2, 'b'}}, + .input = {.left = "12.02", .right = "12.02b"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "suffixes in letter order", - .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'c'}}, + .input = {.left = "12.02b", .right = "12.02c"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "suffix before the next checkpoint", - .input = {.left = {12, 2, 'c'}, .right = {12, 3, '\0'}}, + .input = {.left = "12.02c", .right = "12.03"}, .expectation = {.order = std::strong_ordering::less}, }, { .name = "same suffix", - .input = {.left = {12, 2, 'b'}, .right = {12, 2, 'b'}}, + .input = {.left = "12.02b", .right = "12.02b"}, .expectation = {.order = std::strong_ordering::equal}, }, }; diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 3c546cb9f6..f534e8dd22 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1483,13 +1483,13 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_AutoStorySelect.h Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.cpp Source/Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h + Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h + Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp + Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointId.h Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h - Source/Pokemon/AutoStory/Pokemon_AutoStoryStats.h - Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.cpp - Source/Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp From 82eccbd7c4553f690d61432488fbb314d0de9274 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:14:25 -0800 Subject: [PATCH 6/8] AutoStory: tile grid, step kinds, action args and the built-in action table --- .../ComputerPrograms_UnitTestRunner.cpp | 2 + .../AutoStory/Pokemon_AutoStory_Tests.h | 30 ++++ .../AutoStory/Pokemon_BuiltinStoryActions.cpp | 79 ++++++++++ .../AutoStory/Pokemon_BuiltinStoryActions.h | 66 ++++++++ .../Pokemon/AutoStory/Pokemon_MapUtil.h | 44 ++++++ .../AutoStory/Types/Pokemon_DeclaredPose.h | 53 +++++++ .../AutoStory/Types/Pokemon_GridPose.h | 37 +++++ .../Types/Pokemon_StoryActionArg.cpp | 20 +++ .../AutoStory/Types/Pokemon_StoryActionArg.h | 93 ++++++++++++ .../Types/Pokemon_StoryActionArg_Tests.cpp | 143 ++++++++++++++++++ .../Types/Pokemon_StoryActionArg_Tests.h | 23 +++ .../Types/Pokemon_StoryActionSpec.cpp | 62 ++++++++ .../AutoStory/Types/Pokemon_StoryActionSpec.h | 56 +++++++ .../AutoStory/Types/Pokemon_StoryStepKind.cpp | 52 +++++++ .../AutoStory/Types/Pokemon_StoryStepKind.h | 45 ++++++ .../Pokemon/Navigation/Pokemon_Direction.cpp | 30 ++++ .../Pokemon/Navigation/Pokemon_Direction.h | 36 +++++ .../Pokemon/Navigation/Pokemon_Grid.cpp | 35 +++++ .../Source/Pokemon/Navigation/Pokemon_Grid.h | 78 ++++++++++ SerialPrograms/cmake/SourceFiles.cmake | 18 +++ 20 files changed, 1002 insertions(+) create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_MapUtil.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp create mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.h create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.cpp create mode 100644 SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index cfc9664f24..27d3578671 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -16,6 +16,7 @@ #include "Common/Qt/QtThreadPool_Tests.h" #include "Kernels/Kernels_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" +#include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" #include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" @@ -80,6 +81,7 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Pokemon::add_tests_AutoStorySelect(ret); Pokemon::add_tests_AutoStoryStats(ret); Pokemon::add_tests_CheckpointId(ret); + Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); NintendoSwitch::add_tests_UpdatePopupDetector(ret); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h new file mode 100644 index 0000000000..4767126bfb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -0,0 +1,30 @@ +/* AutoStory Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The unit tests of the pieces every AutoStory program shares, story + * engine or not. add_tests_AutoStory() is the one entry point the unit + * test runner calls; a new shared AutoStory suite registers there. + * + */ + +#ifndef PokemonAutomation_Pokemon_AutoStory_Tests_H +#define PokemonAutomation_Pokemon_AutoStory_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" +#include "Types/Pokemon_StoryActionArg_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers every shared AutoStory suite, for the unit test runner. +inline void add_tests_AutoStory(UnitTestDatabase& database){ + add_tests_StoryActionArg(database); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp new file mode 100644 index 0000000000..898504d384 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp @@ -0,0 +1,79 @@ +/* Builtin Story Actions + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_BuiltinStoryActions.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +StoryArgSpec required(std::string key, StoryArgType type, std::string why){ + return StoryArgSpec{.key = std::move(key), .type = type, .required = true, .fallback = std::nullopt, .why = std::move(why)}; +} + +StoryArgSpec optional(std::string key, StoryArgType type, StoryArgValue fallback, std::string why){ + return StoryArgSpec{.key = std::move(key), .type = type, .required = false, .fallback = std::move(fallback), .why = std::move(why)}; +} + +std::vector make_builtin_actions(){ + const StoryArgSpec fight_wild = optional("fight_wild", StoryArgType::BOOLEAN, false, "fight wild battles on the way instead of running"); + return { + {.kind = StoryStepKind::ROUTE, .name = "route", .args = { + required("target", StoryArgType::GRID_POSE, "where to go; with several tiles, any of them"), + fight_wild, + }}, + {.kind = StoryStepKind::READY, .name = "ready", .args = { + required("target", StoryArgType::GRID_POSE, "the nurse"), + }}, + {.kind = StoryStepKind::LEGS, .name = "legs", .args = { + required("legs", StoryArgType::LEGS, "the straight legs to walk"), + fight_wild, + }}, + {.kind = StoryStepKind::STEP, .name = "face", .args = { + required("direction", StoryArgType::DIRECTION, "the way to step or face"), + }}, + {.kind = StoryStepKind::ANSWER, .name = "answer", .args = { + optional("yes", StoryArgType::BOOLEAN, true, "the answer"), + }}, + {.kind = StoryStepKind::BATTLE, .name = "fight", .args = { + optional("may_lose", StoryArgType::BOOLEAN, false, "losing goes on (a rival's first battle)"), + }}, + {.kind = StoryStepKind::TRAIN, .name = "train", .args = { + required("battles", StoryArgType::INTEGER, "the battles to fight"), + }}, + {.kind = StoryStepKind::SHOP, .name = "shop", .args = { + required("items", StoryArgType::ITEMS, "the items and how many of each"), + }}, + }; +} + + +} // namespace + + +std::span builtin_story_actions(){ + static const std::vector actions = make_builtin_actions(); + return actions; +} + +const StoryActionSpec* find_story_action(std::span actions, StoryStepKind kind, std::string_view name){ + for (const StoryActionSpec& action : actions){ + if (action.kind == kind && (name.empty() || action.name == name)){ + return &action; + } + } + return nullptr; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h new file mode 100644 index 0000000000..a13a29cfec --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h @@ -0,0 +1,66 @@ +/* Builtin Story Actions + * + * From: https://github.com/PokemonAutomation/ + * + * The shared engine's built-in actions (a route, fixed legs, a battle, a + * shop...) with the args each takes, and reading a step's args. A game + * adds its own actions (GAME steps and its other ways of doing a kind) to + * these when it loads its tables. + * + */ + +#ifndef PokemonAutomation_Pokemon_BuiltinStoryActions_H +#define PokemonAutomation_Pokemon_BuiltinStoryActions_H + +#include +#include +#include +#include +#include "Common/Cpp/Exceptions.h" +#include "Pokemon_MapUtil.h" +#include "Types/Pokemon_StoryActionArg.h" +#include "Types/Pokemon_StoryActionSpec.h" +#include "Types/Pokemon_StoryStepKind.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The built-in actions: each kind's usual way. A kind's first one is its +// default, the action of a step that names none. +// route ROUTE target GRID_POSE (required), fight_wild BOOLEAN (false) +// ready READY target GRID_POSE (required): the nurse +// legs LEGS legs LEGS (required), fight_wild BOOLEAN (false) +// face STEP direction DIRECTION (required) +// answer ANSWER yes BOOLEAN (true) +// fight BATTLE may_lose BOOLEAN (false) +// train TRAIN battles INTEGER (required) +// shop SHOP items ITEMS (required) +// Other kinds (TALK, MASH, HEAL, SAVE, ...) take no args. +std::span builtin_story_actions(); + +// The action of `kind` named `name` among `actions`; for an empty name, +// the kind's first. Null when there is none. +const StoryActionSpec* find_story_action(std::span actions, StoryStepKind kind, std::string_view name); + +// The value of arg `key`, which the step's action requires or gives a +// fallback, so the loaded step has it. Throws InternalProgramError when it +// is missing or of another type: a step built without its action's spec. +template +const Type& story_arg(const StoryActionArgs& args, std::string_view key); + + +template +const Type& story_arg(const StoryActionArgs& args, std::string_view key){ + const Type* value = value_if_at_it(args, args.find(key)); + if (value == nullptr){ + throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, std::format("Missing or mistyped story arg: {}", key)); + } + return *value; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_MapUtil.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_MapUtil.h new file mode 100644 index 0000000000..f9be88108b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_MapUtil.h @@ -0,0 +1,44 @@ +/* Map Util + * + * From: https://github.com/PokemonAutomation/ + * + * Lookups in a map through the iterator find() returned, so a call site + * says which lookup it does and never copies the value by accident: + * const int64_t* budget = value_if_at_it(args, args.find("budget")); + * + */ + +#ifndef PokemonAutomation_Pokemon_MapUtil_H +#define PokemonAutomation_Pokemon_MapUtil_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The value at `it`, or null when `it` is `map`'s end. +template +const typename Map::mapped_type* value_at_it(const Map& map, typename Map::const_iterator it); + +// The `Type` alternative of the variant at `it`, or null when `it` is +// `map`'s end or the variant holds another type. +template +const Type* value_if_at_it(const Map& map, typename Map::const_iterator it); + + +template +const typename Map::mapped_type* value_at_it(const Map& map, typename Map::const_iterator it){ + return it == map.end() ? nullptr : &it->second; +} + +template +const Type* value_if_at_it(const Map& map, typename Map::const_iterator it){ + return it == map.end() ? nullptr : std::get_if(&it->second); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h new file mode 100644 index 0000000000..cfcab43d72 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h @@ -0,0 +1,53 @@ +/* Declared Pose + * + * From: https://github.com/PokemonAutomation/ + * + * A pose a checkpoint declares in its data (its start, a step's + * declared_after), as plain data. A tile game (FRLG, BDSP) declares a + * map, tiles and a facing; a game with no tile grid (PLZA) declares a + * place, a spot in it and a heading. Each game reads only its own kind + * (StoryPoseTraits::Declared); the lint fails a checkpoint that declares + * the other. + * + */ + +#ifndef PokemonAutomation_Pokemon_DeclaredPose_H +#define PokemonAutomation_Pokemon_DeclaredPose_H + +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// On one of `tiles` of `map` (any tile when none), facing `facing` if set. +struct TileDeclared{ + std::string map; + std::vector tiles; + std::optional facing; + + bool operator==(const TileDeclared& other) const = default; +}; + +// In `place` ("LumioseCity"), at `spot` in it if set, heading `heading` +// degrees if set. +struct ZoneDeclared{ + std::string place; + std::optional spot; + std::optional heading; + + bool operator==(const ZoneDeclared& other) const = default; +}; + +// Either kind; a game reads only its own (StoryPoseTraits::Declared). +using DeclaredPose = std::variant; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h new file mode 100644 index 0000000000..3280715da5 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h @@ -0,0 +1,37 @@ +/* Grid Pose + * + * From: https://github.com/PokemonAutomation/ + * + * Where a player stands on a 2D tile grid (FRLG, BDSP): a map, the tiles + * they may be on and the way they face. Games that move on a surface or + * in 3D (PLZA) need their own type. + * + */ + +#ifndef PokemonAutomation_Pokemon_GridPose_H +#define PokemonAutomation_Pokemon_GridPose_H + +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// One of `tiles` of `map`, facing `facing` if set. As a target, any of the +// tiles will do. +struct GridPose{ + std::string map; + std::vector tiles; + std::optional facing; + + bool operator==(const GridPose& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp new file mode 100644 index 0000000000..4adc5dd7d6 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp @@ -0,0 +1,20 @@ +/* Story Action Arg + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryActionArg.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +StoryArgType story_arg_type(const StoryArgValue& value){ + return (StoryArgType)value.index(); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h new file mode 100644 index 0000000000..f1a9fb5d50 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h @@ -0,0 +1,93 @@ +/* Story Action Arg + * + * From: https://github.com/PokemonAutomation/ + * + * A step's action and its arguments (StoryStep::action, StoryStep::args), + * e.g. a Mart step's {"map": "CeruleanCity_Mart", "budget": 3000}. Each + * action declares the arguments it takes (Pokemon_StoryActionSpec.h), with their + * types and defaults; the JSON reader (Data/Pokemon_StoryStepJson.h) + * parses a step's args against them and fills in the defaults, so a typo + * fails at load instead of mid-run and nothing after it reads JSON. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionArg_H +#define PokemonAutomation_Pokemon_StoryActionArg_H + +#include +#include +#include +#include +#include +#include +#include +#include "Pokemon/Navigation/Pokemon_Grid.h" +#include "Pokemon_GridPose.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A straight leg of a fixed walk. +struct StoryLeg{ + Direction direction = Direction::NORTH; + int tiles = 0; + + bool operator==(const StoryLeg& other) const = default; +}; + +// A leg of a timed walk in a game with no tiles: turn `turn` degrees +// (clockwise, from the way the player faces), then walk for `ms`. +struct StoryTimedLeg{ + int turn = 0; + int ms = 0; + + bool operator==(const StoryTimedLeg& other) const = default; +}; + +// An item and how many to get. +struct StoryItem{ + std::string item; + size_t quantity = 0; + + bool operator==(const StoryItem& other) const = default; +}; + + +// The type an action declares for an argument, in StoryArgValue's order. +enum class StoryArgType{ + BOOLEAN, + INTEGER, + WORD, + DIRECTION, + GRID_POSE, + LEGS, + WORDS, + ITEMS, + INTEGER_ROWS, + TIMED_LEGS, +}; + +// One argument's value; the index is its StoryArgType. +using StoryArgValue = std::variant< + bool, int64_t, std::string, // BOOLEAN, INTEGER, WORD + Direction, GridPose, // DIRECTION, GRID_POSE + std::vector, // LEGS + std::vector, // WORDS + std::vector, // ITEMS + std::vector>, // INTEGER_ROWS: rows of any length, [[12, 7], [3, 9, 1], []] + std::vector // TIMED_LEGS: [turn degrees, ms] pairs, [[0, 4500], [90, 1200]] +>; +static_assert(std::variant_size_v == (size_t)StoryArgType::TIMED_LEGS + 1, "one StoryArgType per StoryArgValue type"); + +// A step's arguments by key. +using StoryActionArgs = std::map>; + +// The type `value` holds. +StoryArgType story_arg_type(const StoryArgValue& value); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp new file mode 100644 index 0000000000..22a3793a95 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp @@ -0,0 +1,143 @@ +/* Story Action Arg Tests + * + * From: https://github.com/PokemonAutomation/ + * + * Checking a step's args against an action's spec and filling in its + * fallbacks. Built standalone (PA_STORY_DATA_STANDALONE), it runs without + * the application. + * + */ + +#include +#include +#include +#include +#include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" +#include "Pokemon_StoryActionArg.h" +#ifndef PA_STORY_DATA_STANDALONE +#include "Pokemon_StoryActionArg_Tests.h" +#endif + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// The args a Mart step takes, as a game declares them. +const StoryActionSpec& mart_spec(){ + static const StoryActionSpec spec{.kind = StoryStepKind::GAME, .name = "mart", .args = { + {.key = "map", .type = StoryArgType::WORD, .required = true, .fallback = std::nullopt, .why = "the Mart's map"}, + {.key = "budget", .type = StoryArgType::INTEGER, .required = true, .fallback = std::nullopt, .why = ""}, + {.key = "floor", .type = StoryArgType::INTEGER, .required = false, .fallback = int64_t(0), .why = ""}, + {.key = "if_present", .type = StoryArgType::BOOLEAN, .required = false, .fallback = std::nullopt, .why = ""}, + }}; + return spec; +} + +struct ArgsCase{ + // Args checked against the Mart's spec, then filled in. + std::string_view name; + struct Input{ + StoryActionArgs args; + }; + + // The errors joined by "; ", and the args with their fallbacks. + struct Expectation{ + std::string_view error = {}; + StoryActionArgs filled = {}; + }; + + Input input; + Expectation expectation; +}; + +const ArgsCase CASES[] = { + {.name = "mart", .input = {.args = {{"map", std::string("M")}, {"budget", int64_t(1500)}, {"if_present", true}}}, + .expectation = {.filled = {{"map", std::string("M")}, {"budget", int64_t(1500)}, {"floor", int64_t(0)}, {"if_present", true}}}}, + {.name = "fallback does not override", .input = {.args = {{"map", std::string("M")}, {"budget", int64_t(1)}, {"floor", int64_t(2)}}}, + .expectation = {.filled = {{"map", std::string("M")}, {"budget", int64_t(1)}, {"floor", int64_t(2)}}}}, + {.name = "unknown key", .input = {.args = {{"map", std::string("M")}, {"budget", int64_t(1)}, {"budgte", int64_t(1)}}}, + .expectation = {.error = "unknown argument \"budgte\"", .filled = {{"map", std::string("M")}, {"budget", int64_t(1)}, {"budgte", int64_t(1)}, {"floor", int64_t(0)}}}}, + {.name = "wrong type", .input = {.args = {{"map", std::string("M")}, {"budget", std::string("lots")}}}, + .expectation = {.error = "argument \"budget\" has the wrong type", .filled = {{"map", std::string("M")}, {"budget", std::string("lots")}, {"floor", int64_t(0)}}}}, + {.name = "missing required", .input = {.args = {{"budget", int64_t(1)}}}, + .expectation = {.error = "missing argument \"map\" (the Mart's map)", .filled = {{"budget", int64_t(1)}, {"floor", int64_t(0)}}}}, +}; + + +// "" when `test` reads as expected, else what differs. +std::string check_case(const ArgsCase& test){ + std::string error; + for (const std::string& each : story_args_errors(mart_spec(), test.input.args)){ + error += (error.empty() ? "" : "; ") + each; + } + if (error != test.expectation.error){ + return std::format("expected error \"{}\", got \"{}\"", test.expectation.error, error); + } + if (with_fallbacks(mart_spec(), test.input.args) != test.expectation.filled){ + return "filled in other args"; + } + const StoryActionArgs& args = test.expectation.filled; + const int64_t* floor = value_if_at_it(args, args.find("floor")); + if (floor == nullptr || *floor != story_arg(args, "floor")){ + return "reads floor otherwise"; + } + return ""; +} + + +#ifndef PA_STORY_DATA_STANDALONE + +class StoryActionArgTest : public UnitTest{ +public: + explicit StoryActionArgTest(ArgsCase test) + : UnitTest(std::format("Pokemon::StoryActionArg - {}", test.name)) + , m_case(test) + {} + + virtual UnitTestResult run(Logger&, CancellableScope&) const override{ + const std::string error = check_case(m_case); + return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + } + +private: + ArgsCase m_case; +}; + +#endif + + +} // namespace + + +#ifndef PA_STORY_DATA_STANDALONE + +void add_tests_StoryActionArg(UnitTestDatabase& database){ + for (const ArgsCase& test : CASES){ + database.add(test); + } +} + +#endif + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +#ifdef PA_STORY_DATA_STANDALONE + +int main(){ + int failed = 0; + for (const auto& test : PokemonAutomation::Pokemon::CASES){ + const std::string error = PokemonAutomation::Pokemon::check_case(test); + std::cout << test.name << ": " << (error.empty() ? "PASS" : error) << '\n'; + failed += !error.empty(); + } + return failed != 0; +} + +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h new file mode 100644 index 0000000000..d20848c4c2 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h @@ -0,0 +1,23 @@ +/* Story Action Arg Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionArg_Tests_H +#define PokemonAutomation_Pokemon_StoryActionArg_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers unit tests for step args: checking them against an action's spec. +void add_tests_StoryActionArg(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp new file mode 100644 index 0000000000..5839da93a0 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp @@ -0,0 +1,62 @@ +/* Story Action Spec + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include "Pokemon_StoryActionSpec.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +const StoryArgSpec* find_spec(const StoryActionSpec& action, std::string_view key){ + for (const StoryArgSpec& spec : action.args){ + if (spec.key == key){ + return &spec; + } + } + return nullptr; +} + + +} // namespace + + +std::vector story_args_errors(const StoryActionSpec& spec, const StoryActionArgs& args){ + std::vector errors; + for (const auto& [key, value] : args){ + const StoryArgSpec* arg = find_spec(spec, key); + if (arg == nullptr){ + errors.emplace_back(std::format("unknown argument \"{}\"", key)); + }else if (story_arg_type(value) != arg->type){ + errors.emplace_back(std::format("argument \"{}\" has the wrong type", key)); + } + } + for (const StoryArgSpec& arg : spec.args){ + if (arg.required && args.find(arg.key) == args.end()){ + errors.emplace_back(std::format("missing argument \"{}\"{}", arg.key, arg.why.empty() ? "" : std::format(" ({})", arg.why))); + } + } + return errors; +} + +StoryActionArgs with_fallbacks(const StoryActionSpec& spec, StoryActionArgs args){ + for (const StoryArgSpec& arg : spec.args){ + if (arg.fallback){ + args.try_emplace(arg.key, *arg.fallback); + } + } + return args; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h new file mode 100644 index 0000000000..f9deb09ebb --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h @@ -0,0 +1,56 @@ +/* Story Action Spec + * + * From: https://github.com/PokemonAutomation/ + * + * What an action declares: the kind of step it does, its name and the + * args it takes, with their types and fallbacks (Pokemon_StoryActionArg.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryActionSpec_H +#define PokemonAutomation_Pokemon_StoryActionSpec_H + +#include +#include +#include +#include "Pokemon_StoryActionArg.h" +#include "Pokemon_StoryStepKind.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// One argument an action takes. A missing optional one reads as `fallback` +// when it has one. +struct StoryArgSpec{ + std::string key; + StoryArgType type = StoryArgType::WORD; + bool required = false; + std::optional fallback; + // What it is for, shown in lint errors. + std::string why; +}; + +// An action: the kind of step it does, its name and the args it takes. +struct StoryActionSpec{ + StoryStepKind kind = StoryStepKind::GAME; + std::string name; + std::vector args; + // It reads or presses through screens only a console shows (menus, a + // keyboard, a battle AI): the engine refuses it on any other backend + // (StoryBackendKind) with the failure "console_only". + bool console_only = false; +}; + +// Each way `args` don't fit `spec`: a key it doesn't name, a value of +// another type, a required one missing. Empty when they fit. +std::vector story_args_errors(const StoryActionSpec& spec, const StoryActionArgs& args); + +// `args` with each missing optional arg of `spec` set to its fallback. +StoryActionArgs with_fallbacks(const StoryActionSpec& spec, StoryActionArgs args); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp new file mode 100644 index 0000000000..28b755779b --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp @@ -0,0 +1,52 @@ +/* Story Step Kind + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryStepKind.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_step_kind_name(StoryStepKind kind){ + switch (kind){ + case StoryStepKind::NEW_GAME: + return "NEW_GAME"; + case StoryStepKind::OPTIONS: + return "OPTIONS"; + case StoryStepKind::ROUTE: + return "ROUTE"; + case StoryStepKind::LEGS: + return "LEGS"; + case StoryStepKind::STEP: + return "STEP"; + case StoryStepKind::TALK: + return "TALK"; + case StoryStepKind::ANSWER: + return "ANSWER"; + case StoryStepKind::MASH: + return "MASH"; + case StoryStepKind::BATTLE: + return "BATTLE"; + case StoryStepKind::TRAIN: + return "TRAIN"; + case StoryStepKind::HEAL: + return "HEAL"; + case StoryStepKind::SHOP: + return "SHOP"; + case StoryStepKind::SAVE: + return "SAVE"; + case StoryStepKind::READY: + return "READY"; + case StoryStepKind::GAME: + return "GAME"; + } + return "?"; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h new file mode 100644 index 0000000000..a3670b8c6c --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h @@ -0,0 +1,45 @@ +/* Story Step Kind + * + * From: https://github.com/PokemonAutomation/ + * + * The kinds of story step the tile games (FRLG, BDSP) share. A game's + * executor does each kind its own way (Pokemon_StoryStepExecutor.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryStepKind_H +#define PokemonAutomation_Pokemon_StoryStepKind_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Action category classification for individual story steps. +enum class StoryStepKind{ + NEW_GAME, // title screen (no save) to the first room + OPTIONS, // the game's options as the story needs them + ROUTE, // plan and walk to `target` + LEGS, // walk `legs` as given + STEP, // one step `direction`: onto a trigger, into a door + TALK, // A at what the player faces + ANSWER, // text to a YES/NO, then `yes` + MASH, // text until the overworld is back: a scene + BATTLE, // text into a battle, fight it, text to the overworld + TRAIN, // fight `battles` wild battles where the player stands + HEAL, // the nurse, from in front of her or of the Center's door + SHOP, // `items` from a shop + SAVE, // save the game where the player stands + READY, // before a big battle: items, and the nurse at `target` if they can't + GAME, // a step only this game has, named by `action` +}; + +// Returns the readable string representation of a story step kind. +std::string story_step_kind_name(StoryStepKind kind); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp new file mode 100644 index 0000000000..0663f79588 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.cpp @@ -0,0 +1,30 @@ +/* Direction + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_Direction.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string direction_name(Direction direction){ + switch (direction){ + case Direction::NORTH: + return "north"; + case Direction::EAST: + return "east"; + case Direction::SOUTH: + return "south"; + case Direction::WEST: + return "west"; + } + return "?"; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.h new file mode 100644 index 0000000000..5024b3ec69 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Direction.h @@ -0,0 +1,36 @@ +/* Direction + * + * From: https://github.com/PokemonAutomation/ + * + * The four cardinal directions the player can face or move in. + * + */ + +#ifndef PokemonAutomation_Pokemon_Direction_H +#define PokemonAutomation_Pokemon_Direction_H + +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Row 0 is the top of a map, so SOUTH increases y. +enum class Direction{ + NORTH, + EAST, + SOUTH, + WEST, +}; +// Array containing all four cardinal directions. +constexpr Direction ALL_DIRECTIONS[] = { + Direction::NORTH, Direction::EAST, Direction::SOUTH, Direction::WEST, +}; +// Returns the lowercase name string of a cardinal direction. +std::string direction_name(Direction direction); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.cpp b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.cpp new file mode 100644 index 0000000000..84fa47bcb1 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.cpp @@ -0,0 +1,35 @@ +/* Grid + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include "Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +TilePos TilePos::next(Direction direction) const{ + switch (direction){ + case Direction::NORTH: + return {x, y - 1}; + case Direction::EAST: + return {x + 1, y}; + case Direction::SOUTH: + return {x, y + 1}; + case Direction::WEST: + return {x - 1, y}; + } + return *this; +} + +std::string to_string(const TilePos& pos){ + return std::format("({}, {})", pos.x, pos.y); +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.h b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.h new file mode 100644 index 0000000000..6fda4044c8 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/Navigation/Pokemon_Grid.h @@ -0,0 +1,78 @@ +/* Grid + * + * From: https://github.com/PokemonAutomation/ + * + * Tiles on an overworld grid, shared by the games that + * move the player a tile at a time. + * + */ + +#ifndef PokemonAutomation_Pokemon_Grid_H +#define PokemonAutomation_Pokemon_Grid_H + +#include +#include +#include +#include +#include +#include "Pokemon_Direction.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Grid coordinate with integer x and y components. +struct TilePos{ + int x = 0; + int y = 0; + + TilePos next(Direction direction) const; + bool operator==(const TilePos& other) const = default; + // x, then y: an order for sets and maps of tiles, not a walking order. + auto operator<=>(const TilePos& other) const = default; +}; +// Formats a TilePos as "(x, y)". +std::string to_string(const TilePos& pos); + + +// One value per tile of a map, row by row from the top. +template +struct Grid{ + int width = 0; + int height = 0; + std::vector cells; + + bool contains(const TilePos& pos) const{ + return 0 <= pos.x && pos.x < width && 0 <= pos.y && pos.y < height; + } + // `pos` must be inside the grid. + const Cell& at(const TilePos& pos) const{ + return cells[(size_t)pos.y * width + pos.x]; + } +}; +// The cell at `pos` of `grid`, to change it; `pos` must be inside the grid. +// A free function, so that Grid stays a plain value whose methods only read. +template +Cell& cell_at(const TilePos& pos, Grid& grid); + +template +Cell& cell_at(const TilePos& pos, Grid& grid){ + return grid.cells[(size_t)pos.y * grid.width + pos.x]; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation + + +// Hash specialization for TilePos. +template <> +struct std::hash{ + size_t operator()(const PokemonAutomation::Pokemon::TilePos& pos) const{ + return std::hash()(((long long)pos.x << 32) ^ (unsigned)pos.y); + } +}; + + +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index f534e8dd22..6e5342bdc1 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1445,6 +1445,20 @@ file(GLOB LIBRARY_SOURCES Source/NintendoSwitch/Programs/Typewriter/NintendoSwitch_TypewriterTextMapping.h Source/PanelLists.cpp Source/PanelLists.h + Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h + Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp + Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h + Source/Pokemon/AutoStory/Pokemon_MapUtil.h + Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h + Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryActionSpec.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h Source/Pokemon/Inference/Pokemon_AbilityReader.cpp Source/Pokemon/Inference/Pokemon_AbilityReader.h Source/Pokemon/Inference/Pokemon_BerryNameReader.cpp @@ -1467,6 +1481,10 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/Inference/Pokemon_TrainPokemonOCR.h Source/Pokemon/Inference/Pokemon_TypeReader.cpp Source/Pokemon/Inference/Pokemon_TypeReader.h + Source/Pokemon/Navigation/Pokemon_Direction.cpp + Source/Pokemon/Navigation/Pokemon_Direction.h + Source/Pokemon/Navigation/Pokemon_Grid.cpp + Source/Pokemon/Navigation/Pokemon_Grid.h Source/Pokemon/Options/Pokemon_BoxSortingTable.cpp Source/Pokemon/Options/Pokemon_BoxSortingTable.h Source/Pokemon/Options/Pokemon_EncounterBotOptions.h From 19b035106d702bc2c127516a1f1aa24809b1add5 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 00:43:24 -0800 Subject: [PATCH 7/8] AutoStory: drop the standalone test main(); declared poses move to the step types Nothing defines PA_STORY_DATA_STANDALONE, so the standalone main() and its guards were dead. Pokemon_DeclaredPose.h has no reader here; it moves to the PR that adds the step and checkpoint types that hold one. --- .../AutoStory/Types/Pokemon_DeclaredPose.h | 53 ------------------- .../Types/Pokemon_StoryActionArg.cpp | 4 +- .../Types/Pokemon_StoryActionArg_Tests.cpp | 40 +++----------- SerialPrograms/cmake/SourceFiles.cmake | 1 - 4 files changed, 10 insertions(+), 88 deletions(-) delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h deleted file mode 100644 index cfcab43d72..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h +++ /dev/null @@ -1,53 +0,0 @@ -/* Declared Pose - * - * From: https://github.com/PokemonAutomation/ - * - * A pose a checkpoint declares in its data (its start, a step's - * declared_after), as plain data. A tile game (FRLG, BDSP) declares a - * map, tiles and a facing; a game with no tile grid (PLZA) declares a - * place, a spot in it and a heading. Each game reads only its own kind - * (StoryPoseTraits::Declared); the lint fails a checkpoint that declares - * the other. - * - */ - -#ifndef PokemonAutomation_Pokemon_DeclaredPose_H -#define PokemonAutomation_Pokemon_DeclaredPose_H - -#include -#include -#include -#include -#include "Pokemon/Navigation/Pokemon_Grid.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// On one of `tiles` of `map` (any tile when none), facing `facing` if set. -struct TileDeclared{ - std::string map; - std::vector tiles; - std::optional facing; - - bool operator==(const TileDeclared& other) const = default; -}; - -// In `place` ("LumioseCity"), at `spot` in it if set, heading `heading` -// degrees if set. -struct ZoneDeclared{ - std::string place; - std::optional spot; - std::optional heading; - - bool operator==(const ZoneDeclared& other) const = default; -}; - -// Either kind; a game reads only its own (StoryPoseTraits::Declared). -using DeclaredPose = std::variant; - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp index 4adc5dd7d6..8c5fb9b949 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp @@ -11,7 +11,9 @@ namespace Pokemon{ StoryArgType story_arg_type(const StoryArgValue& value){ - return (StoryArgType)value.index(); + // The alternatives are declared in StoryArgType's order. A variant left + // valueless by an exception would give variant_npos; none is kept. + return static_cast(value.index()); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp index 22a3793a95..e756ca9d8d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg_Tests.cpp @@ -3,20 +3,17 @@ * From: https://github.com/PokemonAutomation/ * * Checking a step's args against an action's spec and filling in its - * fallbacks. Built standalone (PA_STORY_DATA_STANDALONE), it runs without - * the application. + * fallbacks. * */ #include -#include +#include #include #include #include "Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h" #include "Pokemon_StoryActionArg.h" -#ifndef PA_STORY_DATA_STANDALONE #include "Pokemon_StoryActionArg_Tests.h" -#endif namespace PokemonAutomation{ namespace Pokemon{ @@ -67,8 +64,8 @@ const ArgsCase CASES[] = { }; -// "" when `test` reads as expected, else what differs. -std::string check_case(const ArgsCase& test){ +// What differs from the expectation, if anything. +std::optional check_case(const ArgsCase& test){ std::string error; for (const std::string& each : story_args_errors(mart_spec(), test.input.args)){ error += (error.empty() ? "" : "; ") + each; @@ -84,12 +81,10 @@ std::string check_case(const ArgsCase& test){ if (floor == nullptr || *floor != story_arg(args, "floor")){ return "reads floor otherwise"; } - return ""; + return std::nullopt; } -#ifndef PA_STORY_DATA_STANDALONE - class StoryActionArgTest : public UnitTest{ public: explicit StoryActionArgTest(ArgsCase test) @@ -98,46 +93,25 @@ class StoryActionArgTest : public UnitTest{ {} virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::string error = check_case(m_case); - return error.empty() ? UnitTestResult(true) : UnitTestResult(error); + const std::optional error = check_case(m_case); + return error ? UnitTestResult(*error) : UnitTestResult(true); } private: ArgsCase m_case; }; -#endif - } // namespace -#ifndef PA_STORY_DATA_STANDALONE - void add_tests_StoryActionArg(UnitTestDatabase& database){ for (const ArgsCase& test : CASES){ database.add(test); } } -#endif - } // namespace Pokemon } // namespace PokemonAutomation - - -#ifdef PA_STORY_DATA_STANDALONE - -int main(){ - int failed = 0; - for (const auto& test : PokemonAutomation::Pokemon::CASES){ - const std::string error = PokemonAutomation::Pokemon::check_case(test); - std::cout << test.name << ": " << (error.empty() ? "PASS" : error) << '\n'; - failed += !error.empty(); - } - return failed != 0; -} - -#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 6e5342bdc1..27c3fe8e61 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1449,7 +1449,6 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.cpp Source/Pokemon/AutoStory/Pokemon_BuiltinStoryActions.h Source/Pokemon/AutoStory/Pokemon_MapUtil.h - Source/Pokemon/AutoStory/Types/Pokemon_DeclaredPose.h Source/Pokemon/AutoStory/Types/Pokemon_GridPose.h Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryActionArg.h From 8910253f4271eebf5d5c6d1d6e7e175072717f64 Mon Sep 17 00:00:00 2001 From: dayfine Date: Sun, 15 Nov 2026 12:33:09 -0800 Subject: [PATCH 8/8] AutoStory: drop CheckpointId; nothing in the PLZA chain uses it --- .../ComputerPrograms_UnitTestRunner.cpp | 2 - .../AutoStory/Pokemon_CheckpointId.cpp | 74 ---- .../Pokemon/AutoStory/Pokemon_CheckpointId.h | 67 ---- .../AutoStory/Pokemon_CheckpointId_Tests.cpp | 326 ------------------ .../AutoStory/Pokemon_CheckpointId_Tests.h | 23 -- SerialPrograms/cmake/SourceFiles.cmake | 4 - 6 files changed, 496 deletions(-) delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp delete mode 100644 SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h diff --git a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp index 27d3578671..06b7be96b3 100644 --- a/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp +++ b/SerialPrograms/Source/ComputerPrograms/ComputerPrograms_UnitTestRunner.cpp @@ -18,7 +18,6 @@ #include "Pokemon/AutoStory/Pokemon_AutoStorySelect_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStory_Tests.h" #include "Pokemon/AutoStory/Pokemon_AutoStoryStats_Tests.h" -#include "Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h" #include "CommonFramework/AudioPipeline/AudioInfo_Tests.h" #include "CommonFramework/AudioPipeline/Tools/ChannelMixer.h" #include "CommonFramework/AudioPipeline/SinkPipeline_Tests.h" @@ -80,7 +79,6 @@ UnitTestDatabase make_UNIT_TESTS_ALL(){ Kernels::add_tests(ret); Pokemon::add_tests_AutoStorySelect(ret); Pokemon::add_tests_AutoStoryStats(ret); - Pokemon::add_tests_CheckpointId(ret); Pokemon::add_tests_AutoStory(ret); NintendoSwitch::add_tests_CheckOnlineDetector(ret); NintendoSwitch::add_tests_FailedToConnectDetector(ret); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp deleted file mode 100644 index 432f32333c..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* AutoStory Checkpoint Id - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include "Pokemon_CheckpointId.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - -namespace{ - -// The value of two decimal digits, or nullopt if either is not a digit. -std::optional parse_two_digits(std::string_view text){ - if (text.size() != 2){ - return std::nullopt; - } - uint8_t value = 0; - for (char c : text){ - if (c < '0' || c > '9'){ - return std::nullopt; - } - value = (uint8_t)(value * 10 + (c - '0')); - } - return value; -} - -} // namespace - - - -std::optional CheckpointId::parse(std::string_view text){ - if ((text.size() != 5 && text.size() != 6) || text[2] != '.'){ - return std::nullopt; - } - std::optional segment = parse_two_digits(text.substr(0, 2)); - std::optional checkpoint = parse_two_digits(text.substr(3, 2)); - if (!segment || !checkpoint){ - return std::nullopt; - } - std::optional suffix; - if (text.size() == 6){ - const char letter = text[5]; - if (letter < 'a' || letter > 'z'){ - return std::nullopt; - } - suffix = letter; - } - return CheckpointId{*segment, *checkpoint, suffix}; -} - -std::string CheckpointId::to_string() const{ - std::string ret = std::format("{:02}.{:02}", m_segment, m_checkpoint); - if (m_suffix){ - ret += *m_suffix; - } - return ret; -} - -std::string CheckpointId::segment_string() const{ - return std::format("{:02}", m_segment); -} - - -bool starts_segment(std::optional previous, CheckpointId next){ - return !previous || previous->segment() != next.segment(); -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h deleted file mode 100644 index 7e5001266e..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId.h +++ /dev/null @@ -1,67 +0,0 @@ -/* AutoStory Checkpoint Id - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_CheckpointId_H -#define PokemonAutomation_Pokemon_CheckpointId_H - -#include -#include -#include -#include -#include - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// An AutoStory checkpoint, written "SS.CC" with an optional lowercase -// letter: segment 03, checkpoint 08 is "03.08". A letter splits a checkpoint -// without renumbering the ones after it, e.g. FRLG's "12.02b" for a boss -// battle cut out of 12.02. Ids order by segment, then checkpoint, then -// letter: 12.02 < 12.02b < 12.02c < 12.03. parse() is the only way to build -// one, so every id is valid. -class CheckpointId{ -public: - // Reads two digits, a dot, two digits and at most one lowercase letter. - // Anything else ("3.08", "03.08B", "03.08bc", "03.08b1", "03-08", - // "03.08.01") returns nullopt. - static std::optional parse(std::string_view text); - - // "03.08", or "03.08b" with a suffix. - std::string to_string() const; - // "03". - std::string segment_string() const; - - uint8_t segment() const{ return m_segment; } - uint8_t checkpoint() const{ return m_checkpoint; } - // 'a' to 'z'. - std::optional suffix() const{ return m_suffix; } - - auto operator<=>(const CheckpointId&) const = default; - -private: - CheckpointId(uint8_t segment, uint8_t checkpoint, std::optional suffix) - : m_segment(segment) - , m_checkpoint(checkpoint) - , m_suffix(suffix) - {} - - uint8_t m_segment = 0; - uint8_t m_checkpoint = 0; - std::optional m_suffix; -}; - - -// Whether reaching `next` starts a segment: true for the first checkpoint of -// a run (`previous` is nullopt) and when the segment differs from the last -// checkpoint's. The suffix does not matter. -bool starts_segment(std::optional previous, CheckpointId next); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp deleted file mode 100644 index 9a507d7234..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp +++ /dev/null @@ -1,326 +0,0 @@ -/* AutoStory Checkpoint Id Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#include -#include -#include -#include -#include -#include -#include -#include "Pokemon_CheckpointId.h" -#include "Pokemon_CheckpointId_Tests.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - -namespace{ - - -// Parse: text in, the id printed back out (nullopt when parse rejects it). - -struct ParseInput{ - std::string text; -}; - -struct ParsedId{ - std::string id; - std::string segment; -}; - -struct ParseExpectation{ - std::optional parsed; -}; - -struct ParseScenario{ - std::string name; - ParseInput input; - ParseExpectation expectation; -}; - -class Test_CheckpointIdParse : public UnitTest{ -public: - Test_CheckpointIdParse(ParseScenario scenario) - : UnitTest(std::format("Pokemon::CheckpointId::parse - {}", scenario.name)) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::optional got = CheckpointId::parse(m_scenario.input.text); - const ParseExpectation& expected = m_scenario.expectation; - if (!expected.parsed){ - if (got){ - return std::format("Expected \"{}\" to be rejected, got {}", m_scenario.input.text, got->to_string()); - } - return true; - } - if (!got){ - return std::format("Expected \"{}\" to parse, it was rejected", m_scenario.input.text); - } - if (got->to_string() != expected.parsed->id){ - return std::format("Expected id {}, got {}", expected.parsed->id, got->to_string()); - } - if (got->segment_string() != expected.parsed->segment){ - return std::format("Expected segment {}, got {}", expected.parsed->segment, got->segment_string()); - } - return true; - } - -private: - const ParseScenario m_scenario; -}; - - -// Ordering: two ids, how the first compares to the second. - -struct OrderInput{ - std::string left; - std::string right; -}; - -struct OrderExpectation{ - std::strong_ordering order; -}; - -struct OrderScenario{ - std::string name; - OrderInput input; - OrderExpectation expectation; -}; - -std::string_view order_name(std::strong_ordering order){ - if (order < 0){ - return "less"; - } - if (order > 0){ - return "greater"; - } - return "equal"; -} - -class Test_CheckpointIdOrder : public UnitTest{ -public: - Test_CheckpointIdOrder(OrderScenario scenario) - : UnitTest(std::format("Pokemon::CheckpointId order - {}", scenario.name)) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - const std::optional left = CheckpointId::parse(m_scenario.input.left); - const std::optional right = CheckpointId::parse(m_scenario.input.right); - if (!left || !right){ - return std::format( - "Bad checkpoint id \"{}\" or \"{}\" in the test input", - m_scenario.input.left, m_scenario.input.right - ); - } - const std::strong_ordering got = *left <=> *right; - if (got != m_scenario.expectation.order){ - return std::format( - "Expected {} to be {} than {}, got {}", - m_scenario.input.left, - order_name(m_scenario.expectation.order), - m_scenario.input.right, - order_name(got) - ); - } - return true; - } - -private: - const OrderScenario m_scenario; -}; - - -// Segment starts: the checkpoints a run passes, in order, and the ones that -// start a segment. - -struct SegmentStartInput{ - std::vector passed; -}; - -struct SegmentStartExpectation{ - std::vector starts; -}; - -struct SegmentStartScenario{ - std::string name; - SegmentStartInput input; - SegmentStartExpectation expectation; -}; - -std::string join(const std::vector& ids){ - std::string ret; - for (const std::string& id : ids){ - if (!ret.empty()){ - ret += ", "; - } - ret += id; - } - return ret; -} - -class Test_CheckpointIdSegmentStart : public UnitTest{ -public: - Test_CheckpointIdSegmentStart(SegmentStartScenario scenario) - : UnitTest(std::format("Pokemon::CheckpointId starts_segment - {}", scenario.name)) - , m_scenario(std::move(scenario)) - {} - - virtual UnitTestResult run(Logger&, CancellableScope&) const override{ - std::vector got; - std::optional previous; - for (const std::string& text : m_scenario.input.passed){ - const std::optional next = CheckpointId::parse(text); - if (!next){ - return std::format("Bad checkpoint id \"{}\" in the test input", text); - } - if (starts_segment(previous, *next)){ - got.emplace_back(next->to_string()); - } - previous = next; - } - if (got != m_scenario.expectation.starts){ - return std::format( - "Expected segment starts [{}], got [{}]", - join(m_scenario.expectation.starts), join(got) - ); - } - return true; - } - -private: - const SegmentStartScenario m_scenario; -}; - - -void add_parse_tests(UnitTestDatabase& database){ - const std::vector parse_cases{ - {.name = "segment and checkpoint", .input = {.text = "03.08"}, .expectation = {.parsed = ParsedId{.id = "03.08", .segment = "03"}}}, - {.name = "first checkpoint", .input = {.text = "00.00"}, .expectation = {.parsed = ParsedId{.id = "00.00", .segment = "00"}}}, - {.name = "largest id", .input = {.text = "99.99"}, .expectation = {.parsed = ParsedId{.id = "99.99", .segment = "99"}}}, - {.name = "letter suffix", .input = {.text = "12.02b"}, .expectation = {.parsed = ParsedId{.id = "12.02b", .segment = "12"}}}, - {.name = "first letter", .input = {.text = "00.12a"}, .expectation = {.parsed = ParsedId{.id = "00.12a", .segment = "00"}}}, - {.name = "last letter", .input = {.text = "03.08z"}, .expectation = {.parsed = ParsedId{.id = "03.08z", .segment = "03"}}}, - {.name = "uppercase letter", .input = {.text = "03.08B"}, .expectation = {}}, - {.name = "two letters", .input = {.text = "03.08bc"}, .expectation = {}}, - {.name = "digit after the letter", .input = {.text = "03.08b1"}, .expectation = {}}, - {.name = "third checkpoint digit", .input = {.text = "03.081"}, .expectation = {}}, - {.name = "punctuation suffix", .input = {.text = "03.08!"}, .expectation = {}}, - {.name = "letter in the segment", .input = {.text = "0b.08"}, .expectation = {}}, - {.name = "one-digit segment", .input = {.text = "3.08"}, .expectation = {}}, - {.name = "one-digit checkpoint", .input = {.text = "03.8"}, .expectation = {}}, - {.name = "three-digit segment", .input = {.text = "100.08"}, .expectation = {}}, - {.name = "three-digit checkpoint", .input = {.text = "03.100"}, .expectation = {}}, - {.name = "no dot", .input = {.text = "0308"}, .expectation = {}}, - {.name = "other separator", .input = {.text = "03-08"}, .expectation = {}}, - {.name = "extra part", .input = {.text = "03.08.01"}, .expectation = {}}, - {.name = "segment only", .input = {.text = "03"}, .expectation = {}}, - {.name = "sign", .input = {.text = "+3.08"}, .expectation = {}}, - {.name = "spaces", .input = {.text = " 3.08"}, .expectation = {}}, - {.name = "empty", .input = {.text = ""}, .expectation = {}}, - }; - for (const ParseScenario& scenario : parse_cases){ - database.add(scenario); - } -} - -void add_order_tests(UnitTestDatabase& database){ - const std::vector order_cases{ - { - .name = "same id", - .input = {.left = "03.08", .right = "03.08"}, - .expectation = {.order = std::strong_ordering::equal}, - }, - { - .name = "later checkpoint in the same segment", - .input = {.left = "03.09", .right = "03.08"}, - .expectation = {.order = std::strong_ordering::greater}, - }, - { - .name = "segment wins over checkpoint", - .input = {.left = "02.99", .right = "03.00"}, - .expectation = {.order = std::strong_ordering::less}, - }, - { - .name = "no suffix before a suffix", - .input = {.left = "12.02", .right = "12.02b"}, - .expectation = {.order = std::strong_ordering::less}, - }, - { - .name = "suffixes in letter order", - .input = {.left = "12.02b", .right = "12.02c"}, - .expectation = {.order = std::strong_ordering::less}, - }, - { - .name = "suffix before the next checkpoint", - .input = {.left = "12.02c", .right = "12.03"}, - .expectation = {.order = std::strong_ordering::less}, - }, - { - .name = "same suffix", - .input = {.left = "12.02b", .right = "12.02b"}, - .expectation = {.order = std::strong_ordering::equal}, - }, - }; - for (const OrderScenario& scenario : order_cases){ - database.add(scenario); - } -} - -void add_segment_start_tests(UnitTestDatabase& database){ - const std::vector segment_cases{ - { - .name = "first checkpoint of a run starts its segment", - .input = {.passed = {"03.08"}}, - .expectation = {.starts = {"03.08"}}, - }, - { - .name = "checkpoints in one segment start it once", - .input = {.passed = {"03.08", "03.09", "03.10"}}, - .expectation = {.starts = {"03.08"}}, - }, - { - .name = "each new segment starts once", - .input = {.passed = {"02.04", "02.05", "03.00", "03.01", "04.00"}}, - .expectation = {.starts = {"02.04", "03.00", "04.00"}}, - }, - { - .name = "suffix does not start a segment", - .input = {.passed = {"12.01", "12.02", "12.02b", "12.03"}}, - .expectation = {.starts = {"12.01"}}, - }, - { - .name = "suffixed checkpoint can start a segment", - .input = {.passed = {"00.12", "00.12b", "01.00"}}, - .expectation = {.starts = {"00.12", "01.00"}}, - }, - { - .name = "empty run starts nothing", - .input = {.passed = {}}, - .expectation = {.starts = {}}, - }, - }; - for (const SegmentStartScenario& scenario : segment_cases){ - database.add(scenario); - } -} - - -} // namespace - - - -void add_tests_CheckpointId(UnitTestDatabase& database){ - add_parse_tests(database); - add_order_tests(database); - add_segment_start_tests(database); -} - - - -} // namespace Pokemon -} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h deleted file mode 100644 index 37212aa763..0000000000 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h +++ /dev/null @@ -1,23 +0,0 @@ -/* AutoStory Checkpoint Id Tests - * - * From: https://github.com/PokemonAutomation/ - * - */ - -#ifndef PokemonAutomation_Pokemon_CheckpointId_Tests_H -#define PokemonAutomation_Pokemon_CheckpointId_Tests_H - -#include "Common/Cpp/TestRunners/UnitTestDatabase.h" - -namespace PokemonAutomation{ -namespace Pokemon{ - - -// Registers the tests for parsing, printing and ordering checkpoint ids. -void add_tests_CheckpointId(UnitTestDatabase& database); - - - -} // namespace Pokemon -} // namespace PokemonAutomation -#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 27c3fe8e61..d0f70a0ddf 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1503,10 +1503,6 @@ 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/AutoStory/Pokemon_CheckpointId.cpp - Source/Pokemon/AutoStory/Pokemon_CheckpointId.h - Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.cpp - Source/Pokemon/AutoStory/Pokemon_CheckpointId_Tests.h Source/Pokemon/Pokemon_AdvRng.cpp Source/Pokemon/Pokemon_AdvRng.h Source/Pokemon/Pokemon_BdspRng.cpp