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
2 changes: 2 additions & 0 deletions SerialPrograms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ add_library(SerialProgramsLib STATIC ${CORE_LIBRARY_SOURCES} ${LIBRARY_SOURCES})

# Data files built into the program (see cmake/EmbedTextFiles.cmake).
include(cmake/EmbedTextFiles.cmake)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/StoryData
VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryStoryFiles.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_checkpoint_json)
pa_embed_text_files(SerialProgramsLib Source/VideoGames/PokemonLZA/Programs/AutoStory/ReferenceData
VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryPlatformArrivalDetector.h PokemonAutomation::NintendoSwitch::PokemonLZA autostory_reference_json)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
#include "Programs/TestPrograms/PokemonLZA_MoveBoxArrow.h"
#include "Programs/TestPrograms/PokemonLZA_TestBoxCellInfo.h"
#include "InferenceTraining/PokemonLZA_GenerateLocationNameOCR.h"
#include "Programs/AutoStory/PokemonLZA_AutoStory.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -68,6 +69,7 @@ PanelListFactory::PanelListFactory()
)
{}

// code-health: allow function_lines -- the list reads top to bottom as the program menu does; splitting it by section hides that order
std::vector<PanelEntry> PanelListFactory::make_panels() const{
std::vector<PanelEntry> ret;

Expand Down Expand Up @@ -121,6 +123,7 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{

ret.emplace_back("---- Public Betas ----");
ret.emplace_back(make_SingleSwitchProgram<StatsReset>());
ret.emplace_back(make_SingleSwitchProgram<AutoStory>());

if (STATIC_GLOBALS.DEVELOPER_MODE){
ret.emplace_back("---- Developer Tools ----");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogDetector_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryGame_Tests.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand All @@ -47,6 +48,7 @@ void add_tests(UnitTestDatabase& database){
add_tests_AutoStoryDetectors(database);
add_tests_AutoStoryConfirmChoicesReader(database);
add_tests_AutoStoryDialogDetector(database);
add_tests_AutoStoryGame(database);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* PokemonLZA AutoStory - Confirm Choices
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <optional>
#include <string>
#include "Common/Cpp/Exceptions.h"
#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h"
#include "CommonFramework/Tools/DebugDumper.h"
#include "CommonFramework/VideoPipeline/VideoFeed.h"
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h"
#include "PokemonLZA_AutoStoryConfirmChoices.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


namespace{

// The "Confirm Choices" highlight on the screen now, reading a few frames
// over `window` with no press. Nullopt if none of them is the screen.
// `dump`: save the frame read to DebugDumps under that name.
std::optional<size_t> read_confirm_choices_now(
ConsoleHandle& console, ProControllerContext& context, Milliseconds window, const std::string& dump
){
const WallClock deadline = current_time() + window;
while (true){
context.wait_for_all_requests();
const VideoSnapshot frame = console.video().snapshot();
const std::optional<size_t> cursor = frame ? read_confirm_choices_cursor(frame) : std::nullopt;
if (cursor || current_time() >= deadline){
if (frame){
dump_debug_image(console, "PokemonLZA/AutoStory/Intro", dump, frame);
}
return cursor;
}
context.wait_for(250ms);
}
}

} // namespace


size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context){
const std::optional<size_t> cursor = read_confirm_choices_now(console, context, 2000ms, "confirm-choices");
if (!cursor){
throw UserSetupError(
console,
"AutoStory: 01.00 starts on the new game's \"Confirm Choices\" screen, with the language, look and name "
"already chosen by hand. It is not on screen; nothing was pressed."
);
}
console.log(std::format("AutoStory intro: Confirm Choices on screen, row {} highlighted.", *cursor));
return *cursor;
}

void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context){
std::optional<size_t> cursor = require_confirm_choices(console, context);
for (size_t press = 0; press < 6 && cursor; press++){
if (*cursor == CONFIRM_CHOICES_READY_ROW){
console.log("AutoStory intro: Confirm Choices: A on \"Ready!\".");
pbf_press_button(context, BUTTON_A, 160ms, 2000ms);
context.wait_for_all_requests();
return;
}
console.log("AutoStory intro: Confirm Choices: DOWN.");
pbf_press_dpad(context, DPAD_DOWN, 160ms, 500ms);
cursor = read_confirm_choices_now(console, context, 1000ms, "confirm-choices-moved");
if (cursor){
console.log(std::format("AutoStory intro: Confirm Choices: the highlight is on row {}.", *cursor));
}
}
OperationFailedExceptionWithScreenshot::fire(
ErrorReportMode::SEND_ERROR_REPORT,
"AutoStory intro: the Confirm Choices highlight was not read on \"Ready!\".",
console
);
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* PokemonLZA AutoStory - Confirm Choices
*
* From: https://github.com/PokemonAutomation/
*
* 01.00's start: the new game's "Confirm Choices" screen, after the
* language, look and name were chosen by hand. Its reader is in
* Detectors/PokemonLZA_AutoStoryConfirmChoicesReader.h.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H
#define PokemonAutomation_PokemonLZA_AutoStoryConfirmChoices_H

#include <cstddef>
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// The highlighted row of "Confirm Choices", read from frames over up to
// 2 s with no press. Throws UserSetupError if the screen does not show.
size_t require_confirm_choices(ConsoleHandle& console, ProControllerContext& context);

// 01.00's first step: the new game's language, look and name were chosen
// by hand and "Confirm Choices" is on screen. Checks that screen first
// (require_confirm_choices()): on any other screen throws UserSetupError
// and presses nothing. Then moves the highlight to "Ready!" one DOWN at a
// time, reading it after each, and presses A there: the save's only
// change is this press, on a screen it has read. Throws
// OperationFailedException if the highlight is lost on the way.
void confirm_new_game_choices(ConsoleHandle& console, ProControllerContext& context);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* PokemonLZA AutoStory - Moves
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <format>
#include <string>
#include <string_view>
#include "PokemonLZA_AutoStoryMoves.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;


namespace{

// Whether the screen on a fresh look has not changed since the look
// before (SCREEN_STILL): the input in between did nothing, a dropped turn
// or a dropped hold. A walk into a wall that already stops the player
// reads still too, and plays its leg twice.
bool screen_still(PlzaBackend& backend){
return backend.observe().events.has(ScreenEvent::SCREEN_STILL);
}

// A recorded turn: story_turn(), then L to recenter the camera, since the
// walk goes where it looks.
void turn_and_recenter(PlzaBackend& backend, double degrees){
story_turn(backend, degrees);
backend.press(BUTTON_L, 100ms, 150ms);
}

// `input`, played again once when the screen stays still after it.
template <typename Input>
void play_checked(PlzaBackend& backend, std::string_view what, Input&& input){
input();
if (screen_still(backend)){
backend.log(std::format("[PLZA] walk: the screen stayed still after {}; once more", what));
input();
}
}

} // namespace


std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg){
std::string label = std::format("leg {}/{} turn {:g}", index + 1, count, leg.turn);
if (leg.walk > Milliseconds(0)){
label += std::format(" walk {} ms", leg.walk.count());
}
return label;
}

Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move){
// The look the first input is checked against.
backend.observe();
for (size_t index = 0; index < move.legs.size(); index++){
const StoryWalkLeg& leg = move.legs[index];
const std::string label = story_walk_leg_label(index, move.legs.size(), leg);
if (leg.turn != 0){
play_checked(backend, std::format("the turn of {}", label), [&]{
turn_and_recenter(backend, leg.turn);
});
}
if (leg.walk > Milliseconds(0)){
play_checked(backend, std::format("the walk of {}", label), [&]{
backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK);
});
}
}
return Pokemon::outcome_ok();
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* PokemonLZA AutoStory - Moves
*
* From: https://github.com/PokemonAutomation/
*
* The story's walks in the overworld: recorded turns and timed walks.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryMoves_H
#define PokemonAutomation_PokemonLZA_AutoStoryMoves_H

#include <string>
#include <vector>
#include "Common/Cpp/Time.h"
#include "Pokemon/AutoStory/Pokemon_StoryOutcome.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryBackend.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{


// One leg of a walk: a turn, then a walk straight on.
struct StoryWalkLeg{
// Degrees clockwise from where the player faces (negative: left), as
// the story was recorded: L recenters the camera behind the player,
// the stick is tapped that way for 150 ms, and L again. The tap also
// takes a step. 0: no turn.
double turn = 0;

// How long to hold the stick forward after the turn, walking (not
// dashing). 0: the turn only.
Milliseconds walk = Milliseconds(0);
};

// A walk in the overworld. Lumiose has no tiles: a move is legs of turns
// and timed walks, as recorded on the console.
struct StoryMove{
std::vector<StoryWalkLeg> legs = {};
};

// From the overworld, walks as `move` says: each leg's turn (and L to
// recenter the camera, since the walk goes where it looks) and walk. A
// look after each turn and each walk; one that shows the screen still
// (the game dropped the input) plays that turn or walk once more, then
// goes on. A leg cut short or blocked part way goes unnoticed, and a step
// that needs a prompt after the walk fails there instead.
Pokemon::Outcome run_story_move(PlzaBackend& backend, const StoryMove& move);

// "leg 2/5 turn 90 walk 1200 ms": leg `index` (from 0) of `count`, for the
// log. A leg with no walk leaves it out.
std::string story_walk_leg_label(size_t index, size_t count, const StoryWalkLeg& leg);



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
#endif
Loading
Loading