Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ constexpr std::string_view WALK_UNTIL_SCENE = "scene";
constexpr std::pair<std::string_view, ScreenEvent> WALK_ENDS[] = {
{"fade_white", ScreenEvent::FADE_WHITE},
{"fade_black", ScreenEvent::FADE_BLACK},
{"dialog", ScreenEvent::DIALOG_BOX},
{"prompt", ScreenEvent::INTERACT_PROMPT},
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySignalProbe.h"
#include "PokemonLZA_AutoStoryLines.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -49,17 +50,31 @@ LinesPassed press_a_through_lines(PlzaBackend& backend, Milliseconds quiet, Mill
return passed;
}

namespace{

// The party bar or text on `look`, logged as the exit's "[Signals]" line.
bool out_of_menu(const ScreenObservation& look, PlzaBackend& backend, SignalProbe& signals){
signals.look(look);
const bool party_bar = look.events.has(ScreenEvent::PLAYER_CAN_MOVE);
if (!party_bar && !text_shows(look)){
return false;
}
backend.log_detail(signals.line("press_b_out_of_menu", "menu_exit", party_bar ? "party_bar" : "arrow"));
return true;
}

} // namespace

bool press_b_out_of_menu(PlzaBackend& backend, size_t presses){
SignalProbe signals;
for (size_t pressed = 0; pressed < presses; pressed++){
backend.scope().throw_if_cancelled();
const ScreenObservation look = backend.observe();
if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX)){
if (out_of_menu(backend.observe(), backend, signals)){
return true;
}
backend.press(BUTTON_B, /*hold=*/160ms, /*release=*/840ms);
}
const ScreenObservation look = backend.observe();
return look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || look.events.has(ScreenEvent::DIALOG_BOX);
return out_of_menu(backend.observe(), backend, signals);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
*/

#include <algorithm>
#include <format>
#include <string>
#include <string_view>
Expand All @@ -12,6 +13,7 @@
#include "Common/Cpp/TestRunners/UnitTest.h"
#include "Pokemon/AutoStory/Testing/Pokemon_StoryFakeBackend.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h"
#include "PokemonLZA_AutoStoryLines.h"
#include "PokemonLZA_AutoStoryLines_Tests.h"

Expand All @@ -35,7 +37,7 @@ ScreenObservation look(ScreenEvents events, std::vector<std::string_view> readin
}

ScreenObservation dialog(Milliseconds at){
return look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, at);
return look(/*events=*/{}, {ADVANCE_ARROW_READING}, at);
}

ScreenObservation card(Milliseconds at){
Expand Down Expand Up @@ -125,6 +127,8 @@ struct MenuCase{
struct Expectation{
bool out = false;
std::string inputs;
// The exit's "[Signals]" line; empty: not checked.
std::string signals = {};
};
Expectation expectation;
};
Expand All @@ -142,6 +146,17 @@ const MenuCase MENU_CASES[] = {
.input = {.looks = {nothing(Milliseconds(0)), dialog(Milliseconds(0))}, .presses = 5},
.expectation = {.out = true, .inputs = "press B"},
},
{
// The log analysis tells a signal never seen from one seen at 0 ms.
.name = "the exit's signals line shows a dash for each signal never seen",
.input = {.looks = {nothing(Milliseconds(0)), nothing(Milliseconds(1000)), party_bar(Milliseconds(2000))}, .presses = 5},
.expectation = {
.out = true,
.inputs = "press B, press B",
.signals = "[Signals] press_b_out_of_menu menu_exit by=party_bar after=2000 first: party_bar@2000 hint@- prompt@- "
"arrow@- shadow.dialog_box@- autosave@- banner@- choice@- fade@- last_busy@- shadow.dialog_box_at_decision=0",
},
},
{
.name = "still in the menu after the presses",
.input = {.looks = {nothing(Milliseconds(0))}, .presses = 2},
Expand All @@ -159,6 +174,10 @@ UnitTestResult check_menu(const MenuCase& test, CancellableScope& scope){
if (inputs != test.expectation.inputs){
return std::format("expected inputs \"{}\", got \"{}\"", test.expectation.inputs, inputs);
}
const std::vector<std::string>& lines = backend.lines();
if (!test.expectation.signals.empty() && std::find(lines.begin(), lines.end(), test.expectation.signals) == lines.end()){
return std::format("expected the line \"{}\" in:\n{}", test.expectation.signals, joined(lines));
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <optional>
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h"
#include "PokemonLZA_AutoStoryMarkerArrival.h"
#include "PokemonLZA_AutoStoryMarkerDistance.h"
#include "PokemonLZA_AutoStoryMarkerWalk.h"
Expand Down Expand Up @@ -77,7 +78,7 @@ class MarkerWalk{
private:
// A dialog not near the objective gets B (B cannot start a talk again).
bool at_objective(const ScreenObservation& look, const std::optional<ObjectiveMarker>& marker){
const bool prompt = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX);
const bool prompt = look.events.has(ScreenEvent::INTERACT_PROMPT) || text_shows(look);
const std::optional<int> read = marker ? marker->distance : std::nullopt;
on_read(read, m_arrival.look(MarkerLook{
.distance = read, .marker_shows = marker.has_value(), .prompt_or_dialog = prompt, .at = current_time(),
Expand All @@ -94,7 +95,7 @@ class MarkerWalk{
));
}
m_prompt_passed = prompt;
if (look.events.has(ScreenEvent::DIALOG_BOX)){
if (text_shows(look)){
pbf_press_button(m_env.context, BUTTON_B, /*hold=*/160ms, /*release=*/240ms);
}
return false;
Expand Down Expand Up @@ -176,7 +177,7 @@ bool objective_reached(StoryEnv& env, PlzaBackend& backend, MarkerArrival& arriv
arrival.look(MarkerLook{
.distance = std::nullopt,
.marker_shows = marker,
.prompt_or_dialog = look.events.has(ScreenEvent::INTERACT_PROMPT) || look.events.has(ScreenEvent::DIALOG_BOX),
.prompt_or_dialog = look.events.has(ScreenEvent::INTERACT_PROMPT) || text_shows(look),
.at = current_time(),
});
if (arrival.arrived()){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryMenuReadings.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySignalProbe.h"
#include "PokemonLZA_AutoStoryMenuSave.h"

namespace PokemonAutomation{
Expand Down Expand Up @@ -70,7 +71,7 @@
if (look.events.has(ScreenEvent::CHOICE_BOX)){
return MenuSaveScreen::CHOICE;
}
if (look.events.has(ScreenEvent::DIALOG_BOX)){
if (has_reading(look, ADVANCE_ARROW_READING)){
return MenuSaveScreen::TEXT;
}
if (has_reading(look, MENU_SAVE_BUTTON_READING)){
Expand All @@ -79,8 +80,8 @@
if (has_reading(look, MAIN_MENU_READING)){
return MenuSaveScreen::MAIN_MENU;
}
// 01.01's platform shows no party bar.
if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || has_reading(look, PLATFORM_READING)){
// 01.01's platform shows no party bar, only the controls hint.
if (look.events.has(ScreenEvent::PLAYER_CAN_MOVE) || has_reading(look, CONTROLS_HINT_READING)){
return MenuSaveScreen::OVERWORLD;
}
return MenuSaveScreen::UNKNOWN;
Expand Down Expand Up @@ -176,14 +177,16 @@
}

// B until the overworld holds; B on the overworld does nothing.
std::optional<Pokemon::StoryFailure> back_to_overworld(PlzaBackend& backend){

Check warning on line 180 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMenuSave.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::back_to_overworld( PlzaBackend & backend): function_lines=35 (advisory above 25, limit 50)
// The looks also bound the step: a script's time can stand still.
const size_t max_looks = MENU_SAVE_LIMIT / MENU_SAVE_POLL + 1;
std::optional<WallClock> start;
size_t overworld_looks = 0;
SignalProbe signals;
for (size_t looks = 1; ; looks++){
backend.scope().throw_if_cancelled();
const ScreenObservation look = backend.observe();
signals.look(look);
if (look.events.has(ScreenEvent::SWITCH_HOME_MENU)){
return Pokemon::game_not_running_failure();
}
Expand All @@ -202,6 +205,8 @@
overworld_looks++;
if (overworld_looks >= MENU_SAVE_HELD_LOOKS){
backend.log(std::format("[PLZA set_text_speed_fast] text speed set, back on the overworld at {:.1f}s", now.count() / 1000.0));
const bool party_bar = look.events.has(ScreenEvent::PLAYER_CAN_MOVE);
backend.log_detail(signals.line("set_text_speed_fast", "text_speed_exit", party_bar ? "party_bar" : "hint"));
return std::nullopt;
}
backend.wait(MENU_SAVE_POLL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ std::optional<Pokemon::StoryFailure> save_from_menu(PlzaBackend& backend);
// PlzaAction::SET_TEXT_SPEED_FAST: X, 2 s for the menu, A on the first
// menu's tutorial card while it shows (1.5 s apart), then blind presses to
// the options' text speed and Fast (L, Up, A, Down, Right), then B until
// the overworld or the platform shows on two looks in a row. Fails as
// the party bar or the controls hint shows on two looks in a row. Fails as
// "text_speed_timeout" when not back within 30 s, and as GAME_NOT_RUNNING
// on the Switch's HOME.
std::optional<Pokemon::StoryFailure> set_text_speed_fast(PlzaBackend& backend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ ScreenObservation card(Milliseconds at){
}

ScreenObservation text(Milliseconds at){
return look({ScreenEvent::DIALOG_BOX}, /*readings=*/{}, at);
return look(/*events=*/{}, {ADVANCE_ARROW_READING}, at);
}

ScreenObservation main_menu(Milliseconds at){
Expand All @@ -57,15 +57,16 @@ ScreenObservation save_button(Milliseconds at){
}

ScreenObservation choice(Milliseconds at){
return look({ScreenEvent::DIALOG_BOX, ScreenEvent::CHOICE_BOX}, /*readings=*/{}, at);
return look({ScreenEvent::CHOICE_BOX}, /*readings=*/{}, at);
}

ScreenObservation home(Milliseconds at){
return look({ScreenEvent::SWITCH_HOME_MENU}, /*readings=*/{}, at);
}

// 01.01's platform: the controls hint, no party bar.
ScreenObservation platform(Milliseconds at){
return look(/*events=*/{}, {PLATFORM_READING}, at);
return look(/*events=*/{}, {CONTROLS_HINT_READING}, at);
}

// No detector reads the options screens.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <format>
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStorySignalProbe.h"
#include "PokemonLZA_AutoStoryPlatformWait.h"

namespace PokemonAutomation{
Expand All @@ -33,7 +34,8 @@
std::optional<WallClock> faded_in = std::nullopt;
};

// True once the platform has held PLATFORM_LOOKS looks.
// True once the platform's controls hint ("(L) Move") has held
// PLATFORM_LOOKS looks: the platform has no party bar.
bool platform_look(PlzaBackend& backend, const ScreenObservation& look, PlatformWait wait, PlatformProgress& progress){
if (look.readings.contains(AUTOSAVE_READING)){
if (!progress.autosave_seen){
Expand All @@ -44,13 +46,13 @@
return false;
}
const bool counts = wait == PlatformWait::PLATFORM_ONLY || progress.autosave_seen;
progress.streak = counts && look.readings.contains(PLATFORM_READING) ? progress.streak + 1 : 0;
progress.streak = counts && look.readings.contains(CONTROLS_HINT_READING) ? progress.streak + 1 : 0;
return progress.streak >= PLATFORM_LOOKS;
}

// The opening's last line ("Your train has arrived in Lumiose City!") waits for A.
bool text_waiting_for_a(const ScreenObservation& look){
return look.events.has(ScreenEvent::DIALOG_BOX) && look.readings.contains(ADVANCE_ARROW_READING);
return look.readings.contains(ADVANCE_ARROW_READING);
}

// A Plus held while the confirm screen still fades out does nothing (console
Expand All @@ -69,29 +71,32 @@
}

// Once per fade-in, while the opening plays. Plus on the platform opens the
// map, so none once the platform or the autosave before it shows.
// map, so none once its controls hint or the autosave before it shows.
bool opening_skip_due(const ScreenObservation& look, PlatformWait wait, const PlatformProgress& progress){
if (wait != PlatformWait::AFTER_AUTOSAVE || progress.autosave_seen || !progress.faded_in){
return false;
}
return look.when - *progress.faded_in >= OPENING_SKIP_AFTER_FADE_IN && !text_shows(look) &&
!look.readings.contains(PLATFORM_READING);
!look.readings.contains(CONTROLS_HINT_READING);
}

} // namespace


std::optional<StoryFailure> wait_for_platform(PlzaBackend& backend, Milliseconds timeout, PlatformWait wait){

Check warning on line 86 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryPlatformWait.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::wait_for_platform( PlzaBackend & backend , Milliseconds timeout , PlatformWait wait): function_lines=38 (advisory above 25, limit 50)
// The looks bound a script whose time stands still.
const size_t max_looks = (size_t)(timeout / PLATFORM_POLL) + 1;
std::optional<WallClock> start;
PlatformProgress progress;
SignalProbe signals;
for (size_t looks = 0; looks < max_looks; looks++){
backend.scope().throw_if_cancelled();
const ScreenObservation look = backend.observe();
signals.look(look);
start = start.value_or(look.when);
if (platform_look(backend, look, wait, progress)){
backend.log(std::format("[PLZA] platform wait: on the platform after {} looks", looks + 1));
backend.log_detail(signals.line("wait_for_platform", "platform", /*by=*/"hint"));
return std::nullopt;
}
if (look.when - *start >= timeout){
Expand All @@ -112,23 +117,11 @@
backend.wait(PLATFORM_POLL);
}
return StoryFailure{.code = "platform_not_seen", .detail = std::format(
"No platform on {} looks in a row within {} ms (autosave {}).",
"No controls hint on {} looks in a row within {} ms (autosave {}).",
PLATFORM_LOOKS, timeout.count(), progress.autosave_seen ? "seen" : "not seen"
)};
}

bool platform_holds_within(PlzaBackend& backend, size_t looks){
size_t streak = 0;
for (size_t look = 0; look < looks; look++){
backend.scope().throw_if_cancelled();
streak = backend.observe().readings.contains(PLATFORM_READING) ? streak + 1 : 0;
if (streak >= PLATFORM_LOOKS){
return true;
}
backend.wait(PLATFORM_POLL);
}
return false;
}



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#ifndef PokemonAutomation_PokemonLZA_AutoStoryPlatformWait_H
#define PokemonAutomation_PokemonLZA_AutoStoryPlatformWait_H

#include <cstddef>
#include <optional>
#include "Common/Cpp/Time.h"
#include "Pokemon/AutoStory/Pokemon_StoryFailure.h"
Expand All @@ -25,13 +24,12 @@ enum class PlatformWait{
AFTER_AUTOSAVE, // "opening_to_platform": the autosave badge first
};

// One A per text box with the advance arrow (A does not advance the
// cutscene). AFTER_AUTOSAVE: Plus held 0.75 s after each fade-in of the
// opening, until it skips to the platform. Fails as "platform_not_seen".
// The platform is its controls hint on two looks in a row. One A per
// advance arrow (A does not advance the cutscene). AFTER_AUTOSAVE: Plus held
// 0.75 s after each fade-in of the opening, until it skips to the platform.
// Fails as "platform_not_seen".
std::optional<Pokemon::StoryFailure> wait_for_platform(PlzaBackend& backend, Milliseconds timeout, PlatformWait wait);

// Whether the platform shows on two looks in a row within `looks` looks.
bool platform_holds_within(PlzaBackend& backend, size_t looks);



Expand Down
Loading
Loading