diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h index 3b2328fa87..075f80f255 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_AutoStory_Tests.h @@ -15,6 +15,7 @@ #include "Pokemon_StoryFailureClasses_Tests.h" #include "Pokemon_StoryLog_Tests.h" #include "Types/Pokemon_StoryActionArg_Tests.h" +#include "Types/Pokemon_StoryRunTypes_Tests.h" namespace PokemonAutomation{ namespace Pokemon{ @@ -25,6 +26,7 @@ inline void add_tests_AutoStory(UnitTestDatabase& database){ add_tests_StoryActionArg(database); add_tests_StoryLog(database); add_tests_StoryFailureClasses(database); + add_tests_StoryRunTypes(database); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.cpp new file mode 100644 index 0000000000..92576991ec --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.cpp @@ -0,0 +1,32 @@ +/* Story Checkpoint Types + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryCheckpointTypes.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +void apply_interrupt_pose(InterruptPose effect, Pose& pose){ + switch (effect){ + case InterruptPose::UNCHANGED: + return; + case InterruptPose::WITHIN_SET: + if (pose.certainty != PoseCertainty::UNKNOWN){ + pose.certainty = PoseCertainty::SET; + } + return; + case InterruptPose::LOST: + pose.where.reset(); + pose.certainty = PoseCertainty::UNKNOWN; + return; + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h new file mode 100644 index 0000000000..213b911eb5 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h @@ -0,0 +1,90 @@ +/* Story Checkpoint Types + * + * From: https://github.com/PokemonAutomation/ + * + * A checkpoint as the event runner sees it: its events, the interrupts + * that can stop one part-way, its start and end checks, and the report of + * a run of it. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpointTypes_H +#define PokemonAutomation_Pokemon_StoryCheckpointTypes_H + +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryLog.h" +#include "Pokemon_StoryEvent.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Pose confidence adjustment following an event interruption. +enum class InterruptPose{ + UNCHANGED, // Cleared where we stood (a talk-only trainer). + WITHIN_SET, // Moved an unknown part of a leg: still within the position we had, no longer exactly where. + LOST, // Anywhere. +}; +// Policy defining how an interrupted event should be resumed. +enum class ResumePolicy{ + RETRY_EVENT, // Run the event again, from its skip_if. + RECHECK_EXPECT, // The action may have finished: done if its expects hold, else retry. + STOP, // Stop the checkpoint. +}; + +// Something that can stop an action part-way. +struct Interrupt{ + std::string name; + Check detect; + Act handle; + InterruptPose pose = InterruptPose::UNCHANGED; + ResumePolicy resume = ResumePolicy::RETRY_EVENT; +}; +// Writes what an interrupt did (`effect`) into `pose`. +void apply_interrupt_pose(InterruptPose effect, Pose& pose); + + +// The stretch of story between two saves, as events. +struct Checkpoint{ + std::string id; + // Must hold before the first event (the save it starts from). + std::vector start; + std::vector interrupts; + std::vector events; + // Must hold after the last event. + std::vector end; + // Saves the game after the end check, if set. + Act save; + // More interrupts than this in one event (wild battles, trainers) is + // a loop, not bad luck: the event fails as "too_many_interrupts". + size_t max_interrupts_per_event = 5; +}; + + +// Result report from executing a checkpoint. +struct CheckpointReport{ + bool completed = false; + // 1-based number of the event that failed; 0 for the start or end check + // or the save, or when nothing failed. + size_t failed_event = 0; + std::string failed_name; + Outcome failure; + size_t events_run = 0; + size_t events_skipped = 0; +}; + +// Where the runner writes its log lines. +using EventLogger = MoveOnlyFunction; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp new file mode 100644 index 0000000000..cd5e892b8f --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp @@ -0,0 +1,41 @@ +/* Story Event + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_StoryEvent.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +Condition condition(std::string name, Check check){ + return Condition{std::move(name), std::move(check)}; +} + +Condition pose_known(std::string map, PoseCertainty need){ + std::string name = std::format("pose {}{}", certainty_name(need), map.empty() ? "" : std::format(" on {}", map)); + return condition(std::move(name), [map = std::move(map), need](RunState& state){ + return (map.empty() || state.pose.map == map) && certainty_covers(state.pose.certainty, need); + }); +} + +std::string recovery_name(RecoveryKind kind){ + switch (kind){ + case RecoveryKind::WAIT: + return "wait"; + case RecoveryKind::REANCHOR: + return "re-anchor"; + case RecoveryKind::STOP: + return "stop"; + } + return "?"; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h new file mode 100644 index 0000000000..c23bbb85fc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Pokemon_StoryEvent.h @@ -0,0 +1,92 @@ +/* Story Event + * + * From: https://github.com/PokemonAutomation/ + * + * One step of a checkpoint as the event runner sees it: what it checks + * before and after its action, when it is already done, and how a failure + * of it is answered. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryEvent_H +#define PokemonAutomation_Pokemon_StoryEvent_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include "Common/Cpp/MoveOnlyFunction.h" +#include "Pokemon_StoryLog.h" +#include "Pokemon_StoryOutcome.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// A question about the run state. +using Check = MoveOnlyFunction; + +// A question the screen (or the run state) answers, named for the log. +struct Condition{ + std::string name; + Check check; +}; +// `check`, named `name` in the log. +Condition condition(std::string name, Check check); + +// Holds while the pose is on `map` (any map if empty) and at least as +// certain as `need`. +Condition pose_known(std::string map, PoseCertainty need); + + +// How a failed event is answered. +enum class RecoveryKind{ + WAIT, // Something transient (a wanderer, a slow scene): wait, try again. + REANCHOR, // The pose is in doubt: confirm it on a landmark, try again. + STOP, // Nothing to do: stop the checkpoint. +}; +// "wait", "re-anchor", "stop": for log lines. +std::string recovery_name(RecoveryKind kind); + +// Answers a failure named `on` (any failure if empty): runs `prepare`, if +// any, and tries the event again, up to `attempts` times. +struct Recovery{ + std::string on; + RecoveryKind kind = RecoveryKind::STOP; + Act prepare; + size_t attempts = 1; + std::string why; +}; + + +// One step of a checkpoint: an action with the conditions around it. +struct Event{ + std::string name; + std::string why; + // Skipped when all of these hold (and there is at least one). + std::vector skip_if; + // Run when the event is skipped: what the event leaves in the run + // state that is already so (a door it opens, a flag it sets). + std::function when_skipped; + // Must hold before `act`; a failure is "require_failed". + std::vector require; + Act act; + // Must hold after `act`; a failure is "expect_failed". + std::vector expect; + // Written to the run's pose once the event succeeds. An action that + // learns its pose as it goes (the row that answered) returns it in + // its Outcome instead. + std::optional pose_learned; + // In order of escalation. + std::vector recoveries; +}; + + + +} // 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_StoryBreakpoints.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.cpp new file mode 100644 index 0000000000..929cc8543e --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.cpp @@ -0,0 +1,78 @@ +/* Story Breakpoints + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include "Pokemon_StoryBreakpoints.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_step_pause_label(std::string_view checkpoint_id, size_t step_index, std::string_view step_name){ + return std::format("{}/{} {}", checkpoint_id, step_index + 1, step_name); +} + +std::string story_leg_pause_label( + size_t index, size_t count, Direction direction, int tiles, const std::optional& destination +){ + std::string label = std::format("leg {}/{} {} {}", index + 1, count, direction_name(direction), tiles); + if (destination){ + label += std::format(" -> ({}, {})", destination->x, destination->y); + } + return label; +} + + +StoryPausePoints::StoryPausePoints(StoryBreakpoints mode, StoryPauseHook hook) + : m_mode(mode) + , m_hook(std::move(hook)) +{} + +void StoryPausePoints::set_log(std::function log){ + m_log = std::move(log); +} + +void StoryPausePoints::before_step(std::string_view label){ + m_step_label = label; + if (m_mode != StoryBreakpoints::OFF){ + pause(label); + } +} + +void StoryPausePoints::before_leg(std::string_view leg_label){ + if (m_mode == StoryBreakpoints::LEGS){ + pause(std::format("{} {}", m_step_label, leg_label)); + } +} + +void StoryPausePoints::pause(std::string_view label){ + if (!m_hook){ + return; + } + if (m_log){ + m_log(std::format("[Break] {}: paused", label)); + } + m_hook(label); + if (m_log){ + m_log(std::format("[Break] {}: continued", label)); + } +} + + +StoryPauseHook make_story_pause_hook(StoryPauseWait wait, CancellableScope& scope){ + if (!wait){ + return {}; + } + return [wait = std::move(wait), &scope](std::string_view label){ + wait(label, scope); + }; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h new file mode 100644 index 0000000000..69133a8984 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h @@ -0,0 +1,93 @@ +/* Story Breakpoints + * + * From: https://github.com/PokemonAutomation/ + * + * A debugger's breakpoints for a story run: the program pauses before each + * step, or before each leg of a walk too, so the user can look at the + * console and then continue. The engine and the walk action call + * StoryPausePoints at those points; what a pause does is the game's + * StoryGame::pause_point hook. A program that passes no wait to + * make_story_pause_hook() gets no hook, so its pause points never block. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryBreakpoints_H +#define PokemonAutomation_Pokemon_StoryBreakpoints_H + +#include +#include +#include +#include +#include "Common/Cpp/CancellableScope.h" +#include "Pokemon/Navigation/Pokemon_Grid.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Where a run pauses. +enum class StoryBreakpoints{ + // Nowhere. + OFF, + // Before each step. + STEPS, + // Before each step and each leg of a walk. + LEGS, +}; + +// "12.03/5 pallet_shore": the checkpoint, the step's place in it (from 1) +// and its name. +std::string story_step_pause_label(std::string_view checkpoint_id, size_t step_index, std::string_view step_name); + +// "leg 2/5 east 4 -> (10, 10)": leg `index` (from 0) of `count`, and the +// tile it ends on when the walk started on one known tile. +std::string story_leg_pause_label( + size_t index, size_t count, Direction direction, int tiles, const std::optional& destination +); + + +// What a pause point does when it is reached, given its full label. It +// returns when the run goes on, or when `scope` is cancelled. +using StoryPauseHook = std::function; + +// The pause points of one run. The engine owns it and hands it to actions. +class StoryPausePoints{ +public: + StoryPausePoints() = default; + StoryPausePoints(StoryBreakpoints mode, StoryPauseHook hook); + + // Where the engine logs "[Break]" lines. + void set_log(std::function log); + + // A step is about to run. Remembers `label` for the legs of the step + // and pauses when the mode asks for steps. + void before_step(std::string_view label); + // A leg is about to be walked: pauses, as " ", + // only in LEGS mode. + void before_leg(std::string_view leg_label); + // The label of the step under way ("12.03/5 pallet_shore"); empty before the first. + const std::string& step_label() const{ return m_step_label; } + +private: + void pause(std::string_view label); + + StoryBreakpoints m_mode = StoryBreakpoints::OFF; + StoryPauseHook m_hook; + std::function m_log; + std::string m_step_label; +}; + + +// A wait for the user, called with the label and the run's scope. It +// returns when the user continues or `scope` is cancelled. +using StoryPauseWait = std::function; + +// The hook for StoryGame::pause_point: `wait` on `scope`, or no hook when +// `wait` is empty. `scope` must outlive the hook. +StoryPauseHook make_story_pause_hook(StoryPauseWait wait, CancellableScope& scope); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h new file mode 100644 index 0000000000..21de004f19 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h @@ -0,0 +1,48 @@ +/* Story Checkpoint + * + * From: https://github.com/PokemonAutomation/ + * + * A checkpoint of a tile game's story table as plain data: where its save + * leaves the player and its steps in order. build_story_checkpoint() + * (Pokemon_StoryStepExecutor.h) turns it into engine events. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpoint_H +#define PokemonAutomation_Pokemon_StoryCheckpoint_H + +#include +#include +#include +#include "Pokemon_DeclaredPose.h" +#include "Pokemon_StoryCheckpointMetadata.h" +#include "Pokemon_StoryPostconditions.h" +#include "Pokemon_StoryStep.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Milestone row in the story table grouping related steps and starting pose. +struct StoryCheckpoint{ + std::string id; + std::string title; + // Where its starting save leaves the player; none for a new game. + std::optional start; + std::vector steps; + // The steps' JSON ("events") as read, dumped compactly; what + // story_steps_hash() hashes. Empty for a checkpoint built in code. + std::string steps_json = {}; + // What must hold when it is done; checked on its own after a run. + // JSON: "metadata": {"postconditions": ...}. + StoryPostconditions postconditions = {}; + // Its phase and verification (JSON "metadata"), which decide when a + // run of it may save. + StoryCheckpointMetadata metadata = {}; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.cpp new file mode 100644 index 0000000000..fb6eac85fd --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.cpp @@ -0,0 +1,37 @@ +/* Story Checkpoint Metadata + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include "Pokemon_StoryCheckpointMetadata.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +std::string story_phase_name(StoryPhase phase){ + switch (phase){ + case StoryPhase::TESTING: + return "testing"; + case StoryPhase::VERIFICATION: + return "verification"; + case StoryPhase::REPRODUCTION: + return "reproduction"; + } + return "?"; +} + +std::optional read_story_phase(std::string_view name){ + for (StoryPhase phase : {StoryPhase::TESTING, StoryPhase::VERIFICATION, StoryPhase::REPRODUCTION}){ + if (story_phase_name(phase) == name){ + return phase; + } + } + return std::nullopt; +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h new file mode 100644 index 0000000000..b2e467859a --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h @@ -0,0 +1,69 @@ +/* Story Checkpoint Metadata + * + * From: https://github.com/PokemonAutomation/ + * + * What a checkpoint's JSON says about the checkpoint rather than how to + * play it: its phase (testing, verification or reproduction, below), the + * steps it was verified with, and where it came from. The engine picks + * the save mode from the phase, and only while the steps still hash to + * the verified ones (Engine/Pokemon_StoryEnginePolicy.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryCheckpointMetadata_H +#define PokemonAutomation_Pokemon_StoryCheckpointMetadata_H + +#include +#include +#include + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Where a checkpoint stands. It moves forward only on passes; a change to +// its steps moves it back to TESTING. +enum class StoryPhase{ + // New or changed: one run, never saves. + TESTING, + // Passed its test: 2-3 runs from the same start save, then one save. + VERIFICATION, + // Verified: a playthrough saves at its end. + REPRODUCTION, +}; + +// The wire and log name of a phase: "testing", "verification", "reproduction". +std::string story_phase_name(StoryPhase phase); + +// The phase named `name`; none for another name. +std::optional read_story_phase(std::string_view name); + +// The runs that moved the checkpoint out of testing, as recorded. +struct StoryVerification{ + // story_steps_hash() of the steps those runs ran: "sha256:". + std::string steps_hash = {}; + // The build those runs used (a commit). + std::string build = {}; + std::string date = {}; + // Passes out of runs, e.g. "3/3". + std::string runs = {}; + + bool operator==(const StoryVerification& other) const = default; +}; + +// A checkpoint's "metadata" object, less its postconditions, which the +// checkpoint keeps beside its steps (StoryCheckpoint::postconditions). +struct StoryCheckpointMetadata{ + StoryPhase phase = StoryPhase::TESTING; + std::optional verified = std::nullopt; + // Where the steps came from: a walkthrough, a console recording. + std::string source = {}; + + bool operator==(const StoryCheckpointMetadata& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h new file mode 100644 index 0000000000..5b3cd7df37 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h @@ -0,0 +1,43 @@ +/* Story Postconditions + * + * From: https://github.com/PokemonAutomation/ + * + * What must hold once a checkpoint is done, as plain data from its plan: + * where the player stands, what the Bag holds, which story flags moved. + * check_postconditions() (Pokemon_StoryPostconditionCheck.h) checks them + * against what the game read, as a step of its own after the run. + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryPostconditions_H +#define PokemonAutomation_Pokemon_StoryPostconditions_H + +#include +#include +#include +#include "Pokemon_GridPose.h" +#include "Pokemon_StoryActionArg.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// The end state a checkpoint's plan promises: position, Bag items and flags. +struct StoryPostconditions{ + // Where the checkpoint ends; unset, it is the last step's target + // (checkpoint_end_pose()). + std::optional position; + // Each item's count in the Bag, exactly, by the game's item id + // ("LIFT_KEY", "RARE_CANDY"). + std::vector items; + // Story flags that must be set: a trainer beaten, a door open. + std::vector flags; + + bool operator==(const StoryPostconditions& other) const = default; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp new file mode 100644 index 0000000000..1e3a662720 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp @@ -0,0 +1,178 @@ +/* Story Run Types Tests + * + * From: https://github.com/PokemonAutomation/ + * + * The phase names a checkpoint's metadata uses + * (Pokemon_StoryCheckpointMetadata.h) and where a run pauses + * (Pokemon_StoryBreakpoints.h). + * + */ + +#include +#include +#include +#include +#include "Common/Cpp/TestRunners/UnitTest.h" +#include "Pokemon_StoryBreakpoints.h" +#include "Pokemon_StoryCheckpointMetadata.h" +#include "Pokemon_StoryRunTypes_Tests.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +namespace{ + + +// A named check that returns its own failure. +struct NamedCheck{ + std::string_view name; + UnitTestResult (*check)(CancellableScope& scope); +}; + +class Test_NamedCheck : public UnitTest{ +public: + explicit Test_NamedCheck(NamedCheck check) + : UnitTest(std::format("Pokemon::StoryRunTypes - {}", check.name)) + , m_check(check) + {} + + virtual UnitTestResult run(Logger&, CancellableScope& scope) const override{ + return m_check.check(scope); + } + +private: + const NamedCheck m_check; +}; + + +UnitTestResult phase_names_round_trip(CancellableScope&){ + for (StoryPhase phase : {StoryPhase::TESTING, StoryPhase::VERIFICATION, StoryPhase::REPRODUCTION}){ + if (read_story_phase(story_phase_name(phase)) != phase){ + return std::format("\"{}\" does not read back", story_phase_name(phase)); + } + } + if (read_story_phase("Testing") || read_story_phase("")){ + return std::string("read a phase from a name that is none"); + } + return true; +} + +std::string joined(const std::vector& lines){ + std::string text; + for (const std::string& line : lines){ + text += "\n " + line; + } + return text; +} + +// The labels each pause point paused on, in `mode`: one step with two legs. +std::vector paused_on(StoryBreakpoints mode){ + std::vector paused; + StoryPausePoints points(mode, [&](std::string_view label){ + paused.emplace_back(label); + }); + points.before_step("12.03/1 first"); + points.before_leg("leg 1/2 east 3"); + points.before_leg("leg 2/2 north 1"); + points.before_step("12.03/2 second"); + return paused; +} + +UnitTestResult pause_points_by_mode(CancellableScope&){ + if (!paused_on(StoryBreakpoints::OFF).empty()){ + return std::format("OFF paused:{}", joined(paused_on(StoryBreakpoints::OFF))); + } + const std::vector steps = {"12.03/1 first", "12.03/2 second"}; + if (paused_on(StoryBreakpoints::STEPS) != steps){ + return std::format("STEPS paused:{}", joined(paused_on(StoryBreakpoints::STEPS))); + } + const std::vector legs = { + "12.03/1 first", "12.03/1 first leg 1/2 east 3", "12.03/1 first leg 2/2 north 1", "12.03/2 second", + }; + if (paused_on(StoryBreakpoints::LEGS) != legs){ + return std::format("LEGS paused:{}", joined(paused_on(StoryBreakpoints::LEGS))); + } + return true; +} + +UnitTestResult pause_points_log_and_keep_the_label(CancellableScope&){ + std::vector lines; + StoryPausePoints points(StoryBreakpoints::STEPS, [](std::string_view){}); + points.set_log([&](std::string_view line){ + lines.emplace_back(line); + }); + points.before_step("12.03/1 first"); + const std::vector expected = {"[Break] 12.03/1 first: paused", "[Break] 12.03/1 first: continued"}; + if (lines != expected){ + return std::format("unexpected log:{}", joined(lines)); + } + if (points.step_label() != "12.03/1 first"){ + return std::format("step label \"{}\"", points.step_label()); + } + StoryPausePoints no_hook(StoryBreakpoints::LEGS, {}); + no_hook.set_log([&](std::string_view line){ + lines.emplace_back(line); + }); + no_hook.before_step("12.03/2 second"); + if (lines.size() != expected.size() || no_hook.step_label() != "12.03/2 second"){ + return std::format("a pause point with no hook logged or lost its label:{}", joined(lines)); + } + return true; +} + +UnitTestResult pause_hook_from_a_wait(CancellableScope& scope){ + if (make_story_pause_hook({}, scope)){ + return std::string("expected no hook without a wait"); + } + std::vector waited; + const StoryPauseHook hook = make_story_pause_hook([&](std::string_view label, CancellableScope& with){ + waited.push_back(std::format("{} {}", label, &with == &scope)); + }, scope); + if (!hook){ + return std::string("expected a hook for a wait"); + } + hook("12.03/1 first"); + if (waited != std::vector{"12.03/1 first true"}){ + return std::format("expected the wait to get the label and the scope:{}", joined(waited)); + } + return true; +} + +UnitTestResult pause_labels(CancellableScope&){ + if (story_step_pause_label("12.03", 4, "pallet_shore") != "12.03/5 pallet_shore"){ + return story_step_pause_label("12.03", 4, "pallet_shore"); + } + const std::string with_end = story_leg_pause_label(1, 5, Direction::EAST, 4, TilePos{10, 10}); + if (with_end != "leg 2/5 east 4 -> (10, 10)"){ + return with_end; + } + const std::string without_end = story_leg_pause_label(0, 1, Direction::NORTH, 2, std::nullopt); + if (without_end != "leg 1/1 north 2"){ + return without_end; + } + return true; +} + +const NamedCheck CHECKS[] = { + {.name = "phase names round-trip", .check = phase_names_round_trip}, + {.name = "pause points by mode", .check = pause_points_by_mode}, + {.name = "pause points log and keep the label", .check = pause_points_log_and_keep_the_label}, + {.name = "pause hook from a wait", .check = pause_hook_from_a_wait}, + {.name = "pause labels", .check = pause_labels}, +}; + + +} // namespace + + +void add_tests_StoryRunTypes(UnitTestDatabase& database){ + for (const NamedCheck& check : CHECKS){ + database.add(check); + } +} + + + +} // namespace Pokemon +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h new file mode 100644 index 0000000000..69cf6cfcfc --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h @@ -0,0 +1,24 @@ +/* Story Run Types Tests + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryRunTypes_Tests_H +#define PokemonAutomation_Pokemon_StoryRunTypes_Tests_H + +#include "Common/Cpp/TestRunners/UnitTestDatabase.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Registers the tests of the save decision, the phase names and the pause +// points. +void add_tests_StoryRunTypes(UnitTestDatabase& database); + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h new file mode 100644 index 0000000000..4500111b71 --- /dev/null +++ b/SerialPrograms/Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h @@ -0,0 +1,60 @@ +/* Story Step + * + * From: https://github.com/PokemonAutomation/ + * + * One step of a tile game's story (FRLG, BDSP) as plain data: its kind, + * the action that does it and the action's args. A game's executor does it + * (Pokemon_StoryStepExecutor.h); the shared JSON reads and writes it + * (Data/Pokemon_StoryStepJson.h). + * + */ + +#ifndef PokemonAutomation_Pokemon_StoryStep_H +#define PokemonAutomation_Pokemon_StoryStep_H + +#include +#include +#include +#include "Pokemon_DeclaredPose.h" +#include "Pokemon_StoryActionArg.h" +#include "Pokemon_StoryStepKind.h" + +namespace PokemonAutomation{ +namespace Pokemon{ + + +// Specification of an atomic story task with actions and postconditions. +struct StoryStep{ + StoryStepKind kind = StoryStepKind::TALK; + std::string name; + std::string why; + + // How the step does its kind: one of the built-in actions + // (Pokemon_BuiltinStoryActions.h) or the game's own; for a GAME step, the + // game's action. Empty is the kind's default action. + std::string action; + // The arguments the action reads, parsed against its spec when the + // game loads the step, with the spec's fallbacks filled in. + StoryActionArgs args; + + // Skipped when all of these hold (and there is at least one): names + // the game's executor answers (StoryStepExecutor::holds()), run flags + // unless the game reads them off the screen. + std::vector skip_if; + // Preparation for what follows, done once per save: a TEACH, a check, + // a read. Once it has succeeded and changed nothing + // (Outcome::changed_nothing), the attempts after a reload of the same + // save skip it, so a retry goes from the reload straight to the + // battle. One that changed the game (a TM taught) runs again, as the + // reload undid it. Only with build_story_checkpoint()'s PreparedSteps. + bool once_per_save = false; + // Where the step leaves the player, when its action does not say: the + // end of a scene that moves the player. + std::optional pose_after; +}; + + + +} // namespace Pokemon +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index 88c0cc4525..109a114c9e 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -1449,6 +1449,10 @@ 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/Pokemon_StoryCheckpointTypes.cpp + Source/Pokemon/AutoStory/Pokemon_StoryCheckpointTypes.h + Source/Pokemon/AutoStory/Pokemon_StoryEvent.cpp + Source/Pokemon/AutoStory/Pokemon_StoryEvent.h Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.cpp Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses.h Source/Pokemon/AutoStory/Pokemon_StoryFailureClasses_Tests.cpp @@ -1461,6 +1465,7 @@ file(GLOB LIBRARY_SOURCES Source/Pokemon/AutoStory/Pokemon_StoryOutcome.h Source/Pokemon/AutoStory/Pokemon_StoryPose.cpp Source/Pokemon/AutoStory/Pokemon_StoryPose.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 @@ -1468,6 +1473,15 @@ file(GLOB LIBRARY_SOURCES 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_StoryBreakpoints.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryBreakpoints.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpoint.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryCheckpointMetadata.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryPostconditions.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.cpp + Source/Pokemon/AutoStory/Types/Pokemon_StoryRunTypes_Tests.h + Source/Pokemon/AutoStory/Types/Pokemon_StoryStep.h Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.cpp Source/Pokemon/AutoStory/Types/Pokemon_StoryStepKind.h Source/Pokemon/Inference/Pokemon_AbilityReader.cpp