diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp index 9313165a6e..3f09cfaeb6 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.cpp @@ -82,6 +82,7 @@ StoryActionRegistry::StoryActionRegistry(const StoryGame& ga : m_game(game) , m_actions(builtin_story_handlers()) , m_game_problems(tile_arg_problems(game)) + , m_pause(game.breakpoints, game.pause_point) { // A game's action replaces the built-in of its kind and name in place, // so a kind's default stays the built-in one's position. @@ -172,7 +173,7 @@ Outcome StoryActionRegistry::dispatch(StoryBackend& backend, )); } const StoryActionArgs args = with_fallbacks(action->spec, step.args); - return action->handler(backend, StoryActionCall{.step = step, .args = args}, state); + return action->handler(backend, StoryActionCall{.step = step, .args = args, .pause = m_pause}, state); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h index f8adbc6c6f..7c2e923acf 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryActionRegistry.h @@ -58,6 +58,12 @@ class StoryActionRegistry{ // actions fails too. std::vector lint(const StoryCheckpoint& checkpoint) const; + // The pause points of this game's run (StoryGame::breakpoints and + // pause_point). Mutable state of the run, not of the game's actions. + StoryPausePoints& pause_points() const{ + return m_pause; + } + // Runs `step`'s action with its args' fallbacks filled in; a step with // no registered action fails as unsupported_step(), a console_only // action on another backend as CONSOLE_ONLY, before any input. @@ -73,6 +79,7 @@ class StoryActionRegistry{ std::vector m_specs; // What is wrong with the game's own actions, reported with every lint. std::vector m_game_problems; + mutable StoryPausePoints m_pause; }; // Instantiated once, in Pokemon_StoryActionRegistry.cpp. diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp index fb3d51c36c..63ca85527d 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryBuiltinActions.cpp @@ -101,7 +101,15 @@ inline std::optional after_legs(const Pose& start, const std::vector Outcome walk_legs(StoryBackend& backend, const StoryActionCall& call, RunState& state){ const std::vector& legs = story_arg>(call.args, "legs"); - for (const StoryLeg& leg : legs){ + const std::vector start = tiles_of(state.pose); + std::optional tile = start.size() == 1 ? std::optional(start[0]) : std::nullopt; + for (size_t index = 0; index < legs.size(); index++){ + const StoryLeg& leg = legs[index]; + for (int c = 0; tile && c < leg.tiles; c++){ + tile = tile->next(leg.direction); + } + call.pause.before_leg(story_leg_pause_label(index, legs.size(), leg.direction, leg.tiles, tile)); + backend.scope().throw_if_cancelled(); for (int c = 0; c < leg.tiles; c++){ backend.tap(leg.direction); } diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp index b874795b8a..a1b502062c 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngine.cpp @@ -26,17 +26,20 @@ namespace{ template class RunnerExecutor : public StoryStepExecutor{ public: - RunnerExecutor(StoryStepRunner& runner, const StoryGame& game) + RunnerExecutor(StoryStepRunner& runner, const StoryGame& game, StoryPausePoints& pause) : m_runner(runner) , m_game(game) + , m_pause(pause) {} virtual void begin_attempt(const StoryCheckpoint& data) override{ + m_checkpoint = &data; if (m_game.begin_attempt){ m_game.begin_attempt(data); } } virtual Outcome run(const StoryStep& step, RunState& state) override{ + m_pause.before_step(pause_label(step)); return m_runner.act(step, state); } virtual bool holds(std::string_view condition, RunState& state) override{ @@ -44,8 +47,22 @@ class RunnerExecutor : public StoryStepExecutor{ } private: + // The step's place in its checkpoint (step names are unique, which the + // lint checks). + std::string pause_label(const StoryStep& step) const{ + size_t index = 0; + if (m_checkpoint != nullptr){ + while (index < m_checkpoint->steps.size() && m_checkpoint->steps[index].name != step.name){ + index++; + } + } + return story_step_pause_label(m_checkpoint == nullptr ? "" : m_checkpoint->id, index, step.name); + } + StoryStepRunner& m_runner; const StoryGame& m_game; + StoryPausePoints& m_pause; + const StoryCheckpoint* m_checkpoint = nullptr; }; // `game` with its SAVE step "save" saving `current` only while `mode` is @@ -167,7 +184,11 @@ StoryEngine::StoryEngine(const StoryGame& game, StoryBackend , m_backend(backend) , m_registry(m_game) , m_runner(m_registry, backend) -{} +{ + m_registry.pause_points().set_log([&backend](std::string_view line){ + backend.log(line); + }); +} template std::string StoryEngine::steps_hash(const StoryCheckpoint& checkpoint) const{ @@ -346,7 +367,7 @@ VerifyReport StoryEngine::verify_checkpoint(const StoryCheckpoint& che template CheckpointReport StoryEngine::run_steps(const StoryCheckpoint& checkpoint, RunState& state){ - RunnerExecutor executor(m_runner, m_game); + RunnerExecutor executor(m_runner, m_game, m_registry.pause_points()); Checkpoint events = build_story_checkpoint(checkpoint, executor, &m_prepared); if (m_game.interrupts){ events.interrupts = m_game.interrupts(m_backend); diff --git a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp index bbed998383..851f380bcc 100644 --- a/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp +++ b/SerialPrograms/Source/Pokemon/AutoStory/Engine/Pokemon_StoryEngineRuns_Tests.cpp @@ -746,6 +746,143 @@ std::string verify_saves_after_all_passed(CancellableScope& scope){ } +// A game whose "mark" action records "run " and whose pause hook +// records "pause