diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp index 9871501ec0..721d967c35 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.cpp @@ -21,13 +21,14 @@ TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const const WallClock deadline = current_time() + TALK_OVER_LIMIT; TalkOver judge(spec.rule); BdspScreenObserver eyes(spec.watch == TalkOverWatch::TRAINER ? BattleCue::TRAINER : BattleCue::WILD_MENU, spec.banner); + bool saw_dialogue = false; while (current_time() < deadline){ context.wait_for_all_requests(); const WallClock now = current_time(); switch (judge.look(eyes.observe(stream.video().snapshot(), now), now)){ case SceneMove::DONE: stream.log(std::format("[Talk-over] {}: over; {}.", label, judge.describe())); - return TalkOverEnd::QUIET; + return saw_dialogue ? TalkOverEnd::DIALOGUE : TalkOverEnd::QUIET; case SceneMove::BATTLE: if (spec.watch == TalkOverWatch::TRAINER){ stream.log("[Talk-over] " + label + ": a battle started."); @@ -37,6 +38,7 @@ TalkOverEnd talk_over(VideoStream& stream, ProControllerContext& context, const run_from_wild_battle(stream, context); continue; case SceneMove::NEXT: + saw_dialogue = true; pbf_press_button(context, spec.next, TALK_OVER_HOLD, talk_over_release(spec.rule)); continue; case SceneMove::B: diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h index 9469c3954c..96a3e80c70 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_TalkOver.h @@ -73,7 +73,8 @@ struct TalkOverSpec{ // How talk_over() ended. enum class TalkOverEnd{ - QUIET, + QUIET, // Quiet without observing dialogue. + DIALOGUE, // Quiet after pressing through dialogue. BATTLE, // Only with TalkOverWatch::TRAINER. }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp index 84dda41bbf..87ca9f1146 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Actions/PokemonBDSP_AutoStory_Talks.cpp @@ -60,7 +60,7 @@ bool talk_or_fight(VideoStream& stream, ProControllerContext& context, const std .rule = {.end_on_quiet = expect != TalkExpect::BATTLE}, .watch = TalkOverWatch::TRAINER, }); - if (end == TalkOverEnd::QUIET){ + if (end != TalkOverEnd::BATTLE){ route_status(stream, label + " only talked."); return false; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp index 3f8104aae0..e9f707f194 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_RecoveryWalk_Tests.cpp @@ -97,7 +97,7 @@ class RecoveryStepIO : public StoryStepIO{ virtual bool run_until_map(Direction, Milliseconds, const std::string&, const std::string&) override{ throw GridSimFailure{"unexpected banner walk"}; } virtual WarpSeen run_into_door(Direction, bool, Milliseconds = Milliseconds(12000), Milliseconds = Milliseconds(0), const std::string& = "") override{ throw GridSimFailure{"unexpected door"}; } virtual std::optional tap_to_banner(Direction, const std::string&, size_t) override{ throw GridSimFailure{"unexpected banner"}; } - virtual void settle(Button, const std::string&, SettleKind, const std::string&) override{ throw GridSimFailure{"unexpected scene"}; } + virtual bool settle(Button, const std::string&, SettleKind, const std::string&) override{ throw GridSimFailure{"unexpected scene"}; } private: RouteIO& m_route; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp index 6e8da5bcb9..66fd17b61f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryActions.cpp @@ -189,12 +189,14 @@ void StoryLogBackend::log(std::string_view line){ // The game first: the registry and the runner keep references to it. struct StoryEngineSteps::Engine{ - Engine(StoryWalkSteps& walks, const StoryBoundActions& bound, Pokemon::StoryBackend& backend) - : game(story_game(walks, bound)) + Engine(StoryWalkSteps& walk_steps, const StoryBoundActions& bound, Pokemon::StoryBackend& backend) + : walks(walk_steps) + , game(story_game(walk_steps, bound)) , registry(game) , runner(registry, backend) {} + StoryWalkSteps& walks; Pokemon::StoryGame game; Pokemon::StoryActionRegistry registry; Pokemon::StoryStepRunner runner; @@ -207,6 +209,7 @@ StoryEngineSteps::StoryEngineSteps(StoryWalkSteps& walks, const StoryBoundAction StoryEngineSteps::~StoryEngineSteps() = default; Outcome StoryEngineSteps::act(const StoryStep& step, RunState& state){ + m_engine->walks.begin_step(step); return m_engine->runner.act(step, state); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp index 300ebdbf54..540b24023f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.cpp @@ -309,9 +309,9 @@ std::optional ConsoleStoryStepIO::tap_to_banner(Direction direction, con // beside the door and walked a whole route in the house. The far map's // banner `banner`, if it has one, counts as the fade: a fade the run // missed is over, but its banner comes after it (#268 slice 1). -void ConsoleStoryStepIO::settle(Button button, const std::string& label, SettleKind kind, const std::string& banner){ +bool ConsoleStoryStepIO::settle(Button button, const std::string& label, SettleKind kind, const std::string& banner){ const TalkOverRule rule{.warp = kind == SettleKind::AFTER_WARP, .door = kind == SettleKind::DOOR}; - talk_over(m_stream, m_context, label, {.next = button, .rule = rule, .banner = banner}); + return talk_over(m_stream, m_context, label, {.next = button, .rule = rule, .banner = banner}) == TalkOverEnd::DIALOGUE; } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h index 3db291d727..d9377436cb 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryStepIO.h @@ -88,7 +88,8 @@ class StoryStepIO{ // Presses through any text until the overworld ends as `kind` says; // for SettleKind::AFTER_WARP, the warp is a fade or the banner `banner` // ("" for a fade only). Fails otherwise. - virtual void settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) = 0; + // Returns whether the settle observed and pressed through dialogue. + virtual bool settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) = 0; }; @@ -106,7 +107,7 @@ class ConsoleStoryStepIO : public StoryStepIO{ virtual WarpSeen run_into_door(Pokemon::Direction direction, bool run, Milliseconds timeout = Milliseconds(12000), Milliseconds dark_from = Milliseconds(0), const std::string& banner = "") override; virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) override; - virtual void settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) override; + virtual bool settle(Button button, const std::string& label, SettleKind kind, const std::string& banner) override; private: diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp index 8472b10b0c..e961df21c3 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.cpp @@ -87,6 +87,10 @@ std::vector traced_tiles(const std::string& map, const std:: namespace{ +// How long a scene waits for its first line when the preceding step did not +// press through its dialogue. +constexpr Milliseconds SCENE_START_WAIT = 15000ms; + // "to_catch_grass" as "to catch grass", for the logs. std::string label_of(const StoryStep& step){ std::string label = step.name; @@ -180,6 +184,8 @@ void StoryWalkSteps::begin_attempt(const std::string& row, const std::vector 0){ return Pokemon::outcome_failed("interrupted_step", step.name); } - m_io.settle(BUTTON_B, label_of(step), SettleKind::TALK, ""); + m_previous_settle_saw_dialogue = m_io.settle(BUTTON_B, label_of(step), SettleKind::TALK, ""); return Pokemon::outcome_ok(); } // A step through a door: walk (or run) on until the screen goes dark, @@ -672,7 +683,7 @@ Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ // slice 1); with neither yet, settle() waits for one. if (step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map){ if (through_already(step)){ - m_io.settle(BUTTON_B, label_of(step), SettleKind::TALK, ""); + m_previous_settle_saw_dialogue = m_io.settle(BUTTON_B, label_of(step), SettleKind::TALK, ""); return Pokemon::outcome_ok(); } const std::string banner = story_arrival_banner(state.pose.map, Pokemon::grid_pose_of(*step.pose_after).map); @@ -681,7 +692,9 @@ Outcome StoryWalkSteps::step(RunState& state, const StoryStep& step){ log(std::format("[{}] {}: {} through the door (timeout {} ms, dark from {} ms): {}.", m_row, step.name, direction_name(direction), door.timeout.count(), door.dark_from.count(), seen.any() ? describe(seen) : "nothing yet")); seen = door_from_beside(step, seen, door.timeout, banner); - m_io.settle(BUTTON_B, label_of(step), seen.any() ? SettleKind::TALK : SettleKind::AFTER_WARP, banner); + m_previous_settle_saw_dialogue = m_io.settle( + BUTTON_B, label_of(step), seen.any() ? SettleKind::TALK : SettleKind::AFTER_WARP, banner + ); return Pokemon::outcome_ok(); } return one_step(state, step); @@ -730,12 +743,18 @@ Outcome StoryWalkSteps::talk(RunState& state, const StoryStep& step){ Outcome StoryWalkSteps::mash(RunState& state, const StoryStep& step){ m_warp = {}; m_scene_open = false; - // The step before may have played the scene already (a door step settles - // its arrival scene), so its first line is not required. - if (!m_route.dialog_within(15000ms, COLOR_CYAN)){ - m_route.note(label_of(step) + ": no line within 15 s; the step before played it.", RouteNote::WARNING); - } - m_io.settle(step.action == "a" ? BUTTON_A : BUTTON_B, label_of(step), + const bool already_played = std::exchange(m_scene_start_already_played, false); + if (already_played){ + m_route.note(std::format( + "{}: dialogue was played by the previous step; skipping the opening-line wait.", label_of(step) + ), RouteNote::STATUS); + }else if (!m_route.dialog_within(SCENE_START_WAIT, COLOR_CYAN)){ + m_route.note(std::format( + "{}: no line within {} ms; the step before may already have played it.", + label_of(step), SCENE_START_WAIT.count() + ), RouteNote::WARNING); + } + m_previous_settle_saw_dialogue = m_io.settle(step.action == "a" ? BUTTON_A : BUTTON_B, label_of(step), step.pose_after && Pokemon::grid_pose_of(*step.pose_after).map != state.pose.map && story_plan_world(state.pose.map, Pokemon::grid_pose_of(*step.pose_after).map) == nullptr ? SettleKind::AFTER_WARP : SettleKind::TALK, ""); if (m_row == RUNNING_SHOES_ROW && step.name == RUNNING_SHOES_STEP){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h index 9f52467e64..45ed00b65b 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/PokemonBDSP_AutoStory_StoryWalkSteps.h @@ -103,6 +103,10 @@ class StoryWalkSteps{ Pokemon::Outcome mash(Pokemon::RunState& state, const Pokemon::StoryStep& step); private: + friend class StoryEngineSteps; + // Starts one engine-dispatched step. Only an immediately following + // MASH may reuse dialogue observed by the preceding step's settle(). + void begin_step(const Pokemon::StoryStep& step); Pokemon::Pose walk_to(const Pokemon::Pose& from, const Pokemon::Pose& goal, const Pokemon::StoryStep& step); // After a wild battle on `leg`'s taps from `starts`: walks to one of // `goals` from wherever the leg could have left us, then faces `facing`. @@ -141,6 +145,10 @@ class StoryWalkSteps{ // The last route ended in the scene of the step after it, or after the // plain step onto its trigger (RouteEnd::scene); that step is not taken. bool m_scene_open = false; + // The step just completed observed dialogue while settling. + bool m_previous_settle_saw_dialogue = false; + // The MASH now running immediately follows that settle. + bool m_scene_start_already_played = false; }; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp index 48ed98935e..897c53de42 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_GridSim_Tests.cpp @@ -448,10 +448,10 @@ class WalkStepsIO : public StoryStepIO{ unsupported("taps to " + map + "'s banner"); return std::nullopt; } - virtual void settle(Button, const std::string& label, SettleKind, const std::string&) override{ + virtual bool settle(Button, const std::string& label, SettleKind, const std::string&) override{ unsupported(label + "'s settle"); + return false; } - private: void unsupported(const std::string& what){ m_route.note(what + " is not simulated here.", RouteNote::FAILURE); diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp index 131924750b..4482ec365f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_SimFailureCases_Tests.cpp @@ -245,6 +245,23 @@ UnitTestResult roark_speaks_late(Logger& logger){ return passes(run_case(logger, "01.03", "01.03", {.fade = {}, .banner = {}}, {.first_line = {{talk, 7000ms}}})); } +// A MASH scene whose preceding step did not observe dialogue keeps the +// original 15 s opening-line watch. Both delays are therefore handled by +// the real mash/settle path, rather than silently walking on mid-scene. +UnitTestResult mash_scene_speaks_late(Logger& logger){ + const Milliseconds delays[] = {7000ms, 9000ms}; + for (Milliseconds delay : delays){ + SimWorldKnobs knobs; + knobs.first_line["barry_warns_us"] = delay; + knobs.scenes["barry_warns_us"] = {sim_line("Barry warns the player")}; + const std::string stopped = run_case(logger, "00.01", "00.01", {.fade = {}, .banner = {}}, knobs); + if (!stopped.empty()){ + return std::format("The scene with its first line at {} ms {}", delay.count(), stopped); + } + } + return true; +} + // `row` with everyone on its maps walking their wander boxes, seeds 1 to 8. UnitTestResult wanderers_move(Logger& logger, const std::string& row){ std::vector failed; @@ -669,6 +686,7 @@ const FailureCase AUTO_STORY_FAILURE_CASES[] = { {.input = {"any one fade with its hold unseen from 00.00 to 01.11: every row still passes", every_single_hold_miss}, .expectation = {}}, {.input = {"01.02 to 01.05 at night, the Gate's dim floor", night_and_dim_floors}, .expectation = {}}, {.input = {"01.03 Roark's first line 7 s after the A press", roark_speaks_late}, .expectation = {}}, + {.input = {"00.01 MASH first lines at 7 s and 9 s", mash_scene_speaks_late}, .expectation = {}}, {.input = {"01.03 with Oreburgh's people wandering (the pokefan on 45, 37..39)", [](Logger& logger){ return wanderers_move(logger, "01.03"); }}, .expectation = {}}, diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp index 95bc677c9a..43d38e7c57 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StoryActions_Tests.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Common/Cpp/CancellableScope.h" @@ -212,6 +213,122 @@ UnitTestResult test_stubbed_steps_need_the_console(Logger& logger){ return true; } +// Every authored scene keeps the full opening-line guard unless the +// preceding settle actually observed dialogue. +UnitTestResult test_scene_start_waits(Logger& logger){ + constexpr std::string_view LAST_ROW = "01.11"; + constexpr size_t EXPECTED_SCENES = 23; + // The default grid naturally plays Barry's doorway scene. The two + // other measured doorway scenes are supplied by the test below. + constexpr std::string_view DEFAULT_SKIP = "barry_goes_home"; + SimExecutor sim(logger); + run_rows_on(sim, logger, {.from = "00.00", .to = std::string(LAST_ROW)}); + if (!sim.result().error.empty()){ + return std::format("The run failed: {}", sim.result().error); + } + size_t scenes = 0; + size_t guarded = 0; + for (const auto& [row, step] : story_steps()){ + if (row > LAST_ROW || step.kind != StoryStepKind::MASH){ + continue; + } + const auto found = sim.misses().dialog_waits.find(step.name); + const bool should_skip = step.name == DEFAULT_SKIP; + if (found == sim.misses().dialog_waits.end()){ + if (!should_skip){ + return std::format("{} {} skipped its opening-line guard without preceding dialogue.", row, step.name); + } + }else{ + if (should_skip){ + return std::format("{} {} repeated its opening-line guard after preceding dialogue.", row, step.name); + } + // Mirrors SCENE_START_WAIT in StoryWalkSteps.cpp. + if (found->second != Milliseconds(15000)){ + return std::format("{} {} waits {} ms for its opening line; expected the full 15000 ms guard.", + row, step.name, found->second.count()); + } + guarded++; + } + scenes++; + } + if (scenes != EXPECTED_SCENES){ + return std::format("Audited {} scene steps through {}; expected {}.", scenes, LAST_ROW, EXPECTED_SCENES); + } + if (guarded + 1 != scenes){ + return std::format("{} of {} scenes kept the guard; expected one exact skip.", guarded, scenes); + } + logger.log(std::format("{} scene steps audited through {}; {} used the full opening-line guard.", scenes, LAST_ROW, guarded)); + return true; +} + +// Console B, 2026-11-15: these arrival scenes had already been played by +// the preceding settle, so their separate 15 s waits did no work. +UnitTestResult test_seen_scene_skips_start_wait(Logger& logger){ + SimWorldKnobs knobs; + knobs.scenes["down_the_stairs"] = {sim_line("Mom calls the player")}; + knobs.scenes["barry_bursts_out"] = {sim_line("Barry goes home")}; + knobs.scenes["up_barrys_stairs"] = {sim_line("Barry runs to the lake")}; + SimExecutor sim(logger, /*misses=*/{}, knobs); + run_rows_on(sim, logger, {.from = "00.01", .to = "00.01"}); + if (!sim.result().error.empty()){ + return std::format("The run failed: {}", sim.result().error); + } + const std::pair expected_skips[] = { + {"down_the_stairs", "mom_calls"}, + {"barry_bursts_out", "barry_goes_home"}, + {"up_barrys_stairs", "barry_runs_to_the_lake"}, + }; + for (const auto& [previous, scene] : expected_skips){ + if (sim.misses().dialog_waits.contains(std::string(scene))){ + return std::format("{} repeated an opening-line wait after {} already observed dialogue.", scene, previous); + } + } + return true; +} + +// Dialogue seen before an intervening TALK or BATTLE belongs to the step +// before that action, not to the following scene. +UnitTestResult test_intervening_step_keeps_start_wait(Logger& logger){ + const StoryStepKind intervening_kinds[] = {StoryStepKind::TALK, StoryStepKind::BATTLE}; + for (StoryStepKind kind : intervening_kinds){ + GridSim grid( + {.world = story_plan_world("TwinleafTown", "TwinleafTown")}, + /*start=*/{0, 0}, Pokemon::Direction::NORTH, /*can_run=*/false + ); + std::vector notes; + SimMisses misses; + const std::string io_step = "scene"; + SimWorldKnobs knobs; + knobs.first_line[io_step] = Milliseconds(0); + knobs.scenes[io_step] = {sim_line("the scene's line")}; + SimStoryStepIO io(grid, notes, misses, knobs, io_step, []{ return false; }, [](const Pokemon::Pose&){}); + StoryWalkSteps walks(io); + CancellableHolder scope; + StoryLogBackend backend(Pokemon::StoryBackendKind::SCRIPT, /*log=*/{}, scope); + StoryBoundActions actions = naming_actions(); + actions.battle = [](const StoryStep&, RunState&){ return Pokemon::outcome_ok(); }; + StoryEngineSteps engine(walks, actions, backend); + RunState state; + + StoryStep preceding{.kind = StoryStepKind::MASH, .name = "preceding_scene"}; + engine.act(preceding, state); + misses.dialog_waits.clear(); + StoryStep intervening{.kind = kind, .name = "intervening_step"}; + engine.act(intervening, state); + misses.dialog_waits.clear(); + StoryStep following{.kind = StoryStepKind::MASH, .name = "following_scene"}; + engine.act(following, state); + + const auto found = misses.dialog_waits.find(io_step); + if (found == misses.dialog_waits.end() || found->second != Milliseconds(15000)){ + return std::format("A scene after {} did not keep its full opening-line guard.", + Pokemon::story_step_kind_name(kind)); + } + } + logger.log("Scenes after TALK and BATTLE keep their full opening-line guard."); + return true; +} + struct AutoStoryStoryActionsInput{ @@ -261,6 +378,18 @@ const AutoStoryStoryActionsCase AUTO_STORY_STORY_ACTIONS_CASES[] = { .input = {"Story actions: the steps the sim stubs from 00.00 to 01.11 all need the console", test_stubbed_steps_need_the_console}, .expectation = {}, }, + { + .input = {"Story actions: scene starts retain the full guard until dialogue is observed", test_scene_start_waits}, + .expectation = {}, + }, + { + .input = {"Story actions: a scene played by the preceding settle skips its start wait", test_seen_scene_skips_start_wait}, + .expectation = {}, + }, + { + .input = {"Story actions: an intervening step invalidates a prior settle's dialogue", test_intervening_step_keeps_start_wait}, + .expectation = {}, + }, }; void add_tests_AutoStory_StoryActions(UnitTestDatabase& database){ diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp index ad14cd0c69..edafd70b5f 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.cpp @@ -134,21 +134,28 @@ std::string play_scene(const SimScene& scene, const TalkOverRule& rule){ } } // namespace detail -// Plays the step's scene knob through TalkOver if it has one. Without, a -// scene may take us to another map (Mom walks us out, 00.04): with a -// dialogue open the stub scene plays it. Otherwise only a door fades. -void SimStoryStepIO::settle(Button, const std::string& label, SettleKind kind, const std::string&){ +// Settles the simulated screen through the same TalkOver rules as the +// console. A scene knob supplies its beats; otherwise a dialogue may take +// us to another map (Mom walks us out, 00.04), or only a door fades. +bool SimStoryStepIO::settle(Button, const std::string& label, SettleKind kind, const std::string&){ const bool fade = kind == SettleKind::AFTER_WARP; m_misses.fading_in = false; + bool saw_dialogue = m_sim.player().dialog; + const Milliseconds uncovered = m_route.take_uncovered_first_line(); const auto scene = m_knobs.scenes.find(m_step); if (scene != m_knobs.scenes.end()){ + SimScene beats = scene->second; + if (uncovered > Milliseconds(0)){ + beats.insert(beats.begin(), sim_pause("the first line is still approaching", uncovered, true)); + } m_misses.fade_unseen = false; m_sim.end_dialogue(); - const std::string early = play_scene(scene->second, {.warp = fade, .door = kind == SettleKind::DOOR}); + saw_dialogue |= std::ranges::any_of(beats, [](const SimBeat& beat){ return beat.line; }); + const std::string early = play_scene(beats, {.warp = fade, .door = kind == SettleKind::DOOR}); if (!early.empty()){ m_route.note(label + ": talk-over " + early + ".", RouteNote::FAILURE); } - return; + return saw_dialogue; } const bool scene_fades = m_sim.player().dialog && m_far_side(); if (fade && ((!m_sim.player().warped && !scene_fades) || m_misses.fade_unseen)){ @@ -156,6 +163,7 @@ void SimStoryStepIO::settle(Button, const std::string& label, SettleKind kind, c } m_misses.fade_unseen = false; m_sim.end_dialogue(); + return saw_dialogue; } SimStoryStepIO::FadeShown SimStoryStepIO::next_fade(){ @@ -357,11 +365,20 @@ WalkResult SimKnobRouteIO::slide_on(const SimSlide& slide, WalkResult result){ } bool SimKnobRouteIO::dialog_within(Milliseconds wait, Color color){ + m_uncovered_first_line = Milliseconds(0); + Milliseconds& longest = m_misses.dialog_waits[m_step]; + longest = std::max(longest, wait); const auto late = m_knobs.first_line.find(m_step); if (late != m_knobs.first_line.end() && late->second > wait){ + m_uncovered_first_line = late->second - wait; note(std::format("[Sim] {}: the first line comes after {} ms, past the {} ms wait.", m_step, late->second.count(), wait.count()), RouteNote::LOG); return false; } + if (late != m_knobs.first_line.end()){ + // The knob is the current step's line; no map dialogue is needed. + m_uncovered_first_line = Milliseconds(0); + return true; + } return GridSimRouteIO::dialog_within(wait, color); } diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h index d1e5427a4b..cf218fe547 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_StorySim.h @@ -81,6 +81,7 @@ struct SimMisses{ bool fading_in = false; bool slid = false; std::string banner_up = {}; + std::map dialog_waits = {}; }; // How a warp's fade looks on the screen (#268's design note); the @@ -167,7 +168,8 @@ struct SimWorldKnobs{ std::map fade_into = {}; // People with a wander box on their map walk it, a step tried every 2 s. std::optional wander_seed = std::nullopt; - // How long after the A press a talk's first line shows, by step name. + // How long after a step begins watching for dialogue its first line + // shows, by step name. std::map first_line = {}; // The scene a step's talk-over plays, by step name: a scene's or a // door's settle(), or the speeches after a battle step's battle. @@ -226,6 +228,9 @@ class SimKnobRouteIO : public GridSimRouteIO{ virtual WalkResult walk_to_wall(const RouteWallWalk& walk) override; virtual WalkResult walk_taps(Pokemon::Direction direction, size_t tiles, const std::string& label) override; virtual bool dialog_within(Milliseconds wait, Color color) override; + Milliseconds take_uncovered_first_line(){ + return std::exchange(m_uncovered_first_line, Milliseconds(0)); + } // With SimWorldKnobs::slow_fade_in, fails the move `label` if the last // warp is still fading in (SimMisses::fading_in). void check_fade_in(const std::string& label); @@ -241,6 +246,7 @@ class SimKnobRouteIO : public GridSimRouteIO{ const std::string& m_step; std::function m_cross; std::set m_slid_steps; + Milliseconds m_uncovered_first_line{0}; }; } // namespace detail @@ -359,7 +365,7 @@ class SimStoryStepIO : public StoryStepIO{ // run before looked for the banner (run_into_door()), so settle() sees // none here. With a scene in SimWorldKnobs::scenes, the program's // TalkOver plays it instead. - virtual void settle(Button, const std::string& label, SettleKind kind, const std::string&) override; + virtual bool settle(Button, const std::string& label, SettleKind kind, const std::string&) override; virtual std::optional tap_to_banner(Pokemon::Direction direction, const std::string& map, size_t max_taps) override; private: diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp index a5ae179f82..03540e9589 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/AutoStory/Simulation/PokemonBDSP_AutoStory_WallBattleSim_Tests.cpp @@ -177,8 +177,9 @@ class RouteOnlyStepIO : public StoryStepIO{ unsupported("taps to " + map + "'s banner"); return std::nullopt; } - virtual void settle(Button, const std::string& label, SettleKind, const std::string&) override{ + virtual bool settle(Button, const std::string& label, SettleKind, const std::string&) override{ unsupported(label + "'s settle"); + return false; } private: