diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp new file mode 100644 index 0000000000..acbbeef087 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp @@ -0,0 +1,563 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include "CommonTools/ScreenMachine/ScreenMachine.h" +#include "PokemonLZA_AutoStorySceneMachine.h" +#include "PokemonLZA_AutoStoryFailures.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + +using namespace std::chrono_literals; +using EngineEvent = PokemonAutomation::ScreenEvent; + + +namespace{ + +// A party bar showing this long is the overworld: it also shows between +// lines of a talk. +constexpr Milliseconds PARTY_BAR_HOLD = 1000ms; +// The controls hint showing this long is free control: two looks or more. +constexpr Milliseconds CONTROLS_HINT_HOLD = 1000ms; +// The longest a fade is waited out in one step (02.07 holds a ~25 s black). +constexpr Milliseconds FADE_WAIT = 30000ms; +// The longest a still screen is waited on in one step. +constexpr Milliseconds STILL_WAIT = 10000ms; +// The longest a cutscene may hold one still frame. +constexpr Milliseconds SCENE_STILL_WAIT = 60000ms; +// How often blind_press_after presses A. +constexpr Milliseconds BLIND_PRESS_EVERY = 2000ms; +// How often a wait looks again. +constexpr Milliseconds POLL = 250ms; +// The longest the machine waits for the mission banner to go once it has +// ended the scene (it shows ~3 s). +constexpr Milliseconds BANNER_GONE_WAIT = 10000ms; +// A banner still up after BANNER_GONE_WAIT gets A this many times, this +// far apart, until it goes: 01.03's "MAIN MISSION 02" stayed past 10 s +//, where "MAIN MISSION 01" went in ~3 s. +constexpr size_t BANNER_PRESSES = 3; +constexpr Milliseconds BANNER_PRESS_EVERY = 1000ms; +// The autosave badge, then nothing busy this long, is control. +constexpr Milliseconds AUTOSAVE_QUIET = 5000ms; + +// Text to press on: a box (or choice), or the advance chevron without one. +bool text_shows(const ScreenObservation& look){ + return look.events.has(EngineEvent::DIALOG_BOX) || look.events.has(EngineEvent::CHOICE_BOX) || + look.readings.contains(ADVANCE_ARROW_READING); +} + +// Something holds the player: text, a battle, a fade, the mission +// complete card. +bool busy(const ScreenObservation& look){ + return text_shows(look) || look.events.has(EngineEvent::BATTLE_STARTED) || + look.events.has(EngineEvent::FADE_BLACK) || look.events.has(EngineEvent::FADE_WHITE) || + look.readings.contains(MISSION_COMPLETE_READING); +} + +// The mission banner, and not the complete card (which has its bar). +bool banner_shows(const ScreenObservation& look){ + return look.readings.contains(MISSION_BANNER_READING) && !look.readings.contains(MISSION_COMPLETE_READING); +} + +// Free control on screen: the controls hint, or an "A: Talk" prompt +// (01.02's battle area shows no controls hint). +bool control_shows(const ScreenObservation& look){ + return look.readings.contains(CONTROLS_HINT_READING) || look.events.has(EngineEvent::INTERACT_PROMPT); +} + +constexpr SceneScreen ALL_SCREENS[] = { + SceneScreen::GOAL_CHOICE, SceneScreen::GOAL_BATTLE, + SceneScreen::FADE_BLACK, SceneScreen::FADE_WHITE, + SceneScreen::CHOICE, SceneScreen::DIALOG, SceneScreen::MISSION_COMPLETE, + SceneScreen::MISSION_BANNER, SceneScreen::BANNER_SHOWING, + SceneScreen::CAN_MOVE, SceneScreen::PARTY_BAR, + SceneScreen::CONTROLS_HINT, SceneScreen::HINT_SHOWING, SceneScreen::QUIET_END, SceneScreen::AUTOSAVE_QUIET, + SceneScreen::BLIND_PRESS, + SceneScreen::SCENE_MOVING, SceneScreen::SCENE_STILL, +}; + +} // namespace + + +std::string_view scene_screen_name(SceneScreen screen){ + switch (screen){ + case SceneScreen::GOAL_CHOICE: + return "goal_choice"; + case SceneScreen::GOAL_BATTLE: + return "goal_battle"; + case SceneScreen::FADE_BLACK: + return "fade_black"; + case SceneScreen::FADE_WHITE: + return "fade_white"; + case SceneScreen::CHOICE: + return "choice"; + case SceneScreen::DIALOG: + return "dialog"; + case SceneScreen::MISSION_COMPLETE: + return "mission_complete"; + case SceneScreen::MISSION_BANNER: + return "mission_banner"; + case SceneScreen::BANNER_SHOWING: + return "banner_showing"; + case SceneScreen::CAN_MOVE: + return "can_move"; + case SceneScreen::PARTY_BAR: + return "party_bar"; + case SceneScreen::CONTROLS_HINT: + return "controls_hint"; + case SceneScreen::HINT_SHOWING: + return "hint_showing"; + case SceneScreen::QUIET_END: + return "quiet_end"; + case SceneScreen::AUTOSAVE_QUIET: + return "autosave_quiet"; + case SceneScreen::BLIND_PRESS: + return "blind_press"; + case SceneScreen::SCENE_MOVING: + return "scene_moving"; + case SceneScreen::SCENE_STILL: + return "scene_still"; + } + return "unknown"; +} + +bool scene_screen_terminal(SceneScreen screen){ + return screen == SceneScreen::GOAL_CHOICE || screen == SceneScreen::GOAL_BATTLE || + screen == SceneScreen::MISSION_BANNER || screen == SceneScreen::CAN_MOVE || + screen == SceneScreen::CONTROLS_HINT || screen == SceneScreen::QUIET_END || + screen == SceneScreen::AUTOSAVE_QUIET; +} + +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config){ + if (scene_screen_terminal(screen)){ + return SceneMashAction::DONE; + } + switch (screen){ + case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: + case SceneScreen::BLIND_PRESS: + return SceneMashAction::PRESS_A; + case SceneScreen::DIALOG: + return SceneMashAction::PRESS_TEXT; + case SceneScreen::SCENE_MOVING: + case SceneScreen::SCENE_STILL: + if (config.goal == SceneMashGoal::BATTLE){ + return SceneMashAction::PRESS_A; + } + return config.text_button == BUTTON_B ? SceneMashAction::PRESS_B : SceneMashAction::WAIT; + default: + return SceneMashAction::WAIT; + } +} + + +SceneTimeline::SceneTimeline(SceneMachineConfig config, WallClock start) + : m_config(std::move(config)) + , m_quiet_from(start + m_config.min_length) + , m_last_busy(start) + , m_last_cue(start) +{} + +std::optional SceneTimeline::quiet_end_due() const{ + if (m_config.goal != SceneMashGoal::OVERWORLD || !m_config.quiet){ + return std::nullopt; + } + return std::max(m_quiet_from, m_last_busy + *m_config.quiet); +} + +std::optional SceneTimeline::autosave_quiet_due() const{ + if (m_config.goal != SceneMashGoal::OVERWORLD || !m_last_autosave){ + return std::nullopt; + } + return std::max(m_last_busy, *m_last_autosave) + AUTOSAVE_QUIET; +} + +bool SceneTimeline::blind_press_due(WallClock now) const{ + if (!m_config.blind_press_after || m_config.goal != SceneMashGoal::OVERWORLD || m_config.text_button != BUTTON_A){ + return false; + } + if (now - m_last_cue < *m_config.blind_press_after){ + return false; + } + return !m_last_blind_press || now - *m_last_blind_press >= BLIND_PRESS_EVERY; +} + +SceneScreen SceneTimeline::classify(const ScreenObservation& look) const{ + const bool choice = look.events.has(EngineEvent::CHOICE_BOX); + if (m_config.goal == SceneMashGoal::CHOICE && choice){ + return SceneScreen::GOAL_CHOICE; + } + if (m_config.goal == SceneMashGoal::BATTLE && look.events.has(EngineEvent::BATTLE_STARTED)){ + return SceneScreen::GOAL_BATTLE; + } + if (look.events.has(EngineEvent::FADE_BLACK)){ + return SceneScreen::FADE_BLACK; + } + if (look.events.has(EngineEvent::FADE_WHITE)){ + return SceneScreen::FADE_WHITE; + } + if (choice){ + return SceneScreen::CHOICE; + } + if (text_shows(look)){ + return SceneScreen::DIALOG; + } + return classify_open(look); +} + +SceneScreen SceneTimeline::classify_open(const ScreenObservation& look) const{ + if (look.readings.contains(MISSION_COMPLETE_READING)){ + return SceneScreen::MISSION_COMPLETE; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && banner_shows(look)){ + return m_banner_looks >= 1 ? SceneScreen::MISSION_BANNER : SceneScreen::BANNER_SHOWING; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && look.events.has(EngineEvent::PLAYER_CAN_MOVE)){ + if (m_party_bar_since && look.when - *m_party_bar_since >= PARTY_BAR_HOLD){ + return SceneScreen::CAN_MOVE; + } + return SceneScreen::PARTY_BAR; + } + if (m_config.goal == SceneMashGoal::OVERWORLD && control_shows(look)){ + if (m_hint_since && look.when - *m_hint_since >= CONTROLS_HINT_HOLD){ + return SceneScreen::CONTROLS_HINT; + } + return SceneScreen::HINT_SHOWING; + } + const std::optional due = quiet_end_due(); + if (due && look.when >= *due && !busy(look)){ + return SceneScreen::QUIET_END; + } + const std::optional autosave_due = autosave_quiet_due(); + if (autosave_due && look.when >= *autosave_due && !busy(look)){ + return SceneScreen::AUTOSAVE_QUIET; + } + // Not at an interact prompt: A would talk to whoever it names. + if (blind_press_due(look.when) && !look.events.has(EngineEvent::INTERACT_PROMPT)){ + return SceneScreen::BLIND_PRESS; + } + return look.events.has(EngineEvent::SCREEN_STILL) ? SceneScreen::SCENE_STILL : SceneScreen::SCENE_MOVING; +} + +namespace{ + +// Starts `since` when `shows` starts, clears it when it stops. +void track_since(bool shows, WallClock when, std::optional& since){ + if (!shows){ + since.reset(); + }else if (!since){ + since = when; + } +} + +} // namespace + +void SceneTimeline::record(const ScreenObservation& look){ + if (classify(look) == SceneScreen::BLIND_PRESS){ + m_last_blind_press = look.when; + } + if (busy(look)){ + m_last_busy = look.when; + } + const bool box = look.events.has(EngineEvent::DIALOG_BOX) || look.events.has(EngineEvent::CHOICE_BOX); + track_since(look.events.has(EngineEvent::PLAYER_CAN_MOVE) && !box, look.when, m_party_bar_since); + track_since(control_shows(look) && !box, look.when, m_hint_since); + m_banner_looks = banner_shows(look) && !box ? m_banner_looks + 1 : 0; + const bool autosave = look.readings.contains(AUTOSAVE_READING); + if (autosave){ + m_last_autosave = look.when; + } + if (busy(look) || autosave || m_party_bar_since || m_hint_since || m_banner_looks > 0){ + m_last_cue = look.when; + } +} + + +namespace{ + +// "text box, choice": a look's events for the log. +std::string describe_look(const ScreenObservation& look){ + const std::pair names[] = { + {EngineEvent::FADE_BLACK, "black fade"}, {EngineEvent::FADE_WHITE, "white fade"}, + {EngineEvent::DIALOG_BOX, "text box"}, {EngineEvent::BATTLE_STARTED, "battle"}, + {EngineEvent::PLAYER_CAN_MOVE, "party bar"}, {EngineEvent::SCREEN_STILL, "still"}, + }; + std::string text; + for (const auto& [event, name] : names){ + if (look.events.has(event)){ + text += std::format("{}{}", text.empty() ? "" : ", ", name); + } + } + if (look.events.has(EngineEvent::CHOICE_BOX)){ + text += text.empty() ? "choice" : ", choice"; + } + if (look.readings.contains(CONTROLS_HINT_READING)){ + text += text.empty() ? "controls hint" : ", controls hint"; + } + if (look.readings.contains(ADVANCE_ARROW_READING)){ + text += text.empty() ? "advance arrow" : ", advance arrow"; + } + if (look.readings.contains(MISSION_BANNER_READING)){ + text += text.empty() ? "mission banner" : ", mission banner"; + } + if (look.readings.contains(MISSION_COMPLETE_READING)){ + text += text.empty() ? "mission complete" : ", mission complete"; + } + if (look.readings.contains(AUTOSAVE_READING)){ + text += text.empty() ? "autosave" : ", autosave"; + } + return text.empty() ? "nothing" : text; +} + +// What the states share: the backend, the timeline and the look they act on. +class SceneRunner{ +public: + SceneRunner(PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, WallClock start) + : m_backend(backend) + , m_label(label) + , m_timeline(config, start) + {} + + const SceneTimeline& timeline() const{ + return m_timeline; + } + + // The step of `screen`, the state of `look`. + void act(SceneScreen screen, const ScreenObservation& look){ + m_timeline.record(look); + const std::string events = describe_look(look); + if (screen != m_logged_screen || events != m_logged_events){ + m_logged_screen = screen; + m_logged_events = events; + m_backend.log(std::format("[PLZA {}] {}: {}", m_label, scene_screen_name(screen), events)); + } + switch (scene_screen_action(screen, m_timeline.config())){ + case SceneMashAction::DONE: + return; + case SceneMashAction::PRESS_TEXT: + m_backend.press(m_timeline.config().text_button, 160ms, 240ms); + return; + case SceneMashAction::PRESS_A: + m_backend.press(BUTTON_A, 160ms, 240ms); + return; + case SceneMashAction::PRESS_B: + m_backend.press(BUTTON_B, 160ms, 240ms); + return; + case SceneMashAction::WAIT: + wait(screen); + return; + } + } + +private: + void wait(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + wait_until(FADE_WAIT, [](const ScreenObservation& look){ + return !look.events.has(EngineEvent::FADE_BLACK) && !look.events.has(EngineEvent::FADE_WHITE); + }); + return; + case SceneScreen::SCENE_STILL: + wait_until(STILL_WAIT, [this](const ScreenObservation& look){ + const std::optional due = m_timeline.quiet_end_due(); + const std::optional autosave_due = m_timeline.autosave_quiet_due(); + return (due && look.when >= *due) || (autosave_due && look.when >= *autosave_due) || + m_timeline.blind_press_due(look.when) || !look.events.has(EngineEvent::SCREEN_STILL); + }); + return; + case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: + m_backend.wait(POLL); + return; + default: + m_backend.wait(2 * POLL); + return; + } + } + + // Looks every POLL until `done` or `limit`. + template + void wait_until(Milliseconds limit, Done&& done){ + for (Milliseconds waited = 0ms; waited < limit; waited += POLL){ + m_backend.wait(POLL); + if (done(m_backend.observe())){ + return; + } + } + } + + PlzaBackend& m_backend; + std::string m_label; + SceneTimeline m_timeline; + std::optional m_logged_screen; + std::string m_logged_events; +}; + +// How often a state may come up before the machine fails: a box or a +// choice that a press does not get past. +size_t max_visits(SceneScreen screen){ + switch (screen){ + case SceneScreen::DIALOG: + return 400; + case SceneScreen::CHOICE: + case SceneScreen::MISSION_COMPLETE: + return 30; + default: + return SIZE_MAX; + } +} + +// How long a state may see an unchanged screen: the waits (a fade, a +// still cutscene, the party bar held before it counts) for as long as +// they wait. Zero for the rest, whose presses must change the screen. +Milliseconds max_still(SceneScreen screen){ + switch (screen){ + case SceneScreen::FADE_BLACK: + case SceneScreen::FADE_WHITE: + return FADE_WAIT; + case SceneScreen::SCENE_STILL: + case SceneScreen::BLIND_PRESS: + // A blind A press need not change the screen. + return SCENE_STILL_WAIT; + case SceneScreen::PARTY_BAR: + case SceneScreen::HINT_SHOWING: + case SceneScreen::BANNER_SHOWING: + return STILL_WAIT; + default: + return Milliseconds::zero(); + } +} + +} // namespace + + +namespace{ + +// run_screen_machine_on_observations()'s failure as an Outcome. +Pokemon::Outcome scene_failure(std::string_view label, const std::string& failure){ + std::string_view kind = "scene_stuck"; + if (failure.starts_with("Did not finish")){ + kind = "scene_timeout"; + }else if (failure.starts_with("The screen matches none")){ + kind = "scene_unknown"; + } + return plza_failed(std::string(kind), std::format("{}: {}", label, failure)); +} + +// Whether a new look shows the mission banner. +bool banner_still_shows(PlzaBackend& backend){ + return backend.observe().readings.contains(MISSION_BANNER_READING); +} + +// After the scene ended on the mission banner: looks until it has gone, +// so the next step starts with control, for at most BANNER_GONE_WAIT. +// Whether it went. +bool wait_banner_gone(PlzaBackend& backend, std::string_view label){ + for (Milliseconds waited = 0ms; waited < BANNER_GONE_WAIT; waited += POLL){ + if (!banner_still_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} ms.", label, waited.count())); + return true; + } + backend.wait(POLL); + } + return false; +} + +// A banner that stayed past BANNER_GONE_WAIT: A up to BANNER_PRESSES +// times, BANNER_PRESS_EVERY apart, until it goes. +void press_banner_away(PlzaBackend& backend, std::string_view label){ + backend.log(std::format("[PLZA {}] the mission banner still shows after {} ms; pressing A.", label, BANNER_GONE_WAIT.count())); + for (size_t presses = 1; presses <= BANNER_PRESSES; presses++){ + backend.press(BUTTON_A, 160ms, 240ms); + backend.wait(BANNER_PRESS_EVERY); + if (!banner_still_shows(backend)){ + backend.log(std::format("[PLZA {}] the mission banner has gone after {} A presses.", label, presses)); + return; + } + } + backend.log(std::format("[PLZA {}] the mission banner still shows after {} A presses; going on.", label, BANNER_PRESSES)); +} + + +// The step's outcome from where the machine ended. +Pokemon::Outcome scene_outcome( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, const ScreenMachineResult& result +){ + if (!result.succeeded()){ + return scene_failure(label, result.failure); + } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END) && config.quiet_is_control){ + backend.log(std::format("[PLZA {}] done: quiet, taken as control.", label)); + return Pokemon::outcome_ok(); + } + if (result.terminal == scene_screen_name(SceneScreen::QUIET_END)){ + // Nothing busy for the quiet time, and no party bar or controls + // hint: a cutscene with no text, or control the detectors miss. + return plza_failed("scene_quiet", std::format( + "{}: nothing busy for {} ms and no sign of control.", label, config.quiet ? config.quiet->count() : 0 + )); + } + if (result.terminal == scene_screen_name(SceneScreen::MISSION_BANNER) && !wait_banner_gone(backend, label)){ + press_banner_away(backend, label); + } + backend.log(std::format("[PLZA {}] done on {}.", label, result.terminal)); + return Pokemon::outcome_ok(); +} + + +} // namespace + + +Pokemon::Outcome run_scene_machine( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +){ + // The look the states match and act on. + Pokemon::Observation look = backend.observe(); + bool first = true; + SceneRunner runner(backend, label, config, look.when); + std::vector states; + for (const SceneScreen screen : ALL_SCREENS){ + ScreenState state; + state.name = std::string(scene_screen_name(screen)); + state.matches_observation = [&runner, screen](const ScreenObservation& observation){ + return runner.timeline().classify(observation) == screen; + }; + state.act = [&runner, &look, screen]{ + runner.act(screen, look); + }; + state.max_visits = max_visits(screen); + state.max_still = max_still(screen); + state.terminal = scene_screen_terminal(screen); + states.push_back(std::move(state)); + } + const std::function observe = [&backend, &look, &first]() -> ScreenObservation{ + if (!first){ + look = backend.observe(); + } + first = false; + return look; + }; + StepTrace trace(std::format("PLZA {}", label), [&backend](const std::string& line){ + backend.log(line); + }); + const ScreenMachineResult result = run_screen_machine_on_observations(observe, backend.scope(), states, timeout, trace); + return scene_outcome(backend, label, config, result); +} + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation diff --git a/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h new file mode 100644 index 0000000000..d879b074f0 --- /dev/null +++ b/SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h @@ -0,0 +1,165 @@ +/* PokemonLZA AutoStory - Scene machine + * + * From: https://github.com/PokemonAutomation/ + * + * 01.00's scenes as ScreenMachine states (CommonTools/ScreenMachine) that + * match on what the backend observed (ScreenState::matches_observation), + * so they run on the console and on a scripted backend alike: every look + * picks one state, the state takes one step, and each change of state is + * logged with the look's events. The player has control when the party + * bar or, in the tutorial (which has no party bar), the controls hint + * (CONTROLS_HINT_READING) holds a second. A main mission's banner + * (MISSION_BANNER_READING) on two looks in a row ends the scene too: it + * shows as the scene hands over control, and the machine then waits for + * it to go, pressing A if it stays. The card a main mission ends with + * (MISSION_COMPLETE_READING) waits for A: the machine presses A at it, + * and it counts as busy. The advance chevron (ADVANCE_ARROW_READING) is + * text to press on, with or without a box. The autosave badge + * (AUTOSAVE_READING), then 5 s with nothing busy, ends the scene as + * control, before the minimum length: the game saves once a scene is over. + * + * The walks between the scenes are Actions/PokemonLZA_AutoStoryWalkScreenChecks.h. + * + */ + +#ifndef PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H +#define PokemonAutomation_PokemonLZA_AutoStorySceneMachine_H + +#include +#include +#include +#include "Common/Cpp/Time.h" +#include "CommonTools/ScreenMachine/ScreenObservation.h" +#include "NintendoSwitch/Controllers/NintendoSwitch_ControllerButtons.h" +#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h" +#include "PokemonLZA_AutoStoryReadings.h" +#include "PokemonLZA_AutoStoryBackend.h" +#include "PokemonLZA_AutoStoryScreenEvents.h" + +namespace PokemonAutomation{ +namespace NintendoSwitch{ +namespace PokemonLZA{ + + +// What a scene machine is after, and how it presses. +struct SceneMachineConfig{ + SceneMashGoal goal = SceneMashGoal::OVERWORLD; + // Advances the text (A or B). + Button text_button = BUTTON_A; + // OVERWORLD only: fails ("scene_quiet") once nothing busy (box, choice, + // battle, fade) and no sign of control has shown for this long... + std::optional quiet = std::nullopt; + // ...and this long has passed since the start: a cutscene can hold a + // still frame longer than `quiet`. + Milliseconds min_length = Milliseconds(0); + // The quiet end is control, not a failure: Hotel Z's halls and rooms + // show no party bar or prompt (02.05). + bool quiet_is_control = false; + // OVERWORLD with A: presses A every 2 s once nothing + // busy and no sign of control has shown for this long, for text the + // detectors miss (A does not skip a cutscene). + std::optional blind_press_after = std::nullopt; +}; + +// The states a scene machine tells apart, in the order it tries them. +enum class SceneScreen{ + GOAL_CHOICE, // terminal: a choice, when it is the goal + GOAL_BATTLE, // terminal: the battle HUD, when it is the goal + FADE_BLACK, // wait until it lifts + FADE_WHITE, // wait until it lifts + CHOICE, // A: the top choice + DIALOG, // the text button: a box, or the advance chevron + MISSION_COMPLETE, // A: a main mission's "COMPLETE" card + MISSION_BANNER, // terminal: a main mission's banner, on a second look + BANNER_SHOWING, // wait: the banner, on its first look + CAN_MOVE, // terminal: the party bar, held a second + PARTY_BAR, // wait: the party bar, not held a second yet + CONTROLS_HINT, // terminal: the tutorial's controls hint, held a second + HINT_SHOWING, // wait: the controls hint, not held a second yet + QUIET_END, // terminal, failing: nothing busy for the quiet time, past the minimum length + AUTOSAVE_QUIET, // terminal: the autosave badge, then nothing busy for AUTOSAVE_QUIET + BLIND_PRESS, // A: nothing busy and no sign of control for blind_press_after + SCENE_MOVING, // no box, the screen changing: a cutscene, or the player in control + SCENE_STILL, // no box, the screen still since the read before +}; + +// "fade_black", the state's name in the StepTrace log. +std::string_view scene_screen_name(SceneScreen screen); + +// Whether the machine ends after this state's step. +bool scene_screen_terminal(SceneScreen screen); + +// The press a state takes: DONE for a terminal state; WAIT for a fade, the +// party bar, and a screen with no box in an A mash (A would talk to whoever +// stands in front); PRESS_B there in a B mash (closes the map and Pokedex +// screens), PRESS_A when the goal is a battle (cards, the VS splash). +SceneMashAction scene_screen_action(SceneScreen screen, const SceneMachineConfig& config); + +// The time-dependent part of the machine, apart from the backend so tests +// can feed it looks: when the screen was last busy, since when the party +// bar shows. A look's time is its `when`. +class SceneTimeline{ +public: + SceneTimeline(SceneMachineConfig config, WallClock start); + + // The state for `look` (STILL: the screen barely changed since the + // look before). Does not change the timeline. + SceneScreen classify(const ScreenObservation& look) const; + + // Takes in the look the machine acted on. + void record(const ScreenObservation& look); + + // When the quiet end is due if nothing busy shows before; none + // without a quiet time. + std::optional quiet_end_due() const; + + // When the autosave quiet end is due if nothing busy shows before: + // AUTOSAVE_QUIET after the badge and after the last busy look, with no + // minimum length; none before the badge or outside an OVERWORLD mash. + std::optional autosave_quiet_due() const; + + // Whether a blind A press is due at `now` (blind_press_after). + bool blind_press_due(WallClock now) const; + + const SceneMachineConfig& config() const{ + return m_config; + } + +private: + SceneMachineConfig m_config; + WallClock m_quiet_from; + WallClock m_last_busy; + // The last look busy or with a sign of control (party bar, hint). + WallClock m_last_cue; + std::optional m_last_blind_press; + std::optional m_party_bar_since; + std::optional m_hint_since; + // The looks in a row with the mission banner. + size_t m_banner_looks = 0; + // The last look with the autosave badge. + std::optional m_last_autosave; + + // classify() past the fades, boxes and the goal. + SceneScreen classify_open(const ScreenObservation& look) const; +}; + + +// Presses through a scene until `config.goal` shows (OVERWORLD: the party +// bar or the controls hint held a second, or the mission banner on two +// looks, then up to 10 s for the banner to go and, if it stays, A up to 3 +// times 1 s apart), on run_screen_machine_on_observations(). +// `label` names its log lines ("[PLZA opening] dialog: text box"). Fails as +// "scene_quiet" on the quiet end, "scene_timeout" when `timeout` passes (by the looks' time), +// "scene_unknown" on a look no state takes and "scene_stuck" on a state +// that comes up too often or a screen that stops changing. +Pokemon::Outcome run_scene_machine( + PlzaBackend& backend, std::string_view label, const SceneMachineConfig& config, Milliseconds timeout +); + + + + +} // namespace PokemonLZA +} // namespace NintendoSwitch +} // namespace PokemonAutomation +#endif diff --git a/SerialPrograms/cmake/SourceFiles.cmake b/SerialPrograms/cmake/SourceFiles.cmake index c1c16db4b8..85e64e7e7b 100644 --- a/SerialPrograms/cmake/SourceFiles.cmake +++ b/SerialPrograms/cmake/SourceFiles.cmake @@ -2421,6 +2421,8 @@ file(GLOB LIBRARY_SOURCES Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures_Tests.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.cpp + Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.cpp Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents_Tests.cpp