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,6 +22,8 @@
#include "Inference/PokemonLZA_PartyBarDetector.h"
#include "Inference/PokemonLZA_SelectionArrowDetector.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryFaultSweep_Tests.h"
#include "Programs/AutoStory/PokemonLZA_AutoStoryStartLoad_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchPlan_Tests.h"
#include "Programs/AutoStory/Actions/PokemonLZA_AutoStoryCatchStop_Tests.h"
Expand All @@ -42,7 +44,7 @@



void add_tests(UnitTestDatabase& database){

Check warning on line 47 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=35 (advisory above 25, limit 50)
add_tests_AlertEyeDetector(database);
add_tests_BoxCellInfoDetector(database);
add_tests_ButtonDetector(database);
Expand All @@ -67,6 +69,8 @@
add_tests_AutoStoryObjectiveMarkerReader(database);
add_tests_AutoStoryZoneGateReader(database);
add_tests_AutoStoryGateWalk(database);
add_tests_AutoStoryFailures(database);
add_tests_AutoStoryFaultSweep(database);
add_tests_AutoStoryCatchPlan(database);
add_tests_AutoStoryCatchStop(database);
add_tests_AutoStoryDialogs(database);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#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{
Expand Down Expand Up @@ -121,12 +122,12 @@ Pokemon::Outcome walk_to_zone_gate_entrance(
angle = gate_steer_degrees(read->x / 1000.0);
}
if (!last && walked >= GATE_FIRST_SEEN_WITHIN){
return Pokemon::outcome_failed("gate_not_seen", std::format(
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 Pokemon::outcome_failed("no_prompt", std::format(
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)
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <format>
#include <optional>
#include <string>
#include <string_view>
#include "Common/Cpp/Color.h"
#include "CommonFramework/Exceptions/OperationFailedExceptionWithScreenshot.h"
#include "CommonTools/Async/InferenceRoutines.h"
Expand All @@ -15,6 +16,7 @@
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_BasicNavigation.h"
#include "VideoGames/PokemonLZA/Programs/PokemonLZA_FastTravelNavigation.h"
#include "PokemonLZA_AutoStoryMoves.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -44,6 +46,32 @@
backend.scope().throw_if_cancelled();
}

// 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
// (a walk into the nurse's counter) reads still too, and plays its leg
// twice.
bool screen_still(PlzaBackend& backend){
return backend.observe().events.has(PokemonAutomation::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


Expand All @@ -54,10 +82,10 @@
now = read_heading(backend, reader);
}
if (!now){
return Pokemon::outcome_failed("heading_unread", "no look read the minimap's heading");
return plza_failed("heading_unread", "no look read the minimap's heading");
}
if (get_angle_between_facing_directions(*now, heading) > tolerance){
return Pokemon::outcome_failed(
return plza_failed(
"heading_missed",
std::format("facing {} deg, wanted {:.0f} +/- {:.0f}", *now, heading, tolerance)
);
Expand All @@ -77,7 +105,7 @@
return label;
}

Pokemon::Outcome run_story_move(

Check warning on line 108 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryMoves.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health (advisory)

PokemonAutomation::NintendoSwitch::PokemonLZA::run_story_move( PlzaBackend & backend , const StoryMove & move , const PlzaReader & heading , Pokemon :: StoryPausePoints * pause): function_lines=43 (advisory above 25, limit 50)
PlzaBackend& backend, const StoryMove& move, const PlzaReader& heading,
Pokemon::StoryPausePoints* pause
){
Expand All @@ -102,16 +130,20 @@
}
return Pokemon::outcome_ok();
}
// 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];
pause_before_leg(backend, pause, move, index);
if (leg.turn != 0){
// The walk goes where the camera looks: recenter it after the turn.
story_turn(backend, leg.turn);
backend.press(BUTTON_L, 100ms, 150ms);
play_checked(backend, std::format("the turn of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{
turn_and_recenter(backend, leg.turn);
});
}
if (leg.walk > Milliseconds(0)){
backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK);
play_checked(backend, std::format("the walk of {}", story_walk_leg_label(index, move.legs.size(), leg)), [&]{
backend.hold(Pokemon::Direction::NORTH, leg.walk, Pokemon::StoryPace::WALK);
});
}
}
return Pokemon::outcome_ok();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ Pokemon::Outcome face_heading(PlzaBackend& backend, const PlzaReader& reader, do

// From the overworld, walks as `move` says: faces its heading first, if it
// has one, then each leg's turn (and L to recenter the camera, since the
// walk goes where it looks) and walk. Nothing is read on the way: a leg
// that is cut short or blocked goes unnoticed, and a step that needs a
// prompt after the walk fails there instead. `heading` reads the compass
// walk goes where it looks) and walk. Not held: 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. `heading` reads the compass
// (face_heading()). `pause`, if given, is the run's pause points: each leg
// is a breakpoint (StoryPausePoints::before_leg(), story_walk_leg_label()).
Pokemon::Outcome run_story_move(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryReadings.h"
#include "PokemonLZA_AutoStoryDialogs.h"
#include "PokemonLZA_AutoStoryScenes.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -89,7 +90,7 @@ Pokemon::Outcome mash_scene(
log_event_change(backend, look.events, last_events);
const bool never_started = started->scene.never_started(look.events, look.when);
if (start_window && never_started){
return Pokemon::outcome_failed("scene_never_started", std::format(
return plza_failed("scene_never_started", std::format(
"mash_scene(): the scene never started (player not at its trigger?): no box, choice or battle in {} ms.",
start_window->count()
));
Expand All @@ -100,7 +101,7 @@ Pokemon::Outcome mash_scene(
}
press_for(backend, action, text_button);
}
return Pokemon::outcome_failed("scene_timeout", std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()));
return plza_failed("scene_timeout", std::format("mash_scene(): the goal did not show within {} ms.", timeout.count()));
}

Pokemon::Outcome mash_story_scene(PlzaBackend& backend, Button text_button, SceneOpening opening){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string>
#include <string_view>
#include "PokemonLZA_AutoStoryWalkScreenChecks.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -66,7 +67,7 @@ Pokemon::Outcome check_can_walk(PlzaBackend& backend, std::string_view walk){
}
backend.wait(FREE_CONTROL_LOOK);
}
return Pokemon::outcome_failed(
return plza_failed(
"walk_not_free",
std::format("{}: no free control within {} ms, last on {}", walk, FREE_CONTROL_WAIT.count(), busy)
);
Expand Down Expand Up @@ -109,7 +110,7 @@ Pokemon::Outcome walk_until(
return Pokemon::outcome_ok();
}
}
return Pokemon::outcome_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count()));
return plza_failed("walk_no_end", std::format("{}: no end event within {} ms of the last leg", walk, hold.count()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "VideoGames/PokemonLZA/Programs/AutoStory/Actions/PokemonLZA_AutoStoryActions.h"
#include "VideoGames/PokemonLZA/Programs/AutoStory/Detectors/PokemonLZA_AutoStoryTutorialCardDetector.h"
#include "PokemonLZA_AutoStoryBackend.h"
#include "PokemonLZA_AutoStoryFailures.h"

namespace PokemonAutomation{
namespace NintendoSwitch{
Expand Down Expand Up @@ -170,7 +171,7 @@ Pokemon::Outcome PlzaConsoleBackend::battle(const Pokemon::StoryActionArgs&){
}catch (Pokemon::StoryActionNotImplemented&){
throw;
}catch (OperationFailedException& e){
return Pokemon::outcome_failed("battle_failed", e.message());
return plza_failed("battle_failed", e.message());
}
return Pokemon::outcome_ok();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* PokemonLZA AutoStory - Engine Tests

Check failure on line 1 in SerialPrograms/Source/VideoGames/PokemonLZA/Programs/AutoStory/PokemonLZA_AutoStoryEngine_Tests.cpp

View workflow job for this annotation

GitHub Actions / code-health

Code health

<file>: file_lines=1108, limit=1000, allowed 1025
*
* From: https://github.com/PokemonAutomation/
*
Expand Down Expand Up @@ -180,10 +180,10 @@

// platform_to_plaza: the first leg, the turn (L, the stick tapped at 90
// degrees) and L again, then the last leg held in slices until the white
// fade.
// fade. Looks: 2 of free control, 3 around the legs' inputs, 1 per slice.
UnitTestResult test_walk_ends_on_white_fade(CancellableScope& scope){
FakeBackend backend = fake_plza({
look_at(0ms), look_at(150ms),
look_at(0ms), look_at(150ms), look_at(250ms), look_at(4750ms), look_at(5000ms),
look_at(12000ms), look_at(12250ms), look_at(12500ms, {EngineEvent::FADE_WHITE}),
}, scope);
const Pokemon::StoryEngineReport report = run_plza(backend, step_of("01.01", "platform_to_plaza"));
Expand Down Expand Up @@ -891,6 +891,87 @@
}


// The inputs of `backend` that start with `kind` ("hold", "press A").
size_t count_inputs(const FakeBackend& backend, std::string_view kind){
return (size_t)std::ranges::count_if(backend.inputs(), [kind](const std::string& input){ return input.starts_with(kind); });
}

// A walk's leg is played once more when the screen stays still after it
// (the game dropped the hold), and once when it moves.
UnitTestResult test_walk_plays_still_leg_again(CancellableScope& scope){
StoryCheckpoint row;
row.id = "03.06";
row.steps.push_back(StoryStep{.kind = Pokemon::StoryStepKind::GAME, .name = "center_to_zone_1", .why = "test", .action = "walk"});
row.steps.back().args["legs"] = std::vector<Pokemon::StoryTimedLeg>{{.turn = 0, .ms = 2000}};
{
FakeBackend backend = fake_plza({look_at(0ms)}, scope);
if (run_plza(backend, row).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "hold") != 1){
return std::format("Moving: expected one hold, got [{}].", joined(backend.inputs()));
}
}
FakeBackend backend = fake_plza({look_at(0ms, {EngineEvent::SCREEN_STILL})}, scope);
if (run_plza(backend, row).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "hold") != 2){
return std::format("Still: expected two holds, got [{}].", joined(backend.inputs()));
}
return true;
}

// A gate's "A Enter" alone in a checkpoint (mash_to_move_hints with
// press_a_first).
StoryCheckpoint enter_gate_row(){
StoryCheckpoint row;
row.id = "03.06";
row.steps.push_back(StoryStep{
.kind = Pokemon::StoryStepKind::GAME, .name = "enter_zone_1", .why = "test", .action = "mash_to_move_hints",
.args = {{"press_a_first", true}},
});
return row;
}

// Looks every 250 ms over each span: [from, to), with its events.
struct LookSpan{
Milliseconds from;
Milliseconds to;
std::initializer_list<EngineEvent> events;
};
std::vector<Observation<ZonePose>> looks_over(std::initializer_list<LookSpan> spans){
std::vector<Observation<ZonePose>> looks;
for (const LookSpan& span : spans){
for (Milliseconds at = span.from; at < span.to; at += 250ms){
looks.push_back(look_at(at, span.events));
}
}
return looks;
}

// press_a_first: the party bar under a gone prompt is not control before
// the gate's late fade (2 s after the A): the step waits the fade out.
UnitTestResult test_prompt_waits_for_late_fade(CancellableScope& scope){
FakeBackend backend = fake_plza(looks_over({
{0ms, 2000ms, {EngineEvent::PLAYER_CAN_MOVE}}, {2000ms, 3000ms, {EngineEvent::FADE_BLACK}},
{3000ms, 6000ms, {EngineEvent::PLAYER_CAN_MOVE}},
}), scope);
if (run_plza(backend, enter_gate_row()).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "press A") != 1){
return std::format("Expected a pass on one A, got [{}]: {}", joined(backend.inputs()), joined(backend.lines()));
}
if (line_index(backend.lines(), "mash_to_move_hints] fade_black") == backend.lines().size()){
return std::format("Expected the step to wait the fade out: {}", joined(backend.lines()));
}
return true;
}

// press_a_first: A again when the prompt stays with no fade (a dropped A).
UnitTestResult test_prompt_pressed_again_when_it_stays(CancellableScope& scope){
FakeBackend backend = fake_plza(looks_over({
{0ms, 3000ms, {EngineEvent::PLAYER_CAN_MOVE, EngineEvent::INTERACT_PROMPT}},
{3000ms, 4000ms, {EngineEvent::FADE_BLACK}}, {4000ms, 7000ms, {EngineEvent::PLAYER_CAN_MOVE}},
}), scope);
if (run_plza(backend, enter_gate_row()).verdict != Pokemon::CheckpointVerdict::PASSED || count_inputs(backend, "press A") != 2){
return std::format("Expected a pass on two A, got [{}]: {}", joined(backend.inputs()), joined(backend.lines()));
}
return true;
}

struct EngineTestCase{
std::string name;
struct Input{
Expand Down Expand Up @@ -951,6 +1032,9 @@
{.name = "reload_when_chained: \"always\" reloads straight through too", .input = {.body = test_straight_through_reloads_always}, .expectation = {}},
{.name = "walk_to_prompt: walks until the A prompt, none when it shows", .input = {.body = test_walk_to_prompt}, .expectation = {}},
{.name = "breakpoints: a walk pauses before each leg", .input = {.body = test_walk_pauses_before_each_leg}, .expectation = {}},
{.name = "walk: a leg the screen stays still after is played once more", .input = {.body = test_walk_plays_still_leg_again}, .expectation = {}},
{.name = "press_a_first: waits out a late fade, not done on the party bar", .input = {.body = test_prompt_waits_for_late_fade}, .expectation = {}},
{.name = "press_a_first: A again while the prompt stays with no fade", .input = {.body = test_prompt_pressed_again_when_it_stays}, .expectation = {}},
{.name = "03.10 to_lab_elevator: one look once the legs are in", .input = {.body = test_walk_looks_after_its_legs}, .expectation = {}},
{.name = "03.11 to_mable: the last leg held until Mable's dialog box", .input = {.body = test_walk_ends_in_mables_talk}, .expectation = {}},
{.name = "03.11 to_mable: a black fade in the last leg ends the walk", .input = {.body = test_walk_ends_on_black_fade}, .expectation = {}},
Expand Down Expand Up @@ -1019,7 +1103,6 @@
}



} // namespace PokemonLZA
} // namespace NintendoSwitch
} // namespace PokemonAutomation
Loading
Loading