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 @@ -22,11 +22,13 @@
#include "Inference/PokemonLZA_PartyBarDetector.h"
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryDialogs_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryConfirmChoicesReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDialogChoiceBox_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryDetectors_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryObjectiveMarkerReader_Tests.h"
#include "Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStorySceneMachine.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryScreenEvents.h"

Expand All @@ -36,7 +38,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 41 in SerialPrograms/Source/VideoGames/PokemonLZA/PokemonLZA_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::add_tests( UnitTestDatabase & database): function_lines=28 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -58,8 +60,10 @@
add_tests_AutoStoryConfirmChoicesReader(database);
add_tests_AutoStoryDialogChoiceBox(database);
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryZoneGateReader(database);
add_tests_AutoStoryFailures(database);
add_tests_AutoStoryDialogs(database);
add_tests_AutoStoryStartLoad(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,143 @@
/* PokemonLZA AutoStory - Walk to a wild zone's gate
*
* From: https://github.com/PokemonAutomation/
*
*/

#include <cmath>
#include <format>
#include <string>
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryZoneGateReader.h"
#include "PokemonLZA_AutoStoryGateWalk.h"
#include "PokemonLZA_AutoStoryMoves.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
namespace PokemonLZA{

using namespace std::chrono_literals;


namespace{

// One slice of the walk between looks, as walk_to_prompt()'s.
constexpr Milliseconds GATE_WALK_SLICE(250);
// Walking this long without a look at the gate fails the walk. From
// Wild Zone 1's catch spot at 60 the far gate is small at first: 1 s
// missed it once.
constexpr Milliseconds GATE_FIRST_SEEN_WITHIN(3000);
// The screen's width in degrees of view, for the steer: a guess.
constexpr double VIEW_DEGREES = 60;
// Closer to the middle than this, the gate is dead ahead.
constexpr double DEAD_AHEAD = 0.02;

// The gate's readings on a look, if any.
struct GateRead{
int x = 0;
int width = 0;
};

std::optional<GateRead> gate_on(const Pokemon::Observation<Pokemon::ZonePose>& look){
const auto x = look.readings.find(ZONE_GATE_X_READING);
const auto width = look.readings.find(ZONE_GATE_WIDTH_READING);
if (x == look.readings.end() || width == look.readings.end()){
return std::nullopt;
}
return GateRead{.x = x->second, .width = width->second};
}

// "gate on 3 of 5 looks, last at x = 0.520 width 0.150", or "no gate on 5 looks".
std::string gate_readings(size_t seen, size_t looks, const std::optional<GateRead>& last){
if (!last){
return std::format("no gate on {} looks", looks);
}
return std::format("gate on {} of {} looks, last at x = {:.3f} width {:.3f}", seen, looks, last->x / 1000.0, last->width / 1000.0);
}

// The stick's angle, degrees clockwise of straight on, toward a gate at
// `x` (0 to 1) on screen.
double gate_steer_degrees(double x){
const double off = x - 0.5;
if (std::abs(off) < DEAD_AHEAD){
return 0;
}
return std::round(off * VIEW_DEGREES);
}

} // namespace


PlzaReader zone_gate_reader(){
return [](PlzaBackend&, Pokemon::Observation<Pokemon::ZonePose>& look){
if (!look.frame || !look.frame->frame){
return;
}
const std::optional<ZoneGateSighting> gate = read_zone_gate(*look.frame->frame);
if (!gate){
return;
}
look.readings[std::string(ZONE_GATE_X_READING)] = (int)std::lround(gate->x * 1000);
look.readings[std::string(ZONE_GATE_WIDTH_READING)] = (int)std::lround(gate->width * 1000);
};
}

Pokemon::Outcome walk_to_zone_gate_entrance(

Check failure on line 85 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryGateWalk.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

PokemonAutomation::NintendoSwitch::PokemonLZA::walk_to_zone_gate_entrance( PlzaBackend & backend , const PlzaReader & heading , const PlzaReader & gate , const GateWalk & walk): function_lines=53, limit=50, allowed 50
PlzaBackend& backend, const PlzaReader& heading, const PlzaReader& gate, const GateWalk& walk
){
if (walk.heading){
const Pokemon::Outcome faced = face_heading(backend, heading, *walk.heading, 10.0);
if (!faced.succeeded()){
return faced;
}
}
Milliseconds walked(0);
double angle = 0;
size_t looks = 0;
size_t seen = 0;
std::optional<GateRead> last;
while (true){
backend.scope().throw_if_cancelled();
Pokemon::Observation<Pokemon::ZonePose> look = backend.observe();
if (gate){
gate(backend, look);
}
looks++;
if (look.events.has(PokemonAutomation::ScreenEvent::INTERACT_PROMPT)){
// The heading at the prompt: the street's way through the gate,
// for a plan's `heading` (none read: -1).
if (heading){
heading(backend, look);
}
backend.log(std::format(
"[PLZA] walk_to_zone_gate_entrance: the A prompt shows after {} ms ({}), heading {}",
walked.count(), gate_readings(seen, looks, last), Pokemon::story_heading(look).value_or(-1)
));
return Pokemon::outcome_ok();
}
const std::optional<GateRead> read = gate_on(look);
if (read){
seen++;
last = read;
angle = gate_steer_degrees(read->x / 1000.0);
}
if (!last && walked >= GATE_FIRST_SEEN_WITHIN){
return plza_failed("gate_not_seen", std::format(
"walk_to_zone_gate_entrance: {} in {} ms of walking", gate_readings(seen, looks, last), walked.count()
));
}
if (walked >= walk.timeout){
return plza_failed("no_prompt", std::format(
"walk_to_zone_gate_entrance: no A prompt in {} ms of walking, {}", walk.timeout.count(), gate_readings(seen, looks, last)
));
}
backend.stick(angle, GATE_WALK_SLICE, 0ms, Pokemon::StoryPace::WALK);
walked += GATE_WALK_SLICE;
}
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/* PokemonLZA AutoStory - Walk to a wild zone's gate
*
* From: https://github.com/PokemonAutomation/
*
* The walk up the street to a wild zone's entrance gate, steered by the
* gate's pillars on screen (read_zone_gate()) until the gate's A prompt
* shows: 03.06's walk into Wild Zone 1. Timed turns from the Pokemon
* Center ended pointing along the sidewalk, and a straight walk from
* there ran 10 s into the zone's wall.
*
*/

#ifndef PokemonAutomation_PokemonLZA_AutoStoryGateWalk_H
#define PokemonAutomation_PokemonLZA_AutoStoryGateWalk_H

#include <optional>
#include <string_view>
#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{


// The reading (ScreenObservation::readings) of a wild zone's entrance
// gate's middle on a look (read_zone_gate()), in thousandths of the
// screen's width; none when no gate shows.
inline constexpr std::string_view ZONE_GATE_X_READING = "walk.zone_gate_x";
// The reading of the gate's width, as ZONE_GATE_X_READING.
inline constexpr std::string_view ZONE_GATE_WIDTH_READING = "walk.zone_gate_width";

// Sets a look's ZONE_GATE_X_READING and ZONE_GATE_WIDTH_READING from its
// frame (read_zone_gate()); nothing on a look with no frame or no gate.
PlzaReader zone_gate_reader();

// What walk_to_zone_gate_entrance() does.
struct GateWalk{
// The compass heading to face first (face_heading(), by `heading`'s
// reader), along the street to the gate; none: no turn first.
std::optional<double> heading = std::nullopt;
// Walking this long with no A prompt fails the walk.
Milliseconds timeout = Milliseconds(15000);
};

// Faces `walk`'s heading, then walks in slices toward the gate, the stick
// turned toward it on each look the gate shows (`gate` sets its
// readings): 0 within 2% of the middle, else the gate's offset from the
// middle times the screen's ~60 degrees of view (a guess, NEEDS CONSOLE),
// rounded, 30 at most at the edges. On until the gate's A prompt (INTERACT_PROMPT) shows; none
// when it shows already. Once the gate is lost (too close: its pillars
// leave the screen), the last angle holds. Fails as "gate_not_seen" when
// the first second of looks shows no gate (the player faces away from
// it; walking on ran into the zone's wall), and as "no_prompt" after the
// timeout, each with how many looks saw the gate and its last reading.
Pokemon::Outcome walk_to_zone_gate_entrance(
PlzaBackend& backend, const PlzaReader& heading, const PlzaReader& gate, const GateWalk& walk
);



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